From c56135b5353f0cd071c82a353c7eb656dff41c81 Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Tue, 10 Nov 2026 14:51:15 +0100 Subject: [PATCH 01/22] Shape, Shape_simple: use geometry-mesh-lib for the geometry file McStas Shape and Shape_simple and McXtrace Shape read and draw the geometry file with geometry-mesh-lib instead of interoff-lib: off_init becomes gmesh_init_fit (same resizing, center passed as is) and off_display becomes gmesh_display. PLY and STL files now work too; the docs say so, and that center centres the bounding box. Co-Authored-By: Claude Opus 5.5 --- mcstas-comps/misc/Shape.comp | 18 +++++++++--------- mcstas-comps/misc/Shape_simple.comp | 18 +++++++++--------- mcxtrace-comps/misc/Shape.comp | 18 +++++++++--------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/mcstas-comps/misc/Shape.comp b/mcstas-comps/misc/Shape.comp index 505b4e2170..7686620be2 100644 --- a/mcstas-comps/misc/Shape.comp +++ b/mcstas-comps/misc/Shape.comp @@ -25,12 +25,12 @@ * cylinder: radius x yheight (thickness=0) * hollow cylinder: radius x yheight and thickness>0 * sphere: radius (yheight=0 thickness=0) -* any shape: geometry=OFF file +* any shape: geometry=OFF, PLY or STL file * * The complex geometry option handles any closed non-convex polyhedra. * It computes the intersection points of the neutron ray with the object * transparently, so that it can be used like a regular sample object. -* It supports the OFF and NOFF file format but not COFF (colored faces). +* It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. * Such files may be generated from XYZ data using qhull/powercrust, and * viewed with geomview * The default size of the object depends of the OFF file data, but its @@ -46,8 +46,8 @@ * yheight: [m] Vert. dimension of sample (bounding box if off file), as a height. A sphere shape is used when 0 and radius is set * zdepth: [m] Depth of sample (bounding box if off file) * thickness: [m] Thickness of hollow box or cylinder (spheres are drawn solid) -* geometry: [str] Name of an Object File Format (OFF) file for complex geometry. The OFF file may be generated from XYZ coordinates using qhull/powercrust -* center: [1] Flag to determine if OFF object is centered on its centre of mass. +* geometry: [str] Name of an OFF, PLY or STL file for complex geometry. The OFF file may be generated from XYZ coordinates using qhull/powercrust +* center: [1] Flag to centre the bounding box of the mesh on the origin. * nx: [1] x-comp of orientation vector * ny: [1] y-comp of orientation vector * nz: [1] z-comp of orientation vector @@ -69,18 +69,18 @@ SETTING PARAMETERS (string geometry=0, radius=0, xwidth=0, yheight=0, zdepth=0, SHARE %{ %include "read_table-lib" -%include "interoff-lib" +%include "geometry-mesh-lib" %} DECLARE %{ - off_struct offdata; + gmesh_struct mesh; %} INITIALIZE %{ if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - if (off_init (geometry, xwidth, yheight, zdepth, !center, &offdata)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_IGNORE, &mesh)) { thickness = 0; } } @@ -93,8 +93,8 @@ TRACE %{ MCDISPLAY %{ - if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* OFF file */ - off_display (offdata); + if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* mesh file */ + gmesh_display (&mesh); } else if (radius > 0 && yheight) { /* cylinder along y*/ cylinder (0, 0, 0, radius, yheight, thickness, nx, ny, nz); } else if (xwidth && yheight) { /* box/rectangle */ diff --git a/mcstas-comps/misc/Shape_simple.comp b/mcstas-comps/misc/Shape_simple.comp index b216942726..a7ecdb0b14 100644 --- a/mcstas-comps/misc/Shape_simple.comp +++ b/mcstas-comps/misc/Shape_simple.comp @@ -26,12 +26,12 @@ * hollow cylinder: radius x yheight and thickness>0 * sphere: radius (yheight=0 thickness=0) * hollow sphere: radius and thickness>0 (yheight=0) -* any shape: geometry=OFF file +* any shape: geometry=OFF, PLY or STL file * * The complex geometry option handles any closed non-convex polyhedra. * It computes the intersection points of the neutron ray with the object * transparently, so that it can be used like a regular sample object. -* It supports the OFF and NOFF file format but not COFF (colored faces). +* It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. * Such files may be generated from XYZ data using qhull/powercrust, and * viewed with geomview * The default size of the object depends of the OFF file data, but its @@ -47,8 +47,8 @@ * yheight: [m] Vert. dimension of sample (bounding box if off file), as a height. A sphere shape is used when 0 and radius is set * zdepth: [m] Depth of sample (bounding box if off file) * thickness: [m] Thickness of hollow sample -* geometry: [str] Name of an Object File Format (OFF) file for complex geometry. The OFF file may be generated from XYZ coordinates using qhull/powercrust -* center: [1] Flag to determine if OFF object is centered on its centre of mass. +* geometry: [str] Name of an OFF, PLY or STL file for complex geometry. The OFF file may be generated from XYZ coordinates using qhull/powercrust +* center: [1] Flag to centre the bounding box of the mesh on the origin. * * %L * Geomview and Object File Format (OFF) @@ -67,18 +67,18 @@ SETTING PARAMETERS (string geometry=0, radius=0, xwidth=0, yheight=0, zdepth=0, SHARE %{ %include "read_table-lib" -%include "interoff-lib" +%include "geometry-mesh-lib" %} DECLARE %{ - off_struct offdata; + gmesh_struct mesh; %} INITIALIZE %{ if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - if (off_init (geometry, xwidth, yheight, zdepth, !center, &offdata)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_IGNORE, &mesh)) { thickness = 0; } } @@ -91,8 +91,8 @@ TRACE %{ MCDISPLAY %{ - if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* OFF file */ - off_display (offdata); + if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* mesh file */ + gmesh_display (&mesh); } else if (radius > 0 && yheight) { /* cylinder */ cylinder (0, 0, 0, radius, yheight, thickness, 0, 1, 0); } else if (xwidth && yheight) { /* box/rectangle XY */ diff --git a/mcxtrace-comps/misc/Shape.comp b/mcxtrace-comps/misc/Shape.comp index a8511c1f15..4c300b94b9 100644 --- a/mcxtrace-comps/misc/Shape.comp +++ b/mcxtrace-comps/misc/Shape.comp @@ -25,12 +25,12 @@ * cylinder: radius x yheight (thickness=0) * hollow cylinder: radius x yheight and thickness>0 * sphere: radius (yheight=0 thickness=0) -* any shape: geometry=OFF file +* any shape: geometry=OFF, PLY or STL file * * The complex geometry option handles any closed non-convex polyhedra. * It computes the intersection points of the X-ray with the object * transparently, so that it can be used like a regular sample object. -* It supports the OFF and NOFF file format but not COFF (colored faces). +* It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. * Such files may be generated from XYZ data using qhull/powercrust, and * viewed with geomview * The default size of the object depends of the OFF file data, but its @@ -46,8 +46,8 @@ * yheight: [m] Vert. dimension of sample (bounding box if off file), as a height. A sphere shape is used when 0 and radius is set * zdepth: [m] Depth of sample (bounding box if off file) * thickness: [m] Thickness of hollow box or cylinder (spheres are drawn solid) -* geometry: [str] Name of an Object File Format (OFF) file for complex geometry. The OFF file may be generated from XYZ coordinates using qhull/powercrust -* center: [1] Flag to determine if OFF object is centered on its centre of mass. +* geometry: [str] Name of an OFF, PLY or STL file for complex geometry. The OFF file may be generated from XYZ coordinates using qhull/powercrust +* center: [1] Flag to centre the bounding box of the mesh on the origin. * nx: [1] x-comp of orientation vector * ny: [1] y-comp of orientation vector * nz: [1] z-comp of orientation vector @@ -67,18 +67,18 @@ SETTING PARAMETERS (string geometry=0, radius=0, xwidth=0, yheight=0, zdepth=0, SHARE %{ %include "read_table-lib" -%include "interoff-lib" +%include "geometry-mesh-lib" %} DECLARE %{ - off_struct offdata; + gmesh_struct mesh; %} INITIALIZE %{ if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - if (off_init (geometry, xwidth, yheight, zdepth, !center, &offdata)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_IGNORE, &mesh)) { thickness = 0; } } @@ -91,8 +91,8 @@ TRACE %{ MCDISPLAY %{ - if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* OFF file */ - off_display (offdata); + if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* mesh file */ + gmesh_display (&mesh); } else if (radius > 0 && yheight) { /* cylinder along y*/ cylinder (0, 0, 0, radius, yheight, thickness, nx, ny, nz); } else if (xwidth && yheight) { /* box/rectangle */ From 597324a5a7f2318ff6c9776d19be5921797e663b Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Tue, 10 Nov 2026 15:35:52 +0100 Subject: [PATCH 02/22] geometry-mesh-lib: add gmesh_intersect_gravity for parabolic paths gmesh_intersect_gravity returns the hits of position + t*velocity + t^2/2*acceleration with the mesh, sorted in t, as gmesh_intersect does for a line. The BVH traversal is shared: gmesh_trace takes the acceleration and then uses a box test per axis (gmesh_slab_gravity) and the crossings of the parabola with each triangle plane (gmesh_tri_hit_gravity, up to two per triangle). in_out uses the velocity at the hit. With zero acceleration the straight-line code runs unchanged, and gmesh_intersect now calls gmesh_intersect_gravity. Co-Authored-By: Claude Opus 5.5 --- common/lib/share/geometry-mesh-lib.c | 293 ++++++++++++++++++++++----- common/lib/share/geometry-mesh-lib.h | 13 ++ 2 files changed, 250 insertions(+), 56 deletions(-) diff --git a/common/lib/share/geometry-mesh-lib.c b/common/lib/share/geometry-mesh-lib.c index b368b61a43..72f649e8b2 100644 --- a/common/lib/share/geometry-mesh-lib.c +++ b/common/lib/share/geometry-mesh-lib.c @@ -1845,6 +1845,158 @@ gmesh_slab (Coords lower, Coords upper, Coords origin, Coords direction, double return 1; } + // real roots of quadratic*t^2 + linear*t + constant = 0 in roots[0..1], + // sorted, solving the linear equation when quadratic is 0. Returns their number. + #pragma acc routine seq +static int +gmesh_quadratic_roots (double quadratic, double linear, double constant, double roots[2]) { + double discriminant; + double q; + if (quadratic == 0) { + if (linear == 0) + return 0; + roots[0] = -constant / linear; + return 1; + } + discriminant = linear * linear - 4 * quadratic * constant; + if (discriminant < 0) + return 0; + // q avoids the cancellation of -linear + sqrt(discriminant) + q = -0.5 * (linear + copysign (sqrt (discriminant), linear)); + if (q == 0) { + roots[0] = 0; + return 1; + } + roots[0] = q / quadratic; + roots[1] = constant / q; + if (roots[0] > roots[1]) { + double swap = roots[0]; + roots[0] = roots[1]; + roots[1] = swap; + } + return 2; +} + + // box test of the parabola origin + t*velocity + t^2/2*acceleration, for t in + // [t_start, t_end]. Each axis cuts the time range into pieces at the times + // the coordinate equals a box border; the range is narrowed to the hull of the + // pieces within the box. Returns 0 only when the parabola misses the box; it + // may return 1 for a box it misses, which only costs extra triangle tests. + #pragma acc routine seq +static int +gmesh_slab_gravity (Coords lower, Coords upper, Coords origin, Coords velocity, Coords acceleration, double t_start, double t_end) { + double origin_xyz[3] = { origin.x, origin.y, origin.z }; + double velocity_xyz[3] = { velocity.x, velocity.y, velocity.z }; + double acceleration_xyz[3] = { acceleration.x, acceleration.y, acceleration.z }; + double lower_xyz[3] = { lower.x, lower.y, lower.z }; + double upper_xyz[3] = { upper.x, upper.y, upper.z }; + int axis; + for (axis = 0; axis < 3; axis++) { + double low = lower_xyz[axis]; + double high = upper_xyz[axis]; + double pad = 1e-9 * (high - low) + 1e-12 * (fabs (low) + fabs (high)) + 1e-300; + double half_a = 0.5 * acceleration_xyz[axis]; + double v = velocity_xyz[axis]; + double o = origin_xyz[axis]; + double borders[2]; + double cuts[6]; + double roots[2]; + double inside_start = FLT_MAX; + double inside_end = -FLT_MAX; + int ncuts = 0; + int i_border; + int i_cut; + borders[0] = low - pad; + borders[1] = high + pad; + cuts[ncuts] = t_start; + ncuts++; + for (i_border = 0; i_border < 2; i_border++) { + int nroots = gmesh_quadratic_roots (half_a, v, o - borders[i_border], roots); + int i_root; + for (i_root = 0; i_root < nroots; i_root++) { + if (roots[i_root] > t_start && roots[i_root] < t_end) { + cuts[ncuts] = roots[i_root]; + ncuts++; + } + } + } + cuts[ncuts] = t_end; + ncuts++; + // insertion sort of the few cut times + for (i_cut = 1; i_cut < ncuts; i_cut++) { + double cut = cuts[i_cut]; + int position = i_cut; + while (position > 0 && cuts[position - 1] > cut) { + cuts[position] = cuts[position - 1]; + position--; + } + cuts[position] = cut; + } + // the coordinate stays on one side of each border within a piece, so its + // value in the middle tells whether the whole piece is within the box. + // That value is compared with a rounding margin, as a piece may be only a + // few ulp long, e.g. for a flat box. + for (i_cut = 0; i_cut + 1 < ncuts; i_cut++) { + double middle = 0.5 * cuts[i_cut] + 0.5 * cuts[i_cut + 1]; + double coordinate = o + (v + half_a * middle) * middle; + double margin = 1e-12 * (fabs (o) + fabs (v * middle) + fabs (half_a * middle * middle)); + if (coordinate >= borders[0] - margin && coordinate <= borders[1] + margin) { + if (cuts[i_cut] < inside_start) + inside_start = cuts[i_cut]; + if (cuts[i_cut + 1] > inside_end) + inside_end = cuts[i_cut + 1]; + } + } + if (inside_start > inside_end) + return 0; + t_start = inside_start; + t_end = inside_end; + } + return 1; +} + + // intersections of the parabola origin + t*velocity + t^2/2*acceleration with + // a triangle of mesh: the times it crosses the plane of the triangle, kept + // when the crossing point is within the triangle (same tolerance as + // gmesh_tri_hit). Returns their number (0 to 2), with the times in hit_t and + // on_edge = 1 for a crossing close to an edge. + #pragma acc routine seq +static int +gmesh_tri_hit_gravity (gmesh_struct* mesh, int triangle, Coords origin, Coords velocity, Coords acceleration, double hit_t[2], int on_edge[2]) { + Coords v0 = mesh->vtx[mesh->tri[3 * triangle]]; + Coords e1 = coords_sub (mesh->vtx[mesh->tri[3 * triangle + 1]], v0); + Coords e2 = coords_sub (mesh->vtx[mesh->tri[3 * triangle + 2]], v0); + Coords normal = mesh->tri_normal[triangle]; + double e1_e1 = coords_sp (e1, e1); + double e1_e2 = coords_sp (e1, e2); + double e2_e2 = coords_sp (e2, e2); + double denominator = e1_e1 * e2_e2 - e1_e2 * e1_e2; + double eps = GMESH_EPSILON; + double roots[2]; + int nroots; + int i_root; + int nhits = 0; + if (denominator <= 0) + return 0; + nroots = gmesh_quadratic_roots (0.5 * coords_sp (normal, acceleration), coords_sp (normal, velocity), coords_sp (normal, coords_sub (origin, v0)), roots); + for (i_root = 0; i_root < nroots; i_root++) { + double time = roots[i_root]; + Coords point = coords_add (origin, coords_add (coords_scale (velocity, time), coords_scale (acceleration, 0.5 * time * time))); + Coords relative = coords_sub (point, v0); + double r_e1 = coords_sp (relative, e1); + double r_e2 = coords_sp (relative, e2); + // barycentric coordinates along e1 (u) and e2 (v), as in gmesh_tri_hit + double u = (e2_e2 * r_e1 - e1_e2 * r_e2) / denominator; + double v = (e1_e1 * r_e2 - e1_e2 * r_e1) / denominator; + if (u < -eps || u > 1 + eps || v < -eps || u + v > 1 + eps) + continue; + hit_t[nhits] = time; + on_edge[nhits] = (u < eps || v < eps || u + v > 1 - eps); + nhits++; + } + return nhits; +} + // Moeller-Trumbore intersection of the line origin + t*direction with a // triangle of mesh, with tolerant barycentric coordinates. Returns 1 on a // hit, with its t in *hit_t and *on_edge = 1 when it is close to an edge. @@ -1884,7 +2036,9 @@ gmesh_tri_hit (gmesh_struct* mesh, int triangle, Coords origin, Coords direction return 1; } - // BVH traversal for the line origin + t*direction, t > t_min. Stores the + // BVH traversal for the line origin + t*direction, or for the parabola + // origin + t*direction + t^2/2*acceleration when acceleration is not zero, + // t > t_min. Stores the // max_hits nearest hits in hits, sorted by t, with hits on shared edges and // vertices kept once; every hit is visited, so a return value above // *stored_count means hits were dropped. When hits is NULL the hits are only counted, each @@ -1892,13 +2046,19 @@ gmesh_tri_hit (gmesh_struct* mesh, int triangle, Coords origin, Coords direction // origin. Returns the number of hits found. #pragma acc routine seq static int -gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, gmesh_hit* hits, int max_hits, int* stored_count, int* ambiguous) { +gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, Coords acceleration, double t_min, gmesh_hit* hits, int max_hits, int* stored_count, + int* ambiguous) { int count_only = (hits == NULL); + int gravity = (acceleration.x != 0 || acceleration.y != 0 || acceleration.z != 0); int stack[GMESH_BVH_STACK + 2]; int top = 0; int total = 0; double direction_length = coords_len (direction); double t_tolerance; + // a parabola may start at rest: its speed over the mesh size is then about + // sqrt(|acceleration| * size) + if (gravity) + direction_length += sqrt (coords_len (acceleration) * mesh->length_scale); if (direction_length <= 0) return 0; if (!mesh->nnodes) @@ -1911,7 +2071,10 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, int t; top--; node = &mesh->node[stack[top]]; - if (!gmesh_slab (node->lo, node->hi, origin, direction, t_min, FLT_MAX)) + if (gravity) { + if (!gmesh_slab_gravity (node->lo, node->hi, origin, direction, acceleration, t_min, FLT_MAX)) + continue; + } else if (!gmesh_slab (node->lo, node->hi, origin, direction, t_min, FLT_MAX)) continue; if (node->count == 0) { // cannot happen: depth limited at build @@ -1924,59 +2087,70 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, continue; } for (t = node->first; t < node->first + node->count; t++) { - double hit_t; - int on_edge; - int in_out; - int k; - int position; - int duplicate = 0; - gmesh_hit hit; - if (!gmesh_tri_hit (mesh, t, origin, direction, &hit_t, &on_edge)) - continue; - if (hit_t <= t_min) - continue; - if (coords_sp (mesh->tri_normal[t], direction) < 0) { - in_out = 1; - } else { - in_out = -1; - } - if (count_only) { - total++; - if (on_edge || fabs (hit_t) <= t_tolerance) - *ambiguous = 1; - continue; - } - hit.t = hit_t; - hit.normal = mesh->tri_normal[t]; - hit.tri = t; - hit.face = mesh->tri_face[t]; - hit.in_out = in_out; - hit.edge = on_edge; - // hits on shared edges/vertices: keep once - for (k = 0; k < *stored_count; k++) { - if (hits[k].in_out == in_out && fabs (hits[k].t - hit_t) <= t_tolerance) { - duplicate = 1; - break; + double crossing_t[2]; + int crossing_edge[2]; + int ncrossings; + int i_crossing; + if (gravity) + ncrossings = gmesh_tri_hit_gravity (mesh, t, origin, direction, acceleration, crossing_t, crossing_edge); + else + ncrossings = gmesh_tri_hit (mesh, t, origin, direction, &crossing_t[0], &crossing_edge[0]); + for (i_crossing = 0; i_crossing < ncrossings; i_crossing++) { + double hit_t = crossing_t[i_crossing]; + int on_edge = crossing_edge[i_crossing]; + Coords hit_velocity = direction; + int in_out; + int k; + int position; + int duplicate = 0; + gmesh_hit hit; + if (hit_t <= t_min) + continue; + if (gravity) + hit_velocity = coords_add (direction, coords_scale (acceleration, hit_t)); + if (coords_sp (mesh->tri_normal[t], hit_velocity) < 0) { + in_out = 1; + } else { + in_out = -1; } - } - if (duplicate) - continue; - total++; - position = *stored_count; - if (position == max_hits) { - if (max_hits == 0) + if (count_only) { + total++; + if (on_edge || fabs (hit_t) <= t_tolerance) + *ambiguous = 1; continue; - if (hit_t >= hits[max_hits - 1].t) + } + hit.t = hit_t; + hit.normal = mesh->tri_normal[t]; + hit.tri = t; + hit.face = mesh->tri_face[t]; + hit.in_out = in_out; + hit.edge = on_edge; + // hits on shared edges/vertices: keep once + for (k = 0; k < *stored_count; k++) { + if (hits[k].in_out == in_out && fabs (hits[k].t - hit_t) <= t_tolerance) { + duplicate = 1; + break; + } + } + if (duplicate) continue; - position = max_hits - 1; - } else { - (*stored_count)++; - } - while (position > 0 && hits[position - 1].t > hit_t) { - hits[position] = hits[position - 1]; - position--; + total++; + position = *stored_count; + if (position == max_hits) { + if (max_hits == 0) + continue; + if (hit_t >= hits[max_hits - 1].t) + continue; + position = max_hits - 1; + } else { + (*stored_count)++; + } + while (position > 0 && hits[position - 1].t > hit_t) { + hits[position] = hits[position - 1]; + position--; + } + hits[position] = hit; } - hits[position] = hit; } } return total; @@ -1986,13 +2160,13 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, int gmesh_overflow_warned = 0; #pragma acc declare create(gmesh_overflow_warned) - // intersections of a line with mesh, see geometry-mesh-lib.h + // intersections of a parabola with mesh, see geometry-mesh-lib.h #pragma acc routine seq int -gmesh_intersect (gmesh_struct* mesh, Coords position, Coords direction, double t_min, gmesh_hit hits[GMESH_MAX_HITS]) { +gmesh_intersect_gravity (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, double t_min, gmesh_hit hits[GMESH_MAX_HITS]) { int stored = 0; int ambiguous = 0; - int total = gmesh_trace (mesh, position, direction, t_min, hits, GMESH_MAX_HITS, &stored, &ambiguous); + int total = gmesh_trace (mesh, position, velocity, acceleration, t_min, hits, GMESH_MAX_HITS, &stored, &ambiguous); if (total > stored && !gmesh_overflow_warned) { gmesh_overflow_warned = 1; fprintf (stderr, @@ -2006,6 +2180,13 @@ gmesh_intersect (gmesh_struct* mesh, Coords position, Coords direction, double t return stored; } + // intersections of a line with mesh, see geometry-mesh-lib.h + #pragma acc routine seq +int +gmesh_intersect (gmesh_struct* mesh, Coords position, Coords direction, double t_min, gmesh_hit hits[GMESH_MAX_HITS]) { + return gmesh_intersect_gravity (mesh, position, direction, coords_set (0, 0, 0), t_min, hits); +} + // 1 when point is inside the box [lower, upper], borders included #pragma acc routine seq static int @@ -2033,7 +2214,7 @@ gmesh_inside (gmesh_struct* mesh, Coords position) { int stored = 0; int ambiguous = 0; Coords direction = coords_set (D[j][0], D[j][1], D[j][2]); - int n = gmesh_trace (mesh, position, direction, 0, NULL, 0, &stored, &ambiguous); + int n = gmesh_trace (mesh, position, direction, coords_set (0, 0, 0), 0, NULL, 0, &stored, &ambiguous); if (!ambiguous) return (int)(n % 2); votes += (int)(n % 2); diff --git a/common/lib/share/geometry-mesh-lib.h b/common/lib/share/geometry-mesh-lib.h index 6ad3a11f08..12c510b49e 100644 --- a/common/lib/share/geometry-mesh-lib.h +++ b/common/lib/share/geometry-mesh-lib.h @@ -41,6 +41,7 @@ * Ray tracing * gmesh_intersect() the GMESH_MAX_HITS nearest intersections of a * straight line, sorted in t + * gmesh_intersect_gravity() the same for a parabola (e.g. under gravity) * gmesh_inside() point-in-mesh test (deterministic, no random numbers) * Hits exactly on an edge or vertex shared by several triangles are * reported once. @@ -166,6 +167,18 @@ int gmesh_init_fit (char* filename, double xwidth, double yheight, double zdepth #pragma acc routine seq int gmesh_intersect (gmesh_struct* mesh, Coords position, Coords direction, double t_min, gmesh_hit hits[GMESH_MAX_HITS]); + // int gmesh_intersect_gravity(gmesh_struct *mesh, Coords position, + // Coords velocity, Coords acceleration, double t_min, + // gmesh_hit hits[GMESH_MAX_HITS]) + // ACTION: as gmesh_intersect, for the parabola + // position + t*velocity + t^2/2*acceleration (e.g. a neutron under + // gravity, with acceleration in the frame of the mesh). in_out refers to + // the velocity at the hit, velocity + t*acceleration. With a zero + // acceleration the result is that of gmesh_intersect. + // RETURN: number of hits stored + #pragma acc routine seq +int gmesh_intersect_gravity (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, double t_min, gmesh_hit hits[GMESH_MAX_HITS]); + // int gmesh_inside(gmesh_struct *mesh, Coords position) // ACTION: point in (closed) mesh test, parity of ray crossings along fixed // directions. Does not use random numbers. From 4fd172ba95df69883e76e4bc5a40e5a148c87633 Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Tue, 10 Nov 2026 15:35:52 +0100 Subject: [PATCH 03/22] McStas components: use geometry-mesh-lib instead of interoff-lib Incoherent, PowderN, Isotropic_Sqw, Single_crystal, Single_magnetic_crystal, Single_crystal_inelastic, Refractor and Lens get a SHARE function _mesh_intersect that picks t0/t3 (and the normals) from gmesh_intersect with the convention of off_intersect, so the TRACE code around the call is unchanged. Hits ahead and behind are searched separately, a hit on the surface the neutron sits on counts as ahead, and the inside test is the one of Monitor_nD. Normals keep the interoff orientation. Guide_anyshape and the obsolete Guide_anyshape_r take the first hit ahead from gmesh_intersect_gravity, so gravity is handled as before; per-face coatings come from gmesh_face_prop. The parked Monitor_Sqw uses Monitor_nD_MeshIntersect. off_init becomes gmesh_init_fit with the same resizing, and off_display gmesh_display. Isotropic_Sqw loses an unused global offdata. Co-Authored-By: Claude Opus 5.5 --- mcstas-comps/contrib/Lens.comp | 87 +++++++++++++++--- .../contrib/Single_crystal_inelastic.comp | 79 ++++++++++++++-- mcstas-comps/obsolete/Guide_anyshape_r.comp | 76 ++++++++-------- mcstas-comps/optics/Guide_anyshape.comp | 85 +++++++++--------- mcstas-comps/optics/Refractor.comp | 81 +++++++++++++++-- .../parked/Test_Monitor_Sqw/Monitor_Sqw.comp | 8 +- mcstas-comps/samples/Incoherent.comp | 87 +++++++++++++++--- mcstas-comps/samples/Isotropic_Sqw.comp | 89 ++++++++++++++++--- mcstas-comps/samples/PowderN.comp | 87 +++++++++++++++--- mcstas-comps/samples/Single_crystal.comp | 87 +++++++++++++++--- .../samples/Single_magnetic_crystal.comp | 83 +++++++++++++++-- 11 files changed, 683 insertions(+), 166 deletions(-) diff --git a/mcstas-comps/contrib/Lens.comp b/mcstas-comps/contrib/Lens.comp index 2ecbce3c3f..6a40a826e4 100644 --- a/mcstas-comps/contrib/Lens.comp +++ b/mcstas-comps/contrib/Lens.comp @@ -26,7 +26,7 @@ * The complex geometry option handles any closed non-convex polyhedra. * It computes the intersection points of the neutron ray with the object * transparently, so that it can be used like a regular sample object. -* It supports the PLY, OFF and NOFF file format but not COFF (colored faces). +* It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. * Such files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off * or @@ -88,7 +88,7 @@ * sigma_abs: [barn] thermal absorption cross section * p_interact: [1] MC Probability for scattering the ray; otherwise transmit * RMS: [AA] root mean square roughness of the surface -* geometry: [str] Name of an Object File Format (OFF) or PLY file for complex geometry. The OFF/PLY file may be generated from XYZ coordinates using qhull/powercrust +* geometry: [str] Name of an OFF, PLY or STL file for complex geometry. An OFF file may be generated from XYZ coordinates using qhull/powercrust * * %L * M. L. Goldberger et al, Phys. Rev. 71, 294 - 310 (1947) @@ -123,7 +123,72 @@ SHARE %{ /* support for OFF/PLY geometry */ %include "read_table-lib" - %include "interoff-lib" + %include "geometry-mesh-lib" + + // Times t0, t3 at which the line position + t*velocity enters and leaves + // mesh, as off_intersect of interoff-lib: inside the mesh, t0 < 0 is the + // nearest hit behind and t3 the nearest ahead; outside, t0 and t3 are the + // next two hits ahead. With a single hit ahead t3 = FLT_MAX, with the mesh + // behind t3 = 0. n0, n3 (may be NULL) get the normals of these hits, with + // the orientation of interoff-lib: opposite to the right-hand rule of the + // vertex order in the file. + // Returns 2 when t0 and t3 are both hits, 1 when only t0 is, else 0. + #pragma acc routine seq + int + Lens_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3, Coords* n0, Coords* n3) { + gmesh_hit ahead[GMESH_MAX_HITS]; + gmesh_hit behind[GMESH_MAX_HITS]; + // a hit on the surface the neutron sits on (|t| < tolerance) is counted ahead + double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); + int n_ahead = gmesh_intersect (mesh, position, velocity, -tolerance, ahead); + int n_behind = gmesh_intersect (mesh, position, coords_scale (velocity, -1), tolerance, behind); + int inside = n_ahead > 0 && n_behind > 0; + Coords no_normal = coords_set (0, 0, 0); + // gmesh normals follow the right-hand rule, unless the mesh was flipped + double normal_sign = mesh->flipped ? 1 : -1; + // normals of a closed, oriented mesh point outward: leaving first means inside + if (inside && mesh->closed && mesh->oriented) + inside = ahead[0].in_out < 0; + else if (inside && mesh->closed) + inside = gmesh_inside (mesh, position); + if (inside) { + *t0 = -behind[0].t; + *t3 = ahead[0].t; + if (n0) + *n0 = coords_scale (behind[0].normal, normal_sign); + if (n3) + *n3 = coords_scale (ahead[0].normal, normal_sign); + return 2; + } + if (n_ahead > 1) { + *t0 = ahead[0].t; + *t3 = ahead[1].t; + if (n0) + *n0 = coords_scale (ahead[0].normal, normal_sign); + if (n3) + *n3 = coords_scale (ahead[1].normal, normal_sign); + return 2; + } + if (n_ahead == 1) { + *t0 = ahead[0].t; + *t3 = FLT_MAX; + if (n0) + *n0 = coords_scale (ahead[0].normal, normal_sign); + if (n3) + *n3 = no_normal; + return 1; + } + if (n_behind) { + *t0 = -behind[0].t; + *t3 = 0; + if (n0) + *n0 = coords_scale (behind[0].normal, normal_sign); + if (n3) + *n3 = no_normal; + return 1; + } + return 0; + } /* Lens_roughness: function to rotate normal vector around axis for roughness * with specified tilt angle (deg) @@ -219,7 +284,7 @@ DECLARE double my_a_v; double mean_n; double events; - off_struct offdata; + gmesh_struct mesh; %} INITIALIZE @@ -239,7 +304,7 @@ INITIALIZE exit (-1); #else /* init the OFF/PLY without centering and scaling */ - if (!off_init (geometry, 0, 0, 0, 1, &offdata)) + if (!gmesh_init_fit (geometry, 0, 0, 0, 0, GMESH_PROPS_IGNORE, &mesh)) exit (printf ("Lens: %s: FATAL: could not initialize geometry file %s [OFF/PLY]\n", NAME_CURRENT_COMP, geometry)); #endif } @@ -266,10 +331,10 @@ TRACE #ifdef OPENACC #ifdef USE_OFF - off_struct thread_offdata = offdata; + gmesh_struct thread_mesh = mesh; #endif #else - #define thread_offdata offdata + #define thread_mesh mesh #endif /* iterate two faces intersection until no more is possible */ @@ -282,7 +347,7 @@ TRACE double t1 = 0, t0 = 0; Coords n, n0, n1; n = n0 = n1 = coords_set (0, 0, 0); - int intersect = off_intersect (&t0, &t1, &n0, &n1, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + int intersect = Lens_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &n0, &n1); if (!intersect) dt = -1; else if (t0 < 0 && 0 < t1) { @@ -393,7 +458,7 @@ TRACE double t1 = 0, t0 = 0; Coords n, n0, n1; n = n0 = n1 = coords_set (0, 0, 0); - int intersect = off_intersect (&t0, &t1, &n0, &n1, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + int intersect = Lens_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &n0, &n1); if (!intersect) dt = -1; else if (t0 < 0 && 0 < t1) { @@ -517,7 +582,7 @@ TRACE double t1 = 0, t0 = 0; Coords n, n0, n1; n = n0 = n1 = coords_set (0, 0, 0); - int intersect = off_intersect (&t0, &t1, &n0, &n1, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + int intersect = Lens_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &n0, &n1); if (!intersect) dt = -1; else if (t0 < 0 && 0 < t1) { @@ -640,7 +705,7 @@ MCDISPLAY /* draw circles to describe the 2 surfaces */ if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* OFF file */ - off_display (offdata); + gmesh_display (&mesh); } else { if (r1 && !focus1) { /* sphere1 */ theta00 = asin (fabs ((radius / 2) / r1)); diff --git a/mcstas-comps/contrib/Single_crystal_inelastic.comp b/mcstas-comps/contrib/Single_crystal_inelastic.comp index 7ff2e0813d..6788403e0c 100644 --- a/mcstas-comps/contrib/Single_crystal_inelastic.comp +++ b/mcstas-comps/contrib/Single_crystal_inelastic.comp @@ -55,7 +55,7 @@ * yheight: [m] Height of crystal. * zdepth: [m] Depth of crystal (no extinction simulated) * sqw: [string] Name of a 4d S(q,w) file - example.sqw4 is included in your installation -* geometry: [string] Name of an Object File Format (OFF) or PLY file for complex geometry. The OFF/PLY file may be generated from XYZ coordinates using qhull/powercrust. +* geometry: [string] Name of an OFF, PLY or STL file for complex geometry. An OFF file may be generated from XYZ coordinates using qhull/powercrust. * delta_d_d: [1] Lattice spacing variance, gaussian RMS. * mosaic: [arcmin] Isotropic crystal mosaic, gaussian RMS. Puts the crystal in the isotropic mosaic model state, thus disregarding other mosaicity parameters. * mosaic_a: [arcmin] Horizontal (rotation around lattice vector a) mosaic (anisotropic), gaussian RMS. Put the crystal in the anisotropic crystal vector state. I.e. model mosaicity through rotation around the crystal lattice vectors. Has precedence over in-plane mosaic model. @@ -120,7 +120,72 @@ SHARE %{ /* used for reading data table from file */ %include "read_table-lib" - %include "interoff-lib" + %include "geometry-mesh-lib" + + // Times t0, t3 at which the line position + t*velocity enters and leaves + // mesh, as off_intersect of interoff-lib: inside the mesh, t0 < 0 is the + // nearest hit behind and t3 the nearest ahead; outside, t0 and t3 are the + // next two hits ahead. With a single hit ahead t3 = FLT_MAX, with the mesh + // behind t3 = 0. n0, n3 (may be NULL) get the normals of these hits, with + // the orientation of interoff-lib: opposite to the right-hand rule of the + // vertex order in the file. + // Returns 2 when t0 and t3 are both hits, 1 when only t0 is, else 0. + #pragma acc routine seq + int + Single_crystal_inelastic_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3, Coords* n0, Coords* n3) { + gmesh_hit ahead[GMESH_MAX_HITS]; + gmesh_hit behind[GMESH_MAX_HITS]; + // a hit on the surface the neutron sits on (|t| < tolerance) is counted ahead + double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); + int n_ahead = gmesh_intersect (mesh, position, velocity, -tolerance, ahead); + int n_behind = gmesh_intersect (mesh, position, coords_scale (velocity, -1), tolerance, behind); + int inside = n_ahead > 0 && n_behind > 0; + Coords no_normal = coords_set (0, 0, 0); + // gmesh normals follow the right-hand rule, unless the mesh was flipped + double normal_sign = mesh->flipped ? 1 : -1; + // normals of a closed, oriented mesh point outward: leaving first means inside + if (inside && mesh->closed && mesh->oriented) + inside = ahead[0].in_out < 0; + else if (inside && mesh->closed) + inside = gmesh_inside (mesh, position); + if (inside) { + *t0 = -behind[0].t; + *t3 = ahead[0].t; + if (n0) + *n0 = coords_scale (behind[0].normal, normal_sign); + if (n3) + *n3 = coords_scale (ahead[0].normal, normal_sign); + return 2; + } + if (n_ahead > 1) { + *t0 = ahead[0].t; + *t3 = ahead[1].t; + if (n0) + *n0 = coords_scale (ahead[0].normal, normal_sign); + if (n3) + *n3 = coords_scale (ahead[1].normal, normal_sign); + return 2; + } + if (n_ahead == 1) { + *t0 = ahead[0].t; + *t3 = FLT_MAX; + if (n0) + *n0 = coords_scale (ahead[0].normal, normal_sign); + if (n3) + *n3 = no_normal; + return 1; + } + if (n_behind) { + *t0 = -behind[0].t; + *t3 = 0; + if (n0) + *n0 = coords_scale (behind[0].normal, normal_sign); + if (n3) + *n3 = no_normal; + return 1; + } + return 0; + } /* Declare structures and functions only once in each instrument. */ #ifndef SINGLE_CRYSTAL_DECL #define SINGLE_CRYSTAL_DECL @@ -663,7 +728,7 @@ SHARE DECLARE %{ struct hkl_info_struct hkl_info; - off_struct offdata; + gmesh_struct mesh; %} INITIALIZE @@ -716,7 +781,7 @@ INITIALIZE hkl_info.shape = -1; /* -1:no shape, 0:cyl, 1:box, 2:sphere, 3:any-shape */ if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - if (off_init (geometry, xwidth, yheight, zdepth, 0, &offdata)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { hkl_info.shape = 3; } } else if (xwidth && yheight && zdepth) @@ -799,7 +864,7 @@ TRACE else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) - intersect = off_intersect (&t1, &t2, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, offdata); + intersect = Single_crystal_inelastic_mesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL, NULL); if (t2 < 0) intersect = 0; /* we passed sample volume already */ @@ -833,7 +898,7 @@ TRACE else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) - intersect = off_intersect (&t1, &t2, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, offdata); + intersect = Single_crystal_inelastic_mesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL, NULL); if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { /* neutron is leaving the sample */ if (hkl_info.flag_warning < 100) @@ -935,7 +1000,7 @@ MCDISPLAY } else if (hkl_info.shape == 2) { /* sphere */ sphere (0, 0, 0, radius); } else if (hkl_info.shape == 3) { /* OFF file */ - off_display (offdata); + gmesh_display (&mesh); } %} END diff --git a/mcstas-comps/obsolete/Guide_anyshape_r.comp b/mcstas-comps/obsolete/Guide_anyshape_r.comp index ffcf7aeea3..7e0e445daa 100644 --- a/mcstas-comps/obsolete/Guide_anyshape_r.comp +++ b/mcstas-comps/obsolete/Guide_anyshape_r.comp @@ -41,7 +41,7 @@ * values as input (common for all the faces), ignoring those specified in the OFF file * * The complex OFF/PLY geometry option handles any closed non-convex polyhedra. -* It supports the OFF and NOFF file format but not COFF (colored faces). +* It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. * Standard .OFF files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off * or @@ -54,7 +54,7 @@ * %P * INPUT PARAMETERS: * -* geometry: [str] Name of the OFF/PLY file describing the guide geometry +* geometry: [str] Name of the OFF, PLY or STL file describing the guide geometry * xwidth: [m] Redimension the object bounding box on X axis is non-zero * yheight: [m] Redimension the object bounding box on Y axis is non-zero * zdepth: [m] Redimension the object bounding box on Z axis is non-zero @@ -95,26 +95,41 @@ DEPENDENCY " -DUSE_OFF " SHARE %{ %include "read_table-lib" -%include "interoff-lib" +%include "geometry-mesh-lib" %include "ref-lib" + +// first hit of the parabola position + t*velocity + t^2/2*acceleration with +// mesh ahead of the neutron: its time dt, the normal and the face hit. +// Returns 1 on a hit, 0 otherwise. +#pragma acc routine seq +int +Guide_anyshape_r_mesh_next (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, double* dt, Coords* normal, int* face) { + gmesh_hit hits[GMESH_MAX_HITS]; + if (!gmesh_intersect_gravity (mesh, position, velocity, acceleration, 0, hits)) + return 0; + *dt = hits[0].t; + *normal = hits[0].normal; + *face = hits[0].face; + return 1; +} %} DECLARE %{ - off_struct offdata; + gmesh_struct mesh; int use_file_coatings; // Flag to specify whether to use (1) or not (0) the mirror parameters given in the .OFF file %} INITIALIZE %{ MPI_MASTER (fprintf (stderr, "Guide_anyshape_r: %s: WARNING: this component is obsolete. Use Guide_anyshape(file_coatings=1) instead.\n", NAME_CURRENT_COMP);); - /* initialize OFF object from the file(s) */ - if (!off_init (geometry, xwidth, yheight, zdepth, !center, &offdata)) + /* initialize the mesh from the file, with the per-face coatings if given */ + if (!gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_OPTIONAL, &mesh)) exit (-1); if (m == 0 && alpha == 0 && W == 0) { use_file_coatings = 1; - if (offdata.nfaceprops < 3) { + if (mesh.nfaceprops < 3) { fprintf (stderr, "Guide_anyshape_r: %s: Error: m, alpha, W are all 0, but %s does not define m, alpha and W values for each face.\n", NAME_CURRENT_COMP, geometry); exit (-1); @@ -134,39 +149,23 @@ TRACE int counter = 0; /* main loop for multiple reflections */ #ifdef OPENACC - off_struct thread_offdata = offdata; + gmesh_struct thread_mesh = mesh; #else - #define thread_offdata offdata + #define thread_mesh mesh #endif do { - double t0 = 0, t3 = 0, dt = 0; - unsigned long faceindex0 = 0, faceindex3 = 0, fi = 0; - Coords n0, n3, n = { 0, 0, 0 }; - /* determine intersections with object; PL: get index of the intersected face */ - - double mc_gx = 0.0, mc_gy = 0.0, mc_gz = 0.0; - if (mcgravitation) { - Coords locgrav; - locgrav = rot_apply (_comp->_rotation_absolute, coords_set (0, -GRAVITY, 0)); - coords_get (locgrav, &mc_gx, &mc_gy, &mc_gz); - } - - intersect = off_intersect_idx (&t0, &t3, &n0, &n3, &faceindex0, &faceindex3, x, y, z, vx, vy, vz, mc_gx, mc_gy, mc_gz, thread_offdata); + double dt = 0; + int fi = 0; + Coords n = { 0, 0, 0 }; + Coords acceleration = coords_set (0, 0, 0); + if (mcgravitation) + acceleration = rot_apply (_comp->_rotation_absolute, coords_set (0, -GRAVITY, 0)); - /* get the smallest positive */ - if (t0 > 0) { - dt = t0; - n = n0; - fi = faceindex0; - } - if (intersect > 1 && dt <= 0 && t3 > dt) { - dt = t3; - n = n3; - fi = faceindex3; - } + /* next intersection with the object, along the parabola under gravity; PL: with the index of the face */ + intersect = Guide_anyshape_r_mesh_next (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, &dt, &n, &fi); /* exit loop when no intersection forward */ - if (dt <= 0 || !intersect) + if (!intersect) break; double nx, ny, nz; @@ -187,9 +186,10 @@ TRACE double W_value = W; if (use_file_coatings == 1) { - m_value = off_face_prop (&thread_offdata, fi, OFF_FACEPROP_M, 0); - alpha_value = off_face_prop (&thread_offdata, fi, OFF_FACEPROP_ALPHA, 0); - W_value = off_face_prop (&thread_offdata, fi, OFF_FACEPROP_W, 0); + // per-face properties m, alpha, W are numbers 0, 1, 2 + m_value = gmesh_face_prop (&thread_mesh, fi, 0, 0); + alpha_value = gmesh_face_prop (&thread_mesh, fi, 1, 0); + W_value = gmesh_face_prop (&thread_mesh, fi, 2, 0); } double par[] = { R0, Qc, alpha_value, m_value, W_value }; StdReflecFunc (q, par, &R); @@ -231,7 +231,7 @@ TRACE MCDISPLAY %{ /* display the object */ - off_display (offdata); + gmesh_display (&mesh); %} END diff --git a/mcstas-comps/optics/Guide_anyshape.comp b/mcstas-comps/optics/Guide_anyshape.comp index 543fadcdc7..842ee4d9b3 100644 --- a/mcstas-comps/optics/Guide_anyshape.comp +++ b/mcstas-comps/optics/Guide_anyshape.comp @@ -41,7 +41,7 @@ * * The complex OFF/PLY geometry option handles any closed non-convex polyhedra, * with polygons of any number of vertices. -* It supports the OFF, NOFF and COFF file formats (colours are ignored, but +* It supports the OFF, NOFF, COFF, PLY and STL file formats (colours are ignored, but * face colours would be interpreted as coatings when file_coatings=1). * Such files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off @@ -55,7 +55,7 @@ * %P * INPUT PARAMETERS: * -* geometry: [str] Name of the OFF/PLY file describing the guide geometry +* geometry: [str] Name of the OFF, PLY or STL file describing the guide geometry * xwidth: [m] Redimension the object bounding box on X axis is non-zero * yheight: [m] Redimension the object bounding box on Y axis is non-zero * zdepth: [m] Redimension the object bounding box on Z axis is non-zero @@ -99,21 +99,36 @@ DEPENDENCY " -DUSE_OFF " SHARE %{ %include "read_table-lib" -%include "interoff-lib" +%include "geometry-mesh-lib" %include "ref-lib" + +// first hit of the parabola position + t*velocity + t^2/2*acceleration with +// mesh ahead of the neutron: its time dt, the normal and the face hit. +// Returns 1 on a hit, 0 otherwise. +#pragma acc routine seq +int +Guide_anyshape_mesh_next (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, double* dt, Coords* normal, int* face) { + gmesh_hit hits[GMESH_MAX_HITS]; + if (!gmesh_intersect_gravity (mesh, position, velocity, acceleration, 0, hits)) + return 0; + *dt = hits[0].t; + *normal = hits[0].normal; + *face = hits[0].face; + return 1; +} %} DECLARE %{ - off_struct offdata; + gmesh_struct mesh; t_Table pTable; int use_table; %} INITIALIZE %{ - /* initialize OFF object from the file(s) */ - if (!off_init (geometry, xwidth, yheight, zdepth, !center, &offdata)) + /* initialize the mesh from the file, with the per-face coatings when used */ + if (!gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, file_coatings ? GMESH_PROPS_OPTIONAL : GMESH_PROPS_IGNORE, &mesh)) exit (-1); /* optionally initialize reflectivity curves */ @@ -129,13 +144,13 @@ INITIALIZE if (file_coatings) { if (use_table) exit (fprintf (stderr, "Guide_anyshape: %s: file_coatings=1 and reflect=%s can not be used together. Aborting.\n", NAME_CURRENT_COMP, reflect)); - if (offdata.nfaceprops < 3) + if (mesh.nfaceprops < 3) exit (fprintf (stderr, "Guide_anyshape: %s: file_coatings=1 but %s does not give m, alpha, W for each face\n" " (expected lines 'nv i1 ... inv m alpha W [R0 [Qc]]'). Aborting.\n", NAME_CURRENT_COMP, geometry)); - MPI_MASTER (printf ("Guide_anyshape: %s: using per-face m, alpha, W%s%s from %s\n", NAME_CURRENT_COMP, offdata.nfaceprops > OFF_FACEPROP_R0 ? ", R0" : "", - offdata.nfaceprops > OFF_FACEPROP_QC ? ", Qc" : "", geometry);); + MPI_MASTER (printf ("Guide_anyshape: %s: using per-face m, alpha, W%s%s from %s\n", NAME_CURRENT_COMP, mesh.nfaceprops > 3 ? ", R0" : "", + mesh.nfaceprops > 4 ? ", Qc" : "", geometry);); } %} @@ -145,39 +160,23 @@ TRACE int counter = 0; /* main loop for multiple reflections */ #ifdef OPENACC - off_struct thread_offdata = offdata; + gmesh_struct thread_mesh = mesh; #else - #define thread_offdata offdata + #define thread_mesh mesh #endif do { - double t0 = 0, t3 = 0, dt = 0; - unsigned long fi0 = 0, fi3 = 0, fi = 0; - Coords n0, n3, n = { 0, 0, 0 }; - /* determine intersections with object */ - - double mc_gx = 0.0, mc_gy = 0.0, mc_gz = 0.0; - if (mcgravitation) { - Coords locgrav; - locgrav = rot_apply (_comp->_rotation_absolute, coords_set (0, -GRAVITY, 0)); - coords_get (locgrav, &mc_gx, &mc_gy, &mc_gz); - } - - intersect = off_intersect_idx (&t0, &t3, &n0, &n3, &fi0, &fi3, x, y, z, vx, vy, vz, mc_gx, mc_gy, mc_gz, thread_offdata); + double dt = 0; + int fi = 0; + Coords n = { 0, 0, 0 }; + Coords acceleration = coords_set (0, 0, 0); + if (mcgravitation) + acceleration = rot_apply (_comp->_rotation_absolute, coords_set (0, -GRAVITY, 0)); - /* get the smallest positive */ - if (t0 > 0) { - dt = t0; - n = n0; - fi = fi0; - } - if (intersect > 1 && dt <= 0 && t3 > dt) { - dt = t3; - n = n3; - fi = fi3; - } + /* next intersection with the object, along the parabola under gravity */ + intersect = Guide_anyshape_mesh_next (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, &dt, &n, &fi); /* exit loop when no intersection forward */ - if (dt <= 0 || !intersect) + if (!intersect) break; double nx, ny, nz; @@ -198,12 +197,12 @@ TRACE TableReflecFunc (q, &pTable, &R); else { double par[] = { R0, Qc, alpha, m, W }; - if (file_coatings) { /* per-face values, parameters as defaults */ - par[0] = off_face_prop (&thread_offdata, fi, OFF_FACEPROP_R0, R0); - par[1] = off_face_prop (&thread_offdata, fi, OFF_FACEPROP_QC, Qc); - par[2] = off_face_prop (&thread_offdata, fi, OFF_FACEPROP_ALPHA, alpha); - par[3] = off_face_prop (&thread_offdata, fi, OFF_FACEPROP_M, m); - par[4] = off_face_prop (&thread_offdata, fi, OFF_FACEPROP_W, W); + if (file_coatings) { /* per-face values m, alpha, W, R0, Qc (properties 0-4), parameters as defaults */ + par[0] = gmesh_face_prop (&thread_mesh, fi, 3, R0); + par[1] = gmesh_face_prop (&thread_mesh, fi, 4, Qc); + par[2] = gmesh_face_prop (&thread_mesh, fi, 1, alpha); + par[3] = gmesh_face_prop (&thread_mesh, fi, 0, m); + par[4] = gmesh_face_prop (&thread_mesh, fi, 2, W); } StdReflecFunc (q, par, &R); } @@ -245,7 +244,7 @@ TRACE MCDISPLAY %{ /* display the object */ - off_display (offdata); + gmesh_display (&mesh); %} END diff --git a/mcstas-comps/optics/Refractor.comp b/mcstas-comps/optics/Refractor.comp index 4e9b5c1da9..9e0a74fa0c 100644 --- a/mcstas-comps/optics/Refractor.comp +++ b/mcstas-comps/optics/Refractor.comp @@ -48,7 +48,7 @@ * The complex geometry option handles any closed non-convex polyhedra. * It computes the intersection points of the neutron ray with the object * transparently, so that it can be used like a regular sample object. -* It supports the PLY, OFF and NOFF file format but not COFF (colored faces). +* It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. * Such files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off * or @@ -154,9 +154,74 @@ SHARE %include "read_table-lib" - %include "interoff-lib" + %include "geometry-mesh-lib" %include "ref-lib" + // Times t0, t3 at which the line position + t*velocity enters and leaves + // mesh, as off_intersect of interoff-lib: inside the mesh, t0 < 0 is the + // nearest hit behind and t3 the nearest ahead; outside, t0 and t3 are the + // next two hits ahead. With a single hit ahead t3 = FLT_MAX, with the mesh + // behind t3 = 0. n0, n3 (may be NULL) get the normals of these hits, with + // the orientation of interoff-lib: opposite to the right-hand rule of the + // vertex order in the file. + // Returns 2 when t0 and t3 are both hits, 1 when only t0 is, else 0. + #pragma acc routine seq + int + Refractor_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3, Coords* n0, Coords* n3) { + gmesh_hit ahead[GMESH_MAX_HITS]; + gmesh_hit behind[GMESH_MAX_HITS]; + // a hit on the surface the neutron sits on (|t| < tolerance) is counted ahead + double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); + int n_ahead = gmesh_intersect (mesh, position, velocity, -tolerance, ahead); + int n_behind = gmesh_intersect (mesh, position, coords_scale (velocity, -1), tolerance, behind); + int inside = n_ahead > 0 && n_behind > 0; + Coords no_normal = coords_set (0, 0, 0); + // gmesh normals follow the right-hand rule, unless the mesh was flipped + double normal_sign = mesh->flipped ? 1 : -1; + // normals of a closed, oriented mesh point outward: leaving first means inside + if (inside && mesh->closed && mesh->oriented) + inside = ahead[0].in_out < 0; + else if (inside && mesh->closed) + inside = gmesh_inside (mesh, position); + if (inside) { + *t0 = -behind[0].t; + *t3 = ahead[0].t; + if (n0) + *n0 = coords_scale (behind[0].normal, normal_sign); + if (n3) + *n3 = coords_scale (ahead[0].normal, normal_sign); + return 2; + } + if (n_ahead > 1) { + *t0 = ahead[0].t; + *t3 = ahead[1].t; + if (n0) + *n0 = coords_scale (ahead[0].normal, normal_sign); + if (n3) + *n3 = coords_scale (ahead[1].normal, normal_sign); + return 2; + } + if (n_ahead == 1) { + *t0 = ahead[0].t; + *t3 = FLT_MAX; + if (n0) + *n0 = coords_scale (ahead[0].normal, normal_sign); + if (n3) + *n3 = no_normal; + return 1; + } + if (n_behind) { + *t0 = -behind[0].t; + *t3 = 0; + if (n0) + *n0 = coords_scale (behind[0].normal, normal_sign); + if (n3) + *n3 = no_normal; + return 1; + } + return 0; + } + /* wrappers to intersection routines which also return the normal vector */ int box_intersect_n (double* t0, double* t1, double x, double y, double z, double vx, double vy, double vz, double dx, double dy, double dz, double* nx, double* ny, @@ -319,7 +384,7 @@ DECLARE %{ double rho; double bc; - off_struct offdata; + gmesh_struct mesh; double mean_n; double events; double theta1; @@ -348,7 +413,7 @@ INITIALIZE exit (-1); #else /* off/ply case */ - if (!off_init (geometry, xwidth, yheight, zdepth, 0, &offdata)) + if (!gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) exit (printf ("Refractor: %s: FATAL: invalid OFF/PLY geometry specification for file '%s'.\n", NAME_CURRENT_COMP, geometry)); #endif } else @@ -382,10 +447,10 @@ TRACE #ifdef OPENACC #ifdef USE_OFF - off_struct thread_offdata = offdata; + gmesh_struct thread_mesh = mesh; #endif #else - #define thread_offdata offdata + #define thread_mesh mesh #endif do { @@ -410,7 +475,7 @@ TRACE #ifdef USE_OFF else if (geometry && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { Coords n0, n1; - intersect = off_intersect (&t0, &t1, &n0, &n1, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = Refractor_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &n0, &n1); if (intersect) { coords_get (t0 <= 0 || t0 > t1 ? n1 : n0, &nx, &ny, &nz); } @@ -722,7 +787,7 @@ MCDISPLAY } box (0, 0, 0, 4 * radius, 4 * radius, 2 * radius + zdepth, 0, 0, 1, 0); } else if (geometry && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - off_display (offdata); + gmesh_display (&mesh); } %} END diff --git a/mcstas-comps/parked/Test_Monitor_Sqw/Monitor_Sqw.comp b/mcstas-comps/parked/Test_Monitor_Sqw/Monitor_Sqw.comp index 3a0bd8e8ec..6ec9007af2 100644 --- a/mcstas-comps/parked/Test_Monitor_Sqw/Monitor_Sqw.comp +++ b/mcstas-comps/parked/Test_Monitor_Sqw/Monitor_Sqw.comp @@ -96,7 +96,7 @@ DECLARE MonitornD_Defines_type DEFS; MonitornD_Variables_type Vars; MCDETECTOR detector; - off_struct offdata; + gmesh_struct mesh; double mean_vx; double mean_vy; double mean_vz; @@ -145,7 +145,7 @@ INITIALIZE } int offflag=0; if (geometry && strlen(geometry) && strcmp(geometry,"NULL") && strcmp(geometry,"0")) - if (!off_init( geometry, xwidth, yheight, zdepth, 0, &offdata )) { + if (!gmesh_init_fit(geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { printf("Monitor_Sqw: %s could not initiate the OFF geometry. \n" " Defaulting to normal Monitor dimensions.\n", NAME_CURRENT_COMP); strcpy(geometry, ""); @@ -180,8 +180,8 @@ TRACE if (geometry && strlen(geometry) && strcmp(geometry,"NULL") && strcmp(geometry,"0")) { /* determine intersections with object */ - intersect = off_intersect(&t0, &t1, NULL, NULL, - x,y,z, vx, vy, vz, 0, 0, 0, offdata ); + int face = 0; + intersect = Monitor_nD_MeshIntersect(&mesh, coords_set(x, y, z), coords_set(vx, vy, vz), &t0, &t1, &face); } else if (abs(Vars.Flag_Shape) == DEFS.SHAPE_SQUARE) /* square xy */ { diff --git a/mcstas-comps/samples/Incoherent.comp b/mcstas-comps/samples/Incoherent.comp index 1e61f4bf47..06cee3dae5 100644 --- a/mcstas-comps/samples/Incoherent.comp +++ b/mcstas-comps/samples/Incoherent.comp @@ -39,12 +39,12 @@ * hollow cylinder: radius x yheight and thickness>0 * sphere: radius (yheight=0 thickness=0) * hollow sphere: radius and thickness>0 (yheight=0) -* any shape: geometry=OFF file +* any shape: geometry=OFF, PLY or STL file * * The complex geometry option handles any closed non-convex polyhedra. * It computes the intersection points of the neutron ray with the object * transparently, so that it can be used like a regular sample object. -* It supports the PLY, OFF and NOFF file format but not COFF (colored faces). +* It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. * Such files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off * or @@ -80,7 +80,7 @@ * gamma: [1] Lorentzian width of quasielastic broadening (HWHM) * Etrans: [meV] Global energy-transfer, for use in inelastic settings * deltaE: [meV] Width in energy around Etrans, for use in inelastic settings -* geometry: [str] Name of an Object File Format (OFF) or PLY file for complex geometry. The OFF/PLY file may be generated from XYZ coordinates using qhull/powercrust +* geometry: [str] Name of an OFF, PLY or STL file for complex geometry. An OFF file may be generated from XYZ coordinates using qhull/powercrust * concentric: [1] Indicate that this component has a hollow geometry and may contain other components. It should then be duplicated after the inside part (only for box, cylinder, sphere) * * order: [1] Limit multiple scattering up to given order 0 means all (default), 1 means single, 2 means double, ... @@ -126,7 +126,72 @@ sigma_abs=5.08, sigma_inc=5.08, Vc=13.827, concentric=0, order=0) SHARE %{ %include "read_table-lib" - %include "interoff-lib" + %include "geometry-mesh-lib" + + // Times t0, t3 at which the line position + t*velocity enters and leaves + // mesh, as off_intersect of interoff-lib: inside the mesh, t0 < 0 is the + // nearest hit behind and t3 the nearest ahead; outside, t0 and t3 are the + // next two hits ahead. With a single hit ahead t3 = FLT_MAX, with the mesh + // behind t3 = 0. n0, n3 (may be NULL) get the normals of these hits, with + // the orientation of interoff-lib: opposite to the right-hand rule of the + // vertex order in the file. + // Returns 2 when t0 and t3 are both hits, 1 when only t0 is, else 0. + #pragma acc routine seq + int + Incoherent_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3, Coords* n0, Coords* n3) { + gmesh_hit ahead[GMESH_MAX_HITS]; + gmesh_hit behind[GMESH_MAX_HITS]; + // a hit on the surface the neutron sits on (|t| < tolerance) is counted ahead + double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); + int n_ahead = gmesh_intersect (mesh, position, velocity, -tolerance, ahead); + int n_behind = gmesh_intersect (mesh, position, coords_scale (velocity, -1), tolerance, behind); + int inside = n_ahead > 0 && n_behind > 0; + Coords no_normal = coords_set (0, 0, 0); + // gmesh normals follow the right-hand rule, unless the mesh was flipped + double normal_sign = mesh->flipped ? 1 : -1; + // normals of a closed, oriented mesh point outward: leaving first means inside + if (inside && mesh->closed && mesh->oriented) + inside = ahead[0].in_out < 0; + else if (inside && mesh->closed) + inside = gmesh_inside (mesh, position); + if (inside) { + *t0 = -behind[0].t; + *t3 = ahead[0].t; + if (n0) + *n0 = coords_scale (behind[0].normal, normal_sign); + if (n3) + *n3 = coords_scale (ahead[0].normal, normal_sign); + return 2; + } + if (n_ahead > 1) { + *t0 = ahead[0].t; + *t3 = ahead[1].t; + if (n0) + *n0 = coords_scale (ahead[0].normal, normal_sign); + if (n3) + *n3 = coords_scale (ahead[1].normal, normal_sign); + return 2; + } + if (n_ahead == 1) { + *t0 = ahead[0].t; + *t3 = FLT_MAX; + if (n0) + *n0 = coords_scale (ahead[0].normal, normal_sign); + if (n3) + *n3 = no_normal; + return 1; + } + if (n_behind) { + *t0 = -behind[0].t; + *t3 = 0; + if (n0) + *n0 = coords_scale (behind[0].normal, normal_sign); + if (n3) + *n3 = no_normal; + return 1; + } + return 0; + } struct StructVarsInc { double sigma_a; /* Absorption cross section per atom (barns) */ double sigma_i; /* Incoherent scattering cross section per atom (barns) */ @@ -143,7 +208,7 @@ SHARE DECLARE %{ struct StructVarsInc VarsInc; - off_struct offdata; + gmesh_struct mesh; %} INITIALIZE @@ -154,7 +219,7 @@ INITIALIZE fprintf (stderr, "Error: You are attempting to use an OFF geometry without -DUSE_OFF. You will need to recompile with that define set!\n"); exit (-1); #else - if (off_init (geometry, xwidth, yheight, zdepth, 0, &offdata)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { VarsInc.shape = 3; thickness = 0; concentric = 0; @@ -261,10 +326,10 @@ TRACE #ifdef OPENACC #ifdef USE_OFF - off_struct thread_offdata = offdata; + gmesh_struct thread_mesh = mesh; #endif #else - #define thread_offdata offdata + #define thread_mesh mesh #endif do { /* Main interaction loop. Ends with intersect=0 */ @@ -278,7 +343,7 @@ TRACE intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (VarsInc.shape == 3) - intersect = off_intersect (&t0, &t3, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = Incoherent_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL, NULL); #endif if (intersect) { @@ -475,7 +540,7 @@ TRACE intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (VarsInc.shape == 3) - intersect = off_intersect (&t0, &t3, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = Incoherent_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL, NULL); #endif flag_ishollow = 0; @@ -526,7 +591,7 @@ TRACE MCDISPLAY %{ if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* OFF file */ - off_display (offdata); + gmesh_display (&mesh); } else if (radius > 0 && yheight) { /* cylinder along y*/ cylinder (0, 0, 0, radius, yheight, thickness, 0, 1, 0); } else if (xwidth && yheight) { /* box/rectangle */ diff --git a/mcstas-comps/samples/Isotropic_Sqw.comp b/mcstas-comps/samples/Isotropic_Sqw.comp index f5797b5ad5..b88dc13bb8 100644 --- a/mcstas-comps/samples/Isotropic_Sqw.comp +++ b/mcstas-comps/samples/Isotropic_Sqw.comp @@ -63,12 +63,12 @@ * hollow cylinder: radius x yheight and thickness>0 * sphere: radius (yheight=0 thickness=0) * hollow sphere: radius and thickness>0 (yheight=0) -* any shape: geometry=OFF file +* any shape: geometry=OFF, PLY or STL file * * The complex geometry option handles any closed non-convex polyhedra. * It computes the intersection points of the neutron ray with the object * transparently, so that it can be used like a regular sample object. -* It supports the OFF, PLY and NOFF file format but not COFF (colored faces). +* It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. * Such files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off * or @@ -204,7 +204,7 @@ * * OPTIONAL PARAMETERS: * concentric: [1] Indicate that this component has a hollow geometry and may contain other components. It should then be duplicated after the inside part (only for box, cylinder, sphere) [1] -* geometry: [str] Name of an Object File Format (OFF) or PLY file for complex geometry. The OFF/PLY file may be generated from XYZ coordinates using qhull/powercrust +* geometry: [str] Name of an OFF, PLY or STL file for complex geometry. An OFF file may be generated from XYZ coordinates using qhull/powercrust * order: [1] Limit multiple scattering up to given order 0:all (default), 1:single, 2:double, ... * verbose: [1] Verbosity level (0:silent, 1:normal, 2:verbose, 3:debug). A verbosity>1 also computes dispersions and S(q,w) analysis. * d_phi: [deg] scattering vertical angular spreading (usually the height of the next component/detector). Use 0 for full space. This is only relevant for single scattering (order=1). @@ -313,7 +313,72 @@ SHARE #define qSq {-1,0,0,0,0,0,1, 0,0 } %include "read_table-lib" - %include "interoff-lib" + %include "geometry-mesh-lib" + + // Times t0, t3 at which the line position + t*velocity enters and leaves + // mesh, as off_intersect of interoff-lib: inside the mesh, t0 < 0 is the + // nearest hit behind and t3 the nearest ahead; outside, t0 and t3 are the + // next two hits ahead. With a single hit ahead t3 = FLT_MAX, with the mesh + // behind t3 = 0. n0, n3 (may be NULL) get the normals of these hits, with + // the orientation of interoff-lib: opposite to the right-hand rule of the + // vertex order in the file. + // Returns 2 when t0 and t3 are both hits, 1 when only t0 is, else 0. + #pragma acc routine seq + int + Isotropic_Sqw_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3, Coords* n0, Coords* n3) { + gmesh_hit ahead[GMESH_MAX_HITS]; + gmesh_hit behind[GMESH_MAX_HITS]; + // a hit on the surface the neutron sits on (|t| < tolerance) is counted ahead + double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); + int n_ahead = gmesh_intersect (mesh, position, velocity, -tolerance, ahead); + int n_behind = gmesh_intersect (mesh, position, coords_scale (velocity, -1), tolerance, behind); + int inside = n_ahead > 0 && n_behind > 0; + Coords no_normal = coords_set (0, 0, 0); + // gmesh normals follow the right-hand rule, unless the mesh was flipped + double normal_sign = mesh->flipped ? 1 : -1; + // normals of a closed, oriented mesh point outward: leaving first means inside + if (inside && mesh->closed && mesh->oriented) + inside = ahead[0].in_out < 0; + else if (inside && mesh->closed) + inside = gmesh_inside (mesh, position); + if (inside) { + *t0 = -behind[0].t; + *t3 = ahead[0].t; + if (n0) + *n0 = coords_scale (behind[0].normal, normal_sign); + if (n3) + *n3 = coords_scale (ahead[0].normal, normal_sign); + return 2; + } + if (n_ahead > 1) { + *t0 = ahead[0].t; + *t3 = ahead[1].t; + if (n0) + *n0 = coords_scale (ahead[0].normal, normal_sign); + if (n3) + *n3 = coords_scale (ahead[1].normal, normal_sign); + return 2; + } + if (n_ahead == 1) { + *t0 = ahead[0].t; + *t3 = FLT_MAX; + if (n0) + *n0 = coords_scale (ahead[0].normal, normal_sign); + if (n3) + *n3 = no_normal; + return 1; + } + if (n_behind) { + *t0 = -behind[0].t; + *t3 = 0; + if (n0) + *n0 = coords_scale (behind[0].normal, normal_sign); + if (n3) + *n3 = no_normal; + return 1; + } + return 0; + } /* For the density of states S(w) */ struct Sqw_W_struct { @@ -430,8 +495,6 @@ SHARE Sqw_Data->q_min_file = 0; } - off_struct offdata; - /* gaussian distribution to appply around Bragg peaks in a powder */ double Sqw_powder_gauss (double x, double mean, double rms) { @@ -2012,7 +2075,7 @@ DECLARE %{ struct Sqw_sample_struct VarSqw; int* columns; - off_struct offdata; + gmesh_struct mesh; %} /*****************************************************************************/ @@ -2031,7 +2094,7 @@ INITIALIZE fprintf (stderr, "Error: You are attempting to use an OFF geometry without -DUSE_OFF. You will need to recompile with that define set!\n"); exit (-1); #else - if (off_init (geometry, xwidth, yheight, zdepth, 0, &offdata)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { VarSqw.shape = 3; thickness = 0; concentric = 0; @@ -2239,10 +2302,10 @@ TRACE #ifdef OPENACC #ifdef USE_OFF - off_struct thread_offdata = offdata; + gmesh_struct thread_mesh = mesh; #endif #else - #define thread_offdata offdata + #define thread_mesh mesh #endif do { /* Main interaction loop. Ends with intersect=0 */ @@ -2258,7 +2321,7 @@ TRACE intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (VarSqw.shape == 3) - intersect = off_intersect (&t0, &t3, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = Isotropic_Sqw_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL, NULL); #endif } else { if (VarSqw.shape == 0) @@ -2271,7 +2334,7 @@ TRACE intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius - thickness); #ifdef USE_OFF else if (VarSqw.shape == 3) - intersect = off_intersect (&t0, &t3, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = Isotropic_Sqw_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL, NULL); #endif } } else @@ -2904,7 +2967,7 @@ MCDISPLAY circle ("xz", 0, 0, 0, radius); circle ("yz", 0, 0, 0, radius); } else if (VarSqw.shape == 3) { /* OFF file */ - off_display (offdata); + gmesh_display (&mesh); } } /* end MCDISPLAY */ diff --git a/mcstas-comps/samples/PowderN.comp b/mcstas-comps/samples/PowderN.comp index 32e9922108..1bf548c25a 100644 --- a/mcstas-comps/samples/PowderN.comp +++ b/mcstas-comps/samples/PowderN.comp @@ -54,7 +54,7 @@ * The complex geometry option handles any closed non-convex polyhedra. * It computes the intersection points of the neutron ray with the object * transparently, so that it can be used like a regular sample object. -* It supports the PLY, OFF and NOFF file format but not COFF (colored faces). +* It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. * Such files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off * or @@ -164,7 +164,7 @@ * p_transmit: [1] Fraction of transmitted (only attenuated) neutron rays. * p_interact: [1] Fraction of events interacting coherently with sample. * concentric: [1] Indicate that this component has a hollow geometry and may contain other components. It should then be duplicated after the inside part (only for box, cylinder, sphere). -* geometry: [str] Name of an Object File Format (OFF) or PLY file for complex geometry. The OFF/PLY file may be generated from XYZ coordinates using qhull/powercrust. +* geometry: [str] Name of an OFF, PLY or STL file for complex geometry. An OFF file may be generated from XYZ coordinates using qhull/powercrust. * barns: [1] Flag to indicate if |F|^2 from 'reflections' is in barns or fm^2 (barns=1 for laz, barns=0 for lau type files). * sigma_abs: [barns] Absorption cross section per unit cell at 2200 m/s. Use a negative value to inactivate it. * sigma_inc: [barns] Incoherent cross section per unit cell. Use a negative value to inactivate it. @@ -224,7 +224,72 @@ SHARE %{ /* used for reading data table from file */ %include "read_table-lib" - %include "interoff-lib" + %include "geometry-mesh-lib" + + // Times t0, t3 at which the line position + t*velocity enters and leaves + // mesh, as off_intersect of interoff-lib: inside the mesh, t0 < 0 is the + // nearest hit behind and t3 the nearest ahead; outside, t0 and t3 are the + // next two hits ahead. With a single hit ahead t3 = FLT_MAX, with the mesh + // behind t3 = 0. n0, n3 (may be NULL) get the normals of these hits, with + // the orientation of interoff-lib: opposite to the right-hand rule of the + // vertex order in the file. + // Returns 2 when t0 and t3 are both hits, 1 when only t0 is, else 0. + #pragma acc routine seq + int + PowderN_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3, Coords* n0, Coords* n3) { + gmesh_hit ahead[GMESH_MAX_HITS]; + gmesh_hit behind[GMESH_MAX_HITS]; + // a hit on the surface the neutron sits on (|t| < tolerance) is counted ahead + double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); + int n_ahead = gmesh_intersect (mesh, position, velocity, -tolerance, ahead); + int n_behind = gmesh_intersect (mesh, position, coords_scale (velocity, -1), tolerance, behind); + int inside = n_ahead > 0 && n_behind > 0; + Coords no_normal = coords_set (0, 0, 0); + // gmesh normals follow the right-hand rule, unless the mesh was flipped + double normal_sign = mesh->flipped ? 1 : -1; + // normals of a closed, oriented mesh point outward: leaving first means inside + if (inside && mesh->closed && mesh->oriented) + inside = ahead[0].in_out < 0; + else if (inside && mesh->closed) + inside = gmesh_inside (mesh, position); + if (inside) { + *t0 = -behind[0].t; + *t3 = ahead[0].t; + if (n0) + *n0 = coords_scale (behind[0].normal, normal_sign); + if (n3) + *n3 = coords_scale (ahead[0].normal, normal_sign); + return 2; + } + if (n_ahead > 1) { + *t0 = ahead[0].t; + *t3 = ahead[1].t; + if (n0) + *n0 = coords_scale (ahead[0].normal, normal_sign); + if (n3) + *n3 = coords_scale (ahead[1].normal, normal_sign); + return 2; + } + if (n_ahead == 1) { + *t0 = ahead[0].t; + *t3 = FLT_MAX; + if (n0) + *n0 = coords_scale (ahead[0].normal, normal_sign); + if (n3) + *n3 = no_normal; + return 1; + } + if (n_behind) { + *t0 = -behind[0].t; + *t3 = 0; + if (n0) + *n0 = coords_scale (behind[0].normal, normal_sign); + if (n3) + *n3 = no_normal; + return 1; + } + return 0; + } /* Declare structures and functions only once in each instrument. */ #ifndef POWDERN_DECL #define POWDERN_DECL @@ -831,7 +896,7 @@ DECLARE %{ struct line_info_struct line_info; double* columns; - off_struct offdata; + gmesh_struct mesh; double tgt_x; double tgt_y; double tgt_z; @@ -875,7 +940,7 @@ INITIALIZE fprintf (stderr, "Error: You are attempting to use an OFF geometry without -DUSE_OFF. You will need to recompile with that define set!\n"); exit (-1); #else - if (off_init (geometry, xwidth, yheight, zdepth, 0, &offdata)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { line_info.shape = 3; thickness = 0; concentric = 0; @@ -1095,10 +1160,10 @@ TRACE #ifdef OPENACC #ifdef USE_OFF - off_struct thread_offdata = offdata; + gmesh_struct thread_mesh = mesh; #endif #else - #define thread_offdata offdata + #define thread_mesh mesh #endif if (line_info.V_0 > 0 && (line_info.count || line_info.my_inc)) { @@ -1114,7 +1179,7 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - intersect = off_intersect (&t0, &t3, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = PowderN_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL, NULL); intersecti = 0; } #endif @@ -1309,7 +1374,7 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - intersect = off_intersect (&t0, &t3, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = PowderN_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL, NULL); intersecti = 0; } #endif @@ -1383,7 +1448,7 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - intersect = off_intersect (&t0, &t3, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = PowderN_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL, NULL); intersecti = 0; } #endif @@ -1532,7 +1597,7 @@ MCDISPLAY circle ("xz", 0, 0, 0, radius); circle ("yz", 0, 0, 0, radius); } else if (line_info.shape == 3) { /* OFF file */ - off_display (offdata); + gmesh_display (&mesh); } } %} diff --git a/mcstas-comps/samples/Single_crystal.comp b/mcstas-comps/samples/Single_crystal.comp index ac29110677..7873d4446d 100644 --- a/mcstas-comps/samples/Single_crystal.comp +++ b/mcstas-comps/samples/Single_crystal.comp @@ -79,12 +79,12 @@ * box/plate: xwidth x yheight x zdepth * cylinder: radius x yheight * sphere: radius (yheight=0) -* any shape: geometry=OFF file +* any shape: geometry=OFF, PLY or STL file * * The complex geometry option handles any closed non-convex polyhedra. * It computes the intersection points of the neutron ray with the object * transparently, so that it can be used like a regular sample object. -* It supports the PLY, OFF and NOFF file format but not COFF (colored faces). +* It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. * Such files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off * or @@ -172,7 +172,7 @@ * xwidth: [m] Width of crystal * yheight: [m] Height of crystal * zdepth: [m] Depth of crystal (no extinction simulated) -* geometry: [str] Name of an Object File Format (OFF) or PLY file for complex geometry. The OFF/PLY file may be generated from XYZ coordinates using qhull/powercrust +* geometry: [str] Name of an OFF, PLY or STL file for complex geometry. An OFF file may be generated from XYZ coordinates using qhull/powercrust * delta_d_d: [1] Lattice spacing variance, gaussian RMS * mosaic: [arc minutes] Crystal mosaic (isotropic), gaussian RMS. Puts the crystal in the isotropic mosaic model state, thus disregarding other mosaicity parameters. * mosaic_a: [arc minutes] Horizontal (rotation around lattice vector a) mosaic (anisotropic), gaussian RMS. Put the crystal in the anisotropic crystal vector state. I.e. model mosaicity through rotation around the crystal lattice vectors. Has precedence over in-plane mosaic model. @@ -309,7 +309,72 @@ SHARE %{ /* used for reading data table from file */ %include "read_table-lib" - %include "interoff-lib" + %include "geometry-mesh-lib" + + // Times t0, t3 at which the line position + t*velocity enters and leaves + // mesh, as off_intersect of interoff-lib: inside the mesh, t0 < 0 is the + // nearest hit behind and t3 the nearest ahead; outside, t0 and t3 are the + // next two hits ahead. With a single hit ahead t3 = FLT_MAX, with the mesh + // behind t3 = 0. n0, n3 (may be NULL) get the normals of these hits, with + // the orientation of interoff-lib: opposite to the right-hand rule of the + // vertex order in the file. + // Returns 2 when t0 and t3 are both hits, 1 when only t0 is, else 0. + #pragma acc routine seq + int + Single_crystal_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3, Coords* n0, Coords* n3) { + gmesh_hit ahead[GMESH_MAX_HITS]; + gmesh_hit behind[GMESH_MAX_HITS]; + // a hit on the surface the neutron sits on (|t| < tolerance) is counted ahead + double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); + int n_ahead = gmesh_intersect (mesh, position, velocity, -tolerance, ahead); + int n_behind = gmesh_intersect (mesh, position, coords_scale (velocity, -1), tolerance, behind); + int inside = n_ahead > 0 && n_behind > 0; + Coords no_normal = coords_set (0, 0, 0); + // gmesh normals follow the right-hand rule, unless the mesh was flipped + double normal_sign = mesh->flipped ? 1 : -1; + // normals of a closed, oriented mesh point outward: leaving first means inside + if (inside && mesh->closed && mesh->oriented) + inside = ahead[0].in_out < 0; + else if (inside && mesh->closed) + inside = gmesh_inside (mesh, position); + if (inside) { + *t0 = -behind[0].t; + *t3 = ahead[0].t; + if (n0) + *n0 = coords_scale (behind[0].normal, normal_sign); + if (n3) + *n3 = coords_scale (ahead[0].normal, normal_sign); + return 2; + } + if (n_ahead > 1) { + *t0 = ahead[0].t; + *t3 = ahead[1].t; + if (n0) + *n0 = coords_scale (ahead[0].normal, normal_sign); + if (n3) + *n3 = coords_scale (ahead[1].normal, normal_sign); + return 2; + } + if (n_ahead == 1) { + *t0 = ahead[0].t; + *t3 = FLT_MAX; + if (n0) + *n0 = coords_scale (ahead[0].normal, normal_sign); + if (n3) + *n3 = no_normal; + return 1; + } + if (n_behind) { + *t0 = -behind[0].t; + *t3 = 0; + if (n0) + *n0 = coords_scale (behind[0].normal, normal_sign); + if (n3) + *n3 = no_normal; + return 1; + } + return 0; + } #ifndef OPENACC %include "opencl-lib" #endif @@ -1327,7 +1392,7 @@ SHARE DECLARE %{ struct hkl_info_struct hkl_info; - off_struct offdata; + gmesh_struct mesh; struct hkl_data* hkl_list; #ifndef OPENACC struct tau_data tau_list[MCSX_REFL_SLIST_SIZE]; @@ -1397,7 +1462,7 @@ INITIALIZE fprintf (stderr, "Error: You are attempting to use an OFF geometry without -DUSE_OFF. You will need to recompile with that define set!\n"); exit (-1); #else - if (off_init (geometry, xwidth, yheight, zdepth, 0, &offdata)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { hkl_info.shape = 3; } #endif @@ -1505,10 +1570,10 @@ TRACE #ifdef OPENACC #ifdef USE_OFF - off_struct thread_offdata = offdata; + gmesh_struct thread_mesh = mesh; #endif #else - #define thread_offdata offdata + #define thread_mesh mesh #endif /* Intersection neutron trajectory / sample (sample surface) */ @@ -1520,7 +1585,7 @@ TRACE intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (hkl_info.shape == 3) - intersect = off_intersect (&t1, &t2, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = Single_crystal_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL, NULL); #endif if (t2 < 0) intersect = 0; /* we passed sample volume already */ @@ -1568,7 +1633,7 @@ TRACE intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (hkl_info.shape == 3) - intersect = off_intersect (&t1, &t2, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = Single_crystal_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL, NULL); #endif if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { /* neutron is leaving the sample */ @@ -1956,7 +2021,7 @@ MCDISPLAY } else if (hkl_info.shape == 2) { /* sphere */ sphere (0, 0, 0, radius); } else if (hkl_info.shape == 3) { /* OFF file */ - off_display (offdata); + gmesh_display (&mesh); } %} END diff --git a/mcstas-comps/samples/Single_magnetic_crystal.comp b/mcstas-comps/samples/Single_magnetic_crystal.comp index d6841ed976..e813c9ef3a 100644 --- a/mcstas-comps/samples/Single_magnetic_crystal.comp +++ b/mcstas-comps/samples/Single_magnetic_crystal.comp @@ -35,12 +35,12 @@ * box/plate: xwidth x yheight x zdepth * cylinder: radius x yheight * sphere: radius (yheight=0) -* any shape: geometry=OFF file +* any shape: geometry=OFF, PLY or STL file * * The complex geometry option handles any closed non-convex polyhedra. * It computes the intersection points of the neutron ray with the object * transparently, so that it can be used like a regular sample object. -* It supports the OFF and NOFF file format but not COFF (colored faces). +* It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. * Such files may be generated from XYZ data using qhull/powercrust, and * viewed with geomview * The default size of the object depends of the OFF file data, but its @@ -55,7 +55,7 @@ * xwidth: [m] Width of crystal * yheight: [m] Height of crystal * zdepth: [m] Depth of crystal (no extinction simulated) -* geometry: [str] Name of an Object File Format (OFF) file for complex geometry. The OFF file may be generated from XYZ coordinates using qhull/powercrust +* geometry: [str] Name of an OFF, PLY or STL file for complex geometry. An OFF file may be generated from XYZ coordinates using qhull/powercrust * delta_d_d: [1] Lattice spacing variance, gaussian RMS * mosaic: [arcmin] Crystal mosaic (isotropic), gaussian RMS * mosaic_h: [arcmin] Horizontal (rotation around Y) mosaic (anisotropic), gaussian RMS @@ -110,8 +110,73 @@ SHARE %{ /* used for reading data table from file */ %include "read_table-lib" - %include "interoff-lib" + %include "geometry-mesh-lib" %include "mccode-complex-lib" + + // Times t0, t3 at which the line position + t*velocity enters and leaves + // mesh, as off_intersect of interoff-lib: inside the mesh, t0 < 0 is the + // nearest hit behind and t3 the nearest ahead; outside, t0 and t3 are the + // next two hits ahead. With a single hit ahead t3 = FLT_MAX, with the mesh + // behind t3 = 0. n0, n3 (may be NULL) get the normals of these hits, with + // the orientation of interoff-lib: opposite to the right-hand rule of the + // vertex order in the file. + // Returns 2 when t0 and t3 are both hits, 1 when only t0 is, else 0. + #pragma acc routine seq + int + Single_magnetic_crystal_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3, Coords* n0, Coords* n3) { + gmesh_hit ahead[GMESH_MAX_HITS]; + gmesh_hit behind[GMESH_MAX_HITS]; + // a hit on the surface the neutron sits on (|t| < tolerance) is counted ahead + double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); + int n_ahead = gmesh_intersect (mesh, position, velocity, -tolerance, ahead); + int n_behind = gmesh_intersect (mesh, position, coords_scale (velocity, -1), tolerance, behind); + int inside = n_ahead > 0 && n_behind > 0; + Coords no_normal = coords_set (0, 0, 0); + // gmesh normals follow the right-hand rule, unless the mesh was flipped + double normal_sign = mesh->flipped ? 1 : -1; + // normals of a closed, oriented mesh point outward: leaving first means inside + if (inside && mesh->closed && mesh->oriented) + inside = ahead[0].in_out < 0; + else if (inside && mesh->closed) + inside = gmesh_inside (mesh, position); + if (inside) { + *t0 = -behind[0].t; + *t3 = ahead[0].t; + if (n0) + *n0 = coords_scale (behind[0].normal, normal_sign); + if (n3) + *n3 = coords_scale (ahead[0].normal, normal_sign); + return 2; + } + if (n_ahead > 1) { + *t0 = ahead[0].t; + *t3 = ahead[1].t; + if (n0) + *n0 = coords_scale (ahead[0].normal, normal_sign); + if (n3) + *n3 = coords_scale (ahead[1].normal, normal_sign); + return 2; + } + if (n_ahead == 1) { + *t0 = ahead[0].t; + *t3 = FLT_MAX; + if (n0) + *n0 = coords_scale (ahead[0].normal, normal_sign); + if (n3) + *n3 = no_normal; + return 1; + } + if (n_behind) { + *t0 = -behind[0].t; + *t3 = 0; + if (n0) + *n0 = coords_scale (behind[0].normal, normal_sign); + if (n3) + *n3 = no_normal; + return 1; + } + return 0; + } // %include "columnfile" /* Declare structures and functions only once in each instrument. */ #ifndef SINGLE_MAGNETIC_CRYSTAL_DECL @@ -499,7 +564,7 @@ SHARE DECLARE %{ struct hkl_info_struct hkl_info; - off_struct offdata; + gmesh_struct mesh; %} INITIALIZE @@ -541,7 +606,7 @@ INITIALIZE hkl_info.shape = -1; /* -1:no shape, 0:cyl, 1:box, 2:sphere, 3:any-shape */ if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - if (off_init (geometry, xwidth, yheight, zdepth, 0, &offdata)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { hkl_info.shape = 3; } } else if (xwidth && yheight && zdepth) @@ -613,7 +678,7 @@ TRACE else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) - intersect = off_intersect (&t1, &t2, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, offdata); + intersect = Single_magnetic_crystal_mesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL, NULL); if (t2 < 0) intersect = 0; /* we passed sample volume already */ @@ -641,7 +706,7 @@ TRACE else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) - intersect = off_intersect (&t1, &t2, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, offdata); + intersect = Single_magnetic_crystal_mesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL, NULL); if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { /* neutron is leaving the sample */ if (hkl_info.flag_warning < 100) @@ -910,7 +975,7 @@ MCDISPLAY } else if (hkl_info.shape == 2) { /* sphere */ sphere (0, 0, 0, radius); } else if (hkl_info.shape == 3) { /* OFF file */ - off_display (offdata); + gmesh_display (&mesh); } %} END From 2e75bfdea73fd5bafa19b68798609334b570f324 Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Tue, 10 Nov 2026 15:47:04 +0100 Subject: [PATCH 04/22] Test_lib_geometry_mesh_lib: check gmesh_intersect_gravity INITIALIZE runs checks of gmesh_intersect_gravity and exits with an error when one fails: zero acceleration gives the hits of gmesh_intersect, parabolas through cube.off give the analytic times and in_out, and 2000 random parabolas through socket.off give the hits of a 4000-piece polyline of gmesh_intersect lines. The random rays use a fixed sequence, so the result does not depend on the seed. Co-Authored-By: Claude Opus 5.5 --- .../Test_lib_geometry_mesh_lib.instr | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr index 7de2f08790..e487331dbf 100644 --- a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr +++ b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr @@ -6,6 +6,12 @@ * * Smoke test: builds and runs with the library geometry-mesh-lib in the instrument. * Fails to compile or link if the library does not build with the runtime. +* INITIALIZE also checks gmesh_intersect_gravity, and exits with an error +* when a check fails: +* - with zero acceleration it gives the hits of gmesh_intersect +* - parabolas through cube.off give the analytic hit times and in_out +* - random parabolas through socket.off give the hits of a fine polyline of +* straight gmesh_intersect pieces * * %Test: Ncount=10 Detector: PSD_I=10 * @@ -16,11 +22,131 @@ DEFINE INSTRUMENT Test_lib_geometry_mesh_lib(Ncount=10) DECLARE %{ %include "geometry-mesh-lib" + +// counts a failed check and prints what failed +void gravity_check (int ok, const char* what, int* failures) { + if (!ok) { + fprintf (stderr, "Test_lib_geometry_mesh_lib: FAILED: %s\n", what); + (*failures)++; + } +} + +// point of the parabola position + time*velocity + time^2/2*acceleration +Coords gravity_parabola (Coords position, Coords velocity, Coords acceleration, double time) { + return coords_add (position, coords_add (coords_scale (velocity, time), coords_scale (acceleration, 0.5 * time * time))); +} + +// uniform number in [low, high] from a fixed sequence, independent of the seed +double gravity_uniform (unsigned long* state, double low, double high) { + *state = (*state * 6364136223846793005UL + 1442695040888963407UL) & 0xFFFFFFFFFFFFFFFFUL; + return low + (high - low) * (double)(*state >> 11) / 9007199254740992.0; +} + +// checks of gmesh_intersect_gravity; returns the number of failed checks +int gravity_tests (void) { + gmesh_struct cube; + gmesh_struct socket; + gmesh_hit hits[GMESH_MAX_HITS]; + gmesh_hit line_hits[GMESH_MAX_HITS]; + unsigned long state = 1234; + int failures = 0; + int nhits; + int i_ray; + int mismatches = 0; + double worst = 0; + + if (!gmesh_init ("cube.off", 1, GMESH_PROPS_IGNORE, &cube) || !gmesh_init_fit ("socket.off", 0.1, 0, 0, 1, GMESH_PROPS_IGNORE, &socket)) { + fprintf (stderr, "Test_lib_geometry_mesh_lib: FAILED: can not read cube.off or socket.off\n"); + return 1; + } + + // falling from rest above the unit cube: top at time sqrt(2), bottom at 2 + nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, 2, 0.5), coords_set (0, 0, 0), coords_set (0, -1, 0), 0, hits); + gravity_check (nhits == 2 && fabs (hits[0].t - sqrt (2)) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].t - 2) < 1e-12 && hits[1].in_out == -1, + "fall from rest through cube.off", &failures); + + // entering through the front face, leaving through the bottom at time sqrt(2) + nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, 0.5, -1), coords_set (0, 0, 1), coords_set (0, -0.5, 0), 0, hits); + gravity_check (nhits == 2 && fabs (hits[0].t - 1) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].t - sqrt (2)) < 1e-12 && hits[1].in_out == -1, + "front face in, bottom out of cube.off", &failures); + + // thrown up into the cube from below, falling out through the same face + nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, -1, 0.5), coords_set (0, 2, 0), coords_set (0, -1.2, 0), 0, hits); + gravity_check (nhits == 2 && fabs (hits[0].t - (2 - sqrt (1.6)) / 1.2) < 1e-12 && hits[0].in_out == 1 + && fabs (hits[1].t - (2 + sqrt (1.6)) / 1.2) < 1e-12 && hits[1].in_out == -1 && hits[0].face == hits[1].face, + "in and out through the bottom of cube.off", &failures); + + // t_min skips the hits before it + nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, -1, 0.5), coords_set (0, 2, 0), coords_set (0, -1.2, 0), 1, hits); + gravity_check (nhits == 1 && hits[0].in_out == -1, "t_min", &failures); + + // the arc passes above the cube + nhits = gmesh_intersect_gravity (&cube, coords_set (-1, 1.5, 0.5), coords_set (1, 0, 0), coords_set (0, -0.1, 0), 0, hits); + gravity_check (nhits == 0, "arc above cube.off", &failures); + + for (i_ray = 0; i_ray < 2000; i_ray++) { + Coords position = coords_set (gravity_uniform (&state, -0.1, 0.1), gravity_uniform (&state, -0.1, 0.1), gravity_uniform (&state, -0.1, 0.1)); + Coords velocity = coords_set (gravity_uniform (&state, -1, 1), gravity_uniform (&state, -1, 1), gravity_uniform (&state, -1, 1)); + Coords acceleration = coords_set (gravity_uniform (&state, -5, 5), gravity_uniform (&state, -5, 5), gravity_uniform (&state, -5, 5)); + double time_end = 0.2; + int nsteps = 4000; + double step = time_end / nsteps; + double line_times[GMESH_MAX_HITS]; + int nline = 0; + int nstraight; + int i_step; + int i_hit; + + // zero acceleration: the hits of the straight line + nstraight = gmesh_intersect (&socket, position, velocity, -FLT_MAX, line_hits); + nhits = gmesh_intersect_gravity (&socket, position, velocity, coords_set (0, 0, 0), -FLT_MAX, hits); + if (nhits != nstraight) { + mismatches++; + continue; + } + for (i_hit = 0; i_hit < nhits; i_hit++) { + if (hits[i_hit].t != line_hits[i_hit].t || hits[i_hit].tri != line_hits[i_hit].tri || hits[i_hit].in_out != line_hits[i_hit].in_out) + mismatches++; + } + + // the parabola against a polyline of nsteps straight pieces up to time_end + for (i_step = 0; i_step < nsteps; i_step++) { + Coords start = gravity_parabola (position, velocity, acceleration, i_step * step); + Coords chord = coords_sub (gravity_parabola (position, velocity, acceleration, (i_step + 1) * step), start); + int npiece = gmesh_intersect (&socket, start, chord, 0, line_hits); + int i_piece; + for (i_piece = 0; i_piece < npiece && line_hits[i_piece].t <= 1 && nline < GMESH_MAX_HITS; i_piece++) { + line_times[nline] = (i_step + line_hits[i_piece].t) * step; + nline++; + } + } + nhits = gmesh_intersect_gravity (&socket, position, velocity, acceleration, 0, hits); + while (nhits > 0 && hits[nhits - 1].t > time_end) + nhits--; + if (nhits != nline) { + mismatches++; + continue; + } + for (i_hit = 0; i_hit < nhits; i_hit++) { + if (fabs (hits[i_hit].t - line_times[i_hit]) > worst) + worst = fabs (hits[i_hit].t - line_times[i_hit]); + } + } + printf ("Test_lib_geometry_mesh_lib: socket.off: %d of 2000 random rays differ from the line or polyline, worst time difference %g s\n", mismatches, + worst); + gravity_check (mismatches == 0 && worst < 1e-6, "random parabolas through socket.off", &failures); + + gmesh_free (&cube); + gmesh_free (&socket); + return failures; +} %} INITIALIZE %{ mcset_ncount(Ncount); + if (gravity_tests ()) + exit (fprintf (stderr, "Test_lib_geometry_mesh_lib: gmesh_intersect_gravity checks failed\n")); %} TRACE From f095187f7d93fda1b7e39a291f60c39e02d49068 Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Tue, 10 Nov 2026 16:23:09 +0100 Subject: [PATCH 05/22] Act on the review of PR #99 - geometry-mesh-lib: gmesh_intersect and gmesh_trace are back to the straight-line code of mesh_lib. Parabolas have their own traversal, gmesh_trace_gravity, under a separate section. gmesh_hit_time returns the time of a hit, for TRACE sections where t is the particle time. - Samples: _mesh_intersect only searches ahead, skipping the surface the neutron sits on. A neutron inside gets t0 = 0 and t3 the next hit; outside, t0 and t3 are the next two hits. - Refractor uses the same function, plus the normal of the next hit and an inside flag for inbulk. Lens takes the next hit and its normal from Lens_mesh_next. - Guide_anyshape, Guide_anyshape_r: gmesh_intersect_gravity inline in TRACE. - The parked Monitor_Sqw and McXtrace Shape are left as on mesh_lib. Co-Authored-By: Claude Opus 5.5 --- common/lib/share/geometry-mesh-lib.c | 288 ++++++++++++------ common/lib/share/geometry-mesh-lib.h | 8 +- mcstas-comps/contrib/Lens.comp | 145 ++------- .../contrib/Single_crystal_inelastic.comp | 78 ++--- .../Test_lib_geometry_mesh_lib.instr | 6 +- mcstas-comps/obsolete/Guide_anyshape_r.comp | 21 +- mcstas-comps/optics/Guide_anyshape.comp | 21 +- mcstas-comps/optics/Refractor.comp | 88 ++---- .../parked/Test_Monitor_Sqw/Monitor_Sqw.comp | 8 +- mcstas-comps/samples/Incoherent.comp | 78 ++--- mcstas-comps/samples/Isotropic_Sqw.comp | 78 ++--- mcstas-comps/samples/PowderN.comp | 80 ++--- mcstas-comps/samples/Single_crystal.comp | 78 ++--- .../samples/Single_magnetic_crystal.comp | 78 ++--- mcxtrace-comps/misc/Shape.comp | 18 +- 15 files changed, 388 insertions(+), 685 deletions(-) diff --git a/common/lib/share/geometry-mesh-lib.c b/common/lib/share/geometry-mesh-lib.c index 72f649e8b2..3f21bfc347 100644 --- a/common/lib/share/geometry-mesh-lib.c +++ b/common/lib/share/geometry-mesh-lib.c @@ -1845,6 +1845,171 @@ gmesh_slab (Coords lower, Coords upper, Coords origin, Coords direction, double return 1; } + // Moeller-Trumbore intersection of the line origin + t*direction with a + // triangle of mesh, with tolerant barycentric coordinates. Returns 1 on a + // hit, with its t in *hit_t and *on_edge = 1 when it is close to an edge. + #pragma acc routine seq +static int +gmesh_tri_hit (gmesh_struct* mesh, int triangle, Coords origin, Coords direction, double* hit_t, int* on_edge) { + Coords v0 = mesh->vtx[mesh->tri[3 * triangle]]; + Coords v1 = mesh->vtx[mesh->tri[3 * triangle + 1]]; + Coords v2 = mesh->vtx[mesh->tri[3 * triangle + 2]]; + Coords e1 = coords_sub (v1, v0); + Coords e2 = coords_sub (v2, v0); + Coords p = coords_xp (direction, e2); + Coords s; + Coords q; + double det = coords_sp (e1, p); + double inv; + double u; + double v; + double eps = GMESH_EPSILON; + // parallel + if (fabs (det) <= 1e-12 * coords_len (e1) * coords_len (e2) * coords_len (direction)) + return 0; + inv = 1 / det; + s = coords_sub (origin, v0); + u = coords_sp (s, p) * inv; + if (u < -eps || u > 1 + eps) + return 0; + q = coords_xp (s, e1); + v = coords_sp (direction, q) * inv; + if (v < -eps || u + v > 1 + eps) + return 0; + *hit_t = coords_sp (e2, q) * inv; + *on_edge = (u < eps || v < eps); + if (u + v > 1 - eps) { + *on_edge = 1; + } + return 1; +} + + // BVH traversal for the line origin + t*direction, t > t_min. Stores the + // max_hits nearest hits in hits, sorted by t, with hits on shared edges and + // vertices kept once; every hit is visited, so a return value above + // *stored_count means hits were dropped. When hits is NULL the hits are only counted, each + // triangle hit once, with *ambiguous = 1 when a hit is on an edge or at the + // origin. Returns the number of hits found. + #pragma acc routine seq +static int +gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, gmesh_hit* hits, int max_hits, int* stored_count, int* ambiguous) { + int count_only = (hits == NULL); + int stack[GMESH_BVH_STACK + 2]; + int top = 0; + int total = 0; + double direction_length = coords_len (direction); + double t_tolerance; + if (direction_length <= 0) + return 0; + if (!mesh->nnodes) + return 0; + t_tolerance = 1e-9 * mesh->length_scale / direction_length; + stack[top] = 0; + top++; + while (top > 0) { + gmesh_node* node; + int t; + top--; + node = &mesh->node[stack[top]]; + if (!gmesh_slab (node->lo, node->hi, origin, direction, t_min, FLT_MAX)) + continue; + if (node->count == 0) { + // cannot happen: depth limited at build + if (top + 2 > GMESH_BVH_STACK) + continue; + stack[top] = node->first; + top++; + stack[top] = node->first + 1; + top++; + continue; + } + for (t = node->first; t < node->first + node->count; t++) { + double hit_t; + int on_edge; + int in_out; + int k; + int position; + int duplicate = 0; + gmesh_hit hit; + if (!gmesh_tri_hit (mesh, t, origin, direction, &hit_t, &on_edge)) + continue; + if (hit_t <= t_min) + continue; + if (coords_sp (mesh->tri_normal[t], direction) < 0) { + in_out = 1; + } else { + in_out = -1; + } + if (count_only) { + total++; + if (on_edge || fabs (hit_t) <= t_tolerance) + *ambiguous = 1; + continue; + } + hit.t = hit_t; + hit.normal = mesh->tri_normal[t]; + hit.tri = t; + hit.face = mesh->tri_face[t]; + hit.in_out = in_out; + hit.edge = on_edge; + // hits on shared edges/vertices: keep once + for (k = 0; k < *stored_count; k++) { + if (hits[k].in_out == in_out && fabs (hits[k].t - hit_t) <= t_tolerance) { + duplicate = 1; + break; + } + } + if (duplicate) + continue; + total++; + position = *stored_count; + if (position == max_hits) { + if (max_hits == 0) + continue; + if (hit_t >= hits[max_hits - 1].t) + continue; + position = max_hits - 1; + } else { + (*stored_count)++; + } + while (position > 0 && hits[position - 1].t > hit_t) { + hits[position] = hits[position - 1]; + position--; + } + hits[position] = hit; + } + } + return total; +} + +// set once a hit buffer overflow has been reported +int gmesh_overflow_warned = 0; + #pragma acc declare create(gmesh_overflow_warned) + + // intersections of a line with mesh, see geometry-mesh-lib.h + #pragma acc routine seq +int +gmesh_intersect (gmesh_struct* mesh, Coords position, Coords direction, double t_min, gmesh_hit hits[GMESH_MAX_HITS]) { + int stored = 0; + int ambiguous = 0; + int total = gmesh_trace (mesh, position, direction, t_min, hits, GMESH_MAX_HITS, &stored, &ambiguous); + if (total > stored && !gmesh_overflow_warned) { + gmesh_overflow_warned = 1; + fprintf (stderr, + "Warning: %s: a ray crossed the mesh %d times, but only the %d nearest hits are kept " + "(geometry-mesh-lib).\n" + " To keep more, add this line after the DEFINE INSTRUMENT line of your .instr file:\n" + " DEPENDENCY \" -DGMESH_MAX_HITS=%d \"\n" + " This warning is shown once.\n", + mesh->filename, total, GMESH_MAX_HITS, total); + } + return stored; +} + + // ========================================================================== + // Ray tracing of parabolas (e.g. under gravity) + // ========================================================================== + // real roots of quadratic*t^2 + linear*t + constant = 0 in roots[0..1], // sorted, solving the linear equation when quadratic is 0. Returns their number. #pragma acc routine seq @@ -1997,84 +2162,33 @@ gmesh_tri_hit_gravity (gmesh_struct* mesh, int triangle, Coords origin, Coords v return nhits; } - // Moeller-Trumbore intersection of the line origin + t*direction with a - // triangle of mesh, with tolerant barycentric coordinates. Returns 1 on a - // hit, with its t in *hit_t and *on_edge = 1 when it is close to an edge. + // BVH traversal for the parabola origin + t*velocity + t^2/2*acceleration, + // t > t_min. Stores the max_hits nearest hits in hits, sorted by t, with hits + // on shared edges and vertices kept once, as gmesh_trace does for a line. + // Returns the number of hits found; above *stored_count, hits were dropped. #pragma acc routine seq static int -gmesh_tri_hit (gmesh_struct* mesh, int triangle, Coords origin, Coords direction, double* hit_t, int* on_edge) { - Coords v0 = mesh->vtx[mesh->tri[3 * triangle]]; - Coords v1 = mesh->vtx[mesh->tri[3 * triangle + 1]]; - Coords v2 = mesh->vtx[mesh->tri[3 * triangle + 2]]; - Coords e1 = coords_sub (v1, v0); - Coords e2 = coords_sub (v2, v0); - Coords p = coords_xp (direction, e2); - Coords s; - Coords q; - double det = coords_sp (e1, p); - double inv; - double u; - double v; - double eps = GMESH_EPSILON; - // parallel - if (fabs (det) <= 1e-12 * coords_len (e1) * coords_len (e2) * coords_len (direction)) - return 0; - inv = 1 / det; - s = coords_sub (origin, v0); - u = coords_sp (s, p) * inv; - if (u < -eps || u > 1 + eps) - return 0; - q = coords_xp (s, e1); - v = coords_sp (direction, q) * inv; - if (v < -eps || u + v > 1 + eps) - return 0; - *hit_t = coords_sp (e2, q) * inv; - *on_edge = (u < eps || v < eps); - if (u + v > 1 - eps) { - *on_edge = 1; - } - return 1; -} - - // BVH traversal for the line origin + t*direction, or for the parabola - // origin + t*direction + t^2/2*acceleration when acceleration is not zero, - // t > t_min. Stores the - // max_hits nearest hits in hits, sorted by t, with hits on shared edges and - // vertices kept once; every hit is visited, so a return value above - // *stored_count means hits were dropped. When hits is NULL the hits are only counted, each - // triangle hit once, with *ambiguous = 1 when a hit is on an edge or at the - // origin. Returns the number of hits found. - #pragma acc routine seq -static int -gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, Coords acceleration, double t_min, gmesh_hit* hits, int max_hits, int* stored_count, - int* ambiguous) { - int count_only = (hits == NULL); - int gravity = (acceleration.x != 0 || acceleration.y != 0 || acceleration.z != 0); +gmesh_trace_gravity (gmesh_struct* mesh, Coords origin, Coords velocity, Coords acceleration, double t_min, gmesh_hit* hits, int max_hits, int* stored_count) { int stack[GMESH_BVH_STACK + 2]; int top = 0; int total = 0; - double direction_length = coords_len (direction); - double t_tolerance; // a parabola may start at rest: its speed over the mesh size is then about // sqrt(|acceleration| * size) - if (gravity) - direction_length += sqrt (coords_len (acceleration) * mesh->length_scale); - if (direction_length <= 0) + double speed = coords_len (velocity) + sqrt (coords_len (acceleration) * mesh->length_scale); + double t_tolerance; + if (speed <= 0) return 0; if (!mesh->nnodes) return 0; - t_tolerance = 1e-9 * mesh->length_scale / direction_length; + t_tolerance = 1e-9 * mesh->length_scale / speed; stack[top] = 0; top++; while (top > 0) { gmesh_node* node; - int t; + int triangle; top--; node = &mesh->node[stack[top]]; - if (gravity) { - if (!gmesh_slab_gravity (node->lo, node->hi, origin, direction, acceleration, t_min, FLT_MAX)) - continue; - } else if (!gmesh_slab (node->lo, node->hi, origin, direction, t_min, FLT_MAX)) + if (!gmesh_slab_gravity (node->lo, node->hi, origin, velocity, acceleration, t_min, FLT_MAX)) continue; if (node->count == 0) { // cannot happen: depth limited at build @@ -2086,19 +2200,14 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, Coords acceler top++; continue; } - for (t = node->first; t < node->first + node->count; t++) { + for (triangle = node->first; triangle < node->first + node->count; triangle++) { double crossing_t[2]; int crossing_edge[2]; - int ncrossings; + int ncrossings = gmesh_tri_hit_gravity (mesh, triangle, origin, velocity, acceleration, crossing_t, crossing_edge); int i_crossing; - if (gravity) - ncrossings = gmesh_tri_hit_gravity (mesh, t, origin, direction, acceleration, crossing_t, crossing_edge); - else - ncrossings = gmesh_tri_hit (mesh, t, origin, direction, &crossing_t[0], &crossing_edge[0]); for (i_crossing = 0; i_crossing < ncrossings; i_crossing++) { double hit_t = crossing_t[i_crossing]; - int on_edge = crossing_edge[i_crossing]; - Coords hit_velocity = direction; + Coords hit_velocity = coords_add (velocity, coords_scale (acceleration, hit_t)); int in_out; int k; int position; @@ -2106,25 +2215,17 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, Coords acceler gmesh_hit hit; if (hit_t <= t_min) continue; - if (gravity) - hit_velocity = coords_add (direction, coords_scale (acceleration, hit_t)); - if (coords_sp (mesh->tri_normal[t], hit_velocity) < 0) { + if (coords_sp (mesh->tri_normal[triangle], hit_velocity) < 0) { in_out = 1; } else { in_out = -1; } - if (count_only) { - total++; - if (on_edge || fabs (hit_t) <= t_tolerance) - *ambiguous = 1; - continue; - } hit.t = hit_t; - hit.normal = mesh->tri_normal[t]; - hit.tri = t; - hit.face = mesh->tri_face[t]; + hit.normal = mesh->tri_normal[triangle]; + hit.tri = triangle; + hit.face = mesh->tri_face[triangle]; hit.in_out = in_out; - hit.edge = on_edge; + hit.edge = crossing_edge[i_crossing]; // hits on shared edges/vertices: keep once for (k = 0; k < *stored_count; k++) { if (hits[k].in_out == in_out && fabs (hits[k].t - hit_t) <= t_tolerance) { @@ -2137,8 +2238,6 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, Coords acceler total++; position = *stored_count; if (position == max_hits) { - if (max_hits == 0) - continue; if (hit_t >= hits[max_hits - 1].t) continue; position = max_hits - 1; @@ -2156,21 +2255,16 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, Coords acceler return total; } -// set once a hit buffer overflow has been reported -int gmesh_overflow_warned = 0; - #pragma acc declare create(gmesh_overflow_warned) - // intersections of a parabola with mesh, see geometry-mesh-lib.h #pragma acc routine seq int gmesh_intersect_gravity (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, double t_min, gmesh_hit hits[GMESH_MAX_HITS]) { int stored = 0; - int ambiguous = 0; - int total = gmesh_trace (mesh, position, velocity, acceleration, t_min, hits, GMESH_MAX_HITS, &stored, &ambiguous); + int total = gmesh_trace_gravity (mesh, position, velocity, acceleration, t_min, hits, GMESH_MAX_HITS, &stored); if (total > stored && !gmesh_overflow_warned) { gmesh_overflow_warned = 1; fprintf (stderr, - "Warning: %s: a ray crossed the mesh %d times, but only the %d nearest hits are kept " + "Warning: %s: a parabola crossed the mesh %d times, but only the %d nearest hits are kept " "(geometry-mesh-lib).\n" " To keep more, add this line after the DEFINE INSTRUMENT line of your .instr file:\n" " DEPENDENCY \" -DGMESH_MAX_HITS=%d \"\n" @@ -2180,11 +2274,11 @@ gmesh_intersect_gravity (gmesh_struct* mesh, Coords position, Coords velocity, C return stored; } - // intersections of a line with mesh, see geometry-mesh-lib.h + // time of a hit, see geometry-mesh-lib.h #pragma acc routine seq -int -gmesh_intersect (gmesh_struct* mesh, Coords position, Coords direction, double t_min, gmesh_hit hits[GMESH_MAX_HITS]) { - return gmesh_intersect_gravity (mesh, position, direction, coords_set (0, 0, 0), t_min, hits); +double +gmesh_hit_time (gmesh_hit* hit) { + return hit->t; } // 1 when point is inside the box [lower, upper], borders included @@ -2214,7 +2308,7 @@ gmesh_inside (gmesh_struct* mesh, Coords position) { int stored = 0; int ambiguous = 0; Coords direction = coords_set (D[j][0], D[j][1], D[j][2]); - int n = gmesh_trace (mesh, position, direction, coords_set (0, 0, 0), 0, NULL, 0, &stored, &ambiguous); + int n = gmesh_trace (mesh, position, direction, 0, NULL, 0, &stored, &ambiguous); if (!ambiguous) return (int)(n % 2); votes += (int)(n % 2); diff --git a/common/lib/share/geometry-mesh-lib.h b/common/lib/share/geometry-mesh-lib.h index 12c510b49e..5ce23c706e 100644 --- a/common/lib/share/geometry-mesh-lib.h +++ b/common/lib/share/geometry-mesh-lib.h @@ -174,11 +174,17 @@ int gmesh_intersect (gmesh_struct* mesh, Coords position, Coords direction, doub // position + t*velocity + t^2/2*acceleration (e.g. a neutron under // gravity, with acceleration in the frame of the mesh). in_out refers to // the velocity at the hit, velocity + t*acceleration. With a zero - // acceleration the result is that of gmesh_intersect. + // acceleration the hits are those of gmesh_intersect, to rounding. // RETURN: number of hits stored #pragma acc routine seq int gmesh_intersect_gravity (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, double t_min, gmesh_hit hits[GMESH_MAX_HITS]); + // double gmesh_hit_time(gmesh_hit *hit) + // RETURN: hit->t. For component TRACE sections, where t is the particle + // time macro and hit.t can not be written. + #pragma acc routine seq +double gmesh_hit_time (gmesh_hit* hit); + // int gmesh_inside(gmesh_struct *mesh, Coords position) // ACTION: point in (closed) mesh test, parity of ray crossings along fixed // directions. Does not use random numbers. diff --git a/mcstas-comps/contrib/Lens.comp b/mcstas-comps/contrib/Lens.comp index 6a40a826e4..50b438ac21 100644 --- a/mcstas-comps/contrib/Lens.comp +++ b/mcstas-comps/contrib/Lens.comp @@ -125,69 +125,21 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" - // Times t0, t3 at which the line position + t*velocity enters and leaves - // mesh, as off_intersect of interoff-lib: inside the mesh, t0 < 0 is the - // nearest hit behind and t3 the nearest ahead; outside, t0 and t3 are the - // next two hits ahead. With a single hit ahead t3 = FLT_MAX, with the mesh - // behind t3 = 0. n0, n3 (may be NULL) get the normals of these hits, with - // the orientation of interoff-lib: opposite to the right-hand rule of the - // vertex order in the file. - // Returns 2 when t0 and t3 are both hits, 1 when only t0 is, else 0. + // time dt and normal of the next hit of the line position + t*velocity with + // mesh, skipping the surface the neutron sits on (hit at |t| < tolerance). + // The normal has the orientation of interoff-lib: opposite to the + // right-hand rule of the vertex order in the file. Returns 1 on a hit, else 0. #pragma acc routine seq int - Lens_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3, Coords* n0, Coords* n3) { - gmesh_hit ahead[GMESH_MAX_HITS]; - gmesh_hit behind[GMESH_MAX_HITS]; - // a hit on the surface the neutron sits on (|t| < tolerance) is counted ahead + Lens_mesh_next (gmesh_struct* mesh, Coords position, Coords velocity, double* dt, Coords* normal) { + gmesh_hit hits[GMESH_MAX_HITS]; double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - int n_ahead = gmesh_intersect (mesh, position, velocity, -tolerance, ahead); - int n_behind = gmesh_intersect (mesh, position, coords_scale (velocity, -1), tolerance, behind); - int inside = n_ahead > 0 && n_behind > 0; - Coords no_normal = coords_set (0, 0, 0); + if (!gmesh_intersect (mesh, position, velocity, tolerance, hits)) + return 0; + *dt = hits[0].t; // gmesh normals follow the right-hand rule, unless the mesh was flipped - double normal_sign = mesh->flipped ? 1 : -1; - // normals of a closed, oriented mesh point outward: leaving first means inside - if (inside && mesh->closed && mesh->oriented) - inside = ahead[0].in_out < 0; - else if (inside && mesh->closed) - inside = gmesh_inside (mesh, position); - if (inside) { - *t0 = -behind[0].t; - *t3 = ahead[0].t; - if (n0) - *n0 = coords_scale (behind[0].normal, normal_sign); - if (n3) - *n3 = coords_scale (ahead[0].normal, normal_sign); - return 2; - } - if (n_ahead > 1) { - *t0 = ahead[0].t; - *t3 = ahead[1].t; - if (n0) - *n0 = coords_scale (ahead[0].normal, normal_sign); - if (n3) - *n3 = coords_scale (ahead[1].normal, normal_sign); - return 2; - } - if (n_ahead == 1) { - *t0 = ahead[0].t; - *t3 = FLT_MAX; - if (n0) - *n0 = coords_scale (ahead[0].normal, normal_sign); - if (n3) - *n3 = no_normal; - return 1; - } - if (n_behind) { - *t0 = -behind[0].t; - *t3 = 0; - if (n0) - *n0 = coords_scale (behind[0].normal, normal_sign); - if (n3) - *n3 = no_normal; - return 1; - } - return 0; + *normal = coords_scale (hits[0].normal, mesh->flipped ? 1 : -1); + return 1; } /* Lens_roughness: function to rotate normal vector around axis for roughness @@ -344,28 +296,11 @@ TRACE nx = ny = nz = 0; #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - double t1 = 0, t0 = 0; - Coords n, n0, n1; - n = n0 = n1 = coords_set (0, 0, 0); - int intersect = Lens_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &n0, &n1); - if (!intersect) + Coords n; + if (Lens_mesh_next (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &dt, &n)) + coords_get (n, &nx, &ny, &nz); + else dt = -1; - else if (t0 < 0 && 0 < t1) { - dt = t1; - n = n1; - } else if (t1 < 0 && 0 < t0) { - dt = t0; - n = n0; - } else { - if (t1 < t0) { - dt = t1; - n = n1; - } else { - dt = t0; - n = n0; - } - } - coords_get (n, &nx, &ny, &nz); } else #endif if (r1 && !focus1) { @@ -455,28 +390,11 @@ TRACE nx = ny = nz = 0; #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - double t1 = 0, t0 = 0; - Coords n, n0, n1; - n = n0 = n1 = coords_set (0, 0, 0); - int intersect = Lens_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &n0, &n1); - if (!intersect) + Coords n; + if (Lens_mesh_next (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &dt, &n)) + coords_get (n, &nx, &ny, &nz); + else dt = -1; - else if (t0 < 0 && 0 < t1) { - dt = t1; - n = n1; - } else if (t1 < 0 && 0 < t0) { - dt = t0; - n = n0; - } else { - if (t1 < t0) { - dt = t1; - n = n1; - } else { - dt = t0; - n = n0; - } - } - coords_get (n, &nx, &ny, &nz); } else #endif if (r2 && !focus2) @@ -579,28 +497,11 @@ TRACE nx = ny = nz = 0; #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - double t1 = 0, t0 = 0; - Coords n, n0, n1; - n = n0 = n1 = coords_set (0, 0, 0); - int intersect = Lens_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &n0, &n1); - if (!intersect) + Coords n; + if (Lens_mesh_next (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &dt, &n)) + coords_get (n, &nx, &ny, &nz); + else dt = -1; - else if (t0 < 0 && 0 < t1) { - dt = t1; - n = n1; - } else if (t1 < 0 && 0 < t0) { - dt = t0; - n = n0; - } else { - if (t1 < t0) { - dt = t1; - n = n1; - } else { - dt = t0; - n = n0; - } - } - coords_get (n, &nx, &ny, &nz); } else #endif if (r2 && !focus2) diff --git a/mcstas-comps/contrib/Single_crystal_inelastic.comp b/mcstas-comps/contrib/Single_crystal_inelastic.comp index 6788403e0c..e012002aef 100644 --- a/mcstas-comps/contrib/Single_crystal_inelastic.comp +++ b/mcstas-comps/contrib/Single_crystal_inelastic.comp @@ -122,69 +122,29 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" - // Times t0, t3 at which the line position + t*velocity enters and leaves - // mesh, as off_intersect of interoff-lib: inside the mesh, t0 < 0 is the - // nearest hit behind and t3 the nearest ahead; outside, t0 and t3 are the - // next two hits ahead. With a single hit ahead t3 = FLT_MAX, with the mesh - // behind t3 = 0. n0, n3 (may be NULL) get the normals of these hits, with - // the orientation of interoff-lib: opposite to the right-hand rule of the - // vertex order in the file. - // Returns 2 when t0 and t3 are both hits, 1 when only t0 is, else 0. + // Next entry time t0 and exit time t3 of the line position + t*velocity + // through mesh, with t0 = 0 for a neutron that starts inside. The surface + // the neutron sits on (hit at |t| < tolerance) is skipped. + // Returns 1 when the mesh is ahead, 0 otherwise. #pragma acc routine seq int - Single_crystal_inelastic_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3, Coords* n0, Coords* n3) { - gmesh_hit ahead[GMESH_MAX_HITS]; - gmesh_hit behind[GMESH_MAX_HITS]; - // a hit on the surface the neutron sits on (|t| < tolerance) is counted ahead + Single_crystal_inelastic_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3) { + gmesh_hit hits[GMESH_MAX_HITS]; double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - int n_ahead = gmesh_intersect (mesh, position, velocity, -tolerance, ahead); - int n_behind = gmesh_intersect (mesh, position, coords_scale (velocity, -1), tolerance, behind); - int inside = n_ahead > 0 && n_behind > 0; - Coords no_normal = coords_set (0, 0, 0); - // gmesh normals follow the right-hand rule, unless the mesh was flipped - double normal_sign = mesh->flipped ? 1 : -1; + int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); + int inside; + if (!nhits) + return 0; // normals of a closed, oriented mesh point outward: leaving first means inside - if (inside && mesh->closed && mesh->oriented) - inside = ahead[0].in_out < 0; - else if (inside && mesh->closed) - inside = gmesh_inside (mesh, position); + inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); if (inside) { - *t0 = -behind[0].t; - *t3 = ahead[0].t; - if (n0) - *n0 = coords_scale (behind[0].normal, normal_sign); - if (n3) - *n3 = coords_scale (ahead[0].normal, normal_sign); - return 2; - } - if (n_ahead > 1) { - *t0 = ahead[0].t; - *t3 = ahead[1].t; - if (n0) - *n0 = coords_scale (ahead[0].normal, normal_sign); - if (n3) - *n3 = coords_scale (ahead[1].normal, normal_sign); - return 2; - } - if (n_ahead == 1) { - *t0 = ahead[0].t; - *t3 = FLT_MAX; - if (n0) - *n0 = coords_scale (ahead[0].normal, normal_sign); - if (n3) - *n3 = no_normal; - return 1; - } - if (n_behind) { - *t0 = -behind[0].t; - *t3 = 0; - if (n0) - *n0 = coords_scale (behind[0].normal, normal_sign); - if (n3) - *n3 = no_normal; - return 1; + *t0 = 0; + *t3 = hits[0].t; + } else { + *t0 = hits[0].t; + *t3 = nhits > 1 ? hits[1].t : hits[0].t; } - return 0; + return 1; } /* Declare structures and functions only once in each instrument. */ #ifndef SINGLE_CRYSTAL_DECL @@ -864,7 +824,7 @@ TRACE else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) - intersect = Single_crystal_inelastic_mesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL, NULL); + intersect = Single_crystal_inelastic_mesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2); if (t2 < 0) intersect = 0; /* we passed sample volume already */ @@ -898,7 +858,7 @@ TRACE else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) - intersect = Single_crystal_inelastic_mesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL, NULL); + intersect = Single_crystal_inelastic_mesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2); if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { /* neutron is leaving the sample */ if (hkl_info.flag_warning < 100) diff --git a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr index e487331dbf..013c8dcff9 100644 --- a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr +++ b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr @@ -8,7 +8,7 @@ * Fails to compile or link if the library does not build with the runtime. * INITIALIZE also checks gmesh_intersect_gravity, and exits with an error * when a check fails: -* - with zero acceleration it gives the hits of gmesh_intersect +* - with zero acceleration it gives the hits of gmesh_intersect, to rounding * - parabolas through cube.off give the analytic hit times and in_out * - random parabolas through socket.off give the hits of a fine polyline of * straight gmesh_intersect pieces @@ -97,7 +97,7 @@ int gravity_tests (void) { int i_step; int i_hit; - // zero acceleration: the hits of the straight line + // zero acceleration: the hits of the straight line, to rounding nstraight = gmesh_intersect (&socket, position, velocity, -FLT_MAX, line_hits); nhits = gmesh_intersect_gravity (&socket, position, velocity, coords_set (0, 0, 0), -FLT_MAX, hits); if (nhits != nstraight) { @@ -105,7 +105,7 @@ int gravity_tests (void) { continue; } for (i_hit = 0; i_hit < nhits; i_hit++) { - if (hits[i_hit].t != line_hits[i_hit].t || hits[i_hit].tri != line_hits[i_hit].tri || hits[i_hit].in_out != line_hits[i_hit].in_out) + if (fabs (hits[i_hit].t - line_hits[i_hit].t) > 1e-12 * (1 + fabs (line_hits[i_hit].t)) || hits[i_hit].in_out != line_hits[i_hit].in_out) mismatches++; } diff --git a/mcstas-comps/obsolete/Guide_anyshape_r.comp b/mcstas-comps/obsolete/Guide_anyshape_r.comp index 7e0e445daa..5f93a35c44 100644 --- a/mcstas-comps/obsolete/Guide_anyshape_r.comp +++ b/mcstas-comps/obsolete/Guide_anyshape_r.comp @@ -97,21 +97,6 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" %include "ref-lib" - -// first hit of the parabola position + t*velocity + t^2/2*acceleration with -// mesh ahead of the neutron: its time dt, the normal and the face hit. -// Returns 1 on a hit, 0 otherwise. -#pragma acc routine seq -int -Guide_anyshape_r_mesh_next (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, double* dt, Coords* normal, int* face) { - gmesh_hit hits[GMESH_MAX_HITS]; - if (!gmesh_intersect_gravity (mesh, position, velocity, acceleration, 0, hits)) - return 0; - *dt = hits[0].t; - *normal = hits[0].normal; - *face = hits[0].face; - return 1; -} %} DECLARE @@ -157,16 +142,20 @@ TRACE double dt = 0; int fi = 0; Coords n = { 0, 0, 0 }; + gmesh_hit hits[GMESH_MAX_HITS]; Coords acceleration = coords_set (0, 0, 0); if (mcgravitation) acceleration = rot_apply (_comp->_rotation_absolute, coords_set (0, -GRAVITY, 0)); /* next intersection with the object, along the parabola under gravity; PL: with the index of the face */ - intersect = Guide_anyshape_r_mesh_next (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, &dt, &n, &fi); + intersect = gmesh_intersect_gravity (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, 0, hits); /* exit loop when no intersection forward */ if (!intersect) break; + dt = gmesh_hit_time (&hits[0]); + n = hits[0].normal; + fi = hits[0].face; double nx, ny, nz; coords_get (n, &nx, &ny, &nz); diff --git a/mcstas-comps/optics/Guide_anyshape.comp b/mcstas-comps/optics/Guide_anyshape.comp index 842ee4d9b3..965912a5fa 100644 --- a/mcstas-comps/optics/Guide_anyshape.comp +++ b/mcstas-comps/optics/Guide_anyshape.comp @@ -101,21 +101,6 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" %include "ref-lib" - -// first hit of the parabola position + t*velocity + t^2/2*acceleration with -// mesh ahead of the neutron: its time dt, the normal and the face hit. -// Returns 1 on a hit, 0 otherwise. -#pragma acc routine seq -int -Guide_anyshape_mesh_next (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, double* dt, Coords* normal, int* face) { - gmesh_hit hits[GMESH_MAX_HITS]; - if (!gmesh_intersect_gravity (mesh, position, velocity, acceleration, 0, hits)) - return 0; - *dt = hits[0].t; - *normal = hits[0].normal; - *face = hits[0].face; - return 1; -} %} DECLARE @@ -168,16 +153,20 @@ TRACE double dt = 0; int fi = 0; Coords n = { 0, 0, 0 }; + gmesh_hit hits[GMESH_MAX_HITS]; Coords acceleration = coords_set (0, 0, 0); if (mcgravitation) acceleration = rot_apply (_comp->_rotation_absolute, coords_set (0, -GRAVITY, 0)); /* next intersection with the object, along the parabola under gravity */ - intersect = Guide_anyshape_mesh_next (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, &dt, &n, &fi); + intersect = gmesh_intersect_gravity (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, 0, hits); /* exit loop when no intersection forward */ if (!intersect) break; + dt = gmesh_hit_time (&hits[0]); + n = hits[0].normal; + fi = hits[0].face; double nx, ny, nz; coords_get (n, &nx, &ny, &nz); diff --git a/mcstas-comps/optics/Refractor.comp b/mcstas-comps/optics/Refractor.comp index 9e0a74fa0c..c78ba08049 100644 --- a/mcstas-comps/optics/Refractor.comp +++ b/mcstas-comps/optics/Refractor.comp @@ -157,69 +157,32 @@ SHARE %include "geometry-mesh-lib" %include "ref-lib" - // Times t0, t3 at which the line position + t*velocity enters and leaves - // mesh, as off_intersect of interoff-lib: inside the mesh, t0 < 0 is the - // nearest hit behind and t3 the nearest ahead; outside, t0 and t3 are the - // next two hits ahead. With a single hit ahead t3 = FLT_MAX, with the mesh - // behind t3 = 0. n0, n3 (may be NULL) get the normals of these hits, with - // the orientation of interoff-lib: opposite to the right-hand rule of the - // vertex order in the file. - // Returns 2 when t0 and t3 are both hits, 1 when only t0 is, else 0. + // Next entry time t0 and exit time t1 of the line position + t*velocity + // through mesh, with t0 = 0 and inside = 1 for a neutron that starts inside. + // normal is the normal at the next hit (the exit when inside), with the + // orientation of interoff-lib: opposite to the right-hand rule of the vertex + // order in the file. The surface the neutron sits on (hit at |t| < tolerance) + // is skipped. Returns 1 when the mesh is ahead, 0 otherwise. #pragma acc routine seq int - Refractor_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3, Coords* n0, Coords* n3) { - gmesh_hit ahead[GMESH_MAX_HITS]; - gmesh_hit behind[GMESH_MAX_HITS]; - // a hit on the surface the neutron sits on (|t| < tolerance) is counted ahead + Refractor_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t1, Coords* normal, int* inside) { + gmesh_hit hits[GMESH_MAX_HITS]; double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - int n_ahead = gmesh_intersect (mesh, position, velocity, -tolerance, ahead); - int n_behind = gmesh_intersect (mesh, position, coords_scale (velocity, -1), tolerance, behind); - int inside = n_ahead > 0 && n_behind > 0; - Coords no_normal = coords_set (0, 0, 0); - // gmesh normals follow the right-hand rule, unless the mesh was flipped - double normal_sign = mesh->flipped ? 1 : -1; + int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); + if (!nhits) + return 0; // normals of a closed, oriented mesh point outward: leaving first means inside - if (inside && mesh->closed && mesh->oriented) - inside = ahead[0].in_out < 0; - else if (inside && mesh->closed) - inside = gmesh_inside (mesh, position); - if (inside) { - *t0 = -behind[0].t; - *t3 = ahead[0].t; - if (n0) - *n0 = coords_scale (behind[0].normal, normal_sign); - if (n3) - *n3 = coords_scale (ahead[0].normal, normal_sign); - return 2; - } - if (n_ahead > 1) { - *t0 = ahead[0].t; - *t3 = ahead[1].t; - if (n0) - *n0 = coords_scale (ahead[0].normal, normal_sign); - if (n3) - *n3 = coords_scale (ahead[1].normal, normal_sign); - return 2; - } - if (n_ahead == 1) { - *t0 = ahead[0].t; - *t3 = FLT_MAX; - if (n0) - *n0 = coords_scale (ahead[0].normal, normal_sign); - if (n3) - *n3 = no_normal; - return 1; + *inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); + if (*inside) { + *t0 = 0; + *t1 = hits[0].t; + } else { + *t0 = hits[0].t; + *t1 = nhits > 1 ? hits[1].t : hits[0].t; } - if (n_behind) { - *t0 = -behind[0].t; - *t3 = 0; - if (n0) - *n0 = coords_scale (behind[0].normal, normal_sign); - if (n3) - *n3 = no_normal; - return 1; - } - return 0; + // gmesh normals follow the right-hand rule, unless the mesh was flipped + *normal = coords_scale (hits[0].normal, mesh->flipped ? 1 : -1); + return 1; } /* wrappers to intersection routines which also return the normal vector */ @@ -456,6 +419,7 @@ TRACE do { double nx = 0, ny = 0, nz = 0; double t0 = 0, t1 = 0, dt = 0; + int mesh_inside = 0; intersect = 0; iterations++; @@ -474,10 +438,10 @@ TRACE } #ifdef USE_OFF else if (geometry && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - Coords n0, n1; - intersect = Refractor_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &n0, &n1); + Coords normal; + intersect = Refractor_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &normal, &mesh_inside); if (intersect) { - coords_get (t0 <= 0 || t0 > t1 ? n1 : n0, &nx, &ny, &nz); + coords_get (normal, &nx, &ny, &nz); } } #endif @@ -506,7 +470,7 @@ TRACE char scatter_me = 0, refract_me = 0; double my_t = 0, d_path = 0; double v = 0, lambda = 0; - char inbulk = t0 < 0 && t1 > 0; + char inbulk = (t0 < 0 && t1 > 0) || mesh_inside; /* when density is given negative, we assume bulk refractive material is 'outside' or 'before' the shape */ diff --git a/mcstas-comps/parked/Test_Monitor_Sqw/Monitor_Sqw.comp b/mcstas-comps/parked/Test_Monitor_Sqw/Monitor_Sqw.comp index 6ec9007af2..3a0bd8e8ec 100644 --- a/mcstas-comps/parked/Test_Monitor_Sqw/Monitor_Sqw.comp +++ b/mcstas-comps/parked/Test_Monitor_Sqw/Monitor_Sqw.comp @@ -96,7 +96,7 @@ DECLARE MonitornD_Defines_type DEFS; MonitornD_Variables_type Vars; MCDETECTOR detector; - gmesh_struct mesh; + off_struct offdata; double mean_vx; double mean_vy; double mean_vz; @@ -145,7 +145,7 @@ INITIALIZE } int offflag=0; if (geometry && strlen(geometry) && strcmp(geometry,"NULL") && strcmp(geometry,"0")) - if (!gmesh_init_fit(geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { + if (!off_init( geometry, xwidth, yheight, zdepth, 0, &offdata )) { printf("Monitor_Sqw: %s could not initiate the OFF geometry. \n" " Defaulting to normal Monitor dimensions.\n", NAME_CURRENT_COMP); strcpy(geometry, ""); @@ -180,8 +180,8 @@ TRACE if (geometry && strlen(geometry) && strcmp(geometry,"NULL") && strcmp(geometry,"0")) { /* determine intersections with object */ - int face = 0; - intersect = Monitor_nD_MeshIntersect(&mesh, coords_set(x, y, z), coords_set(vx, vy, vz), &t0, &t1, &face); + intersect = off_intersect(&t0, &t1, NULL, NULL, + x,y,z, vx, vy, vz, 0, 0, 0, offdata ); } else if (abs(Vars.Flag_Shape) == DEFS.SHAPE_SQUARE) /* square xy */ { diff --git a/mcstas-comps/samples/Incoherent.comp b/mcstas-comps/samples/Incoherent.comp index 06cee3dae5..b9b4a370e5 100644 --- a/mcstas-comps/samples/Incoherent.comp +++ b/mcstas-comps/samples/Incoherent.comp @@ -128,69 +128,29 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" - // Times t0, t3 at which the line position + t*velocity enters and leaves - // mesh, as off_intersect of interoff-lib: inside the mesh, t0 < 0 is the - // nearest hit behind and t3 the nearest ahead; outside, t0 and t3 are the - // next two hits ahead. With a single hit ahead t3 = FLT_MAX, with the mesh - // behind t3 = 0. n0, n3 (may be NULL) get the normals of these hits, with - // the orientation of interoff-lib: opposite to the right-hand rule of the - // vertex order in the file. - // Returns 2 when t0 and t3 are both hits, 1 when only t0 is, else 0. + // Next entry time t0 and exit time t3 of the line position + t*velocity + // through mesh, with t0 = 0 for a neutron that starts inside. The surface + // the neutron sits on (hit at |t| < tolerance) is skipped. + // Returns 1 when the mesh is ahead, 0 otherwise. #pragma acc routine seq int - Incoherent_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3, Coords* n0, Coords* n3) { - gmesh_hit ahead[GMESH_MAX_HITS]; - gmesh_hit behind[GMESH_MAX_HITS]; - // a hit on the surface the neutron sits on (|t| < tolerance) is counted ahead + Incoherent_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3) { + gmesh_hit hits[GMESH_MAX_HITS]; double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - int n_ahead = gmesh_intersect (mesh, position, velocity, -tolerance, ahead); - int n_behind = gmesh_intersect (mesh, position, coords_scale (velocity, -1), tolerance, behind); - int inside = n_ahead > 0 && n_behind > 0; - Coords no_normal = coords_set (0, 0, 0); - // gmesh normals follow the right-hand rule, unless the mesh was flipped - double normal_sign = mesh->flipped ? 1 : -1; + int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); + int inside; + if (!nhits) + return 0; // normals of a closed, oriented mesh point outward: leaving first means inside - if (inside && mesh->closed && mesh->oriented) - inside = ahead[0].in_out < 0; - else if (inside && mesh->closed) - inside = gmesh_inside (mesh, position); + inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); if (inside) { - *t0 = -behind[0].t; - *t3 = ahead[0].t; - if (n0) - *n0 = coords_scale (behind[0].normal, normal_sign); - if (n3) - *n3 = coords_scale (ahead[0].normal, normal_sign); - return 2; - } - if (n_ahead > 1) { - *t0 = ahead[0].t; - *t3 = ahead[1].t; - if (n0) - *n0 = coords_scale (ahead[0].normal, normal_sign); - if (n3) - *n3 = coords_scale (ahead[1].normal, normal_sign); - return 2; - } - if (n_ahead == 1) { - *t0 = ahead[0].t; - *t3 = FLT_MAX; - if (n0) - *n0 = coords_scale (ahead[0].normal, normal_sign); - if (n3) - *n3 = no_normal; - return 1; - } - if (n_behind) { - *t0 = -behind[0].t; - *t3 = 0; - if (n0) - *n0 = coords_scale (behind[0].normal, normal_sign); - if (n3) - *n3 = no_normal; - return 1; + *t0 = 0; + *t3 = hits[0].t; + } else { + *t0 = hits[0].t; + *t3 = nhits > 1 ? hits[1].t : hits[0].t; } - return 0; + return 1; } struct StructVarsInc { double sigma_a; /* Absorption cross section per atom (barns) */ @@ -343,7 +303,7 @@ TRACE intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (VarsInc.shape == 3) - intersect = Incoherent_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL, NULL); + intersect = Incoherent_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3); #endif if (intersect) { @@ -540,7 +500,7 @@ TRACE intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (VarsInc.shape == 3) - intersect = Incoherent_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL, NULL); + intersect = Incoherent_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3); #endif flag_ishollow = 0; diff --git a/mcstas-comps/samples/Isotropic_Sqw.comp b/mcstas-comps/samples/Isotropic_Sqw.comp index b88dc13bb8..8600910c35 100644 --- a/mcstas-comps/samples/Isotropic_Sqw.comp +++ b/mcstas-comps/samples/Isotropic_Sqw.comp @@ -315,69 +315,29 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" - // Times t0, t3 at which the line position + t*velocity enters and leaves - // mesh, as off_intersect of interoff-lib: inside the mesh, t0 < 0 is the - // nearest hit behind and t3 the nearest ahead; outside, t0 and t3 are the - // next two hits ahead. With a single hit ahead t3 = FLT_MAX, with the mesh - // behind t3 = 0. n0, n3 (may be NULL) get the normals of these hits, with - // the orientation of interoff-lib: opposite to the right-hand rule of the - // vertex order in the file. - // Returns 2 when t0 and t3 are both hits, 1 when only t0 is, else 0. + // Next entry time t0 and exit time t3 of the line position + t*velocity + // through mesh, with t0 = 0 for a neutron that starts inside. The surface + // the neutron sits on (hit at |t| < tolerance) is skipped. + // Returns 1 when the mesh is ahead, 0 otherwise. #pragma acc routine seq int - Isotropic_Sqw_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3, Coords* n0, Coords* n3) { - gmesh_hit ahead[GMESH_MAX_HITS]; - gmesh_hit behind[GMESH_MAX_HITS]; - // a hit on the surface the neutron sits on (|t| < tolerance) is counted ahead + Isotropic_Sqw_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3) { + gmesh_hit hits[GMESH_MAX_HITS]; double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - int n_ahead = gmesh_intersect (mesh, position, velocity, -tolerance, ahead); - int n_behind = gmesh_intersect (mesh, position, coords_scale (velocity, -1), tolerance, behind); - int inside = n_ahead > 0 && n_behind > 0; - Coords no_normal = coords_set (0, 0, 0); - // gmesh normals follow the right-hand rule, unless the mesh was flipped - double normal_sign = mesh->flipped ? 1 : -1; + int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); + int inside; + if (!nhits) + return 0; // normals of a closed, oriented mesh point outward: leaving first means inside - if (inside && mesh->closed && mesh->oriented) - inside = ahead[0].in_out < 0; - else if (inside && mesh->closed) - inside = gmesh_inside (mesh, position); + inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); if (inside) { - *t0 = -behind[0].t; - *t3 = ahead[0].t; - if (n0) - *n0 = coords_scale (behind[0].normal, normal_sign); - if (n3) - *n3 = coords_scale (ahead[0].normal, normal_sign); - return 2; - } - if (n_ahead > 1) { - *t0 = ahead[0].t; - *t3 = ahead[1].t; - if (n0) - *n0 = coords_scale (ahead[0].normal, normal_sign); - if (n3) - *n3 = coords_scale (ahead[1].normal, normal_sign); - return 2; - } - if (n_ahead == 1) { - *t0 = ahead[0].t; - *t3 = FLT_MAX; - if (n0) - *n0 = coords_scale (ahead[0].normal, normal_sign); - if (n3) - *n3 = no_normal; - return 1; - } - if (n_behind) { - *t0 = -behind[0].t; - *t3 = 0; - if (n0) - *n0 = coords_scale (behind[0].normal, normal_sign); - if (n3) - *n3 = no_normal; - return 1; + *t0 = 0; + *t3 = hits[0].t; + } else { + *t0 = hits[0].t; + *t3 = nhits > 1 ? hits[1].t : hits[0].t; } - return 0; + return 1; } /* For the density of states S(w) */ @@ -2321,7 +2281,7 @@ TRACE intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (VarSqw.shape == 3) - intersect = Isotropic_Sqw_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL, NULL); + intersect = Isotropic_Sqw_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3); #endif } else { if (VarSqw.shape == 0) @@ -2334,7 +2294,7 @@ TRACE intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius - thickness); #ifdef USE_OFF else if (VarSqw.shape == 3) - intersect = Isotropic_Sqw_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL, NULL); + intersect = Isotropic_Sqw_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3); #endif } } else diff --git a/mcstas-comps/samples/PowderN.comp b/mcstas-comps/samples/PowderN.comp index 1bf548c25a..065aa213f4 100644 --- a/mcstas-comps/samples/PowderN.comp +++ b/mcstas-comps/samples/PowderN.comp @@ -226,69 +226,29 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" - // Times t0, t3 at which the line position + t*velocity enters and leaves - // mesh, as off_intersect of interoff-lib: inside the mesh, t0 < 0 is the - // nearest hit behind and t3 the nearest ahead; outside, t0 and t3 are the - // next two hits ahead. With a single hit ahead t3 = FLT_MAX, with the mesh - // behind t3 = 0. n0, n3 (may be NULL) get the normals of these hits, with - // the orientation of interoff-lib: opposite to the right-hand rule of the - // vertex order in the file. - // Returns 2 when t0 and t3 are both hits, 1 when only t0 is, else 0. + // Next entry time t0 and exit time t3 of the line position + t*velocity + // through mesh, with t0 = 0 for a neutron that starts inside. The surface + // the neutron sits on (hit at |t| < tolerance) is skipped. + // Returns 1 when the mesh is ahead, 0 otherwise. #pragma acc routine seq int - PowderN_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3, Coords* n0, Coords* n3) { - gmesh_hit ahead[GMESH_MAX_HITS]; - gmesh_hit behind[GMESH_MAX_HITS]; - // a hit on the surface the neutron sits on (|t| < tolerance) is counted ahead + PowderN_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3) { + gmesh_hit hits[GMESH_MAX_HITS]; double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - int n_ahead = gmesh_intersect (mesh, position, velocity, -tolerance, ahead); - int n_behind = gmesh_intersect (mesh, position, coords_scale (velocity, -1), tolerance, behind); - int inside = n_ahead > 0 && n_behind > 0; - Coords no_normal = coords_set (0, 0, 0); - // gmesh normals follow the right-hand rule, unless the mesh was flipped - double normal_sign = mesh->flipped ? 1 : -1; + int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); + int inside; + if (!nhits) + return 0; // normals of a closed, oriented mesh point outward: leaving first means inside - if (inside && mesh->closed && mesh->oriented) - inside = ahead[0].in_out < 0; - else if (inside && mesh->closed) - inside = gmesh_inside (mesh, position); + inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); if (inside) { - *t0 = -behind[0].t; - *t3 = ahead[0].t; - if (n0) - *n0 = coords_scale (behind[0].normal, normal_sign); - if (n3) - *n3 = coords_scale (ahead[0].normal, normal_sign); - return 2; - } - if (n_ahead > 1) { - *t0 = ahead[0].t; - *t3 = ahead[1].t; - if (n0) - *n0 = coords_scale (ahead[0].normal, normal_sign); - if (n3) - *n3 = coords_scale (ahead[1].normal, normal_sign); - return 2; - } - if (n_ahead == 1) { - *t0 = ahead[0].t; - *t3 = FLT_MAX; - if (n0) - *n0 = coords_scale (ahead[0].normal, normal_sign); - if (n3) - *n3 = no_normal; - return 1; - } - if (n_behind) { - *t0 = -behind[0].t; - *t3 = 0; - if (n0) - *n0 = coords_scale (behind[0].normal, normal_sign); - if (n3) - *n3 = no_normal; - return 1; + *t0 = 0; + *t3 = hits[0].t; + } else { + *t0 = hits[0].t; + *t3 = nhits > 1 ? hits[1].t : hits[0].t; } - return 0; + return 1; } /* Declare structures and functions only once in each instrument. */ #ifndef POWDERN_DECL @@ -1179,7 +1139,7 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - intersect = PowderN_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL, NULL); + intersect = PowderN_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3); intersecti = 0; } #endif @@ -1374,7 +1334,7 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - intersect = PowderN_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL, NULL); + intersect = PowderN_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3); intersecti = 0; } #endif @@ -1448,7 +1408,7 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - intersect = PowderN_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL, NULL); + intersect = PowderN_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3); intersecti = 0; } #endif diff --git a/mcstas-comps/samples/Single_crystal.comp b/mcstas-comps/samples/Single_crystal.comp index 7873d4446d..4db2cef6d3 100644 --- a/mcstas-comps/samples/Single_crystal.comp +++ b/mcstas-comps/samples/Single_crystal.comp @@ -311,69 +311,29 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" - // Times t0, t3 at which the line position + t*velocity enters and leaves - // mesh, as off_intersect of interoff-lib: inside the mesh, t0 < 0 is the - // nearest hit behind and t3 the nearest ahead; outside, t0 and t3 are the - // next two hits ahead. With a single hit ahead t3 = FLT_MAX, with the mesh - // behind t3 = 0. n0, n3 (may be NULL) get the normals of these hits, with - // the orientation of interoff-lib: opposite to the right-hand rule of the - // vertex order in the file. - // Returns 2 when t0 and t3 are both hits, 1 when only t0 is, else 0. + // Next entry time t0 and exit time t3 of the line position + t*velocity + // through mesh, with t0 = 0 for a neutron that starts inside. The surface + // the neutron sits on (hit at |t| < tolerance) is skipped. + // Returns 1 when the mesh is ahead, 0 otherwise. #pragma acc routine seq int - Single_crystal_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3, Coords* n0, Coords* n3) { - gmesh_hit ahead[GMESH_MAX_HITS]; - gmesh_hit behind[GMESH_MAX_HITS]; - // a hit on the surface the neutron sits on (|t| < tolerance) is counted ahead + Single_crystal_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3) { + gmesh_hit hits[GMESH_MAX_HITS]; double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - int n_ahead = gmesh_intersect (mesh, position, velocity, -tolerance, ahead); - int n_behind = gmesh_intersect (mesh, position, coords_scale (velocity, -1), tolerance, behind); - int inside = n_ahead > 0 && n_behind > 0; - Coords no_normal = coords_set (0, 0, 0); - // gmesh normals follow the right-hand rule, unless the mesh was flipped - double normal_sign = mesh->flipped ? 1 : -1; + int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); + int inside; + if (!nhits) + return 0; // normals of a closed, oriented mesh point outward: leaving first means inside - if (inside && mesh->closed && mesh->oriented) - inside = ahead[0].in_out < 0; - else if (inside && mesh->closed) - inside = gmesh_inside (mesh, position); + inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); if (inside) { - *t0 = -behind[0].t; - *t3 = ahead[0].t; - if (n0) - *n0 = coords_scale (behind[0].normal, normal_sign); - if (n3) - *n3 = coords_scale (ahead[0].normal, normal_sign); - return 2; - } - if (n_ahead > 1) { - *t0 = ahead[0].t; - *t3 = ahead[1].t; - if (n0) - *n0 = coords_scale (ahead[0].normal, normal_sign); - if (n3) - *n3 = coords_scale (ahead[1].normal, normal_sign); - return 2; - } - if (n_ahead == 1) { - *t0 = ahead[0].t; - *t3 = FLT_MAX; - if (n0) - *n0 = coords_scale (ahead[0].normal, normal_sign); - if (n3) - *n3 = no_normal; - return 1; - } - if (n_behind) { - *t0 = -behind[0].t; - *t3 = 0; - if (n0) - *n0 = coords_scale (behind[0].normal, normal_sign); - if (n3) - *n3 = no_normal; - return 1; + *t0 = 0; + *t3 = hits[0].t; + } else { + *t0 = hits[0].t; + *t3 = nhits > 1 ? hits[1].t : hits[0].t; } - return 0; + return 1; } #ifndef OPENACC %include "opencl-lib" @@ -1585,7 +1545,7 @@ TRACE intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (hkl_info.shape == 3) - intersect = Single_crystal_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL, NULL); + intersect = Single_crystal_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2); #endif if (t2 < 0) intersect = 0; /* we passed sample volume already */ @@ -1633,7 +1593,7 @@ TRACE intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (hkl_info.shape == 3) - intersect = Single_crystal_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL, NULL); + intersect = Single_crystal_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2); #endif if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { /* neutron is leaving the sample */ diff --git a/mcstas-comps/samples/Single_magnetic_crystal.comp b/mcstas-comps/samples/Single_magnetic_crystal.comp index e813c9ef3a..399fe86478 100644 --- a/mcstas-comps/samples/Single_magnetic_crystal.comp +++ b/mcstas-comps/samples/Single_magnetic_crystal.comp @@ -113,69 +113,29 @@ SHARE %include "geometry-mesh-lib" %include "mccode-complex-lib" - // Times t0, t3 at which the line position + t*velocity enters and leaves - // mesh, as off_intersect of interoff-lib: inside the mesh, t0 < 0 is the - // nearest hit behind and t3 the nearest ahead; outside, t0 and t3 are the - // next two hits ahead. With a single hit ahead t3 = FLT_MAX, with the mesh - // behind t3 = 0. n0, n3 (may be NULL) get the normals of these hits, with - // the orientation of interoff-lib: opposite to the right-hand rule of the - // vertex order in the file. - // Returns 2 when t0 and t3 are both hits, 1 when only t0 is, else 0. + // Next entry time t0 and exit time t3 of the line position + t*velocity + // through mesh, with t0 = 0 for a neutron that starts inside. The surface + // the neutron sits on (hit at |t| < tolerance) is skipped. + // Returns 1 when the mesh is ahead, 0 otherwise. #pragma acc routine seq int - Single_magnetic_crystal_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3, Coords* n0, Coords* n3) { - gmesh_hit ahead[GMESH_MAX_HITS]; - gmesh_hit behind[GMESH_MAX_HITS]; - // a hit on the surface the neutron sits on (|t| < tolerance) is counted ahead + Single_magnetic_crystal_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3) { + gmesh_hit hits[GMESH_MAX_HITS]; double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - int n_ahead = gmesh_intersect (mesh, position, velocity, -tolerance, ahead); - int n_behind = gmesh_intersect (mesh, position, coords_scale (velocity, -1), tolerance, behind); - int inside = n_ahead > 0 && n_behind > 0; - Coords no_normal = coords_set (0, 0, 0); - // gmesh normals follow the right-hand rule, unless the mesh was flipped - double normal_sign = mesh->flipped ? 1 : -1; + int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); + int inside; + if (!nhits) + return 0; // normals of a closed, oriented mesh point outward: leaving first means inside - if (inside && mesh->closed && mesh->oriented) - inside = ahead[0].in_out < 0; - else if (inside && mesh->closed) - inside = gmesh_inside (mesh, position); + inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); if (inside) { - *t0 = -behind[0].t; - *t3 = ahead[0].t; - if (n0) - *n0 = coords_scale (behind[0].normal, normal_sign); - if (n3) - *n3 = coords_scale (ahead[0].normal, normal_sign); - return 2; + *t0 = 0; + *t3 = hits[0].t; + } else { + *t0 = hits[0].t; + *t3 = nhits > 1 ? hits[1].t : hits[0].t; } - if (n_ahead > 1) { - *t0 = ahead[0].t; - *t3 = ahead[1].t; - if (n0) - *n0 = coords_scale (ahead[0].normal, normal_sign); - if (n3) - *n3 = coords_scale (ahead[1].normal, normal_sign); - return 2; - } - if (n_ahead == 1) { - *t0 = ahead[0].t; - *t3 = FLT_MAX; - if (n0) - *n0 = coords_scale (ahead[0].normal, normal_sign); - if (n3) - *n3 = no_normal; - return 1; - } - if (n_behind) { - *t0 = -behind[0].t; - *t3 = 0; - if (n0) - *n0 = coords_scale (behind[0].normal, normal_sign); - if (n3) - *n3 = no_normal; - return 1; - } - return 0; + return 1; } // %include "columnfile" /* Declare structures and functions only once in each instrument. */ @@ -678,7 +638,7 @@ TRACE else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) - intersect = Single_magnetic_crystal_mesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL, NULL); + intersect = Single_magnetic_crystal_mesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2); if (t2 < 0) intersect = 0; /* we passed sample volume already */ @@ -706,7 +666,7 @@ TRACE else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) - intersect = Single_magnetic_crystal_mesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL, NULL); + intersect = Single_magnetic_crystal_mesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2); if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { /* neutron is leaving the sample */ if (hkl_info.flag_warning < 100) diff --git a/mcxtrace-comps/misc/Shape.comp b/mcxtrace-comps/misc/Shape.comp index 4c300b94b9..a8511c1f15 100644 --- a/mcxtrace-comps/misc/Shape.comp +++ b/mcxtrace-comps/misc/Shape.comp @@ -25,12 +25,12 @@ * cylinder: radius x yheight (thickness=0) * hollow cylinder: radius x yheight and thickness>0 * sphere: radius (yheight=0 thickness=0) -* any shape: geometry=OFF, PLY or STL file +* any shape: geometry=OFF file * * The complex geometry option handles any closed non-convex polyhedra. * It computes the intersection points of the X-ray with the object * transparently, so that it can be used like a regular sample object. -* It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. +* It supports the OFF and NOFF file format but not COFF (colored faces). * Such files may be generated from XYZ data using qhull/powercrust, and * viewed with geomview * The default size of the object depends of the OFF file data, but its @@ -46,8 +46,8 @@ * yheight: [m] Vert. dimension of sample (bounding box if off file), as a height. A sphere shape is used when 0 and radius is set * zdepth: [m] Depth of sample (bounding box if off file) * thickness: [m] Thickness of hollow box or cylinder (spheres are drawn solid) -* geometry: [str] Name of an OFF, PLY or STL file for complex geometry. The OFF file may be generated from XYZ coordinates using qhull/powercrust -* center: [1] Flag to centre the bounding box of the mesh on the origin. +* geometry: [str] Name of an Object File Format (OFF) file for complex geometry. The OFF file may be generated from XYZ coordinates using qhull/powercrust +* center: [1] Flag to determine if OFF object is centered on its centre of mass. * nx: [1] x-comp of orientation vector * ny: [1] y-comp of orientation vector * nz: [1] z-comp of orientation vector @@ -67,18 +67,18 @@ SETTING PARAMETERS (string geometry=0, radius=0, xwidth=0, yheight=0, zdepth=0, SHARE %{ %include "read_table-lib" -%include "geometry-mesh-lib" +%include "interoff-lib" %} DECLARE %{ - gmesh_struct mesh; + off_struct offdata; %} INITIALIZE %{ if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_IGNORE, &mesh)) { + if (off_init (geometry, xwidth, yheight, zdepth, !center, &offdata)) { thickness = 0; } } @@ -91,8 +91,8 @@ TRACE %{ MCDISPLAY %{ - if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* mesh file */ - gmesh_display (&mesh); + if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* OFF file */ + off_display (offdata); } else if (radius > 0 && yheight) { /* cylinder along y*/ cylinder (0, 0, 0, radius, yheight, thickness, nx, ny, nz); } else if (xwidth && yheight) { /* box/rectangle */ From f6a7235eac073d01f82d75f9a3da38f3167531c8 Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Tue, 10 Nov 2026 16:33:46 +0100 Subject: [PATCH 06/22] geometry-mesh-lib: rename gmesh_hit.t to time, drop gmesh_hit_time In component TRACE sections t is the particle-time macro, so hit.t did not compile there. With the field named time, TRACE reads hits[i].time directly and the gmesh_hit_time accessor is removed. All users are updated: geometry-mesh-lib, the McStas components and libtest instrument of this branch, monitor_nd-lib, and the McStas and McXtrace union-lib. Co-Authored-By: Claude Opus 5.5 --- common/lib/share/geometry-mesh-lib.c | 23 +++++++------------ common/lib/share/geometry-mesh-lib.h | 8 +------ mcstas-comps/contrib/Lens.comp | 2 +- .../contrib/Single_crystal_inelastic.comp | 6 ++--- .../Test_lib_geometry_mesh_lib.instr | 20 ++++++++-------- mcstas-comps/obsolete/Guide_anyshape_r.comp | 2 +- mcstas-comps/optics/Guide_anyshape.comp | 2 +- mcstas-comps/optics/Refractor.comp | 6 ++--- mcstas-comps/samples/Incoherent.comp | 6 ++--- mcstas-comps/samples/Isotropic_Sqw.comp | 6 ++--- mcstas-comps/samples/PowderN.comp | 6 ++--- mcstas-comps/samples/Single_crystal.comp | 6 ++--- .../samples/Single_magnetic_crystal.comp | 6 ++--- mcstas-comps/share/monitor_nd-lib.c | 8 +++---- mcstas-comps/share/union-lib.c | 2 +- mcxtrace-comps/share/union-lib.c | 2 +- 16 files changed, 49 insertions(+), 62 deletions(-) diff --git a/common/lib/share/geometry-mesh-lib.c b/common/lib/share/geometry-mesh-lib.c index 3f21bfc347..f0e552ace0 100644 --- a/common/lib/share/geometry-mesh-lib.c +++ b/common/lib/share/geometry-mesh-lib.c @@ -1946,7 +1946,7 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, *ambiguous = 1; continue; } - hit.t = hit_t; + hit.time = hit_t; hit.normal = mesh->tri_normal[t]; hit.tri = t; hit.face = mesh->tri_face[t]; @@ -1954,7 +1954,7 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, hit.edge = on_edge; // hits on shared edges/vertices: keep once for (k = 0; k < *stored_count; k++) { - if (hits[k].in_out == in_out && fabs (hits[k].t - hit_t) <= t_tolerance) { + if (hits[k].in_out == in_out && fabs (hits[k].time - hit_t) <= t_tolerance) { duplicate = 1; break; } @@ -1966,13 +1966,13 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, if (position == max_hits) { if (max_hits == 0) continue; - if (hit_t >= hits[max_hits - 1].t) + if (hit_t >= hits[max_hits - 1].time) continue; position = max_hits - 1; } else { (*stored_count)++; } - while (position > 0 && hits[position - 1].t > hit_t) { + while (position > 0 && hits[position - 1].time > hit_t) { hits[position] = hits[position - 1]; position--; } @@ -2220,7 +2220,7 @@ gmesh_trace_gravity (gmesh_struct* mesh, Coords origin, Coords velocity, Coords } else { in_out = -1; } - hit.t = hit_t; + hit.time = hit_t; hit.normal = mesh->tri_normal[triangle]; hit.tri = triangle; hit.face = mesh->tri_face[triangle]; @@ -2228,7 +2228,7 @@ gmesh_trace_gravity (gmesh_struct* mesh, Coords origin, Coords velocity, Coords hit.edge = crossing_edge[i_crossing]; // hits on shared edges/vertices: keep once for (k = 0; k < *stored_count; k++) { - if (hits[k].in_out == in_out && fabs (hits[k].t - hit_t) <= t_tolerance) { + if (hits[k].in_out == in_out && fabs (hits[k].time - hit_t) <= t_tolerance) { duplicate = 1; break; } @@ -2238,13 +2238,13 @@ gmesh_trace_gravity (gmesh_struct* mesh, Coords origin, Coords velocity, Coords total++; position = *stored_count; if (position == max_hits) { - if (hit_t >= hits[max_hits - 1].t) + if (hit_t >= hits[max_hits - 1].time) continue; position = max_hits - 1; } else { (*stored_count)++; } - while (position > 0 && hits[position - 1].t > hit_t) { + while (position > 0 && hits[position - 1].time > hit_t) { hits[position] = hits[position - 1]; position--; } @@ -2274,13 +2274,6 @@ gmesh_intersect_gravity (gmesh_struct* mesh, Coords position, Coords velocity, C return stored; } - // time of a hit, see geometry-mesh-lib.h - #pragma acc routine seq -double -gmesh_hit_time (gmesh_hit* hit) { - return hit->t; -} - // 1 when point is inside the box [lower, upper], borders included #pragma acc routine seq static int diff --git a/common/lib/share/geometry-mesh-lib.h b/common/lib/share/geometry-mesh-lib.h index 5ce23c706e..02212020e5 100644 --- a/common/lib/share/geometry-mesh-lib.h +++ b/common/lib/share/geometry-mesh-lib.h @@ -83,7 +83,7 @@ #endif typedef struct gmesh_hit { - double t; // position along the line: pos + t*dir + double time; // position along the line: pos + time*dir Coords normal; // unit normal of the triangle hit (outward for closed oriented meshes) int tri; // triangle index (BVH order) int face; // index of the face (polygon) in the file, 0-based @@ -179,12 +179,6 @@ int gmesh_intersect (gmesh_struct* mesh, Coords position, Coords direction, doub #pragma acc routine seq int gmesh_intersect_gravity (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, double t_min, gmesh_hit hits[GMESH_MAX_HITS]); - // double gmesh_hit_time(gmesh_hit *hit) - // RETURN: hit->t. For component TRACE sections, where t is the particle - // time macro and hit.t can not be written. - #pragma acc routine seq -double gmesh_hit_time (gmesh_hit* hit); - // int gmesh_inside(gmesh_struct *mesh, Coords position) // ACTION: point in (closed) mesh test, parity of ray crossings along fixed // directions. Does not use random numbers. diff --git a/mcstas-comps/contrib/Lens.comp b/mcstas-comps/contrib/Lens.comp index 50b438ac21..ee169162db 100644 --- a/mcstas-comps/contrib/Lens.comp +++ b/mcstas-comps/contrib/Lens.comp @@ -136,7 +136,7 @@ SHARE double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); if (!gmesh_intersect (mesh, position, velocity, tolerance, hits)) return 0; - *dt = hits[0].t; + *dt = hits[0].time; // gmesh normals follow the right-hand rule, unless the mesh was flipped *normal = coords_scale (hits[0].normal, mesh->flipped ? 1 : -1); return 1; diff --git a/mcstas-comps/contrib/Single_crystal_inelastic.comp b/mcstas-comps/contrib/Single_crystal_inelastic.comp index e012002aef..ee9c0cb857 100644 --- a/mcstas-comps/contrib/Single_crystal_inelastic.comp +++ b/mcstas-comps/contrib/Single_crystal_inelastic.comp @@ -139,10 +139,10 @@ SHARE inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); if (inside) { *t0 = 0; - *t3 = hits[0].t; + *t3 = hits[0].time; } else { - *t0 = hits[0].t; - *t3 = nhits > 1 ? hits[1].t : hits[0].t; + *t0 = hits[0].time; + *t3 = nhits > 1 ? hits[1].time : hits[0].time; } return 1; } diff --git a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr index 013c8dcff9..273af62f37 100644 --- a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr +++ b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr @@ -62,18 +62,18 @@ int gravity_tests (void) { // falling from rest above the unit cube: top at time sqrt(2), bottom at 2 nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, 2, 0.5), coords_set (0, 0, 0), coords_set (0, -1, 0), 0, hits); - gravity_check (nhits == 2 && fabs (hits[0].t - sqrt (2)) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].t - 2) < 1e-12 && hits[1].in_out == -1, + gravity_check (nhits == 2 && fabs (hits[0].time - sqrt (2)) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].time - 2) < 1e-12 && hits[1].in_out == -1, "fall from rest through cube.off", &failures); // entering through the front face, leaving through the bottom at time sqrt(2) nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, 0.5, -1), coords_set (0, 0, 1), coords_set (0, -0.5, 0), 0, hits); - gravity_check (nhits == 2 && fabs (hits[0].t - 1) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].t - sqrt (2)) < 1e-12 && hits[1].in_out == -1, + gravity_check (nhits == 2 && fabs (hits[0].time - 1) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].time - sqrt (2)) < 1e-12 && hits[1].in_out == -1, "front face in, bottom out of cube.off", &failures); // thrown up into the cube from below, falling out through the same face nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, -1, 0.5), coords_set (0, 2, 0), coords_set (0, -1.2, 0), 0, hits); - gravity_check (nhits == 2 && fabs (hits[0].t - (2 - sqrt (1.6)) / 1.2) < 1e-12 && hits[0].in_out == 1 - && fabs (hits[1].t - (2 + sqrt (1.6)) / 1.2) < 1e-12 && hits[1].in_out == -1 && hits[0].face == hits[1].face, + gravity_check (nhits == 2 && fabs (hits[0].time - (2 - sqrt (1.6)) / 1.2) < 1e-12 && hits[0].in_out == 1 + && fabs (hits[1].time - (2 + sqrt (1.6)) / 1.2) < 1e-12 && hits[1].in_out == -1 && hits[0].face == hits[1].face, "in and out through the bottom of cube.off", &failures); // t_min skips the hits before it @@ -105,7 +105,7 @@ int gravity_tests (void) { continue; } for (i_hit = 0; i_hit < nhits; i_hit++) { - if (fabs (hits[i_hit].t - line_hits[i_hit].t) > 1e-12 * (1 + fabs (line_hits[i_hit].t)) || hits[i_hit].in_out != line_hits[i_hit].in_out) + if (fabs (hits[i_hit].time - line_hits[i_hit].time) > 1e-12 * (1 + fabs (line_hits[i_hit].time)) || hits[i_hit].in_out != line_hits[i_hit].in_out) mismatches++; } @@ -115,21 +115,21 @@ int gravity_tests (void) { Coords chord = coords_sub (gravity_parabola (position, velocity, acceleration, (i_step + 1) * step), start); int npiece = gmesh_intersect (&socket, start, chord, 0, line_hits); int i_piece; - for (i_piece = 0; i_piece < npiece && line_hits[i_piece].t <= 1 && nline < GMESH_MAX_HITS; i_piece++) { - line_times[nline] = (i_step + line_hits[i_piece].t) * step; + for (i_piece = 0; i_piece < npiece && line_hits[i_piece].time <= 1 && nline < GMESH_MAX_HITS; i_piece++) { + line_times[nline] = (i_step + line_hits[i_piece].time) * step; nline++; } } nhits = gmesh_intersect_gravity (&socket, position, velocity, acceleration, 0, hits); - while (nhits > 0 && hits[nhits - 1].t > time_end) + while (nhits > 0 && hits[nhits - 1].time > time_end) nhits--; if (nhits != nline) { mismatches++; continue; } for (i_hit = 0; i_hit < nhits; i_hit++) { - if (fabs (hits[i_hit].t - line_times[i_hit]) > worst) - worst = fabs (hits[i_hit].t - line_times[i_hit]); + if (fabs (hits[i_hit].time - line_times[i_hit]) > worst) + worst = fabs (hits[i_hit].time - line_times[i_hit]); } } printf ("Test_lib_geometry_mesh_lib: socket.off: %d of 2000 random rays differ from the line or polyline, worst time difference %g s\n", mismatches, diff --git a/mcstas-comps/obsolete/Guide_anyshape_r.comp b/mcstas-comps/obsolete/Guide_anyshape_r.comp index 5f93a35c44..3487aed6a8 100644 --- a/mcstas-comps/obsolete/Guide_anyshape_r.comp +++ b/mcstas-comps/obsolete/Guide_anyshape_r.comp @@ -153,7 +153,7 @@ TRACE /* exit loop when no intersection forward */ if (!intersect) break; - dt = gmesh_hit_time (&hits[0]); + dt = hits[0].time; n = hits[0].normal; fi = hits[0].face; diff --git a/mcstas-comps/optics/Guide_anyshape.comp b/mcstas-comps/optics/Guide_anyshape.comp index 965912a5fa..1a441b8054 100644 --- a/mcstas-comps/optics/Guide_anyshape.comp +++ b/mcstas-comps/optics/Guide_anyshape.comp @@ -164,7 +164,7 @@ TRACE /* exit loop when no intersection forward */ if (!intersect) break; - dt = gmesh_hit_time (&hits[0]); + dt = hits[0].time; n = hits[0].normal; fi = hits[0].face; diff --git a/mcstas-comps/optics/Refractor.comp b/mcstas-comps/optics/Refractor.comp index c78ba08049..b0f31df90f 100644 --- a/mcstas-comps/optics/Refractor.comp +++ b/mcstas-comps/optics/Refractor.comp @@ -175,10 +175,10 @@ SHARE *inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); if (*inside) { *t0 = 0; - *t1 = hits[0].t; + *t1 = hits[0].time; } else { - *t0 = hits[0].t; - *t1 = nhits > 1 ? hits[1].t : hits[0].t; + *t0 = hits[0].time; + *t1 = nhits > 1 ? hits[1].time : hits[0].time; } // gmesh normals follow the right-hand rule, unless the mesh was flipped *normal = coords_scale (hits[0].normal, mesh->flipped ? 1 : -1); diff --git a/mcstas-comps/samples/Incoherent.comp b/mcstas-comps/samples/Incoherent.comp index b9b4a370e5..620ff16bde 100644 --- a/mcstas-comps/samples/Incoherent.comp +++ b/mcstas-comps/samples/Incoherent.comp @@ -145,10 +145,10 @@ SHARE inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); if (inside) { *t0 = 0; - *t3 = hits[0].t; + *t3 = hits[0].time; } else { - *t0 = hits[0].t; - *t3 = nhits > 1 ? hits[1].t : hits[0].t; + *t0 = hits[0].time; + *t3 = nhits > 1 ? hits[1].time : hits[0].time; } return 1; } diff --git a/mcstas-comps/samples/Isotropic_Sqw.comp b/mcstas-comps/samples/Isotropic_Sqw.comp index 8600910c35..b70add9bd8 100644 --- a/mcstas-comps/samples/Isotropic_Sqw.comp +++ b/mcstas-comps/samples/Isotropic_Sqw.comp @@ -332,10 +332,10 @@ SHARE inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); if (inside) { *t0 = 0; - *t3 = hits[0].t; + *t3 = hits[0].time; } else { - *t0 = hits[0].t; - *t3 = nhits > 1 ? hits[1].t : hits[0].t; + *t0 = hits[0].time; + *t3 = nhits > 1 ? hits[1].time : hits[0].time; } return 1; } diff --git a/mcstas-comps/samples/PowderN.comp b/mcstas-comps/samples/PowderN.comp index 065aa213f4..342d97ca14 100644 --- a/mcstas-comps/samples/PowderN.comp +++ b/mcstas-comps/samples/PowderN.comp @@ -243,10 +243,10 @@ SHARE inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); if (inside) { *t0 = 0; - *t3 = hits[0].t; + *t3 = hits[0].time; } else { - *t0 = hits[0].t; - *t3 = nhits > 1 ? hits[1].t : hits[0].t; + *t0 = hits[0].time; + *t3 = nhits > 1 ? hits[1].time : hits[0].time; } return 1; } diff --git a/mcstas-comps/samples/Single_crystal.comp b/mcstas-comps/samples/Single_crystal.comp index 4db2cef6d3..5d74f30cf4 100644 --- a/mcstas-comps/samples/Single_crystal.comp +++ b/mcstas-comps/samples/Single_crystal.comp @@ -328,10 +328,10 @@ SHARE inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); if (inside) { *t0 = 0; - *t3 = hits[0].t; + *t3 = hits[0].time; } else { - *t0 = hits[0].t; - *t3 = nhits > 1 ? hits[1].t : hits[0].t; + *t0 = hits[0].time; + *t3 = nhits > 1 ? hits[1].time : hits[0].time; } return 1; } diff --git a/mcstas-comps/samples/Single_magnetic_crystal.comp b/mcstas-comps/samples/Single_magnetic_crystal.comp index 399fe86478..b275458c92 100644 --- a/mcstas-comps/samples/Single_magnetic_crystal.comp +++ b/mcstas-comps/samples/Single_magnetic_crystal.comp @@ -130,10 +130,10 @@ SHARE inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); if (inside) { *t0 = 0; - *t3 = hits[0].t; + *t3 = hits[0].time; } else { - *t0 = hits[0].t; - *t3 = nhits > 1 ? hits[1].t : hits[0].t; + *t0 = hits[0].time; + *t3 = nhits > 1 ? hits[1].time : hits[0].time; } return 1; } diff --git a/mcstas-comps/share/monitor_nd-lib.c b/mcstas-comps/share/monitor_nd-lib.c index 1acdeacb5a..d690885c1f 100644 --- a/mcstas-comps/share/monitor_nd-lib.c +++ b/mcstas-comps/share/monitor_nd-lib.c @@ -2225,12 +2225,12 @@ int Monitor_nD_MeshIntersect(gmesh_struct* mesh, Coords position, Coords velocit inside = gmesh_inside(mesh, position); if (inside) { *t0 = 0; - *t1 = hits[0].t; + *t1 = hits[0].time; } else { - *t0 = hits[0].t; - *t1 = hits[0].t; + *t0 = hits[0].time; + *t1 = hits[0].time; if (nhits > 1) - *t1 = hits[1].t; + *t1 = hits[1].time; } *face = hits[0].face; return 1; diff --git a/mcstas-comps/share/union-lib.c b/mcstas-comps/share/union-lib.c index 7c62432402..301ea96b69 100644 --- a/mcstas-comps/share/union-lib.c +++ b/mcstas-comps/share/union-lib.c @@ -2995,7 +2995,7 @@ int sample_mesh_intersect(double *t, *num_solutions = gmesh_intersect(triangle_mesh, position, velocity, t_min, hits); for (i = 0; i < *num_solutions; i++) { Coords normal = rot_apply(geometry->rotation_matrix, hits[i].normal); - t[i] = hits[i].t; + t[i] = hits[i].time; nx[i] = normal.x; ny[i] = normal.y; nz[i] = normal.z; diff --git a/mcxtrace-comps/share/union-lib.c b/mcxtrace-comps/share/union-lib.c index e8d2ba95bd..48c9d7f504 100644 --- a/mcxtrace-comps/share/union-lib.c +++ b/mcxtrace-comps/share/union-lib.c @@ -2621,7 +2621,7 @@ int sample_mesh_intersect(double *t,int *num_solutions,double *r,double *v,struc // McXtrace intersect functions return lengths, so trace along the unit direction direction = coords_scale(direction, 1.0/coords_len(direction)); *num_solutions = gmesh_intersect(triangle_mesh, position, direction, -1e-9*triangle_mesh->length_scale, hits); - for (i = 0; i < *num_solutions; i++) t[i] = hits[i].t; + for (i = 0; i < *num_solutions; i++) t[i] = hits[i].time; return *num_solutions > 0; } From ed142b3d725329ecf338688d7f8baa4f8ec78049 Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Tue, 10 Nov 2026 17:02:04 +0100 Subject: [PATCH 07/22] geometry-mesh-lib: add gmesh_entry_exit, used by the samples and Refractor gmesh_entry_exit gives the next entry and exit hits of a line through a mesh, or only the exit when the particle is inside. It skips the surface the particle sits on; an open mesh counts as outside. Incoherent, PowderN, Isotropic_Sqw, Single_crystal, Single_magnetic_crystal and Single_crystal_inelastic call it in TRACE instead of their own _mesh_intersect, with t0 = 0 inside. Refractor calls it inline and takes the normal of the next hit from it. The libtest instrument checks it on cube.off. Comments added or edited on this branch use //. Co-Authored-By: Claude Opus 5.5 --- common/lib/share/geometry-mesh-lib.c | 30 +++++++++++ common/lib/share/geometry-mesh-lib.h | 12 +++++ .../contrib/Single_crystal_inelastic.comp | 42 ++++++--------- .../Test_lib_geometry_mesh_lib.instr | 49 ++++++++++++++---- mcstas-comps/misc/Shape.comp | 2 +- mcstas-comps/misc/Shape_simple.comp | 2 +- mcstas-comps/obsolete/Guide_anyshape_r.comp | 4 +- mcstas-comps/optics/Guide_anyshape.comp | 6 +-- mcstas-comps/optics/Refractor.comp | 39 +++----------- mcstas-comps/samples/Incoherent.comp | 42 ++++++--------- mcstas-comps/samples/Isotropic_Sqw.comp | 43 ++++++---------- mcstas-comps/samples/PowderN.comp | 51 +++++++++---------- mcstas-comps/samples/Single_crystal.comp | 42 ++++++--------- .../samples/Single_magnetic_crystal.comp | 42 ++++++--------- 14 files changed, 199 insertions(+), 207 deletions(-) diff --git a/common/lib/share/geometry-mesh-lib.c b/common/lib/share/geometry-mesh-lib.c index f0e552ace0..e0678db5ad 100644 --- a/common/lib/share/geometry-mesh-lib.c +++ b/common/lib/share/geometry-mesh-lib.c @@ -2309,6 +2309,36 @@ gmesh_inside (gmesh_struct* mesh, Coords position) { return votes >= 2; } + // entry and exit of a line through mesh, see geometry-mesh-lib.h + #pragma acc routine seq +int +gmesh_entry_exit (gmesh_struct* mesh, Coords position, Coords velocity, gmesh_hit* entering, gmesh_hit* leaving) { + gmesh_hit hits[GMESH_MAX_HITS]; + // skips the surface the particle sits on, e.g. after a propagation to it + double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); + int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); + int inside; + if (!nhits) + return 0; + // normals of a closed, oriented mesh point outward: leaving first means inside + if (!mesh->closed) + inside = 0; + else if (mesh->oriented) + inside = hits[0].in_out < 0; + else + inside = gmesh_inside (mesh, position); + if (inside) { + *leaving = hits[0]; + return 1; + } + *entering = hits[0]; + if (nhits > 1) + *leaving = hits[1]; + else + *leaving = hits[0]; + return 2; +} + // per-face property, see geometry-mesh-lib.h #pragma acc routine seq double diff --git a/common/lib/share/geometry-mesh-lib.h b/common/lib/share/geometry-mesh-lib.h index 02212020e5..280827c921 100644 --- a/common/lib/share/geometry-mesh-lib.h +++ b/common/lib/share/geometry-mesh-lib.h @@ -43,6 +43,7 @@ * straight line, sorted in t * gmesh_intersect_gravity() the same for a parabola (e.g. under gravity) * gmesh_inside() point-in-mesh test (deterministic, no random numbers) + * gmesh_entry_exit() next entry and exit (only exit from inside) * Hits exactly on an edge or vertex shared by several triangles are * reported once. * @@ -186,6 +187,17 @@ int gmesh_intersect_gravity (gmesh_struct* mesh, Coords position, Coords velocit #pragma acc routine seq int gmesh_inside (gmesh_struct* mesh, Coords position); + // int gmesh_entry_exit(gmesh_struct *mesh, Coords position, Coords velocity, + // gmesh_hit *entering, gmesh_hit *leaving) + // ACTION: next entry and exit of the line position + t*velocity through + // mesh, for a particle inside or outside a (closed) mesh. A hit on the + // surface the particle sits on (|t| below the mesh tolerance) is skipped. + // An open mesh counts as outside; with a single hit ahead, leaving = entering. + // RETURN: 0 when the mesh is not ahead, 1 inside (only leaving is set), + // 2 outside (entering and leaving are set) + #pragma acc routine seq +int gmesh_entry_exit (gmesh_struct* mesh, Coords position, Coords velocity, gmesh_hit* entering, gmesh_hit* leaving); + // per-face property number 'property' of face 'face', or fallback when it is // not given in the file #pragma acc routine seq diff --git a/mcstas-comps/contrib/Single_crystal_inelastic.comp b/mcstas-comps/contrib/Single_crystal_inelastic.comp index ee9c0cb857..f0aae94fc3 100644 --- a/mcstas-comps/contrib/Single_crystal_inelastic.comp +++ b/mcstas-comps/contrib/Single_crystal_inelastic.comp @@ -122,30 +122,6 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" - // Next entry time t0 and exit time t3 of the line position + t*velocity - // through mesh, with t0 = 0 for a neutron that starts inside. The surface - // the neutron sits on (hit at |t| < tolerance) is skipped. - // Returns 1 when the mesh is ahead, 0 otherwise. - #pragma acc routine seq - int - Single_crystal_inelastic_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3) { - gmesh_hit hits[GMESH_MAX_HITS]; - double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); - int inside; - if (!nhits) - return 0; - // normals of a closed, oriented mesh point outward: leaving first means inside - inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); - if (inside) { - *t0 = 0; - *t3 = hits[0].time; - } else { - *t0 = hits[0].time; - *t3 = nhits > 1 ? hits[1].time : hits[0].time; - } - return 1; - } /* Declare structures and functions only once in each instrument. */ #ifndef SINGLE_CRYSTAL_DECL #define SINGLE_CRYSTAL_DECL @@ -824,7 +800,14 @@ TRACE else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) - intersect = Single_crystal_inelastic_mesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2); + { + gmesh_hit entering, leaving; + intersect = gmesh_entry_exit (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (intersect) { + t1 = intersect == 2 ? entering.time : 0; // 0 when inside + t2 = leaving.time; + } + } if (t2 < 0) intersect = 0; /* we passed sample volume already */ @@ -858,7 +841,14 @@ TRACE else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) - intersect = Single_crystal_inelastic_mesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2); + { + gmesh_hit entering, leaving; + intersect = gmesh_entry_exit (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (intersect) { + t1 = intersect == 2 ? entering.time : 0; // 0 when inside + t2 = leaving.time; + } + } if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { /* neutron is leaving the sample */ if (hkl_info.flag_warning < 100) diff --git a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr index 273af62f37..0a246016e6 100644 --- a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr +++ b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr @@ -6,8 +6,10 @@ * * Smoke test: builds and runs with the library geometry-mesh-lib in the instrument. * Fails to compile or link if the library does not build with the runtime. -* INITIALIZE also checks gmesh_intersect_gravity, and exits with an error -* when a check fails: +* INITIALIZE also checks gmesh_entry_exit and gmesh_intersect_gravity, and +* exits with an error when a check fails: +* - gmesh_entry_exit on cube.off from outside, inside, on the entry face and +* past the cube * - with zero acceleration it gives the hits of gmesh_intersect, to rounding * - parabolas through cube.off give the analytic hit times and in_out * - random parabolas through socket.off give the hits of a fine polyline of @@ -24,7 +26,7 @@ DECLARE %include "geometry-mesh-lib" // counts a failed check and prints what failed -void gravity_check (int ok, const char* what, int* failures) { +void mesh_check (int ok, const char* what, int* failures) { if (!ok) { fprintf (stderr, "Test_lib_geometry_mesh_lib: FAILED: %s\n", what); (*failures)++; @@ -42,6 +44,31 @@ double gravity_uniform (unsigned long* state, double low, double high) { return low + (high - low) * (double)(*state >> 11) / 9007199254740992.0; } +// checks of gmesh_entry_exit on the unit cube; returns the number of failed checks +int entry_exit_tests (void) { + gmesh_struct cube; + gmesh_hit entering; + gmesh_hit leaving; + int failures = 0; + int result; + if (!gmesh_init ("cube.off", 1, GMESH_PROPS_IGNORE, &cube)) { + fprintf (stderr, "Test_lib_geometry_mesh_lib: FAILED: can not read cube.off\n"); + return 1; + } + result = gmesh_entry_exit (&cube, coords_set (0.5, 0.5, -1), coords_set (0, 0, 2), &entering, &leaving); + mesh_check (result == 2 && fabs (entering.time - 0.5) < 1e-12 && fabs (leaving.time - 1) < 1e-12, "entry_exit from outside", &failures); + result = gmesh_entry_exit (&cube, coords_set (0.5, 0.5, 0.25), coords_set (0, 0, 1), &entering, &leaving); + mesh_check (result == 1 && fabs (leaving.time - 0.75) < 1e-12, "entry_exit from inside", &failures); + result = gmesh_entry_exit (&cube, coords_set (0.5, 0.5, 0), coords_set (0, 0, 1), &entering, &leaving); + mesh_check (result == 1 && fabs (leaving.time - 1) < 1e-12, "entry_exit on the entry face", &failures); + result = gmesh_entry_exit (&cube, coords_set (0.5, 0.5, 1), coords_set (0, 0, 1), &entering, &leaving); + mesh_check (result == 0, "entry_exit on the exit face, leaving", &failures); + result = gmesh_entry_exit (&cube, coords_set (0.5, 0.5, 2), coords_set (0, 0, 1), &entering, &leaving); + mesh_check (result == 0, "entry_exit past the cube", &failures); + gmesh_free (&cube); + return failures; +} + // checks of gmesh_intersect_gravity; returns the number of failed checks int gravity_tests (void) { gmesh_struct cube; @@ -62,27 +89,27 @@ int gravity_tests (void) { // falling from rest above the unit cube: top at time sqrt(2), bottom at 2 nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, 2, 0.5), coords_set (0, 0, 0), coords_set (0, -1, 0), 0, hits); - gravity_check (nhits == 2 && fabs (hits[0].time - sqrt (2)) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].time - 2) < 1e-12 && hits[1].in_out == -1, + mesh_check (nhits == 2 && fabs (hits[0].time - sqrt (2)) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].time - 2) < 1e-12 && hits[1].in_out == -1, "fall from rest through cube.off", &failures); // entering through the front face, leaving through the bottom at time sqrt(2) nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, 0.5, -1), coords_set (0, 0, 1), coords_set (0, -0.5, 0), 0, hits); - gravity_check (nhits == 2 && fabs (hits[0].time - 1) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].time - sqrt (2)) < 1e-12 && hits[1].in_out == -1, + mesh_check (nhits == 2 && fabs (hits[0].time - 1) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].time - sqrt (2)) < 1e-12 && hits[1].in_out == -1, "front face in, bottom out of cube.off", &failures); // thrown up into the cube from below, falling out through the same face nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, -1, 0.5), coords_set (0, 2, 0), coords_set (0, -1.2, 0), 0, hits); - gravity_check (nhits == 2 && fabs (hits[0].time - (2 - sqrt (1.6)) / 1.2) < 1e-12 && hits[0].in_out == 1 + mesh_check (nhits == 2 && fabs (hits[0].time - (2 - sqrt (1.6)) / 1.2) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].time - (2 + sqrt (1.6)) / 1.2) < 1e-12 && hits[1].in_out == -1 && hits[0].face == hits[1].face, "in and out through the bottom of cube.off", &failures); // t_min skips the hits before it nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, -1, 0.5), coords_set (0, 2, 0), coords_set (0, -1.2, 0), 1, hits); - gravity_check (nhits == 1 && hits[0].in_out == -1, "t_min", &failures); + mesh_check (nhits == 1 && hits[0].in_out == -1, "t_min", &failures); // the arc passes above the cube nhits = gmesh_intersect_gravity (&cube, coords_set (-1, 1.5, 0.5), coords_set (1, 0, 0), coords_set (0, -0.1, 0), 0, hits); - gravity_check (nhits == 0, "arc above cube.off", &failures); + mesh_check (nhits == 0, "arc above cube.off", &failures); for (i_ray = 0; i_ray < 2000; i_ray++) { Coords position = coords_set (gravity_uniform (&state, -0.1, 0.1), gravity_uniform (&state, -0.1, 0.1), gravity_uniform (&state, -0.1, 0.1)); @@ -134,7 +161,7 @@ int gravity_tests (void) { } printf ("Test_lib_geometry_mesh_lib: socket.off: %d of 2000 random rays differ from the line or polyline, worst time difference %g s\n", mismatches, worst); - gravity_check (mismatches == 0 && worst < 1e-6, "random parabolas through socket.off", &failures); + mesh_check (mismatches == 0 && worst < 1e-6, "random parabolas through socket.off", &failures); gmesh_free (&cube); gmesh_free (&socket); @@ -145,8 +172,8 @@ int gravity_tests (void) { INITIALIZE %{ mcset_ncount(Ncount); - if (gravity_tests ()) - exit (fprintf (stderr, "Test_lib_geometry_mesh_lib: gmesh_intersect_gravity checks failed\n")); + if (entry_exit_tests () + gravity_tests ()) + exit (fprintf (stderr, "Test_lib_geometry_mesh_lib: geometry-mesh-lib checks failed\n")); %} TRACE diff --git a/mcstas-comps/misc/Shape.comp b/mcstas-comps/misc/Shape.comp index 7686620be2..6a2cee6176 100644 --- a/mcstas-comps/misc/Shape.comp +++ b/mcstas-comps/misc/Shape.comp @@ -93,7 +93,7 @@ TRACE %{ MCDISPLAY %{ - if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* mesh file */ + if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { // mesh file gmesh_display (&mesh); } else if (radius > 0 && yheight) { /* cylinder along y*/ cylinder (0, 0, 0, radius, yheight, thickness, nx, ny, nz); diff --git a/mcstas-comps/misc/Shape_simple.comp b/mcstas-comps/misc/Shape_simple.comp index a7ecdb0b14..baaf31db8d 100644 --- a/mcstas-comps/misc/Shape_simple.comp +++ b/mcstas-comps/misc/Shape_simple.comp @@ -91,7 +91,7 @@ TRACE %{ MCDISPLAY %{ - if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* mesh file */ + if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { // mesh file gmesh_display (&mesh); } else if (radius > 0 && yheight) { /* cylinder */ cylinder (0, 0, 0, radius, yheight, thickness, 0, 1, 0); diff --git a/mcstas-comps/obsolete/Guide_anyshape_r.comp b/mcstas-comps/obsolete/Guide_anyshape_r.comp index 3487aed6a8..982b0c914d 100644 --- a/mcstas-comps/obsolete/Guide_anyshape_r.comp +++ b/mcstas-comps/obsolete/Guide_anyshape_r.comp @@ -108,7 +108,7 @@ DECLARE INITIALIZE %{ MPI_MASTER (fprintf (stderr, "Guide_anyshape_r: %s: WARNING: this component is obsolete. Use Guide_anyshape(file_coatings=1) instead.\n", NAME_CURRENT_COMP);); - /* initialize the mesh from the file, with the per-face coatings if given */ + // initialize the mesh from the file, with the per-face coatings if given if (!gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_OPTIONAL, &mesh)) exit (-1); @@ -147,7 +147,7 @@ TRACE if (mcgravitation) acceleration = rot_apply (_comp->_rotation_absolute, coords_set (0, -GRAVITY, 0)); - /* next intersection with the object, along the parabola under gravity; PL: with the index of the face */ + // next intersection with the object, along the parabola under gravity; PL: with the index of the face intersect = gmesh_intersect_gravity (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, 0, hits); /* exit loop when no intersection forward */ diff --git a/mcstas-comps/optics/Guide_anyshape.comp b/mcstas-comps/optics/Guide_anyshape.comp index 1a441b8054..1fa7979b13 100644 --- a/mcstas-comps/optics/Guide_anyshape.comp +++ b/mcstas-comps/optics/Guide_anyshape.comp @@ -112,7 +112,7 @@ DECLARE INITIALIZE %{ - /* initialize the mesh from the file, with the per-face coatings when used */ + // initialize the mesh from the file, with the per-face coatings when used if (!gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, file_coatings ? GMESH_PROPS_OPTIONAL : GMESH_PROPS_IGNORE, &mesh)) exit (-1); @@ -158,7 +158,7 @@ TRACE if (mcgravitation) acceleration = rot_apply (_comp->_rotation_absolute, coords_set (0, -GRAVITY, 0)); - /* next intersection with the object, along the parabola under gravity */ + // next intersection with the object, along the parabola under gravity intersect = gmesh_intersect_gravity (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, 0, hits); /* exit loop when no intersection forward */ @@ -186,7 +186,7 @@ TRACE TableReflecFunc (q, &pTable, &R); else { double par[] = { R0, Qc, alpha, m, W }; - if (file_coatings) { /* per-face values m, alpha, W, R0, Qc (properties 0-4), parameters as defaults */ + if (file_coatings) { // per-face values m, alpha, W, R0, Qc (properties 0-4), parameters as defaults par[0] = gmesh_face_prop (&thread_mesh, fi, 3, R0); par[1] = gmesh_face_prop (&thread_mesh, fi, 4, Qc); par[2] = gmesh_face_prop (&thread_mesh, fi, 1, alpha); diff --git a/mcstas-comps/optics/Refractor.comp b/mcstas-comps/optics/Refractor.comp index b0f31df90f..845ecc2cda 100644 --- a/mcstas-comps/optics/Refractor.comp +++ b/mcstas-comps/optics/Refractor.comp @@ -157,34 +157,6 @@ SHARE %include "geometry-mesh-lib" %include "ref-lib" - // Next entry time t0 and exit time t1 of the line position + t*velocity - // through mesh, with t0 = 0 and inside = 1 for a neutron that starts inside. - // normal is the normal at the next hit (the exit when inside), with the - // orientation of interoff-lib: opposite to the right-hand rule of the vertex - // order in the file. The surface the neutron sits on (hit at |t| < tolerance) - // is skipped. Returns 1 when the mesh is ahead, 0 otherwise. - #pragma acc routine seq - int - Refractor_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t1, Coords* normal, int* inside) { - gmesh_hit hits[GMESH_MAX_HITS]; - double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); - if (!nhits) - return 0; - // normals of a closed, oriented mesh point outward: leaving first means inside - *inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); - if (*inside) { - *t0 = 0; - *t1 = hits[0].time; - } else { - *t0 = hits[0].time; - *t1 = nhits > 1 ? hits[1].time : hits[0].time; - } - // gmesh normals follow the right-hand rule, unless the mesh was flipped - *normal = coords_scale (hits[0].normal, mesh->flipped ? 1 : -1); - return 1; - } - /* wrappers to intersection routines which also return the normal vector */ int box_intersect_n (double* t0, double* t1, double x, double y, double z, double vx, double vy, double vz, double dx, double dy, double dz, double* nx, double* ny, @@ -438,10 +410,15 @@ TRACE } #ifdef USE_OFF else if (geometry && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - Coords normal; - intersect = Refractor_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &normal, &mesh_inside); + gmesh_hit entering, leaving; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); if (intersect) { - coords_get (normal, &nx, &ny, &nz); + mesh_inside = (intersect == 1); + t0 = mesh_inside ? 0 : entering.time; + t1 = leaving.time; + // normal of the next hit, oriented as in interoff-lib: opposite to the + // right-hand rule of the vertex order in the file, unless the mesh was flipped + coords_get (coords_scale (mesh_inside ? leaving.normal : entering.normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); } } #endif diff --git a/mcstas-comps/samples/Incoherent.comp b/mcstas-comps/samples/Incoherent.comp index 620ff16bde..2a12340330 100644 --- a/mcstas-comps/samples/Incoherent.comp +++ b/mcstas-comps/samples/Incoherent.comp @@ -128,30 +128,6 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" - // Next entry time t0 and exit time t3 of the line position + t*velocity - // through mesh, with t0 = 0 for a neutron that starts inside. The surface - // the neutron sits on (hit at |t| < tolerance) is skipped. - // Returns 1 when the mesh is ahead, 0 otherwise. - #pragma acc routine seq - int - Incoherent_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3) { - gmesh_hit hits[GMESH_MAX_HITS]; - double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); - int inside; - if (!nhits) - return 0; - // normals of a closed, oriented mesh point outward: leaving first means inside - inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); - if (inside) { - *t0 = 0; - *t3 = hits[0].time; - } else { - *t0 = hits[0].time; - *t3 = nhits > 1 ? hits[1].time : hits[0].time; - } - return 1; - } struct StructVarsInc { double sigma_a; /* Absorption cross section per atom (barns) */ double sigma_i; /* Incoherent scattering cross section per atom (barns) */ @@ -303,7 +279,14 @@ TRACE intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (VarsInc.shape == 3) - intersect = Incoherent_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3); + { + gmesh_hit entering, leaving; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (intersect) { + t0 = intersect == 2 ? entering.time : 0; // 0 when inside + t3 = leaving.time; + } + } #endif if (intersect) { @@ -500,7 +483,14 @@ TRACE intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (VarsInc.shape == 3) - intersect = Incoherent_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3); + { + gmesh_hit entering, leaving; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (intersect) { + t0 = intersect == 2 ? entering.time : 0; // 0 when inside + t3 = leaving.time; + } + } #endif flag_ishollow = 0; diff --git a/mcstas-comps/samples/Isotropic_Sqw.comp b/mcstas-comps/samples/Isotropic_Sqw.comp index b70add9bd8..16af9de4a4 100644 --- a/mcstas-comps/samples/Isotropic_Sqw.comp +++ b/mcstas-comps/samples/Isotropic_Sqw.comp @@ -315,31 +315,6 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" - // Next entry time t0 and exit time t3 of the line position + t*velocity - // through mesh, with t0 = 0 for a neutron that starts inside. The surface - // the neutron sits on (hit at |t| < tolerance) is skipped. - // Returns 1 when the mesh is ahead, 0 otherwise. - #pragma acc routine seq - int - Isotropic_Sqw_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3) { - gmesh_hit hits[GMESH_MAX_HITS]; - double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); - int inside; - if (!nhits) - return 0; - // normals of a closed, oriented mesh point outward: leaving first means inside - inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); - if (inside) { - *t0 = 0; - *t3 = hits[0].time; - } else { - *t0 = hits[0].time; - *t3 = nhits > 1 ? hits[1].time : hits[0].time; - } - return 1; - } - /* For the density of states S(w) */ struct Sqw_W_struct { double omega; /* omega value for the data block */ @@ -2281,7 +2256,14 @@ TRACE intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (VarSqw.shape == 3) - intersect = Isotropic_Sqw_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3); + { + gmesh_hit entering, leaving; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (intersect) { + t0 = intersect == 2 ? entering.time : 0; // 0 when inside + t3 = leaving.time; + } + } #endif } else { if (VarSqw.shape == 0) @@ -2294,7 +2276,14 @@ TRACE intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius - thickness); #ifdef USE_OFF else if (VarSqw.shape == 3) - intersect = Isotropic_Sqw_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3); + { + gmesh_hit entering, leaving; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (intersect) { + t0 = intersect == 2 ? entering.time : 0; // 0 when inside + t3 = leaving.time; + } + } #endif } } else diff --git a/mcstas-comps/samples/PowderN.comp b/mcstas-comps/samples/PowderN.comp index 342d97ca14..dc9c450641 100644 --- a/mcstas-comps/samples/PowderN.comp +++ b/mcstas-comps/samples/PowderN.comp @@ -226,30 +226,6 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" - // Next entry time t0 and exit time t3 of the line position + t*velocity - // through mesh, with t0 = 0 for a neutron that starts inside. The surface - // the neutron sits on (hit at |t| < tolerance) is skipped. - // Returns 1 when the mesh is ahead, 0 otherwise. - #pragma acc routine seq - int - PowderN_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3) { - gmesh_hit hits[GMESH_MAX_HITS]; - double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); - int inside; - if (!nhits) - return 0; - // normals of a closed, oriented mesh point outward: leaving first means inside - inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); - if (inside) { - *t0 = 0; - *t3 = hits[0].time; - } else { - *t0 = hits[0].time; - *t3 = nhits > 1 ? hits[1].time : hits[0].time; - } - return 1; - } /* Declare structures and functions only once in each instrument. */ #ifndef POWDERN_DECL #define POWDERN_DECL @@ -1139,7 +1115,14 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - intersect = PowderN_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3); + { + gmesh_hit entering, leaving; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (intersect) { + t0 = intersect == 2 ? entering.time : 0; // 0 when inside + t3 = leaving.time; + } + } intersecti = 0; } #endif @@ -1334,7 +1317,14 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - intersect = PowderN_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3); + { + gmesh_hit entering, leaving; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (intersect) { + t0 = intersect == 2 ? entering.time : 0; // 0 when inside + t3 = leaving.time; + } + } intersecti = 0; } #endif @@ -1408,7 +1398,14 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - intersect = PowderN_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3); + { + gmesh_hit entering, leaving; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (intersect) { + t0 = intersect == 2 ? entering.time : 0; // 0 when inside + t3 = leaving.time; + } + } intersecti = 0; } #endif diff --git a/mcstas-comps/samples/Single_crystal.comp b/mcstas-comps/samples/Single_crystal.comp index 5d74f30cf4..2fb13c829b 100644 --- a/mcstas-comps/samples/Single_crystal.comp +++ b/mcstas-comps/samples/Single_crystal.comp @@ -311,30 +311,6 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" - // Next entry time t0 and exit time t3 of the line position + t*velocity - // through mesh, with t0 = 0 for a neutron that starts inside. The surface - // the neutron sits on (hit at |t| < tolerance) is skipped. - // Returns 1 when the mesh is ahead, 0 otherwise. - #pragma acc routine seq - int - Single_crystal_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3) { - gmesh_hit hits[GMESH_MAX_HITS]; - double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); - int inside; - if (!nhits) - return 0; - // normals of a closed, oriented mesh point outward: leaving first means inside - inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); - if (inside) { - *t0 = 0; - *t3 = hits[0].time; - } else { - *t0 = hits[0].time; - *t3 = nhits > 1 ? hits[1].time : hits[0].time; - } - return 1; - } #ifndef OPENACC %include "opencl-lib" #endif @@ -1545,7 +1521,14 @@ TRACE intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (hkl_info.shape == 3) - intersect = Single_crystal_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2); + { + gmesh_hit entering, leaving; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (intersect) { + t1 = intersect == 2 ? entering.time : 0; // 0 when inside + t2 = leaving.time; + } + } #endif if (t2 < 0) intersect = 0; /* we passed sample volume already */ @@ -1593,7 +1576,14 @@ TRACE intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (hkl_info.shape == 3) - intersect = Single_crystal_mesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2); + { + gmesh_hit entering, leaving; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (intersect) { + t1 = intersect == 2 ? entering.time : 0; // 0 when inside + t2 = leaving.time; + } + } #endif if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { /* neutron is leaving the sample */ diff --git a/mcstas-comps/samples/Single_magnetic_crystal.comp b/mcstas-comps/samples/Single_magnetic_crystal.comp index b275458c92..2d0c971977 100644 --- a/mcstas-comps/samples/Single_magnetic_crystal.comp +++ b/mcstas-comps/samples/Single_magnetic_crystal.comp @@ -113,30 +113,6 @@ SHARE %include "geometry-mesh-lib" %include "mccode-complex-lib" - // Next entry time t0 and exit time t3 of the line position + t*velocity - // through mesh, with t0 = 0 for a neutron that starts inside. The surface - // the neutron sits on (hit at |t| < tolerance) is skipped. - // Returns 1 when the mesh is ahead, 0 otherwise. - #pragma acc routine seq - int - Single_magnetic_crystal_mesh_intersect (gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t3) { - gmesh_hit hits[GMESH_MAX_HITS]; - double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); - int inside; - if (!nhits) - return 0; - // normals of a closed, oriented mesh point outward: leaving first means inside - inside = mesh->closed && (mesh->oriented ? hits[0].in_out < 0 : gmesh_inside (mesh, position)); - if (inside) { - *t0 = 0; - *t3 = hits[0].time; - } else { - *t0 = hits[0].time; - *t3 = nhits > 1 ? hits[1].time : hits[0].time; - } - return 1; - } // %include "columnfile" /* Declare structures and functions only once in each instrument. */ #ifndef SINGLE_MAGNETIC_CRYSTAL_DECL @@ -638,7 +614,14 @@ TRACE else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) - intersect = Single_magnetic_crystal_mesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2); + { + gmesh_hit entering, leaving; + intersect = gmesh_entry_exit (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (intersect) { + t1 = intersect == 2 ? entering.time : 0; // 0 when inside + t2 = leaving.time; + } + } if (t2 < 0) intersect = 0; /* we passed sample volume already */ @@ -666,7 +649,14 @@ TRACE else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) - intersect = Single_magnetic_crystal_mesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2); + { + gmesh_hit entering, leaving; + intersect = gmesh_entry_exit (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (intersect) { + t1 = intersect == 2 ? entering.time : 0; // 0 when inside + t2 = leaving.time; + } + } if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { /* neutron is leaving the sample */ if (hkl_info.flag_warning < 100) From 6ca520d44aa8c4b1fb5f605928bc1d790db8a937 Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Tue, 10 Nov 2026 19:25:31 +0100 Subject: [PATCH 08/22] Lens: use gmesh_entry_exit for the next mesh hit The next hit is the entry from outside and the exit from inside, so Lens calls gmesh_entry_exit at its three call sites and its own Lens_mesh_next is removed. Results are unchanged. Co-Authored-By: Claude Opus 5.5 --- mcstas-comps/contrib/Lens.comp | 59 +++++++++++++++++----------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/mcstas-comps/contrib/Lens.comp b/mcstas-comps/contrib/Lens.comp index ee169162db..749dec1dd7 100644 --- a/mcstas-comps/contrib/Lens.comp +++ b/mcstas-comps/contrib/Lens.comp @@ -125,23 +125,6 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" - // time dt and normal of the next hit of the line position + t*velocity with - // mesh, skipping the surface the neutron sits on (hit at |t| < tolerance). - // The normal has the orientation of interoff-lib: opposite to the - // right-hand rule of the vertex order in the file. Returns 1 on a hit, else 0. - #pragma acc routine seq - int - Lens_mesh_next (gmesh_struct* mesh, Coords position, Coords velocity, double* dt, Coords* normal) { - gmesh_hit hits[GMESH_MAX_HITS]; - double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - if (!gmesh_intersect (mesh, position, velocity, tolerance, hits)) - return 0; - *dt = hits[0].time; - // gmesh normals follow the right-hand rule, unless the mesh was flipped - *normal = coords_scale (hits[0].normal, mesh->flipped ? 1 : -1); - return 1; - } - /* Lens_roughness: function to rotate normal vector around axis for roughness * with specified tilt angle (deg) * RETURNS: rotated nx,ny,nz coordinates @@ -296,10 +279,16 @@ TRACE nx = ny = nz = 0; #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - Coords n; - if (Lens_mesh_next (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &dt, &n)) - coords_get (n, &nx, &ny, &nz); - else + gmesh_hit entering, leaving; + int nmesh = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (nmesh) { + // next hit: the entry from outside, the exit from inside + gmesh_hit* next = nmesh == 2 ? &entering : &leaving; + dt = next->time; + // normal oriented as in interoff-lib: opposite to the right-hand rule of + // the vertex order in the file, unless the mesh was flipped + coords_get (coords_scale (next->normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); + } else dt = -1; } else #endif @@ -390,10 +379,16 @@ TRACE nx = ny = nz = 0; #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - Coords n; - if (Lens_mesh_next (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &dt, &n)) - coords_get (n, &nx, &ny, &nz); - else + gmesh_hit entering, leaving; + int nmesh = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (nmesh) { + // next hit: the entry from outside, the exit from inside + gmesh_hit* next = nmesh == 2 ? &entering : &leaving; + dt = next->time; + // normal oriented as in interoff-lib: opposite to the right-hand rule of + // the vertex order in the file, unless the mesh was flipped + coords_get (coords_scale (next->normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); + } else dt = -1; } else #endif @@ -497,10 +492,16 @@ TRACE nx = ny = nz = 0; #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - Coords n; - if (Lens_mesh_next (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &dt, &n)) - coords_get (n, &nx, &ny, &nz); - else + gmesh_hit entering, leaving; + int nmesh = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (nmesh) { + // next hit: the entry from outside, the exit from inside + gmesh_hit* next = nmesh == 2 ? &entering : &leaving; + dt = next->time; + // normal oriented as in interoff-lib: opposite to the right-hand rule of + // the vertex order in the file, unless the mesh was flipped + coords_get (coords_scale (next->normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); + } else dt = -1; } else #endif From 43233c151f35415896e4bf9e272fd1543e682955 Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Tue, 10 Nov 2026 19:30:18 +0100 Subject: [PATCH 09/22] geometry-mesh-lib: add gmesh_entry_exit_gravity gmesh_entry_exit_gravity is gmesh_entry_exit for a parabola: it takes the hits from gmesh_intersect_gravity. Both pick the entry and exit with gmesh_pick_entry_exit. The libtest instrument checks it on cube.off. Co-Authored-By: Claude Opus 5.5 --- common/lib/share/geometry-mesh-lib.c | 35 +++++++++++++++---- common/lib/share/geometry-mesh-lib.h | 9 +++++ .../Test_lib_geometry_mesh_lib.instr | 14 +++++++- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/common/lib/share/geometry-mesh-lib.c b/common/lib/share/geometry-mesh-lib.c index e0678db5ad..501a0da6ce 100644 --- a/common/lib/share/geometry-mesh-lib.c +++ b/common/lib/share/geometry-mesh-lib.c @@ -2309,14 +2309,11 @@ gmesh_inside (gmesh_struct* mesh, Coords position) { return votes >= 2; } - // entry and exit of a line through mesh, see geometry-mesh-lib.h + // entry and exit among the nhits hits ahead of a particle at position, for + // gmesh_entry_exit and gmesh_entry_exit_gravity (same return values) #pragma acc routine seq -int -gmesh_entry_exit (gmesh_struct* mesh, Coords position, Coords velocity, gmesh_hit* entering, gmesh_hit* leaving) { - gmesh_hit hits[GMESH_MAX_HITS]; - // skips the surface the particle sits on, e.g. after a propagation to it - double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); - int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); +static int +gmesh_pick_entry_exit (gmesh_struct* mesh, Coords position, gmesh_hit* hits, int nhits, gmesh_hit* entering, gmesh_hit* leaving) { int inside; if (!nhits) return 0; @@ -2339,6 +2336,30 @@ gmesh_entry_exit (gmesh_struct* mesh, Coords position, Coords velocity, gmesh_hi return 2; } + // entry and exit of a line through mesh, see geometry-mesh-lib.h + #pragma acc routine seq +int +gmesh_entry_exit (gmesh_struct* mesh, Coords position, Coords velocity, gmesh_hit* entering, gmesh_hit* leaving) { + gmesh_hit hits[GMESH_MAX_HITS]; + // skips the surface the particle sits on, e.g. after a propagation to it + double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); + int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); + return gmesh_pick_entry_exit (mesh, position, hits, nhits, entering, leaving); +} + + // entry and exit of a parabola through mesh, see geometry-mesh-lib.h + #pragma acc routine seq +int +gmesh_entry_exit_gravity (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, gmesh_hit* entering, gmesh_hit* leaving) { + gmesh_hit hits[GMESH_MAX_HITS]; + // skips the surface the particle sits on; the speed scale is the one of + // gmesh_trace_gravity, as a parabola may start at rest + double speed = coords_len (velocity) + sqrt (coords_len (acceleration) * mesh->length_scale); + double tolerance = 1e-9 * mesh->length_scale / speed; + int nhits = gmesh_intersect_gravity (mesh, position, velocity, acceleration, tolerance, hits); + return gmesh_pick_entry_exit (mesh, position, hits, nhits, entering, leaving); +} + // per-face property, see geometry-mesh-lib.h #pragma acc routine seq double diff --git a/common/lib/share/geometry-mesh-lib.h b/common/lib/share/geometry-mesh-lib.h index 280827c921..f75c6b9e28 100644 --- a/common/lib/share/geometry-mesh-lib.h +++ b/common/lib/share/geometry-mesh-lib.h @@ -44,6 +44,7 @@ * gmesh_intersect_gravity() the same for a parabola (e.g. under gravity) * gmesh_inside() point-in-mesh test (deterministic, no random numbers) * gmesh_entry_exit() next entry and exit (only exit from inside) + * gmesh_entry_exit_gravity() the same for a parabola * Hits exactly on an edge or vertex shared by several triangles are * reported once. * @@ -198,6 +199,14 @@ int gmesh_inside (gmesh_struct* mesh, Coords position); #pragma acc routine seq int gmesh_entry_exit (gmesh_struct* mesh, Coords position, Coords velocity, gmesh_hit* entering, gmesh_hit* leaving); + // int gmesh_entry_exit_gravity(gmesh_struct *mesh, Coords position, + // Coords velocity, Coords acceleration, gmesh_hit *entering, gmesh_hit *leaving) + // ACTION: as gmesh_entry_exit, for the parabola + // position + t*velocity + t^2/2*acceleration (see gmesh_intersect_gravity) + // RETURN: as gmesh_entry_exit + #pragma acc routine seq +int gmesh_entry_exit_gravity (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, gmesh_hit* entering, gmesh_hit* leaving); + // per-face property number 'property' of face 'face', or fallback when it is // not given in the file #pragma acc routine seq diff --git a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr index 0a246016e6..7065eb31ee 100644 --- a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr +++ b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr @@ -9,7 +9,7 @@ * INITIALIZE also checks gmesh_entry_exit and gmesh_intersect_gravity, and * exits with an error when a check fails: * - gmesh_entry_exit on cube.off from outside, inside, on the entry face and -* past the cube +* past the cube, and gmesh_entry_exit_gravity for arcs into and out of it * - with zero acceleration it gives the hits of gmesh_intersect, to rounding * - parabolas through cube.off give the analytic hit times and in_out * - random parabolas through socket.off give the hits of a fine polyline of @@ -65,6 +65,18 @@ int entry_exit_tests (void) { mesh_check (result == 0, "entry_exit on the exit face, leaving", &failures); result = gmesh_entry_exit (&cube, coords_set (0.5, 0.5, 2), coords_set (0, 0, 1), &entering, &leaving); mesh_check (result == 0, "entry_exit past the cube", &failures); + // falling from rest above the cube: entry through the top at sqrt(2), exit through the bottom at 2 + result = gmesh_entry_exit_gravity (&cube, coords_set (0.5, 2, 0.5), coords_set (0, 0, 0), coords_set (0, -1, 0), &entering, &leaving); + mesh_check (result == 2 && fabs (entering.time - sqrt (2)) < 1e-12 && fabs (leaving.time - 2) < 1e-12, "entry_exit_gravity from outside", &failures); + // thrown up from inside, falling out through the bottom at (1+sqrt(5))/2 + result = gmesh_entry_exit_gravity (&cube, coords_set (0.5, 0.5, 0.5), coords_set (0, 0.5, 0), coords_set (0, -1, 0), &entering, &leaving); + mesh_check (result == 1 && fabs (leaving.time - (1 + sqrt (5)) / 2) < 1e-12, "entry_exit_gravity from inside", &failures); + // the arc passes above the cube + result = gmesh_entry_exit_gravity (&cube, coords_set (-1, 1.5, 0.5), coords_set (1, 0, 0), coords_set (0, -0.1, 0), &entering, &leaving); + mesh_check (result == 0, "entry_exit_gravity arc above the cube", &failures); + // zero acceleration: as gmesh_entry_exit + result = gmesh_entry_exit_gravity (&cube, coords_set (0.5, 0.5, -1), coords_set (0, 0, 2), coords_set (0, 0, 0), &entering, &leaving); + mesh_check (result == 2 && fabs (entering.time - 0.5) < 1e-12 && fabs (leaving.time - 1) < 1e-12, "entry_exit_gravity without acceleration", &failures); gmesh_free (&cube); return failures; } From 96e6cf7508614b8fc3357e083f8a36cff83289b5 Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Tue, 10 Nov 2026 19:30:18 +0100 Subject: [PATCH 10/22] Monitor_nD: use gmesh_entry_exit for mesh geometry Monitor_nD and Monitor_nD_noacc take t0/t1 and the mantid pixel from gmesh_entry_exit, and Monitor_nD_MeshIntersect is removed from monitor_nd-lib. Co-Authored-By: Claude Opus 5.5 --- mcstas-comps/monitors/Monitor_nD.comp | 11 ++++++-- mcstas-comps/monitors/Monitor_nD_noacc.comp | 11 ++++++-- mcstas-comps/share/monitor_nd-lib.c | 31 --------------------- mcstas-comps/share/monitor_nd-lib.h | 2 -- 4 files changed, 16 insertions(+), 39 deletions(-) diff --git a/mcstas-comps/monitors/Monitor_nD.comp b/mcstas-comps/monitors/Monitor_nD.comp index 2c0f212e3f..5593997c39 100644 --- a/mcstas-comps/monitors/Monitor_nD.comp +++ b/mcstas-comps/monitors/Monitor_nD.comp @@ -521,11 +521,16 @@ TRACE #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "0") && strcmp (geometry, "NULL")) { /* determine intersections with object */ - int face = 0; - intersect = Monitor_nD_MeshIntersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &face); + gmesh_hit entering, leaving; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (intersect) { + t0 = intersect == 2 ? entering.time : 0; // 0 when inside + t1 = leaving.time; + } if (Vars.Flag_mantid) { if (intersect) { - Vars.OFF_polyidx = face; + // pixel of the next hit: the entry from outside, the exit from inside + Vars.OFF_polyidx = intersect == 2 ? entering.face : leaving.face; } else { Vars.OFF_polyidx = -1; } diff --git a/mcstas-comps/monitors/Monitor_nD_noacc.comp b/mcstas-comps/monitors/Monitor_nD_noacc.comp index 157eac7c1d..05d5733181 100644 --- a/mcstas-comps/monitors/Monitor_nD_noacc.comp +++ b/mcstas-comps/monitors/Monitor_nD_noacc.comp @@ -516,11 +516,16 @@ TRACE */ if (geometry && strlen (geometry) && strcmp (geometry, "0") && strcmp (geometry, "NULL")) { /* determine intersections with object */ - int face = 0; - intersect = Monitor_nD_MeshIntersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &face); + gmesh_hit entering, leaving; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + if (intersect) { + t0 = intersect == 2 ? entering.time : 0; // 0 when inside + t1 = leaving.time; + } if (Vars.Flag_mantid) { if (intersect) { - Vars.OFF_polyidx = face; + // pixel of the next hit: the entry from outside, the exit from inside + Vars.OFF_polyidx = intersect == 2 ? entering.face : leaving.face; } else { Vars.OFF_polyidx = -1; } diff --git a/mcstas-comps/share/monitor_nd-lib.c b/mcstas-comps/share/monitor_nd-lib.c index d690885c1f..5c8b713884 100644 --- a/mcstas-comps/share/monitor_nd-lib.c +++ b/mcstas-comps/share/monitor_nd-lib.c @@ -2205,37 +2205,6 @@ void Monitor_nD_McDisplay(MonitornD_Defines_type *DEFS, /* defined by a mesh file (geometry parameter) */ /* ========================================================================= */ -// Hits of the line position + t*velocity with mesh ahead of the neutron -// (t > 0). Inside a closed mesh, t0 is 0 and t1 the first exit; otherwise -// t0 is the first hit and t1 the next one (t0 when there is a single hit). -// face is the face of the first hit ahead. Returns 1 on a hit, 0 otherwise. -#pragma acc routine seq -int Monitor_nD_MeshIntersect(gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t1, int* face) -{ - gmesh_hit hits[GMESH_MAX_HITS]; - double t_min = 0; - int nhits = gmesh_intersect(mesh, position, velocity, t_min, hits); - int inside = 0; - if (!nhits) - return 0; - // normals of a closed, oriented mesh point outward: leaving first means inside - if (mesh->closed && mesh->oriented) - inside = hits[0].in_out < 0; - else if (mesh->closed) - inside = gmesh_inside(mesh, position); - if (inside) { - *t0 = 0; - *t1 = hits[0].time; - } else { - *t0 = hits[0].time; - *t1 = hits[0].time; - if (nhits > 1) - *t1 = hits[1].time; - } - *face = hits[0].face; - return 1; -} - // Warns when mantid is combined with a closed mesh: pixel IDs are panels. void Monitor_nD_CheckMantidMesh(gmesh_struct* mesh, int flag_mantid, char* name) { diff --git a/mcstas-comps/share/monitor_nd-lib.h b/mcstas-comps/share/monitor_nd-lib.h index f4c332ecc6..fc54e7b485 100644 --- a/mcstas-comps/share/monitor_nd-lib.h +++ b/mcstas-comps/share/monitor_nd-lib.h @@ -220,8 +220,6 @@ void Monitor_nD_EnsureCapacity(MonitornD_Defines_type *, MonitornD_Variables_typ MCDETECTOR Monitor_nD_Save(MonitornD_Defines_type *, MonitornD_Variables_type *); void Monitor_nD_Finally(MonitornD_Defines_type *, MonitornD_Variables_type *); void Monitor_nD_McDisplay(MonitornD_Defines_type *, MonitornD_Variables_type *); -#pragma acc routine seq -int Monitor_nD_MeshIntersect(gmesh_struct*, Coords, Coords, double*, double*, int*); void Monitor_nD_MeshMantid(gmesh_struct*, long); void Monitor_nD_CheckMantidMesh(gmesh_struct*, int, char*); From 198343d06bcc164743be7799aafc3832c4d332ea Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Tue, 10 Nov 2026 20:27:19 +0100 Subject: [PATCH 11/22] geometry-mesh-lib: GMESH_TOL for the surface tolerance of gmesh_entry_exit gmesh_entry_exit and gmesh_entry_exit_gravity skip a hit closer than GMESH_TOL (default 1e-12 m) as the surface the particle sits on, instead of 1e-9 of the mesh size. It can be changed from an instrument with DEPENDENCY " -DGMESH_TOL=... ". The comments now say first entry and exit. Co-Authored-By: Claude Opus 5.5 --- common/lib/share/geometry-mesh-lib.c | 16 ++++++++-------- common/lib/share/geometry-mesh-lib.h | 14 +++++++++++--- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/common/lib/share/geometry-mesh-lib.c b/common/lib/share/geometry-mesh-lib.c index 501a0da6ce..a579fb6ad6 100644 --- a/common/lib/share/geometry-mesh-lib.c +++ b/common/lib/share/geometry-mesh-lib.c @@ -2309,7 +2309,7 @@ gmesh_inside (gmesh_struct* mesh, Coords position) { return votes >= 2; } - // entry and exit among the nhits hits ahead of a particle at position, for + // first entry and exit among the nhits hits ahead of a particle at position, for // gmesh_entry_exit and gmesh_entry_exit_gravity (same return values) #pragma acc routine seq static int @@ -2336,26 +2336,26 @@ gmesh_pick_entry_exit (gmesh_struct* mesh, Coords position, gmesh_hit* hits, int return 2; } - // entry and exit of a line through mesh, see geometry-mesh-lib.h + // first entry and exit of a line through mesh, see geometry-mesh-lib.h #pragma acc routine seq int gmesh_entry_exit (gmesh_struct* mesh, Coords position, Coords velocity, gmesh_hit* entering, gmesh_hit* leaving) { gmesh_hit hits[GMESH_MAX_HITS]; - // skips the surface the particle sits on, e.g. after a propagation to it - double tolerance = 1e-9 * mesh->length_scale / coords_len (velocity); + // skips the surface the particle sits on (closer than GMESH_TOL), e.g. after a propagation to it + double tolerance = GMESH_TOL / coords_len (velocity); int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); return gmesh_pick_entry_exit (mesh, position, hits, nhits, entering, leaving); } - // entry and exit of a parabola through mesh, see geometry-mesh-lib.h + // first entry and exit of a parabola through mesh, see geometry-mesh-lib.h #pragma acc routine seq int gmesh_entry_exit_gravity (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, gmesh_hit* entering, gmesh_hit* leaving) { gmesh_hit hits[GMESH_MAX_HITS]; - // skips the surface the particle sits on; the speed scale is the one of - // gmesh_trace_gravity, as a parabola may start at rest + // skips the surface the particle sits on (closer than GMESH_TOL); the speed + // scale is the one of gmesh_trace_gravity, as a parabola may start at rest double speed = coords_len (velocity) + sqrt (coords_len (acceleration) * mesh->length_scale); - double tolerance = 1e-9 * mesh->length_scale / speed; + double tolerance = GMESH_TOL / speed; int nhits = gmesh_intersect_gravity (mesh, position, velocity, acceleration, tolerance, hits); return gmesh_pick_entry_exit (mesh, position, hits, nhits, entering, leaving); } diff --git a/common/lib/share/geometry-mesh-lib.h b/common/lib/share/geometry-mesh-lib.h index f75c6b9e28..21cfbefd57 100644 --- a/common/lib/share/geometry-mesh-lib.h +++ b/common/lib/share/geometry-mesh-lib.h @@ -43,7 +43,7 @@ * straight line, sorted in t * gmesh_intersect_gravity() the same for a parabola (e.g. under gravity) * gmesh_inside() point-in-mesh test (deterministic, no random numbers) - * gmesh_entry_exit() next entry and exit (only exit from inside) + * gmesh_entry_exit() first entry and exit (only exit from inside) * gmesh_entry_exit_gravity() the same for a parabola * Hits exactly on an edge or vertex shared by several triangles are * reported once. @@ -80,6 +80,13 @@ #ifndef GMESH_MAX_HITS #define GMESH_MAX_HITS 50 #endif + // distance [m] within which gmesh_entry_exit takes a hit as the surface the + // particle sits on and skips it; a neutron is larger than this, so the ray + // picture does not hold below it anyway. To change it from an instrument, + // add DEPENDENCY " -DGMESH_TOL=1e-10 " after its DEFINE INSTRUMENT line + #ifndef GMESH_TOL + #define GMESH_TOL 1e-12 + #endif #ifndef GMESH_DISPLAY_MAX #define GMESH_DISPLAY_MAX 200000 #endif @@ -190,9 +197,10 @@ int gmesh_inside (gmesh_struct* mesh, Coords position); // int gmesh_entry_exit(gmesh_struct *mesh, Coords position, Coords velocity, // gmesh_hit *entering, gmesh_hit *leaving) - // ACTION: next entry and exit of the line position + t*velocity through + // ACTION: first entry and exit of the line position + t*velocity through // mesh, for a particle inside or outside a (closed) mesh. A hit on the - // surface the particle sits on (|t| below the mesh tolerance) is skipped. + // surface the particle sits on is skipped: the mesh tolerance is a distance + // of 1e-12 m, which can be changed with GMESH_TOL. // An open mesh counts as outside; with a single hit ahead, leaving = entering. // RETURN: 0 when the mesh is not ahead, 1 inside (only leaving is set), // 2 outside (entering and leaving are set) From ef7efd29979dff081c9e3a240ae5861dd739ad1d Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Wed, 11 Nov 2026 11:06:40 +0100 Subject: [PATCH 12/22] socket.off: retriangulate the flat top and bottom faces The triangle fans of the faces at z = -25 and z = 25 reached past the chords on which the side wall stands, and one triangle at each end (faces 159, 160, 195, 219) folded back over the fan. The mesh was closed and consistently oriented, but overlapped itself. Each face is now triangulated inside its outline (two regions with a hole each). The volume is unchanged; vertices 0, 1, 325 and 326 are no longer used. Co-Authored-By: Claude Opus 5.5 --- mcstas-comps/data/socket.off | 182 +++++++++++++++++------------------ 1 file changed, 87 insertions(+), 95 deletions(-) diff --git a/mcstas-comps/data/socket.off b/mcstas-comps/data/socket.off index 512d5ee977..708ea91958 100644 --- a/mcstas-comps/data/socket.off +++ b/mcstas-comps/data/socket.off @@ -1,6 +1,6 @@ OFF # This is an Object File Format (geomview) to describe a mechanical socket -327 678 0 +327 670 0 -100.000000 0.000000 -25.000000 -100.000000 0.000000 25.000000 -99.999170 0.407363 -0.064789 @@ -487,8 +487,92 @@ OFF 3 12 8 6 3 8 5 2 3 6 2 4 -3 1 6 4 -3 5 0 3 +3 12 6 4 +3 12 4 10 +3 12 10 16 +3 12 16 28 +3 28 44 50 +3 28 50 60 +3 28 60 62 +3 28 62 42 +3 28 42 37 +3 28 37 32 +3 28 32 23 +3 28 23 20 +3 12 28 20 +3 12 20 25 +3 12 25 34 +3 12 34 39 +3 39 42 62 +3 12 39 62 +3 12 62 75 +3 12 75 46 +3 12 46 30 +3 30 18 12 +3 315 321 323 +3 315 323 317 +3 315 317 311 +3 315 311 299 +3 299 283 277 +3 299 277 269 +3 299 269 263 +3 299 263 285 +3 299 285 290 +3 299 290 295 +3 299 295 304 +3 299 304 307 +3 315 299 307 +3 315 307 302 +3 315 302 293 +3 315 293 288 +3 288 285 263 +3 315 288 263 +3 315 263 253 +3 315 253 251 +3 315 251 281 +3 315 281 297 +3 297 309 315 +3 9 3 5 +3 9 5 11 +3 9 11 17 +3 9 17 29 +3 29 45 74 +3 29 74 61 +3 29 61 41 +3 29 41 38 +3 29 38 33 +3 29 33 24 +3 29 24 19 +3 9 29 19 +3 9 19 22 +3 9 22 31 +3 9 31 36 +3 36 41 61 +3 9 36 61 +3 9 61 49 +3 9 49 43 +3 9 43 27 +3 27 15 9 +3 316 322 303 +3 316 303 294 +3 316 294 289 +3 306 303 322 +3 301 306 322 +3 301 322 320 +3 292 301 320 +3 292 320 314 +3 287 292 314 +3 287 314 308 +3 287 308 296 +3 284 287 296 +3 284 296 280 +3 284 280 250 +3 284 250 276 +3 289 284 276 +3 316 289 276 +3 316 276 282 +3 316 282 298 +3 298 310 316 3 2 3 7 3 4 7 10 3 7 9 13 @@ -507,7 +591,6 @@ OFF 3 279 250 280 3 318 320 324 3 321 324 323 -3 326 321 323 3 318 315 312 3 314 312 308 3 314 318 312 @@ -523,7 +606,6 @@ OFF 3 297 300 309 3 300 308 312 3 309 312 315 -3 320 325 322 3 324 322 319 3 323 319 317 3 319 316 313 @@ -830,29 +912,6 @@ OFF 3 206 264 278 3 278 213 206 3 231 230 264 -3 25 75 20 -3 25 34 75 -3 34 39 75 -3 75 39 62 -3 62 39 42 -3 62 42 37 -3 60 37 32 -3 10 23 4 -3 10 16 23 -3 23 16 28 -3 23 60 32 -3 60 23 28 -3 62 37 60 -3 23 20 4 -3 4 20 1 -3 1 20 75 -3 6 75 12 -3 6 1 75 -3 46 30 75 -3 75 30 18 -3 12 75 18 -3 28 44 60 -3 60 44 50 3 54 51 56 3 54 52 51 3 51 52 53 @@ -879,79 +938,12 @@ OFF 3 62 60 57 3 64 76 69 3 62 57 59 -3 22 9 19 -3 22 31 9 -3 15 9 27 -3 27 9 43 -3 41 61 36 -3 41 38 61 -3 61 38 29 -3 45 74 29 -3 38 33 29 -3 33 24 29 -3 29 24 11 -3 11 17 29 -3 24 19 11 -3 11 19 0 -3 5 11 0 -3 74 61 29 -3 36 61 9 -3 43 9 49 -3 9 3 0 -3 19 9 0 -3 31 36 9 -3 49 9 61 3 61 74 73 3 61 73 48 3 48 49 61 -3 287 250 284 -3 287 292 250 -3 325 296 280 -3 250 325 280 -3 314 325 320 -3 314 308 325 -3 325 308 296 -3 306 325 301 -3 306 303 325 -3 310 325 298 -3 310 316 325 -3 303 294 325 -3 325 294 282 -3 276 294 289 -3 276 282 294 -3 276 289 284 -3 250 276 284 -3 301 325 292 -3 250 292 325 -3 282 298 325 -3 325 316 322 3 265 275 276 3 265 276 250 3 265 250 249 -3 290 263 285 -3 290 295 263 -3 323 299 283 -3 269 283 277 -3 269 323 283 -3 295 304 323 -3 317 311 323 -3 323 311 299 -3 304 307 323 -3 323 307 326 -3 326 307 302 -3 321 302 293 -3 315 293 288 -3 253 288 263 -3 253 281 288 -3 297 288 281 -3 297 309 288 -3 288 309 315 -3 288 285 263 -3 251 281 253 -3 315 321 293 -3 321 326 302 -3 263 295 323 -3 269 263 323 3 273 278 271 3 273 274 278 3 277 278 274 From 1a4c743b6677cbe40f00fb28976535d620ef2e8f Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Wed, 11 Nov 2026 11:06:59 +0100 Subject: [PATCH 13/22] geometry-mesh-lib: fold check and gmesh_inside shortcut; components use gmesh_inside + gmesh_intersect geometry-mesh-lib: - gmesh_init fails for a folded mesh, where the two triangles of an edge turn back onto each other (nfolded counts these edges). - gmesh_inside answers from the first hit along a ray for a closed, oriented mesh, skipping hits closer than GMESH_TOL, with the parity test for edge hits and coincident hits. The bounding box test is padded by GMESH_TOL. - gmesh_entry_exit and gmesh_entry_exit_gravity are removed. Components: - Incoherent, PowderN, Isotropic_Sqw, Single_crystal, Single_magnetic_crystal, Single_crystal_inelastic and Refractor use gmesh_inside and gmesh_intersect (t_min = GMESH_TOL/|v|) inline. The crystals enter a mesh 1 ns past its surface. Lens takes the first hit of gmesh_intersect. Isotropic_Sqw handles a mesh once, for either sign of thickness. - The sample components document that a mesh must be a single, closed object. - Shape, Shape_simple and these samples abort when the geometry file can not be used. - Monitor_nD, Monitor_nD_noacc and monitor_nd-lib are as on mesh_lib, apart from the gmesh_hit time rename. The libtest instrument checks the fold check and the gmesh_inside shortcut instead of gmesh_entry_exit. Co-Authored-By: Claude Opus 5.5 --- common/lib/share/geometry-mesh-lib.c | 95 +++++++++---------- common/lib/share/geometry-mesh-lib.h | 45 +++------ mcstas-comps/contrib/Lens.comp | 33 +++---- .../contrib/Single_crystal_inelastic.comp | 29 ++++-- .../Test_lib_geometry_mesh_lib.instr | 75 ++++++++------- mcstas-comps/misc/Shape.comp | 3 +- mcstas-comps/misc/Shape_simple.comp | 3 +- mcstas-comps/monitors/Monitor_nD.comp | 11 +-- mcstas-comps/monitors/Monitor_nD_noacc.comp | 11 +-- mcstas-comps/optics/Refractor.comp | 13 +-- mcstas-comps/samples/Incoherent.comp | 30 ++++-- mcstas-comps/samples/Isotropic_Sqw.comp | 46 ++++----- mcstas-comps/samples/PowderN.comp | 39 +++++--- mcstas-comps/samples/Single_crystal.comp | 31 ++++-- .../samples/Single_magnetic_crystal.comp | 31 ++++-- mcstas-comps/share/monitor_nd-lib.c | 31 ++++++ mcstas-comps/share/monitor_nd-lib.h | 2 + 17 files changed, 295 insertions(+), 233 deletions(-) diff --git a/common/lib/share/geometry-mesh-lib.c b/common/lib/share/geometry-mesh-lib.c index a579fb6ad6..b1bb9f0265 100644 --- a/common/lib/share/geometry-mesh-lib.c +++ b/common/lib/share/geometry-mesh-lib.c @@ -1410,6 +1410,7 @@ gmesh_load (char* filename, double scale, double xwidth, double yheight, double int ok = 0; int i, k; int* map; + int folded_face[2] = { -1, -1 }; if (!mesh || !filename) return 0; @@ -1722,6 +1723,20 @@ gmesh_load (char* filename, double scale, double xwidth, double yheight, double } else if (edge->dir != 0) { mesh->oriented = 0; } + // folded: the two triangles of the edge turn back onto each other, i.e. + // their normals are antiparallel once oriented alike (dir 0) + if (edge->n == 2) { + double turn = coords_sp (mesh->tri_normal[edge->t0], mesh->tri_normal[edge->t1]); + if (edge->dir != 0) + turn = -turn; + if (turn < -1 + GMESH_EPSILON) { + if (!mesh->nfolded) { + folded_face[0] = mesh->tri_face[edge->t0]; + folded_face[1] = mesh->tri_face[edge->t1]; + } + mesh->nfolded++; + } + } } if (mesh->nnonmanifold) mesh->oriented = 0; @@ -1739,6 +1754,16 @@ gmesh_load (char* filename, double scale, double xwidth, double yheight, double free (used); } + if (mesh->nfolded) { + fprintf (stderr, + "Error: %s: %d edges where the surface folds back onto itself, e.g. between faces %d and %d (0-based): " + "the mesh overlaps itself, so inside and outside are not defined. Fix the vertex order of the folded faces " + "(geometry-mesh-lib)\n", + filename, mesh->nfolded, folded_face[0], folded_face[1]); + gmesh_free (mesh); + return 0; + } + // volume (divergence theorem); outward normals for closed oriented meshes { double volume = 0; @@ -2293,10 +2318,27 @@ int gmesh_inside (gmesh_struct* mesh, Coords position) { // fixed, "irrational" directions: no random numbers are consumed const double D[3][3] = { { 0.4719, 0.6213, 0.6254 }, { 0.8017, -0.3259, 0.5012 }, { -0.2263, 0.9147, -0.3349 } }; + Coords pad = coords_set (GMESH_TOL, GMESH_TOL, GMESH_TOL); int j; int votes = 0; - if (!gmesh_in_box (position, mesh->bbmin, mesh->bbmax)) + // the box is padded by GMESH_TOL, for a point on the surface after rounding + if (!gmesh_in_box (position, coords_sub (mesh->bbmin, pad), coords_add (mesh->bbmax, pad))) return 0; + // a closed, oriented mesh has outward normals: the point is inside when the + // first hit along a ray is leaving. Hits closer than GMESH_TOL are skipped, + // as they are the surface the point sits on. A first hit on an edge, or two + // hits at the same distance (touching parts of the mesh), may disagree, so + // they are left to the parity test. + if (mesh->closed && mesh->oriented) { + gmesh_hit first[2]; + int stored = 0; + int ambiguous = 0; + gmesh_trace (mesh, position, coords_set (D[0][0], D[0][1], D[0][2]), GMESH_TOL, first, 2, &stored, &ambiguous); + if (!stored) + return 0; + if (!first[0].edge && (stored == 1 || first[1].time - first[0].time > GMESH_TOL)) + return first[0].in_out < 0; + } for (j = 0; j < 3; j++) { int stored = 0; int ambiguous = 0; @@ -2309,57 +2351,6 @@ gmesh_inside (gmesh_struct* mesh, Coords position) { return votes >= 2; } - // first entry and exit among the nhits hits ahead of a particle at position, for - // gmesh_entry_exit and gmesh_entry_exit_gravity (same return values) - #pragma acc routine seq -static int -gmesh_pick_entry_exit (gmesh_struct* mesh, Coords position, gmesh_hit* hits, int nhits, gmesh_hit* entering, gmesh_hit* leaving) { - int inside; - if (!nhits) - return 0; - // normals of a closed, oriented mesh point outward: leaving first means inside - if (!mesh->closed) - inside = 0; - else if (mesh->oriented) - inside = hits[0].in_out < 0; - else - inside = gmesh_inside (mesh, position); - if (inside) { - *leaving = hits[0]; - return 1; - } - *entering = hits[0]; - if (nhits > 1) - *leaving = hits[1]; - else - *leaving = hits[0]; - return 2; -} - - // first entry and exit of a line through mesh, see geometry-mesh-lib.h - #pragma acc routine seq -int -gmesh_entry_exit (gmesh_struct* mesh, Coords position, Coords velocity, gmesh_hit* entering, gmesh_hit* leaving) { - gmesh_hit hits[GMESH_MAX_HITS]; - // skips the surface the particle sits on (closer than GMESH_TOL), e.g. after a propagation to it - double tolerance = GMESH_TOL / coords_len (velocity); - int nhits = gmesh_intersect (mesh, position, velocity, tolerance, hits); - return gmesh_pick_entry_exit (mesh, position, hits, nhits, entering, leaving); -} - - // first entry and exit of a parabola through mesh, see geometry-mesh-lib.h - #pragma acc routine seq -int -gmesh_entry_exit_gravity (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, gmesh_hit* entering, gmesh_hit* leaving) { - gmesh_hit hits[GMESH_MAX_HITS]; - // skips the surface the particle sits on (closer than GMESH_TOL); the speed - // scale is the one of gmesh_trace_gravity, as a parabola may start at rest - double speed = coords_len (velocity) + sqrt (coords_len (acceleration) * mesh->length_scale); - double tolerance = GMESH_TOL / speed; - int nhits = gmesh_intersect_gravity (mesh, position, velocity, acceleration, tolerance, hits); - return gmesh_pick_entry_exit (mesh, position, hits, nhits, entering, leaving); -} - // per-face property, see geometry-mesh-lib.h #pragma acc routine seq double diff --git a/common/lib/share/geometry-mesh-lib.h b/common/lib/share/geometry-mesh-lib.h index 21cfbefd57..dd990da9e4 100644 --- a/common/lib/share/geometry-mesh-lib.h +++ b/common/lib/share/geometry-mesh-lib.h @@ -35,6 +35,9 @@ * boundary / non-manifold edges, Euler characteristic, consistent * orientation. Closed, consistently oriented meshes are oriented with * outward normals. + * - rejects a folded mesh (gmesh_init fails): one where the two triangles + * of an edge turn back onto each other (antiparallel normals), so the + * surface overlaps itself and inside and outside are not defined * - builds a bounding volume hierarchy (BVH) so that a ray costs * O(log N) instead of O(N) triangle tests, without any allocation per ray. * @@ -43,8 +46,6 @@ * straight line, sorted in t * gmesh_intersect_gravity() the same for a parabola (e.g. under gravity) * gmesh_inside() point-in-mesh test (deterministic, no random numbers) - * gmesh_entry_exit() first entry and exit (only exit from inside) - * gmesh_entry_exit_gravity() the same for a parabola * Hits exactly on an edge or vertex shared by several triangles are * reported once. * @@ -80,10 +81,11 @@ #ifndef GMESH_MAX_HITS #define GMESH_MAX_HITS 50 #endif - // distance [m] within which gmesh_entry_exit takes a hit as the surface the - // particle sits on and skips it; a neutron is larger than this, so the ray - // picture does not hold below it anyway. To change it from an instrument, - // add DEPENDENCY " -DGMESH_TOL=1e-10 " after its DEFINE INSTRUMENT line + // distance [m] within which a hit is the surface the particle sits on, to be + // skipped: gmesh_inside skips it, and components pass t_min = GMESH_TOL/|v| + // to gmesh_intersect. A neutron is larger than this, so the ray picture does + // not hold below it anyway. To change it from an instrument, add + // DEPENDENCY " -DGMESH_TOL=1e-10 " after its DEFINE INSTRUMENT line #ifndef GMESH_TOL #define GMESH_TOL 1e-12 #endif @@ -135,6 +137,7 @@ typedef struct gmesh_struct { double area, volume; // surface area, enclosed volume (closed meshes) int nedges, nboundary, nnonmanifold, ndegenerate, nwelded; int nduplicate; // faces given more than once (all copies removed) + int nfolded; // edges where the surface folds back onto itself (gmesh_init then fails) int euler; // V - E + F (2 for a closed genus-0 surface) int closed; // every edge shared by exactly two triangles int oriented; // shared edges run in opposite directions @@ -153,7 +156,7 @@ typedef struct gmesh_struct { // ACTION: read an STL/OFF/PLY file and build the mesh structure. // INPUT: scale: multiply file coordinates (1 for m, 1e-3 for mm, 0 same as 1) // face_props: GMESH_PROPS_IGNORE, GMESH_PROPS_OPTIONAL or GMESH_PROPS_REQUIRED -// RETURN: number of triangles, 0 on error +// RETURN: number of triangles, 0 on error (also for a folded mesh) int gmesh_init (char* filename, double scale, int face_props, gmesh_struct* mesh); // int gmesh_init_fit(char *filename, double xwidth, double yheight, double zdepth, @@ -162,7 +165,7 @@ int gmesh_init (char* filename, double scale, int face_props, gmesh_struct* mesh // non-zero sizes set the box size along x/y/z; a single size scales all // axes by the same factor (proportions kept). With center, the box centre // is moved to the origin; otherwise coordinates are scaled about the origin. -// RETURN: number of triangles, 0 on error +// RETURN: number of triangles, 0 on error (also for a folded mesh) int gmesh_init_fit (char* filename, double xwidth, double yheight, double zdepth, int center, int face_props, gmesh_struct* mesh); // int gmesh_intersect(gmesh_struct *mesh, Coords position, Coords direction, @@ -189,32 +192,14 @@ int gmesh_intersect (gmesh_struct* mesh, Coords position, Coords direction, doub int gmesh_intersect_gravity (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, double t_min, gmesh_hit hits[GMESH_MAX_HITS]); // int gmesh_inside(gmesh_struct *mesh, Coords position) - // ACTION: point in (closed) mesh test, parity of ray crossings along fixed - // directions. Does not use random numbers. + // ACTION: point in (closed) mesh test. For a closed, oriented mesh: whether + // the first hit along a fixed direction, beyond GMESH_TOL, is leaving; + // otherwise the parity of ray crossings along fixed directions. Does not use + // random numbers. // RETURN: 1 inside, 0 outside #pragma acc routine seq int gmesh_inside (gmesh_struct* mesh, Coords position); - // int gmesh_entry_exit(gmesh_struct *mesh, Coords position, Coords velocity, - // gmesh_hit *entering, gmesh_hit *leaving) - // ACTION: first entry and exit of the line position + t*velocity through - // mesh, for a particle inside or outside a (closed) mesh. A hit on the - // surface the particle sits on is skipped: the mesh tolerance is a distance - // of 1e-12 m, which can be changed with GMESH_TOL. - // An open mesh counts as outside; with a single hit ahead, leaving = entering. - // RETURN: 0 when the mesh is not ahead, 1 inside (only leaving is set), - // 2 outside (entering and leaving are set) - #pragma acc routine seq -int gmesh_entry_exit (gmesh_struct* mesh, Coords position, Coords velocity, gmesh_hit* entering, gmesh_hit* leaving); - - // int gmesh_entry_exit_gravity(gmesh_struct *mesh, Coords position, - // Coords velocity, Coords acceleration, gmesh_hit *entering, gmesh_hit *leaving) - // ACTION: as gmesh_entry_exit, for the parabola - // position + t*velocity + t^2/2*acceleration (see gmesh_intersect_gravity) - // RETURN: as gmesh_entry_exit - #pragma acc routine seq -int gmesh_entry_exit_gravity (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, gmesh_hit* entering, gmesh_hit* leaving); - // per-face property number 'property' of face 'face', or fallback when it is // not given in the file #pragma acc routine seq diff --git a/mcstas-comps/contrib/Lens.comp b/mcstas-comps/contrib/Lens.comp index 749dec1dd7..1efd60dcef 100644 --- a/mcstas-comps/contrib/Lens.comp +++ b/mcstas-comps/contrib/Lens.comp @@ -279,15 +279,12 @@ TRACE nx = ny = nz = 0; #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - gmesh_hit entering, leaving; - int nmesh = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); - if (nmesh) { - // next hit: the entry from outside, the exit from inside - gmesh_hit* next = nmesh == 2 ? &entering : &leaving; - dt = next->time; + gmesh_hit hits[GMESH_MAX_HITS]; + if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits)) { + dt = hits[0].time; // normal oriented as in interoff-lib: opposite to the right-hand rule of // the vertex order in the file, unless the mesh was flipped - coords_get (coords_scale (next->normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); + coords_get (coords_scale (hits[0].normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); } else dt = -1; } else @@ -379,15 +376,12 @@ TRACE nx = ny = nz = 0; #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - gmesh_hit entering, leaving; - int nmesh = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); - if (nmesh) { - // next hit: the entry from outside, the exit from inside - gmesh_hit* next = nmesh == 2 ? &entering : &leaving; - dt = next->time; + gmesh_hit hits[GMESH_MAX_HITS]; + if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits)) { + dt = hits[0].time; // normal oriented as in interoff-lib: opposite to the right-hand rule of // the vertex order in the file, unless the mesh was flipped - coords_get (coords_scale (next->normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); + coords_get (coords_scale (hits[0].normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); } else dt = -1; } else @@ -492,15 +486,12 @@ TRACE nx = ny = nz = 0; #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - gmesh_hit entering, leaving; - int nmesh = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); - if (nmesh) { - // next hit: the entry from outside, the exit from inside - gmesh_hit* next = nmesh == 2 ? &entering : &leaving; - dt = next->time; + gmesh_hit hits[GMESH_MAX_HITS]; + if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits)) { + dt = hits[0].time; // normal oriented as in interoff-lib: opposite to the right-hand rule of // the vertex order in the file, unless the mesh was flipped - coords_get (coords_scale (next->normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); + coords_get (coords_scale (hits[0].normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); } else dt = -1; } else diff --git a/mcstas-comps/contrib/Single_crystal_inelastic.comp b/mcstas-comps/contrib/Single_crystal_inelastic.comp index f0aae94fc3..85482154b9 100644 --- a/mcstas-comps/contrib/Single_crystal_inelastic.comp +++ b/mcstas-comps/contrib/Single_crystal_inelastic.comp @@ -55,7 +55,7 @@ * yheight: [m] Height of crystal. * zdepth: [m] Depth of crystal (no extinction simulated) * sqw: [string] Name of a 4d S(q,w) file - example.sqw4 is included in your installation -* geometry: [string] Name of an OFF, PLY or STL file for complex geometry. An OFF file may be generated from XYZ coordinates using qhull/powercrust. +* geometry: [string] Name of an OFF, PLY or STL file for complex geometry. An OFF file may be generated from XYZ coordinates using qhull/powercrust. The mesh must be a single, closed object: only the first entry and exit along the neutron path are used, so a second part of the mesh further along the path (disjoint parts, or a concave shape the path re-enters) is not seen. * delta_d_d: [1] Lattice spacing variance, gaussian RMS. * mosaic: [arcmin] Isotropic crystal mosaic, gaussian RMS. Puts the crystal in the isotropic mosaic model state, thus disregarding other mosaicity parameters. * mosaic_a: [arcmin] Horizontal (rotation around lattice vector a) mosaic (anisotropic), gaussian RMS. Put the crystal in the anisotropic crystal vector state. I.e. model mosaicity through rotation around the crystal lattice vectors. Has precedence over in-plane mosaic model. @@ -719,7 +719,8 @@ INITIALIZE if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { hkl_info.shape = 3; - } + } else + exit (fprintf (stderr, "Single_crystal_inelastic: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); } else if (xwidth && yheight && zdepth) hkl_info.shape = 1; /* box */ else if (radius > 0 && yheight) @@ -801,11 +802,18 @@ TRACE intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) { - gmesh_hit entering, leaving; - intersect = gmesh_entry_exit (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); + intersect = nhits > 0; if (intersect) { - t1 = intersect == 2 ? entering.time : 0; // 0 when inside - t2 = leaving.time; + if (gmesh_inside (&mesh, coords_set (x, y, z))) { + t1 = 0; + t2 = hits[0].time; + } else { + // 1 ns past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) + t1 = hits[0].time + 1e-9; + t2 = nhits > 1 ? hits[1].time : hits[0].time; + } } } @@ -842,11 +850,12 @@ TRACE intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) { - gmesh_hit entering, leaving; - intersect = gmesh_entry_exit (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); + intersect = nhits > 0 && gmesh_inside (&mesh, coords_set (x, y, z)); if (intersect) { - t1 = intersect == 2 ? entering.time : 0; // 0 when inside - t2 = leaving.time; + t1 = 0; + t2 = hits[0].time; } } if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { diff --git a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr index 7065eb31ee..e481bbf53c 100644 --- a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr +++ b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr @@ -6,10 +6,11 @@ * * Smoke test: builds and runs with the library geometry-mesh-lib in the instrument. * Fails to compile or link if the library does not build with the runtime. -* INITIALIZE also checks gmesh_entry_exit and gmesh_intersect_gravity, and +* INITIALIZE also checks gmesh_inside and gmesh_intersect_gravity, and * exits with an error when a check fails: -* - gmesh_entry_exit on cube.off from outside, inside, on the entry face and -* past the cube, and gmesh_entry_exit_gravity for arcs into and out of it +* - gmesh_init rejects a folded mesh +* - gmesh_inside on cube.off, and its shortcut for closed, oriented meshes +* against the parity of ray crossings for random points around socket.off * - with zero acceleration it gives the hits of gmesh_intersect, to rounding * - parabolas through cube.off give the analytic hit times and in_out * - random parabolas through socket.off give the hits of a fine polyline of @@ -44,40 +45,48 @@ double gravity_uniform (unsigned long* state, double low, double high) { return low + (high - low) * (double)(*state >> 11) / 9007199254740992.0; } -// checks of gmesh_entry_exit on the unit cube; returns the number of failed checks -int entry_exit_tests (void) { +// checks of gmesh_inside; returns the number of failed checks +int inside_tests (void) { gmesh_struct cube; - gmesh_hit entering; - gmesh_hit leaving; + gmesh_struct socket; + gmesh_struct socket_parity; + unsigned long state = 4321; int failures = 0; - int result; - if (!gmesh_init ("cube.off", 1, GMESH_PROPS_IGNORE, &cube)) { - fprintf (stderr, "Test_lib_geometry_mesh_lib: FAILED: can not read cube.off\n"); + int differ = 0; + int i_point; + if (!gmesh_init ("cube.off", 1, GMESH_PROPS_IGNORE, &cube) || !gmesh_init_fit ("socket.off", 0.1, 0, 0, 1, GMESH_PROPS_IGNORE, &socket)) { + fprintf (stderr, "Test_lib_geometry_mesh_lib: FAILED: can not read cube.off or socket.off\n"); return 1; } - result = gmesh_entry_exit (&cube, coords_set (0.5, 0.5, -1), coords_set (0, 0, 2), &entering, &leaving); - mesh_check (result == 2 && fabs (entering.time - 0.5) < 1e-12 && fabs (leaving.time - 1) < 1e-12, "entry_exit from outside", &failures); - result = gmesh_entry_exit (&cube, coords_set (0.5, 0.5, 0.25), coords_set (0, 0, 1), &entering, &leaving); - mesh_check (result == 1 && fabs (leaving.time - 0.75) < 1e-12, "entry_exit from inside", &failures); - result = gmesh_entry_exit (&cube, coords_set (0.5, 0.5, 0), coords_set (0, 0, 1), &entering, &leaving); - mesh_check (result == 1 && fabs (leaving.time - 1) < 1e-12, "entry_exit on the entry face", &failures); - result = gmesh_entry_exit (&cube, coords_set (0.5, 0.5, 1), coords_set (0, 0, 1), &entering, &leaving); - mesh_check (result == 0, "entry_exit on the exit face, leaving", &failures); - result = gmesh_entry_exit (&cube, coords_set (0.5, 0.5, 2), coords_set (0, 0, 1), &entering, &leaving); - mesh_check (result == 0, "entry_exit past the cube", &failures); - // falling from rest above the cube: entry through the top at sqrt(2), exit through the bottom at 2 - result = gmesh_entry_exit_gravity (&cube, coords_set (0.5, 2, 0.5), coords_set (0, 0, 0), coords_set (0, -1, 0), &entering, &leaving); - mesh_check (result == 2 && fabs (entering.time - sqrt (2)) < 1e-12 && fabs (leaving.time - 2) < 1e-12, "entry_exit_gravity from outside", &failures); - // thrown up from inside, falling out through the bottom at (1+sqrt(5))/2 - result = gmesh_entry_exit_gravity (&cube, coords_set (0.5, 0.5, 0.5), coords_set (0, 0.5, 0), coords_set (0, -1, 0), &entering, &leaving); - mesh_check (result == 1 && fabs (leaving.time - (1 + sqrt (5)) / 2) < 1e-12, "entry_exit_gravity from inside", &failures); - // the arc passes above the cube - result = gmesh_entry_exit_gravity (&cube, coords_set (-1, 1.5, 0.5), coords_set (1, 0, 0), coords_set (0, -0.1, 0), &entering, &leaving); - mesh_check (result == 0, "entry_exit_gravity arc above the cube", &failures); - // zero acceleration: as gmesh_entry_exit - result = gmesh_entry_exit_gravity (&cube, coords_set (0.5, 0.5, -1), coords_set (0, 0, 2), coords_set (0, 0, 0), &entering, &leaving); - mesh_check (result == 2 && fabs (entering.time - 0.5) < 1e-12 && fabs (leaving.time - 1) < 1e-12, "entry_exit_gravity without acceleration", &failures); + mesh_check (gmesh_inside (&cube, coords_set (0.5, 0.5, 0.5)) == 1, "inside the cube", &failures); + // a folded mesh: a unit cube whose bottom reaches past the edge x = 1 to + // (1.5, 0.5, 0), where triangle 2 1 8 folds back. It is closed and + // consistently oriented, but overlaps itself, so gmesh_init fails. + { + gmesh_struct folded; + FILE* file = fopen ("Test_lib_geometry_mesh_lib_folded.off", "w"); + if (file) { + fprintf (file, "OFF\n9 9 0\n0 0 0\n1 0 0\n1 1 0\n0 1 0\n0 0 1\n1 0 1\n1 1 1\n0 1 1\n1.5 0.5 0\n" + "4 4 5 6 7\n4 0 1 5 4\n4 1 2 6 5\n4 2 3 7 6\n4 0 4 7 3\n3 1 0 8\n3 0 3 8\n3 3 2 8\n3 2 1 8\n"); + fclose (file); + mesh_check (gmesh_init ("Test_lib_geometry_mesh_lib_folded.off", 1, GMESH_PROPS_IGNORE, &folded) == 0, "folded mesh rejected", &failures); + remove ("Test_lib_geometry_mesh_lib_folded.off"); + } + } + mesh_check (gmesh_inside (&cube, coords_set (0.5, 0.5, 1.5)) == 0, "outside the cube", &failures); + // the shortcut for closed, oriented meshes against the parity of ray crossings + socket_parity = socket; + socket_parity.oriented = 0; + for (i_point = 0; i_point < 2000; i_point++) { + Coords point = coords_set (gravity_uniform (&state, socket.bbmin.x, socket.bbmax.x), gravity_uniform (&state, socket.bbmin.y, socket.bbmax.y), + gravity_uniform (&state, socket.bbmin.z, socket.bbmax.z)); + if (gmesh_inside (&socket, point) != gmesh_inside (&socket_parity, point)) + differ++; + } + printf ("Test_lib_geometry_mesh_lib: socket.off: gmesh_inside shortcut and parity differ for %d of 2000 points\n", differ); + mesh_check (socket.closed && socket.oriented && differ == 0, "gmesh_inside shortcut against parity on socket.off", &failures); gmesh_free (&cube); + gmesh_free (&socket); return failures; } @@ -184,7 +193,7 @@ int gravity_tests (void) { INITIALIZE %{ mcset_ncount(Ncount); - if (entry_exit_tests () + gravity_tests ()) + if (inside_tests () + gravity_tests ()) exit (fprintf (stderr, "Test_lib_geometry_mesh_lib: geometry-mesh-lib checks failed\n")); %} diff --git a/mcstas-comps/misc/Shape.comp b/mcstas-comps/misc/Shape.comp index 6a2cee6176..fdd3d90afb 100644 --- a/mcstas-comps/misc/Shape.comp +++ b/mcstas-comps/misc/Shape.comp @@ -82,7 +82,8 @@ INITIALIZE if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_IGNORE, &mesh)) { thickness = 0; - } + } else + exit (fprintf (stderr, "Shape: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); } %} diff --git a/mcstas-comps/misc/Shape_simple.comp b/mcstas-comps/misc/Shape_simple.comp index baaf31db8d..f0eaabb118 100644 --- a/mcstas-comps/misc/Shape_simple.comp +++ b/mcstas-comps/misc/Shape_simple.comp @@ -80,7 +80,8 @@ INITIALIZE if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_IGNORE, &mesh)) { thickness = 0; - } + } else + exit (fprintf (stderr, "Shape_simple: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); } %} diff --git a/mcstas-comps/monitors/Monitor_nD.comp b/mcstas-comps/monitors/Monitor_nD.comp index 5593997c39..2c0f212e3f 100644 --- a/mcstas-comps/monitors/Monitor_nD.comp +++ b/mcstas-comps/monitors/Monitor_nD.comp @@ -521,16 +521,11 @@ TRACE #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "0") && strcmp (geometry, "NULL")) { /* determine intersections with object */ - gmesh_hit entering, leaving; - intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); - if (intersect) { - t0 = intersect == 2 ? entering.time : 0; // 0 when inside - t1 = leaving.time; - } + int face = 0; + intersect = Monitor_nD_MeshIntersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &face); if (Vars.Flag_mantid) { if (intersect) { - // pixel of the next hit: the entry from outside, the exit from inside - Vars.OFF_polyidx = intersect == 2 ? entering.face : leaving.face; + Vars.OFF_polyidx = face; } else { Vars.OFF_polyidx = -1; } diff --git a/mcstas-comps/monitors/Monitor_nD_noacc.comp b/mcstas-comps/monitors/Monitor_nD_noacc.comp index 05d5733181..157eac7c1d 100644 --- a/mcstas-comps/monitors/Monitor_nD_noacc.comp +++ b/mcstas-comps/monitors/Monitor_nD_noacc.comp @@ -516,16 +516,11 @@ TRACE */ if (geometry && strlen (geometry) && strcmp (geometry, "0") && strcmp (geometry, "NULL")) { /* determine intersections with object */ - gmesh_hit entering, leaving; - intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); - if (intersect) { - t0 = intersect == 2 ? entering.time : 0; // 0 when inside - t1 = leaving.time; - } + int face = 0; + intersect = Monitor_nD_MeshIntersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &face); if (Vars.Flag_mantid) { if (intersect) { - // pixel of the next hit: the entry from outside, the exit from inside - Vars.OFF_polyidx = intersect == 2 ? entering.face : leaving.face; + Vars.OFF_polyidx = face; } else { Vars.OFF_polyidx = -1; } diff --git a/mcstas-comps/optics/Refractor.comp b/mcstas-comps/optics/Refractor.comp index 845ecc2cda..c6f90bd652 100644 --- a/mcstas-comps/optics/Refractor.comp +++ b/mcstas-comps/optics/Refractor.comp @@ -410,15 +410,16 @@ TRACE } #ifdef USE_OFF else if (geometry && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - gmesh_hit entering, leaving; - intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); + intersect = nhits > 0; if (intersect) { - mesh_inside = (intersect == 1); - t0 = mesh_inside ? 0 : entering.time; - t1 = leaving.time; + mesh_inside = gmesh_inside (&thread_mesh, coords_set (x, y, z)); + t0 = mesh_inside ? 0 : hits[0].time; + t1 = mesh_inside || nhits == 1 ? hits[0].time : hits[1].time; // normal of the next hit, oriented as in interoff-lib: opposite to the // right-hand rule of the vertex order in the file, unless the mesh was flipped - coords_get (coords_scale (mesh_inside ? leaving.normal : entering.normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); + coords_get (coords_scale (hits[0].normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); } } #endif diff --git a/mcstas-comps/samples/Incoherent.comp b/mcstas-comps/samples/Incoherent.comp index 2a12340330..be87a498a3 100644 --- a/mcstas-comps/samples/Incoherent.comp +++ b/mcstas-comps/samples/Incoherent.comp @@ -45,6 +45,10 @@ * It computes the intersection points of the neutron ray with the object * transparently, so that it can be used like a regular sample object. * It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. +* A mesh must be a single, closed object: the neutron path through it is +* taken from the first entry and exit only, so a second part of the mesh +* further along the path (disjoint parts, or a concave shape the path +* re-enters) is not seen. * Such files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off * or @@ -159,7 +163,8 @@ INITIALIZE VarsInc.shape = 3; thickness = 0; concentric = 0; - } + } else + exit (fprintf (stderr, "Incoherent: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); #endif } else if (xwidth && yheight && zdepth) VarsInc.shape = 1; /* box */ @@ -280,11 +285,17 @@ TRACE #ifdef USE_OFF else if (VarsInc.shape == 3) { - gmesh_hit entering, leaving; - intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); + intersect = nhits > 0; if (intersect) { - t0 = intersect == 2 ? entering.time : 0; // 0 when inside - t3 = leaving.time; + if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { + t0 = 0; + t3 = hits[0].time; + } else { + t0 = hits[0].time; + t3 = nhits > 1 ? hits[1].time : hits[0].time; + } } } #endif @@ -484,11 +495,12 @@ TRACE #ifdef USE_OFF else if (VarsInc.shape == 3) { - gmesh_hit entering, leaving; - intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); + intersect = nhits > 0; if (intersect) { - t0 = intersect == 2 ? entering.time : 0; // 0 when inside - t3 = leaving.time; + t0 = 0; + t3 = hits[0].time; } } #endif diff --git a/mcstas-comps/samples/Isotropic_Sqw.comp b/mcstas-comps/samples/Isotropic_Sqw.comp index 16af9de4a4..7c887640cf 100644 --- a/mcstas-comps/samples/Isotropic_Sqw.comp +++ b/mcstas-comps/samples/Isotropic_Sqw.comp @@ -69,6 +69,10 @@ * It computes the intersection points of the neutron ray with the object * transparently, so that it can be used like a regular sample object. * It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. +* A mesh must be a single, closed object: the neutron path through it is +* taken from the first entry and exit only, so a second part of the mesh +* further along the path (disjoint parts, or a concave shape the path +* re-enters) is not seen. * Such files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off * or @@ -2033,7 +2037,8 @@ INITIALIZE VarSqw.shape = 3; thickness = 0; concentric = 0; - } + } else + exit (fprintf (stderr, "Isotropic_Sqw: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); #endif } else if (xwidth && yheight && zdepth) VarSqw.shape = 1; /* box */ @@ -2247,6 +2252,23 @@ TRACE /* Intersection neutron trajectory / sample (sample surface) */ if (VarSqw.s_coh > 0 || VarSqw.s_inc > 0) { + #ifdef USE_OFF + // a mesh is used as given, for either sign of thickness + if (VarSqw.shape == 3) { + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); + intersect = nhits > 0; + if (intersect) { + if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { + t0 = 0; + t3 = hits[0].time; + } else { + t0 = hits[0].time; + t3 = nhits > 1 ? hits[1].time : hits[0].time; + } + } + } else + #endif if (thickness >= 0) { if (VarSqw.shape == 0) intersect = cylinder_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius, yheight); @@ -2254,17 +2276,6 @@ TRACE intersect = box_intersect (&t0, &t3, x, y, z, vx, vy, vz, xwidth, yheight, zdepth); else if (VarSqw.shape == 2) intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); - #ifdef USE_OFF - else if (VarSqw.shape == 3) - { - gmesh_hit entering, leaving; - intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); - if (intersect) { - t0 = intersect == 2 ? entering.time : 0; // 0 when inside - t3 = leaving.time; - } - } - #endif } else { if (VarSqw.shape == 0) intersect = cylinder_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius - thickness, yheight - 2 * thickness > 0 ? yheight - 2 * thickness : yheight); @@ -2274,17 +2285,6 @@ TRACE yheight - 2 * thickness > 0 ? yheight - 2 * thickness : yheight, zdepth - 2 * thickness > 0 ? zdepth - 2 * thickness : zdepth); else if (VarSqw.shape == 2) intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius - thickness); - #ifdef USE_OFF - else if (VarSqw.shape == 3) - { - gmesh_hit entering, leaving; - intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); - if (intersect) { - t0 = intersect == 2 ? entering.time : 0; // 0 when inside - t3 = leaving.time; - } - } - #endif } } else intersect = 0; diff --git a/mcstas-comps/samples/PowderN.comp b/mcstas-comps/samples/PowderN.comp index dc9c450641..95c252be5b 100644 --- a/mcstas-comps/samples/PowderN.comp +++ b/mcstas-comps/samples/PowderN.comp @@ -55,6 +55,10 @@ * It computes the intersection points of the neutron ray with the object * transparently, so that it can be used like a regular sample object. * It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. +* A mesh must be a single, closed object: the neutron path through it is +* taken from the first entry and exit only, so a second part of the mesh +* further along the path (disjoint parts, or a concave shape the path +* re-enters) is not seen. * Such files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off * or @@ -880,7 +884,8 @@ INITIALIZE line_info.shape = 3; thickness = 0; concentric = 0; - } + } else + exit (fprintf (stderr, "PowderN: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); #endif } else if (xwidth && yheight && zdepth) line_info.shape = 1; /* box */ @@ -1116,11 +1121,17 @@ TRACE #ifdef USE_OFF else if (line_info.shape == 3) { { - gmesh_hit entering, leaving; - intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); + intersect = nhits > 0; if (intersect) { - t0 = intersect == 2 ? entering.time : 0; // 0 when inside - t3 = leaving.time; + if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { + t0 = 0; + t3 = hits[0].time; + } else { + t0 = hits[0].time; + t3 = nhits > 1 ? hits[1].time : hits[0].time; + } } } intersecti = 0; @@ -1318,11 +1329,12 @@ TRACE #ifdef USE_OFF else if (line_info.shape == 3) { { - gmesh_hit entering, leaving; - intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); + intersect = nhits > 0; if (intersect) { - t0 = intersect == 2 ? entering.time : 0; // 0 when inside - t3 = leaving.time; + t0 = 0; + t3 = hits[0].time; } } intersecti = 0; @@ -1399,11 +1411,12 @@ TRACE #ifdef USE_OFF else if (line_info.shape == 3) { { - gmesh_hit entering, leaving; - intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); + intersect = nhits > 0; if (intersect) { - t0 = intersect == 2 ? entering.time : 0; // 0 when inside - t3 = leaving.time; + t0 = 0; + t3 = hits[0].time; } } intersecti = 0; diff --git a/mcstas-comps/samples/Single_crystal.comp b/mcstas-comps/samples/Single_crystal.comp index 2fb13c829b..4530080e20 100644 --- a/mcstas-comps/samples/Single_crystal.comp +++ b/mcstas-comps/samples/Single_crystal.comp @@ -85,6 +85,10 @@ * It computes the intersection points of the neutron ray with the object * transparently, so that it can be used like a regular sample object. * It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. +* A mesh must be a single, closed object: the neutron path through it is +* taken from the first entry and exit only, so a second part of the mesh +* further along the path (disjoint parts, or a concave shape the path +* re-enters) is not seen. * Such files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off * or @@ -1400,7 +1404,8 @@ INITIALIZE #else if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { hkl_info.shape = 3; - } + } else + exit (fprintf (stderr, "Single_crystal: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); #endif } else if (xwidth && yheight && zdepth) hkl_info.shape = 1; /* box */ @@ -1522,11 +1527,18 @@ TRACE #ifdef USE_OFF else if (hkl_info.shape == 3) { - gmesh_hit entering, leaving; - intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); + intersect = nhits > 0; if (intersect) { - t1 = intersect == 2 ? entering.time : 0; // 0 when inside - t2 = leaving.time; + if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { + t1 = 0; + t2 = hits[0].time; + } else { + // 1 ns past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) + t1 = hits[0].time + 1e-9; + t2 = nhits > 1 ? hits[1].time : hits[0].time; + } } } #endif @@ -1577,11 +1589,12 @@ TRACE #ifdef USE_OFF else if (hkl_info.shape == 3) { - gmesh_hit entering, leaving; - intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); + intersect = nhits > 0 && gmesh_inside (&thread_mesh, coords_set (x, y, z)); if (intersect) { - t1 = intersect == 2 ? entering.time : 0; // 0 when inside - t2 = leaving.time; + t1 = 0; + t2 = hits[0].time; } } #endif diff --git a/mcstas-comps/samples/Single_magnetic_crystal.comp b/mcstas-comps/samples/Single_magnetic_crystal.comp index 2d0c971977..e47ff41292 100644 --- a/mcstas-comps/samples/Single_magnetic_crystal.comp +++ b/mcstas-comps/samples/Single_magnetic_crystal.comp @@ -41,6 +41,10 @@ * It computes the intersection points of the neutron ray with the object * transparently, so that it can be used like a regular sample object. * It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. +* A mesh must be a single, closed object: the neutron path through it is +* taken from the first entry and exit only, so a second part of the mesh +* further along the path (disjoint parts, or a concave shape the path +* re-enters) is not seen. * Such files may be generated from XYZ data using qhull/powercrust, and * viewed with geomview * The default size of the object depends of the OFF file data, but its @@ -544,7 +548,8 @@ INITIALIZE if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { hkl_info.shape = 3; - } + } else + exit (fprintf (stderr, "Single_magnetic_crystal: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); } else if (xwidth && yheight && zdepth) hkl_info.shape = 1; /* box */ else if (radius > 0 && yheight) @@ -615,11 +620,18 @@ TRACE intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) { - gmesh_hit entering, leaving; - intersect = gmesh_entry_exit (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); + intersect = nhits > 0; if (intersect) { - t1 = intersect == 2 ? entering.time : 0; // 0 when inside - t2 = leaving.time; + if (gmesh_inside (&mesh, coords_set (x, y, z))) { + t1 = 0; + t2 = hits[0].time; + } else { + // 1 ns past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) + t1 = hits[0].time + 1e-9; + t2 = nhits > 1 ? hits[1].time : hits[0].time; + } } } @@ -650,11 +662,12 @@ TRACE intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) { - gmesh_hit entering, leaving; - intersect = gmesh_entry_exit (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &entering, &leaving); + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); + intersect = nhits > 0 && gmesh_inside (&mesh, coords_set (x, y, z)); if (intersect) { - t1 = intersect == 2 ? entering.time : 0; // 0 when inside - t2 = leaving.time; + t1 = 0; + t2 = hits[0].time; } } if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { diff --git a/mcstas-comps/share/monitor_nd-lib.c b/mcstas-comps/share/monitor_nd-lib.c index 5c8b713884..d690885c1f 100644 --- a/mcstas-comps/share/monitor_nd-lib.c +++ b/mcstas-comps/share/monitor_nd-lib.c @@ -2205,6 +2205,37 @@ void Monitor_nD_McDisplay(MonitornD_Defines_type *DEFS, /* defined by a mesh file (geometry parameter) */ /* ========================================================================= */ +// Hits of the line position + t*velocity with mesh ahead of the neutron +// (t > 0). Inside a closed mesh, t0 is 0 and t1 the first exit; otherwise +// t0 is the first hit and t1 the next one (t0 when there is a single hit). +// face is the face of the first hit ahead. Returns 1 on a hit, 0 otherwise. +#pragma acc routine seq +int Monitor_nD_MeshIntersect(gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t1, int* face) +{ + gmesh_hit hits[GMESH_MAX_HITS]; + double t_min = 0; + int nhits = gmesh_intersect(mesh, position, velocity, t_min, hits); + int inside = 0; + if (!nhits) + return 0; + // normals of a closed, oriented mesh point outward: leaving first means inside + if (mesh->closed && mesh->oriented) + inside = hits[0].in_out < 0; + else if (mesh->closed) + inside = gmesh_inside(mesh, position); + if (inside) { + *t0 = 0; + *t1 = hits[0].time; + } else { + *t0 = hits[0].time; + *t1 = hits[0].time; + if (nhits > 1) + *t1 = hits[1].time; + } + *face = hits[0].face; + return 1; +} + // Warns when mantid is combined with a closed mesh: pixel IDs are panels. void Monitor_nD_CheckMantidMesh(gmesh_struct* mesh, int flag_mantid, char* name) { diff --git a/mcstas-comps/share/monitor_nd-lib.h b/mcstas-comps/share/monitor_nd-lib.h index fc54e7b485..f4c332ecc6 100644 --- a/mcstas-comps/share/monitor_nd-lib.h +++ b/mcstas-comps/share/monitor_nd-lib.h @@ -220,6 +220,8 @@ void Monitor_nD_EnsureCapacity(MonitornD_Defines_type *, MonitornD_Variables_typ MCDETECTOR Monitor_nD_Save(MonitornD_Defines_type *, MonitornD_Variables_type *); void Monitor_nD_Finally(MonitornD_Defines_type *, MonitornD_Variables_type *); void Monitor_nD_McDisplay(MonitornD_Defines_type *, MonitornD_Variables_type *); +#pragma acc routine seq +int Monitor_nD_MeshIntersect(gmesh_struct*, Coords, Coords, double*, double*, int*); void Monitor_nD_MeshMantid(gmesh_struct*, long); void Monitor_nD_CheckMantidMesh(gmesh_struct*, int, char*); From 37da27fdd1d922fc81d985c6ef0cf4d33f2b58e4 Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Wed, 11 Nov 2026 13:11:02 +0100 Subject: [PATCH 14/22] geometry-mesh-lib: gmesh_inside without box padding; t_min = GMESH_TOL in components - gmesh_inside no longer pads the bounding box, and returns 0 for an open mesh, which encloses no volume. - Monitor_nD_MeshIntersect uses gmesh_inside for the inside test. - The components pass t_min = GMESH_TOL to gmesh_intersect. - Redundant braces in the mesh code of the components are removed. Co-Authored-By: Claude Opus 5.5 --- common/lib/share/geometry-mesh-lib.c | 7 +-- common/lib/share/geometry-mesh-lib.h | 17 ++++--- mcstas-comps/contrib/Lens.comp | 6 +-- .../contrib/Single_crystal_inelastic.comp | 48 +++++++++--------- mcstas-comps/misc/Shape.comp | 4 +- mcstas-comps/misc/Shape_simple.comp | 4 +- mcstas-comps/optics/Refractor.comp | 2 +- mcstas-comps/samples/Incoherent.comp | 42 ++++++++-------- mcstas-comps/samples/Isotropic_Sqw.comp | 2 +- mcstas-comps/samples/PowderN.comp | 50 ++++++++----------- mcstas-comps/samples/Single_crystal.comp | 48 +++++++++--------- .../samples/Single_magnetic_crystal.comp | 48 +++++++++--------- mcstas-comps/share/monitor_nd-lib.c | 8 +-- 13 files changed, 134 insertions(+), 152 deletions(-) diff --git a/common/lib/share/geometry-mesh-lib.c b/common/lib/share/geometry-mesh-lib.c index b1bb9f0265..21f0b0146e 100644 --- a/common/lib/share/geometry-mesh-lib.c +++ b/common/lib/share/geometry-mesh-lib.c @@ -2318,11 +2318,12 @@ int gmesh_inside (gmesh_struct* mesh, Coords position) { // fixed, "irrational" directions: no random numbers are consumed const double D[3][3] = { { 0.4719, 0.6213, 0.6254 }, { 0.8017, -0.3259, 0.5012 }, { -0.2263, 0.9147, -0.3349 } }; - Coords pad = coords_set (GMESH_TOL, GMESH_TOL, GMESH_TOL); int j; int votes = 0; - // the box is padded by GMESH_TOL, for a point on the surface after rounding - if (!gmesh_in_box (position, coords_sub (mesh->bbmin, pad), coords_add (mesh->bbmax, pad))) + // an open mesh encloses no volume + if (!mesh->closed) + return 0; + if (!gmesh_in_box (position, mesh->bbmin, mesh->bbmax)) return 0; // a closed, oriented mesh has outward normals: the point is inside when the // first hit along a ray is leaving. Hits closer than GMESH_TOL are skipped, diff --git a/common/lib/share/geometry-mesh-lib.h b/common/lib/share/geometry-mesh-lib.h index dd990da9e4..e29313474d 100644 --- a/common/lib/share/geometry-mesh-lib.h +++ b/common/lib/share/geometry-mesh-lib.h @@ -81,10 +81,11 @@ #ifndef GMESH_MAX_HITS #define GMESH_MAX_HITS 50 #endif - // distance [m] within which a hit is the surface the particle sits on, to be - // skipped: gmesh_inside skips it, and components pass t_min = GMESH_TOL/|v| - // to gmesh_intersect. A neutron is larger than this, so the ray picture does - // not hold below it anyway. To change it from an instrument, add + // tolerance for hits on the surface a particle sits on, to be skipped: + // gmesh_inside skips hits closer than GMESH_TOL [m] along its ray, and + // components pass t_min = GMESH_TOL [s] to gmesh_intersect. A neutron is + // larger than this, so the ray picture does not hold below it anyway. To + // change it from an instrument, add // DEPENDENCY " -DGMESH_TOL=1e-10 " after its DEFINE INSTRUMENT line #ifndef GMESH_TOL #define GMESH_TOL 1e-12 @@ -192,10 +193,10 @@ int gmesh_intersect (gmesh_struct* mesh, Coords position, Coords direction, doub int gmesh_intersect_gravity (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, double t_min, gmesh_hit hits[GMESH_MAX_HITS]); // int gmesh_inside(gmesh_struct *mesh, Coords position) - // ACTION: point in (closed) mesh test. For a closed, oriented mesh: whether - // the first hit along a fixed direction, beyond GMESH_TOL, is leaving; - // otherwise the parity of ray crossings along fixed directions. Does not use - // random numbers. + // ACTION: point in closed mesh test; 0 for an open mesh. For a closed, + // oriented mesh: whether the first hit along a fixed direction, beyond + // GMESH_TOL, is leaving; otherwise the parity of ray crossings along fixed + // directions. Does not use random numbers. // RETURN: 1 inside, 0 outside #pragma acc routine seq int gmesh_inside (gmesh_struct* mesh, Coords position); diff --git a/mcstas-comps/contrib/Lens.comp b/mcstas-comps/contrib/Lens.comp index 1efd60dcef..f960708b3c 100644 --- a/mcstas-comps/contrib/Lens.comp +++ b/mcstas-comps/contrib/Lens.comp @@ -280,7 +280,7 @@ TRACE #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { gmesh_hit hits[GMESH_MAX_HITS]; - if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits)) { + if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits)) { dt = hits[0].time; // normal oriented as in interoff-lib: opposite to the right-hand rule of // the vertex order in the file, unless the mesh was flipped @@ -377,7 +377,7 @@ TRACE #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { gmesh_hit hits[GMESH_MAX_HITS]; - if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits)) { + if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits)) { dt = hits[0].time; // normal oriented as in interoff-lib: opposite to the right-hand rule of // the vertex order in the file, unless the mesh was flipped @@ -487,7 +487,7 @@ TRACE #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { gmesh_hit hits[GMESH_MAX_HITS]; - if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits)) { + if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits)) { dt = hits[0].time; // normal oriented as in interoff-lib: opposite to the right-hand rule of // the vertex order in the file, unless the mesh was flipped diff --git a/mcstas-comps/contrib/Single_crystal_inelastic.comp b/mcstas-comps/contrib/Single_crystal_inelastic.comp index 85482154b9..ab53a69331 100644 --- a/mcstas-comps/contrib/Single_crystal_inelastic.comp +++ b/mcstas-comps/contrib/Single_crystal_inelastic.comp @@ -717,9 +717,9 @@ INITIALIZE hkl_info.shape = -1; /* -1:no shape, 0:cyl, 1:box, 2:sphere, 3:any-shape */ if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) hkl_info.shape = 3; - } else + else exit (fprintf (stderr, "Single_crystal_inelastic: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); } else if (xwidth && yheight && zdepth) hkl_info.shape = 1; /* box */ @@ -800,22 +800,21 @@ TRACE intersect = box_intersect (&t1, &t2, x, y, z, vx, vy, vz, xwidth, yheight, zdepth); else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); - else if (hkl_info.shape == 3) - { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); - intersect = nhits > 0; - if (intersect) { - if (gmesh_inside (&mesh, coords_set (x, y, z))) { - t1 = 0; - t2 = hits[0].time; - } else { - // 1 ns past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) - t1 = hits[0].time + 1e-9; - t2 = nhits > 1 ? hits[1].time : hits[0].time; - } + else if (hkl_info.shape == 3) { + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + intersect = nhits > 0; + if (intersect) { + if (gmesh_inside (&mesh, coords_set (x, y, z))) { + t1 = 0; + t2 = hits[0].time; + } else { + // 1 ns past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) + t1 = hits[0].time + 1e-9; + t2 = nhits > 1 ? hits[1].time : hits[0].time; } } + } if (t2 < 0) intersect = 0; /* we passed sample volume already */ @@ -848,16 +847,15 @@ TRACE intersect = box_intersect (&t1, &t2, x, y, z, vx, vy, vz, xwidth, yheight, zdepth); else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); - else if (hkl_info.shape == 3) - { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); - intersect = nhits > 0 && gmesh_inside (&mesh, coords_set (x, y, z)); - if (intersect) { - t1 = 0; - t2 = hits[0].time; - } + else if (hkl_info.shape == 3) { + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + intersect = nhits > 0 && gmesh_inside (&mesh, coords_set (x, y, z)); + if (intersect) { + t1 = 0; + t2 = hits[0].time; } + } if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { /* neutron is leaving the sample */ if (hkl_info.flag_warning < 100) diff --git a/mcstas-comps/misc/Shape.comp b/mcstas-comps/misc/Shape.comp index fdd3d90afb..bea2af36f8 100644 --- a/mcstas-comps/misc/Shape.comp +++ b/mcstas-comps/misc/Shape.comp @@ -80,9 +80,9 @@ DECLARE INITIALIZE %{ if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_IGNORE, &mesh)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_IGNORE, &mesh)) thickness = 0; - } else + else exit (fprintf (stderr, "Shape: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); } %} diff --git a/mcstas-comps/misc/Shape_simple.comp b/mcstas-comps/misc/Shape_simple.comp index f0eaabb118..bf485e1137 100644 --- a/mcstas-comps/misc/Shape_simple.comp +++ b/mcstas-comps/misc/Shape_simple.comp @@ -78,9 +78,9 @@ DECLARE INITIALIZE %{ if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_IGNORE, &mesh)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_IGNORE, &mesh)) thickness = 0; - } else + else exit (fprintf (stderr, "Shape_simple: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); } %} diff --git a/mcstas-comps/optics/Refractor.comp b/mcstas-comps/optics/Refractor.comp index c6f90bd652..3ba3990c5d 100644 --- a/mcstas-comps/optics/Refractor.comp +++ b/mcstas-comps/optics/Refractor.comp @@ -411,7 +411,7 @@ TRACE #ifdef USE_OFF else if (geometry && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); intersect = nhits > 0; if (intersect) { mesh_inside = gmesh_inside (&thread_mesh, coords_set (x, y, z)); diff --git a/mcstas-comps/samples/Incoherent.comp b/mcstas-comps/samples/Incoherent.comp index be87a498a3..c46b5a7b23 100644 --- a/mcstas-comps/samples/Incoherent.comp +++ b/mcstas-comps/samples/Incoherent.comp @@ -283,21 +283,20 @@ TRACE else if (VarsInc.shape == 2) intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF - else if (VarsInc.shape == 3) - { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); - intersect = nhits > 0; - if (intersect) { - if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { - t0 = 0; - t3 = hits[0].time; - } else { - t0 = hits[0].time; - t3 = nhits > 1 ? hits[1].time : hits[0].time; - } + else if (VarsInc.shape == 3) { + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + intersect = nhits > 0; + if (intersect) { + if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { + t0 = 0; + t3 = hits[0].time; + } else { + t0 = hits[0].time; + t3 = nhits > 1 ? hits[1].time : hits[0].time; } } + } #endif if (intersect) { @@ -493,16 +492,15 @@ TRACE else if (VarsInc.shape == 2) intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF - else if (VarsInc.shape == 3) - { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); - intersect = nhits > 0; - if (intersect) { - t0 = 0; - t3 = hits[0].time; - } + else if (VarsInc.shape == 3) { + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + intersect = nhits > 0; + if (intersect) { + t0 = 0; + t3 = hits[0].time; } + } #endif flag_ishollow = 0; diff --git a/mcstas-comps/samples/Isotropic_Sqw.comp b/mcstas-comps/samples/Isotropic_Sqw.comp index 7c887640cf..eb867fdaa4 100644 --- a/mcstas-comps/samples/Isotropic_Sqw.comp +++ b/mcstas-comps/samples/Isotropic_Sqw.comp @@ -2256,7 +2256,7 @@ TRACE // a mesh is used as given, for either sign of thickness if (VarSqw.shape == 3) { gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); intersect = nhits > 0; if (intersect) { if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { diff --git a/mcstas-comps/samples/PowderN.comp b/mcstas-comps/samples/PowderN.comp index 95c252be5b..d38962f8f4 100644 --- a/mcstas-comps/samples/PowderN.comp +++ b/mcstas-comps/samples/PowderN.comp @@ -1120,18 +1120,16 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); - intersect = nhits > 0; - if (intersect) { - if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { - t0 = 0; - t3 = hits[0].time; - } else { - t0 = hits[0].time; - t3 = nhits > 1 ? hits[1].time : hits[0].time; - } + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + intersect = nhits > 0; + if (intersect) { + if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { + t0 = 0; + t3 = hits[0].time; + } else { + t0 = hits[0].time; + t3 = nhits > 1 ? hits[1].time : hits[0].time; } } intersecti = 0; @@ -1328,14 +1326,12 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); - intersect = nhits > 0; - if (intersect) { - t0 = 0; - t3 = hits[0].time; - } + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + intersect = nhits > 0; + if (intersect) { + t0 = 0; + t3 = hits[0].time; } intersecti = 0; } @@ -1410,14 +1406,12 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); - intersect = nhits > 0; - if (intersect) { - t0 = 0; - t3 = hits[0].time; - } + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + intersect = nhits > 0; + if (intersect) { + t0 = 0; + t3 = hits[0].time; } intersecti = 0; } diff --git a/mcstas-comps/samples/Single_crystal.comp b/mcstas-comps/samples/Single_crystal.comp index 4530080e20..758136698b 100644 --- a/mcstas-comps/samples/Single_crystal.comp +++ b/mcstas-comps/samples/Single_crystal.comp @@ -1402,9 +1402,9 @@ INITIALIZE fprintf (stderr, "Error: You are attempting to use an OFF geometry without -DUSE_OFF. You will need to recompile with that define set!\n"); exit (-1); #else - if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) hkl_info.shape = 3; - } else + else exit (fprintf (stderr, "Single_crystal: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); #endif } else if (xwidth && yheight && zdepth) @@ -1525,22 +1525,21 @@ TRACE else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF - else if (hkl_info.shape == 3) - { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); - intersect = nhits > 0; - if (intersect) { - if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { - t1 = 0; - t2 = hits[0].time; - } else { - // 1 ns past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) - t1 = hits[0].time + 1e-9; - t2 = nhits > 1 ? hits[1].time : hits[0].time; - } + else if (hkl_info.shape == 3) { + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + intersect = nhits > 0; + if (intersect) { + if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { + t1 = 0; + t2 = hits[0].time; + } else { + // 1 ns past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) + t1 = hits[0].time + 1e-9; + t2 = nhits > 1 ? hits[1].time : hits[0].time; } } + } #endif if (t2 < 0) intersect = 0; /* we passed sample volume already */ @@ -1587,16 +1586,15 @@ TRACE else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF - else if (hkl_info.shape == 3) - { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); - intersect = nhits > 0 && gmesh_inside (&thread_mesh, coords_set (x, y, z)); - if (intersect) { - t1 = 0; - t2 = hits[0].time; - } + else if (hkl_info.shape == 3) { + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + intersect = nhits > 0 && gmesh_inside (&thread_mesh, coords_set (x, y, z)); + if (intersect) { + t1 = 0; + t2 = hits[0].time; } + } #endif if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { /* neutron is leaving the sample */ diff --git a/mcstas-comps/samples/Single_magnetic_crystal.comp b/mcstas-comps/samples/Single_magnetic_crystal.comp index e47ff41292..1106d65a40 100644 --- a/mcstas-comps/samples/Single_magnetic_crystal.comp +++ b/mcstas-comps/samples/Single_magnetic_crystal.comp @@ -546,9 +546,9 @@ INITIALIZE hkl_info.shape = -1; /* -1:no shape, 0:cyl, 1:box, 2:sphere, 3:any-shape */ if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) hkl_info.shape = 3; - } else + else exit (fprintf (stderr, "Single_magnetic_crystal: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); } else if (xwidth && yheight && zdepth) hkl_info.shape = 1; /* box */ @@ -618,22 +618,21 @@ TRACE intersect = box_intersect (&t1, &t2, x, y, z, vx, vy, vz, xwidth, yheight, zdepth); else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); - else if (hkl_info.shape == 3) - { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); - intersect = nhits > 0; - if (intersect) { - if (gmesh_inside (&mesh, coords_set (x, y, z))) { - t1 = 0; - t2 = hits[0].time; - } else { - // 1 ns past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) - t1 = hits[0].time + 1e-9; - t2 = nhits > 1 ? hits[1].time : hits[0].time; - } + else if (hkl_info.shape == 3) { + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + intersect = nhits > 0; + if (intersect) { + if (gmesh_inside (&mesh, coords_set (x, y, z))) { + t1 = 0; + t2 = hits[0].time; + } else { + // 1 ns past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) + t1 = hits[0].time + 1e-9; + t2 = nhits > 1 ? hits[1].time : hits[0].time; } } + } if (t2 < 0) intersect = 0; /* we passed sample volume already */ @@ -660,16 +659,15 @@ TRACE intersect = box_intersect (&t1, &t2, x, y, z, vx, vy, vz, xwidth, yheight, zdepth); else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); - else if (hkl_info.shape == 3) - { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL / sqrt (vx * vx + vy * vy + vz * vz), hits); - intersect = nhits > 0 && gmesh_inside (&mesh, coords_set (x, y, z)); - if (intersect) { - t1 = 0; - t2 = hits[0].time; - } + else if (hkl_info.shape == 3) { + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + intersect = nhits > 0 && gmesh_inside (&mesh, coords_set (x, y, z)); + if (intersect) { + t1 = 0; + t2 = hits[0].time; } + } if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { /* neutron is leaving the sample */ if (hkl_info.flag_warning < 100) diff --git a/mcstas-comps/share/monitor_nd-lib.c b/mcstas-comps/share/monitor_nd-lib.c index d690885c1f..7764148c80 100644 --- a/mcstas-comps/share/monitor_nd-lib.c +++ b/mcstas-comps/share/monitor_nd-lib.c @@ -2215,15 +2215,9 @@ int Monitor_nD_MeshIntersect(gmesh_struct* mesh, Coords position, Coords velocit gmesh_hit hits[GMESH_MAX_HITS]; double t_min = 0; int nhits = gmesh_intersect(mesh, position, velocity, t_min, hits); - int inside = 0; if (!nhits) return 0; - // normals of a closed, oriented mesh point outward: leaving first means inside - if (mesh->closed && mesh->oriented) - inside = hits[0].in_out < 0; - else if (mesh->closed) - inside = gmesh_inside(mesh, position); - if (inside) { + if (gmesh_inside(mesh, position)) { *t0 = 0; *t1 = hits[0].time; } else { From 9ed95baf063448c2fcb3cd7790a15906f9bdd9ab Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Wed, 11 Nov 2026 13:51:18 +0100 Subject: [PATCH 15/22] geometry-mesh-lib: optional closed check gmesh_check_closed with the fold check The fold check moves out of gmesh_init into gmesh_check_closed. That optional check passes a mesh that is closed and does not fold back onto itself, and prints the reason otherwise. A folded mesh gets closed = 0, so afterwards mesh->closed holds the result. gmesh_init no longer rejects folded meshes, so open or folded surfaces can still be used where no volume is needed (Shape, Guide_anyshape, Monitor_nD). Incoherent, PowderN, Isotropic_Sqw, Single_crystal, Single_magnetic_crystal, Single_crystal_inelastic, Refractor, Lens and Union_mesh (McStas and McXtrace) call it and abort for a mesh that does not pass. The libtest instrument checks it on cube.off, socket.off, the open Guide.off and a folded cube. Co-Authored-By: Claude Opus 5.5 --- common/lib/share/geometry-mesh-lib.c | 70 ++++++++++++------- common/lib/share/geometry-mesh-lib.h | 21 ++++-- mcstas-comps/contrib/Lens.comp | 2 + .../contrib/Single_crystal_inelastic.comp | 2 + .../Test_lib_geometry_mesh_lib.instr | 16 ++++- mcstas-comps/optics/Refractor.comp | 2 + mcstas-comps/samples/Incoherent.comp | 2 + mcstas-comps/samples/Isotropic_Sqw.comp | 2 + mcstas-comps/samples/PowderN.comp | 2 + mcstas-comps/samples/Single_crystal.comp | 2 + .../samples/Single_magnetic_crystal.comp | 2 + mcstas-comps/union/Union_mesh.comp | 6 +- mcxtrace-comps/union/Union_mesh.comp | 6 +- 13 files changed, 94 insertions(+), 41 deletions(-) diff --git a/common/lib/share/geometry-mesh-lib.c b/common/lib/share/geometry-mesh-lib.c index 21f0b0146e..f2c15ebff1 100644 --- a/common/lib/share/geometry-mesh-lib.c +++ b/common/lib/share/geometry-mesh-lib.c @@ -1410,7 +1410,6 @@ gmesh_load (char* filename, double scale, double xwidth, double yheight, double int ok = 0; int i, k; int* map; - int folded_face[2] = { -1, -1 }; if (!mesh || !filename) return 0; @@ -1723,20 +1722,6 @@ gmesh_load (char* filename, double scale, double xwidth, double yheight, double } else if (edge->dir != 0) { mesh->oriented = 0; } - // folded: the two triangles of the edge turn back onto each other, i.e. - // their normals are antiparallel once oriented alike (dir 0) - if (edge->n == 2) { - double turn = coords_sp (mesh->tri_normal[edge->t0], mesh->tri_normal[edge->t1]); - if (edge->dir != 0) - turn = -turn; - if (turn < -1 + GMESH_EPSILON) { - if (!mesh->nfolded) { - folded_face[0] = mesh->tri_face[edge->t0]; - folded_face[1] = mesh->tri_face[edge->t1]; - } - mesh->nfolded++; - } - } } if (mesh->nnonmanifold) mesh->oriented = 0; @@ -1754,16 +1739,6 @@ gmesh_load (char* filename, double scale, double xwidth, double yheight, double free (used); } - if (mesh->nfolded) { - fprintf (stderr, - "Error: %s: %d edges where the surface folds back onto itself, e.g. between faces %d and %d (0-based): " - "the mesh overlaps itself, so inside and outside are not defined. Fix the vertex order of the folded faces " - "(geometry-mesh-lib)\n", - filename, mesh->nfolded, folded_face[0], folded_face[1]); - gmesh_free (mesh); - return 0; - } - // volume (divergence theorem); outward normals for closed oriented meshes { double volume = 0; @@ -2352,6 +2327,50 @@ gmesh_inside (gmesh_struct* mesh, Coords position) { return votes >= 2; } + // closed check, see geometry-mesh-lib.h +int +gmesh_check_closed (gmesh_struct* mesh) { + gmesh_etab edges; + int i; + int nfolded = 0; + int folded_face[2] = { -1, -1 }; + if (!mesh->closed) { + fprintf (stderr, "Error: %s: %d boundary and %d non-manifold edges: the mesh is not closed (geometry-mesh-lib)\n", mesh->filename, mesh->nboundary, + mesh->nnonmanifold); + return 0; + } + // folded edges: the two triangles of the edge turn back onto each other, + // i.e. their normals are antiparallel once oriented alike (dir 0) + gmesh_etab_build (&edges, mesh->tri, mesh->ntris); + for (i = 0; i < edges.cap; i++) { + gmesh_edge* edge = &edges.e[i]; + double turn; + if (edge->n != 2) + continue; + turn = coords_sp (mesh->tri_normal[edge->t0], mesh->tri_normal[edge->t1]); + if (edge->dir != 0) + turn = -turn; + if (turn < -1 + GMESH_EPSILON) { + if (!nfolded) { + folded_face[0] = mesh->tri_face[edge->t0]; + folded_face[1] = mesh->tri_face[edge->t1]; + } + nfolded++; + } + } + free (edges.e); + if (nfolded) { + fprintf (stderr, + "Error: %s: %d edges where the surface folds back onto itself, e.g. between faces %d and %d (0-based): " + "the mesh overlaps itself, so inside and outside are not defined (geometry-mesh-lib)\n", + mesh->filename, nfolded, folded_face[0], folded_face[1]); + // a folded mesh encloses no well-defined volume + mesh->closed = 0; + return 0; + } + return 1; +} + // per-face property, see geometry-mesh-lib.h #pragma acc routine seq double @@ -2386,6 +2405,7 @@ gmesh_print_info (gmesh_struct* mesh) { if (!mesh->closed) { printf (" Warning: %d boundary and %d non-manifold edges: the mesh does not enclose a volume.\n", mesh->nboundary, mesh->nnonmanifold); } + if (mesh->nfaceprops) printf (" Per-face properties: %d\n", mesh->nfaceprops); } diff --git a/common/lib/share/geometry-mesh-lib.h b/common/lib/share/geometry-mesh-lib.h index e29313474d..0a11d4eede 100644 --- a/common/lib/share/geometry-mesh-lib.h +++ b/common/lib/share/geometry-mesh-lib.h @@ -35,9 +35,6 @@ * boundary / non-manifold edges, Euler characteristic, consistent * orientation. Closed, consistently oriented meshes are oriented with * outward normals. - * - rejects a folded mesh (gmesh_init fails): one where the two triangles - * of an edge turn back onto each other (antiparallel normals), so the - * surface overlaps itself and inside and outside are not defined * - builds a bounding volume hierarchy (BVH) so that a ray costs * O(log N) instead of O(N) triangle tests, without any allocation per ray. * @@ -46,6 +43,7 @@ * straight line, sorted in t * gmesh_intersect_gravity() the same for a parabola (e.g. under gravity) * gmesh_inside() point-in-mesh test (deterministic, no random numbers) + * gmesh_check_closed() optional check that a mesh is closed and not folded * Hits exactly on an edge or vertex shared by several triangles are * reported once. * @@ -138,9 +136,8 @@ typedef struct gmesh_struct { double area, volume; // surface area, enclosed volume (closed meshes) int nedges, nboundary, nnonmanifold, ndegenerate, nwelded; int nduplicate; // faces given more than once (all copies removed) - int nfolded; // edges where the surface folds back onto itself (gmesh_init then fails) int euler; // V - E + F (2 for a closed genus-0 surface) - int closed; // every edge shared by exactly two triangles + int closed; // every edge shared by exactly two triangles; after gmesh_check_closed, also not folded int oriented; // shared edges run in opposite directions int flipped; // orientation was reversed to make normals point outward char* filename; @@ -157,7 +154,7 @@ typedef struct gmesh_struct { // ACTION: read an STL/OFF/PLY file and build the mesh structure. // INPUT: scale: multiply file coordinates (1 for m, 1e-3 for mm, 0 same as 1) // face_props: GMESH_PROPS_IGNORE, GMESH_PROPS_OPTIONAL or GMESH_PROPS_REQUIRED -// RETURN: number of triangles, 0 on error (also for a folded mesh) +// RETURN: number of triangles, 0 on error int gmesh_init (char* filename, double scale, int face_props, gmesh_struct* mesh); // int gmesh_init_fit(char *filename, double xwidth, double yheight, double zdepth, @@ -166,7 +163,7 @@ int gmesh_init (char* filename, double scale, int face_props, gmesh_struct* mesh // non-zero sizes set the box size along x/y/z; a single size scales all // axes by the same factor (proportions kept). With center, the box centre // is moved to the origin; otherwise coordinates are scaled about the origin. -// RETURN: number of triangles, 0 on error (also for a folded mesh) +// RETURN: number of triangles, 0 on error int gmesh_init_fit (char* filename, double xwidth, double yheight, double zdepth, int center, int face_props, gmesh_struct* mesh); // int gmesh_intersect(gmesh_struct *mesh, Coords position, Coords direction, @@ -201,6 +198,16 @@ int gmesh_intersect_gravity (gmesh_struct* mesh, Coords position, Coords velocit #pragma acc routine seq int gmesh_inside (gmesh_struct* mesh, Coords position); + // int gmesh_check_closed(gmesh_struct *mesh) + // ACTION: optional check for components that need a volume: the mesh must be + // closed (every edge shared by two triangles) and must not fold back onto + // itself (no edge whose two triangles turn back onto each other, with + // antiparallel normals), as then inside and outside are not defined. + // Prints the reason when the mesh does not pass. A folded mesh gets + // mesh->closed = 0, so that afterwards mesh->closed holds the result. + // RETURN: 1 when the mesh passes, 0 otherwise +int gmesh_check_closed (gmesh_struct* mesh); + // per-face property number 'property' of face 'face', or fallback when it is // not given in the file #pragma acc routine seq diff --git a/mcstas-comps/contrib/Lens.comp b/mcstas-comps/contrib/Lens.comp index f960708b3c..e199c3e9d3 100644 --- a/mcstas-comps/contrib/Lens.comp +++ b/mcstas-comps/contrib/Lens.comp @@ -241,6 +241,8 @@ INITIALIZE /* init the OFF/PLY without centering and scaling */ if (!gmesh_init_fit (geometry, 0, 0, 0, 0, GMESH_PROPS_IGNORE, &mesh)) exit (printf ("Lens: %s: FATAL: could not initialize geometry file %s [OFF/PLY]\n", NAME_CURRENT_COMP, geometry)); + if (!gmesh_check_closed (&mesh)) + exit (fprintf (stderr, "Lens: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); #endif } diff --git a/mcstas-comps/contrib/Single_crystal_inelastic.comp b/mcstas-comps/contrib/Single_crystal_inelastic.comp index ab53a69331..c0999dbe12 100644 --- a/mcstas-comps/contrib/Single_crystal_inelastic.comp +++ b/mcstas-comps/contrib/Single_crystal_inelastic.comp @@ -721,6 +721,8 @@ INITIALIZE hkl_info.shape = 3; else exit (fprintf (stderr, "Single_crystal_inelastic: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); + if (!gmesh_check_closed (&mesh)) + exit (fprintf (stderr, "Single_crystal_inelastic: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); } else if (xwidth && yheight && zdepth) hkl_info.shape = 1; /* box */ else if (radius > 0 && yheight) diff --git a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr index e481bbf53c..b46961213d 100644 --- a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr +++ b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr @@ -8,7 +8,8 @@ * Fails to compile or link if the library does not build with the runtime. * INITIALIZE also checks gmesh_inside and gmesh_intersect_gravity, and * exits with an error when a check fails: -* - gmesh_init rejects a folded mesh +* - gmesh_check_closed passes cube.off and socket.off, and fails the open +* Guide.off and a folded (closed, oriented) cube * - gmesh_inside on cube.off, and its shortcut for closed, oriented meshes * against the parity of ray crossings for random points around socket.off * - with zero acceleration it gives the hits of gmesh_intersect, to rounding @@ -61,7 +62,7 @@ int inside_tests (void) { mesh_check (gmesh_inside (&cube, coords_set (0.5, 0.5, 0.5)) == 1, "inside the cube", &failures); // a folded mesh: a unit cube whose bottom reaches past the edge x = 1 to // (1.5, 0.5, 0), where triangle 2 1 8 folds back. It is closed and - // consistently oriented, but overlaps itself, so gmesh_init fails. + // consistently oriented, but overlaps itself at 2 edges. { gmesh_struct folded; FILE* file = fopen ("Test_lib_geometry_mesh_lib_folded.off", "w"); @@ -69,7 +70,9 @@ int inside_tests (void) { fprintf (file, "OFF\n9 9 0\n0 0 0\n1 0 0\n1 1 0\n0 1 0\n0 0 1\n1 0 1\n1 1 1\n0 1 1\n1.5 0.5 0\n" "4 4 5 6 7\n4 0 1 5 4\n4 1 2 6 5\n4 2 3 7 6\n4 0 4 7 3\n3 1 0 8\n3 0 3 8\n3 3 2 8\n3 2 1 8\n"); fclose (file); - mesh_check (gmesh_init ("Test_lib_geometry_mesh_lib_folded.off", 1, GMESH_PROPS_IGNORE, &folded) == 0, "folded mesh rejected", &failures); + mesh_check (gmesh_init ("Test_lib_geometry_mesh_lib_folded.off", 1, GMESH_PROPS_IGNORE, &folded) && folded.closed && folded.oriented + && !gmesh_check_closed (&folded) && !folded.closed, "folded mesh fails gmesh_check_closed and is then not closed", &failures); + gmesh_free (&folded); remove ("Test_lib_geometry_mesh_lib_folded.off"); } } @@ -85,6 +88,13 @@ int inside_tests (void) { } printf ("Test_lib_geometry_mesh_lib: socket.off: gmesh_inside shortcut and parity differ for %d of 2000 points\n", differ); mesh_check (socket.closed && socket.oriented && differ == 0, "gmesh_inside shortcut against parity on socket.off", &failures); + mesh_check (gmesh_check_closed (&cube) && gmesh_check_closed (&socket), "gmesh_check_closed on cube.off and socket.off", &failures); + { + gmesh_struct guide; + mesh_check (gmesh_init ("Guide.off", 1, GMESH_PROPS_IGNORE, &guide) && !guide.closed && !gmesh_check_closed (&guide), "gmesh_check_closed on the open Guide.off", + &failures); + gmesh_free (&guide); + } gmesh_free (&cube); gmesh_free (&socket); return failures; diff --git a/mcstas-comps/optics/Refractor.comp b/mcstas-comps/optics/Refractor.comp index 3ba3990c5d..a049671fe2 100644 --- a/mcstas-comps/optics/Refractor.comp +++ b/mcstas-comps/optics/Refractor.comp @@ -350,6 +350,8 @@ INITIALIZE /* off/ply case */ if (!gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) exit (printf ("Refractor: %s: FATAL: invalid OFF/PLY geometry specification for file '%s'.\n", NAME_CURRENT_COMP, geometry)); + if (!gmesh_check_closed (&mesh)) + exit (fprintf (stderr, "Refractor: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); #endif } else exit (printf ("Refractor: %s: FATAL: invalid geometry specification.\n" diff --git a/mcstas-comps/samples/Incoherent.comp b/mcstas-comps/samples/Incoherent.comp index c46b5a7b23..709e174f91 100644 --- a/mcstas-comps/samples/Incoherent.comp +++ b/mcstas-comps/samples/Incoherent.comp @@ -165,6 +165,8 @@ INITIALIZE concentric = 0; } else exit (fprintf (stderr, "Incoherent: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); + if (!gmesh_check_closed (&mesh)) + exit (fprintf (stderr, "Incoherent: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); #endif } else if (xwidth && yheight && zdepth) VarsInc.shape = 1; /* box */ diff --git a/mcstas-comps/samples/Isotropic_Sqw.comp b/mcstas-comps/samples/Isotropic_Sqw.comp index eb867fdaa4..29c3eac252 100644 --- a/mcstas-comps/samples/Isotropic_Sqw.comp +++ b/mcstas-comps/samples/Isotropic_Sqw.comp @@ -2039,6 +2039,8 @@ INITIALIZE concentric = 0; } else exit (fprintf (stderr, "Isotropic_Sqw: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); + if (!gmesh_check_closed (&mesh)) + exit (fprintf (stderr, "Isotropic_Sqw: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); #endif } else if (xwidth && yheight && zdepth) VarSqw.shape = 1; /* box */ diff --git a/mcstas-comps/samples/PowderN.comp b/mcstas-comps/samples/PowderN.comp index d38962f8f4..c92af17187 100644 --- a/mcstas-comps/samples/PowderN.comp +++ b/mcstas-comps/samples/PowderN.comp @@ -886,6 +886,8 @@ INITIALIZE concentric = 0; } else exit (fprintf (stderr, "PowderN: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); + if (!gmesh_check_closed (&mesh)) + exit (fprintf (stderr, "PowderN: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); #endif } else if (xwidth && yheight && zdepth) line_info.shape = 1; /* box */ diff --git a/mcstas-comps/samples/Single_crystal.comp b/mcstas-comps/samples/Single_crystal.comp index 758136698b..8389a28460 100644 --- a/mcstas-comps/samples/Single_crystal.comp +++ b/mcstas-comps/samples/Single_crystal.comp @@ -1406,6 +1406,8 @@ INITIALIZE hkl_info.shape = 3; else exit (fprintf (stderr, "Single_crystal: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); + if (!gmesh_check_closed (&mesh)) + exit (fprintf (stderr, "Single_crystal: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); #endif } else if (xwidth && yheight && zdepth) hkl_info.shape = 1; /* box */ diff --git a/mcstas-comps/samples/Single_magnetic_crystal.comp b/mcstas-comps/samples/Single_magnetic_crystal.comp index 1106d65a40..da168afb69 100644 --- a/mcstas-comps/samples/Single_magnetic_crystal.comp +++ b/mcstas-comps/samples/Single_magnetic_crystal.comp @@ -550,6 +550,8 @@ INITIALIZE hkl_info.shape = 3; else exit (fprintf (stderr, "Single_magnetic_crystal: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); + if (!gmesh_check_closed (&mesh)) + exit (fprintf (stderr, "Single_magnetic_crystal: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); } else if (xwidth && yheight && zdepth) hkl_info.shape = 1; /* box */ else if (radius > 0 && yheight) diff --git a/mcstas-comps/union/Union_mesh.comp b/mcstas-comps/union/Union_mesh.comp index 903150111a..d4654f7f34 100644 --- a/mcstas-comps/union/Union_mesh.comp +++ b/mcstas-comps/union/Union_mesh.comp @@ -444,10 +444,10 @@ INITIALIZE printf ("\nERROR IN %s: could not read the geometry file %s\n", NAME_CURRENT_COMP, filename); exit (1); } - if (!this_mesh_storage.mesh.closed) { - printf ("\n\nERROR: Input mesh %s of %s is not closed: %d boundary and %d non-manifold edges." + if (!gmesh_check_closed (&this_mesh_storage.mesh)) { + printf ("\n\nERROR: Input mesh %s of %s is not closed or folds back onto itself (see the error above)." "\nUnion_mesh requires a closed (watertight) mesh.\n\n", - filename, NAME_CURRENT_COMP, this_mesh_storage.mesh.nboundary, this_mesh_storage.mesh.nnonmanifold); + filename, NAME_CURRENT_COMP); exit (1); } diff --git a/mcxtrace-comps/union/Union_mesh.comp b/mcxtrace-comps/union/Union_mesh.comp index 3059010e74..e8d037b1ed 100644 --- a/mcxtrace-comps/union/Union_mesh.comp +++ b/mcxtrace-comps/union/Union_mesh.comp @@ -435,10 +435,10 @@ INITIALIZE printf ("\nERROR IN %s: could not read the geometry file %s\n", NAME_CURRENT_COMP, filename); exit (1); } - if (!this_mesh_storage.mesh.closed) { - printf ("\n\nERROR: Input mesh %s of %s is not closed: %d boundary and %d non-manifold edges." + if (!gmesh_check_closed (&this_mesh_storage.mesh)) { + printf ("\n\nERROR: Input mesh %s of %s is not closed or folds back onto itself (see the error above)." "\nUnion_mesh requires a closed (watertight) mesh.\n\n", - filename, NAME_CURRENT_COMP, this_mesh_storage.mesh.nboundary, this_mesh_storage.mesh.nnonmanifold); + filename, NAME_CURRENT_COMP); exit (1); } From b16525ad1cdd5b7d67dd1691de0154d0f22cb655 Mon Sep 17 00:00:00 2001 From: Lomholy <111510975+Lomholy@users.noreply.github.com> Date: Wed, 11 Nov 2026 14:00:07 +0100 Subject: [PATCH 16/22] Remove unnecessary closed mesh check --- common/lib/share/geometry-mesh-lib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/lib/share/geometry-mesh-lib.c b/common/lib/share/geometry-mesh-lib.c index f2c15ebff1..02e36b32a0 100644 --- a/common/lib/share/geometry-mesh-lib.c +++ b/common/lib/share/geometry-mesh-lib.c @@ -2305,7 +2305,7 @@ gmesh_inside (gmesh_struct* mesh, Coords position) { // as they are the surface the point sits on. A first hit on an edge, or two // hits at the same distance (touching parts of the mesh), may disagree, so // they are left to the parity test. - if (mesh->closed && mesh->oriented) { + if (mesh->oriented) { gmesh_hit first[2]; int stored = 0; int ambiguous = 0; From 8fddf30bab173479b58915e63b3c00737c5edf27 Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Wed, 11 Nov 2026 14:04:44 +0100 Subject: [PATCH 17/22] geometry-mesh-lib: count folded edges in gmesh_init, reject them in gmesh_check_closed gmesh_init counts the edges where the surface folds back onto itself (nfolded, with the first pair of faces in folded_face) and warns about them in its summary. Only gmesh_check_closed rejects such a mesh, using these counts. Co-Authored-By: Claude Opus 5.5 --- common/lib/share/geometry-mesh-lib.c | 46 ++++++++----------- common/lib/share/geometry-mesh-lib.h | 5 +- .../Test_lib_geometry_mesh_lib.instr | 8 ++-- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/common/lib/share/geometry-mesh-lib.c b/common/lib/share/geometry-mesh-lib.c index 02e36b32a0..4cc747836d 100644 --- a/common/lib/share/geometry-mesh-lib.c +++ b/common/lib/share/geometry-mesh-lib.c @@ -1722,6 +1722,20 @@ gmesh_load (char* filename, double scale, double xwidth, double yheight, double } else if (edge->dir != 0) { mesh->oriented = 0; } + // folded: the two triangles of the edge turn back onto each other, i.e. + // their normals are antiparallel once oriented alike (dir 0) + if (edge->n == 2) { + double turn = coords_sp (mesh->tri_normal[edge->t0], mesh->tri_normal[edge->t1]); + if (edge->dir != 0) + turn = -turn; + if (turn < -1 + GMESH_EPSILON) { + if (!mesh->nfolded) { + mesh->folded_face[0] = mesh->tri_face[edge->t0]; + mesh->folded_face[1] = mesh->tri_face[edge->t1]; + } + mesh->nfolded++; + } + } } if (mesh->nnonmanifold) mesh->oriented = 0; @@ -2330,40 +2344,16 @@ gmesh_inside (gmesh_struct* mesh, Coords position) { // closed check, see geometry-mesh-lib.h int gmesh_check_closed (gmesh_struct* mesh) { - gmesh_etab edges; - int i; - int nfolded = 0; - int folded_face[2] = { -1, -1 }; if (!mesh->closed) { fprintf (stderr, "Error: %s: %d boundary and %d non-manifold edges: the mesh is not closed (geometry-mesh-lib)\n", mesh->filename, mesh->nboundary, mesh->nnonmanifold); return 0; } - // folded edges: the two triangles of the edge turn back onto each other, - // i.e. their normals are antiparallel once oriented alike (dir 0) - gmesh_etab_build (&edges, mesh->tri, mesh->ntris); - for (i = 0; i < edges.cap; i++) { - gmesh_edge* edge = &edges.e[i]; - double turn; - if (edge->n != 2) - continue; - turn = coords_sp (mesh->tri_normal[edge->t0], mesh->tri_normal[edge->t1]); - if (edge->dir != 0) - turn = -turn; - if (turn < -1 + GMESH_EPSILON) { - if (!nfolded) { - folded_face[0] = mesh->tri_face[edge->t0]; - folded_face[1] = mesh->tri_face[edge->t1]; - } - nfolded++; - } - } - free (edges.e); - if (nfolded) { + if (mesh->nfolded) { fprintf (stderr, "Error: %s: %d edges where the surface folds back onto itself, e.g. between faces %d and %d (0-based): " "the mesh overlaps itself, so inside and outside are not defined (geometry-mesh-lib)\n", - mesh->filename, nfolded, folded_face[0], folded_face[1]); + mesh->filename, mesh->nfolded, mesh->folded_face[0], mesh->folded_face[1]); // a folded mesh encloses no well-defined volume mesh->closed = 0; return 0; @@ -2405,6 +2395,10 @@ gmesh_print_info (gmesh_struct* mesh) { if (!mesh->closed) { printf (" Warning: %d boundary and %d non-manifold edges: the mesh does not enclose a volume.\n", mesh->nboundary, mesh->nnonmanifold); } + if (mesh->nfolded) { + printf (" Warning: %d edges where the surface folds back onto itself, e.g. between faces %d and %d (0-based).\n", mesh->nfolded, mesh->folded_face[0], + mesh->folded_face[1]); + } if (mesh->nfaceprops) printf (" Per-face properties: %d\n", mesh->nfaceprops); diff --git a/common/lib/share/geometry-mesh-lib.h b/common/lib/share/geometry-mesh-lib.h index 0a11d4eede..98354a9d90 100644 --- a/common/lib/share/geometry-mesh-lib.h +++ b/common/lib/share/geometry-mesh-lib.h @@ -34,7 +34,8 @@ * - computes unit normals, area, volume and mesh topology: number of edges, * boundary / non-manifold edges, Euler characteristic, consistent * orientation. Closed, consistently oriented meshes are oriented with - * outward normals. + * outward normals. Edges where the surface folds back onto itself are + * counted (nfolded); gmesh_check_closed rejects such a mesh. * - builds a bounding volume hierarchy (BVH) so that a ray costs * O(log N) instead of O(N) triangle tests, without any allocation per ray. * @@ -136,6 +137,8 @@ typedef struct gmesh_struct { double area, volume; // surface area, enclosed volume (closed meshes) int nedges, nboundary, nnonmanifold, ndegenerate, nwelded; int nduplicate; // faces given more than once (all copies removed) + int nfolded; // edges where the surface folds back onto itself (see gmesh_check_closed) + int folded_face[2]; // faces of the first folded edge, 0-based int euler; // V - E + F (2 for a closed genus-0 surface) int closed; // every edge shared by exactly two triangles; after gmesh_check_closed, also not folded int oriented; // shared edges run in opposite directions diff --git a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr index b46961213d..c5944e3f51 100644 --- a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr +++ b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr @@ -8,8 +8,9 @@ * Fails to compile or link if the library does not build with the runtime. * INITIALIZE also checks gmesh_inside and gmesh_intersect_gravity, and * exits with an error when a check fails: -* - gmesh_check_closed passes cube.off and socket.off, and fails the open -* Guide.off and a folded (closed, oriented) cube +* - gmesh_init counts the folded edges of a folded (closed, oriented) cube, and +* gmesh_check_closed fails it and the open Guide.off, and passes cube.off +* and socket.off * - gmesh_inside on cube.off, and its shortcut for closed, oriented meshes * against the parity of ray crossings for random points around socket.off * - with zero acceleration it gives the hits of gmesh_intersect, to rounding @@ -71,7 +72,8 @@ int inside_tests (void) { "4 4 5 6 7\n4 0 1 5 4\n4 1 2 6 5\n4 2 3 7 6\n4 0 4 7 3\n3 1 0 8\n3 0 3 8\n3 3 2 8\n3 2 1 8\n"); fclose (file); mesh_check (gmesh_init ("Test_lib_geometry_mesh_lib_folded.off", 1, GMESH_PROPS_IGNORE, &folded) && folded.closed && folded.oriented - && !gmesh_check_closed (&folded) && !folded.closed, "folded mesh fails gmesh_check_closed and is then not closed", &failures); + && folded.nfolded == 2 && !gmesh_check_closed (&folded) && !folded.closed, + "folded mesh: 2 folded edges, fails gmesh_check_closed and is then not closed", &failures); gmesh_free (&folded); remove ("Test_lib_geometry_mesh_lib_folded.off"); } From 0407ea70eeec5933770f1985023be246f24cac77 Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Wed, 11 Nov 2026 14:27:17 +0100 Subject: [PATCH 18/22] geometry-mesh-lib: remove GMESH_TOL; components step past the surface instead GMESH_TOL is removed. gmesh_inside traces from t_min = 0, and tells coincident hits apart with GMESH_EPSILON times the mesh size. The samples call gmesh_intersect with t_min = 0. The crystals enter a mesh 1e-13 s past its surface (was 1 ns), and Refractor adds 1e-13 s to the hit times it propagates to, so that a neutron does not sit on the surface. Lens keeps a t_min of 1e-13 s, as a neutron reflected there would otherwise be past the surface it left. Co-Authored-By: Claude Opus 5.5 --- common/lib/share/geometry-mesh-lib.c | 7 +++---- common/lib/share/geometry-mesh-lib.h | 15 +++------------ mcstas-comps/contrib/Lens.comp | 9 ++++++--- .../contrib/Single_crystal_inelastic.comp | 8 ++++---- mcstas-comps/optics/Refractor.comp | 7 ++++--- mcstas-comps/samples/Incoherent.comp | 4 ++-- mcstas-comps/samples/Isotropic_Sqw.comp | 2 +- mcstas-comps/samples/PowderN.comp | 6 +++--- mcstas-comps/samples/Single_crystal.comp | 8 ++++---- mcstas-comps/samples/Single_magnetic_crystal.comp | 8 ++++---- 10 files changed, 34 insertions(+), 40 deletions(-) diff --git a/common/lib/share/geometry-mesh-lib.c b/common/lib/share/geometry-mesh-lib.c index 4cc747836d..385994593e 100644 --- a/common/lib/share/geometry-mesh-lib.c +++ b/common/lib/share/geometry-mesh-lib.c @@ -2315,18 +2315,17 @@ gmesh_inside (gmesh_struct* mesh, Coords position) { if (!gmesh_in_box (position, mesh->bbmin, mesh->bbmax)) return 0; // a closed, oriented mesh has outward normals: the point is inside when the - // first hit along a ray is leaving. Hits closer than GMESH_TOL are skipped, - // as they are the surface the point sits on. A first hit on an edge, or two + // first hit along a ray is leaving. A first hit on an edge, or two // hits at the same distance (touching parts of the mesh), may disagree, so // they are left to the parity test. if (mesh->oriented) { gmesh_hit first[2]; int stored = 0; int ambiguous = 0; - gmesh_trace (mesh, position, coords_set (D[0][0], D[0][1], D[0][2]), GMESH_TOL, first, 2, &stored, &ambiguous); + gmesh_trace (mesh, position, coords_set (D[0][0], D[0][1], D[0][2]), 0, first, 2, &stored, &ambiguous); if (!stored) return 0; - if (!first[0].edge && (stored == 1 || first[1].time - first[0].time > GMESH_TOL)) + if (!first[0].edge && (stored == 1 || first[1].time - first[0].time > GMESH_EPSILON * mesh->length_scale)) return first[0].in_out < 0; } for (j = 0; j < 3; j++) { diff --git a/common/lib/share/geometry-mesh-lib.h b/common/lib/share/geometry-mesh-lib.h index 98354a9d90..50f3fcb52d 100644 --- a/common/lib/share/geometry-mesh-lib.h +++ b/common/lib/share/geometry-mesh-lib.h @@ -80,15 +80,6 @@ #ifndef GMESH_MAX_HITS #define GMESH_MAX_HITS 50 #endif - // tolerance for hits on the surface a particle sits on, to be skipped: - // gmesh_inside skips hits closer than GMESH_TOL [m] along its ray, and - // components pass t_min = GMESH_TOL [s] to gmesh_intersect. A neutron is - // larger than this, so the ray picture does not hold below it anyway. To - // change it from an instrument, add - // DEPENDENCY " -DGMESH_TOL=1e-10 " after its DEFINE INSTRUMENT line - #ifndef GMESH_TOL - #define GMESH_TOL 1e-12 - #endif #ifndef GMESH_DISPLAY_MAX #define GMESH_DISPLAY_MAX 200000 #endif @@ -194,9 +185,9 @@ int gmesh_intersect_gravity (gmesh_struct* mesh, Coords position, Coords velocit // int gmesh_inside(gmesh_struct *mesh, Coords position) // ACTION: point in closed mesh test; 0 for an open mesh. For a closed, - // oriented mesh: whether the first hit along a fixed direction, beyond - // GMESH_TOL, is leaving; otherwise the parity of ray crossings along fixed - // directions. Does not use random numbers. + // oriented mesh: whether the first hit along a fixed direction is leaving; + // otherwise the parity of ray crossings along fixed directions. Does not + // use random numbers. // RETURN: 1 inside, 0 outside #pragma acc routine seq int gmesh_inside (gmesh_struct* mesh, Coords position); diff --git a/mcstas-comps/contrib/Lens.comp b/mcstas-comps/contrib/Lens.comp index e199c3e9d3..ac49c50ae7 100644 --- a/mcstas-comps/contrib/Lens.comp +++ b/mcstas-comps/contrib/Lens.comp @@ -282,7 +282,8 @@ TRACE #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { gmesh_hit hits[GMESH_MAX_HITS]; - if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits)) { + // t_min = 1e-13 s skips the surface the neutron sits on after a refraction or reflection + if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 1e-13, hits)) { dt = hits[0].time; // normal oriented as in interoff-lib: opposite to the right-hand rule of // the vertex order in the file, unless the mesh was flipped @@ -379,7 +380,8 @@ TRACE #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { gmesh_hit hits[GMESH_MAX_HITS]; - if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits)) { + // t_min = 1e-13 s skips the surface the neutron sits on after a refraction or reflection + if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 1e-13, hits)) { dt = hits[0].time; // normal oriented as in interoff-lib: opposite to the right-hand rule of // the vertex order in the file, unless the mesh was flipped @@ -489,7 +491,8 @@ TRACE #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { gmesh_hit hits[GMESH_MAX_HITS]; - if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits)) { + // t_min = 1e-13 s skips the surface the neutron sits on after a refraction or reflection + if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 1e-13, hits)) { dt = hits[0].time; // normal oriented as in interoff-lib: opposite to the right-hand rule of // the vertex order in the file, unless the mesh was flipped diff --git a/mcstas-comps/contrib/Single_crystal_inelastic.comp b/mcstas-comps/contrib/Single_crystal_inelastic.comp index c0999dbe12..c7cadbbcaf 100644 --- a/mcstas-comps/contrib/Single_crystal_inelastic.comp +++ b/mcstas-comps/contrib/Single_crystal_inelastic.comp @@ -804,15 +804,15 @@ TRACE intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) { gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); intersect = nhits > 0; if (intersect) { if (gmesh_inside (&mesh, coords_set (x, y, z))) { t1 = 0; t2 = hits[0].time; } else { - // 1 ns past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) - t1 = hits[0].time + 1e-9; + // 1e-13 s past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) + t1 = hits[0].time + 1e-13; t2 = nhits > 1 ? hits[1].time : hits[0].time; } } @@ -851,7 +851,7 @@ TRACE intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) { gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); intersect = nhits > 0 && gmesh_inside (&mesh, coords_set (x, y, z)); if (intersect) { t1 = 0; diff --git a/mcstas-comps/optics/Refractor.comp b/mcstas-comps/optics/Refractor.comp index a049671fe2..c30bb627c4 100644 --- a/mcstas-comps/optics/Refractor.comp +++ b/mcstas-comps/optics/Refractor.comp @@ -413,12 +413,13 @@ TRACE #ifdef USE_OFF else if (geometry && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); intersect = nhits > 0; if (intersect) { mesh_inside = gmesh_inside (&thread_mesh, coords_set (x, y, z)); - t0 = mesh_inside ? 0 : hits[0].time; - t1 = mesh_inside || nhits == 1 ? hits[0].time : hits[1].time; + // 1e-13 s past the surfaces, so that the next intersection does not find them again + t0 = mesh_inside ? 0 : hits[0].time + 1e-13; + t1 = (mesh_inside || nhits == 1 ? hits[0].time : hits[1].time) + 1e-13; // normal of the next hit, oriented as in interoff-lib: opposite to the // right-hand rule of the vertex order in the file, unless the mesh was flipped coords_get (coords_scale (hits[0].normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); diff --git a/mcstas-comps/samples/Incoherent.comp b/mcstas-comps/samples/Incoherent.comp index 709e174f91..51a981c0b3 100644 --- a/mcstas-comps/samples/Incoherent.comp +++ b/mcstas-comps/samples/Incoherent.comp @@ -287,7 +287,7 @@ TRACE #ifdef USE_OFF else if (VarsInc.shape == 3) { gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); intersect = nhits > 0; if (intersect) { if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { @@ -496,7 +496,7 @@ TRACE #ifdef USE_OFF else if (VarsInc.shape == 3) { gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); intersect = nhits > 0; if (intersect) { t0 = 0; diff --git a/mcstas-comps/samples/Isotropic_Sqw.comp b/mcstas-comps/samples/Isotropic_Sqw.comp index 29c3eac252..3837133879 100644 --- a/mcstas-comps/samples/Isotropic_Sqw.comp +++ b/mcstas-comps/samples/Isotropic_Sqw.comp @@ -2258,7 +2258,7 @@ TRACE // a mesh is used as given, for either sign of thickness if (VarSqw.shape == 3) { gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); intersect = nhits > 0; if (intersect) { if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { diff --git a/mcstas-comps/samples/PowderN.comp b/mcstas-comps/samples/PowderN.comp index c92af17187..c2f62ac105 100644 --- a/mcstas-comps/samples/PowderN.comp +++ b/mcstas-comps/samples/PowderN.comp @@ -1123,7 +1123,7 @@ TRACE #ifdef USE_OFF else if (line_info.shape == 3) { gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); intersect = nhits > 0; if (intersect) { if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { @@ -1329,7 +1329,7 @@ TRACE #ifdef USE_OFF else if (line_info.shape == 3) { gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); intersect = nhits > 0; if (intersect) { t0 = 0; @@ -1409,7 +1409,7 @@ TRACE #ifdef USE_OFF else if (line_info.shape == 3) { gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); intersect = nhits > 0; if (intersect) { t0 = 0; diff --git a/mcstas-comps/samples/Single_crystal.comp b/mcstas-comps/samples/Single_crystal.comp index 8389a28460..619e0d2a76 100644 --- a/mcstas-comps/samples/Single_crystal.comp +++ b/mcstas-comps/samples/Single_crystal.comp @@ -1529,15 +1529,15 @@ TRACE #ifdef USE_OFF else if (hkl_info.shape == 3) { gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); intersect = nhits > 0; if (intersect) { if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { t1 = 0; t2 = hits[0].time; } else { - // 1 ns past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) - t1 = hits[0].time + 1e-9; + // 1e-13 s past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) + t1 = hits[0].time + 1e-13; t2 = nhits > 1 ? hits[1].time : hits[0].time; } } @@ -1590,7 +1590,7 @@ TRACE #ifdef USE_OFF else if (hkl_info.shape == 3) { gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); intersect = nhits > 0 && gmesh_inside (&thread_mesh, coords_set (x, y, z)); if (intersect) { t1 = 0; diff --git a/mcstas-comps/samples/Single_magnetic_crystal.comp b/mcstas-comps/samples/Single_magnetic_crystal.comp index da168afb69..3eb93108e6 100644 --- a/mcstas-comps/samples/Single_magnetic_crystal.comp +++ b/mcstas-comps/samples/Single_magnetic_crystal.comp @@ -622,15 +622,15 @@ TRACE intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) { gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); intersect = nhits > 0; if (intersect) { if (gmesh_inside (&mesh, coords_set (x, y, z))) { t1 = 0; t2 = hits[0].time; } else { - // 1 ns past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) - t1 = hits[0].time + 1e-9; + // 1e-13 s past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) + t1 = hits[0].time + 1e-13; t2 = nhits > 1 ? hits[1].time : hits[0].time; } } @@ -663,7 +663,7 @@ TRACE intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); else if (hkl_info.shape == 3) { gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), GMESH_TOL, hits); + int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); intersect = nhits > 0 && gmesh_inside (&mesh, coords_set (x, y, z)); if (intersect) { t1 = 0; From 25f644557061e46bf96d1def42380432b7bb533a Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Wed, 11 Nov 2026 18:08:35 +0100 Subject: [PATCH 19/22] geometry-mesh-lib: rename constant parameter; libtest file per MPI process - gmesh_quadratic_roots had a parameter named constant. SasView's sas_kernel_header.c defines constant as a macro (const), so any instrument combining a SasView component with a geometry-mesh-lib user (e.g. Monitor_nD in templateSasView_Mantid) failed to compile. The parameter is now constant_term. - Test_lib_geometry_mesh_lib wrote, read and removed one scratch file for its folded-mesh check from every MPI process, so under MPI one process could find it missing or half written. The file name now carries the MPI rank. Co-Authored-By: Claude Opus 5.5 --- common/lib/share/geometry-mesh-lib.c | 10 +++++----- .../Test_lib_geometry_mesh_lib.instr | 12 +++++++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/common/lib/share/geometry-mesh-lib.c b/common/lib/share/geometry-mesh-lib.c index 385994593e..1644ada6ca 100644 --- a/common/lib/share/geometry-mesh-lib.c +++ b/common/lib/share/geometry-mesh-lib.c @@ -2024,20 +2024,20 @@ gmesh_intersect (gmesh_struct* mesh, Coords position, Coords direction, double t // Ray tracing of parabolas (e.g. under gravity) // ========================================================================== - // real roots of quadratic*t^2 + linear*t + constant = 0 in roots[0..1], + // real roots of quadratic*t^2 + linear*t + constant_term = 0 in roots[0..1], // sorted, solving the linear equation when quadratic is 0. Returns their number. #pragma acc routine seq static int -gmesh_quadratic_roots (double quadratic, double linear, double constant, double roots[2]) { +gmesh_quadratic_roots (double quadratic, double linear, double constant_term, double roots[2]) { double discriminant; double q; if (quadratic == 0) { if (linear == 0) return 0; - roots[0] = -constant / linear; + roots[0] = -constant_term / linear; return 1; } - discriminant = linear * linear - 4 * quadratic * constant; + discriminant = linear * linear - 4 * quadratic * constant_term; if (discriminant < 0) return 0; // q avoids the cancellation of -linear + sqrt(discriminant) @@ -2047,7 +2047,7 @@ gmesh_quadratic_roots (double quadratic, double linear, double constant, double return 1; } roots[0] = q / quadratic; - roots[1] = constant / q; + roots[1] = constant_term / q; if (roots[0] > roots[1]) { double swap = roots[0]; roots[0] = roots[1]; diff --git a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr index c5944e3f51..a23d14571d 100644 --- a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr +++ b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr @@ -66,16 +66,22 @@ int inside_tests (void) { // consistently oriented, but overlaps itself at 2 edges. { gmesh_struct folded; - FILE* file = fopen ("Test_lib_geometry_mesh_lib_folded.off", "w"); + // one file per MPI process, as all processes run these checks + char folded_file[64] = "Test_lib_geometry_mesh_lib_folded.off"; + FILE* file; + #ifdef USE_MPI + snprintf (folded_file, sizeof (folded_file), "Test_lib_geometry_mesh_lib_folded_%d.off", mpi_node_rank); + #endif + file = fopen (folded_file, "w"); if (file) { fprintf (file, "OFF\n9 9 0\n0 0 0\n1 0 0\n1 1 0\n0 1 0\n0 0 1\n1 0 1\n1 1 1\n0 1 1\n1.5 0.5 0\n" "4 4 5 6 7\n4 0 1 5 4\n4 1 2 6 5\n4 2 3 7 6\n4 0 4 7 3\n3 1 0 8\n3 0 3 8\n3 3 2 8\n3 2 1 8\n"); fclose (file); - mesh_check (gmesh_init ("Test_lib_geometry_mesh_lib_folded.off", 1, GMESH_PROPS_IGNORE, &folded) && folded.closed && folded.oriented + mesh_check (gmesh_init (folded_file, 1, GMESH_PROPS_IGNORE, &folded) && folded.closed && folded.oriented && folded.nfolded == 2 && !gmesh_check_closed (&folded) && !folded.closed, "folded mesh: 2 folded edges, fails gmesh_check_closed and is then not closed", &failures); gmesh_free (&folded); - remove ("Test_lib_geometry_mesh_lib_folded.off"); + remove (folded_file); } } mesh_check (gmesh_inside (&cube, coords_set (0.5, 0.5, 1.5)) == 0, "outside the cube", &failures); From 2dcd7ad725b4e044943786c8c142255778804005 Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Thu, 12 Nov 2026 09:33:00 +0100 Subject: [PATCH 20/22] Format mesh_lib components with mccode-clangformat Run devel/bin/mccode-clangformat (clang-format 23.1.5) on the components changed in mccode-dev/McCode#3090. Whitespace and line wrapping only. Co-Authored-By: Claude Opus 5.5 --- .../contrib/Single_crystal_inelastic.comp | 14 ++-- mcstas-comps/obsolete/Guide_anyshape_r.comp | 4 +- mcstas-comps/optics/Refractor.comp | 3 +- mcstas-comps/samples/Incoherent.comp | 3 +- mcstas-comps/samples/Isotropic_Sqw.comp | 83 +++++++++---------- mcstas-comps/samples/Single_crystal.comp | 3 +- .../samples/Single_magnetic_crystal.comp | 3 +- 7 files changed, 56 insertions(+), 57 deletions(-) diff --git a/mcstas-comps/contrib/Single_crystal_inelastic.comp b/mcstas-comps/contrib/Single_crystal_inelastic.comp index c7cadbbcaf..358b696960 100644 --- a/mcstas-comps/contrib/Single_crystal_inelastic.comp +++ b/mcstas-comps/contrib/Single_crystal_inelastic.comp @@ -164,10 +164,10 @@ SHARE double* SwCDF; /* Cumulative dist. func. of S(|Q|) for inv. transform sampling */ int nSw; /* Number of points in CDF */ int** SwQi; - int* nQ; /* Number of q-points at each energy */ - double* SqwCDF; /* Cumulative dist. func. of S(E) at particular values of |Q| */ - int* iSqwCDF; /* Index into CDF of S(E) */ - int maxecount; /* Maximum number of E-slice for each |Q| */ + int* nQ; /* Number of q-points at each energy */ + double* SqwCDF; /* Cumulative dist. func. of S(E) at particular values of |Q| */ + int* iSqwCDF; /* Index into CDF of S(E) */ + int maxecount; /* Maximum number of E-slice for each |Q| */ }; /* Sum of S(q,w) over the hkl-E points kinematically accessible with (kix,kiy,kiz). @@ -240,7 +240,8 @@ SHARE } int - read_hkl_data (char* SC_file, struct hkl_info_struct* info, double SC_mosaic, double SC_mosaic_a, double SC_mosaic_b, double SC_mosaic_c, double* SC_mosaic_AB) { + read_hkl_data (char* SC_file, struct hkl_info_struct* info, double SC_mosaic, double SC_mosaic_a, double SC_mosaic_b, double SC_mosaic_c, + double* SC_mosaic_AB) { struct hkl_data* list = NULL; int size = 0; t_Table sTable; /* sample data table structure from SC_file */ @@ -722,7 +723,8 @@ INITIALIZE else exit (fprintf (stderr, "Single_crystal_inelastic: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); if (!gmesh_check_closed (&mesh)) - exit (fprintf (stderr, "Single_crystal_inelastic: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); + exit (fprintf (stderr, "Single_crystal_inelastic: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", + NAME_CURRENT_COMP, geometry)); } else if (xwidth && yheight && zdepth) hkl_info.shape = 1; /* box */ else if (radius > 0 && yheight) diff --git a/mcstas-comps/obsolete/Guide_anyshape_r.comp b/mcstas-comps/obsolete/Guide_anyshape_r.comp index 982b0c914d..b2775e6150 100644 --- a/mcstas-comps/obsolete/Guide_anyshape_r.comp +++ b/mcstas-comps/obsolete/Guide_anyshape_r.comp @@ -115,8 +115,8 @@ INITIALIZE if (m == 0 && alpha == 0 && W == 0) { use_file_coatings = 1; if (mesh.nfaceprops < 3) { - fprintf (stderr, "Guide_anyshape_r: %s: Error: m, alpha, W are all 0, but %s does not define m, alpha and W values for each face.\n", - NAME_CURRENT_COMP, geometry); + fprintf (stderr, "Guide_anyshape_r: %s: Error: m, alpha, W are all 0, but %s does not define m, alpha and W values for each face.\n", NAME_CURRENT_COMP, + geometry); exit (-1); } printf ("Guide_anyshape_r: %s: All of m, alpha, W assigned 0: \n - We are using H. Jacobsen reflectivity model if m-values are given in %s\n", diff --git a/mcstas-comps/optics/Refractor.comp b/mcstas-comps/optics/Refractor.comp index c30bb627c4..02eb9c27db 100644 --- a/mcstas-comps/optics/Refractor.comp +++ b/mcstas-comps/optics/Refractor.comp @@ -351,7 +351,8 @@ INITIALIZE if (!gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) exit (printf ("Refractor: %s: FATAL: invalid OFF/PLY geometry specification for file '%s'.\n", NAME_CURRENT_COMP, geometry)); if (!gmesh_check_closed (&mesh)) - exit (fprintf (stderr, "Refractor: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); + exit ( + fprintf (stderr, "Refractor: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); #endif } else exit (printf ("Refractor: %s: FATAL: invalid geometry specification.\n" diff --git a/mcstas-comps/samples/Incoherent.comp b/mcstas-comps/samples/Incoherent.comp index 51a981c0b3..99ae4d2f2d 100644 --- a/mcstas-comps/samples/Incoherent.comp +++ b/mcstas-comps/samples/Incoherent.comp @@ -166,7 +166,8 @@ INITIALIZE } else exit (fprintf (stderr, "Incoherent: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); if (!gmesh_check_closed (&mesh)) - exit (fprintf (stderr, "Incoherent: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); + exit (fprintf (stderr, "Incoherent: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, + geometry)); #endif } else if (xwidth && yheight && zdepth) VarsInc.shape = 1; /* box */ diff --git a/mcstas-comps/samples/Isotropic_Sqw.comp b/mcstas-comps/samples/Isotropic_Sqw.comp index 3837133879..381628beaa 100644 --- a/mcstas-comps/samples/Isotropic_Sqw.comp +++ b/mcstas-comps/samples/Isotropic_Sqw.comp @@ -1599,10 +1599,7 @@ SHARE T = 0; /* unrealistic */ if (!T_file && T) { MPI_MASTER ( - if (Sqw->verbose_output > 0) { - printf ("Isotropic_Sqw: %s: Temperature computed from S(q,w) data from %s is T=%g [K].\n", Sqw->compname, file, T); - } - ); + if (Sqw->verbose_output > 0) { printf ("Isotropic_Sqw: %s: Temperature computed from S(q,w) data from %s is T=%g [K].\n", Sqw->compname, file, T); }); } if (Sqw->Temperature == 0) { @@ -2040,7 +2037,8 @@ INITIALIZE } else exit (fprintf (stderr, "Isotropic_Sqw: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); if (!gmesh_check_closed (&mesh)) - exit (fprintf (stderr, "Isotropic_Sqw: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); + exit (fprintf (stderr, "Isotropic_Sqw: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, + geometry)); #endif } else if (xwidth && yheight && zdepth) VarSqw.shape = 1; /* box */ @@ -2270,24 +2268,24 @@ TRACE } } } else - #endif - if (thickness >= 0) { - if (VarSqw.shape == 0) - intersect = cylinder_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius, yheight); - else if (VarSqw.shape == 1) - intersect = box_intersect (&t0, &t3, x, y, z, vx, vy, vz, xwidth, yheight, zdepth); - else if (VarSqw.shape == 2) - intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); - } else { - if (VarSqw.shape == 0) - intersect = cylinder_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius - thickness, yheight - 2 * thickness > 0 ? yheight - 2 * thickness : yheight); - else if (VarSqw.shape == 1) - intersect - = box_intersect (&t0, &t3, x, y, z, vx, vy, vz, xwidth - 2 * thickness > 0 ? xwidth - 2 * thickness : xwidth, - yheight - 2 * thickness > 0 ? yheight - 2 * thickness : yheight, zdepth - 2 * thickness > 0 ? zdepth - 2 * thickness : zdepth); - else if (VarSqw.shape == 2) - intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius - thickness); - } + #endif + if (thickness >= 0) { + if (VarSqw.shape == 0) + intersect = cylinder_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius, yheight); + else if (VarSqw.shape == 1) + intersect = box_intersect (&t0, &t3, x, y, z, vx, vy, vz, xwidth, yheight, zdepth); + else if (VarSqw.shape == 2) + intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); + } else { + if (VarSqw.shape == 0) + intersect = cylinder_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius - thickness, yheight - 2 * thickness > 0 ? yheight - 2 * thickness : yheight); + else if (VarSqw.shape == 1) + intersect + = box_intersect (&t0, &t3, x, y, z, vx, vy, vz, xwidth - 2 * thickness > 0 ? xwidth - 2 * thickness : xwidth, + yheight - 2 * thickness > 0 ? yheight - 2 * thickness : yheight, zdepth - 2 * thickness > 0 ? zdepth - 2 * thickness : zdepth); + else if (VarSqw.shape == 2) + intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius - thickness); + } } else intersect = 0; @@ -2413,7 +2411,7 @@ TRACE } /* Proba of scattering vs absorption (integrating along the whole trajectory) */ - ws = my_s / my_t; /* (inc+coh)/(inc+coh+abs) */ + ws = my_s / my_t; /* (inc+coh)/(inc+coh+abs) */ d_path = v * (dt0 + dt2); /* total path lenght in sample */ /* Proba of transmission/interaction along length d_path */ p_trans = exp (-my_t * d_path); @@ -2833,27 +2831,22 @@ FINALLY mc_MPI_Sum (&VarSqw.multi, 1); } #endif - MPI_MASTER( - if (VarSqw.neutron_removed) - printf("Isotropic_Sqw: %s: %li neutron events (out of %li) that should have\n" - " scattered were transmitted because scattering conditions\n" - "WARNING could not be satisfied after %i tries.\n", - NAME_CURRENT_COMP, VarSqw.neutron_removed, - VarSqw.neutron_exit+VarSqw.neutron_removed, VarSqw.maxloop); - if (VarSqw.neutron_pmult) - printf("Isotropic_Sqw: %s: %li neutron events (out of %li) reached\n" - "WARNING unrealistic weight. The S(q,w) norm might be too high.\n", - NAME_CURRENT_COMP, VarSqw.neutron_pmult, VarSqw.neutron_exit); - - if (VarSqw.verbose_output >= 1 && VarSqw.psum_scatt > 0) { - printf ("Isotropic_Sqw: %s: Scattering fraction=%g of incoming intensity\n" - " Absorption fraction =%g\n", - NAME_CURRENT_COMP, VarSqw.mean_scatt / VarSqw.psum_scatt, VarSqw.mean_abs / VarSqw.psum_scatt); - printf (" Single scattering intensity =%g (coh=%g inc=%g)\n" - " Multiple scattering intensity =%g\n", - VarSqw.single_coh + VarSqw.single_inc, VarSqw.single_coh, VarSqw.single_inc, VarSqw.multi); - } - ); + MPI_MASTER (if (VarSqw.neutron_removed) printf ("Isotropic_Sqw: %s: %li neutron events (out of %li) that should have\n" + " scattered were transmitted because scattering conditions\n" + "WARNING could not be satisfied after %i tries.\n", + NAME_CURRENT_COMP, VarSqw.neutron_removed, VarSqw.neutron_exit + VarSqw.neutron_removed, VarSqw.maxloop); + if (VarSqw.neutron_pmult) printf ("Isotropic_Sqw: %s: %li neutron events (out of %li) reached\n" + "WARNING unrealistic weight. The S(q,w) norm might be too high.\n", + NAME_CURRENT_COMP, VarSqw.neutron_pmult, VarSqw.neutron_exit); + + if (VarSqw.verbose_output >= 1 && VarSqw.psum_scatt > 0) { + printf ("Isotropic_Sqw: %s: Scattering fraction=%g of incoming intensity\n" + " Absorption fraction =%g\n", + NAME_CURRENT_COMP, VarSqw.mean_scatt / VarSqw.psum_scatt, VarSqw.mean_abs / VarSqw.psum_scatt); + printf (" Single scattering intensity =%g (coh=%g inc=%g)\n" + " Multiple scattering intensity =%g\n", + VarSqw.single_coh + VarSqw.single_inc, VarSqw.single_coh, VarSqw.single_inc, VarSqw.multi); + }); /* end FINALLY */ %} /*****************************************************************************/ diff --git a/mcstas-comps/samples/Single_crystal.comp b/mcstas-comps/samples/Single_crystal.comp index 619e0d2a76..503fd439fd 100644 --- a/mcstas-comps/samples/Single_crystal.comp +++ b/mcstas-comps/samples/Single_crystal.comp @@ -1407,7 +1407,8 @@ INITIALIZE else exit (fprintf (stderr, "Single_crystal: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); if (!gmesh_check_closed (&mesh)) - exit (fprintf (stderr, "Single_crystal: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); + exit (fprintf (stderr, "Single_crystal: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, + geometry)); #endif } else if (xwidth && yheight && zdepth) hkl_info.shape = 1; /* box */ diff --git a/mcstas-comps/samples/Single_magnetic_crystal.comp b/mcstas-comps/samples/Single_magnetic_crystal.comp index 3eb93108e6..2720fb04dd 100644 --- a/mcstas-comps/samples/Single_magnetic_crystal.comp +++ b/mcstas-comps/samples/Single_magnetic_crystal.comp @@ -551,7 +551,8 @@ INITIALIZE else exit (fprintf (stderr, "Single_magnetic_crystal: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); if (!gmesh_check_closed (&mesh)) - exit (fprintf (stderr, "Single_magnetic_crystal: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); + exit (fprintf (stderr, "Single_magnetic_crystal: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", + NAME_CURRENT_COMP, geometry)); } else if (xwidth && yheight && zdepth) hkl_info.shape = 1; /* box */ else if (radius > 0 && yheight) From 0a7e626477034e43616daaf6d186ea0868c9a94d Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Fri, 13 Nov 2026 11:59:17 +0100 Subject: [PATCH 21/22] geometry-mesh-lib: review changes for mccode-dev/McCode#3090 - gmesh_init_fit resizes the box about its centre; with center=0 the centre stays in place - gmesh_entry_exit: first entry and exit of a line, inside from the first hit (in_out < 0), hit times 1e-13 s past the surface. Used by all sample components, Refractor and Monitor_nD (replaces Monitor_nD_MeshIntersect); gmesh_inside back to the parity test - keep gmesh_hit.t: Lens and Guide_anyshape(_r) read the hit in a SHARE function, as t is the particle time in TRACE - a folded mesh is not closed (set in gmesh_load); gmesh_check_closed only checks - Guide_anyshape: named per-face property indices - exit(-1) after printing the error in new and touched code - libtest: gmesh_init_fit placement and gmesh_entry_exit checks Co-Authored-By: Claude Opus 5.5 --- common/lib/share/geometry-mesh-lib.c | 87 +++++++++-------- common/lib/share/geometry-mesh-lib.h | 32 +++--- mcstas-comps/contrib/Lens.comp | 62 ++++++------ .../contrib/Single_crystal_inelastic.comp | 42 +++----- .../Test_lib_geometry_mesh_lib.instr | 97 +++++++++++++------ mcstas-comps/misc/Shape.comp | 6 +- mcstas-comps/misc/Shape_simple.comp | 6 +- mcstas-comps/monitors/Monitor_nD.comp | 6 +- mcstas-comps/monitors/Monitor_nD_noacc.comp | 6 +- mcstas-comps/obsolete/Guide_anyshape_r.comp | 20 +++- mcstas-comps/optics/Guide_anyshape.comp | 50 ++++++---- mcstas-comps/optics/Refractor.comp | 32 +++--- mcstas-comps/samples/Incoherent.comp | 40 +++----- mcstas-comps/samples/Isotropic_Sqw.comp | 70 ++++++------- mcstas-comps/samples/PowderN.comp | 41 +++----- mcstas-comps/samples/Single_crystal.comp | 42 +++----- .../samples/Single_magnetic_crystal.comp | 42 +++----- mcstas-comps/share/monitor_nd-lib.c | 25 ----- mcstas-comps/share/monitor_nd-lib.h | 2 - mcstas-comps/share/union-lib.c | 2 +- mcstas-comps/union/Union_mesh.comp | 6 +- mcxtrace-comps/share/union-lib.c | 2 +- mcxtrace-comps/union/Union_mesh.comp | 6 +- 23 files changed, 343 insertions(+), 381 deletions(-) diff --git a/common/lib/share/geometry-mesh-lib.c b/common/lib/share/geometry-mesh-lib.c index 1644ada6ca..1368a91a48 100644 --- a/common/lib/share/geometry-mesh-lib.c +++ b/common/lib/share/geometry-mesh-lib.c @@ -1400,9 +1400,8 @@ gmesh_fit_scale (Coords range, double xwidth, double yheight, double zdepth) { } // read an STL/OFF/PLY file into mesh: multiply by scale, then resize the -// bounding box to xwidth/yheight/zdepth (0: keep); with center the box centre -// is moved to the origin and the box resized about it, otherwise the -// coordinates are scaled about the origin (as off_init of interoff-lib) +// bounding box about its centre to xwidth/yheight/zdepth (0: keep); with +// center the box centre is moved to the origin, otherwise it stays in place static int gmesh_load (char* filename, double scale, double xwidth, double yheight, double zdepth, int center, int face_props, gmesh_struct* mesh) { gmesh_raw raw; @@ -1477,16 +1476,19 @@ gmesh_load (char* filename, double scale, double xwidth, double yheight, double } if (xwidth || yheight || zdepth || center) { Coords range = coords_sub (mesh->bbmax, mesh->bbmin); - Coords origin = center ? coords_scale (coords_add (mesh->bbmin, mesh->bbmax), 0.5) : coords_set (0, 0, 0); + Coords box_center = coords_scale (coords_add (mesh->bbmin, mesh->bbmax), 0.5); + Coords shift = center ? coords_set (0, 0, 0) : box_center; Coords scale = gmesh_fit_scale (range, xwidth, yheight, zdepth); for (i = 0; i < raw.nverts; i++) { Coords* p = &raw.verts[i]; - p->x = (p->x - origin.x) * scale.x; - p->y = (p->y - origin.y) * scale.y; - p->z = (p->z - origin.z) * scale.z; + p->x = (p->x - box_center.x) * scale.x + shift.x; + p->y = (p->y - box_center.y) * scale.y + shift.y; + p->z = (p->z - box_center.z) * scale.z + shift.z; } - mesh->bbmin = coords_set ((mesh->bbmin.x - origin.x) * scale.x, (mesh->bbmin.y - origin.y) * scale.y, (mesh->bbmin.z - origin.z) * scale.z); - mesh->bbmax = coords_set ((mesh->bbmax.x - origin.x) * scale.x, (mesh->bbmax.y - origin.y) * scale.y, (mesh->bbmax.z - origin.z) * scale.z); + mesh->bbmin = coords_set ((mesh->bbmin.x - box_center.x) * scale.x + shift.x, (mesh->bbmin.y - box_center.y) * scale.y + shift.y, + (mesh->bbmin.z - box_center.z) * scale.z + shift.z); + mesh->bbmax = coords_set ((mesh->bbmax.x - box_center.x) * scale.x + shift.x, (mesh->bbmax.y - box_center.y) * scale.y + shift.y, + (mesh->bbmax.z - box_center.z) * scale.z + shift.z); MPI_MASTER ( if (scale.x != scale.y || scale.x != scale.z) printf ("Warning: Aspect ratio of the geometry %s was modified.\n" @@ -1747,7 +1749,7 @@ gmesh_load (char* filename, double scale, double xwidth, double yheight, double } } mesh->euler = (int)(nused - mesh->nedges + mesh->ntris); - mesh->closed = (mesh->nboundary == 0 && mesh->nnonmanifold == 0); + mesh->closed = (mesh->nboundary == 0 && mesh->nnonmanifold == 0 && mesh->nfolded == 0); free (queue); free (visited); free (used); @@ -1960,7 +1962,7 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, *ambiguous = 1; continue; } - hit.time = hit_t; + hit.t = hit_t; hit.normal = mesh->tri_normal[t]; hit.tri = t; hit.face = mesh->tri_face[t]; @@ -1968,7 +1970,7 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, hit.edge = on_edge; // hits on shared edges/vertices: keep once for (k = 0; k < *stored_count; k++) { - if (hits[k].in_out == in_out && fabs (hits[k].time - hit_t) <= t_tolerance) { + if (hits[k].in_out == in_out && fabs (hits[k].t - hit_t) <= t_tolerance) { duplicate = 1; break; } @@ -1980,13 +1982,13 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, if (position == max_hits) { if (max_hits == 0) continue; - if (hit_t >= hits[max_hits - 1].time) + if (hit_t >= hits[max_hits - 1].t) continue; position = max_hits - 1; } else { (*stored_count)++; } - while (position > 0 && hits[position - 1].time > hit_t) { + while (position > 0 && hits[position - 1].t > hit_t) { hits[position] = hits[position - 1]; position--; } @@ -2234,7 +2236,7 @@ gmesh_trace_gravity (gmesh_struct* mesh, Coords origin, Coords velocity, Coords } else { in_out = -1; } - hit.time = hit_t; + hit.t = hit_t; hit.normal = mesh->tri_normal[triangle]; hit.tri = triangle; hit.face = mesh->tri_face[triangle]; @@ -2242,7 +2244,7 @@ gmesh_trace_gravity (gmesh_struct* mesh, Coords origin, Coords velocity, Coords hit.edge = crossing_edge[i_crossing]; // hits on shared edges/vertices: keep once for (k = 0; k < *stored_count; k++) { - if (hits[k].in_out == in_out && fabs (hits[k].time - hit_t) <= t_tolerance) { + if (hits[k].in_out == in_out && fabs (hits[k].t - hit_t) <= t_tolerance) { duplicate = 1; break; } @@ -2252,13 +2254,13 @@ gmesh_trace_gravity (gmesh_struct* mesh, Coords origin, Coords velocity, Coords total++; position = *stored_count; if (position == max_hits) { - if (hit_t >= hits[max_hits - 1].time) + if (hit_t >= hits[max_hits - 1].t) continue; position = max_hits - 1; } else { (*stored_count)++; } - while (position > 0 && hits[position - 1].time > hit_t) { + while (position > 0 && hits[position - 1].t > hit_t) { hits[position] = hits[position - 1]; position--; } @@ -2309,25 +2311,8 @@ gmesh_inside (gmesh_struct* mesh, Coords position) { const double D[3][3] = { { 0.4719, 0.6213, 0.6254 }, { 0.8017, -0.3259, 0.5012 }, { -0.2263, 0.9147, -0.3349 } }; int j; int votes = 0; - // an open mesh encloses no volume - if (!mesh->closed) - return 0; if (!gmesh_in_box (position, mesh->bbmin, mesh->bbmax)) return 0; - // a closed, oriented mesh has outward normals: the point is inside when the - // first hit along a ray is leaving. A first hit on an edge, or two - // hits at the same distance (touching parts of the mesh), may disagree, so - // they are left to the parity test. - if (mesh->oriented) { - gmesh_hit first[2]; - int stored = 0; - int ambiguous = 0; - gmesh_trace (mesh, position, coords_set (D[0][0], D[0][1], D[0][2]), 0, first, 2, &stored, &ambiguous); - if (!stored) - return 0; - if (!first[0].edge && (stored == 1 || first[1].time - first[0].time > GMESH_EPSILON * mesh->length_scale)) - return first[0].in_out < 0; - } for (j = 0; j < 3; j++) { int stored = 0; int ambiguous = 0; @@ -2340,10 +2325,36 @@ gmesh_inside (gmesh_struct* mesh, Coords position) { return votes >= 2; } + // first entry and exit of a line through mesh, see geometry-mesh-lib.h + #pragma acc routine seq +int +gmesh_entry_exit (gmesh_struct* mesh, Coords position, Coords velocity, double* t_in, double* t_out, gmesh_hit* first) { + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (mesh, position, velocity, 0, hits); + int inside = 0; + if (!nhits) + return 0; + // normals of a closed, oriented mesh point outward: leaving first means inside + if (mesh->closed && mesh->oriented) + inside = hits[0].in_out < 0; + else if (mesh->closed) + inside = gmesh_inside (mesh, position); + if (first) + *first = hits[0]; + if (inside) { + *t_in = 0; + *t_out = hits[0].t + 1e-13; + return 1; + } + *t_in = hits[0].t + 1e-13; + *t_out = (nhits > 1 ? hits[1].t : hits[0].t) + 1e-13; + return 2; +} + // closed check, see geometry-mesh-lib.h int gmesh_check_closed (gmesh_struct* mesh) { - if (!mesh->closed) { + if (mesh->nboundary || mesh->nnonmanifold) { fprintf (stderr, "Error: %s: %d boundary and %d non-manifold edges: the mesh is not closed (geometry-mesh-lib)\n", mesh->filename, mesh->nboundary, mesh->nnonmanifold); return 0; @@ -2353,8 +2364,6 @@ gmesh_check_closed (gmesh_struct* mesh) { "Error: %s: %d edges where the surface folds back onto itself, e.g. between faces %d and %d (0-based): " "the mesh overlaps itself, so inside and outside are not defined (geometry-mesh-lib)\n", mesh->filename, mesh->nfolded, mesh->folded_face[0], mesh->folded_face[1]); - // a folded mesh encloses no well-defined volume - mesh->closed = 0; return 0; } return 1; @@ -2391,7 +2400,7 @@ gmesh_print_info (gmesh_struct* mesh) { printf (", volume %g", mesh->volume); printf ("\n Topology: %d edges, Euler characteristic %d, %s, %s%s\n", mesh->nedges, mesh->euler, mesh->closed ? "closed" : "OPEN", mesh->oriented ? "consistently oriented" : "NOT consistently oriented", mesh->flipped ? " (orientation reversed to outward normals)" : ""); - if (!mesh->closed) { + if (mesh->nboundary || mesh->nnonmanifold) { printf (" Warning: %d boundary and %d non-manifold edges: the mesh does not enclose a volume.\n", mesh->nboundary, mesh->nnonmanifold); } if (mesh->nfolded) { diff --git a/common/lib/share/geometry-mesh-lib.h b/common/lib/share/geometry-mesh-lib.h index 50f3fcb52d..9e3455bbbf 100644 --- a/common/lib/share/geometry-mesh-lib.h +++ b/common/lib/share/geometry-mesh-lib.h @@ -35,7 +35,7 @@ * boundary / non-manifold edges, Euler characteristic, consistent * orientation. Closed, consistently oriented meshes are oriented with * outward normals. Edges where the surface folds back onto itself are - * counted (nfolded); gmesh_check_closed rejects such a mesh. + * counted (nfolded); a folded mesh is not closed. * - builds a bounding volume hierarchy (BVH) so that a ray costs * O(log N) instead of O(N) triangle tests, without any allocation per ray. * @@ -44,6 +44,7 @@ * straight line, sorted in t * gmesh_intersect_gravity() the same for a parabola (e.g. under gravity) * gmesh_inside() point-in-mesh test (deterministic, no random numbers) + * gmesh_entry_exit() first entry and exit of a line (only exit from inside) * gmesh_check_closed() optional check that a mesh is closed and not folded * Hits exactly on an edge or vertex shared by several triangles are * reported once. @@ -85,7 +86,7 @@ #endif typedef struct gmesh_hit { - double time; // position along the line: pos + time*dir + double t; // position along the line: pos + t*dir Coords normal; // unit normal of the triangle hit (outward for closed oriented meshes) int tri; // triangle index (BVH order) int face; // index of the face (polygon) in the file, 0-based @@ -131,7 +132,7 @@ typedef struct gmesh_struct { int nfolded; // edges where the surface folds back onto itself (see gmesh_check_closed) int folded_face[2]; // faces of the first folded edge, 0-based int euler; // V - E + F (2 for a closed genus-0 surface) - int closed; // every edge shared by exactly two triangles; after gmesh_check_closed, also not folded + int closed; // every edge shared by exactly two triangles, and not folded int oriented; // shared edges run in opposite directions int flipped; // orientation was reversed to make normals point outward char* filename; @@ -153,10 +154,10 @@ int gmesh_init (char* filename, double scale, int face_props, gmesh_struct* mesh // int gmesh_init_fit(char *filename, double xwidth, double yheight, double zdepth, // int center, int face_props, gmesh_struct *mesh) -// ACTION: as gmesh_init, then resize the bounding box as off_init of interoff-lib: +// ACTION: as gmesh_init, then resize the bounding box about its centre: // non-zero sizes set the box size along x/y/z; a single size scales all // axes by the same factor (proportions kept). With center, the box centre -// is moved to the origin; otherwise coordinates are scaled about the origin. +// is moved to the origin; otherwise it stays in place. // RETURN: number of triangles, 0 on error int gmesh_init_fit (char* filename, double xwidth, double yheight, double zdepth, int center, int face_props, gmesh_struct* mesh); @@ -184,21 +185,30 @@ int gmesh_intersect (gmesh_struct* mesh, Coords position, Coords direction, doub int gmesh_intersect_gravity (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, double t_min, gmesh_hit hits[GMESH_MAX_HITS]); // int gmesh_inside(gmesh_struct *mesh, Coords position) - // ACTION: point in closed mesh test; 0 for an open mesh. For a closed, - // oriented mesh: whether the first hit along a fixed direction is leaving; - // otherwise the parity of ray crossings along fixed directions. Does not - // use random numbers. + // ACTION: point in (closed) mesh test, parity of ray crossings along fixed + // directions. Does not use random numbers. // RETURN: 1 inside, 0 outside #pragma acc routine seq int gmesh_inside (gmesh_struct* mesh, Coords position); + // int gmesh_entry_exit(gmesh_struct *mesh, Coords position, Coords velocity, + // double *t_in, double *t_out, gmesh_hit *first) + // ACTION: path of the line position + t*velocity (t > 0) through a closed + // mesh. Inside: t_in = 0 and t_out is the first hit. Outside: t_in is the + // first hit and t_out the next one (the first when it is the only one). + // Hit times are 1e-13 s past the surface, so that a particle moved there is + // on the far side and does not hit that surface again. An open mesh counts + // as outside. first, when not NULL, gets the first hit. + // RETURN: 0 when the mesh is not ahead, 1 inside, 2 outside + #pragma acc routine seq +int gmesh_entry_exit (gmesh_struct* mesh, Coords position, Coords velocity, double* t_in, double* t_out, gmesh_hit* first); + // int gmesh_check_closed(gmesh_struct *mesh) // ACTION: optional check for components that need a volume: the mesh must be // closed (every edge shared by two triangles) and must not fold back onto // itself (no edge whose two triangles turn back onto each other, with // antiparallel normals), as then inside and outside are not defined. - // Prints the reason when the mesh does not pass. A folded mesh gets - // mesh->closed = 0, so that afterwards mesh->closed holds the result. + // Prints the reason when the mesh does not pass. // RETURN: 1 when the mesh passes, 0 otherwise int gmesh_check_closed (gmesh_struct* mesh); diff --git a/mcstas-comps/contrib/Lens.comp b/mcstas-comps/contrib/Lens.comp index ac49c50ae7..5eb75b1ddb 100644 --- a/mcstas-comps/contrib/Lens.comp +++ b/mcstas-comps/contrib/Lens.comp @@ -125,6 +125,20 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" + // time to the next surface of mesh (t > 1e-13 s, which skips the surface the + // neutron sits on after a refraction or reflection), -1 when there is none. + // normal is oriented as in interoff-lib: opposite to the right-hand rule of + // the vertex order in the file, unless the mesh was flipped. + #pragma acc routine seq + double + Lens_mesh_hit (gmesh_struct* mesh, Coords position, Coords velocity, Coords* normal) { + gmesh_hit hits[GMESH_MAX_HITS]; + if (!gmesh_intersect (mesh, position, velocity, 1e-13, hits)) + return -1; + *normal = coords_scale (hits[0].normal, mesh->flipped ? 1 : -1); + return hits[0].t; + } + /* Lens_roughness: function to rotate normal vector around axis for roughness * with specified tilt angle (deg) * RETURNS: rotated nx,ny,nz coordinates @@ -239,10 +253,14 @@ INITIALIZE exit (-1); #else /* init the OFF/PLY without centering and scaling */ - if (!gmesh_init_fit (geometry, 0, 0, 0, 0, GMESH_PROPS_IGNORE, &mesh)) - exit (printf ("Lens: %s: FATAL: could not initialize geometry file %s [OFF/PLY]\n", NAME_CURRENT_COMP, geometry)); - if (!gmesh_check_closed (&mesh)) - exit (fprintf (stderr, "Lens: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); + if (!gmesh_init_fit (geometry, 0, 0, 0, 0, GMESH_PROPS_IGNORE, &mesh)) { + printf ("Lens: %s: FATAL: could not initialize geometry file %s [OFF/PLY]\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } + if (!gmesh_check_closed (&mesh)) { + fprintf (stderr, "Lens: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } #endif } @@ -281,15 +299,9 @@ TRACE nx = ny = nz = 0; #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - gmesh_hit hits[GMESH_MAX_HITS]; - // t_min = 1e-13 s skips the surface the neutron sits on after a refraction or reflection - if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 1e-13, hits)) { - dt = hits[0].time; - // normal oriented as in interoff-lib: opposite to the right-hand rule of - // the vertex order in the file, unless the mesh was flipped - coords_get (coords_scale (hits[0].normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); - } else - dt = -1; + Coords n = coords_set (0, 0, 0); + dt = Lens_mesh_hit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &n); + coords_get (n, &nx, &ny, &nz); } else #endif if (r1 && !focus1) { @@ -379,15 +391,9 @@ TRACE nx = ny = nz = 0; #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - gmesh_hit hits[GMESH_MAX_HITS]; - // t_min = 1e-13 s skips the surface the neutron sits on after a refraction or reflection - if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 1e-13, hits)) { - dt = hits[0].time; - // normal oriented as in interoff-lib: opposite to the right-hand rule of - // the vertex order in the file, unless the mesh was flipped - coords_get (coords_scale (hits[0].normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); - } else - dt = -1; + Coords n = coords_set (0, 0, 0); + dt = Lens_mesh_hit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &n); + coords_get (n, &nx, &ny, &nz); } else #endif if (r2 && !focus2) @@ -490,15 +496,9 @@ TRACE nx = ny = nz = 0; #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - gmesh_hit hits[GMESH_MAX_HITS]; - // t_min = 1e-13 s skips the surface the neutron sits on after a refraction or reflection - if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 1e-13, hits)) { - dt = hits[0].time; - // normal oriented as in interoff-lib: opposite to the right-hand rule of - // the vertex order in the file, unless the mesh was flipped - coords_get (coords_scale (hits[0].normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); - } else - dt = -1; + Coords n = coords_set (0, 0, 0); + dt = Lens_mesh_hit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &n); + coords_get (n, &nx, &ny, &nz); } else #endif if (r2 && !focus2) diff --git a/mcstas-comps/contrib/Single_crystal_inelastic.comp b/mcstas-comps/contrib/Single_crystal_inelastic.comp index 358b696960..6b1487e118 100644 --- a/mcstas-comps/contrib/Single_crystal_inelastic.comp +++ b/mcstas-comps/contrib/Single_crystal_inelastic.comp @@ -720,11 +720,15 @@ INITIALIZE if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) hkl_info.shape = 3; - else - exit (fprintf (stderr, "Single_crystal_inelastic: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); - if (!gmesh_check_closed (&mesh)) - exit (fprintf (stderr, "Single_crystal_inelastic: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", - NAME_CURRENT_COMP, geometry)); + else { + fprintf (stderr, "Single_crystal_inelastic: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } + if (!gmesh_check_closed (&mesh)) { + fprintf (stderr, "Single_crystal_inelastic: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", + NAME_CURRENT_COMP, geometry); + exit (-1); + } } else if (xwidth && yheight && zdepth) hkl_info.shape = 1; /* box */ else if (radius > 0 && yheight) @@ -804,21 +808,8 @@ TRACE intersect = box_intersect (&t1, &t2, x, y, z, vx, vy, vz, xwidth, yheight, zdepth); else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); - else if (hkl_info.shape == 3) { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); - intersect = nhits > 0; - if (intersect) { - if (gmesh_inside (&mesh, coords_set (x, y, z))) { - t1 = 0; - t2 = hits[0].time; - } else { - // 1e-13 s past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) - t1 = hits[0].time + 1e-13; - t2 = nhits > 1 ? hits[1].time : hits[0].time; - } - } - } + else if (hkl_info.shape == 3) + intersect = gmesh_entry_exit (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL); if (t2 < 0) intersect = 0; /* we passed sample volume already */ @@ -851,15 +842,8 @@ TRACE intersect = box_intersect (&t1, &t2, x, y, z, vx, vy, vz, xwidth, yheight, zdepth); else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); - else if (hkl_info.shape == 3) { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); - intersect = nhits > 0 && gmesh_inside (&mesh, coords_set (x, y, z)); - if (intersect) { - t1 = 0; - t2 = hits[0].time; - } - } + else if (hkl_info.shape == 3) + intersect = gmesh_entry_exit (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL) == 1; if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { /* neutron is leaving the sample */ if (hkl_info.flag_warning < 100) diff --git a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr index a23d14571d..98bf67ebb1 100644 --- a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr +++ b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr @@ -6,13 +6,16 @@ * * Smoke test: builds and runs with the library geometry-mesh-lib in the instrument. * Fails to compile or link if the library does not build with the runtime. -* INITIALIZE also checks gmesh_inside and gmesh_intersect_gravity, and -* exits with an error when a check fails: -* - gmesh_init counts the folded edges of a folded (closed, oriented) cube, and -* gmesh_check_closed fails it and the open Guide.off, and passes cube.off -* and socket.off -* - gmesh_inside on cube.off, and its shortcut for closed, oriented meshes -* against the parity of ray crossings for random points around socket.off +* INITIALIZE also checks gmesh_init_fit, gmesh_inside, gmesh_entry_exit and +* gmesh_intersect_gravity, and exits with an error when a check fails: +* - gmesh_init_fit resizes cube.off about its box centre, which stays in place +* with center=0 and is moved to the origin with center=1 +* - gmesh_init counts the folded edges of a folded cube, which is then not +* closed, and gmesh_check_closed fails it and the open Guide.off, and passes +* cube.off and socket.off +* - gmesh_inside on cube.off +* - gmesh_entry_exit on cube.off, and its inside/outside from the first hit +* against gmesh_inside for random points and directions around socket.off * - with zero acceleration it gives the hits of gmesh_intersect, to rounding * - parabolas through cube.off give the analytic hit times and in_out * - random parabolas through socket.off give the hits of a fine polyline of @@ -47,11 +50,11 @@ double gravity_uniform (unsigned long* state, double low, double high) { return low + (high - low) * (double)(*state >> 11) / 9007199254740992.0; } -// checks of gmesh_inside; returns the number of failed checks +// checks of gmesh_init_fit, gmesh_inside and gmesh_entry_exit; returns the +// number of failed checks int inside_tests (void) { gmesh_struct cube; gmesh_struct socket; - gmesh_struct socket_parity; unsigned long state = 4321; int failures = 0; int differ = 0; @@ -61,9 +64,21 @@ int inside_tests (void) { return 1; } mesh_check (gmesh_inside (&cube, coords_set (0.5, 0.5, 0.5)) == 1, "inside the cube", &failures); + // the unit cube [0, 1]^3 resized to a side of 2 about its centre (0.5, 0.5, 0.5) + { + gmesh_struct fit; + mesh_check (gmesh_init_fit ("cube.off", 2, 0, 0, 0, GMESH_PROPS_IGNORE, &fit) && fabs (fit.bbmin.x + 0.5) < 1e-12 && fabs (fit.bbmax.y - 1.5) < 1e-12 + && fabs (fit.bbmax.z - 1.5) < 1e-12, + "gmesh_init_fit with center=0 keeps the box centre", &failures); + gmesh_free (&fit); + mesh_check (gmesh_init_fit ("cube.off", 2, 0, 0, 1, GMESH_PROPS_IGNORE, &fit) && fabs (fit.bbmin.x + 1) < 1e-12 && fabs (fit.bbmax.y - 1) < 1e-12 + && fabs (fit.bbmax.z - 1) < 1e-12, + "gmesh_init_fit with center=1 moves the box centre to the origin", &failures); + gmesh_free (&fit); + } // a folded mesh: a unit cube whose bottom reaches past the edge x = 1 to - // (1.5, 0.5, 0), where triangle 2 1 8 folds back. It is closed and - // consistently oriented, but overlaps itself at 2 edges. + // (1.5, 0.5, 0), where triangle 2 1 8 folds back. It has no boundary edges + // and is consistently oriented, but overlaps itself at 2 edges. { gmesh_struct folded; // one file per MPI process, as all processes run these checks @@ -77,25 +92,41 @@ int inside_tests (void) { fprintf (file, "OFF\n9 9 0\n0 0 0\n1 0 0\n1 1 0\n0 1 0\n0 0 1\n1 0 1\n1 1 1\n0 1 1\n1.5 0.5 0\n" "4 4 5 6 7\n4 0 1 5 4\n4 1 2 6 5\n4 2 3 7 6\n4 0 4 7 3\n3 1 0 8\n3 0 3 8\n3 3 2 8\n3 2 1 8\n"); fclose (file); - mesh_check (gmesh_init (folded_file, 1, GMESH_PROPS_IGNORE, &folded) && folded.closed && folded.oriented - && folded.nfolded == 2 && !gmesh_check_closed (&folded) && !folded.closed, - "folded mesh: 2 folded edges, fails gmesh_check_closed and is then not closed", &failures); + mesh_check (gmesh_init (folded_file, 1, GMESH_PROPS_IGNORE, &folded) && !folded.nboundary && !folded.nnonmanifold && folded.oriented + && folded.nfolded == 2 && !folded.closed && !gmesh_check_closed (&folded), + "folded mesh: 2 folded edges, not closed, fails gmesh_check_closed", &failures); gmesh_free (&folded); remove (folded_file); } } mesh_check (gmesh_inside (&cube, coords_set (0.5, 0.5, 1.5)) == 0, "outside the cube", &failures); - // the shortcut for closed, oriented meshes against the parity of ray crossings - socket_parity = socket; - socket_parity.oriented = 0; + // entry and exit, 1e-13 s past the surfaces + { + double t_in = -1; + double t_out = -1; + gmesh_hit first; + mesh_check (gmesh_entry_exit (&cube, coords_set (0.5, 0.5, -1), coords_set (0, 0, 1), &t_in, &t_out, &first) == 2 && fabs (t_in - 1 - 1e-13) < 1e-15 + && fabs (t_out - 2 - 1e-13) < 1e-15 && first.in_out == 1, + "gmesh_entry_exit from outside cube.off", &failures); + mesh_check (gmesh_entry_exit (&cube, coords_set (0.5, 0.5, 0.5), coords_set (0, 0, 1), &t_in, &t_out, NULL) == 1 && t_in == 0 + && fabs (t_out - 0.5 - 1e-13) < 1e-15, + "gmesh_entry_exit from inside cube.off", &failures); + mesh_check (gmesh_entry_exit (&cube, coords_set (0.5, 0.5, 2), coords_set (0, 0, 1), &t_in, &t_out, NULL) == 0, "gmesh_entry_exit past cube.off", + &failures); + } + // inside from the first hit against the parity of ray crossings for (i_point = 0; i_point < 2000; i_point++) { Coords point = coords_set (gravity_uniform (&state, socket.bbmin.x, socket.bbmax.x), gravity_uniform (&state, socket.bbmin.y, socket.bbmax.y), gravity_uniform (&state, socket.bbmin.z, socket.bbmax.z)); - if (gmesh_inside (&socket, point) != gmesh_inside (&socket_parity, point)) + Coords direction = coords_set (gravity_uniform (&state, -1, 1), gravity_uniform (&state, -1, 1), gravity_uniform (&state, -1, 1)); + double t_in; + double t_out; + int side = gmesh_entry_exit (&socket, point, direction, &t_in, &t_out, NULL); + if (side && (side == 1) != gmesh_inside (&socket, point)) differ++; } - printf ("Test_lib_geometry_mesh_lib: socket.off: gmesh_inside shortcut and parity differ for %d of 2000 points\n", differ); - mesh_check (socket.closed && socket.oriented && differ == 0, "gmesh_inside shortcut against parity on socket.off", &failures); + printf ("Test_lib_geometry_mesh_lib: socket.off: gmesh_entry_exit and gmesh_inside differ for %d of 2000 points\n", differ); + mesh_check (socket.closed && socket.oriented && differ == 0, "gmesh_entry_exit against gmesh_inside on socket.off", &failures); mesh_check (gmesh_check_closed (&cube) && gmesh_check_closed (&socket), "gmesh_check_closed on cube.off and socket.off", &failures); { gmesh_struct guide; @@ -128,18 +159,18 @@ int gravity_tests (void) { // falling from rest above the unit cube: top at time sqrt(2), bottom at 2 nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, 2, 0.5), coords_set (0, 0, 0), coords_set (0, -1, 0), 0, hits); - mesh_check (nhits == 2 && fabs (hits[0].time - sqrt (2)) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].time - 2) < 1e-12 && hits[1].in_out == -1, + mesh_check (nhits == 2 && fabs (hits[0].t - sqrt (2)) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].t - 2) < 1e-12 && hits[1].in_out == -1, "fall from rest through cube.off", &failures); // entering through the front face, leaving through the bottom at time sqrt(2) nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, 0.5, -1), coords_set (0, 0, 1), coords_set (0, -0.5, 0), 0, hits); - mesh_check (nhits == 2 && fabs (hits[0].time - 1) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].time - sqrt (2)) < 1e-12 && hits[1].in_out == -1, + mesh_check (nhits == 2 && fabs (hits[0].t - 1) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].t - sqrt (2)) < 1e-12 && hits[1].in_out == -1, "front face in, bottom out of cube.off", &failures); // thrown up into the cube from below, falling out through the same face nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, -1, 0.5), coords_set (0, 2, 0), coords_set (0, -1.2, 0), 0, hits); - mesh_check (nhits == 2 && fabs (hits[0].time - (2 - sqrt (1.6)) / 1.2) < 1e-12 && hits[0].in_out == 1 - && fabs (hits[1].time - (2 + sqrt (1.6)) / 1.2) < 1e-12 && hits[1].in_out == -1 && hits[0].face == hits[1].face, + mesh_check (nhits == 2 && fabs (hits[0].t - (2 - sqrt (1.6)) / 1.2) < 1e-12 && hits[0].in_out == 1 + && fabs (hits[1].t - (2 + sqrt (1.6)) / 1.2) < 1e-12 && hits[1].in_out == -1 && hits[0].face == hits[1].face, "in and out through the bottom of cube.off", &failures); // t_min skips the hits before it @@ -171,7 +202,7 @@ int gravity_tests (void) { continue; } for (i_hit = 0; i_hit < nhits; i_hit++) { - if (fabs (hits[i_hit].time - line_hits[i_hit].time) > 1e-12 * (1 + fabs (line_hits[i_hit].time)) || hits[i_hit].in_out != line_hits[i_hit].in_out) + if (fabs (hits[i_hit].t - line_hits[i_hit].t) > 1e-12 * (1 + fabs (line_hits[i_hit].t)) || hits[i_hit].in_out != line_hits[i_hit].in_out) mismatches++; } @@ -181,21 +212,21 @@ int gravity_tests (void) { Coords chord = coords_sub (gravity_parabola (position, velocity, acceleration, (i_step + 1) * step), start); int npiece = gmesh_intersect (&socket, start, chord, 0, line_hits); int i_piece; - for (i_piece = 0; i_piece < npiece && line_hits[i_piece].time <= 1 && nline < GMESH_MAX_HITS; i_piece++) { - line_times[nline] = (i_step + line_hits[i_piece].time) * step; + for (i_piece = 0; i_piece < npiece && line_hits[i_piece].t <= 1 && nline < GMESH_MAX_HITS; i_piece++) { + line_times[nline] = (i_step + line_hits[i_piece].t) * step; nline++; } } nhits = gmesh_intersect_gravity (&socket, position, velocity, acceleration, 0, hits); - while (nhits > 0 && hits[nhits - 1].time > time_end) + while (nhits > 0 && hits[nhits - 1].t > time_end) nhits--; if (nhits != nline) { mismatches++; continue; } for (i_hit = 0; i_hit < nhits; i_hit++) { - if (fabs (hits[i_hit].time - line_times[i_hit]) > worst) - worst = fabs (hits[i_hit].time - line_times[i_hit]); + if (fabs (hits[i_hit].t - line_times[i_hit]) > worst) + worst = fabs (hits[i_hit].t - line_times[i_hit]); } } printf ("Test_lib_geometry_mesh_lib: socket.off: %d of 2000 random rays differ from the line or polyline, worst time difference %g s\n", mismatches, @@ -211,8 +242,10 @@ int gravity_tests (void) { INITIALIZE %{ mcset_ncount(Ncount); - if (inside_tests () + gravity_tests ()) - exit (fprintf (stderr, "Test_lib_geometry_mesh_lib: geometry-mesh-lib checks failed\n")); + if (inside_tests () + gravity_tests ()) { + fprintf (stderr, "Test_lib_geometry_mesh_lib: geometry-mesh-lib checks failed\n"); + exit (-1); + } %} TRACE diff --git a/mcstas-comps/misc/Shape.comp b/mcstas-comps/misc/Shape.comp index bea2af36f8..ade46a17d7 100644 --- a/mcstas-comps/misc/Shape.comp +++ b/mcstas-comps/misc/Shape.comp @@ -82,8 +82,10 @@ INITIALIZE if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_IGNORE, &mesh)) thickness = 0; - else - exit (fprintf (stderr, "Shape: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); + else { + fprintf (stderr, "Shape: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } } %} diff --git a/mcstas-comps/misc/Shape_simple.comp b/mcstas-comps/misc/Shape_simple.comp index bf485e1137..c2ac79fc25 100644 --- a/mcstas-comps/misc/Shape_simple.comp +++ b/mcstas-comps/misc/Shape_simple.comp @@ -80,8 +80,10 @@ INITIALIZE if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_IGNORE, &mesh)) thickness = 0; - else - exit (fprintf (stderr, "Shape_simple: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); + else { + fprintf (stderr, "Shape_simple: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } } %} diff --git a/mcstas-comps/monitors/Monitor_nD.comp b/mcstas-comps/monitors/Monitor_nD.comp index 2c0f212e3f..d635499bab 100644 --- a/mcstas-comps/monitors/Monitor_nD.comp +++ b/mcstas-comps/monitors/Monitor_nD.comp @@ -521,11 +521,11 @@ TRACE #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "0") && strcmp (geometry, "NULL")) { /* determine intersections with object */ - int face = 0; - intersect = Monitor_nD_MeshIntersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &face); + gmesh_hit first; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &first); if (Vars.Flag_mantid) { if (intersect) { - Vars.OFF_polyidx = face; + Vars.OFF_polyidx = first.face; } else { Vars.OFF_polyidx = -1; } diff --git a/mcstas-comps/monitors/Monitor_nD_noacc.comp b/mcstas-comps/monitors/Monitor_nD_noacc.comp index 157eac7c1d..fd9fa13076 100644 --- a/mcstas-comps/monitors/Monitor_nD_noacc.comp +++ b/mcstas-comps/monitors/Monitor_nD_noacc.comp @@ -516,11 +516,11 @@ TRACE */ if (geometry && strlen (geometry) && strcmp (geometry, "0") && strcmp (geometry, "NULL")) { /* determine intersections with object */ - int face = 0; - intersect = Monitor_nD_MeshIntersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &face); + gmesh_hit first; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &first); if (Vars.Flag_mantid) { if (intersect) { - Vars.OFF_polyidx = face; + Vars.OFF_polyidx = first.face; } else { Vars.OFF_polyidx = -1; } diff --git a/mcstas-comps/obsolete/Guide_anyshape_r.comp b/mcstas-comps/obsolete/Guide_anyshape_r.comp index b2775e6150..223f44e381 100644 --- a/mcstas-comps/obsolete/Guide_anyshape_r.comp +++ b/mcstas-comps/obsolete/Guide_anyshape_r.comp @@ -97,6 +97,19 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" %include "ref-lib" + +// next hit of the parabola position + t*velocity + t^2/2*acceleration with +// mesh (t > 0): its time, 0 when there is none, with its normal and face +#pragma acc routine seq +double +Guide_anyshape_r_hit (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, Coords* normal, int* face) { + gmesh_hit hits[GMESH_MAX_HITS]; + if (!gmesh_intersect_gravity (mesh, position, velocity, acceleration, 0, hits)) + return 0; + *normal = hits[0].normal; + *face = hits[0].face; + return hits[0].t; +} %} DECLARE @@ -142,20 +155,17 @@ TRACE double dt = 0; int fi = 0; Coords n = { 0, 0, 0 }; - gmesh_hit hits[GMESH_MAX_HITS]; Coords acceleration = coords_set (0, 0, 0); if (mcgravitation) acceleration = rot_apply (_comp->_rotation_absolute, coords_set (0, -GRAVITY, 0)); // next intersection with the object, along the parabola under gravity; PL: with the index of the face - intersect = gmesh_intersect_gravity (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, 0, hits); + dt = Guide_anyshape_r_hit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, &n, &fi); + intersect = dt > 0; /* exit loop when no intersection forward */ if (!intersect) break; - dt = hits[0].time; - n = hits[0].normal; - fi = hits[0].face; double nx, ny, nz; coords_get (n, &nx, &ny, &nz); diff --git a/mcstas-comps/optics/Guide_anyshape.comp b/mcstas-comps/optics/Guide_anyshape.comp index 1fa7979b13..847e7909f2 100644 --- a/mcstas-comps/optics/Guide_anyshape.comp +++ b/mcstas-comps/optics/Guide_anyshape.comp @@ -101,6 +101,21 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" %include "ref-lib" +// per-face coating properties in the geometry file: nv i1 ... inv m alpha W [R0 [Qc]] +enum { GUIDE_ANYSHAPE_M, GUIDE_ANYSHAPE_ALPHA, GUIDE_ANYSHAPE_W, GUIDE_ANYSHAPE_R0, GUIDE_ANYSHAPE_QC }; + +// next hit of the parabola position + t*velocity + t^2/2*acceleration with +// mesh (t > 0): its time, 0 when there is none, with its normal and face +#pragma acc routine seq +double +Guide_anyshape_hit (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, Coords* normal, int* face) { + gmesh_hit hits[GMESH_MAX_HITS]; + if (!gmesh_intersect_gravity (mesh, position, velocity, acceleration, 0, hits)) + return 0; + *normal = hits[0].normal; + *face = hits[0].face; + return hits[0].t; +} %} DECLARE @@ -129,13 +144,15 @@ INITIALIZE if (file_coatings) { if (use_table) exit (fprintf (stderr, "Guide_anyshape: %s: file_coatings=1 and reflect=%s can not be used together. Aborting.\n", NAME_CURRENT_COMP, reflect)); - if (mesh.nfaceprops < 3) - exit (fprintf (stderr, - "Guide_anyshape: %s: file_coatings=1 but %s does not give m, alpha, W for each face\n" - " (expected lines 'nv i1 ... inv m alpha W [R0 [Qc]]'). Aborting.\n", - NAME_CURRENT_COMP, geometry)); - MPI_MASTER (printf ("Guide_anyshape: %s: using per-face m, alpha, W%s%s from %s\n", NAME_CURRENT_COMP, mesh.nfaceprops > 3 ? ", R0" : "", - mesh.nfaceprops > 4 ? ", Qc" : "", geometry);); + if (mesh.nfaceprops <= GUIDE_ANYSHAPE_W) { + fprintf (stderr, + "Guide_anyshape: %s: file_coatings=1 but %s does not give m, alpha, W for each face\n" + " (expected lines 'nv i1 ... inv m alpha W [R0 [Qc]]'). Aborting.\n", + NAME_CURRENT_COMP, geometry); + exit (-1); + } + MPI_MASTER (printf ("Guide_anyshape: %s: using per-face m, alpha, W%s%s from %s\n", NAME_CURRENT_COMP, mesh.nfaceprops > GUIDE_ANYSHAPE_R0 ? ", R0" : "", + mesh.nfaceprops > GUIDE_ANYSHAPE_QC ? ", Qc" : "", geometry);); } %} @@ -153,20 +170,17 @@ TRACE double dt = 0; int fi = 0; Coords n = { 0, 0, 0 }; - gmesh_hit hits[GMESH_MAX_HITS]; Coords acceleration = coords_set (0, 0, 0); if (mcgravitation) acceleration = rot_apply (_comp->_rotation_absolute, coords_set (0, -GRAVITY, 0)); // next intersection with the object, along the parabola under gravity - intersect = gmesh_intersect_gravity (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, 0, hits); + dt = Guide_anyshape_hit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, &n, &fi); + intersect = dt > 0; /* exit loop when no intersection forward */ if (!intersect) break; - dt = hits[0].time; - n = hits[0].normal; - fi = hits[0].face; double nx, ny, nz; coords_get (n, &nx, &ny, &nz); @@ -186,12 +200,12 @@ TRACE TableReflecFunc (q, &pTable, &R); else { double par[] = { R0, Qc, alpha, m, W }; - if (file_coatings) { // per-face values m, alpha, W, R0, Qc (properties 0-4), parameters as defaults - par[0] = gmesh_face_prop (&thread_mesh, fi, 3, R0); - par[1] = gmesh_face_prop (&thread_mesh, fi, 4, Qc); - par[2] = gmesh_face_prop (&thread_mesh, fi, 1, alpha); - par[3] = gmesh_face_prop (&thread_mesh, fi, 0, m); - par[4] = gmesh_face_prop (&thread_mesh, fi, 2, W); + if (file_coatings) { /* per-face values, parameters as defaults */ + par[0] = gmesh_face_prop (&thread_mesh, fi, GUIDE_ANYSHAPE_R0, R0); + par[1] = gmesh_face_prop (&thread_mesh, fi, GUIDE_ANYSHAPE_QC, Qc); + par[2] = gmesh_face_prop (&thread_mesh, fi, GUIDE_ANYSHAPE_ALPHA, alpha); + par[3] = gmesh_face_prop (&thread_mesh, fi, GUIDE_ANYSHAPE_M, m); + par[4] = gmesh_face_prop (&thread_mesh, fi, GUIDE_ANYSHAPE_W, W); } StdReflecFunc (q, par, &R); } diff --git a/mcstas-comps/optics/Refractor.comp b/mcstas-comps/optics/Refractor.comp index 02eb9c27db..ebb43ee641 100644 --- a/mcstas-comps/optics/Refractor.comp +++ b/mcstas-comps/optics/Refractor.comp @@ -348,11 +348,14 @@ INITIALIZE exit (-1); #else /* off/ply case */ - if (!gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) - exit (printf ("Refractor: %s: FATAL: invalid OFF/PLY geometry specification for file '%s'.\n", NAME_CURRENT_COMP, geometry)); - if (!gmesh_check_closed (&mesh)) - exit ( - fprintf (stderr, "Refractor: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); + if (!gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { + printf ("Refractor: %s: FATAL: invalid OFF/PLY geometry specification for file '%s'.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } + if (!gmesh_check_closed (&mesh)) { + fprintf (stderr, "Refractor: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } #endif } else exit (printf ("Refractor: %s: FATAL: invalid geometry specification.\n" @@ -413,18 +416,13 @@ TRACE } #ifdef USE_OFF else if (geometry && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); - intersect = nhits > 0; - if (intersect) { - mesh_inside = gmesh_inside (&thread_mesh, coords_set (x, y, z)); - // 1e-13 s past the surfaces, so that the next intersection does not find them again - t0 = mesh_inside ? 0 : hits[0].time + 1e-13; - t1 = (mesh_inside || nhits == 1 ? hits[0].time : hits[1].time) + 1e-13; - // normal of the next hit, oriented as in interoff-lib: opposite to the - // right-hand rule of the vertex order in the file, unless the mesh was flipped - coords_get (coords_scale (hits[0].normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); - } + gmesh_hit first; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &first); + mesh_inside = intersect == 1; + // normal of the next hit, oriented as in interoff-lib: opposite to the + // right-hand rule of the vertex order in the file, unless the mesh was flipped + if (intersect) + coords_get (coords_scale (first.normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); } #endif diff --git a/mcstas-comps/samples/Incoherent.comp b/mcstas-comps/samples/Incoherent.comp index 99ae4d2f2d..3a7c1ba436 100644 --- a/mcstas-comps/samples/Incoherent.comp +++ b/mcstas-comps/samples/Incoherent.comp @@ -163,11 +163,14 @@ INITIALIZE VarsInc.shape = 3; thickness = 0; concentric = 0; - } else - exit (fprintf (stderr, "Incoherent: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); - if (!gmesh_check_closed (&mesh)) - exit (fprintf (stderr, "Incoherent: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, - geometry)); + } else { + fprintf (stderr, "Incoherent: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } + if (!gmesh_check_closed (&mesh)) { + fprintf (stderr, "Incoherent: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } #endif } else if (xwidth && yheight && zdepth) VarsInc.shape = 1; /* box */ @@ -286,20 +289,8 @@ TRACE else if (VarsInc.shape == 2) intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF - else if (VarsInc.shape == 3) { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); - intersect = nhits > 0; - if (intersect) { - if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { - t0 = 0; - t3 = hits[0].time; - } else { - t0 = hits[0].time; - t3 = nhits > 1 ? hits[1].time : hits[0].time; - } - } - } + else if (VarsInc.shape == 3) + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL); #endif if (intersect) { @@ -495,15 +486,8 @@ TRACE else if (VarsInc.shape == 2) intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF - else if (VarsInc.shape == 3) { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); - intersect = nhits > 0; - if (intersect) { - t0 = 0; - t3 = hits[0].time; - } - } + else if (VarsInc.shape == 3) + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL); #endif flag_ishollow = 0; diff --git a/mcstas-comps/samples/Isotropic_Sqw.comp b/mcstas-comps/samples/Isotropic_Sqw.comp index 381628beaa..377441716e 100644 --- a/mcstas-comps/samples/Isotropic_Sqw.comp +++ b/mcstas-comps/samples/Isotropic_Sqw.comp @@ -2034,11 +2034,14 @@ INITIALIZE VarSqw.shape = 3; thickness = 0; concentric = 0; - } else - exit (fprintf (stderr, "Isotropic_Sqw: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); - if (!gmesh_check_closed (&mesh)) - exit (fprintf (stderr, "Isotropic_Sqw: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, - geometry)); + } else { + fprintf (stderr, "Isotropic_Sqw: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } + if (!gmesh_check_closed (&mesh)) { + fprintf (stderr, "Isotropic_Sqw: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } #endif } else if (xwidth && yheight && zdepth) VarSqw.shape = 1; /* box */ @@ -2252,40 +2255,31 @@ TRACE /* Intersection neutron trajectory / sample (sample surface) */ if (VarSqw.s_coh > 0 || VarSqw.s_inc > 0) { - #ifdef USE_OFF - // a mesh is used as given, for either sign of thickness - if (VarSqw.shape == 3) { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); - intersect = nhits > 0; - if (intersect) { - if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { - t0 = 0; - t3 = hits[0].time; - } else { - t0 = hits[0].time; - t3 = nhits > 1 ? hits[1].time : hits[0].time; - } - } - } else + if (thickness >= 0) { + if (VarSqw.shape == 0) + intersect = cylinder_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius, yheight); + else if (VarSqw.shape == 1) + intersect = box_intersect (&t0, &t3, x, y, z, vx, vy, vz, xwidth, yheight, zdepth); + else if (VarSqw.shape == 2) + intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); + #ifdef USE_OFF + else if (VarSqw.shape == 3) + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL); #endif - if (thickness >= 0) { - if (VarSqw.shape == 0) - intersect = cylinder_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius, yheight); - else if (VarSqw.shape == 1) - intersect = box_intersect (&t0, &t3, x, y, z, vx, vy, vz, xwidth, yheight, zdepth); - else if (VarSqw.shape == 2) - intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); - } else { - if (VarSqw.shape == 0) - intersect = cylinder_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius - thickness, yheight - 2 * thickness > 0 ? yheight - 2 * thickness : yheight); - else if (VarSqw.shape == 1) - intersect - = box_intersect (&t0, &t3, x, y, z, vx, vy, vz, xwidth - 2 * thickness > 0 ? xwidth - 2 * thickness : xwidth, - yheight - 2 * thickness > 0 ? yheight - 2 * thickness : yheight, zdepth - 2 * thickness > 0 ? zdepth - 2 * thickness : zdepth); - else if (VarSqw.shape == 2) - intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius - thickness); - } + } else { + if (VarSqw.shape == 0) + intersect = cylinder_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius - thickness, yheight - 2 * thickness > 0 ? yheight - 2 * thickness : yheight); + else if (VarSqw.shape == 1) + intersect + = box_intersect (&t0, &t3, x, y, z, vx, vy, vz, xwidth - 2 * thickness > 0 ? xwidth - 2 * thickness : xwidth, + yheight - 2 * thickness > 0 ? yheight - 2 * thickness : yheight, zdepth - 2 * thickness > 0 ? zdepth - 2 * thickness : zdepth); + else if (VarSqw.shape == 2) + intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius - thickness); + #ifdef USE_OFF + else if (VarSqw.shape == 3) + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL); + #endif + } } else intersect = 0; diff --git a/mcstas-comps/samples/PowderN.comp b/mcstas-comps/samples/PowderN.comp index c2f62ac105..db09eacf4c 100644 --- a/mcstas-comps/samples/PowderN.comp +++ b/mcstas-comps/samples/PowderN.comp @@ -884,10 +884,14 @@ INITIALIZE line_info.shape = 3; thickness = 0; concentric = 0; - } else - exit (fprintf (stderr, "PowderN: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); - if (!gmesh_check_closed (&mesh)) - exit (fprintf (stderr, "PowderN: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry)); + } else { + fprintf (stderr, "PowderN: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } + if (!gmesh_check_closed (&mesh)) { + fprintf (stderr, "PowderN: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } #endif } else if (xwidth && yheight && zdepth) line_info.shape = 1; /* box */ @@ -1122,18 +1126,7 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); - intersect = nhits > 0; - if (intersect) { - if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { - t0 = 0; - t3 = hits[0].time; - } else { - t0 = hits[0].time; - t3 = nhits > 1 ? hits[1].time : hits[0].time; - } - } + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL); intersecti = 0; } #endif @@ -1328,13 +1321,7 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); - intersect = nhits > 0; - if (intersect) { - t0 = 0; - t3 = hits[0].time; - } + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL); intersecti = 0; } #endif @@ -1408,13 +1395,7 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); - intersect = nhits > 0; - if (intersect) { - t0 = 0; - t3 = hits[0].time; - } + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL); intersecti = 0; } #endif diff --git a/mcstas-comps/samples/Single_crystal.comp b/mcstas-comps/samples/Single_crystal.comp index 503fd439fd..d9c75ce116 100644 --- a/mcstas-comps/samples/Single_crystal.comp +++ b/mcstas-comps/samples/Single_crystal.comp @@ -1404,11 +1404,15 @@ INITIALIZE #else if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) hkl_info.shape = 3; - else - exit (fprintf (stderr, "Single_crystal: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); - if (!gmesh_check_closed (&mesh)) - exit (fprintf (stderr, "Single_crystal: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, - geometry)); + else { + fprintf (stderr, "Single_crystal: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } + if (!gmesh_check_closed (&mesh)) { + fprintf (stderr, "Single_crystal: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, + geometry); + exit (-1); + } #endif } else if (xwidth && yheight && zdepth) hkl_info.shape = 1; /* box */ @@ -1528,21 +1532,8 @@ TRACE else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF - else if (hkl_info.shape == 3) { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); - intersect = nhits > 0; - if (intersect) { - if (gmesh_inside (&thread_mesh, coords_set (x, y, z))) { - t1 = 0; - t2 = hits[0].time; - } else { - // 1e-13 s past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) - t1 = hits[0].time + 1e-13; - t2 = nhits > 1 ? hits[1].time : hits[0].time; - } - } - } + else if (hkl_info.shape == 3) + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL); #endif if (t2 < 0) intersect = 0; /* we passed sample volume already */ @@ -1589,15 +1580,8 @@ TRACE else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF - else if (hkl_info.shape == 3) { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); - intersect = nhits > 0 && gmesh_inside (&thread_mesh, coords_set (x, y, z)); - if (intersect) { - t1 = 0; - t2 = hits[0].time; - } - } + else if (hkl_info.shape == 3) + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL) == 1; #endif if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { /* neutron is leaving the sample */ diff --git a/mcstas-comps/samples/Single_magnetic_crystal.comp b/mcstas-comps/samples/Single_magnetic_crystal.comp index 2720fb04dd..ad7277227f 100644 --- a/mcstas-comps/samples/Single_magnetic_crystal.comp +++ b/mcstas-comps/samples/Single_magnetic_crystal.comp @@ -548,11 +548,15 @@ INITIALIZE if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) hkl_info.shape = 3; - else - exit (fprintf (stderr, "Single_magnetic_crystal: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry)); - if (!gmesh_check_closed (&mesh)) - exit (fprintf (stderr, "Single_magnetic_crystal: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", - NAME_CURRENT_COMP, geometry)); + else { + fprintf (stderr, "Single_magnetic_crystal: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } + if (!gmesh_check_closed (&mesh)) { + fprintf (stderr, "Single_magnetic_crystal: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", + NAME_CURRENT_COMP, geometry); + exit (-1); + } } else if (xwidth && yheight && zdepth) hkl_info.shape = 1; /* box */ else if (radius > 0 && yheight) @@ -621,21 +625,8 @@ TRACE intersect = box_intersect (&t1, &t2, x, y, z, vx, vy, vz, xwidth, yheight, zdepth); else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); - else if (hkl_info.shape == 3) { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); - intersect = nhits > 0; - if (intersect) { - if (gmesh_inside (&mesh, coords_set (x, y, z))) { - t1 = 0; - t2 = hits[0].time; - } else { - // 1e-13 s past the entry, so that gmesh_inside sees the neutron inside after PROP_DT (t1) - t1 = hits[0].time + 1e-13; - t2 = nhits > 1 ? hits[1].time : hits[0].time; - } - } - } + else if (hkl_info.shape == 3) + intersect = gmesh_entry_exit (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL); if (t2 < 0) intersect = 0; /* we passed sample volume already */ @@ -662,15 +653,8 @@ TRACE intersect = box_intersect (&t1, &t2, x, y, z, vx, vy, vz, xwidth, yheight, zdepth); else if (hkl_info.shape == 2) intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); - else if (hkl_info.shape == 3) { - gmesh_hit hits[GMESH_MAX_HITS]; - int nhits = gmesh_intersect (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 0, hits); - intersect = nhits > 0 && gmesh_inside (&mesh, coords_set (x, y, z)); - if (intersect) { - t1 = 0; - t2 = hits[0].time; - } - } + else if (hkl_info.shape == 3) + intersect = gmesh_entry_exit (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL) == 1; if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { /* neutron is leaving the sample */ if (hkl_info.flag_warning < 100) diff --git a/mcstas-comps/share/monitor_nd-lib.c b/mcstas-comps/share/monitor_nd-lib.c index 7764148c80..5c8b713884 100644 --- a/mcstas-comps/share/monitor_nd-lib.c +++ b/mcstas-comps/share/monitor_nd-lib.c @@ -2205,31 +2205,6 @@ void Monitor_nD_McDisplay(MonitornD_Defines_type *DEFS, /* defined by a mesh file (geometry parameter) */ /* ========================================================================= */ -// Hits of the line position + t*velocity with mesh ahead of the neutron -// (t > 0). Inside a closed mesh, t0 is 0 and t1 the first exit; otherwise -// t0 is the first hit and t1 the next one (t0 when there is a single hit). -// face is the face of the first hit ahead. Returns 1 on a hit, 0 otherwise. -#pragma acc routine seq -int Monitor_nD_MeshIntersect(gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t1, int* face) -{ - gmesh_hit hits[GMESH_MAX_HITS]; - double t_min = 0; - int nhits = gmesh_intersect(mesh, position, velocity, t_min, hits); - if (!nhits) - return 0; - if (gmesh_inside(mesh, position)) { - *t0 = 0; - *t1 = hits[0].time; - } else { - *t0 = hits[0].time; - *t1 = hits[0].time; - if (nhits > 1) - *t1 = hits[1].time; - } - *face = hits[0].face; - return 1; -} - // Warns when mantid is combined with a closed mesh: pixel IDs are panels. void Monitor_nD_CheckMantidMesh(gmesh_struct* mesh, int flag_mantid, char* name) { diff --git a/mcstas-comps/share/monitor_nd-lib.h b/mcstas-comps/share/monitor_nd-lib.h index f4c332ecc6..fc54e7b485 100644 --- a/mcstas-comps/share/monitor_nd-lib.h +++ b/mcstas-comps/share/monitor_nd-lib.h @@ -220,8 +220,6 @@ void Monitor_nD_EnsureCapacity(MonitornD_Defines_type *, MonitornD_Variables_typ MCDETECTOR Monitor_nD_Save(MonitornD_Defines_type *, MonitornD_Variables_type *); void Monitor_nD_Finally(MonitornD_Defines_type *, MonitornD_Variables_type *); void Monitor_nD_McDisplay(MonitornD_Defines_type *, MonitornD_Variables_type *); -#pragma acc routine seq -int Monitor_nD_MeshIntersect(gmesh_struct*, Coords, Coords, double*, double*, int*); void Monitor_nD_MeshMantid(gmesh_struct*, long); void Monitor_nD_CheckMantidMesh(gmesh_struct*, int, char*); diff --git a/mcstas-comps/share/union-lib.c b/mcstas-comps/share/union-lib.c index 301ea96b69..7c62432402 100644 --- a/mcstas-comps/share/union-lib.c +++ b/mcstas-comps/share/union-lib.c @@ -2995,7 +2995,7 @@ int sample_mesh_intersect(double *t, *num_solutions = gmesh_intersect(triangle_mesh, position, velocity, t_min, hits); for (i = 0; i < *num_solutions; i++) { Coords normal = rot_apply(geometry->rotation_matrix, hits[i].normal); - t[i] = hits[i].time; + t[i] = hits[i].t; nx[i] = normal.x; ny[i] = normal.y; nz[i] = normal.z; diff --git a/mcstas-comps/union/Union_mesh.comp b/mcstas-comps/union/Union_mesh.comp index d4654f7f34..903150111a 100644 --- a/mcstas-comps/union/Union_mesh.comp +++ b/mcstas-comps/union/Union_mesh.comp @@ -444,10 +444,10 @@ INITIALIZE printf ("\nERROR IN %s: could not read the geometry file %s\n", NAME_CURRENT_COMP, filename); exit (1); } - if (!gmesh_check_closed (&this_mesh_storage.mesh)) { - printf ("\n\nERROR: Input mesh %s of %s is not closed or folds back onto itself (see the error above)." + if (!this_mesh_storage.mesh.closed) { + printf ("\n\nERROR: Input mesh %s of %s is not closed: %d boundary and %d non-manifold edges." "\nUnion_mesh requires a closed (watertight) mesh.\n\n", - filename, NAME_CURRENT_COMP); + filename, NAME_CURRENT_COMP, this_mesh_storage.mesh.nboundary, this_mesh_storage.mesh.nnonmanifold); exit (1); } diff --git a/mcxtrace-comps/share/union-lib.c b/mcxtrace-comps/share/union-lib.c index 48c9d7f504..e8d2ba95bd 100644 --- a/mcxtrace-comps/share/union-lib.c +++ b/mcxtrace-comps/share/union-lib.c @@ -2621,7 +2621,7 @@ int sample_mesh_intersect(double *t,int *num_solutions,double *r,double *v,struc // McXtrace intersect functions return lengths, so trace along the unit direction direction = coords_scale(direction, 1.0/coords_len(direction)); *num_solutions = gmesh_intersect(triangle_mesh, position, direction, -1e-9*triangle_mesh->length_scale, hits); - for (i = 0; i < *num_solutions; i++) t[i] = hits[i].time; + for (i = 0; i < *num_solutions; i++) t[i] = hits[i].t; return *num_solutions > 0; } diff --git a/mcxtrace-comps/union/Union_mesh.comp b/mcxtrace-comps/union/Union_mesh.comp index e8d037b1ed..3059010e74 100644 --- a/mcxtrace-comps/union/Union_mesh.comp +++ b/mcxtrace-comps/union/Union_mesh.comp @@ -435,10 +435,10 @@ INITIALIZE printf ("\nERROR IN %s: could not read the geometry file %s\n", NAME_CURRENT_COMP, filename); exit (1); } - if (!gmesh_check_closed (&this_mesh_storage.mesh)) { - printf ("\n\nERROR: Input mesh %s of %s is not closed or folds back onto itself (see the error above)." + if (!this_mesh_storage.mesh.closed) { + printf ("\n\nERROR: Input mesh %s of %s is not closed: %d boundary and %d non-manifold edges." "\nUnion_mesh requires a closed (watertight) mesh.\n\n", - filename, NAME_CURRENT_COMP); + filename, NAME_CURRENT_COMP, this_mesh_storage.mesh.nboundary, this_mesh_storage.mesh.nnonmanifold); exit (1); } From fb9ee5945a95d88fb7ee2076a48daf5d7322048a Mon Sep 17 00:00:00 2001 From: lomholy_agentic Date: Mon, 16 Nov 2026 09:17:44 +0100 Subject: [PATCH 22/22] geometry-mesh-lib: keep origin scaling and gmesh_hit.time Revert points 1 and 4 of the review changes, as agreed in mccode-dev/McCode#3090: - gmesh_init_fit with center=0 scales about the origin again, as off_init of interoff-lib; libtest checks that placement - gmesh_hit.t is gmesh_hit.time again (t is the particle time in TRACE); Lens and Guide_anyshape(_r) read hits[0].time inline, without the SHARE hit functions Co-Authored-By: Claude Opus 5.5 --- common/lib/share/geometry-mesh-lib.c | 42 ++++++++-------- common/lib/share/geometry-mesh-lib.h | 6 +-- mcstas-comps/contrib/Lens.comp | 50 ++++++++++--------- .../Test_lib_geometry_mesh_lib.instr | 32 ++++++------ mcstas-comps/obsolete/Guide_anyshape_r.comp | 20 ++------ mcstas-comps/optics/Guide_anyshape.comp | 20 ++------ mcstas-comps/share/union-lib.c | 2 +- mcxtrace-comps/share/union-lib.c | 2 +- 8 files changed, 78 insertions(+), 96 deletions(-) diff --git a/common/lib/share/geometry-mesh-lib.c b/common/lib/share/geometry-mesh-lib.c index 1368a91a48..de42b83922 100644 --- a/common/lib/share/geometry-mesh-lib.c +++ b/common/lib/share/geometry-mesh-lib.c @@ -1400,8 +1400,9 @@ gmesh_fit_scale (Coords range, double xwidth, double yheight, double zdepth) { } // read an STL/OFF/PLY file into mesh: multiply by scale, then resize the -// bounding box about its centre to xwidth/yheight/zdepth (0: keep); with -// center the box centre is moved to the origin, otherwise it stays in place +// bounding box to xwidth/yheight/zdepth (0: keep); with center the box centre +// is moved to the origin and the box resized about it, otherwise the +// coordinates are scaled about the origin (as off_init of interoff-lib) static int gmesh_load (char* filename, double scale, double xwidth, double yheight, double zdepth, int center, int face_props, gmesh_struct* mesh) { gmesh_raw raw; @@ -1476,19 +1477,16 @@ gmesh_load (char* filename, double scale, double xwidth, double yheight, double } if (xwidth || yheight || zdepth || center) { Coords range = coords_sub (mesh->bbmax, mesh->bbmin); - Coords box_center = coords_scale (coords_add (mesh->bbmin, mesh->bbmax), 0.5); - Coords shift = center ? coords_set (0, 0, 0) : box_center; + Coords origin = center ? coords_scale (coords_add (mesh->bbmin, mesh->bbmax), 0.5) : coords_set (0, 0, 0); Coords scale = gmesh_fit_scale (range, xwidth, yheight, zdepth); for (i = 0; i < raw.nverts; i++) { Coords* p = &raw.verts[i]; - p->x = (p->x - box_center.x) * scale.x + shift.x; - p->y = (p->y - box_center.y) * scale.y + shift.y; - p->z = (p->z - box_center.z) * scale.z + shift.z; + p->x = (p->x - origin.x) * scale.x; + p->y = (p->y - origin.y) * scale.y; + p->z = (p->z - origin.z) * scale.z; } - mesh->bbmin = coords_set ((mesh->bbmin.x - box_center.x) * scale.x + shift.x, (mesh->bbmin.y - box_center.y) * scale.y + shift.y, - (mesh->bbmin.z - box_center.z) * scale.z + shift.z); - mesh->bbmax = coords_set ((mesh->bbmax.x - box_center.x) * scale.x + shift.x, (mesh->bbmax.y - box_center.y) * scale.y + shift.y, - (mesh->bbmax.z - box_center.z) * scale.z + shift.z); + mesh->bbmin = coords_set ((mesh->bbmin.x - origin.x) * scale.x, (mesh->bbmin.y - origin.y) * scale.y, (mesh->bbmin.z - origin.z) * scale.z); + mesh->bbmax = coords_set ((mesh->bbmax.x - origin.x) * scale.x, (mesh->bbmax.y - origin.y) * scale.y, (mesh->bbmax.z - origin.z) * scale.z); MPI_MASTER ( if (scale.x != scale.y || scale.x != scale.z) printf ("Warning: Aspect ratio of the geometry %s was modified.\n" @@ -1962,7 +1960,7 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, *ambiguous = 1; continue; } - hit.t = hit_t; + hit.time = hit_t; hit.normal = mesh->tri_normal[t]; hit.tri = t; hit.face = mesh->tri_face[t]; @@ -1970,7 +1968,7 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, hit.edge = on_edge; // hits on shared edges/vertices: keep once for (k = 0; k < *stored_count; k++) { - if (hits[k].in_out == in_out && fabs (hits[k].t - hit_t) <= t_tolerance) { + if (hits[k].in_out == in_out && fabs (hits[k].time - hit_t) <= t_tolerance) { duplicate = 1; break; } @@ -1982,13 +1980,13 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, if (position == max_hits) { if (max_hits == 0) continue; - if (hit_t >= hits[max_hits - 1].t) + if (hit_t >= hits[max_hits - 1].time) continue; position = max_hits - 1; } else { (*stored_count)++; } - while (position > 0 && hits[position - 1].t > hit_t) { + while (position > 0 && hits[position - 1].time > hit_t) { hits[position] = hits[position - 1]; position--; } @@ -2236,7 +2234,7 @@ gmesh_trace_gravity (gmesh_struct* mesh, Coords origin, Coords velocity, Coords } else { in_out = -1; } - hit.t = hit_t; + hit.time = hit_t; hit.normal = mesh->tri_normal[triangle]; hit.tri = triangle; hit.face = mesh->tri_face[triangle]; @@ -2244,7 +2242,7 @@ gmesh_trace_gravity (gmesh_struct* mesh, Coords origin, Coords velocity, Coords hit.edge = crossing_edge[i_crossing]; // hits on shared edges/vertices: keep once for (k = 0; k < *stored_count; k++) { - if (hits[k].in_out == in_out && fabs (hits[k].t - hit_t) <= t_tolerance) { + if (hits[k].in_out == in_out && fabs (hits[k].time - hit_t) <= t_tolerance) { duplicate = 1; break; } @@ -2254,13 +2252,13 @@ gmesh_trace_gravity (gmesh_struct* mesh, Coords origin, Coords velocity, Coords total++; position = *stored_count; if (position == max_hits) { - if (hit_t >= hits[max_hits - 1].t) + if (hit_t >= hits[max_hits - 1].time) continue; position = max_hits - 1; } else { (*stored_count)++; } - while (position > 0 && hits[position - 1].t > hit_t) { + while (position > 0 && hits[position - 1].time > hit_t) { hits[position] = hits[position - 1]; position--; } @@ -2343,11 +2341,11 @@ gmesh_entry_exit (gmesh_struct* mesh, Coords position, Coords velocity, double* *first = hits[0]; if (inside) { *t_in = 0; - *t_out = hits[0].t + 1e-13; + *t_out = hits[0].time + 1e-13; return 1; } - *t_in = hits[0].t + 1e-13; - *t_out = (nhits > 1 ? hits[1].t : hits[0].t) + 1e-13; + *t_in = hits[0].time + 1e-13; + *t_out = (nhits > 1 ? hits[1].time : hits[0].time) + 1e-13; return 2; } diff --git a/common/lib/share/geometry-mesh-lib.h b/common/lib/share/geometry-mesh-lib.h index 9e3455bbbf..3e0b87a1c1 100644 --- a/common/lib/share/geometry-mesh-lib.h +++ b/common/lib/share/geometry-mesh-lib.h @@ -86,7 +86,7 @@ #endif typedef struct gmesh_hit { - double t; // position along the line: pos + t*dir + double time; // position along the line: pos + time*dir Coords normal; // unit normal of the triangle hit (outward for closed oriented meshes) int tri; // triangle index (BVH order) int face; // index of the face (polygon) in the file, 0-based @@ -154,10 +154,10 @@ int gmesh_init (char* filename, double scale, int face_props, gmesh_struct* mesh // int gmesh_init_fit(char *filename, double xwidth, double yheight, double zdepth, // int center, int face_props, gmesh_struct *mesh) -// ACTION: as gmesh_init, then resize the bounding box about its centre: +// ACTION: as gmesh_init, then resize the bounding box as off_init of interoff-lib: // non-zero sizes set the box size along x/y/z; a single size scales all // axes by the same factor (proportions kept). With center, the box centre -// is moved to the origin; otherwise it stays in place. +// is moved to the origin; otherwise coordinates are scaled about the origin. // RETURN: number of triangles, 0 on error int gmesh_init_fit (char* filename, double xwidth, double yheight, double zdepth, int center, int face_props, gmesh_struct* mesh); diff --git a/mcstas-comps/contrib/Lens.comp b/mcstas-comps/contrib/Lens.comp index 5eb75b1ddb..7c19524f9c 100644 --- a/mcstas-comps/contrib/Lens.comp +++ b/mcstas-comps/contrib/Lens.comp @@ -125,20 +125,6 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" - // time to the next surface of mesh (t > 1e-13 s, which skips the surface the - // neutron sits on after a refraction or reflection), -1 when there is none. - // normal is oriented as in interoff-lib: opposite to the right-hand rule of - // the vertex order in the file, unless the mesh was flipped. - #pragma acc routine seq - double - Lens_mesh_hit (gmesh_struct* mesh, Coords position, Coords velocity, Coords* normal) { - gmesh_hit hits[GMESH_MAX_HITS]; - if (!gmesh_intersect (mesh, position, velocity, 1e-13, hits)) - return -1; - *normal = coords_scale (hits[0].normal, mesh->flipped ? 1 : -1); - return hits[0].t; - } - /* Lens_roughness: function to rotate normal vector around axis for roughness * with specified tilt angle (deg) * RETURNS: rotated nx,ny,nz coordinates @@ -299,9 +285,15 @@ TRACE nx = ny = nz = 0; #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - Coords n = coords_set (0, 0, 0); - dt = Lens_mesh_hit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &n); - coords_get (n, &nx, &ny, &nz); + gmesh_hit hits[GMESH_MAX_HITS]; + // t_min = 1e-13 s skips the surface the neutron sits on after a refraction or reflection + if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 1e-13, hits)) { + dt = hits[0].time; + // normal oriented as in interoff-lib: opposite to the right-hand rule of + // the vertex order in the file, unless the mesh was flipped + coords_get (coords_scale (hits[0].normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); + } else + dt = -1; } else #endif if (r1 && !focus1) { @@ -391,9 +383,15 @@ TRACE nx = ny = nz = 0; #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - Coords n = coords_set (0, 0, 0); - dt = Lens_mesh_hit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &n); - coords_get (n, &nx, &ny, &nz); + gmesh_hit hits[GMESH_MAX_HITS]; + // t_min = 1e-13 s skips the surface the neutron sits on after a refraction or reflection + if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 1e-13, hits)) { + dt = hits[0].time; + // normal oriented as in interoff-lib: opposite to the right-hand rule of + // the vertex order in the file, unless the mesh was flipped + coords_get (coords_scale (hits[0].normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); + } else + dt = -1; } else #endif if (r2 && !focus2) @@ -496,9 +494,15 @@ TRACE nx = ny = nz = 0; #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - Coords n = coords_set (0, 0, 0); - dt = Lens_mesh_hit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &n); - coords_get (n, &nx, &ny, &nz); + gmesh_hit hits[GMESH_MAX_HITS]; + // t_min = 1e-13 s skips the surface the neutron sits on after a refraction or reflection + if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 1e-13, hits)) { + dt = hits[0].time; + // normal oriented as in interoff-lib: opposite to the right-hand rule of + // the vertex order in the file, unless the mesh was flipped + coords_get (coords_scale (hits[0].normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); + } else + dt = -1; } else #endif if (r2 && !focus2) diff --git a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr index 98bf67ebb1..f3e6b5ddac 100644 --- a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr +++ b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr @@ -8,8 +8,8 @@ * Fails to compile or link if the library does not build with the runtime. * INITIALIZE also checks gmesh_init_fit, gmesh_inside, gmesh_entry_exit and * gmesh_intersect_gravity, and exits with an error when a check fails: -* - gmesh_init_fit resizes cube.off about its box centre, which stays in place -* with center=0 and is moved to the origin with center=1 +* - gmesh_init_fit resizes cube.off about the origin with center=0, and about +* its box centre, moved to the origin, with center=1 * - gmesh_init counts the folded edges of a folded cube, which is then not * closed, and gmesh_check_closed fails it and the open Guide.off, and passes * cube.off and socket.off @@ -64,12 +64,12 @@ int inside_tests (void) { return 1; } mesh_check (gmesh_inside (&cube, coords_set (0.5, 0.5, 0.5)) == 1, "inside the cube", &failures); - // the unit cube [0, 1]^3 resized to a side of 2 about its centre (0.5, 0.5, 0.5) + // the unit cube [0, 1]^3 resized to a side of 2 { gmesh_struct fit; - mesh_check (gmesh_init_fit ("cube.off", 2, 0, 0, 0, GMESH_PROPS_IGNORE, &fit) && fabs (fit.bbmin.x + 0.5) < 1e-12 && fabs (fit.bbmax.y - 1.5) < 1e-12 - && fabs (fit.bbmax.z - 1.5) < 1e-12, - "gmesh_init_fit with center=0 keeps the box centre", &failures); + mesh_check (gmesh_init_fit ("cube.off", 2, 0, 0, 0, GMESH_PROPS_IGNORE, &fit) && fabs (fit.bbmin.x) < 1e-12 && fabs (fit.bbmax.y - 2) < 1e-12 + && fabs (fit.bbmax.z - 2) < 1e-12, + "gmesh_init_fit with center=0 scales about the origin", &failures); gmesh_free (&fit); mesh_check (gmesh_init_fit ("cube.off", 2, 0, 0, 1, GMESH_PROPS_IGNORE, &fit) && fabs (fit.bbmin.x + 1) < 1e-12 && fabs (fit.bbmax.y - 1) < 1e-12 && fabs (fit.bbmax.z - 1) < 1e-12, @@ -159,18 +159,18 @@ int gravity_tests (void) { // falling from rest above the unit cube: top at time sqrt(2), bottom at 2 nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, 2, 0.5), coords_set (0, 0, 0), coords_set (0, -1, 0), 0, hits); - mesh_check (nhits == 2 && fabs (hits[0].t - sqrt (2)) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].t - 2) < 1e-12 && hits[1].in_out == -1, + mesh_check (nhits == 2 && fabs (hits[0].time - sqrt (2)) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].time - 2) < 1e-12 && hits[1].in_out == -1, "fall from rest through cube.off", &failures); // entering through the front face, leaving through the bottom at time sqrt(2) nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, 0.5, -1), coords_set (0, 0, 1), coords_set (0, -0.5, 0), 0, hits); - mesh_check (nhits == 2 && fabs (hits[0].t - 1) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].t - sqrt (2)) < 1e-12 && hits[1].in_out == -1, + mesh_check (nhits == 2 && fabs (hits[0].time - 1) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].time - sqrt (2)) < 1e-12 && hits[1].in_out == -1, "front face in, bottom out of cube.off", &failures); // thrown up into the cube from below, falling out through the same face nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, -1, 0.5), coords_set (0, 2, 0), coords_set (0, -1.2, 0), 0, hits); - mesh_check (nhits == 2 && fabs (hits[0].t - (2 - sqrt (1.6)) / 1.2) < 1e-12 && hits[0].in_out == 1 - && fabs (hits[1].t - (2 + sqrt (1.6)) / 1.2) < 1e-12 && hits[1].in_out == -1 && hits[0].face == hits[1].face, + mesh_check (nhits == 2 && fabs (hits[0].time - (2 - sqrt (1.6)) / 1.2) < 1e-12 && hits[0].in_out == 1 + && fabs (hits[1].time - (2 + sqrt (1.6)) / 1.2) < 1e-12 && hits[1].in_out == -1 && hits[0].face == hits[1].face, "in and out through the bottom of cube.off", &failures); // t_min skips the hits before it @@ -202,7 +202,7 @@ int gravity_tests (void) { continue; } for (i_hit = 0; i_hit < nhits; i_hit++) { - if (fabs (hits[i_hit].t - line_hits[i_hit].t) > 1e-12 * (1 + fabs (line_hits[i_hit].t)) || hits[i_hit].in_out != line_hits[i_hit].in_out) + if (fabs (hits[i_hit].time - line_hits[i_hit].time) > 1e-12 * (1 + fabs (line_hits[i_hit].time)) || hits[i_hit].in_out != line_hits[i_hit].in_out) mismatches++; } @@ -212,21 +212,21 @@ int gravity_tests (void) { Coords chord = coords_sub (gravity_parabola (position, velocity, acceleration, (i_step + 1) * step), start); int npiece = gmesh_intersect (&socket, start, chord, 0, line_hits); int i_piece; - for (i_piece = 0; i_piece < npiece && line_hits[i_piece].t <= 1 && nline < GMESH_MAX_HITS; i_piece++) { - line_times[nline] = (i_step + line_hits[i_piece].t) * step; + for (i_piece = 0; i_piece < npiece && line_hits[i_piece].time <= 1 && nline < GMESH_MAX_HITS; i_piece++) { + line_times[nline] = (i_step + line_hits[i_piece].time) * step; nline++; } } nhits = gmesh_intersect_gravity (&socket, position, velocity, acceleration, 0, hits); - while (nhits > 0 && hits[nhits - 1].t > time_end) + while (nhits > 0 && hits[nhits - 1].time > time_end) nhits--; if (nhits != nline) { mismatches++; continue; } for (i_hit = 0; i_hit < nhits; i_hit++) { - if (fabs (hits[i_hit].t - line_times[i_hit]) > worst) - worst = fabs (hits[i_hit].t - line_times[i_hit]); + if (fabs (hits[i_hit].time - line_times[i_hit]) > worst) + worst = fabs (hits[i_hit].time - line_times[i_hit]); } } printf ("Test_lib_geometry_mesh_lib: socket.off: %d of 2000 random rays differ from the line or polyline, worst time difference %g s\n", mismatches, diff --git a/mcstas-comps/obsolete/Guide_anyshape_r.comp b/mcstas-comps/obsolete/Guide_anyshape_r.comp index 223f44e381..b2775e6150 100644 --- a/mcstas-comps/obsolete/Guide_anyshape_r.comp +++ b/mcstas-comps/obsolete/Guide_anyshape_r.comp @@ -97,19 +97,6 @@ SHARE %include "read_table-lib" %include "geometry-mesh-lib" %include "ref-lib" - -// next hit of the parabola position + t*velocity + t^2/2*acceleration with -// mesh (t > 0): its time, 0 when there is none, with its normal and face -#pragma acc routine seq -double -Guide_anyshape_r_hit (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, Coords* normal, int* face) { - gmesh_hit hits[GMESH_MAX_HITS]; - if (!gmesh_intersect_gravity (mesh, position, velocity, acceleration, 0, hits)) - return 0; - *normal = hits[0].normal; - *face = hits[0].face; - return hits[0].t; -} %} DECLARE @@ -155,17 +142,20 @@ TRACE double dt = 0; int fi = 0; Coords n = { 0, 0, 0 }; + gmesh_hit hits[GMESH_MAX_HITS]; Coords acceleration = coords_set (0, 0, 0); if (mcgravitation) acceleration = rot_apply (_comp->_rotation_absolute, coords_set (0, -GRAVITY, 0)); // next intersection with the object, along the parabola under gravity; PL: with the index of the face - dt = Guide_anyshape_r_hit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, &n, &fi); - intersect = dt > 0; + intersect = gmesh_intersect_gravity (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, 0, hits); /* exit loop when no intersection forward */ if (!intersect) break; + dt = hits[0].time; + n = hits[0].normal; + fi = hits[0].face; double nx, ny, nz; coords_get (n, &nx, &ny, &nz); diff --git a/mcstas-comps/optics/Guide_anyshape.comp b/mcstas-comps/optics/Guide_anyshape.comp index 847e7909f2..64e627372f 100644 --- a/mcstas-comps/optics/Guide_anyshape.comp +++ b/mcstas-comps/optics/Guide_anyshape.comp @@ -103,19 +103,6 @@ SHARE %include "ref-lib" // per-face coating properties in the geometry file: nv i1 ... inv m alpha W [R0 [Qc]] enum { GUIDE_ANYSHAPE_M, GUIDE_ANYSHAPE_ALPHA, GUIDE_ANYSHAPE_W, GUIDE_ANYSHAPE_R0, GUIDE_ANYSHAPE_QC }; - -// next hit of the parabola position + t*velocity + t^2/2*acceleration with -// mesh (t > 0): its time, 0 when there is none, with its normal and face -#pragma acc routine seq -double -Guide_anyshape_hit (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, Coords* normal, int* face) { - gmesh_hit hits[GMESH_MAX_HITS]; - if (!gmesh_intersect_gravity (mesh, position, velocity, acceleration, 0, hits)) - return 0; - *normal = hits[0].normal; - *face = hits[0].face; - return hits[0].t; -} %} DECLARE @@ -170,17 +157,20 @@ TRACE double dt = 0; int fi = 0; Coords n = { 0, 0, 0 }; + gmesh_hit hits[GMESH_MAX_HITS]; Coords acceleration = coords_set (0, 0, 0); if (mcgravitation) acceleration = rot_apply (_comp->_rotation_absolute, coords_set (0, -GRAVITY, 0)); // next intersection with the object, along the parabola under gravity - dt = Guide_anyshape_hit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, &n, &fi); - intersect = dt > 0; + intersect = gmesh_intersect_gravity (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, 0, hits); /* exit loop when no intersection forward */ if (!intersect) break; + dt = hits[0].time; + n = hits[0].normal; + fi = hits[0].face; double nx, ny, nz; coords_get (n, &nx, &ny, &nz); diff --git a/mcstas-comps/share/union-lib.c b/mcstas-comps/share/union-lib.c index 7c62432402..301ea96b69 100644 --- a/mcstas-comps/share/union-lib.c +++ b/mcstas-comps/share/union-lib.c @@ -2995,7 +2995,7 @@ int sample_mesh_intersect(double *t, *num_solutions = gmesh_intersect(triangle_mesh, position, velocity, t_min, hits); for (i = 0; i < *num_solutions; i++) { Coords normal = rot_apply(geometry->rotation_matrix, hits[i].normal); - t[i] = hits[i].t; + t[i] = hits[i].time; nx[i] = normal.x; ny[i] = normal.y; nz[i] = normal.z; diff --git a/mcxtrace-comps/share/union-lib.c b/mcxtrace-comps/share/union-lib.c index e8d2ba95bd..48c9d7f504 100644 --- a/mcxtrace-comps/share/union-lib.c +++ b/mcxtrace-comps/share/union-lib.c @@ -2621,7 +2621,7 @@ int sample_mesh_intersect(double *t,int *num_solutions,double *r,double *v,struc // McXtrace intersect functions return lengths, so trace along the unit direction direction = coords_scale(direction, 1.0/coords_len(direction)); *num_solutions = gmesh_intersect(triangle_mesh, position, direction, -1e-9*triangle_mesh->length_scale, hits); - for (i = 0; i < *num_solutions; i++) t[i] = hits[i].t; + for (i = 0; i < *num_solutions; i++) t[i] = hits[i].time; return *num_solutions > 0; }