nereids_physics/surrogate.rs
1//! Forward-model surrogates for multi-isotope accelerated fits.
2//!
3//! Currently exposes [`SparseEmpiricalCubaturePlan`] — a Jacobian-anchored
4//! sparse empirical cubature on the joint σ-pushforward manifold. An
5//! algorithm-design study that benchmarked several candidate surrogates
6//! against the real VENUS operator selected this scheme as the k ≥ 2
7//! winner; this module is a Rust port of the study's winning reference
8//! implementation, and the compression table below records the study's
9//! measurements.
10//!
11//! # Mathematical basis
12//!
13//! Let `R` be the resolution operator on a fixed target grid, `σ_1(E'),
14//! …, σ_k(E')` the per-isotope cross-sections, and `x_ℓ = (σ_1(E'_ℓ), …,
15//! σ_k(E'_ℓ)) ∈ ℝ^k` the pushforward of a source point `E'_ℓ`. For each
16//! row `i`, exact evaluation is
17//!
18//! ```text
19//! T_i(n) = Σ_ℓ R_{iℓ} exp(-n · x_ℓ)
20//! ∂T_i/∂n_j = -Σ_ℓ R_{iℓ} x_{ℓ,j} exp(-n · x_ℓ)
21//! ```
22//!
23//! The row support contains ~82 ℓ's on the VENUS 3471-bin production
24//! grid. By [Carathéodory / Tchakaloff], any nonneg combination of
25//! feature vectors over this support is matched (in feature space) by an
26//! equivalent nonneg combination supported on at most `d + 1` atoms,
27//! where `d` is the feature dimension. Choosing features = forward
28//! evaluations at `S` training densities + Jacobian evaluations at one
29//! anchor density gives `d = S + k` features, so each row collapses to
30//! ≤ `S + k + 1` atoms while preserving positivity, row-stochasticity,
31//! and the exact Jacobian at the anchor.
32//!
33//! # Empirical compression (design-study measurements, real VENUS operator)
34//!
35//! | Scenario | k | avg atoms/row | max atoms/row | compression vs exact |
36//! |-----------------------------------|---|---------------|---------------|----------------------|
37//! | Hf (natural group) | 1 | 3.53 | 67 | 23.3× |
38//! | Hf + W | 2 | 5.65 | 7 | 14.5× |
39//! | U-235 + U-238 | 2 | 5.32 | 7 | 15.5× |
40//! | Gd + Eu + Sm | 3 | 8.59 | 9 | 9.6× |
41//! | Hf-174/176/177/178/179/180 indep. | 6 | 9.03 | 15 | 9.1× |
42//!
43//! # LP solver
44//!
45//! Row-wise Tchakaloff reduction is framed as a feasibility LP (minimize
46//! `0` subject to the equality constraints) and solved with `microlp`.
47//! The problem is small (≤ S + k + 1 rows × |support| columns, here
48//! typically ~ 10 × ~ 100) so a pure-Rust simplex is fast enough.
49
50use std::fmt;
51
52use microlp::{ComparisonOp, OptimizationDirection, Problem};
53
54use crate::resolution::ResolutionMatrix;
55
56/// Errors from [`SparseEmpiricalCubaturePlan`] construction.
57#[derive(Debug)]
58pub enum CubatureBuildError {
59 /// Flat `sigmas` storage has the wrong total element count.
60 ///
61 /// `sigmas` is stored row-major as `sigmas[j * n_rows + ℓ] =
62 /// σ_j(E'_ℓ)`, so the expected total length is `k * n_rows`.
63 SigmaGridMismatch {
64 /// Expected total element count (`k * n_rows`).
65 expected: usize,
66 /// Actual `sigmas.len()`.
67 actual: usize,
68 },
69 /// Zero isotopes supplied — the cubature has no meaning for k = 0.
70 ZeroIsotopes,
71 /// Zero training densities supplied — the LP construction requires
72 /// at least one forward feature per row.
73 ZeroTrainingDensities,
74 /// A training density vector has a length different from the
75 /// isotope count.
76 TrainingDensityLength {
77 /// Expected (k).
78 expected: usize,
79 /// Actual (`training_densities[i].len()`).
80 actual: usize,
81 /// Offending index.
82 index: usize,
83 },
84 /// The Jacobian anchor density has a length different from the
85 /// isotope count.
86 AnchorLength {
87 /// Expected (k).
88 expected: usize,
89 /// Actual.
90 actual: usize,
91 },
92 /// The row-wise LP failed to produce a feasible solution. Should
93 /// never fire on a well-formed problem because the uniform
94 /// (non-sparse) weight is always feasible; if it does, it signals
95 /// a numerical degeneracy (e.g., identical atoms in the row
96 /// support) worth investigating.
97 LpInfeasible {
98 /// Row of the resolution matrix where the LP failed.
99 row: usize,
100 },
101}
102
103impl fmt::Display for CubatureBuildError {
104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105 match self {
106 Self::SigmaGridMismatch { expected, actual } => write!(
107 f,
108 "sigmas flat length ({actual}) must equal k * n_rows ({expected})",
109 ),
110 Self::ZeroIsotopes => write!(f, "cubature requires at least one isotope"),
111 Self::ZeroTrainingDensities => {
112 write!(f, "cubature requires at least one training density sample",)
113 }
114 Self::TrainingDensityLength {
115 expected,
116 actual,
117 index,
118 } => write!(
119 f,
120 "training_densities[{index}] has length {actual} (expected k = {expected})",
121 ),
122 Self::AnchorLength { expected, actual } => write!(
123 f,
124 "jacobian_anchor has length {actual} (expected k = {expected})",
125 ),
126 Self::LpInfeasible { row } => write!(
127 f,
128 "row-wise LP failed to find a feasible cubature for row {row} — \
129 likely numerical degeneracy in the row support",
130 ),
131 }
132 }
133}
134
135impl std::error::Error for CubatureBuildError {}
136
137/// Row-wise Tchakaloff cubature of the joint σ-pushforward measure on a
138/// fixed target grid.
139///
140/// Laid out in flat Struct-of-Arrays (SoA) form for cache-friendly online
141/// evaluation:
142///
143/// * `row_starts[i]..row_starts[i+1]` indexes into `weights`/`atoms` for
144/// row `i`.
145/// * `weights[q]` is the per-atom nonneg weight (sums to 1.0 within each
146/// row since the source measure is row-stochastic).
147/// * `atoms[q]` is a flat row-major block of length `k` storing the
148/// atom's joint σ coordinates.
149///
150/// Built once per `(grid, isotope_set, training_densities, anchor)`
151/// tuple and applied repeatedly during LM / KL iterations via
152/// [`Self::forward`] and [`Self::forward_and_jacobian`].
153#[derive(Debug, Clone)]
154pub struct SparseEmpiricalCubaturePlan {
155 /// Target energy grid the plan was built for (owned copy, same
156 /// pattern as [`crate::resolution::ResolutionPlan`] /
157 /// [`crate::resolution::ResolutionMatrix`]). Callers implementing
158 /// plan caches compare this against their current grid to decide
159 /// whether the plan is still valid.
160 target_energies: Vec<f64>,
161 /// Number of isotopes (per-atom dimensionality).
162 k: usize,
163 /// `row_starts[i]..row_starts[i+1]` — CSR-style row offsets.
164 /// Length `target_energies.len() + 1`.
165 row_starts: Vec<u32>,
166 /// Per-atom nonneg weights. Within each row, `Σ_q weights[q] = 1`.
167 weights: Vec<f64>,
168 /// Row-major flat storage of atom coordinates in ℝ^k. Length
169 /// `k * weights.len()`. Atom `q` occupies indices `k*q .. k*(q+1)`.
170 atoms: Vec<f64>,
171 /// Optional training-density upper bound — the per-isotope
172 /// `train_max` used to build the plan. When set, dispatch
173 /// layers can compare the current fit iterate against it and
174 /// fall back to the exact path when the iterate strays beyond
175 /// the box (with a tolerance multiplier to avoid thrashing).
176 /// `None` means "no box information available; dispatch cannot
177 /// safety-check against it". Set via
178 /// [`Self::with_density_box`].
179 density_box: Option<Vec<f64>>,
180}
181
182impl SparseEmpiricalCubaturePlan {
183 /// Canonical default training-density rule from the design-study
184 /// reference implementation: for an upper-bound density vector
185 /// `train_max ∈ ℝ^k`, return `S = 2 + k` training points
186 /// consisting of `0.25 * train_max`, `0.75 * train_max`, and the
187 /// k axis-aligned "unit" points `train_max[i] · e_i` (all other
188 /// components zero). Exposed as a helper so callers don't have
189 /// to hand-roll the rule.
190 ///
191 /// Duplicates are NOT removed. In practice the rule produces
192 /// `S = k + 2` distinct points for any `k ≥ 1` with all
193 /// `train_max[i] > 0`.
194 pub fn default_training_points(train_max: &[f64]) -> Vec<Vec<f64>> {
195 let k = train_max.len();
196 let mut points: Vec<Vec<f64>> = Vec::with_capacity(k + 2);
197 points.push(train_max.iter().map(|&x| 0.25 * x).collect());
198 points.push(train_max.iter().map(|&x| 0.75 * x).collect());
199 for (i, &max_i) in train_max.iter().enumerate() {
200 let mut p = vec![0.0_f64; k];
201 p[i] = max_i;
202 points.push(p);
203 }
204 points
205 }
206
207 /// Canonical default Jacobian anchor from the design-study
208 /// reference implementation: `0.5 * train_max`, the midpoint of
209 /// the density box.
210 pub fn default_jacobian_anchor(train_max: &[f64]) -> Vec<f64> {
211 train_max.iter().map(|&x| 0.5 * x).collect()
212 }
213
214 /// Build a Tchakaloff sparse-cubature plan row-by-row from an exact
215 /// [`ResolutionMatrix`] + isotope cross-section stack.
216 ///
217 /// # Arguments
218 ///
219 /// * `matrix` — exact sparse R (built via
220 /// [`crate::resolution::ResolutionPlan::compile_to_matrix`]).
221 /// * `sigmas` — per-isotope cross-sections on the matrix's target
222 /// grid, flat row-major: `sigmas[j * n_rows + ℓ]` = σ_j(E'_ℓ).
223 /// * `k` — number of isotopes (must match `sigmas.len() / n_rows`).
224 /// * `training_densities` — a slice of density vectors `n^(s) ∈
225 /// ℝ^k` covering the density box the fit is expected to explore.
226 /// The canonical default rule is `[0.25 * train_max, 0.75 *
227 /// train_max] ∪ {train_max_e_i : i=1..k}` which gives `S = 2 + k`
228 /// distinct training points.
229 /// * `jacobian_anchor` — a single density `n* ∈ ℝ^k` at which the
230 /// Jacobian features are evaluated. The canonical default is
231 /// `0.5 * train_max`.
232 ///
233 /// Per-row LP:
234 ///
235 /// ```text
236 /// find x ≥ 0 in ℝ^{|support|}
237 /// s.t. Σ_q x_q = 1
238 /// phi[s, q] = exp(-n^(s) · σ_support[q]) for s = 1..S
239 /// phi[ℓ, q] = σ_{ℓ, support[q]} · exp(-n* · σ_support[q])
240 /// for ℓ = 1..k
241 /// phi @ x = phi @ w_exact_support
242 /// ```
243 ///
244 /// where `w_exact_support = R[i, support] / Σ_q R[i, support[q]]`
245 /// is the **exact full-support row measure** (the existing
246 /// non-sparse weight distribution — NOT uniform; the entries
247 /// carry the kernel shape). It serves as the feasibility
248 /// fallback for the LP: the identity `x = w_exact_support`
249 /// always satisfies the equality constraints, so a feasible
250 /// solution exists. The returned basic feasible solution has
251 /// at most `S + k + 1` nonzero entries (Carathéodory).
252 pub fn build(
253 matrix: &ResolutionMatrix,
254 sigmas: &[f64],
255 k: usize,
256 training_densities: &[Vec<f64>],
257 jacobian_anchor: &[f64],
258 ) -> Result<Self, CubatureBuildError> {
259 if k == 0 {
260 return Err(CubatureBuildError::ZeroIsotopes);
261 }
262 if training_densities.is_empty() {
263 return Err(CubatureBuildError::ZeroTrainingDensities);
264 }
265 let n_rows = matrix.len();
266 if sigmas.len() != k * n_rows {
267 return Err(CubatureBuildError::SigmaGridMismatch {
268 expected: k * n_rows,
269 actual: sigmas.len(),
270 });
271 }
272 for (idx, td) in training_densities.iter().enumerate() {
273 if td.len() != k {
274 return Err(CubatureBuildError::TrainingDensityLength {
275 expected: k,
276 actual: td.len(),
277 index: idx,
278 });
279 }
280 }
281 if jacobian_anchor.len() != k {
282 return Err(CubatureBuildError::AnchorLength {
283 expected: k,
284 actual: jacobian_anchor.len(),
285 });
286 }
287
288 // Empty matrix — return an empty plan.
289 if n_rows == 0 {
290 return Ok(Self {
291 target_energies: matrix.target_energies().to_vec(),
292 k,
293 row_starts: vec![0],
294 weights: Vec::new(),
295 atoms: Vec::new(),
296 density_box: None,
297 });
298 }
299
300 let n_train = training_densities.len();
301 // Per-row LP has `n_train + k` equality rows for `phi @ x =
302 // target` plus 1 for `sum x = 1`.
303 let phi_rows = n_train + k;
304
305 let mut row_starts: Vec<u32> = Vec::with_capacity(n_rows + 1);
306 row_starts.push(0);
307 let mut weights: Vec<f64> = Vec::new();
308 let mut atoms: Vec<f64> = Vec::new();
309
310 // Reusable scratch across rows. Per-row support widths differ,
311 // but the max is bounded by `max(row_nnz) ≤ 132` on the real
312 // VENUS operator; `clear()` reuses the `Vec` capacity.
313 let mut support_sigma: Vec<f64> = Vec::new(); // k * |support|, row-major over atoms
314 let mut w_exact: Vec<f64> = Vec::new(); // |support|
315 let mut phi_fwd: Vec<f64> = Vec::new(); // n_train × |support|, row-major over rows
316 let mut phi_grad: Vec<f64> = Vec::new(); // k × |support|
317 let mut grad_base: Vec<f64> = Vec::new(); // |support| — exp(-anchor · σ_q) hoisted out of ell loop
318 let mut target: Vec<f64> = Vec::new(); // phi_rows
319 let mut phi_col_buf: Vec<(microlp::Variable, f64)> = Vec::new();
320
321 for i in 0..n_rows {
322 let start = matrix.row_starts()[i] as usize;
323 let end = matrix.row_starts()[i + 1] as usize;
324 let support_cols = &matrix.col_indices()[start..end];
325 let support_vals = &matrix.values()[start..end];
326 let support_len = support_cols.len();
327
328 // Passthrough / empty row → emit uniform weight directly.
329 // No LP needed. (A single row with a single entry at col
330 // i, value 1.0, stays as a single atom — its pushforward
331 // coordinates are just σ at that column.)
332 if support_len == 0 {
333 row_starts.push(weights.len() as u32);
334 continue;
335 }
336
337 // Shortcut: if the row support has only 1 column, the
338 // cubature is that single atom with weight 1. No LP and
339 // no feature matrix needed. Must check BEFORE building
340 // w_exact / phi to avoid the work the shortcut then
341 // discards.
342 if support_len == 1 {
343 let col = support_cols[0] as usize;
344 weights.push(1.0);
345 atoms.extend((0..k).map(|j| sigmas[j * n_rows + col]));
346 row_starts.push(weights.len() as u32);
347 continue;
348 }
349
350 // Non-trivial row (support_len ≥ 2). Build normalized
351 // exact-weight distribution + collect support-column σ
352 // vectors.
353 //
354 // **Zero-weight CSR cells MUST be filtered out** before
355 // they reach the LP. [`ResolutionPlan::compile_to_matrix`]
356 // deliberately retains `value == 0.0` entries for the
357 // `frac == +0.0` branch to preserve downstream NaN-safety
358 // when the matrix is re-applied to a spectrum containing
359 // NaN at `lo + 1`. But the cubature LP has a zero
360 // objective, so the simplex is free to assign positive
361 // mass to any zero-weight variable — the training
362 // constraints pass trivially (w_exact = 0 → target
363 // contribution = 0), yet held-out forward/Jacobian
364 // predictions can pick up mass at energies the exact
365 // resolution operator never samples. Filter them here
366 // so no zero-R column ever becomes an LP variable or a
367 // stored atom.
368 //
369 // Row sum guard: the source matrix is row-stochastic
370 // (Σ_q R_{iq} = 1 to machine precision), so dropping
371 // exactly-zero columns preserves `row_sum > 0`.
372 let row_sum: f64 = support_vals.iter().sum();
373 support_sigma.clear();
374 support_sigma.reserve(k * support_len);
375 w_exact.clear();
376 w_exact.reserve(support_len);
377 for (q, &col_u32) in support_cols.iter().enumerate() {
378 if support_vals[q] == 0.0 {
379 continue;
380 }
381 let col = col_u32 as usize;
382 for j in 0..k {
383 support_sigma.push(sigmas[j * n_rows + col]);
384 }
385 w_exact.push(support_vals[q] / row_sum);
386 }
387 // Effective support length after dropping zero-weight
388 // CSR cells. Subsequent LP / feature-matrix code uses
389 // this, not the original `support_len` that included
390 // zero-weight cells.
391 let support_len = w_exact.len();
392
393 // Re-check the degenerate cases on the filtered support.
394 // If all CSR cells happened to be zero, treat like an
395 // empty row. If exactly one survives, take the shortcut.
396 if support_len == 0 {
397 row_starts.push(weights.len() as u32);
398 continue;
399 }
400 if support_len == 1 {
401 weights.push(1.0);
402 atoms.extend_from_slice(&support_sigma[..k]);
403 row_starts.push(weights.len() as u32);
404 continue;
405 }
406
407 // Build per-row feature matrix phi (row-major over feature
408 // rows, then support columns).
409 phi_fwd.clear();
410 phi_fwd.reserve(n_train * support_len);
411 for td in training_densities.iter() {
412 for q in 0..support_len {
413 let mut dot = 0.0_f64;
414 for j in 0..k {
415 dot += td[j] * support_sigma[q * k + j];
416 }
417 phi_fwd.push((-dot).exp());
418 }
419 }
420 // Jacobian features `phi_grad[ℓ, q] = σ_{ℓ,q} · exp(-n* ·
421 // σ_q)`. The `exp(-n* · σ_q)` factor depends only on `q`,
422 // not `ℓ`, so hoist it into a row-local `grad_base[q]`
423 // buffer to avoid recomputing |support| × k exponentials
424 // (matches the design study's Python reference `phi_grad_base`
425 // layout).
426 phi_grad.clear();
427 phi_grad.reserve(k * support_len);
428 grad_base.clear();
429 grad_base.reserve(support_len);
430 for q in 0..support_len {
431 let mut dot = 0.0_f64;
432 for j in 0..k {
433 dot += jacobian_anchor[j] * support_sigma[q * k + j];
434 }
435 grad_base.push((-dot).exp());
436 }
437 for ell in 0..k {
438 for q in 0..support_len {
439 phi_grad.push(support_sigma[q * k + ell] * grad_base[q]);
440 }
441 }
442
443 // Target = phi @ w_exact, built streaming per feature row.
444 target.clear();
445 target.reserve(phi_rows);
446 for s in 0..n_train {
447 let mut t = 0.0_f64;
448 for q in 0..support_len {
449 t += phi_fwd[s * support_len + q] * w_exact[q];
450 }
451 target.push(t);
452 }
453 for ell in 0..k {
454 let mut t = 0.0_f64;
455 for q in 0..support_len {
456 t += phi_grad[ell * support_len + q] * w_exact[q];
457 }
458 target.push(t);
459 }
460
461 // Feasibility LP: minimize 0 subject to the equality
462 // constraints. Each column = one atom; coefficient on the
463 // objective = 0. `x_q ∈ [0, ∞)`.
464 let mut problem = Problem::new(OptimizationDirection::Minimize);
465 let vars: Vec<microlp::Variable> = (0..support_len)
466 .map(|_| problem.add_var(0.0, (0.0, f64::INFINITY)))
467 .collect();
468
469 // sum x_q = 1
470 phi_col_buf.clear();
471 for &v in &vars {
472 phi_col_buf.push((v, 1.0));
473 }
474 problem.add_constraint(&phi_col_buf, ComparisonOp::Eq, 1.0);
475
476 // phi @ x = target, one equality per feature row.
477 for s in 0..n_train {
478 phi_col_buf.clear();
479 for q in 0..support_len {
480 phi_col_buf.push((vars[q], phi_fwd[s * support_len + q]));
481 }
482 problem.add_constraint(&phi_col_buf, ComparisonOp::Eq, target[s]);
483 }
484 for ell in 0..k {
485 phi_col_buf.clear();
486 for q in 0..support_len {
487 phi_col_buf.push((vars[q], phi_grad[ell * support_len + q]));
488 }
489 problem.add_constraint(&phi_col_buf, ComparisonOp::Eq, target[n_train + ell]);
490 }
491
492 // Solve. If `microlp` fails (it may on numerically
493 // degenerate row supports — e.g., identical σ across the
494 // row, which is physically rare but possible), fall back
495 // to the exact full-support row measure `w_exact`. This
496 // preserves correctness at the cost of giving up
497 // compression on that row. The `LpInfeasible` error
498 // variant (returned below) only fires if BOTH the LP
499 // solution AND the `w_exact` fallback produce an empty
500 // active set after the `WEIGHT_EPSILON` filter — which
501 // is physically impossible on a valid row-stochastic
502 // `ResolutionMatrix` row (`Σ w_exact = 1` implies at
503 // least one entry exceeds `1 / support_len > 1e-12`).
504 let sparse_weights: Vec<f64> = match problem.solve() {
505 Ok(solution) => vars.iter().map(|&v| solution.var_value(v)).collect(),
506 Err(_) => w_exact.clone(),
507 };
508
509 // Drop numerically-zero atoms and renormalize so the row
510 // still sums to exactly 1.0 after simplex roundoff.
511 const WEIGHT_EPSILON: f64 = 1e-12;
512 let mut active: Vec<(usize, f64)> = sparse_weights
513 .iter()
514 .enumerate()
515 .filter_map(|(q, &w)| (w > WEIGHT_EPSILON).then_some((q, w)))
516 .collect();
517 if active.is_empty() {
518 // Extreme fallback — should never happen because
519 // w_exact is already feasible with support_len > 0,
520 // but defend against a corrupt LP result.
521 active = w_exact
522 .iter()
523 .enumerate()
524 .filter_map(|(q, &w)| (w > WEIGHT_EPSILON).then_some((q, w)))
525 .collect();
526 if active.is_empty() {
527 return Err(CubatureBuildError::LpInfeasible { row: i });
528 }
529 }
530 let active_sum: f64 = active.iter().map(|&(_, w)| w).sum();
531 // Note: rows with repeated σ patterns (physically
532 // uncommon but possible) end up with multiple atoms at
533 // identical x. We emit them separately and rely on
534 // online forward evaluation to sum the weighted
535 // exponentials, which is algebraically identical to a
536 // pre-merged atom. Merging would be a micro-optimization
537 // worth revisiting only if profiling shows the duplicate
538 // work matters.
539
540 for (q, w) in active {
541 weights.push(w / active_sum);
542 for j in 0..k {
543 atoms.push(support_sigma[q * k + j]);
544 }
545 }
546 row_starts.push(weights.len() as u32);
547 }
548
549 Ok(Self {
550 target_energies: matrix.target_energies().to_vec(),
551 k,
552 row_starts,
553 weights,
554 atoms,
555 density_box: None,
556 })
557 }
558
559 /// Number of rows (target-grid size) covered by this plan.
560 pub fn len(&self) -> usize {
561 self.target_energies.len()
562 }
563
564 /// True when the plan covers no target energies.
565 pub fn is_empty(&self) -> bool {
566 self.target_energies.is_empty()
567 }
568
569 /// Number of isotopes (per-atom dimensionality).
570 pub fn k(&self) -> usize {
571 self.k
572 }
573
574 /// Total number of stored atoms across all rows.
575 pub fn n_atoms(&self) -> usize {
576 self.weights.len()
577 }
578
579 /// Target energy grid the plan was built for.
580 ///
581 /// Mirrors [`crate::resolution::ResolutionPlan::target_energies`]
582 /// / [`crate::resolution::ResolutionMatrix::target_energies`] —
583 /// callers implementing plan caches compare this against their
584 /// current grid to decide whether the plan is still valid.
585 pub fn target_energies(&self) -> &[f64] {
586 &self.target_energies
587 }
588
589 /// CSR row-start offsets. `row_starts()[i]..row_starts()[i+1]`
590 /// names the atom range for row `i`. Length `len() + 1`.
591 pub fn row_starts(&self) -> &[u32] {
592 &self.row_starts
593 }
594
595 /// Per-atom weights.
596 pub fn weights(&self) -> &[f64] {
597 &self.weights
598 }
599
600 /// Per-atom σ coordinates, flat row-major. Atom `q` at
601 /// `atoms()[k * q .. k * (q + 1)]`.
602 pub fn atoms(&self) -> &[f64] {
603 &self.atoms
604 }
605
606 /// Training-density upper bound recorded at build time, if any.
607 /// Dispatch layers use this to detect when the fit iterate
608 /// escapes the training region and safely fall back to the
609 /// exact path (cubature accuracy degrades quickly outside the
610 /// trained box). `None` when the caller chose not to record
611 /// one — in that case dispatch cannot safety-check.
612 pub fn density_box(&self) -> Option<&[f64]> {
613 self.density_box.as_deref()
614 }
615
616 /// Attach the training-density upper bound (`train_max`) used
617 /// during build, so dispatch can refuse to fire on iterates
618 /// that escape the trained region. Builder-style; returns
619 /// `self` for chaining. Callers in `spatial_map_typed`
620 /// populate this with the same `train_max` vector fed into
621 /// [`Self::default_training_points`] / [`Self::default_jacobian_anchor`].
622 ///
623 /// # Panics
624 ///
625 /// Panics if `train_max.len() != self.k()`.
626 #[must_use]
627 pub fn with_density_box(mut self, train_max: Vec<f64>) -> Self {
628 assert_eq!(
629 train_max.len(),
630 self.k,
631 "train_max length ({}) must equal k ({})",
632 train_max.len(),
633 self.k,
634 );
635 self.density_box = Some(train_max);
636 self
637 }
638
639 /// Evaluate the surrogate forward model `T_i(n)` at density vector
640 /// `n ∈ ℝ^k`.
641 ///
642 /// # Panics
643 ///
644 /// Panics if `n.len() != self.k()`.
645 pub fn forward(&self, n: &[f64]) -> Vec<f64> {
646 assert_eq!(
647 n.len(),
648 self.k,
649 "density vector length ({}) must match plan isotope count ({})",
650 n.len(),
651 self.k,
652 );
653 let mut out = vec![0.0_f64; self.target_energies.len()];
654 for (i, out_i) in out.iter_mut().enumerate() {
655 let s = self.row_starts[i] as usize;
656 let e = self.row_starts[i + 1] as usize;
657 let mut acc = 0.0_f64;
658 for q in s..e {
659 let atom = &self.atoms[q * self.k..(q + 1) * self.k];
660 let mut dot = 0.0_f64;
661 for j in 0..self.k {
662 dot += n[j] * atom[j];
663 }
664 acc += self.weights[q] * (-dot).exp();
665 }
666 *out_i = acc;
667 }
668 out
669 }
670
671 /// Evaluate forward + per-density Jacobian at density vector `n`.
672 /// Returns `(T, J)` where `T[i] = T_i(n)` and `J[i * k + ℓ] =
673 /// ∂T_i/∂n_ℓ`, both computed from the same atom scan so the online
674 /// cost is `(k + 1)` FLOPs per atom rather than `k + 1` separate
675 /// passes.
676 ///
677 /// # Panics
678 ///
679 /// Panics if `n.len() != self.k()`.
680 pub fn forward_and_jacobian(&self, n: &[f64]) -> (Vec<f64>, Vec<f64>) {
681 assert_eq!(
682 n.len(),
683 self.k,
684 "density vector length ({}) must match plan isotope count ({})",
685 n.len(),
686 self.k,
687 );
688 let mut forward = vec![0.0_f64; self.target_energies.len()];
689 let mut jac = vec![0.0_f64; self.target_energies.len() * self.k];
690 for i in 0..self.target_energies.len() {
691 let s = self.row_starts[i] as usize;
692 let e = self.row_starts[i + 1] as usize;
693 let mut t_i = 0.0_f64;
694 let jac_row = &mut jac[i * self.k..(i + 1) * self.k];
695 for q in s..e {
696 let atom = &self.atoms[q * self.k..(q + 1) * self.k];
697 let mut dot = 0.0_f64;
698 for j in 0..self.k {
699 dot += n[j] * atom[j];
700 }
701 let term = self.weights[q] * (-dot).exp();
702 t_i += term;
703 for (ell, jac_slot) in jac_row.iter_mut().enumerate() {
704 *jac_slot -= term * atom[ell];
705 }
706 }
707 forward[i] = t_i;
708 }
709 (forward, jac)
710 }
711}
712
713// ═══════════════════════════════════════════════════════════════════
714// Scalar (k = 1) surrogate — epic #472.
715// ═══════════════════════════════════════════════════════════════════
716//
717// The [`SparseEmpiricalCubaturePlan`] above is the k ≥ 2 production
718// winner, but its generic atom construction over-damps the grouped
719// Hf k = 1 KL scatter by ~27 % (design-study measurement). The
720// scalar path gets a dedicated surrogate. Both
721// study candidates (Lanczos σ-pushforward Gauss quadrature,
722// Chebyshev-in-density) were built side-by-side and benched on
723// the real VENUS 3471-bin production grid. Chebyshev won both the
724// accuracy (max_err ≤ 2e-15 vs ≤ 4e-15) **and** the wall-time
725// axis by a wide margin — the ordering is stable across
726// hardware, even though absolute µs-per-row numbers aren't.
727// **Chebyshev won**; Lanczos + Gauss-pushforward machinery was
728// deleted per the issue's "drop the loser" contract — no
729// same-name-different-function duplication. If future research
730// finds a better scalar surrogate, the public
731// [`ScalarSurrogatePlan`] alias below is the stable swap point.
732
733/// Errors from scalar surrogate plan construction.
734#[derive(Debug)]
735pub enum ScalarSurrogateBuildError {
736 /// `sigma` flat length disagrees with the matrix grid size.
737 SigmaGridMismatch {
738 /// Expected length (`n_rows`).
739 expected: usize,
740 /// Actual `sigma.len()`.
741 actual: usize,
742 },
743 /// A Chebyshev-node build was given `n_max ≤ 0` or `M < 2`.
744 InvalidChebyshevBox {
745 /// Offending upper bound.
746 n_max: f64,
747 /// Requested node count.
748 m: usize,
749 },
750 /// The Chebyshev interpolant cannot reach target accuracy on the
751 /// requested `[0, n_max]` box with `M` nodes — the box is too
752 /// wide for the σ profile. Chebyshev converges exponentially in
753 /// `M` for smooth `T(n) = exp(-n σ)`, but if `max(n_max · σ)` is
754 /// large the interpolant loses precision. Callers should either
755 /// shrink `n_max` (preferred — tighter fit-exploration bounds
756 /// fix this) or increase `M`.
757 InsufficientAccuracyOnBox {
758 /// Requested density box upper bound.
759 n_max: f64,
760 /// Chebyshev node count that failed.
761 m: usize,
762 /// Measured maximum relative error of the interpolant
763 /// against the exact `apply_r ∘ exp(-n σ)` on the box
764 /// (evaluated at midpoints between Chebyshev nodes).
765 max_rel_err: f64,
766 /// Required tolerance (currently `1e-6`).
767 tolerance: f64,
768 },
769}
770
771impl fmt::Display for ScalarSurrogateBuildError {
772 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
773 match self {
774 Self::SigmaGridMismatch { expected, actual } => write!(
775 f,
776 "scalar sigma length ({actual}) must equal n_rows ({expected})",
777 ),
778 Self::InvalidChebyshevBox { n_max, m } => write!(
779 f,
780 "Chebyshev plan requires n_max > 0 and M ≥ 2, got n_max = {n_max}, M = {m}",
781 ),
782 Self::InsufficientAccuracyOnBox {
783 n_max,
784 m,
785 max_rel_err,
786 tolerance,
787 } => write!(
788 f,
789 "Chebyshev plan ({m} nodes) on box [0, {n_max}] hit max rel err \
790 {max_rel_err:.3e} > tolerance {tolerance:.0e}; either shrink n_max \
791 (solver exploration range) or increase M",
792 ),
793 }
794 }
795}
796
797impl std::error::Error for ScalarSurrogateBuildError {}
798
799/// Chebyshev-in-density interpolant of `T_i(n)` for scalar (k = 1)
800/// forward models. For each row `i`, pre-samples `T_i(n_j)` at
801/// `M` Chebyshev-of-the-first-kind nodes in `[0, n_max]`, then
802/// stores the Chebyshev coefficients. Online evaluation is
803/// Clenshaw recurrence with `M` multiply-adds per row.
804///
805/// Unlike the Gauss quadrature, the Chebyshev representation is a
806/// **scalar interpolant** in density space — one pass evaluates
807/// the interpolant at `n`, and the derivative needs a separate
808/// derivative-coefficient series.
809#[derive(Debug, Clone)]
810pub struct ScalarChebyshevPlan {
811 /// Target energy grid the plan was built for.
812 target_energies: Vec<f64>,
813 /// Upper bound of the density box `[0, n_max]` the interpolant
814 /// is valid on.
815 n_max: f64,
816 /// Number of Chebyshev nodes (order + 1). Same for every row.
817 m: usize,
818 /// Row-major Chebyshev coefficients: `coeffs[i * m + k]` is the
819 /// `k`-th Chebyshev coefficient of row `i`.
820 coeffs: Vec<f64>,
821 /// Optional training-density upper bound (defaults to `n_max`
822 /// if the builder doesn't override).
823 density_box: Option<f64>,
824 /// Shared reference to the [`crate::resolution::ResolutionPlan`]
825 /// the plan was built from. Dispatch uses `Arc::ptr_eq` between
826 /// this and the model's currently attached resolution plan as
827 /// an O(1) identity check to refuse stale plans on the same
828 /// energy grid.
829 source_resolution_plan: std::sync::Arc<crate::resolution::ResolutionPlan>,
830 /// FNV-1a-64 fingerprint of the σ slice (`to_bits()` per
831 /// element) the plan was built from. Dispatch recomputes
832 /// from the model's current σ and compares — catches stale
833 /// plans where the grid is unchanged but σ differs.
834 sigma_fingerprint: u64,
835}
836
837/// FNV-1a-64 hash of an `f64` slice by bit pattern — used for
838/// scalar-surrogate dispatch's σ-identity check.
839pub fn fingerprint_f64_slice(xs: &[f64]) -> u64 {
840 const FNV_OFFSET: u64 = 0xcbf29ce484222325;
841 const FNV_PRIME: u64 = 0x100000001b3;
842 let mut h = FNV_OFFSET;
843 for &v in xs {
844 h ^= v.to_bits();
845 h = h.wrapping_mul(FNV_PRIME);
846 }
847 h
848}
849
850impl ScalarChebyshevPlan {
851 /// Build an `M`-node Chebyshev-in-density plan from a shared
852 /// [`crate::resolution::ResolutionPlan`] + scalar σ + density
853 /// box `[0, n_max]`.
854 ///
855 /// The `source_resolution_plan` `Arc` is **stored on the plan**
856 /// so the dispatch-time eligibility check can use
857 /// `Arc::ptr_eq` to refuse stale plans on the same grid.
858 /// A matching σ fingerprint
859 /// is also computed and stored for the same reason: same-grid
860 /// σ-mismatch would otherwise trigger silently-wrong
861 /// transmissions.
862 ///
863 /// Internally calls `source_resolution_plan.compile_to_matrix()`
864 /// once, then `crate::resolution::apply_r` `M` times (one per
865 /// Chebyshev node) to get exact row evaluations, then runs a
866 /// per-row discrete cosine transform to extract Chebyshev
867 /// coefficients.
868 ///
869 /// Cost: one matrix compile + `M × N × avg_nnz_per_row` FMAs
870 /// for the exact sampling pass, plus `M^2` per row for the DCT.
871 pub fn build(
872 source_resolution_plan: std::sync::Arc<crate::resolution::ResolutionPlan>,
873 sigma: &[f64],
874 n_max: f64,
875 m: usize,
876 ) -> Result<Self, ScalarSurrogateBuildError> {
877 let matrix = source_resolution_plan.compile_to_matrix();
878 let n_rows = matrix.len();
879 if sigma.len() != n_rows {
880 return Err(ScalarSurrogateBuildError::SigmaGridMismatch {
881 expected: n_rows,
882 actual: sigma.len(),
883 });
884 }
885 if !n_max.is_finite() || n_max <= 0.0 || m < 2 {
886 return Err(ScalarSurrogateBuildError::InvalidChebyshevBox { n_max, m });
887 }
888
889 // Chebyshev nodes of the first kind on [-1, 1]:
890 // x_j = cos(π (j + 0.5) / M) for j = 0..M-1
891 // Mapped to [0, n_max]:
892 // n_j = (n_max / 2) (x_j + 1)
893 let nodes_x: Vec<f64> = (0..m)
894 .map(|j| {
895 let pj = (j as f64 + 0.5) * std::f64::consts::PI / m as f64;
896 pj.cos()
897 })
898 .collect();
899 let nodes_n: Vec<f64> = nodes_x.iter().map(|&x| 0.5 * n_max * (x + 1.0)).collect();
900
901 // Evaluate T_i(n_j) exactly for each j. `values[j * n_rows
902 // + i]` = T_i(n_j).
903 let mut samples = vec![0.0_f64; m * n_rows];
904 for (j, &nj) in nodes_n.iter().enumerate() {
905 let t_un: Vec<f64> = (0..n_rows).map(|i| (-nj * sigma[i]).exp()).collect();
906 let t_res = crate::resolution::apply_r(&matrix, &t_un);
907 for (i, &v) in t_res.iter().enumerate() {
908 samples[j * n_rows + i] = v;
909 }
910 }
911
912 // DCT-II to extract Chebyshev coefficients per row.
913 // c_k = (2 / M) Σ_j T_i(n_j) T_k(x_j) for k ≥ 1
914 // c_0 = (1 / M) Σ_j T_i(n_j)
915 // where T_k(cos θ) = cos(k θ), θ_j = π (j + 0.5) / M.
916 let mut coeffs = vec![0.0_f64; n_rows * m];
917 for i in 0..n_rows {
918 for k in 0..m {
919 let mut sum = 0.0_f64;
920 for j in 0..m {
921 let theta_j = (j as f64 + 0.5) * std::f64::consts::PI / m as f64;
922 sum += samples[j * n_rows + i] * (k as f64 * theta_j).cos();
923 }
924 let scale = if k == 0 { 1.0 } else { 2.0 } / m as f64;
925 coeffs[i * m + k] = scale * sum;
926 }
927 }
928
929 // Build-time accuracy self-check.
930 //
931 // Chebyshev interpolants are exact at their nodes by
932 // construction; the test points that reveal how wide the
933 // box can safely be are the **midpoints** between
934 // Chebyshev nodes (where the standard Chebyshev error
935 // bound attains its supremum on the box). We evaluate
936 // the just-built interpolant at those midpoints, compare
937 // to the exact `apply_r ∘ exp(-n σ)`, and refuse to
938 // return a plan that blows the accuracy budget.
939 //
940 // The threshold (`1e-6` max rel err) matches the "close
941 // to exact" bar in the scalar-surrogate docstrings. For
942 // typical VENUS fits (τ_peak ≲ 1, box = 2 × initial
943 // density) the interpolant achieves ≤ 1e-15 — this
944 // guard fires only when a caller passes a pathologically
945 // wide box.
946 let sigma_fingerprint = fingerprint_f64_slice(sigma);
947 let plan = Self {
948 target_energies: matrix.target_energies().to_vec(),
949 n_max,
950 m,
951 coeffs,
952 density_box: Some(n_max),
953 source_resolution_plan: std::sync::Arc::clone(&source_resolution_plan),
954 sigma_fingerprint,
955 };
956 const TOLERANCE: f64 = 1e-6;
957 let mut max_rel_err = 0.0_f64;
958 for j in 0..m.saturating_sub(1) {
959 // Midpoint between Chebyshev node j and j+1, in density space.
960 let n_mid = 0.5 * (nodes_n[j] + nodes_n[j + 1]);
961 let t_interp = plan.forward_scalar(n_mid);
962 let t_un: Vec<f64> = (0..n_rows).map(|i| (-n_mid * sigma[i]).exp()).collect();
963 let t_exact = crate::resolution::apply_r(&matrix, &t_un);
964 // Plain relative error with a 1e-15 denominator floor
965 // (matches `max_hybrid_err` conventions elsewhere in
966 // the crate). The previous
967 // `abs.min(rel)` could dramatically under-report when
968 // `|a|, |b|` are both small, hiding catastrophic
969 // divergence where the interpolant drifts to O(1)
970 // while the exact value tends to 0.
971 for (a, b) in t_interp.iter().zip(t_exact.iter()) {
972 let abs = (a - b).abs();
973 let rel = abs / a.abs().max(b.abs()).max(1e-15);
974 max_rel_err = max_rel_err.max(rel);
975 }
976 }
977 if !max_rel_err.is_finite() || max_rel_err > TOLERANCE {
978 return Err(ScalarSurrogateBuildError::InsufficientAccuracyOnBox {
979 n_max,
980 m,
981 max_rel_err,
982 tolerance: TOLERANCE,
983 });
984 }
985
986 Ok(plan)
987 }
988
989 pub fn len(&self) -> usize {
990 self.target_energies.len()
991 }
992 pub fn is_empty(&self) -> bool {
993 self.target_energies.is_empty()
994 }
995 pub fn target_energies(&self) -> &[f64] {
996 &self.target_energies
997 }
998 pub fn n_max(&self) -> f64 {
999 self.n_max
1000 }
1001 pub fn m(&self) -> usize {
1002 self.m
1003 }
1004 pub fn density_box(&self) -> Option<f64> {
1005 self.density_box
1006 }
1007 /// Accessor for the shared
1008 /// [`crate::resolution::ResolutionPlan`] the plan was built from.
1009 /// Dispatch uses `Arc::ptr_eq` between this and the model's
1010 /// currently attached `resolution_plan` as the O(1) identity
1011 /// check that refuses stale plans on the same grid.
1012 pub fn source_resolution_plan(&self) -> &std::sync::Arc<crate::resolution::ResolutionPlan> {
1013 &self.source_resolution_plan
1014 }
1015 /// FNV-1a-64 fingerprint of the σ slice (by `to_bits()`) the
1016 /// plan was built from. Dispatch recomputes from the model's
1017 /// current σ and compares to catch same-grid σ-mismatch.
1018 pub fn sigma_fingerprint(&self) -> u64 {
1019 self.sigma_fingerprint
1020 }
1021
1022 /// Evaluate the Chebyshev interpolant at density `n`. Density
1023 /// outside `[0, n_max]` extrapolates (caller responsibility —
1024 /// dispatch should reject via the density-box check).
1025 pub fn forward_scalar(&self, n: f64) -> Vec<f64> {
1026 let n_rows = self.target_energies.len();
1027 let mut out = vec![0.0_f64; n_rows];
1028 if self.m == 0 {
1029 return out;
1030 }
1031 // Map n → x ∈ [-1, 1].
1032 let x = 2.0 * n / self.n_max - 1.0;
1033 // Clenshaw recurrence: evaluate Σ_k c_k T_k(x).
1034 // b_{M+1} = b_{M+2} = 0; b_k = 2 x b_{k+1} - b_{k+2} + c_k;
1035 // result = c_0 + x b_1 - b_2.
1036 for (i, out_i) in out.iter_mut().enumerate() {
1037 let row_start = i * self.m;
1038 let mut b_next = 0.0_f64;
1039 let mut b_next_next = 0.0_f64;
1040 for k in (1..self.m).rev() {
1041 let b_k = 2.0 * x * b_next - b_next_next + self.coeffs[row_start + k];
1042 b_next_next = b_next;
1043 b_next = b_k;
1044 }
1045 *out_i = self.coeffs[row_start] + x * b_next - b_next_next;
1046 }
1047 out
1048 }
1049
1050 /// Evaluate forward + derivative in one pass. The derivative
1051 /// of a Chebyshev series can be evaluated via a modified
1052 /// Clenshaw recurrence that internally tracks the derivative
1053 /// coefficients — or we use the standard identity
1054 /// `T_k'(x) = k · U_{k-1}(x)` (Chebyshev-of-the-second-kind
1055 /// recurrence). Here we run two parallel Clenshaw sweeps: one
1056 /// for `T(x)` and one for `d/dx T(x)`, then scale by
1057 /// `dx/dn = 2 / n_max`.
1058 pub fn forward_and_derivative_scalar(&self, n: f64) -> (Vec<f64>, Vec<f64>) {
1059 let n_rows = self.target_energies.len();
1060 let mut forward = vec![0.0_f64; n_rows];
1061 let mut deriv = vec![0.0_f64; n_rows];
1062 if self.m == 0 {
1063 return (forward, deriv);
1064 }
1065 let x = 2.0 * n / self.n_max - 1.0;
1066 let dx_dn = 2.0 / self.n_max;
1067
1068 // Derivative coefficients d_k such that Σ d_k T_k(x) =
1069 // d/dx Σ c_k T_k(x). Standard recurrence:
1070 // d_{M-1} = 0
1071 // d_{M-2} = 2 (M-1) c_{M-1}
1072 // d_k = d_{k+2} + 2 (k+1) c_{k+1} for k = M-3..0
1073 // (then d_0 needs to be halved if we want a simple Clenshaw,
1074 // but it's cleaner to use the "recurrence with halved d_0"
1075 // convention; we apply the same Clenshaw as forward.)
1076 let m = self.m;
1077 let mut d_coeffs = vec![0.0_f64; m];
1078
1079 for (i, (out_t, out_d)) in forward.iter_mut().zip(deriv.iter_mut()).enumerate() {
1080 let row_start = i * m;
1081 // Compute d_coeffs for this row.
1082 d_coeffs.fill(0.0);
1083 if m >= 2 {
1084 // d_{M-1} = 0 (already zero)
1085 // d_{M-2} = 2 (M-1) c_{M-1} for M ≥ 2
1086 for k in (0..m - 1).rev() {
1087 let prev = if k + 2 < m { d_coeffs[k + 2] } else { 0.0 };
1088 d_coeffs[k] = prev + 2.0 * (k as f64 + 1.0) * self.coeffs[row_start + k + 1];
1089 }
1090 d_coeffs[0] *= 0.5; // Clenshaw convention: halve d_0.
1091 }
1092
1093 // Clenshaw on forward coefficients.
1094 let mut b_next = 0.0_f64;
1095 let mut b_next_next = 0.0_f64;
1096 for k in (1..m).rev() {
1097 let b_k = 2.0 * x * b_next - b_next_next + self.coeffs[row_start + k];
1098 b_next_next = b_next;
1099 b_next = b_k;
1100 }
1101 *out_t = self.coeffs[row_start] + x * b_next - b_next_next;
1102
1103 // Clenshaw on derivative coefficients (dx/dx side).
1104 let mut b_next = 0.0_f64;
1105 let mut b_next_next = 0.0_f64;
1106 for k in (1..m).rev() {
1107 let b_k = 2.0 * x * b_next - b_next_next + d_coeffs[k];
1108 b_next_next = b_next;
1109 b_next = b_k;
1110 }
1111 let deriv_dx = d_coeffs[0] + x * b_next - b_next_next;
1112 *out_d = deriv_dx * dx_dn;
1113 }
1114 (forward, deriv)
1115 }
1116}
1117
1118/// Scalar (k = 1) surrogate used by the downstream dispatch
1119/// layers (see `TransmissionFitModel` / `PrecomputedTransmissionModel`).
1120///
1121/// This was an enum of `Gauss` vs `Chebyshev` during the
1122/// bench-off period; Chebyshev won the real-VENUS bench on both
1123/// accuracy (≤ 2e-15 vs ≤ 4e-15) and wall-time axes, and Lanczos
1124/// Gauss was deleted per the issue's "drop the loser" contract.
1125/// The type alias is kept as a public stable name so callers and
1126/// downstream dispatch code aren't coupled to the winning impl's
1127/// concrete type — if a future research sprint finds a better
1128/// scalar surrogate, only the alias moves.
1129pub type ScalarSurrogatePlan = ScalarChebyshevPlan;
1130
1131#[cfg(test)]
1132mod tests {
1133 use super::*;
1134 use crate::resolution::ResolutionPlan;
1135
1136 // ---------- Synthetic plan helpers (CI-hermetic) ----------
1137
1138 /// Build a synthetic (energies, sigmas, ResolutionMatrix) triple
1139 /// with a uniform triangular-kernel resolution operator and a
1140 /// hand-designed multi-isotope σ pattern. Avoids loading any
1141 /// fixture — these tests run on every `cargo test`.
1142 fn synthetic_setup(
1143 n_grid: usize,
1144 half_kernel: usize,
1145 k: usize,
1146 ) -> (
1147 Vec<f64>,
1148 Vec<f64>,
1149 crate::resolution::ResolutionMatrix,
1150 std::sync::Arc<crate::resolution::ResolutionPlan>,
1151 ) {
1152 assert!(n_grid > 2 * half_kernel);
1153 let energies: Vec<f64> = (0..n_grid).map(|i| 10.0 + i as f64).collect();
1154 // Build a ResolutionMatrix from a hand-constructed plan with
1155 // triangular-kernel rows — the same `make_synthetic_overlap_plan`
1156 // approach used in `resolution.rs` tests, inlined here to avoid
1157 // cross-module test visibility.
1158 let mut starts: Vec<u32> = Vec::with_capacity(n_grid + 1);
1159 starts.push(0);
1160 let mut lo_idx: Vec<u32> = Vec::new();
1161 let mut frac_arr: Vec<f64> = Vec::new();
1162 let mut weight_arr: Vec<f64> = Vec::new();
1163 let mut norm: Vec<f64> = Vec::with_capacity(n_grid);
1164 for i in 0..n_grid {
1165 let lo_min = i.saturating_sub(half_kernel);
1166 let lo_max = (i + half_kernel).min(n_grid - 2);
1167 let mut row_norm = 0.0_f64;
1168 for lo in lo_min..=lo_max {
1169 let d = (lo as i64 - i as i64).abs() as f64;
1170 let w = 1.0 - d / (half_kernel as f64 + 1.0);
1171 lo_idx.push(lo as u32);
1172 frac_arr.push(0.5);
1173 weight_arr.push(w);
1174 row_norm += w;
1175 }
1176 norm.push(row_norm);
1177 starts.push(lo_idx.len() as u32);
1178 }
1179 // Use the raw constructor via compile_to_matrix on a
1180 // manually-assembled plan. ResolutionPlan's fields are
1181 // crate-private, so we build it via the canonical plan
1182 // constructor (`TabulatedResolution::plan`) would require a
1183 // kernel — so we instead invoke the test-visible constructor
1184 // pattern the resolution module already uses internally.
1185 //
1186 // For the surrogate tests we only need the compiled matrix,
1187 // not the plan; we therefore build the ResolutionMatrix
1188 // directly (mirroring compile_to_matrix's output format)
1189 // without going through ResolutionPlan. This is done by
1190 // constructing the plan via the public `plan()` route from
1191 // a trivial TabulatedResolution proxy: a single-energy,
1192 // delta-kernel resolution that produces identity rows; then
1193 // overriding via a synthetic plan fixture would require
1194 // crate-private access.
1195 //
1196 // Simplest path: use a minimal `ResolutionPlan` surrogate by
1197 // directly building a `ResolutionMatrix`-equivalent CSR via
1198 // the `ResolutionPlan::compile_to_matrix` pathway. Since
1199 // that method consumes only the public fields above, we
1200 // expose a test-only helper `from_raw_parts` on ResolutionPlan.
1201 // (Added in this module as `SyntheticPlanBuilder` below.)
1202 let plan =
1203 SyntheticPlanBuilder::new(energies.clone(), starts, lo_idx, frac_arr, weight_arr, norm)
1204 .build();
1205 let matrix = plan.compile_to_matrix();
1206
1207 // Synthetic σ: k independent Gaussian resonances per isotope at
1208 // distinct energies, bounded in a physically plausible range.
1209 let mut sigmas = vec![0.0_f64; k * n_grid];
1210 for j in 0..k {
1211 let e_center = 10.0 + (j as f64 + 1.0) * (n_grid as f64) / (k as f64 + 1.0);
1212 let width = 3.0;
1213 for ell in 0..n_grid {
1214 let e = 10.0 + ell as f64;
1215 let g = (-((e - e_center).powi(2)) / (width * width)).exp();
1216 sigmas[j * n_grid + ell] = 100.0 * g + 5.0;
1217 }
1218 }
1219 let plan_arc = std::sync::Arc::new(plan);
1220 (energies, sigmas, matrix, plan_arc)
1221 }
1222
1223 /// Helper that exposes a way to build a `ResolutionPlan` from raw
1224 /// parts — needed because the fields are private to
1225 /// `resolution.rs`. This test-only wrapper uses the same round-
1226 /// trip trick the resolution tests use: build via the public
1227 /// `TabulatedResolution::plan` surface on a trivial grid. For the
1228 /// purpose of surrogate tests we don't care that the raw plan
1229 /// weights differ from what a real kernel would produce — what
1230 /// matters is that `compile_to_matrix` produces a valid CSR.
1231 struct SyntheticPlanBuilder {
1232 energies: Vec<f64>,
1233 starts: Vec<u32>,
1234 lo_idx: Vec<u32>,
1235 frac: Vec<f64>,
1236 weight: Vec<f64>,
1237 norm: Vec<f64>,
1238 }
1239
1240 impl SyntheticPlanBuilder {
1241 fn new(
1242 energies: Vec<f64>,
1243 starts: Vec<u32>,
1244 lo_idx: Vec<u32>,
1245 frac: Vec<f64>,
1246 weight: Vec<f64>,
1247 norm: Vec<f64>,
1248 ) -> Self {
1249 Self {
1250 energies,
1251 starts,
1252 lo_idx,
1253 frac,
1254 weight,
1255 norm,
1256 }
1257 }
1258
1259 /// Build a `ResolutionPlan` by going through the crate-public
1260 /// test-only constructor exposed on the resolution module.
1261 fn build(self) -> ResolutionPlan {
1262 crate::resolution::test_support::plan_from_raw_parts(
1263 self.energies,
1264 self.starts,
1265 self.lo_idx,
1266 self.frac,
1267 self.weight,
1268 self.norm,
1269 )
1270 }
1271 }
1272
1273 // ---------- Tests ----------
1274
1275 #[test]
1276 fn cubature_rejects_zero_isotopes() {
1277 let (_e, _s, matrix, _plan) = synthetic_setup(20, 3, 2);
1278 let err = SparseEmpiricalCubaturePlan::build(&matrix, &[], 0, &[vec![0.0]], &[0.0])
1279 .expect_err("k = 0 must reject");
1280 assert!(matches!(err, CubatureBuildError::ZeroIsotopes));
1281 }
1282
1283 #[test]
1284 fn cubature_rejects_mismatched_sigmas() {
1285 let (_e, _s, matrix, _plan) = synthetic_setup(20, 3, 2);
1286 let err = SparseEmpiricalCubaturePlan::build(
1287 &matrix,
1288 &[0.0; 7], // wrong length
1289 2,
1290 &[vec![1e-4, 1e-4]],
1291 &[1e-4, 1e-4],
1292 )
1293 .expect_err("sigma grid mismatch must reject");
1294 assert!(matches!(err, CubatureBuildError::SigmaGridMismatch { .. }));
1295 }
1296
1297 #[test]
1298 fn cubature_empty_matrix_empty_plan() {
1299 // Reuse the synthetic fabric but with n_grid = 0 — the helper
1300 // can't produce that directly (assertion), so build an empty
1301 // matrix via a zero-row plan.
1302 let plan = crate::resolution::test_support::plan_from_raw_parts(
1303 Vec::new(),
1304 vec![0_u32],
1305 Vec::new(),
1306 Vec::new(),
1307 Vec::new(),
1308 Vec::new(),
1309 );
1310 let matrix = plan.compile_to_matrix();
1311 let cub = SparseEmpiricalCubaturePlan::build(&matrix, &[], 3, &[vec![0.0; 3]], &[0.0; 3])
1312 .expect("empty matrix must build empty cubature");
1313 assert_eq!(cub.len(), 0);
1314 assert!(cub.is_empty());
1315 assert_eq!(cub.n_atoms(), 0);
1316 assert!(cub.target_energies().is_empty());
1317 }
1318
1319 #[test]
1320 fn cubature_target_energies_mirror_matrix_grid() {
1321 let (energies, sigmas, matrix, _plan) = synthetic_setup(20, 3, 2);
1322 let train_max = [1e-4_f64, 1e-4];
1323 let training = SparseEmpiricalCubaturePlan::default_training_points(&train_max);
1324 let anchor = SparseEmpiricalCubaturePlan::default_jacobian_anchor(&train_max);
1325 let cub = SparseEmpiricalCubaturePlan::build(&matrix, &sigmas, 2, &training, &anchor)
1326 .expect("build");
1327 // target_energies must byte-match the matrix's stored grid so
1328 // callers can use it as a cache key (same pattern as
1329 // ResolutionPlan / ResolutionMatrix).
1330 assert_eq!(cub.target_energies(), matrix.target_energies());
1331 assert_eq!(cub.target_energies(), energies.as_slice());
1332 }
1333
1334 #[test]
1335 fn cubature_default_training_points_shape() {
1336 let train_max = [1e-4_f64, 2e-4, 5e-5];
1337 let pts = SparseEmpiricalCubaturePlan::default_training_points(&train_max);
1338 // S = k + 2 = 5 points for k = 3.
1339 assert_eq!(pts.len(), 5);
1340 for p in &pts {
1341 assert_eq!(p.len(), 3);
1342 }
1343 // First two points are quarter / three-quarter of train_max.
1344 for (i, &m) in train_max.iter().enumerate() {
1345 assert!((pts[0][i] - 0.25 * m).abs() < 1e-15);
1346 assert!((pts[1][i] - 0.75 * m).abs() < 1e-15);
1347 }
1348 // Remaining k points are axis-aligned.
1349 for (i, &max_i) in train_max.iter().enumerate() {
1350 for (j, &value) in pts[2 + i].iter().enumerate() {
1351 let expected = if i == j { max_i } else { 0.0 };
1352 assert!((value - expected).abs() < 1e-15);
1353 }
1354 }
1355 // Anchor is the midpoint.
1356 let anchor = SparseEmpiricalCubaturePlan::default_jacobian_anchor(&train_max);
1357 for (i, &m) in train_max.iter().enumerate() {
1358 assert!((anchor[i] - 0.5 * m).abs() < 1e-15);
1359 }
1360 }
1361
1362 /// Zero-weight CSR cells retained by
1363 /// [`crate::resolution::ResolutionPlan::compile_to_matrix`] for
1364 /// NaN-safety (the `frac == +0.0` branch) MUST NOT become
1365 /// cubature atoms, even though the LP's zero objective would let
1366 /// the simplex put arbitrary mass on them. This test guards
1367 /// against regression.
1368 #[test]
1369 fn cubature_rejects_zero_weight_csr_cells_as_atoms() {
1370 // Hand-construct a 5-cell synthetic plan where every
1371 // regular-bracket entry has `frac = +0.0`, producing CSR
1372 // rows with an explicit `(lo + 1, 0.0)` zero-weight column.
1373 let energies: Vec<f64> = (0..5).map(|i| 10.0 + i as f64).collect();
1374 let mut starts: Vec<u32> = vec![0];
1375 let mut lo_idx: Vec<u32> = Vec::new();
1376 let mut frac: Vec<f64> = Vec::new();
1377 let mut weight: Vec<f64> = Vec::new();
1378 let mut norm: Vec<f64> = Vec::new();
1379 for i in 0..5 {
1380 // Row i: one regular-bracket entry at lo = i.min(3) with
1381 // frac = +0.0. This produces CSR columns {i.min(3),
1382 // i.min(3) + 1} with values {1.0, 0.0} respectively.
1383 let lo = i.min(3);
1384 lo_idx.push(lo as u32);
1385 frac.push(0.0); // +0.0, not the -0.0 sentinel
1386 weight.push(1.0);
1387 norm.push(1.0);
1388 starts.push(lo_idx.len() as u32);
1389 }
1390 let plan = crate::resolution::test_support::plan_from_raw_parts(
1391 energies, starts, lo_idx, frac, weight, norm,
1392 );
1393 let matrix = plan.compile_to_matrix();
1394
1395 // Confirm the matrix actually has zero-weight CSR cells.
1396 let total_nnz = matrix.nnz();
1397 let zero_weight_cells = matrix.values().iter().filter(|&&v| v == 0.0).count();
1398 assert!(
1399 zero_weight_cells > 0,
1400 "test fixture must include zero-weight CSR cells — got {total_nnz} nnz, {zero_weight_cells} zero",
1401 );
1402
1403 // Build a cubature. The resulting atoms must correspond ONLY
1404 // to CSR cells with non-zero weight.
1405 let sigmas = vec![0.5_f64, 1.0, 1.5, 2.0, 2.5];
1406 let train_max = [1e-4_f64];
1407 let training = SparseEmpiricalCubaturePlan::default_training_points(&train_max);
1408 let anchor = SparseEmpiricalCubaturePlan::default_jacobian_anchor(&train_max);
1409 let cub = SparseEmpiricalCubaturePlan::build(&matrix, &sigmas, 1, &training, &anchor)
1410 .expect("build must succeed on zero-weight-cell fixture");
1411
1412 // Collect the σ values retained as atoms; each must correspond
1413 // to a support column with non-zero CSR value. With k = 1,
1414 // the atom sigma is either 0.5, 1.0, 1.5, 2.0, or 2.5 —
1415 // whichever column was non-zero in the source row.
1416 for (i, window) in cub.row_starts().windows(2).enumerate() {
1417 let (s, e) = (window[0] as usize, window[1] as usize);
1418 for q in s..e {
1419 let atom_sigma = cub.atoms()[q];
1420 // The corresponding CSR cell at the nearest source
1421 // column must have non-zero weight.
1422 let row_start = matrix.row_starts()[i] as usize;
1423 let row_end = matrix.row_starts()[i + 1] as usize;
1424 let row_cols = &matrix.col_indices()[row_start..row_end];
1425 let row_vals = &matrix.values()[row_start..row_end];
1426 let source_nonzero = row_cols
1427 .iter()
1428 .zip(row_vals)
1429 .find(|&(&col, _)| (sigmas[col as usize] - atom_sigma).abs() < 1e-15)
1430 .map(|(_, &v)| v);
1431 assert!(
1432 source_nonzero.is_some() && source_nonzero.unwrap() > 0.0,
1433 "row {i} atom sigma {atom_sigma} has no non-zero source in CSR row",
1434 );
1435 }
1436 }
1437 }
1438
1439 #[test]
1440 fn cubature_build_error_display() {
1441 // Cover each error variant's Display message so a future
1442 // refactor that breaks the formatting fails loudly.
1443 let e = CubatureBuildError::ZeroIsotopes;
1444 assert!(format!("{e}").contains("at least one isotope"));
1445
1446 let e = CubatureBuildError::ZeroTrainingDensities;
1447 assert!(format!("{e}").contains("at least one training density"));
1448
1449 let e = CubatureBuildError::SigmaGridMismatch {
1450 expected: 100,
1451 actual: 50,
1452 };
1453 let s = format!("{e}");
1454 assert!(s.contains("sigmas") && s.contains("100") && s.contains("50"));
1455
1456 let e = CubatureBuildError::TrainingDensityLength {
1457 expected: 3,
1458 actual: 2,
1459 index: 7,
1460 };
1461 let s = format!("{e}");
1462 assert!(s.contains("training_densities[7]") && s.contains("length 2"));
1463
1464 let e = CubatureBuildError::AnchorLength {
1465 expected: 3,
1466 actual: 5,
1467 };
1468 let s = format!("{e}");
1469 assert!(s.contains("jacobian_anchor") && s.contains("length 5"));
1470
1471 let e = CubatureBuildError::LpInfeasible { row: 42 };
1472 assert!(format!("{e}").contains("row 42"));
1473 }
1474
1475 /// Forward equivalence at the training densities: the cubature's
1476 /// feasibility LP pins `phi_fwd @ x = phi_fwd @ w_exact`, so
1477 /// `cubature.forward(n^(s))` equals `sum_q R_{iq} exp(-n^(s)
1478 /// · σ_q)` (the exact surrogate output) at every training density
1479 /// `n^(s)` — row by row.
1480 #[test]
1481 fn cubature_forward_matches_exact_at_training_densities() {
1482 let (_e, sigmas, matrix, _plan) = synthetic_setup(40, 4, 2);
1483 let train_max = [2e-4_f64, 1.5e-4];
1484 let training = SparseEmpiricalCubaturePlan::default_training_points(&train_max);
1485 let anchor = SparseEmpiricalCubaturePlan::default_jacobian_anchor(&train_max);
1486 let cub = SparseEmpiricalCubaturePlan::build(&matrix, &sigmas, 2, &training, &anchor)
1487 .expect("build");
1488
1489 for (s, n) in training.iter().enumerate() {
1490 let t_cub = cub.forward(n);
1491 let t_exact = exact_forward(&matrix, &sigmas, 2, n);
1492 let max_err = max_hybrid_err(&t_cub, &t_exact);
1493 assert!(
1494 max_err < 1e-9,
1495 "training[{s}] n={n:?} max hybrid err = {max_err:.3e} (expected < 1e-9)",
1496 );
1497 }
1498 }
1499
1500 /// Forward accuracy at a held-out density inside the training
1501 /// convex hull: the cubature's bias should be bounded (Jensen-like
1502 /// term on the missing feature directions) but still within the
1503 /// ≤1e-3 max abs error band the design study measured on real VENUS.
1504 #[test]
1505 fn cubature_forward_held_out_bounded_error() {
1506 let (_e, sigmas, matrix, _plan) = synthetic_setup(40, 4, 2);
1507 let train_max = [2e-4_f64, 1.5e-4];
1508 let training = SparseEmpiricalCubaturePlan::default_training_points(&train_max);
1509 let anchor = SparseEmpiricalCubaturePlan::default_jacobian_anchor(&train_max);
1510 let cub = SparseEmpiricalCubaturePlan::build(&matrix, &sigmas, 2, &training, &anchor)
1511 .expect("build");
1512
1513 // Moderate density at 50 % of the box, both isotopes active.
1514 let n_test = vec![0.5 * train_max[0], 0.5 * train_max[1]];
1515 let t_cub = cub.forward(&n_test);
1516 let t_exact = exact_forward(&matrix, &sigmas, 2, &n_test);
1517 let max_abs = t_cub
1518 .iter()
1519 .zip(t_exact.iter())
1520 .map(|(a, b)| (a - b).abs())
1521 .fold(0.0_f64, f64::max);
1522 assert!(
1523 max_abs < 1e-2,
1524 "held-out max abs err = {max_abs:.3e} (expected < 1e-2)",
1525 );
1526 }
1527
1528 /// Jacobian at the anchor density: the cubature's LP pins
1529 /// `phi_grad @ x = phi_grad @ w_exact`, so the Jacobian columns at
1530 /// `n*` should match the exact Jacobian `-R[-σ_ℓ exp(-n* · σ)]` to
1531 /// LP tolerance.
1532 #[test]
1533 fn cubature_jacobian_matches_exact_at_anchor() {
1534 let (_e, sigmas, matrix, _plan) = synthetic_setup(30, 4, 3);
1535 let train_max = [2e-4_f64, 1.5e-4, 1e-4];
1536 let training = SparseEmpiricalCubaturePlan::default_training_points(&train_max);
1537 let anchor = SparseEmpiricalCubaturePlan::default_jacobian_anchor(&train_max);
1538 let cub = SparseEmpiricalCubaturePlan::build(&matrix, &sigmas, 3, &training, &anchor)
1539 .expect("build");
1540
1541 let (_t_cub, j_cub) = cub.forward_and_jacobian(&anchor);
1542 let j_exact = exact_jacobian(&matrix, &sigmas, 3, &anchor);
1543 let max_err = max_hybrid_err(&j_cub, &j_exact);
1544 // Looser than the forward-at-training-densities bound (1e-9)
1545 // because Jacobian features `σ_ℓ · exp(-n · σ)` have magnitudes
1546 // O(50) (σ in barns) vs forward features' O(1). The simplex
1547 // solver's equality residuals accumulate ~1e-8 abs error which
1548 // is LP precision, not a cubature correctness issue — the study's
1549 // Python reference implementation hits the same band.
1550 assert!(
1551 max_err < 1e-7,
1552 "Jacobian at anchor max hybrid err = {max_err:.3e} (expected < 1e-7)",
1553 );
1554 }
1555
1556 /// Row weights sum to 1 after renormalization.
1557 #[test]
1558 fn cubature_rows_are_probability_measures() {
1559 let (_e, sigmas, matrix, _plan) = synthetic_setup(30, 4, 2);
1560 let train_max = [2e-4_f64, 1.5e-4];
1561 let training = SparseEmpiricalCubaturePlan::default_training_points(&train_max);
1562 let anchor = SparseEmpiricalCubaturePlan::default_jacobian_anchor(&train_max);
1563 let cub = SparseEmpiricalCubaturePlan::build(&matrix, &sigmas, 2, &training, &anchor)
1564 .expect("build");
1565 for i in 0..cub.len() {
1566 let s = cub.row_starts()[i] as usize;
1567 let e = cub.row_starts()[i + 1] as usize;
1568 let row_sum: f64 = cub.weights()[s..e].iter().sum();
1569 assert!(
1570 (row_sum - 1.0).abs() < 1e-12,
1571 "row {i} sum = {row_sum} (expected 1.0 within 1e-12)",
1572 );
1573 }
1574 }
1575
1576 /// k = 6 curse-of-dim stress: confirm the build succeeds, atoms
1577 /// stay bounded (~S+k+1 per row), and held-out forward error stays
1578 /// modest. Mirrors the design study's k = 6 independent-Hf scenario in
1579 /// structural shape.
1580 #[test]
1581 fn cubature_k6_builds_and_evaluates() {
1582 let (_e, sigmas, matrix, _plan) = synthetic_setup(30, 4, 6);
1583 let train_max: Vec<f64> = (0..6).map(|j| 1e-4 * (1.0 + 0.2 * j as f64)).collect();
1584 // S training points = 2 midpoints + k axis-aligned points = 8.
1585 let training = SparseEmpiricalCubaturePlan::default_training_points(&train_max);
1586 let anchor = SparseEmpiricalCubaturePlan::default_jacobian_anchor(&train_max);
1587 let cub = SparseEmpiricalCubaturePlan::build(&matrix, &sigmas, 6, &training, &anchor)
1588 .expect("k=6 build");
1589
1590 // Atom counts: the Carathéodory bound is S + k + 1 = 15.
1591 // The LP may produce fewer (columns genuinely redundant). Allow
1592 // a small slack above the theoretical bound for numerical edge
1593 // cases.
1594 let max_atoms = cub
1595 .row_starts()
1596 .windows(2)
1597 .map(|w| (w[1] - w[0]) as usize)
1598 .max()
1599 .unwrap_or(0);
1600 assert!(
1601 max_atoms <= 18,
1602 "k=6 max atoms/row = {max_atoms} (expected ≤ 18 = S+k+1+slack)",
1603 );
1604
1605 // Forward at held-out density inside the box.
1606 let n_test: Vec<f64> = train_max.iter().map(|&x| 0.4 * x).collect();
1607 let t_cub = cub.forward(&n_test);
1608 let t_exact = exact_forward(&matrix, &sigmas, 6, &n_test);
1609 let max_abs = t_cub
1610 .iter()
1611 .zip(t_exact.iter())
1612 .map(|(a, b)| (a - b).abs())
1613 .fold(0.0_f64, f64::max);
1614 assert!(
1615 max_abs < 1e-2,
1616 "k=6 held-out max abs err = {max_abs:.3e} (expected < 1e-2)",
1617 );
1618 }
1619
1620 // ---------- helpers ----------
1621
1622 fn exact_forward(
1623 matrix: &crate::resolution::ResolutionMatrix,
1624 sigmas: &[f64],
1625 k: usize,
1626 n: &[f64],
1627 ) -> Vec<f64> {
1628 let n_rows = matrix.len();
1629 // T_un[ℓ] = exp(-Σ_j n_j σ_j(ℓ)).
1630 let mut t_un = vec![0.0_f64; n_rows];
1631 for (ell, t) in t_un.iter_mut().enumerate() {
1632 let mut dot = 0.0_f64;
1633 for j in 0..k {
1634 dot += n[j] * sigmas[j * n_rows + ell];
1635 }
1636 *t = (-dot).exp();
1637 }
1638 crate::resolution::apply_r(matrix, &t_un)
1639 }
1640
1641 fn exact_jacobian(
1642 matrix: &crate::resolution::ResolutionMatrix,
1643 sigmas: &[f64],
1644 k: usize,
1645 n: &[f64],
1646 ) -> Vec<f64> {
1647 let n_rows = matrix.len();
1648 let mut jac = vec![0.0_f64; n_rows * k];
1649 // ∂T_i/∂n_ℓ = -Σ_q R_{iq} σ_ℓ(q) exp(-n · σ_q).
1650 let mut t_un = vec![0.0_f64; n_rows];
1651 for (q, t) in t_un.iter_mut().enumerate() {
1652 let mut dot = 0.0_f64;
1653 for j in 0..k {
1654 dot += n[j] * sigmas[j * n_rows + q];
1655 }
1656 *t = (-dot).exp();
1657 }
1658 for ell in 0..k {
1659 let mut inner = vec![0.0_f64; n_rows];
1660 for q in 0..n_rows {
1661 inner[q] = -sigmas[ell * n_rows + q] * t_un[q];
1662 }
1663 let col = crate::resolution::apply_r(matrix, &inner);
1664 for (i, &v) in col.iter().enumerate() {
1665 jac[i * k + ell] = v;
1666 }
1667 }
1668 jac
1669 }
1670
1671 fn max_hybrid_err(a: &[f64], b: &[f64]) -> f64 {
1672 a.iter()
1673 .zip(b)
1674 .map(|(x, y)| {
1675 let denom = x.abs().max(y.abs()).max(1e-12);
1676 (x - y).abs() / denom
1677 })
1678 .fold(0.0_f64, f64::max)
1679 }
1680
1681 // ---------------------------------------------------------------
1682 // VENUS-like cubature regression
1683 // (`cubature_real_venus_k1_forward_equivalence`) moved to
1684 // `crates/nereids-physics/tests/venus_usr_surrogate.rs` — see
1685 // issues #497 and #557. It parses a synthetic SAMMY USR-format
1686 // kernel via `common::synthetic_venus_usr_tab()`.
1687 // ---------------------------------------------------------------
1688
1689 // ── Scalar (k = 1) surrogate tests ─────────────────────────────
1690
1691 /// Build a 1-isotope synthetic σ + matrix pair, shared by both
1692 /// scalar surrogate tests.
1693 fn scalar_setup(
1694 n_grid: usize,
1695 half_kernel: usize,
1696 ) -> (
1697 Vec<f64>,
1698 crate::resolution::ResolutionMatrix,
1699 std::sync::Arc<crate::resolution::ResolutionPlan>,
1700 ) {
1701 let (_e, sigmas, matrix, plan) = synthetic_setup(n_grid, half_kernel, 1);
1702 (sigmas, matrix, plan) // sigmas for k=1 is flat length n_grid
1703 }
1704
1705 #[test]
1706 fn scalar_chebyshev_matches_exact_at_multiple_densities() {
1707 let (sigmas_flat, matrix, res_plan) = scalar_setup(40, 4);
1708 let sigma = &sigmas_flat;
1709 let n_max = 2e-4_f64;
1710 let plan =
1711 ScalarChebyshevPlan::build(res_plan, sigma, n_max, 16).expect("build chebyshev plan");
1712 for n in [1e-5_f64, 1e-4, 1.6e-4] {
1713 let t_plan = plan.forward_scalar(n);
1714 let t_un: Vec<f64> = sigma.iter().map(|&s| (-n * s).exp()).collect();
1715 let t_exact = crate::resolution::apply_r(&matrix, &t_un);
1716 let max_err = max_hybrid_err(&t_plan, &t_exact);
1717 // Chebyshev accuracy depends on M; for M = 16 on a
1718 // bounded T ∈ [0, 1] signal, expect ≤ 1e-8.
1719 assert!(
1720 max_err < 1e-8,
1721 "Chebyshev vs exact at n = {n:.1e}: max hybrid err = {max_err:.3e}",
1722 );
1723 }
1724 }
1725
1726 #[test]
1727 fn scalar_chebyshev_derivative_matches_finite_difference() {
1728 let (sigmas_flat, _matrix, res_plan) = scalar_setup(30, 4);
1729 let sigma = &sigmas_flat;
1730 let n_max = 2e-4_f64;
1731 let plan = ScalarChebyshevPlan::build(res_plan, sigma, n_max, 16).expect("build");
1732 let n = 1.6e-4_f64;
1733 let h = 1e-8_f64;
1734 let (_t, dt_an) = plan.forward_and_derivative_scalar(n);
1735 let t_plus = plan.forward_scalar(n + h);
1736 let t_minus = plan.forward_scalar(n - h);
1737 for i in 0..plan.len() {
1738 let dt_fd = (t_plus[i] - t_minus[i]) / (2.0 * h);
1739 let denom = dt_an[i].abs().max(dt_fd.abs()).max(1e-12);
1740 let rel = (dt_an[i] - dt_fd).abs() / denom;
1741 assert!(
1742 rel < 1e-4,
1743 "row {i}: analytic {} vs FD {} rel = {:.3e}",
1744 dt_an[i],
1745 dt_fd,
1746 rel,
1747 );
1748 }
1749 }
1750
1751 #[test]
1752 fn scalar_chebyshev_rejects_invalid_box() {
1753 let (sigmas_flat, _matrix, res_plan) = scalar_setup(20, 3);
1754 let err =
1755 ScalarChebyshevPlan::build(std::sync::Arc::clone(&res_plan), &sigmas_flat, 0.0, 16)
1756 .expect_err("n_max = 0 must reject");
1757 assert!(matches!(
1758 err,
1759 ScalarSurrogateBuildError::InvalidChebyshevBox { .. }
1760 ));
1761 let err = ScalarChebyshevPlan::build(res_plan, &sigmas_flat, 1e-4, 1)
1762 .expect_err("M = 1 must reject");
1763 assert!(matches!(
1764 err,
1765 ScalarSurrogateBuildError::InvalidChebyshevBox { .. }
1766 ));
1767 }
1768
1769 #[test]
1770 fn scalar_chebyshev_rejects_overwide_box() {
1771 // The build-time self-check refuses
1772 // boxes where 16-node Chebyshev can't resolve the
1773 // exp(-n · σ) surface. A pathologically wide box on the
1774 // synthetic σ used by scalar_setup exceeds the 1e-6
1775 // tolerance and must be rejected.
1776 let (sigma, _matrix, res_plan) = scalar_setup(40, 4);
1777 // The scalar_setup σ has max ≈ 105 on a Gaussian peak.
1778 // At n_max = 2.0, τ_peak ≈ 210 → exp(-210) becomes
1779 // extremely small (~7e-92, still representable in f64 but
1780 // way below the dynamic range where smooth interpolation
1781 // converges). The Chebyshev polynomial can't track this
1782 // with M = 16 nodes. Must reject at build time rather
1783 // than quietly return a plan that produces huge forward
1784 // errors on dispatch.
1785 let err = ScalarChebyshevPlan::build(res_plan, &sigma, 2.0, 16)
1786 .expect_err("overwide box must reject");
1787 match err {
1788 ScalarSurrogateBuildError::InsufficientAccuracyOnBox {
1789 n_max,
1790 m,
1791 max_rel_err,
1792 tolerance,
1793 } => {
1794 assert_eq!(n_max, 2.0);
1795 assert_eq!(m, 16);
1796 assert!(
1797 max_rel_err > tolerance,
1798 "expected max_rel_err {max_rel_err:.3e} > tolerance {tolerance:.0e}",
1799 );
1800 }
1801 other => panic!("expected InsufficientAccuracyOnBox, got {other:?}"),
1802 }
1803 }
1804
1805 #[test]
1806 fn scalar_plan_rejects_sigma_size_mismatch() {
1807 let (_sigmas_flat, _matrix, res_plan) = scalar_setup(20, 3);
1808 let wrong = vec![0.0_f64; 15];
1809 let err = ScalarChebyshevPlan::build(res_plan, &wrong, 1e-4, 16)
1810 .expect_err("Chebyshev sigma mismatch must reject");
1811 assert!(matches!(
1812 err,
1813 ScalarSurrogateBuildError::SigmaGridMismatch { .. }
1814 ));
1815 }
1816
1817 // ---------------------------------------------------------------
1818 // VENUS-like scalar Chebyshev regression
1819 // (`scalar_chebyshev_real_venus_k1_regression`) moved to
1820 // `crates/nereids-physics/tests/venus_usr_surrogate.rs` — see
1821 // issues #497 and #557. It parses a synthetic SAMMY USR-format
1822 // kernel via `common::synthetic_venus_usr_tab()`.
1823 // ---------------------------------------------------------------
1824}