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 (INT=2 lin-lin, INT=5 log-log; other INT codes are valid
186//        ENDF but not yet implemented and cause the URR range to be skipped).
187//
188// Reference: ENDF-6 Formats Manual §2.2.2; SAMMY unr/munr03.f90 Csig3
189
190/// Average widths for one (L, J) combination in the Unresolved Resonance Region.
191///
192/// For LRF=1: `energies` is empty; each width vector has exactly one element.
193/// For LRF=2: all vectors have length NE; `int_code` selects the interpolation
194/// law (INT=2 lin-lin or INT=5 log-log).
195///
196/// Reference: ENDF-6 Formats Manual §2.2.2; SAMMY `unr/munr03.f90`
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    /// 2 = lin-lin, 5 = log-log.  Ignored for LRF=1 (no table).
220    #[serde(default = "default_int_code")]
221    pub int_code: u32,
222}
223
224fn default_int_code() -> u32 {
225    2
226}
227
228/// Average URR parameters for one L-value.
229///
230/// Reference: ENDF-6 Formats Manual §2.2.2
231#[derive(Debug, Clone, Serialize, Deserialize)]
232pub struct UrrLGroup {
233    /// Orbital angular momentum quantum number.
234    pub l: u32,
235    /// Atomic weight ratio for this L-group.
236    pub awri: f64,
237    /// J-groups within this L-value.
238    pub j_groups: Vec<UrrJGroup>,
239}
240
241/// Complete Unresolved Resonance Region data for one energy range (LRU=2).
242///
243/// Stored in `ResonanceRange::urr` when the range is an URR range.
244///
245/// Reference: ENDF-6 Formats Manual §2.2.2; SAMMY `unr/munr03.f90`
246#[derive(Debug, Clone, Serialize, Deserialize)]
247pub struct UrrData {
248    /// LRF flag: 1 = single-level BWR (energy-independent widths),
249    ///           2 = multi-level BWR (energy-dependent width tables).
250    pub lrf: u32,
251    /// Target spin I.
252    pub spi: f64,
253    /// Scattering radius AP in fm (converted from ENDF 10⁻¹² cm at parse time).
254    pub ap: f64,
255    /// Lower URR energy bound (eV).
256    pub e_low: f64,
257    /// Upper URR energy bound (eV).
258    pub e_high: f64,
259    /// L-groups (one per orbital angular momentum value).
260    pub l_groups: Vec<UrrLGroup>,
261}
262
263/// Top-level container for all resonance data parsed from an ENDF file.
264#[derive(Debug, Clone, Serialize, Deserialize)]
265pub struct ResonanceData {
266    /// The isotope this data belongs to.
267    pub isotope: Isotope,
268    /// ZA identifier (Z*1000 + A).
269    pub za: u32,
270    /// Atomic weight ratio (mass of target / neutron mass).
271    pub awr: f64,
272    /// Energy ranges containing resonance parameters.
273    pub ranges: Vec<ResonanceRange>,
274}
275
276/// A single energy range within the resolved resonance region.
277#[derive(Debug, Clone, Serialize, Deserialize)]
278pub struct ResonanceRange {
279    /// Lower energy bound (eV).
280    pub energy_low: f64,
281    /// Upper energy bound (eV).
282    pub energy_high: f64,
283    /// Resolved (true) or unresolved (false).
284    pub resolved: bool,
285    /// Resonance formalism used in this range.
286    pub formalism: ResonanceFormalism,
287    /// Target spin (I).
288    pub target_spin: f64,
289    /// Scattering radius (fm).
290    ///
291    /// Constant value from the ENDF CONT header AP field.
292    /// When `ap_table` is `Some`, use `scattering_radius_at(energy_ev)` instead
293    /// of reading this field directly — the table provides the energy-dependent
294    /// value, clamping to the nearest endpoint for energies outside the table
295    /// range.  This constant is only used when `ap_table` is `None` (NRO=0).
296    pub scattering_radius: f64,
297    /// NAPS flag: scattering radius calculation control.
298    ///
299    /// NAPS=0: use the channel radius for penetrability/shift calculations.
300    /// NAPS=1: use the scattering radius (AP or AP(E)) for penetrability/shift.
301    /// Reference: ENDF-6 Formats Manual §2.2.1
302    #[serde(default)]
303    pub naps: i32,
304    /// Energy-dependent scattering radius AP(E) (fm), present when NRO=1.
305    ///
306    /// ENDF-6 §2.2.1: when NRO≠0 a TAB1 record immediately follows the range
307    /// CONT header to give AP(E) as a piecewise function.  At each energy the
308    /// table value replaces the constant `scattering_radius` in penetrability,
309    /// shift, and hard-sphere phase calculations.
310    ///
311    /// `None` when the range has NRO=0 (constant AP).
312    ///
313    /// Reference: ENDF-6 Formats Manual §2.2.1; SAMMY `mlb/mmlb1.f90`
314    #[serde(default)]
315    pub ap_table: Option<Tab1>,
316    /// Spin groups for LRF=1/2/3 (L-grouped). Empty for LRF=7 and LRU=2.
317    pub l_groups: Vec<LGroup>,
318    /// R-Matrix Limited data for LRF=7. `None` for LRF=1/2/3 and LRU=2.
319    pub rml: Option<Box<RmlData>>,
320    /// Unresolved Resonance Region data (LRU=2). `None` for all LRU=1 ranges.
321    ///
322    /// When `Some`, cross-sections are computed via the Hauser-Feshbach
323    /// formula in `nereids_physics::urr::urr_cross_sections`.
324    #[serde(default)]
325    pub urr: Option<Box<UrrData>>,
326    /// R-external (background R-matrix) entries per spin group.
327    ///
328    /// Diagonal, real-valued corrections to the R-matrix that approximate
329    /// the effect of distant (unresolved) resonances.  Keyed by (L, J).
330    ///
331    /// Populated from SAMMY's "R-EXTERNAL PARAMETERS FOLLOW" section.
332    /// Empty for ENDF-only data or SAMMY cases without R-external.
333    ///
334    /// SAMMY Ref: Manual Section II.B.1.d, mpar03.f90 Readrx
335    #[serde(default)]
336    pub r_external: Vec<RExternalEntry>,
337}
338
339/// Parameters grouped by orbital angular momentum L.
340///
341/// In ENDF File 2 (LRF=3, Reich-Moore), resonances are grouped by L-value.
342/// Each L-group contains resonances with different J values.
343#[derive(Debug, Clone, Serialize, Deserialize)]
344pub struct LGroup {
345    /// Orbital angular momentum quantum number.
346    pub l: u32,
347    /// Atomic weight ratio for this group.
348    pub awr: f64,
349    /// Channel scattering radius for this L (fm). 0.0 means use the global value.
350    pub apl: f64,
351    /// Q-value for competitive width (eV). Only meaningful for BW formalisms
352    /// (LRF=1/2) where LRX=1; zero otherwise.
353    /// Reference: ENDF-6 Formats Manual §2.2.1.1, L-value CONT record (C2 field).
354    #[serde(default)]
355    pub qx: f64,
356    /// Competitive width flag. LRX=0: no competitive width; LRX=1: competitive
357    /// reaction exists (width = GT - GN - GG - GF). Only used in BW formalisms.
358    /// Reference: ENDF-6 Formats Manual §2.2.1.1, L-value CONT record (L2 field).
359    #[serde(default)]
360    pub lrx: i32,
361    /// Individual resonances in this L-group.
362    pub resonances: Vec<Resonance>,
363}
364
365/// A single resonance entry.
366///
367/// The meaning of the width fields depends on the formalism:
368///
369/// ## Reich-Moore (LRF=3)
370/// - `gn`: Neutron width Γn (eV)
371/// - `gg`: Radiation (gamma) width Γγ (eV)
372/// - `gfa`: First fission width Γf1 (eV), 0.0 if non-fissile
373/// - `gfb`: Second fission width Γf2 (eV), 0.0 if non-fissile
374///
375/// ## SLBW/MLBW (LRF=1/2)
376/// - `gn`: Neutron width Γn (eV)
377/// - `gg`: Radiation width Γγ (eV)
378/// - `gfa`: Fission width Γf (eV)
379/// - `gfb`: Not used (0.0)
380///
381/// Reference: ENDF-6 Formats Manual, Section 2.2.1
382/// Reference: SAMMY manual, Section 2 (Scattering Theory)
383#[derive(Debug, Clone, Serialize, Deserialize)]
384pub struct Resonance {
385    /// Resonance energy (eV).
386    pub energy: f64,
387    /// Total angular momentum J.
388    pub j: f64,
389    /// Neutron width Γn (eV).
390    pub gn: f64,
391    /// Radiation (capture/gamma) width Γγ (eV).
392    pub gg: f64,
393    /// First fission width (eV). Zero for non-fissile isotopes.
394    pub gfa: f64,
395    /// Second fission width (eV). Zero for non-fissile isotopes.
396    pub gfb: f64,
397}
398
399// ─── R-External (Background R-Matrix) ─────────────────────────────────────────
400
401/// R-external (background R-matrix) parameters for a single spin group channel.
402///
403/// Parameterizes smooth R-matrix contribution from distant (unresolved)
404/// resonances.  The background R-matrix is diagonal and real-valued,
405/// parameterized as a logarithmic polynomial in energy.
406///
407/// ## Formula
408/// ```text
409/// R_ext(E) = R_con + R_lin·E + R_quad·E²
410///          + s_lin·(E_up − E_low)
411///          − (s_con + s_lin·E)·ln[(E_up − E) / (E − E_low)]
412/// ```
413///
414/// SAMMY Ref: Manual Section II.B.1.d, mcro2.f90 lines 180-193
415#[derive(Debug, Clone, Default, Serialize, Deserialize)]
416pub struct RExternalEntry {
417    /// Orbital angular momentum L of the spin group.
418    pub l: u32,
419    /// Total angular momentum J (signed, per SAMMY convention).
420    pub j: f64,
421    /// Lower energy bound (eV).
422    pub e_low: f64,
423    /// Upper energy bound (eV).
424    pub e_up: f64,
425    /// Constant term in R-matrix polynomial.
426    pub r_con: f64,
427    /// Linear coefficient (eV⁻¹).
428    pub r_lin: f64,
429    /// Constant logarithmic coefficient.
430    pub s_con: f64,
431    /// Linear logarithmic coefficient (eV⁻¹).
432    pub s_lin: f64,
433    /// Quadratic coefficient (eV⁻²).
434    pub r_quad: f64,
435}
436
437impl RExternalEntry {
438    /// Evaluate R_ext(E) at the given energy.
439    ///
440    /// The polynomial part (`r_con + r_lin·E + r_quad·E²`) applies at all
441    /// energies.  The logarithmic terms are only added when `E` is strictly
442    /// inside `(e_low, e_up)`.
443    ///
444    /// SAMMY Ref: mcro2.f90 Setr_Cro, lines 180-193
445    pub fn evaluate(&self, energy_ev: f64) -> f64 {
446        let e = energy_ev;
447        let mut r = self.r_con + self.r_lin * e + self.r_quad * e * e;
448
449        let e_up_diff = self.e_up - e;
450        let e_low_diff = e - self.e_low;
451        if e_up_diff > 0.0 && e_low_diff > 0.0 {
452            let log_val = (e_up_diff / e_low_diff).ln();
453            r -= (self.s_con + self.s_lin * e) * log_val;
454            r += self.s_lin * (self.e_up - self.e_low);
455        }
456
457        r
458    }
459}
460
461// ─── LRF=7 (R-Matrix Limited) Data Structures ────────────────────────────────
462//
463// LRF=7 organizes resonances by spin group (J,π) rather than L-value.
464// Each spin group has multiple explicit reaction channels. Resonances carry
465// reduced width amplitudes γ per channel, not formal widths Γ.
466//
467// Reference: ENDF-6 Formats Manual §2.2.1.6; SAMMY manual Ch. 3
468// SAMMY source: rml/mrml01.f (reader), rml/mrml11.f (cross-section calc)
469
470/// Particle pair definition for LRF=7 R-Matrix Limited.
471///
472/// Identifies the two particles in a reaction channel (e.g., neutron + W-184,
473/// or gamma + W-185). Used to determine which channels are entrance (neutron)
474/// channels and which are exit (fission, capture) channels.
475///
476/// Reference: ENDF-6 Formats Manual §2.2.1.6, Table 2.2
477#[derive(Debug, Clone, Serialize, Deserialize)]
478pub struct ParticlePair {
479    /// Mass of particle a (neutron = 1.0, in neutron mass units).
480    pub ma: f64,
481    /// Mass of particle b (target nucleus, in neutron mass units).
482    pub mb: f64,
483    /// Charge number Z of particle a, as stored in the ENDF LRF=7 particle-pair list.
484    /// ENDF LRF=7 stores the charge directly: neutron/photon = 0, proton = 1, alpha = 2.
485    /// Reference: SAMMY rml/mrml03.f — `Docoul = Kzb * Kza` (product of charges).
486    pub za: f64,
487    /// Charge number Z of particle b (target or recoil), as stored in ENDF LRF=7.
488    pub zb: f64,
489    /// Spin of particle a (1/2 for neutron).
490    pub ia: f64,
491    /// Spin of particle b (target spin I).
492    pub ib: f64,
493    /// Q-value for this reaction (eV). 0 for elastic.
494    pub q: f64,
495    /// Penetrability flag.
496    ///
497    /// `PNT=1`: calculate penetrability P_c analytically (Blatt-Weisskopf).
498    /// Used for massive-particle channels (neutron elastic).
499    /// `PNT=0`: do not calculate penetrability; set P_c = 0 (photon/massless channels).
500    pub pnt: i32,
501    /// Shift factor flag.
502    ///
503    /// `SHF=1`: calculate shift factor S_c analytically (Blatt-Weisskopf).
504    /// `SHF=0`: do not calculate; treat S_c = B_c so (S_c − B_c) = 0 in level matrix.
505    pub shf: i32,
506    /// ENDF MT number identifying the reaction (2=elastic, 18=fission, 102=capture).
507    pub mt: u32,
508    /// Parity of particle a.
509    pub pa: f64,
510    /// Parity of particle b.
511    pub pb: f64,
512}
513
514/// A single reaction channel within an LRF=7 spin group.
515///
516/// Specifies which particle pair, what orbital angular momentum, and the
517/// radii used to compute penetrabilities and hard-sphere phase shifts.
518///
519/// Reference: ENDF-6 Formats Manual §2.2.1.6, Table 2.3
520#[derive(Debug, Clone, Serialize, Deserialize)]
521pub struct RmlChannel {
522    /// Index into the parent `RmlData::particle_pairs` vector.
523    pub particle_pair_idx: usize,
524    /// Orbital angular momentum quantum number L.
525    pub l: u32,
526    /// Channel spin S = |I ± 1/2|.
527    pub channel_spin: f64,
528    /// Boundary condition B (usually 0.0; shifts the shift factor reference).
529    pub boundary: f64,
530    /// Effective channel radius APE (fm), used to compute P_l and S_l.
531    pub effective_radius: f64,
532    /// True channel radius APT (fm), used to compute hard-sphere phase φ_l.
533    pub true_radius: f64,
534}
535
536/// A single resonance in LRF=7 format.
537///
538/// For KRM=2 (standard R-matrix), `widths` contains reduced width amplitudes
539/// γ_c (eV^{1/2}) and `gamma_gamma = 0.0`.
540///
541/// For KRM=3 (Reich-Moore approximation), `widths` contains formal partial widths
542/// Γ_c (eV) and `gamma_gamma` is the capture width Γ_γ (eV) used to form complex
543/// pole energies: Ẽ_n = E_n - i·Γ_γn/2. The reduced amplitudes are derived as
544/// γ_nc = √(Γ_nc / (2·P_c(E_n))).
545///
546/// Reference: ENDF-6 Formats Manual §2.2.1.6; SAMMY manual §3.1
547#[derive(Debug, Clone, Serialize, Deserialize)]
548pub struct RmlResonance {
549    /// Resonance energy (eV).
550    pub energy: f64,
551    /// Width amplitudes per channel (eV^{1/2} for KRM=2; eV for KRM=3).
552    ///
553    /// Sign convention: sign(γ) encodes interference between resonances.
554    /// `widths.len()` equals the number of channels in the parent `SpinGroup`.
555    pub widths: Vec<f64>,
556    /// Capture (gamma) width Γ_γ (eV) for KRM=3 Reich-Moore approximation.
557    ///
558    /// Used to make the R-matrix denominator complex: E_n → E_n - i·Γ_γ/2.
559    /// Zero for KRM=2 (standard R-matrix, no complex energy shift).
560    pub gamma_gamma: f64,
561}
562
563/// A spin group (J, π) in LRF=7 R-Matrix Limited format.
564///
565/// Groups all resonances with the same total angular momentum J and parity π.
566/// Each spin group has its own set of reaction channels.
567///
568/// Reference: ENDF-6 Formats Manual §2.2.1.6; SAMMY rml/mrml01.f
569#[derive(Debug, Clone, Serialize, Deserialize)]
570pub struct SpinGroup {
571    /// Total angular momentum J.
572    pub j: f64,
573    /// Parity: +1.0 (even) or -1.0 (odd).
574    pub parity: f64,
575    /// Reaction channels for this spin group.
576    pub channels: Vec<RmlChannel>,
577    /// Resonances in this spin group.
578    pub resonances: Vec<RmlResonance>,
579    /// True when the ENDF file contained KBK > 0 or KPS > 0 background correction
580    /// records for this spin group.  The records are consumed by the parser but
581    /// the background terms are **not applied** to the cross-section calculation
582    /// (matching SAMMY behaviour: mrml10.f is a matrix utility, not a background
583    /// reader; KPS is explicitly ignored in mrml07.f).  Cross-sections computed
584    /// for spin groups with background corrections are therefore approximate.
585    #[serde(default)]
586    pub has_background_correction: bool,
587}
588
589/// Complete R-Matrix Limited data for one energy range (LRF=7).
590///
591/// Stored in `ResonanceRange::rml` when the formalism is `RMatrixLimited`.
592///
593/// Reference: ENDF-6 Formats Manual §2.2.1.6; SAMMY rml/mrml01.f
594#[derive(Debug, Clone, Serialize, Deserialize)]
595pub struct RmlData {
596    /// Target spin I.
597    pub target_spin: f64,
598    /// Atomic weight ratio (mass of target / neutron mass).
599    pub awr: f64,
600    /// Global scattering radius AP (fm); used as fallback when per-channel APE = 0.
601    pub scattering_radius: f64,
602    /// R-matrix type flag from ENDF CONT header.
603    ///
604    /// KRM=2: Standard multi-channel R-matrix (widths are reduced amplitudes γ).
605    /// KRM=3: Reich-Moore approximation (widths are formal partial widths Γ;
606    ///        capture enters via complex pole energies Ẽ_n = E_n - i·Γ_γ/2).
607    /// Reference: ENDF-6 Formats Manual §2.2.1.6; SAMMY rml/mrml01.f
608    pub krm: u32,
609    /// Particle pair definitions (NPP entries).
610    pub particle_pairs: Vec<ParticlePair>,
611    /// Spin groups (NJS entries), one per (J, π) combination.
612    pub spin_groups: Vec<SpinGroup>,
613}
614
615impl ResonanceData {
616    /// Total number of resonances across all ranges and groups.
617    ///
618    /// For LRF=7 ranges, counts resonances across all spin groups.
619    pub fn total_resonance_count(&self) -> usize {
620        self.ranges.iter().map(|r| r.resonance_count()).sum()
621    }
622
623    /// Get all resonances in the resolved region (LRF=1/2/3 only), sorted by energy.
624    ///
625    /// Returns an empty vec for LRF=7 ranges; use `ResonanceRange::rml` directly
626    /// to access R-Matrix Limited resonances.
627    pub fn all_resolved_resonances(&self) -> Vec<&Resonance> {
628        let mut resonances: Vec<&Resonance> = self
629            .ranges
630            .iter()
631            .filter(|r| r.resolved && r.rml.is_none())
632            .flat_map(|r| &r.l_groups)
633            .flat_map(|lg| &lg.resonances)
634            .collect();
635        resonances.sort_by(|a, b| a.energy.total_cmp(&b.energy));
636        resonances
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}