Random Ray Linear Source Gradient Limiter - #4121
Open
jtramm wants to merge 24 commits into
Open
Conversation
Spurious gradients from noisy fitted moments (amplified by near-singular moment matrices in thin regions) can drive the modeled linear source negative over part of a region. Rays crossing the negative lobe carry negative angular flux downstream, and optically thin media with scattering ratios near one can amplify the exported negativity without bound, producing severe negative-flux instability in shielding-type problems. The limiter rescales a group's gradient whenever the worst-case overshoot of the linear term over the region's moment ellipsoid, sqrt(3 g^T M g), exceeds the flat source, capping the overshoot at the flat source. The flat component is untouched and the linear term integrates to zero over the region, so mean emission is preserved exactly, and gradients that pass the test are left bit-identical. A group whose flat source is negative has its gradient zeroed, which subsumes the previous negative-source gate at the gradient computation and applies after the external source is added rather than before. Groups in which the region is optically thick along the gradient direction are exempt, since steep fits across a thick span are physical and clipping them produces an error that compounds with depth in deep penetration problems. The warmup batch count also gains a named constant. Linear source reference results are regenerated and a new regression test exercises a starved linear configuration. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
The ellipsoid bound covers the region as described by its spatial moments, not the true region shape. Corners of box-like or mesh-cut regions lie beyond it by up to sqrt(3), so the limiter reduces rather than eliminates modeled-source negativity for arbitrary shapes. Say so in the code comment, the methods derivation, and the user's guide note. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
The limiter is enabled with source_gradient_limiter in the random_ray settings dictionary. When enabled it applies universally, with the optical-thickness exemption removed. When disabled, linear source handling is identical to develop, so the regenerated linear source reference results revert to their develop versions and the stability regression test now pins the limiter on explicitly. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
The sqrt(3) corner-distance cap is only true for boxes, and OpenMC regions are arbitrary CSG shapes. State instead that the bound is exact for the moment ellipsoid and that real shapes can extend beyond it, so residual negativity is possible. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
… reference Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
Random ray constants belong beside the solver that uses them rather than in the global constants header, following the convention the adaptive volume estimator work established in flat_source_domain.h. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
The limiter no longer alters develop's gradient gate, so with the limiter on a group whose scattering and fission source is negative falls back to a flat source exactly as it does with the limiter off, and the limiter is a pure addition after the external source is added. Its negative-flat case folds into the rescale by capping at zero, which scales such a gradient away without a separate branch. In the regime where the two gates can differ, source-bearing regions whose scattering source has gone negative, the change moves the thin hall's negative bin count by three in four and a half thousand. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
MPACT reduces the gradient by the same mean-preserving factor, but finds each region's minimum source exactly over the entrance and exit points of every segment crossing it, which requires the fixed track set that deterministic MOC lays down once. Random ray samples new rays every batch, so the moment ellipsoid bound stands in for that exact minimum. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
The bound was described as a worst case scaled to physical extents by a uniform-interval argument, which holds only for a box. The limiter treats a region as if it were shaped like its moment representation, which an arbitrary CSG region essentially never is, so it reduces negative sources rather than eliminating them. The methods section, the user's guide, and the code comment now say that and no more. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
Naming the ten-batch warmup was a cleanup riding along with the limiter, and the limiter no longer touches that line. Restoring develop's line keeps this change to the limiter itself. The warmup count can be named in its own small cleanup. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
The bound sqrt(3 g^T M g) keeps the modeled source non-negative within sqrt(3) standard deviations of the centroid along the gradient, the half-extent of a uniform slab with the region's second moment. It does not cover the ellipsoid a uniform body with those moments actually fills, which reaches sqrt(5) standard deviations for a sphere and three for a cube's corner, so describing it as non-negative over the moment ellipsoid overstated it. The docs, the setting's description, and the code comment now state the slab extent and the residual it leaves. The comment also no longer attributes the zero cap to negative adjoint sources, which OpenMC never produces. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
The moment ellipsoid bound kept the modeled source non-negative only within sqrt(3) standard deviations of the centroid, the extent of a slab, so a region shaped like a sphere or a box still went negative after limiting. The limiter now bounds the linear term over the region's axis-aligned bounding box as sampled by the ray segment endpoints, which lie on the region boundary. The box contains the region, so the modeled source is non-negative throughout it once the boundary has been sampled, exactly for box-shaped regions and conservatively for rounded ones. The box is six values per region, stored only for linear source solves with the limiter enabled, and grown with twelve comparisons per segment inside the lock the linear source already holds. With the limiter off nothing is stored or compared, and the linear source reference results are unchanged. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
The limiter's extent update ran six unconditional stores per segment endpoint inside the source region lock, which cost about 6% of the transport sweep on coarse-mesh C5G7. Writing only when a bound moves leaves the update as untaken compares once the box has converged, and the sweep cost falls within run-to-run noise (about 1%). Results are unchanged. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
The methods section gives the corner bound over the sampled box in place of the moment-ellipsoid bound, the user's guide and the settings descriptions name the box, and the MPACT comparison points to the box as the stand-in for the exact segment minimum. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
The offsets are the distances from the centroid to the box faces, not half-extents. Also drop the <cmath> include left over from the square root of the earlier bound. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
Non-negativity of the modeled source depends on how far the linear term dips below zero over the box, which is reached at the corner each gradient component points away from. The limiter had bounded the term's maximum instead, which coincides with the dip only when the centroid sits at the center of its box, so regions cut by curved or oblique boundaries were limited against the wrong corner: under-limited when the gradient dipped toward the far face and over-limited when it dipped toward the near face. The rescale factor is now the flat source over the dip, which is the mean-preserving factor of MPACT's limited linear source approximation with the box minimum in place of the segment minimum. The regression test's cubic regions all had centered centroids and could not tell the two bounds apart, so it now uses spherical source and void regions whose boundaries cut the overlay mesh into off-center pieces. Its reference results are regenerated; it runs in 0.3 s at 2 threads. Also drops the <algorithm> include the header no longer needs. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
The methods section now gives the minimum of the linear term over the box and the rescale that brings the modeled source to zero at that corner, notes that only segments past a ray's inactive length feed the box, softens the claim that rays crossing a negative lobe always carry negative flux, and gains a label so the user's guide can link straight to it. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
The adjoint reset zeroed the centroids but kept the forward run's bounding boxes, so for the first adjoint batch the centroid sat outside its box. Harmless, since the gradients are zero then, but the limiter's offsets are meant to be measured from a centroid inside the box, so the boxes are now reset alongside the centroids and re-sampled. The random ray summary now states whether the limiter is on for linear source runs, an unused accessor is dropped, and the regression test's comment names the physically steep absorber gradients the limiter clips there as well as the noisy ones. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
The bound is exact for axis-aligned boxes only, and an oblique thin region can be limited far more than a sphere's factor of sqrt(3), which had read as the worst case. The box contains the region once its extreme points have been sampled, which is a consequence of the sampling rather than a premise. Two paragraphs are reflowed. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
Brings in the adaptive volume estimators (openmc-dev#4110). The source region container now takes both the adaptive flags and the limiter's extent flag, and the limiter block follows the adaptive estimator's demoted-region gradient zeroing in the linear source update. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P9rxXRzNh13xQBGzePHEeK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR adds an optional limiter for linear source gradients. It is off by default, but can be enabled via:
Background
The fitted gradient inverts the region's spatial moment matrix, which amplifies noise along any thin extent of the region, so a poorly sampled region can carry a spuriously steep gradient. Such a region emits a negative source over part of its extent, and rays crossing the negative lobe carry negative angular flux downstream.
When enabled, the limiter rescales the gradient so the modeled source stays non-negative over the region's bounding box, preserving mean emission exactly. Linear source mode only, flat source solves are unaffected.
A similar gradient limiting treatment exists in production MOC as MPACT's limited linear source approximation, developed by Sooyoung Choi. MPACT computes each region's minimum source exactly, as the smaller of the modeled source at the entrance and exit of every segment crossing the region, over all rays and angles, and when that minimum is negative it rescales the gradient by the mean-preserving factor q/(q − q_min).
The reduction factor being introduced here follows similar logic. What differs is how the minimum is found. MPACT's exact minimum needs the whole track set before the sweep, which deterministic MOC has because its rays are laid down once and reused every iteration. Random ray samples new rays every batch, so no segment set exists when the source is built. Instead, this PR bounds the modeled source over the region's axis-aligned bounding box. Ray segment endpoints lie on the region boundary (or inside it, where a ray starts or ends), so accumulating their componentwise minimum and maximum over the batches gives a box that, once the region's extreme points along each axis have been hit, contains the region. The lowest value the linear term can take over the box is at a corner, a sum computed from the gradient components and the centroid's offsets to the box faces. When the flat source does not cover that dip, the gradient is scaled by their ratio, which is MPACT's factor with the box minimum standing in for the segment minimum.
The box only grows, since the geometry is static, and after the first few batches it covers the region. The bound is exact for axis-aligned box regions and conservative for others: a sphere is limited by up to sqrt(3) more than necessary, and a thin region lying diagonally to the axes by much more, since its bounding box is far larger than the region. It costs two positions per source region, accumulated only when the limiter is enabled.
The limiter is off by default because limiting cuts both ways. A steep fit can be noise, but it can also be physical. The clearest case is deep penetration, where a linear fit to an exponentially attenuated flux legitimately crosses zero near the far edge of a source region, and clipping it produces an error that compounds with depth. The results below quantify both sides.
I experimented with a number of methods for attempting to dynamically limit gradients in specific source regions when certain problematic conditions were detected. However, I was generally not able to find a strategy that worked in all types of cases, so am leaving that as a potential for future work. Users should therefore enable the limiter when they observe negative sources destabilizing a linear source simulation.
This PR is part of a set of recent numerical improvements to the random ray solver, e.g., the adaptive volume estimator PR (#4110), and the linear source consistency PR (#4111). These PRs are reviewable in any order. All results below are measured against a "baseline" of the combination of #4110 and #4111 together with the limiter disabled, so the deltas shown are what enabling the limiter does on top of everything else. Both arms of every comparison run the same binary and differ only in the setting.
Results
Stability was measured across a range of ray densities, three estimators, and four problems, all with linear sources. The test problems:
Thin hall with the naive estimator, varying the ray density (gradient limiter off -> on):
As we can see above, limiting gradients does greatly reduce negative flux occurrences. However, at lower ray densities, this can also come with a small loss of accuracy.
Now we look at a wider variety of problems and volume estimators:
The limiter removes all of the negativity in the problems where noisy gradients cause it. The TLD naive case drops from 3,250 negative bins to zero, and the thin hall reaches zero at every density and seed. The hybrid row is unaffected because its instability comes from the volume estimator issue addressed in #4110 rather than from source gradients. The collisional hall and the TLD auto case retain a residual because part of their negativity arises in the flat source and flux themselves, which the limiter leaves alone by design.
The flux error column shows the cost. Enabling the limiter shifts the flux in these problems by up to about two percentage points, nearly always in the negative direction. This is the clipping bias. The walls of these problems carry physically steep gradients, and limiting them reduces transmission. In problems where steep gradients dominate the cost is larger, as the slab results below show. This is the tradeoff that makes the limiter an option rather than a default.
The cost where linear sources are working well
Coarse-mesh C5G7 (102x102 overlay, ~15k source regions) is the case where linear sources deliver a large accuracy win over flat. Here we vary the ray density and observe the eigenvalue and pin power (AAPE) errors.
At fine ray counts the limiter costs about 0.2 AAPE and about 100 pcm, and limited linear sources still beat flat (by 1.75x at 200 rays and 1.3x at 100). Under heavy ray starvation it removes most of the large k bias that noisy gradients otherwise develop and pulls the pin powers back toward the flat-source level.
Runtime
The limiter's cost is the bounding-box update, two endpoint comparisons per ray segment inside the source region lock, plus a few operations per region and group in the source update. Measured on the 200-ray C5G7 case above (3,000 batches, 4 threads, three repeats per arm, run back to back on an otherwise idle machine):
MPACT reports 5--6% for its limited linear source approximation, most of it in the segment search. Since the box is not needed when the limiter is off, the bookkeeping exists only when it is on, and flat source and unlimited linear source runs carry no cost. Enabling the limiter on a well-resolved problem is mildly harmful and unnecessary, which is the fine-ray half of the default-off rationale.
Where steep gradients are physical
The other half of the default-off rationale is that whole problem classes offer the limiter nothing to fix. The test problem is a slab 24 mean free paths deep, with a source at one end, mild scattering (c = 0.3), and source regions 3 mean free paths thick. The reference is a discretization-converged fine-mesh flat-source run (0.1 MFP regions). Flux errors at depth:
Linear sources are the whole game on this problem, cutting the deep-flux error from +594% to -22% at this coarse region size. Every fitted gradient here is physical. The flux decays exponentially, so the linear fit legitimately crosses zero near each region's far edge, and the limiter clips every one of them. The result is not a breakdown. Enabling the limiter trades the -22% under-prediction for a +32% over-prediction at every depth. Both errors shrink as regions are refined: at 1.5 mean free paths the deepest bin is -1.9% with or without the limiter, which by then fires only at the 1e-7 level, and at 1.0 it is -0.6% with the limiter fully quiet. But at the coarse size the limited answer comes from discarding correct shape information, so on a problem like this the limiter changes every deep result while having nothing to offer.
Compatibility and testing
random_ray_linear_source_stabilityregression test pins the limiter on in a naive linear source configuration whose spherical region boundaries cut the overlay mesh into pieces with off-center centroids, the geometry where the bound's asymmetry matters.Checklist