You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Source_Optimizer / Monitor_Optimizer: do not compile, and give biased intensities #3220
sources/Source_Optimizer.comp and sources/Monitor_Optimizer.comp have no example instrument and do not compile. The same errors are in McStas 2.7.2.
Compile errors
The SHARE structs lack a closing ;.
DECLARE uses the struct types without the struct keyword.
Wrong intensity after fixing compilation
Setup: Source_simple, then Source_Optimizer, a Slit in x, Monitor_Optimizer and an L_monitor. 1e6 neutrons, three seeds each.
Case
lmon_I
vs. no optimizer
No optimizer
1.25e8
–
SetXY
1.02e8
−18%
SetV
7.9e7
−37%
default
7.9e7 – 8.4e7
≈ −35%
The slit sits at the optimizer plane and does not depend on velocity, so SetV should reproduce the reference exactly. With the slit 2 m downstream, results spread from 2.3e8 to 7.4e8 across seeds (the reference is 2.5e8).
Cause
When a bin is "full", the neutron is moved to another position or velocity bin.
It is then reweighted by a product of 1D Reference/Source ratios.
That weight does not match the density the neutrons were actually sampled from, and it ignores correlations between variables.
The "passing" flux grows with each optimisation step.
The per-neutron state is kept in the shared Vars struct. Moving it to USERVARS would fix thread/GPU safety, but not the bias.
Options
Rewrite as proper importance sampling: draw from the optimised distribution and weight by the true density ratio.
Move both components to obsolete/.
Found during the component consolidation in #3219.
Alternative: generalise Source_adapt / Adapt_check instead of fixing the optimizers
Source_adapt already does adaptive importance sampling correctly: neutrons are drawn from a learned bin distribution and weighted by the sampling-probability ratio, with beta keeping a uniform fraction to avoid false learning. In #3219, Test_Source_adapt matches Source_simple to within 0.3%.
Current limits:
It learns only in (E, x, horizontal divergence), and only for its own rectangular flat source.
It does not work properly with MPI.
A possible generalisation is an adaptive layer placed after any source, as Source_Optimizer intended. It would re-sample in a chosen set of variables, possibly also y and vertical divergence, with the same proper weighting and Adapt_check as the feedback. Then both optimizers could move to obsolete/ rather than being rewritten.
Activity
willend commentedon Nov 29, 2026
Alternative: generalise
Source_adapt/Adapt_checkinstead of fixing the optimizersSource_adaptalready does adaptive importance sampling correctly: neutrons are drawn from a learned bin distribution and weighted by the sampling-probability ratio, withbetakeeping a uniform fraction to avoid false learning. In #3219,Test_Source_adaptmatchesSource_simpleto within 0.3%.Current limits:
A possible generalisation is an adaptive layer placed after any source, as
Source_Optimizerintended. It would re-sample in a chosen set of variables, possibly also y and vertical divergence, with the same proper weighting andAdapt_checkas the feedback. Then both optimizers could move toobsolete/rather than being rewritten.