Skip to content

Standalone KIM diagnostics: D_ql,e22 and integrated parallel current#137

Open
marjohma wants to merge 5 commits into
mainfrom
feature/kim-dqle22-output
Open

Standalone KIM diagnostics: D_ql,e22 and integrated parallel current#137
marjohma wants to merge 5 commits into
mainfrom
feature/kim-dqle22-output

Conversation

@marjohma

Copy link
Copy Markdown
Member

Summary

Adds standalone diagnostics to KIM so it can drive density/temperature
parameter scans of the quasilinear bifurcation criterion without
QL-Balance
, sidestepping the ParameterScan profile-propagation defect
in #136.

Five focused commits:

  1. integrate_Ipar — integrated resonant parallel current (complex
    trapezoid of 2π r j_∥), in a new KIM/src/diagnostics/ module.
  2. calc_dqle22 — quasilinear electron heat diffusion coefficient
    D_ql,e22, ported term-for-term from QL-Balance's
    diff_coeffs.f90 (2,2) component, calling the shared getIfunc
    (no duplicated susceptibility code).
  3. Output wiring — HDF5 /diagnostics/{dqle22,Ipar_re,Ipar_im, Ipar_e_re,Ipar_e_im,br_abs_res} and a flat kim_diagnostics.dat,
    gated by a new &KIM_IO flag write_diagnostics_dat (default
    .false.), computed once after the EM solve.
  4. Guard against a missing resonant surface (skip output, no
    error stop).
  5. Evaluate fields/plasma at r_res by interpolation (not the nearest
    node) and emit br_abs_res — needed for narrow resonant layers.

Production-safety: with both diagnostics flags off,
compute_and_write_diagnostics returns before touching any global
state, so existing KIM runs are byte-identical. The namelist flag is
backward-compatible (old namelists read fine).

These diagnostics were used to run a full KIM-vs-KiLCA nT scan of the
Markl 2023 (NF 63 126007) bifurcation criterion; KIM standalone
reproduces the QL-Balance+KIM pipeline at base profiles to ~2%.

Test Plan

  • New test_kim_diagnostics passes (analytic trapezoid on
    non-equidistant grids; complex interpolation exact-on-cubic;
    D_ql,e22 vs the Markl 2023 Eq. 31 closed form in two collisionality
    regimes; two EM-solve integration tests incl. the no-resonance skip).
  • Full ctest green except the pre-existing test_rhs_balance
    (QL-Balance, fails identically on main @ 225a877 — unrelated).
  • Whole-branch review passed (isolation verified by control-flow;
    D22 port verified term-by-term).

Related: #136 (the QL-Balance ParameterScan+KIM defect this work
routes around).

🤖 Generated with Claude Code

marjohma and others added 5 commits June 12, 2026 10:42
Add kim_diagnostics_m with pure integrate_Ipar: complex trapezoid
of 2*pi*r*jpar over the passed (layer-restricted) radial grid.
First of the KIM standalone diagnostics; analytic unit tests on a
non-equidistant grid cover constant and linear jpar profiles.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Port the electron (2,2) Onsager component from QL-Balance
calc_transport_coeffs_ornuhl (diff_coeffs.f90) into a unit-testable
KIM module kim_qldiff_m::calc_dqle22 taking plain scalars. Reuses the
getIfunc/W2_arr susceptibility functions already compiled into KIM_lib
via the KIM_fokkerplanck source group.

Tests verify the x1 -> 0 resonant-surface limit against the closed
form of Markl et al 2023 NF 63 126007 Eq. 31 for omE/nue = 0.5 and
2.0 (rel err ~1e-12). Also harden assert_close against zero real or
imaginary parts of the expected value.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Add compute_and_write_diagnostics to kim_diagnostics_m: after the
electromagnetic solve it evaluates D_ql,e22 at the grid point nearest
the resonant surface (local vTe, nue, om_E, B0, k_par interpolated
with the standard binsrc/plag_coeff stencil) and integrates the total
and electron-only parallel currents from EBdat.

Outputs via new IO_collection_m::write_kim_diagnostics:
- HDF5 datasets /diagnostics/{dqle22,Ipar_re,Ipar_im,Ipar_e_re,
  Ipar_e_im} when hdf5_output is on
- flat file kim_diagnostics.dat (es23.15, # header) when the new
  KIM_IO flag write_diagnostics_dat is on, regardless of hdf5_output;
  this is the scan driver interface

Equidistant grids never call recnsplit, so the diagnostics routine
triggers the lazy resonance detection itself via the prop guard.

Integration test runs the EM solve on a coarse in-memory plasma
(QL-Balance adapter injection path) and asserts the flat file exists,
parses, has five finite values and dqle22 > 0 (1.4 s runtime).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
When |m/n| lies outside the q range, prepare_resonances leaves
r_res = 0 and compute_and_write_diagnostics evaluated everything at
the innermost grid point, writing plausible-looking but meaningless
values that the scan driver machine-reads. Now an error is logged to
stderr and no output is written; the missing kim_diagnostics.dat is
the driver's failure signal. Covered by a new integration test that
runs the EM solve with m/n = -4/1 against test profiles with
q in [1.5, 2.5].

Also:
- IO_collection: open the existing diagnostics/ HDF5 group instead of
  leaving h5grpid undefined when the group already exists (no cheap
  test; HDF5 re-entry path).
- kim_diagnostics: drop the dead exact-zero kpar fallback; the A2 unit
  test shows the formula is well-behaved at x1 ~ 1e-6 and the
  collisional getIfunc branch handles x1 = 0.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Gate V1 found the dqle22/|Br|^2 resonant layer can be narrower than
the grid spacing (FWHM 0.034 cm vs a 0.0133 cm nearest-node offset,
a 33% error for m=7), so nearest-node evaluation is not good enough
for production scans. Interpolate Es, Br (new interp_local_complex,
4-point Lagrange) and the plasma parameters to r_res itself; kpar is
the interpolated near-zero value, which calc_dqle22 handles since the
collisional response is regular at kpar -> 0.

Also write |Br(r_res)| as a 6th output (HDF5 /diagnostics/br_abs_res
and a 6th flat-file column) so the scan driver gets it without
digging through field profiles. Flat-file column order: dqle22,
Ipar_re, Ipar_im, Ipar_e_re, Ipar_e_im, br_abs_res.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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