Skip to content

[math-06] Verify equilibrium and plasma-background formulas#218

Draft
marjohma wants to merge 3 commits into
fix/kim-fp-197-poisson-oraclefrom
fix/kim-fp-198-equilibrium-oracle
Draft

[math-06] Verify equilibrium and plasma-background formulas#218
marjohma wants to merge 3 commits into
fix/kim-fp-197-poisson-oraclefrom
fix/kim-fp-198-equilibrium-oracle

Conversation

@marjohma

Copy link
Copy Markdown
Member

Summary

  • derive multispecies equilibrium, collision, thermodynamic-force, and geometry formulas
  • correct shared constants and background formula inconsistencies across KIM and KiLCA
  • add deterministic KIM and KiLCA oracle consumers

Why

Both solvers use source-checked equilibrium quantities with consistent units and species sums.

Validation

Validated cumulatively at the stack head:

  • cmake --build build -j2
  • ctest --test-dir build -E 'test_rhs_balance|test_periodic_convergence|test_periodic_vs_global' --output-on-failure — 35/35 passed

Known red gates are intentionally excluded from that command: the pre-existing test_rhs_balance baseline failure, the periodization-deformation acceptance gate, and the unresolved periodic-vs-global 5% agreement gate.

Stack

Base: fix/kim-fp-188-matched-bvp
Head: fix/kim-fp-198-equilibrium-oracle

Closes #198

@marjohma

Copy link
Copy Markdown
Member Author

