Skip to main content

nereids_endf/
resonance.rs

1//! Resonance parameter data structures.
2//!
3//! These types represent parsed ENDF-6 File 2 resonance data, organized
4//! following the structure in SAMMY's `SammyRMatrixParameters.h`.
5//!
6//! ## SAMMY Reference
7//! - `sammy/external/openScale/repo/packages/ScaleUtils/EndfLib/RMatResonanceParam.h`
8//! - `sammy/src/endf/SammyRMatrixParameters.h`
9
10use nereids_core::types::Isotope;
11use serde::{Deserialize, Serialize};
12
13// ─── ENDF TAB1: one-dimensional interpolation table ──────────────────────────
14//
15// TAB1 records encode a piecewise function y(x) with up to 5 interpolation laws
16// (ENDF INT codes 1–5).  Used here for the energy-dependent scattering radius
17// AP(E) when NRO=1.
18//
19// Reference: ENDF-6 Formats Manual §0.5 (TAB1 record type)
20
21/// One-dimensional interpolation table (ENDF TAB1 record).
22///
23/// Stores piecewise-interpolated y(x) data.  Multiple interpolation regions
24/// are supported via ENDF NBT/INT boundary pairs.
25///
26/// Interpolation law codes (ENDF INT), per ENDF-6 Formats Manual §0.5:
27/// - 1: Histogram (y constant = y_left)
28/// - 2: Linear-linear
29/// - 3: Log in x, linear in y  (y linear in ln(x))
30/// - 4: Linear in x, log in y  (ln(y) linear in x)
31/// - 5: Log-log
32///
33/// Verified against SAMMY OpenScale `CELibrary/Interpolate.h`:
34///   case 3 → `LinByLog` = log-x/linear-y
35///   case 4 → `LogByLin` = linear-x/log-y
36///
37/// Reference: ENDF-6 Formats Manual §0.5; SAMMY OpenScale `CELibrary/Interpolate.h`
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct Tab1 {
40    /// Interpolation region boundaries (NBT, 1-based index of the last point
41    /// in each region).  `boundaries.len() == interp_codes.len()`.
42    pub boundaries: Vec<usize>,
43    /// Interpolation law codes (INT) for each region.
44    pub interp_codes: Vec<u32>,
45    /// Data points as (x, y) pairs, sorted ascending in x.
46    pub points: Vec<(f64, f64)>,
47}
48
49impl Tab1 {
50    /// Evaluate the tabulated function at `x` by piecewise interpolation.
51    ///
52    /// Values outside the tabulated range are clamped to the nearest endpoint
53    /// (no extrapolation).
54    ///
55    /// Log-interpolation modes (INT=3, 4, 5) require strictly positive
56    /// arguments for the logarithm.  If a tabulated value or x-coordinate
57    /// is non-positive where a logarithm would be taken, the function
58    /// transparently falls back to lin-lin interpolation for that interval
59    /// rather than producing NaN or panicking.  In practice, ENDF AP(E)
60    /// tables always have positive x (energy) and positive y (radius in fm),
61    /// so this guard is defensive only.
62    pub fn evaluate(&self, x: f64) -> f64 {
63        let pts = &self.points;
64        if pts.is_empty() {
65            // The parser rejects NP=0, so an empty table indicates a bug in
66            // test-code construction.  Panic in debug builds; return 0.0 in
67            // release to avoid UB.
68            debug_assert!(
69                !pts.is_empty(),
70                "Tab1::evaluate called with empty points table"
71            );
72            return 0.0;
73        }
74        // NaN/±inf: partition_point's comparisons are all false for NaN,
75        // returning index 0, and pts[0 - 1] would underflow.  Clamp to the
76        // nearest finite endpoint instead.
77        if !x.is_finite() {
78            debug_assert!(x.is_finite(), "Tab1::evaluate: non-finite argument {x}");
79            return if x > 0.0 {
80                pts[pts.len() - 1].1
81            } else {
82                pts[0].1
83            };
84        }
85        if x <= pts[0].0 {
86            return pts[0].1;
87        }
88        if x >= pts[pts.len() - 1].0 {
89            return pts[pts.len() - 1].1;
90        }
91
92        // Binary search: find the first index where pts[i].0 > x.
93        // The interval containing x is [pts[i-1], pts[i]].
94        // Because the outer clamps ensure pts[0].0 < x < pts[last].0,
95        // we are guaranteed x0 < x1 (strict), so (x1 - x0) > 0.
96        let i = pts.partition_point(|(xi, _)| *xi <= x);
97        let (x0, y0) = pts[i - 1];
98        let (x1, y1) = pts[i];
99
100        // Fallback to lin-lin for any interval; used when log guards fire.
101        let lin_lin = || {
102            let t = (x - x0) / (x1 - x0);
103            y0 + t * (y1 - y0)
104        };
105
106        match self.interp_code_for_interval(i - 1) {
107            1 => y0, // histogram: constant left value
108            3 => {
109                // INT=3: y linear in ln(x) — log in x, linear in y.
110                // SAMMY OpenScale: case 3 → LinByLog (requires x0, x1, x > 0).
111                if x0 > 0.0 && x1 > 0.0 && x > 0.0 {
112                    let t = (x.ln() - x0.ln()) / (x1.ln() - x0.ln());
113                    y0 + t * (y1 - y0)
114                } else {
115                    lin_lin()
116                }
117            }
118            4 => {
119                // INT=4: ln(y) linear in x — linear in x, log in y.
120                // SAMMY OpenScale: case 4 → LogByLin (requires y0, y1 > 0).
121                if y0 > 0.0 && y1 > 0.0 {
122                    let t = (x - x0) / (x1 - x0);
123                    (y0.ln() + t * (y1.ln() - y0.ln())).exp()
124                } else {
125                    lin_lin()
126                }
127            }
128            5 => {
129                // log-log; requires x0, x1, x, y0, y1 > 0
130                if x0 > 0.0 && x1 > 0.0 && x > 0.0 && y0 > 0.0 && y1 > 0.0 {
131                    let t = (x.ln() - x0.ln()) / (x1.ln() - x0.ln());
132                    (y0.ln() + t * (y1.ln() - y0.ln())).exp()
133                } else {
134                    lin_lin()
135                }
136            }
137            _ => {
138                // INT=2 (lin-lin) and any unknown code: linear interpolation
139                lin_lin()
140            }
141        }
142    }
143
144    /// Return the ENDF interpolation code for the interval [pts[idx], pts[idx+1]].
145    ///
146    /// ENDF NBT boundaries are 1-based indices of the *last point* in each region.
147    /// Interval `idx` (0-based) belongs to the first region j where `idx + 2 <= NBT[j]`.
148    fn interp_code_for_interval(&self, idx: usize) -> u32 {
149        for (j, &nbt) in self.boundaries.iter().enumerate() {
150            if idx + 2 <= nbt {
151                return self.interp_codes[j];
152            }
153        }
154        self.interp_codes.last().copied().unwrap_or(2)
155    }
156}
157
158/// Resonance formalism flag (ENDF LRF values).
159///
160/// Reference: ENDF-6 Formats Manual, File 2.
161#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
162pub enum ResonanceFormalism {
163    /// Single-Level Breit-Wigner (LRF=1 with SLBW treatment, or SAMMY LRF=-1).
164    SLBW,
165    /// Multi-Level Breit-Wigner (LRF=2).
166    MLBW,
167    /// Reich-Moore (LRF=3). Primary formalism for light and actinide isotopes.
168    ReichMoore,
169    /// R-Matrix Limited (LRF=7). General multi-channel formalism; used for
170    /// many medium-heavy isotopes (W, Ta, Zr, etc.) in ENDF/B-VIII.0.
171    RMatrixLimited,
172    /// Unresolved Resonance Region (LRU=2). Average cross-sections via
173    /// Hauser-Feshbach formalism. Cross-sections computed in `urr::urr_cross_sections`.
174    Unresolved,
175}
176
177// ─── LRU=2 (Unresolved Resonance Region) Data Structures ─────────────────────
178//
179// The URR uses average level-spacing and width parameters rather than discrete
180// resonances. Cross-sections are computed via the Hauser-Feshbach formula.
181//
182// LRF=1: single energy-independent width set per (L, J); Γ_n derived from
183//        reduced neutron width GNO via Γ_n = 2·P_L·GNO.
184// LRF=2: tabulated energy-dependent widths with an interpolation law per
185//        J-group; supported INT codes enforced by the parser at load time.
186//
187// Reference: ENDF-6 Formats Manual §2.2.2
188
189/// Average widths for one (L, J) combination in the Unresolved Resonance Region.
190///
191/// For LRF=1: `energies` is empty; each width vector has exactly one element.
192/// For LRF=2: all vectors have length NE; `int_code` selects the interpolation
193/// law (INT=1..=5 per ENDF-6 §0.5; validated by the parser, dispatched in
194/// `nereids_physics::urr`).
195///
196/// Reference: ENDF-6 Formats Manual §2.2.2
197#[derive(Debug, Clone, Serialize, Deserialize)]
198pub struct UrrJGroup {
199    /// Total angular momentum J.
200    pub j: f64,
201    /// Neutron χ² degrees of freedom (AMUN).
202    pub amun: f64,
203    /// Fission χ² degrees of freedom (AMUF); 0 for LRF=1 non-fissile.
204    pub amuf: f64,
205    /// Tabulation energies (eV). Empty for LRF=1.
206    pub energies: Vec<f64>,
207    /// Average level spacing D (eV). Single-element for LRF=1.
208    pub d: Vec<f64>,
209    /// Competitive width GX (eV). Single-element 0 for LRF=1.
210    pub gx: Vec<f64>,
211    /// Average neutron width (eV). For LRF=1 this is GNO (reduced width);
212    /// for LRF=2 this is the actual average Γ_n from the table.
213    pub gn: Vec<f64>,
214    /// Average gamma (capture) width GG (eV). Single-element for LRF=1.
215    pub gg: Vec<f64>,
216    /// Average fission width GF (eV). Single-element for LRF=1.
217    pub gf: Vec<f64>,
218    /// Interpolation law for the energy table (LRF=2 only).
219    /// INT=1..=5 per ENDF-6 §0.5 (1: histogram; 2: y linear in E;
220    /// 3: y linear in ln E; 4: ln y linear in E; 5: ln y linear in ln E).
221    /// Ignored for LRF=1 (no table).
222    #[serde(default = "default_int_code")]
223    pub int_code: u32,
224}
225
226fn default_int_code() -> u32 {
227    2
228}
229
230/// Average URR parameters for one L-value.
231///
232/// Reference: ENDF-6 Formats Manual §2.2.2
233#[derive(Debug, Clone, Serialize, Deserialize)]
234pub struct UrrLGroup {
235    /// Orbital angular momentum quantum number.
236    pub l: u32,
237    /// Atomic weight ratio for this L-group.
238    pub awri: f64,
239    /// J-groups within this L-value.
240    pub j_groups: Vec<UrrJGroup>,
241}
242
243/// Complete Unresolved Resonance Region data for one energy range (LRU=2).
244///
245/// Stored in `ResonanceRange::urr` when the range is an URR range.
246///
247/// Reference: ENDF-6 Formats Manual §2.2.2
248#[derive(Debug, Clone, Serialize, Deserialize)]
249pub struct UrrData {
250    /// LRF flag: 1 = single-level BWR (energy-independent widths),
251    ///           2 = multi-level BWR (energy-dependent width tables).
252    pub lrf: u32,
253    /// Target spin I.
254    pub spi: f64,
255    /// Scattering radius AP in fm (converted from ENDF 10⁻¹² cm at parse time).
256    pub ap: f64,
257    /// Lower URR energy bound (eV).
258    pub e_low: f64,
259    /// Upper URR energy bound (eV).
260    pub e_high: f64,
261    /// L-groups (one per orbital angular momentum value).
262    pub l_groups: Vec<UrrLGroup>,
263}
264
265/// Top-level container for all resonance data parsed from an ENDF file.
266#[derive(Debug, Clone, Serialize, Deserialize)]
267pub struct ResonanceData {
268    /// The isotope this data belongs to.
269    pub isotope: Isotope,
270    /// ZA identifier (Z*1000 + A).
271    pub za: u32,
272    /// Atomic weight ratio (mass of target / neutron mass).
273    pub awr: f64,
274    /// Energy ranges containing resonance parameters.
275    pub ranges: Vec<ResonanceRange>,
276}
277
278/// A single energy range within the resolved resonance region.
279#[derive(Debug, Clone, Serialize, Deserialize)]
280pub struct ResonanceRange {
281    /// Lower energy bound (eV).
282    pub energy_low: f64,
283    /// Upper energy bound (eV).
284    pub energy_high: f64,
285    /// Resolved (true) or unresolved (false).
286    pub resolved: bool,
287    /// Resonance formalism used in this range.
288    pub formalism: ResonanceFormalism,
289    /// Target spin (I).
290    pub target_spin: f64,
291    /// Scattering radius (fm).
292    ///
293    /// Constant value from the ENDF CONT header AP field.
294    /// When `ap_table` is `Some`, use `scattering_radius_at(energy_ev)` instead
295    /// of reading this field directly — the table provides the energy-dependent
296    /// value, clamping to the nearest endpoint for energies outside the table
297    /// range.  This constant is only used when `ap_table` is `None` (NRO=0).
298    pub scattering_radius: f64,
299    /// NAPS flag: scattering radius calculation control.
300    ///
301    /// NAPS=0: use the channel radius for penetrability/shift calculations.
302    /// NAPS=1: use the scattering radius (AP or AP(E)) for penetrability/shift.
303    /// Reference: ENDF-6 Formats Manual §2.2.1
304    #[serde(default)]
305    pub naps: i32,
306    /// Energy-dependent scattering radius AP(E) (fm), present when NRO=1.
307    ///
308    /// ENDF-6 §2.2.1: when NRO≠0 a TAB1 record immediately follows the range
309    /// CONT header to give AP(E) as a piecewise function.  At each energy the
310    /// table value replaces the constant `scattering_radius` in penetrability,
311    /// shift, and hard-sphere phase calculations.
312    ///
313    /// `None` when the range has NRO=0 (constant AP).
314    ///
315    /// Reference: ENDF-6 Formats Manual §2.2.1; SAMMY `mlb/mmlb1.f90`
316    #[serde(default)]
317    pub ap_table: Option<Tab1>,
318    /// Spin groups for LRF=1/2/3 (L-grouped). Empty for LRF=7 and LRU=2.
319    pub l_groups: Vec<LGroup>,
320    /// R-Matrix Limited data for LRF=7. `None` for LRF=1/2/3 and LRU=2.
321    pub rml: Option<Box<RmlData>>,
322    /// Unresolved Resonance Region data (LRU=2). `None` for all LRU=1 ranges.
323    ///
324    /// When `Some`, cross-sections are computed via the Hauser-Feshbach
325    /// formula in `nereids_physics::urr::urr_cross_sections`.
326    #[serde(default)]
327    pub urr: Option<Box<UrrData>>,
328    /// R-external (background R-matrix) entries per spin group.
329    ///
330    /// Diagonal, real-valued corrections to the R-matrix that approximate
331    /// the effect of distant (unresolved) resonances.  Keyed by (L, J).
332    ///
333    /// Populated from SAMMY's "R-EXTERNAL PARAMETERS FOLLOW" section.
334    /// Empty for ENDF-only data or SAMMY cases without R-external.
335    ///
336    /// SAMMY Ref: Manual Section II.B.1.d, mpar03.f90 Readrx
337    #[serde(default)]
338    pub r_external: Vec<RExternalEntry>,
339}
340
341/// Parameters grouped by orbital angular momentum L.
342///
343/// In ENDF File 2 (LRF=3, Reich-Moore), resonances are grouped by L-value.
344/// Each L-group contains resonances with different J values.
345#[derive(Debug, Clone, Serialize, Deserialize)]
346pub struct LGroup {
347    /// Orbital angular momentum quantum number.
348    pub l: u32,
349    /// Atomic weight ratio for this group.
350    pub awr: f64,
351    /// Channel scattering radius for this L (fm). 0.0 means use the global value.
352    pub apl: f64,
353    /// Q-value for competitive width (eV). Only meaningful for BW formalisms
354    /// (LRF=1/2) where LRX=1; zero otherwise.
355    /// Reference: ENDF-6 Formats Manual §2.2.1.1, L-value CONT record (C2 field).
356    #[serde(default)]
357    pub qx: f64,
358    /// Competitive width flag. LRX=0: no competitive width; LRX=1: competitive
359    /// reaction exists (width = GT - GN - GG - GF). Only used in BW formalisms.
360    /// Reference: ENDF-6 Formats Manual §2.2.1.1, L-value CONT record (L2 field).
361    #[serde(default)]
362    pub lrx: i32,
363    /// Individual resonances in this L-group.
364    pub resonances: Vec<Resonance>,
365}
366
367/// A single resonance entry.
368///
369/// The meaning of the width fields depends on the formalism:
370///
371/// ## Reich-Moore (LRF=3)
372/// - `gn`: Neutron width Γn (eV)
373/// - `gg`: Radiation (gamma) width Γγ (eV)
374/// - `gfa`: First fission width Γf1 (eV), 0.0 if non-fissile
375/// - `gfb`: Second fission width Γf2 (eV), 0.0 if non-fissile
376///
377/// ## SLBW/MLBW (LRF=1/2)
378/// - `gn`: Neutron width Γn (eV)
379/// - `gg`: Radiation width Γγ (eV)
380/// - `gfa`: Fission width Γf (eV)
381/// - `gfb`: Not used (0.0)
382///
383/// Reference: ENDF-6 Formats Manual, Section 2.2.1
384/// Reference: SAMMY manual, Section 2 (Scattering Theory)
385#[derive(Debug, Clone, Serialize, Deserialize)]
386pub struct Resonance {
387    /// Resonance energy (eV).
388    pub energy: f64,
389    /// Total angular momentum J.
390    pub j: f64,
391    /// Neutron width Γn (eV).
392    pub gn: f64,
393    /// Radiation (capture/gamma) width Γγ (eV).
394    pub gg: f64,
395    /// First fission width (eV). Zero for non-fissile isotopes.
396    pub gfa: f64,
397    /// Second fission width (eV). Zero for non-fissile isotopes.
398    pub gfb: f64,
399}
400
401// ─── R-External (Background R-Matrix) ─────────────────────────────────────────
402
403/// R-external (background R-matrix) parameters for a single spin group channel.
404///
405/// Parameterizes smooth R-matrix contribution from distant (unresolved)
406/// resonances.  The background R-matrix is diagonal and real-valued,
407/// parameterized as a logarithmic polynomial in energy.
408///
409/// ## Formula
410/// ```text
411/// R_ext(E) = R_con + R_lin·E + R_quad·E²
412///          + s_lin·(E_up − E_low)
413///          − (s_con + s_lin·E)·ln[(E_up − E) / (E − E_low)]
414/// ```
415///
416/// SAMMY Ref: Manual Section II.B.1.d, mcro2.f90 lines 180-193
417#[derive(Debug, Clone, Default, Serialize, Deserialize)]
418pub struct RExternalEntry {
419    /// Orbital angular momentum L of the spin group.
420    pub l: u32,
421    /// Total angular momentum J (signed, per SAMMY convention).
422    pub j: f64,
423    /// Lower energy bound (eV).
424    pub e_low: f64,
425    /// Upper energy bound (eV).
426    pub e_up: f64,
427    /// Constant term in R-matrix polynomial.
428    pub r_con: f64,
429    /// Linear coefficient (eV⁻¹).
430    pub r_lin: f64,
431    /// Constant logarithmic coefficient.
432    pub s_con: f64,
433    /// Linear logarithmic coefficient (eV⁻¹).
434    pub s_lin: f64,
435    /// Quadratic coefficient (eV⁻²).
436    pub r_quad: f64,
437}
438
439impl RExternalEntry {
440    /// Evaluate R_ext(E) at the given energy.
441    ///
442    /// The polynomial part (`r_con + r_lin·E + r_quad·E²`) applies at all
443    /// energies.  The logarithmic terms are only added when `E` is strictly
444    /// inside `(e_low, e_up)`.
445    ///
446    /// SAMMY Ref: mcro2.f90 Setr_Cro, lines 180-193
447    pub fn evaluate(&self, energy_ev: f64) -> f64 {
448        let e = energy_ev;
449        let mut r = self.r_con + self.r_lin * e + self.r_quad * e * e;
450
451        let e_up_diff = self.e_up - e;
452        let e_low_diff = e - self.e_low;
453        if e_up_diff > 0.0 && e_low_diff > 0.0 {
454            let log_val = (e_up_diff / e_low_diff).ln();
455            r -= (self.s_con + self.s_lin * e) * log_val;
456            r += self.s_lin * (self.e_up - self.e_low);
457        }
458
459        r
460    }
461}
462
463// ─── LRF=7 (R-Matrix Limited) Data Structures ────────────────────────────────
464//
465// LRF=7 organizes resonances by spin group (J,π) rather than L-value.
466// Each spin group has multiple explicit reaction channels. Resonances carry
467// reduced width amplitudes γ per channel, not formal widths Γ.
468//
469// Reference: ENDF-6 Formats Manual §2.2.1.6; SAMMY manual Ch. 3
470// SAMMY source: rml/mrml01.f (reader), rml/mrml11.f (cross-section calc)
471
472/// Particle pair definition for LRF=7 R-Matrix Limited.
473///
474/// Identifies the two particles in a reaction channel (e.g., neutron + W-184,
475/// or gamma + W-185). Used to determine which channels are entrance (neutron)
476/// channels and which are exit (fission, capture) channels.
477///
478/// Reference: ENDF-6 Formats Manual §2.2.1.6, Table 2.2
479#[derive(Debug, Clone, Serialize, Deserialize)]
480pub struct ParticlePair {
481    /// Mass of particle a (neutron = 1.0, in neutron mass units).
482    pub ma: f64,
483    /// Mass of particle b (target nucleus, in neutron mass units).
484    pub mb: f64,
485    /// Charge number Z of particle a, as stored in the ENDF LRF=7 particle-pair list.
486    /// ENDF LRF=7 stores the charge directly: neutron/photon = 0, proton = 1, alpha = 2.
487    /// Reference: SAMMY rml/mrml03.f — `Docoul = Kzb * Kza` (product of charges).
488    pub za: f64,
489    /// Charge number Z of particle b (target or recoil), as stored in ENDF LRF=7.
490    pub zb: f64,
491    /// Spin of particle a (1/2 for neutron).
492    pub ia: f64,
493    /// Spin of particle b (target spin I).
494    pub ib: f64,
495    /// Q-value for this reaction (eV). 0 for elastic.
496    pub q: f64,
497    /// Penetrability flag (ENDF `PNT`, SAMMY `Lpent`).
498    ///
499    /// `PNT=1`: calculate penetrability P_c and shift S_c analytically
500    /// (Blatt-Weisskopf / Coulomb). Used for open particle channels.
501    /// `PNT=0`: no penetrability — the channel contributes only the
502    /// `Ymat(2,Ii) -= 1` term (SAMMY `rml/mrml07.f:118-122`), encoded here as
503    /// `P_c=1, S_c=B_c`. Always the case for the photon/eliminated channel.
504    /// `PNT∉{0,1}` is rejected at parse time (SAMMY `Check_Quantum`,
505    /// `rml/mrml03.f:22`).
506    pub pnt: i32,
507    /// Shift factor flag.
508    ///
509    /// `SHF=1`: calculate shift factor S_c analytically (Blatt-Weisskopf).
510    /// `SHF=0`: do not calculate; treat S_c = B_c so (S_c − B_c) = 0 in level matrix.
511    pub shf: i32,
512    /// ENDF MT number identifying the reaction (2=elastic, 18=fission, 102=capture).
513    pub mt: u32,
514    /// Parity of particle a.
515    pub pa: f64,
516    /// Parity of particle b.
517    pub pb: f64,
518}
519
520/// A single reaction channel within an LRF=7 spin group.
521///
522/// Specifies which particle pair, what orbital angular momentum, and the
523/// radii used to compute penetrabilities and hard-sphere phase shifts.
524///
525/// Reference: ENDF-6 Formats Manual §2.2.1.6, Table 2.3
526#[derive(Debug, Clone, Serialize, Deserialize)]
527pub struct RmlChannel {
528    /// Index into the parent `RmlData::particle_pairs` vector.
529    pub particle_pair_idx: usize,
530    /// Orbital angular momentum quantum number L.
531    pub l: u32,
532    /// Channel spin S = |I ± 1/2|.
533    pub channel_spin: f64,
534    /// Boundary condition B (usually 0.0; shifts the shift factor reference).
535    pub boundary: f64,
536    /// Effective channel radius APE (fm), used to compute the hard-sphere phase φ_l.
537    ///
538    /// Per SAMMY `rml/mrml07.f:129,134` (`Rhof = Zkfe·Ex`, `Zkfe = Z·Rdeff`) the
539    /// EFFECTIVE radius feeds the phase shift (Sinsix), not the penetrability.
540    /// Independently corroborated by PLEIADES `models.py:385` ("Radius for
541    /// potential scattering").
542    pub effective_radius: f64,
543    /// True channel radius APT (fm), used to compute penetrability P_l and shift S_l.
544    ///
545    /// Per SAMMY `rml/mrml07.f:128,136` (`Rho = Zkte·Ex`, `Zkte = Z·Rdtru`) and
546    /// `rml/mrml03.f:244` (Betset width conversion), the TRUE radius feeds the
547    /// penetrability and shift (Pgh). Corroborated by PLEIADES `models.py:386`
548    /// ("Radius for penetrabilities and shifts").
549    pub true_radius: f64,
550}
551
552/// A single resonance in LRF=7 format.
553///
554/// For KRM=2 (standard R-matrix), `widths` contains reduced width amplitudes
555/// γ_c (eV^{1/2}) and `gamma_gamma = 0.0`.
556///
557/// For KRM=3 (Reich-Moore approximation), `widths` contains formal partial widths
558/// Γ_c (eV) and `gamma_gamma` is the capture width Γ_γ (eV) used to form complex
559/// pole energies: Ẽ_n = E_n - i·Γ_γn/2. The reduced amplitudes are derived as
560/// γ_nc = √(Γ_nc / (2·P_c(E_n))).
561///
562/// Reference: ENDF-6 Formats Manual §2.2.1.6; SAMMY manual §3.1
563#[derive(Debug, Clone, Serialize, Deserialize)]
564pub struct RmlResonance {
565    /// Resonance energy (eV).
566    pub energy: f64,
567    /// Width amplitudes per channel (eV^{1/2} for KRM=2; eV for KRM=3).
568    ///
569    /// Sign convention: sign(γ) encodes interference between resonances.
570    /// `widths.len()` equals the number of channels in the parent `SpinGroup`.
571    pub widths: Vec<f64>,
572    /// Capture (gamma) width Γ_γ (eV) for KRM=3 Reich-Moore approximation.
573    ///
574    /// Used to make the R-matrix denominator complex: E_n → E_n - i·Γ_γ/2.
575    /// Zero for KRM=2 (standard R-matrix, no complex energy shift).
576    pub gamma_gamma: f64,
577}
578
579/// A spin group (J, π) in LRF=7 R-Matrix Limited format.
580///
581/// Groups all resonances with the same total angular momentum J and parity π.
582/// Each spin group has its own set of reaction channels.
583///
584/// Reference: ENDF-6 Formats Manual §2.2.1.6; SAMMY rml/mrml01.f
585#[derive(Debug, Clone, Serialize, Deserialize)]
586pub struct SpinGroup {
587    /// Total angular momentum J.
588    pub j: f64,
589    /// Parity: +1.0 (even) or -1.0 (odd).
590    pub parity: f64,
591    /// Reaction channels for this spin group.
592    pub channels: Vec<RmlChannel>,
593    /// Resonances in this spin group.
594    pub resonances: Vec<RmlResonance>,
595    /// True when the ENDF file contained KBK > 0 or KPS > 0 background correction
596    /// records for this spin group.  The records are consumed by the parser but
597    /// the background terms are **not applied** to the cross-section calculation
598    /// (matching SAMMY behaviour: mrml10.f is a matrix utility, not a background
599    /// reader; KPS is explicitly ignored in mrml07.f).  Cross-sections computed
600    /// for spin groups with background corrections are therefore approximate.
601    #[serde(default)]
602    pub has_background_correction: bool,
603}
604
605/// Complete R-Matrix Limited data for one energy range (LRF=7).
606///
607/// Stored in `ResonanceRange::rml` when the formalism is `RMatrixLimited`.
608///
609/// Reference: ENDF-6 Formats Manual §2.2.1.6; SAMMY rml/mrml01.f
610#[derive(Debug, Clone, Serialize, Deserialize)]
611pub struct RmlData {
612    /// Target spin I.
613    pub target_spin: f64,
614    /// Atomic weight ratio (mass of target / neutron mass).
615    pub awr: f64,
616    /// Global scattering radius AP (fm); used as fallback when per-channel APE = 0.
617    pub scattering_radius: f64,
618    /// R-matrix type flag from ENDF CONT header.
619    ///
620    /// KRM=2: Standard multi-channel R-matrix (widths are reduced amplitudes γ).
621    /// KRM=3: Reich-Moore approximation (widths are formal partial widths Γ;
622    ///        capture enters via complex pole energies Ẽ_n = E_n - i·Γ_γ/2).
623    /// Reference: ENDF-6 Formats Manual §2.2.1.6; SAMMY rml/mrml01.f
624    pub krm: u32,
625    /// Particle pair definitions (NPP entries).
626    pub particle_pairs: Vec<ParticlePair>,
627    /// Spin groups (NJS entries), one per (J, π) combination.
628    pub spin_groups: Vec<SpinGroup>,
629}
630
631impl ResonanceData {
632    /// Total number of resonances across all ranges and groups.
633    ///
634    /// For LRF=7 ranges, counts resonances across all spin groups.
635    pub fn total_resonance_count(&self) -> usize {
636        self.ranges.iter().map(|r| r.resonance_count()).sum()
637    }
638}
639
640impl ResonanceRange {
641    /// Scattering radius at a given neutron energy.
642    ///
643    /// Returns the interpolated value from `ap_table` when NRO=1 (energy-dependent
644    /// radius), or the constant `scattering_radius` when NRO=0.
645    ///
646    /// Use this method in all physics calculations that need the channel radius,
647    /// rather than reading `scattering_radius` directly.
648    ///
649    /// # Arguments
650    /// * `energy_ev` — Lab-frame neutron energy in eV.
651    pub fn scattering_radius_at(&self, energy_ev: f64) -> f64 {
652        if let Some(table) = &self.ap_table {
653            table.evaluate(energy_ev)
654        } else {
655            self.scattering_radius
656        }
657    }
658
659    /// Total resonance count for this range (works for both LRF=1/2/3 and LRF=7).
660    pub fn resonance_count(&self) -> usize {
661        if let Some(rml) = &self.rml {
662            rml.spin_groups.iter().map(|sg| sg.resonances.len()).sum()
663        } else {
664            self.l_groups.iter().map(|lg| lg.resonances.len()).sum()
665        }
666    }
667}
668
669/// Group resonances by their total angular momentum J value (test-only).
670///
671/// Returns a vector of `(J, resonances)` pairs. Two J values are considered
672/// equal if they differ by less than [`nereids_core::constants::QUANTUM_NUMBER_EPS`].
673///
674/// Note: The physics crate uses `group_resonances_by_j` (in `reich_moore.rs`)
675/// for cross-section precomputation, which builds per-resonance invariants
676/// directly during grouping. This function is retained for unit-level tests
677/// of the grouping logic itself.
678#[cfg(test)]
679fn group_by_j(resonances: &[Resonance]) -> Vec<(f64, Vec<&Resonance>)> {
680    let mut groups: Vec<(f64, Vec<&Resonance>)> = Vec::new();
681    for res in resonances {
682        let j = res.j;
683        if let Some(group) = groups
684            .iter_mut()
685            .find(|(gj, _)| (*gj - j).abs() < nereids_core::constants::QUANTUM_NUMBER_EPS)
686        {
687            group.1.push(res);
688        } else {
689            groups.push((j, vec![res]));
690        }
691    }
692    groups
693}
694
695impl std::fmt::Display for ResonanceData {
696    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
697        write!(
698            f,
699            "ResonanceData(ZA={}, AWR={:.4}, ranges={}, total_resonances={})",
700            self.za,
701            self.awr,
702            self.ranges.len(),
703            self.total_resonance_count()
704        )
705    }
706}
707
708#[cfg(test)]
709mod tests {
710    use super::*;
711
712    fn make_linlin_table(points: Vec<(f64, f64)>) -> Tab1 {
713        let n = points.len();
714        Tab1 {
715            boundaries: vec![n],
716            interp_codes: vec![2],
717            points,
718        }
719    }
720
721    /// Linear-linear interpolation in the interior of the table.
722    #[test]
723    fn test_tab1_linlin_interior() {
724        let table = make_linlin_table(vec![(1.0, 10.0), (5.0, 30.0), (10.0, 5.0)]);
725        // midpoint of [1,5]: x=3 → 10 + (3-1)/(5-1) * (30-10) = 10 + 0.5*20 = 20
726        let v = table.evaluate(3.0);
727        assert!((v - 20.0).abs() < 1e-10, "lin-lin midpoint, got {v}");
728        // midpoint of [5,10]: x=7.5 → 30 + (7.5-5)/(10-5) * (5-30) = 30 + 0.5*(-25) = 17.5
729        let v2 = table.evaluate(7.5);
730        assert!(
731            (v2 - 17.5).abs() < 1e-10,
732            "lin-lin second interval, got {v2}"
733        );
734    }
735
736    /// Values outside the table range clamp to the boundary value.
737    #[test]
738    fn test_tab1_clamping() {
739        let table = make_linlin_table(vec![(2.0, 5.0), (8.0, 15.0)]);
740        assert_eq!(table.evaluate(0.0), 5.0, "below low bound");
741        assert_eq!(table.evaluate(100.0), 15.0, "above high bound");
742        assert_eq!(table.evaluate(2.0), 5.0, "at low bound");
743        assert_eq!(table.evaluate(8.0), 15.0, "at high bound");
744    }
745
746    /// Histogram interpolation (INT=1): y stays constant from left endpoint.
747    #[test]
748    fn test_tab1_histogram() {
749        let table = Tab1 {
750            boundaries: vec![3],
751            interp_codes: vec![1],
752            points: vec![(0.0, 10.0), (5.0, 20.0), (10.0, 30.0)],
753        };
754        assert_eq!(
755            table.evaluate(2.5),
756            10.0,
757            "histogram: should return left value"
758        );
759        assert_eq!(table.evaluate(7.5), 20.0, "histogram: second interval");
760    }
761
762    /// Two-region table: lin-lin for low energies, log-x/lin-y (INT=3) for high.
763    #[test]
764    fn test_tab1_multiregion() {
765        // Region 0 (INT=2, lin-lin): points 0..2  (NBT=2)
766        // Region 1 (INT=3, log in x / linear in y): points 2..4  (NBT=4)
767        // Points: (1,1), (3,3), (10,3), (100,30)
768        let table = Tab1 {
769            boundaries: vec![2, 4],
770            interp_codes: vec![2, 3],
771            points: vec![(1.0, 1.0), (3.0, 3.0), (10.0, 3.0), (100.0, 30.0)],
772        };
773        // Interval 0 ([1,3], INT=2 lin-lin): x=2 → 1 + (2-1)/(3-1) * (3-1) = 2
774        assert!(
775            (table.evaluate(2.0) - 2.0).abs() < 1e-10,
776            "region 0 lin-lin"
777        );
778        // Interval 1 ([3,10], INT=3 log-x/lin-y): x=5.
779        // y0==y1==3.0, so any interpolation mode yields 3.0 regardless.
780        // This verifies the region boundary is crossed correctly and that
781        // x=5 routes to interval 1 (not interval 0 or 2).
782        assert!(
783            (table.evaluate(5.0) - 3.0).abs() < 1e-10,
784            "region 1 INT=3 (constant y segment): x=5 should give 3.0"
785        );
786        // Interval 2 ([10,100], INT=3 log-x/lin-y): x=31.62 ≈ sqrt(10*100) = geometric midpoint.
787        // INT=3: t = ln(x/x0) / ln(x1/x0) = ln(31.62/10) / ln(100/10) = ln(3.162)/ln(10) ≈ 0.5
788        // y = y0 + t*(y1 - y0) = 3 + 0.5*(30 - 3) = 16.5
789        let v = table.evaluate(31.62);
790        assert!(
791            (v - 16.5).abs() < 0.1,
792            "region 2 INT=3 at geometric midpoint: expected 16.5, got {v}"
793        );
794    }
795
796    /// scattering_radius_at falls back to constant when ap_table is None.
797    #[test]
798    fn test_scattering_radius_at_constant() {
799        let range = ResonanceRange {
800            energy_low: 1e-5,
801            energy_high: 1e4,
802            resolved: true,
803            formalism: crate::resonance::ResonanceFormalism::ReichMoore,
804            target_spin: 0.0,
805            scattering_radius: 9.4285,
806            naps: 1,
807            ap_table: None,
808            l_groups: vec![],
809            rml: None,
810            urr: None,
811            r_external: vec![],
812        };
813        assert_eq!(range.scattering_radius_at(1.0), 9.4285);
814        assert_eq!(range.scattering_radius_at(1000.0), 9.4285);
815    }
816
817    /// scattering_radius_at interpolates from ap_table when NRO=1.
818    #[test]
819    fn test_scattering_radius_at_energy_dependent() {
820        // AP goes from 8.0 fm at 1 eV to 10.0 fm at 1000 eV (lin-lin).
821        let table = make_linlin_table(vec![(1.0, 8.0), (1000.0, 10.0)]);
822        let range = ResonanceRange {
823            energy_low: 1e-5,
824            energy_high: 1e4,
825            resolved: true,
826            formalism: crate::resonance::ResonanceFormalism::ReichMoore,
827            target_spin: 0.0,
828            scattering_radius: 9.0, // constant fallback (ignored when table is Some)
829            naps: 1,
830            ap_table: Some(table),
831            l_groups: vec![],
832            rml: None,
833            urr: None,
834            r_external: vec![],
835        };
836        // At 1 eV: 8.0 fm
837        assert!((range.scattering_radius_at(1.0) - 8.0).abs() < 1e-10);
838        // At 1000 eV: 10.0 fm
839        assert!((range.scattering_radius_at(1000.0) - 10.0).abs() < 1e-10);
840        // At 500.5 eV (midpoint): 9.0 fm
841        let mid = range.scattering_radius_at(500.5);
842        assert!((mid - 9.0).abs() < 0.01, "midpoint AP ≈ 9.0, got {mid}");
843    }
844
845    /// Log-guard fallback: if an x-coordinate is non-positive in an INT=3
846    /// (log-x, linear-y) interval, evaluate() falls back to lin-lin.
847    #[test]
848    fn test_tab1_log_guard_nonpositive_x() {
849        // INT=3 (log in x, linear in y) with x0=0.0 — 0.0_f64.ln() = -inf without guard.
850        let table = Tab1 {
851            boundaries: vec![2],
852            interp_codes: vec![3], // log in x, linear in y
853            points: vec![(0.0, 8.0), (10.0, 10.0)],
854        };
855        // x=0.0 is at the left boundary; evaluate() clamps to y=8.0 before interpolation.
856        assert!((table.evaluate(0.0) - 8.0).abs() < 1e-10);
857        // x=5.0 is interior; x0=0.0 triggers the log guard → lin-lin fallback.
858        let result = table.evaluate(5.0);
859        assert!(
860            result.is_finite(),
861            "fallback to lin-lin should give finite result, got {result}"
862        );
863    }
864
865    /// Log-guard fallback: if a y-value is non-positive in an INT=4
866    /// (linear-x, log-y) interval, evaluate() falls back to lin-lin.
867    #[test]
868    fn test_tab1_log_guard_nonpositive_y() {
869        // INT=4 (linear in x, log in y) with y0=0.0 — 0.0_f64.ln() = -inf without guard.
870        let table = Tab1 {
871            boundaries: vec![2],
872            interp_codes: vec![4], // linear in x, log in y
873            points: vec![(1.0, 0.0), (10.0, 1.0)],
874        };
875        let result = table.evaluate(5.0);
876        assert!(
877            result.is_finite(),
878            "fallback to lin-lin should give finite result, got {result}"
879        );
880    }
881
882    /// INT=3 (log in x, linear in y): verify correct formula against analytic values.
883    #[test]
884    fn test_tab1_logx_linear_y() {
885        // Points at x=1 (y=0) and x=100 (y=2.0).
886        // At x=10: t = ln(10)/ln(100) = 1/2, y = 0 + 0.5*2 = 1.0
887        let table = Tab1 {
888            boundaries: vec![2],
889            interp_codes: vec![3], // log in x, linear in y
890            points: vec![(1.0, 0.0), (100.0, 2.0)],
891        };
892        let y = table.evaluate(10.0);
893        assert!(
894            (y - 1.0).abs() < 1e-12,
895            "INT=3 at geometric midpoint x=10: expected y=1.0, got {y}"
896        );
897    }
898
899    /// INT=4 (linear in x, log in y): verify correct formula against analytic values.
900    #[test]
901    fn test_tab1_linear_x_logy() {
902        // Points at x=0 (y=1) and x=2 (y=e²).
903        // At x=1 (midpoint): t=0.5, y = exp(0 + 0.5*2) = exp(1) = e
904        let e = std::f64::consts::E;
905        let table = Tab1 {
906            boundaries: vec![2],
907            interp_codes: vec![4], // linear in x, log in y
908            points: vec![(0.0, 1.0), (2.0, e * e)],
909        };
910        let y = table.evaluate(1.0);
911        assert!(
912            (y - e).abs() < 1e-12,
913            "INT=4 at midpoint x=1: expected y=e={e:.6}, got {y:.6}"
914        );
915    }
916
917    #[test]
918    fn test_group_by_j() {
919        // Empty input
920        let groups = group_by_j(&[]);
921        assert!(groups.is_empty());
922
923        // Single resonance
924        let r1 = Resonance {
925            energy: 6.67,
926            j: 0.5,
927            gn: 0.001,
928            gg: 0.023,
929            gfa: 0.0,
930            gfb: 0.0,
931        };
932        let single = [r1.clone()];
933        let groups = group_by_j(&single);
934        assert_eq!(groups.len(), 1);
935        assert_eq!(groups[0].1.len(), 1);
936
937        // Multiple J values
938        let r2 = Resonance {
939            j: 1.5,
940            ..r1.clone()
941        };
942        let r3 = Resonance {
943            j: 0.5,
944            energy: 20.0,
945            ..r1.clone()
946        };
947        let multi = [r1, r2, r3];
948        let groups = group_by_j(&multi);
949        assert_eq!(groups.len(), 2); // J=0.5 and J=1.5
950        // J=0.5 group should have 2 resonances
951        let j05 = groups
952            .iter()
953            .find(|(j, _)| (*j - 0.5).abs() < nereids_core::constants::QUANTUM_NUMBER_EPS)
954            .unwrap();
955        assert_eq!(j05.1.len(), 2);
956    }
957}
958
959/// Synthetic [`ResonanceData`] / [`ResonanceRange`] builders for cross-crate
960/// tests.  Gated on `#[cfg(any(test, feature = "test-support"))]`: visible to
961/// in-crate `#[cfg(test)] mod tests` AND to integration tests in sibling crates
962/// that enable the `test-support` feature in their `[dev-dependencies]`.  Never
963/// compiled into release builds.  Consolidates previously-scattered ad-hoc
964/// builders into one named API, mirroring PR #545's
965/// `nereids_physics::resolution::test_support`.
966#[cfg(any(test, feature = "test-support"))]
967pub mod test_support {
968    use super::{LGroup, Resonance, ResonanceData, ResonanceFormalism, ResonanceRange};
969    use nereids_core::types::Isotope;
970
971    /// Parameters for [`single_resonance`].  No `Default`: every field is
972    /// required because different absorbing sites used different "defaults",
973    /// and forcing callers to be explicit prevents silent drift.
974    pub struct SingleResonanceParams {
975        pub energy: f64,
976        pub gamma_n: f64,
977        pub gamma_g: f64,
978        pub j: f64,
979        pub l: u32,
980        pub awr: f64,
981        pub target_spin: f64,
982        pub scattering_radius: f64,
983    }
984
985    // --- Private structural helpers ---
986    //
987    // The public fixtures below all build a single `ResonanceRange` with one
988    // `LGroup` whose body varies only in well-defined ways.  The two private
989    // helpers below absorb the structural skeleton (resolved/rml/urr/ap_table/
990    // r_external/qx/lrx/gfa/gfb) so each public helper carries only the
991    // physically-meaningful parameters.
992
993    /// One resolved `ResonanceRange` with a single L-group.  All "structural
994    /// invariants" (resolved, `rml`/`urr`/`ap_table`/`r_external`,
995    /// `qx`/`lrx`) get the minimal-fixture defaults.
996    #[allow(clippy::too_many_arguments)]
997    fn make_range(
998        energy_low: f64,
999        energy_high: f64,
1000        formalism: ResonanceFormalism,
1001        target_spin: f64,
1002        scattering_radius: f64,
1003        naps: i32,
1004        l: u32,
1005        lgroup_awr: f64,
1006        apl: f64,
1007        resonances: Vec<Resonance>,
1008    ) -> ResonanceRange {
1009        ResonanceRange {
1010            energy_low,
1011            energy_high,
1012            resolved: true,
1013            formalism,
1014            target_spin,
1015            scattering_radius,
1016            naps,
1017            l_groups: vec![LGroup {
1018                l,
1019                awr: lgroup_awr,
1020                apl,
1021                qx: 0.0,
1022                lrx: 0,
1023                resonances,
1024            }],
1025            rml: None,
1026            urr: None,
1027            ap_table: None,
1028            r_external: vec![],
1029        }
1030    }
1031
1032    /// Wrap a `ResonanceRange` in a `ResonanceData` for caller-chosen `(z, a, awr)`.
1033    fn wrap(z: u32, a: u32, awr: f64, range: ResonanceRange) -> ResonanceData {
1034        ResonanceData {
1035            isotope: Isotope::new(z, a).unwrap(),
1036            za: z * 1000 + a,
1037            awr,
1038            ranges: vec![range],
1039        }
1040    }
1041
1042    /// One `Resonance` with `gfa = gfb = 0` (the common minimal-fixture case).
1043    fn res(energy: f64, j: f64, gn: f64, gg: f64) -> Resonance {
1044        Resonance {
1045            energy,
1046            j,
1047            gn,
1048            gg,
1049            gfa: 0.0,
1050            gfb: 0.0,
1051        }
1052    }
1053
1054    // --- Public fixtures ---
1055
1056    /// Canonical U-238 6.674 eV Reich-Moore single-resonance.  Byte-identical
1057    /// anchor for the most common synthetic case; absorbs four previously-
1058    /// duplicated copies across pipeline / physics / fitting.
1059    pub fn u238_single_resonance() -> ResonanceData {
1060        u238_with_formalism(ResonanceFormalism::ReichMoore)
1061    }
1062
1063    /// Same as [`u238_single_resonance`] with a caller-chosen formalism.
1064    /// Default RM-style range `1e-5 .. 1e4` eV.
1065    pub fn u238_with_formalism(formalism: ResonanceFormalism) -> ResonanceData {
1066        wrap(
1067            92,
1068            238,
1069            236.006,
1070            make_range(
1071                1e-5,
1072                1e4,
1073                formalism,
1074                0.0,
1075                9.4285,
1076                1,
1077                0,
1078                236.006,
1079                0.0,
1080                vec![res(6.674, 0.5, 1.493e-3, 23.0e-3)],
1081            ),
1082        )
1083    }
1084
1085    /// As [`u238_with_formalism`] with wider range `1e-6 .. 1e5` eV for the
1086    /// velocity-factor regression suite (`slbw_velocity_factor.rs`).
1087    pub fn u238_with_formalism_wide_range(formalism: ResonanceFormalism) -> ResonanceData {
1088        wrap(
1089            92,
1090            238,
1091            236.006,
1092            make_range(
1093                1e-6,
1094                1e5,
1095                formalism,
1096                0.0,
1097                9.4285,
1098                1,
1099                0,
1100                236.006,
1101                0.0,
1102                vec![res(6.674, 0.5, 1.493e-3, 23.0e-3)],
1103            ),
1104        )
1105    }
1106
1107    /// Fully-parameterized U-238 ZA single-resonance, Reich-Moore.  For the
1108    /// RM-harness tests that vary (E_r, Γn, Γγ, J, L, AWR, I, AP) per case.
1109    pub fn single_resonance(p: SingleResonanceParams) -> ResonanceData {
1110        wrap(
1111            92,
1112            238,
1113            p.awr,
1114            make_range(
1115                1e-5,
1116                1e4,
1117                ResonanceFormalism::ReichMoore,
1118                p.target_spin,
1119                p.scattering_radius,
1120                1,
1121                p.l,
1122                p.awr,
1123                0.0,
1124                vec![res(p.energy, p.j, p.gamma_n, p.gamma_g)],
1125            ),
1126        )
1127    }
1128
1129    /// Synthetic single-resonance for an arbitrary `(z, a, awr, energy)`.
1130    /// Hard-codes RM, AP=5, I=0, L=0, J=0.5, Γn=1e-3, Γγ=1e-2.  Used by
1131    /// multi-isotope group-fit / calibration tests.
1132    pub fn synthetic_single_resonance(z: u32, a: u32, awr: f64, energy: f64) -> ResonanceData {
1133        wrap(
1134            z,
1135            a,
1136            awr,
1137            make_range(
1138                1e-5,
1139                1e4,
1140                ResonanceFormalism::ReichMoore,
1141                0.0,
1142                5.0,
1143                1,
1144                0,
1145                awr,
1146                0.0,
1147                vec![res(energy, 0.5, 1e-3, 1e-2)],
1148            ),
1149        )
1150    }
1151
1152    /// U-238-ZA single s-wave SLBW over the wider `1e-5 .. 1e6` eV range used
1153    /// by the elastic-oracle regression test (`slbw_elastic_oracle.rs`).
1154    /// I=0 so `g_J = 1` for J=1/2.
1155    pub fn synthetic_swave_slbw(
1156        awr: f64,
1157        e_r_ev: f64,
1158        gn_ev: f64,
1159        gg_ev: f64,
1160        scattering_radius_fm: f64,
1161    ) -> ResonanceData {
1162        wrap(
1163            92,
1164            238,
1165            awr,
1166            make_range(
1167                1e-5,
1168                1e6,
1169                ResonanceFormalism::SLBW,
1170                0.0,
1171                scattering_radius_fm,
1172                1,
1173                0,
1174                awr,
1175                0.0,
1176                vec![res(e_r_ev, 0.5, gn_ev, gg_ev)],
1177            ),
1178        )
1179    }
1180
1181    /// Minimal single-resonance for offline detectability tests.  Auto-derives
1182    /// `awr ≈ a - 0.009` (rough neutron-mass correction); hard-codes RM, AP=6,
1183    /// I=0, L=0, J=0.5.
1184    pub fn synthetic_isotope(z: u32, a: u32, res_energy: f64, gn: f64, gg: f64) -> ResonanceData {
1185        let awr = a as f64 - 0.009;
1186        wrap(
1187            z,
1188            a,
1189            awr,
1190            make_range(
1191                1e-5,
1192                1e4,
1193                ResonanceFormalism::ReichMoore,
1194                0.0,
1195                6.0,
1196                1,
1197                0,
1198                awr,
1199                0.0,
1200                vec![res(res_energy, 0.5, gn, gg)],
1201            ),
1202        )
1203    }
1204
1205    /// Hf-178 MLBW: two s-waves at 7.8 and 16.9 eV in the same J=1/2 group.
1206    /// Range `0 .. 100` eV, AP=9.48, NAPS=0.  MLBW positivity and
1207    /// total-vs-components regression tests in `slbw.rs`.
1208    pub fn hf178_mlbw_two_resonances() -> ResonanceData {
1209        wrap(
1210            72,
1211            178,
1212            177.94,
1213            make_range(
1214                0.0,
1215                100.0,
1216                ResonanceFormalism::MLBW,
1217                0.0,
1218                9.48,
1219                0,
1220                0,
1221                177.94,
1222                0.0,
1223                vec![res(7.8, 0.5, 0.002, 0.060), res(16.9, 0.5, 0.004, 0.055)],
1224            ),
1225        )
1226    }
1227
1228    /// Hf-177 MLBW: two s-waves at 2.386 and 5.89 eV in the same high-J group
1229    /// (J=4.0), target spin I=3.5.  Range `1e-5 .. 1e3` eV, AP=7.0, NAPS=0.
1230    /// MLBW coherent-vs-incoherent dispatcher regression (PR #465 root cause).
1231    pub fn hf177_mlbw_two_resonances_high_j() -> ResonanceData {
1232        wrap(
1233            72,
1234            177,
1235            175.4232,
1236            make_range(
1237                1e-5,
1238                1e3,
1239                ResonanceFormalism::MLBW,
1240                3.5,
1241                7.0,
1242                0,
1243                0,
1244                175.4232,
1245                0.0,
1246                vec![
1247                    res(2.386, 4.0, 2.0e-3, 60.0e-3),
1248                    res(5.89, 4.0, 3.5e-3, 62.0e-3),
1249                ],
1250            ),
1251        )
1252    }
1253
1254    /// SAMMY ex001 hydrogen-anchor: SLBW single resonance at 10 eV on the
1255    /// synthetic ZA=1010 (AWR=10).  Doppler-broadening reference suite.
1256    /// Widths are in eV (SAMMY par file has them in meV; conversion baked in).
1257    pub fn ex001_hydrogen_single_resonance() -> ResonanceData {
1258        wrap(
1259            1,
1260            10,
1261            10.0,
1262            make_range(
1263                0.0,
1264                100.0,
1265                ResonanceFormalism::SLBW,
1266                0.0,
1267                2.908,
1268                1,
1269                0,
1270                10.0,
1271                2.908,
1272                vec![res(10.0, 0.5, 0.5e-3, 1.0e-3)],
1273            ),
1274        )
1275    }
1276
1277    /// Minimal SLBW `ResonanceRange` (not a full `ResonanceData`) using
1278    /// U-238-like parameters with a single 6.674 eV s-wave.  For
1279    /// `slbw_cross_sections_for_range` panic tests at the range-level entry.
1280    pub fn minimal_slbw_range() -> ResonanceRange {
1281        make_range(
1282            1e-5,
1283            1e4,
1284            ResonanceFormalism::SLBW,
1285            0.0,
1286            9.4285,
1287            1,
1288            0,
1289            236.006,
1290            0.0,
1291            vec![res(6.674, 0.5, 1.493e-3, 23.0e-3)],
1292        )
1293    }
1294}
1295
1296#[cfg(test)]
1297mod test_support_tests {
1298    use super::ResonanceFormalism;
1299    use super::test_support::*;
1300
1301    #[test]
1302    fn u238_single_resonance_has_canonical_za_and_energy() {
1303        let d = u238_single_resonance();
1304        assert_eq!(d.za, 92238);
1305        assert_eq!(d.ranges[0].l_groups[0].resonances[0].energy, 6.674);
1306    }
1307
1308    #[test]
1309    fn u238_with_formalism_slbw_returns_slbw() {
1310        let d = u238_with_formalism(ResonanceFormalism::SLBW);
1311        assert_eq!(d.ranges[0].formalism, ResonanceFormalism::SLBW);
1312        assert_eq!(d.ranges[0].energy_low, 1e-5);
1313        assert_eq!(d.ranges[0].energy_high, 1e4);
1314    }
1315
1316    #[test]
1317    fn u238_with_formalism_wide_range_uses_wide_bounds() {
1318        let d = u238_with_formalism_wide_range(ResonanceFormalism::MLBW);
1319        assert_eq!(d.ranges[0].energy_low, 1e-6);
1320        assert_eq!(d.ranges[0].energy_high, 1e5);
1321        assert_eq!(d.ranges[0].formalism, ResonanceFormalism::MLBW);
1322    }
1323
1324    #[test]
1325    fn single_resonance_param_struct_builds_rm() {
1326        let d = single_resonance(SingleResonanceParams {
1327            energy: 6.674,
1328            gamma_n: 1.493e-3,
1329            gamma_g: 23.0e-3,
1330            j: 0.5,
1331            l: 0,
1332            awr: 236.006,
1333            target_spin: 0.0,
1334            scattering_radius: 9.4285,
1335        });
1336        assert_eq!(d.ranges[0].formalism, ResonanceFormalism::ReichMoore);
1337        assert_eq!(d.za, 92238);
1338    }
1339
1340    #[test]
1341    fn hf178_mlbw_two_resonances_returns_two() {
1342        let d = hf178_mlbw_two_resonances();
1343        assert_eq!(d.ranges[0].l_groups[0].resonances.len(), 2);
1344        assert_eq!(d.za, 72178);
1345    }
1346
1347    #[test]
1348    fn synthetic_isotope_uses_caller_za() {
1349        let d = synthetic_isotope(74, 184, 10.0, 1e-3, 1e-2);
1350        assert_eq!(d.za, 74184);
1351        assert_eq!(d.ranges[0].l_groups[0].resonances[0].energy, 10.0);
1352    }
1353}