From ed0dc3e7329848104512c913cc5f6b5284e9b767 Mon Sep 17 00:00:00 2001 From: mads-bertelsen-agentic <301266180+mads-bertelsen-agentic@users.noreply.github.com> Date: Wed, 9 Sep 2026 11:24:19 +0200 Subject: [PATCH 1/3] Add gravity-independent PROP macros --- docs/manuals/mcstas/kernelcalls.tex | 8 ++ mccode/nlib/share/mcstas-r.h | 24 ++++ .../Unittest_PROP_NO_G.instr | 122 ++++++++++++++++++ support/common/editors/mccode.lang | 4 + support/common/editors/mccode.vim | 4 + 5 files changed, 162 insertions(+) create mode 100644 mcstas-comps/examples/Tests_grammar/Unittest_PROP_NO_G/Unittest_PROP_NO_G.instr diff --git a/docs/manuals/mcstas/kernelcalls.tex b/docs/manuals/mcstas/kernelcalls.tex index 62ae059dc0..f2caac26a8 100644 --- a/docs/manuals/mcstas/kernelcalls.tex +++ b/docs/manuals/mcstas/kernelcalls.tex @@ -79,6 +79,14 @@ \subsection{Neutron propagation} \item \textbfMCRH{PROP\_GRAV\_DT}$(dt,Ax,Ay,Az)$. Like \textbf{PROP\_DT}, but it also includes gravity using the acceleration $(Ax,Ay,Az)$. In addition to adjusting $(x,y,z)$ and $t$, also $(vx,vy,vz)$ is modified. +\item \textbfMCRH{PROP\_NO\_G\_DT}$(dt)$. Propagates the neutron through the + time interval $dt$ using straight-line motion, regardless of the global + gravitation setting. The negative-time and back-propagation rules are the + same as for \textbf{PROP\_DT}. +\item \textbfMCRH{PROP\_NO\_G\_X0}, \textbfMCRH{PROP\_NO\_G\_Y0}, and + \textbfMCRH{PROP\_NO\_G\_Z0}. Propagate the neutron to the corresponding + local coordinate plane using straight-line motion, regardless of the global + gravitation setting. \item \textbfMCRH{ALLOW\_BACKPROP}. Indicates that the next propagation routine will not remove the neutron ray, even if negative propagation times are found. Subsequent propagations are not affected. diff --git a/mccode/nlib/share/mcstas-r.h b/mccode/nlib/share/mcstas-r.h index 0b61255415..33bea2eded 100644 --- a/mccode/nlib/share/mcstas-r.h +++ b/mccode/nlib/share/mcstas-r.h @@ -201,6 +201,30 @@ void SCATTER_func(_class_particle *_particle); /* provides function to SCATTER f } while(0) +/* Propagation variants that always use straight-line motion. */ +#define PROP_NO_G_DT(dt) \ + do { \ + if(dt < 0 && allow_backprop == 0) { RESTORE=1; ABSORB; }; \ + mcPROP_DT(dt); \ + DISALLOW_BACKPROP; \ + } while(0) + +#define PROP_NO_G_Z0 \ + do { \ + mcPROP_Z0; \ + } while(0) + +#define PROP_NO_G_X0 \ + do { \ + mcPROP_X0; \ + } while(0) + +#define PROP_NO_G_Y0 \ + do { \ + mcPROP_Y0; \ + } while(0) + + #ifdef DEBUG #define DEBUG_STATE() if(!mcdotrace); else \ diff --git a/mcstas-comps/examples/Tests_grammar/Unittest_PROP_NO_G/Unittest_PROP_NO_G.instr b/mcstas-comps/examples/Tests_grammar/Unittest_PROP_NO_G/Unittest_PROP_NO_G.instr new file mode 100644 index 0000000000..39932278d3 --- /dev/null +++ b/mcstas-comps/examples/Tests_grammar/Unittest_PROP_NO_G/Unittest_PROP_NO_G.instr @@ -0,0 +1,122 @@ +/******************************************************************************* +* McStas, neutron ray-tracing package +* +* Instrument: Unittest_PROP_NO_G +* +* %Identification +* Written by: McStas developers +* Date: September 2026 +* Origin: McStas +* %INSTRUMENT_SITE: Tests_grammar +* +* Unittest for the PROP_NO_G_* propagation macros. +* +* %Description +* Verifies straight-line propagation with global gravity enabled and checks +* that PROP_NO_G_DT retains the normal back-propagation behavior. +* +* %Example: Backprop=0 -g Detector: PropNoGMonitor_I=1 +* %Example: Backprop=1 -g Detector: PropNoGMonitor_I=1 +* +* %Parameters +* Backprop: [1] Flag to allow the final negative propagation +* +* %End +*******************************************************************************/ +DEFINE INSTRUMENT Unittest_PROP_NO_G(int Backprop=0) + +TRACE + +COMPONENT Origin = Progress_bar() + AT (0, 0, 0) ABSOLUTE + +COMPONENT Source = Source_simple( + radius = 0.01, + dist = 1, + focus_xw = 0.1, + focus_yh = 0.1, + lambda0 = 5, + dlambda = 0, + flux = 0) + AT (0, 0, 0) RELATIVE Origin +EXTEND %{ + x = 0; y = 0; z = 0; + vx = 1; vy = 1; vz = 1; t = 0; + p = 1.0/mcget_ncount(); +%} + +COMPONENT PropNoG = Arm() + AT (0, 0, 0) RELATIVE Source +EXTEND %{ + int failed = 0; + const double tolerance = 1e-12; + + p = 1.0/mcget_ncount(); + + x = 1; y = 1; z = 1; + vx = 2; vy = 3; vz = 4; t = 0; + PROP_NO_G_DT(0.5); + if (fabs(x - 2) > tolerance || fabs(y - 2.5) > tolerance || + fabs(z - 3) > tolerance || fabs(t - 0.5) > tolerance || + fabs(vx - 2) > tolerance || fabs(vy - 3) > tolerance || + fabs(vz - 4) > tolerance) + failed = 1; + + x = 1; y = 1; z = 1; + vx = -2; vy = 3; vz = 4; t = 0; + PROP_NO_G_X0; + if (fabs(x) > tolerance || fabs(y - 2.5) > tolerance || + fabs(z - 3) > tolerance || fabs(t - 0.5) > tolerance || + fabs(vx + 2) > tolerance || fabs(vy - 3) > tolerance || + fabs(vz - 4) > tolerance) + failed = 1; + + x = 1; y = 1; z = 1; + vx = 2; vy = -3; vz = 4; t = 0; + PROP_NO_G_Y0; + if (fabs(x - 5.0/3.0) > tolerance || fabs(y) > tolerance || + fabs(z - 7.0/3.0) > tolerance || fabs(t - 1.0/3.0) > tolerance || + fabs(vx - 2) > tolerance || fabs(vy + 3) > tolerance || + fabs(vz - 4) > tolerance) + failed = 1; + + x = 1; y = 1; z = 1; + vx = 2; vy = 3; vz = -4; t = 0; + PROP_NO_G_Z0; + if (fabs(x - 1.5) > tolerance || fabs(y - 1.75) > tolerance || + fabs(z) > tolerance || fabs(t - 0.25) > tolerance || + fabs(vx - 2) > tolerance || fabs(vy - 3) > tolerance || + fabs(vz + 4) > tolerance) + failed = 1; + + if (failed) + ABSORB; + + if (INSTRUMENT_GETPAR(Backprop)) + ALLOW_BACKPROP; + PROP_NO_G_DT(-0.1); +%} + +COMPONENT PropNoGCheck = Arm() + AT (0, 0, 0) RELATIVE PropNoG +EXTEND %{ + const double tolerance = 1e-12; + const int backprop = INSTRUMENT_GETPAR(Backprop); + const double expected_x = backprop ? 1.3 : 0; + const double expected_y = backprop ? 1.45 : 0; + const double expected_z = backprop ? 0.4 : 0; + const double expected_t = backprop ? 0.15 : 0; + + if (fabs(x - expected_x) > tolerance || + fabs(y - expected_y) > tolerance || + fabs(z - expected_z) > tolerance || + fabs(t - expected_t) > tolerance) + ABSORB; +%} + +COMPONENT PropNoGMonitor = Monitor( + xwidth = 10, + yheight = 10) + AT (0, 0, 0) RELATIVE PropNoG + +END diff --git a/support/common/editors/mccode.lang b/support/common/editors/mccode.lang index e7dd2f6361..54002c0c42 100644 --- a/support/common/editors/mccode.lang +++ b/support/common/editors/mccode.lang @@ -397,9 +397,13 @@ RESTORE_NEUTRON PROP_GRAV_DT PROP_DT + PROP_NO_G_DT PROP_Z0 PROP_X0 PROP_Y0 + PROP_NO_G_Z0 + PROP_NO_G_X0 + PROP_NO_G_Y0 vec_prod scalar_prod NORM diff --git a/support/common/editors/mccode.vim b/support/common/editors/mccode.vim index fada7871c1..f52551edab 100644 --- a/support/common/editors/mccode.vim +++ b/support/common/editors/mccode.vim @@ -65,10 +65,14 @@ \ RESTORE_NEUTRON \ PROP_GRAV_DT \ PROP_DT + \ PROP_NO_G_DT \ PROP_DL \ PROP_Z0 \ PROP_X0 \ PROP_Y0 + \ PROP_NO_G_Z0 + \ PROP_NO_G_X0 + \ PROP_NO_G_Y0 \ vec_prod \ scalar_prod \ NORM From d0d84c4a0631e7d4e008cb72314d5ad40bb74283 Mon Sep 17 00:00:00 2001 From: mads-bertelsen-agentic <301266180+mads-bertelsen-agentic@users.noreply.github.com> Date: Wed, 9 Sep 2026 14:37:24 +0200 Subject: [PATCH 2/3] Simplify PROP_NO_G regression test --- .../Unittest_PROP_NO_G.instr | 99 +++---------------- 1 file changed, 14 insertions(+), 85 deletions(-) diff --git a/mcstas-comps/examples/Tests_grammar/Unittest_PROP_NO_G/Unittest_PROP_NO_G.instr b/mcstas-comps/examples/Tests_grammar/Unittest_PROP_NO_G/Unittest_PROP_NO_G.instr index 39932278d3..e3aeed9017 100644 --- a/mcstas-comps/examples/Tests_grammar/Unittest_PROP_NO_G/Unittest_PROP_NO_G.instr +++ b/mcstas-comps/examples/Tests_grammar/Unittest_PROP_NO_G/Unittest_PROP_NO_G.instr @@ -9,21 +9,18 @@ * Origin: McStas * %INSTRUMENT_SITE: Tests_grammar * -* Unittest for the PROP_NO_G_* propagation macros. +* Test for the PROP_NO_G_DT propagation macro. * * %Description -* Verifies straight-line propagation with global gravity enabled and checks -* that PROP_NO_G_DT retains the normal back-propagation behavior. +* A long-wavelength beam is focused onto a small detector 10 m away. The +* intermediate Arm uses PROP_NO_G_DT, so the beam should remain focused even +* when gravity is enabled. * -* %Example: Backprop=0 -g Detector: PropNoGMonitor_I=1 -* %Example: Backprop=1 -g Detector: PropNoGMonitor_I=1 -* -* %Parameters -* Backprop: [1] Flag to allow the final negative propagation +* %Example: -g Detector: PropNoGMonitor_I=7.95774e-8 * * %End *******************************************************************************/ -DEFINE INSTRUMENT Unittest_PROP_NO_G(int Backprop=0) +DEFINE INSTRUMENT Unittest_PROP_NO_G() TRACE @@ -32,91 +29,23 @@ COMPONENT Origin = Progress_bar() COMPONENT Source = Source_simple( radius = 0.01, - dist = 1, - focus_xw = 0.1, - focus_yh = 0.1, - lambda0 = 5, + dist = 10, + focus_xw = 0.01, + focus_yh = 0.01, + lambda0 = 30, dlambda = 0, flux = 0) AT (0, 0, 0) RELATIVE Origin -EXTEND %{ - x = 0; y = 0; z = 0; - vx = 1; vy = 1; vz = 1; t = 0; - p = 1.0/mcget_ncount(); -%} COMPONENT PropNoG = Arm() AT (0, 0, 0) RELATIVE Source EXTEND %{ - int failed = 0; - const double tolerance = 1e-12; - - p = 1.0/mcget_ncount(); - - x = 1; y = 1; z = 1; - vx = 2; vy = 3; vz = 4; t = 0; - PROP_NO_G_DT(0.5); - if (fabs(x - 2) > tolerance || fabs(y - 2.5) > tolerance || - fabs(z - 3) > tolerance || fabs(t - 0.5) > tolerance || - fabs(vx - 2) > tolerance || fabs(vy - 3) > tolerance || - fabs(vz - 4) > tolerance) - failed = 1; - - x = 1; y = 1; z = 1; - vx = -2; vy = 3; vz = 4; t = 0; - PROP_NO_G_X0; - if (fabs(x) > tolerance || fabs(y - 2.5) > tolerance || - fabs(z - 3) > tolerance || fabs(t - 0.5) > tolerance || - fabs(vx + 2) > tolerance || fabs(vy - 3) > tolerance || - fabs(vz - 4) > tolerance) - failed = 1; - - x = 1; y = 1; z = 1; - vx = 2; vy = -3; vz = 4; t = 0; - PROP_NO_G_Y0; - if (fabs(x - 5.0/3.0) > tolerance || fabs(y) > tolerance || - fabs(z - 7.0/3.0) > tolerance || fabs(t - 1.0/3.0) > tolerance || - fabs(vx - 2) > tolerance || fabs(vy + 3) > tolerance || - fabs(vz - 4) > tolerance) - failed = 1; - - x = 1; y = 1; z = 1; - vx = 2; vy = 3; vz = -4; t = 0; - PROP_NO_G_Z0; - if (fabs(x - 1.5) > tolerance || fabs(y - 1.75) > tolerance || - fabs(z) > tolerance || fabs(t - 0.25) > tolerance || - fabs(vx - 2) > tolerance || fabs(vy - 3) > tolerance || - fabs(vz + 4) > tolerance) - failed = 1; - - if (failed) - ABSORB; - - if (INSTRUMENT_GETPAR(Backprop)) - ALLOW_BACKPROP; - PROP_NO_G_DT(-0.1); -%} - -COMPONENT PropNoGCheck = Arm() - AT (0, 0, 0) RELATIVE PropNoG -EXTEND %{ - const double tolerance = 1e-12; - const int backprop = INSTRUMENT_GETPAR(Backprop); - const double expected_x = backprop ? 1.3 : 0; - const double expected_y = backprop ? 1.45 : 0; - const double expected_z = backprop ? 0.4 : 0; - const double expected_t = backprop ? 0.15 : 0; - - if (fabs(x - expected_x) > tolerance || - fabs(y - expected_y) > tolerance || - fabs(z - expected_z) > tolerance || - fabs(t - expected_t) > tolerance) - ABSORB; + PROP_NO_G_DT(10/vz); %} COMPONENT PropNoGMonitor = Monitor( - xwidth = 10, - yheight = 10) - AT (0, 0, 0) RELATIVE PropNoG + xwidth = 0.01, + yheight = 0.01) + AT (0, 0, 10) RELATIVE PropNoG END From ef3656d438d096c4dfcfc13a87dad1bb65773a5c Mon Sep 17 00:00:00 2001 From: mads-bertelsen-agentic <301266180+mads-bertelsen-agentic@users.noreply.github.com> Date: Thu, 10 Sep 2026 08:29:52 +0200 Subject: [PATCH 3/3] Update PROP_NO_G test metadata --- .../Tests_grammar/Unittest_PROP_NO_G/Unittest_PROP_NO_G.instr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mcstas-comps/examples/Tests_grammar/Unittest_PROP_NO_G/Unittest_PROP_NO_G.instr b/mcstas-comps/examples/Tests_grammar/Unittest_PROP_NO_G/Unittest_PROP_NO_G.instr index e3aeed9017..7895f43e35 100644 --- a/mcstas-comps/examples/Tests_grammar/Unittest_PROP_NO_G/Unittest_PROP_NO_G.instr +++ b/mcstas-comps/examples/Tests_grammar/Unittest_PROP_NO_G/Unittest_PROP_NO_G.instr @@ -4,9 +4,9 @@ * Instrument: Unittest_PROP_NO_G * * %Identification -* Written by: McStas developers +* Written by: Mads Bertelsen * Date: September 2026 -* Origin: McStas +* Origin: ESS * %INSTRUMENT_SITE: Tests_grammar * * Test for the PROP_NO_G_DT propagation macro.