Review (standards + spec vs #198) — keeping this in draft. Two distinct things are bundled here, and they deserve different fates.

1. Undisclosed production-numerics changes (the main reason for the hold). Four real fixes hide inside a fix(background): verify… commit:

  • species_mod.f90 ion-ion Coulomb logarithm rewritten: the old Lii lacked the NRL sqrt and had misplaced parentheses — at n=2e13 cm⁻³, T=800 eV this shifts Lii ~5.7 → ~17.4, i.e. ~ν_ii×3 in every run, single-ion included; the ion-partner loop also went upper-triangle → full (multi-ion change).
  • constants_mod.f90:8: sol literal lacked _dp and was stored single-precision (29979246592 vs 29979245800) — every c-dependent result shifts ~2.6e-8. Correct fix, disclosed nowhere.
  • compute_rg_cell_centers: derived quantities now recomputed from averaged primitives (was: averaged derived values), plus the x2_cc omega_c(j)omega_c_cc(j) index fix.
  • Ion-density fallback n_e·Z_s/ΣZn_e/ΣZ (only the new form satisfies ΣZ·n=n_e), with new hard error stops.

These all look physically right — and the new KIM oracle test genuinely exercises production species_m code at 5e-12 and would fail against the old formulas, which is exactly what an oracle should do. But: the golden-record CI (rtol 1e-8) will flag the Lii and sol changes, so a baseline re-bless is needed, and result-changing physics fixes shouldn't ride silently in a verification commit. Suggest splitting them into an explicit fix(KIM): commit/PR with the numerics impact stated, then rebasing the verification on top. (The KiLCA calc_back.cpp extraction, in contrast, was verified behavior-preserving term-by-term — that part is clean plumbing.)

2. Verification-layer gaps (the recurring family pattern).

  • "Prove sum_s Z_s n_s = n_e for arbitrary mixtures": no symbolic proof — the WL check sets nd = ne − 6·nCarbon and then verifies nd + 6·nCarbon == ne (tautology); the Fortran side is fixed D/C examples plus a runtime residual.
  • Production calculate_equil and KiLCA calculate_equilibrium/find_f0_parameters are never executed against the oracle — the geometry/current/force-balance/E_r rows (kinds 1/5/6) are only self-consistency-checked in the Fortran test, and the KiLCA test skips kind≠4. FORMULA_INDEX's "independent [math-06] Verify equilibrium and plasma-background formulas #198 fixture" for KIM-03/KILCA-05/06 overstates this; the WL formulas there are transcriptions.
  • The issue's own success-criteria regex test_(periodic_background|profile_input|plasma) does not match the new test_kilca_plasma_background_oracle — the KiLCA gate never runs under the spec's command.
  • The four negative fixtures mutate the WL script's own constants/toys, not production formulas; several checks ("zero flow removes rotation term" compares a call to an identical call) cannot fail unmutated.
  • Untested production paths: rescale_density=.true. branch, A1_cc/A2_cc.
  • Hardcoded counts again (17 rows, kind counts, 29 sites) — mitigated by the row-wise 1e-30 regeneration cross-check.

Genuine wins worth keeping as-is: derivatives-after-periodization is properly verified against production (test_periodic_background_build.f90:292-315), both oracle CTests are registered and read the committed .dat, provenance is good, and the q-inversion/Lorentz-force symbolic checks are real.

Minor: two >100-col lines in species_mod.f90 and two in the new .wl.

marjohma added a commit that referenced this pull request Jul 14, 2026
Review follow-ups for #187:

- Oracle assertions now compare per-row RELATIVE (rows committed as
  exactly zero must stay exactly zero). The old max(1,|expected|)
  scaling made every rho-B row (magnitudes 1e-14..1e-22) vacuous: a
  stubbed-zero fp_rho_B_harmonic passed the oracle subtest. With the
  fix, a mutation battery of sign, 4*pi, Fourier phase, Bessel order,
  and ks^2 each fails the ctest; the pristine kernel's maximum scaled
  error is 6.4e-14 against the predeclared 1e-12 tolerance.
- The enforced oracle immediately caught a real defect: the speed of
  light in constants_mod lacked _dp and was rounded through single
  precision (29979246592 vs 29979245800, 2.6e-8 relative), exactly the
  rho-B mismatch observed. Fixed here (same correction as #218 carries);
  every c-dependent result shifts by ~2.6e-8, so the golden baseline
  needs a re-bless.
- fp_rho_phi_harmonic/fp_rho_B_harmonic take rho_L as an explicit
  argument instead of recomputing abs(vT/omega_c); hatG_* pass the
  stored species rho_L, so ion_flr_scale_factor acts on the
  off-diagonal path exactly as on the diagonal one (regression test
  added).
- fp_ks_model is now configurable: KIM_CONFIG key fp_ks_model_name
  ('kperp_full' default, 'kr_only' legacy approximation), mapped in
  kim_read_config with a hard error on unknown names; documented in
  both shipped nmls and KIM/README.md including the default-behavior
  change relative to the old kern_include_ks2=.false.
- New fp_mphi_truncation_check validation gate (fails closed on
  unassessable sums) with a slow-decay negative fixture; infinite
  on-axis ks negative fixture; kernel tests restore the production
  default model; FORMULA_INDEX rho-B/KIM-05/KIM-06 claims reworded to
  what is actually verified (the rho-B WL entry remains a transcription;
  an independent K^{rho B} derivation pass is still open).
Base automatically changed from fix/kim-fp-188-matched-bvp to fix/kim-fp-197-poisson-oracle July 14, 2026 12:45
## Summary

- report reciprocal condition estimates and scaled backward errors for
dense solves
- reject singular, non-finite, ill-conditioned, and inaccurate systems
with explicit statuses
- turn quadrature, Fourier, harmonic, deformation, and cross-method
tolerances into nonzero test exits

## Why

Numerical trust failures are visible and cannot be mistaken for accepted
physics.

## Validation

Validated cumulatively at the stack head:

- `cmake --build build -j2`
- `ctest --test-dir build -E
'test_rhs_balance|test_periodic_convergence|test_periodic_vs_global'
--output-on-failure` — 35/35 passed

Known red gates are intentionally excluded from that command: the
pre-existing `test_rhs_balance` baseline failure, the
periodization-deformation acceptance gate, and the unresolved
periodic-vs-global 5% agreement gate.

## Stack

Base: `fix/kim-fp-198-equilibrium-oracle`
Head: `fix/kim-fp-189-numerical-gates`

Closes #189
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant