Repository navigation
Conversation
s was (psi - psi_first)/(psi_last - psi_first) over the equilibrium file, whose first row is not the axis and whose last row is not the Boozer boundary, so NEO-RT was evaluated on shifted flux surfaces. Use psi_tor / |psi_tor_boundary| with the boundary flux NEO-RT reports through neort_get_equilibrium and reject rmin or rsepar that fall outside 0 < s <= 1. NEO-RT's boundary flux carries bfac while the equilibrium file flux does not, so bfac /= 1 is rejected by the namelist validator. Requires neort_lib to export equilibrium_t (major_radius, minor_radius, toroidal_flux_boundary) and neort_get_equilibrium, valid after neort_init. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
NEO-RT turns M_t back into Omega_tE = vth M_t / R0 with R0 from the Boozer file (169.1 cm for AUG 33353), while KAMEL formed M_t with its own rtor (165 cm), so every rotation frequency NEO-RT saw was 2.4 % too small. Pass the major radius reported by neort_get_equilibrium instead. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Heyn (2014), text after eq. (68), and Markl (2023), eq. (27), give D12 = 3/2 D11 for the anomalous transport. With exactly 3/2 the thermodiffusive terms of the anomalous particle flux cancel, so their Jacobian entries come out exactly zero. rhs_balance takes the Jacobian sparsity pattern from the values (abs(dy) > tiny), so the count pass and the fill pass then disagree and test_equilibrium_source_stability stops with "Jacobian sparsity differs from the count pass". Keep 1.499999 and replace the "no idea why" remark by this reason. The deviation from 3/2 is 7e-7 relative and leaves a negligible non-ambipolar remainder in the anomalous flux. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e/Z_i The ion energy balance evolves 3/2 n_i T_i with n_i = n_e / Z_i: the ion heat flux, the QL heating and the E0r force balance all carry n / Z. The conversion to dT_i/dt divided by the electron density without the factor Z_i, so for Z_i /= 1 the ion temperature changed Z_i times too slowly relative to its fluxes and sources. No change for Z_i = 1. compute_dot_params_at_point becomes public for testing. test_rhs_balance checks with Z_i = 2 that an ion heating rate Q gives dT_i/dt = Q / (3/2 n_e / Z_i) and that an ion source carrying 3/2 T_i per ion leaves T_i unchanged. Both checks fail without the fix. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
params(1, :) is the electron density, but NEO-RT's plasma profile takes the density of ion species 1 with charge Z1 = Z_i. No change for Z_i = 1. test_ntv_feedback checks with Z_i = 2 that NEO-RT gets n_e / Z_i as the density of species 1. The check fails without the fix. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The equilibrium check in QL-Balance needs NEO-RT's poloidal flux derivative, which neort_lib exports since 4e21c69 (#281). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Compare the signed dpsi_pol/ds of NEO-RT's Boozer file with KAMEL's psi'_pol dr/ds on the NEO-RT flux surfaces at init. Opposite signs mean the two inputs have opposite orientation, a magnitude mismatch above 10 % means they, or the s <-> r mapping between them, do not describe the same equilibrium. Either would silently flip or scale Omega_tE and the NTV torque. KAMEL's psi'_pol = r btor / q moves into dpsi_pol_dr so the check tests exactly the denominator used for the Mach number. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The NEO-RT s grid ran from s(rmin) to s(rsepar). For AUG 33353 at 2.9 s, rsepar lies at the last row of the equilibrium file, which maps to s = 0.9973. So close to the separatrix the Boozer q rises steeply, while an input q profile need not follow it. The equilibrium check then stops the run at that point (relative dpsi_pol/ds mismatch 0.109). The new &NEORT key s_max (0 < s_max <= 1, default 1) ends the grid at min(s_max, s(rsepar)). Beyond the last grid point the NTV torque stays zero, as apply_ntv_transport already does. The default leaves the grid unchanged, and the equilibrium check keeps its 10 % tolerance on the points that are computed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fddff73 to
cdd2b51
Compare
marjohma
left a comment
There was a problem hiding this comment.
No blocking issues in the changed tokamak code: the dT_i/dt fix agrees with the ion heat fluxes, which already use n_e/Z_i. Four minor inline comments below, plus one bug that predates this PR:
Same Z_i bug is still in the stellarator RHS (outside this diff). QL-Balance/src/stellarator/rhs_balance_stell.f90 lines 312-313 and 644-645 still convert the ion energy rate to dT_i/dt by dividing by n_e without the Z_i factor, although its Q_i and fluxes_dif_lin(4) use n_e/Z_i like the tokamak path. With Z_i = 2, the stellarator T_i rises half as fast as it should. Suggested fix in both places: (-params(4)*dot_params(1) + Z_i*dot_params(4)/1.5d0)/params(1), or reuse compute_dot_params_at_point. Fine as a follow-up if you prefer to keep this PR tokamak-only.
Not reviewed: anything that depends on the new NEO-RT pin 4e21c69 (neort_get_flux_surface, poloidal_flux_derivative, whether psi_pr carries bfac).
…librium check poloidal_flux_problem let NaN through: the sign test and the mismatch test are both false for NaN, and maxval skips NaN unless every value is NaN. Non-finite derivatives on either side are now rejected first. An s outside the Boozer file does not give NaN: NEO-RT limits s to its tabulated surfaces and reports the s it used in flux_surface_t%s, so such a point silently got the edge surface's iota. The new boozer_s_range_problem compares the requested and the reported s, and initTimeEvolutionNTV stops if they differ. For AUG 33353 the grid (s = 0.0022 ... 0.99) lies inside the Boozer surfaces (s = 2.5e-4 ... 0.99975). test_neort_grid covers NaN on both sides and s beyond the first and the last Boozer surface. All four checks fail without the fix. Review: #435 (comment) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XJ76D1CKoSGf4y1nvjwisM
…m check The F6.3 format printed ****** for a relative mismatch of 100 or more, which is the case the message must diagnose: Boozer flux in T m^2 against G cm^2 gives about 1e8. Use ES10.3. test_neort_grid checks that such a mismatch is printed, and fails with F6.3. Review: #435 (comment) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XJ76D1CKoSGf4y1nvjwisM
initTimeEvolutionNTV required s(rsepar) <= 1 before it applied the namelist s_max, so s_max could not rescue a run whose rsepar maps slightly beyond the Boozer boundary: s(rsepar) = 1.002 with s_max = 0.99 stopped, although the grid would end at 0.99. The new s_grid_problem checks only the grid that is used, from s(rmin) to min(s(rsepar), s_max), against 0 < s <= 1. With the default s_max = 1 such a grid now ends at s = 1. If that lies beyond the last Boozer surface, the Boozer range check stops the run with a message naming that s. test_neort_grid covers the review example, the default s_max and the three invalid ranges. The two s(rsepar) > 1 cases fail with the old check order. Review: #435 (comment) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XJ76D1CKoSGf4y1nvjwisM
Since the dT_i/dt conversion uses n_i = n_e / Z_i (e2d6b66), convert_rates_to_equilibrium_sources returned n_e dT_i/dt + T_i dn_e/dt = Z_i (2/3) S_w,i as the ion entry, while it is documented, and written to equisource.dat, as (2/3) S_w,i. The dynamics were unaffected because compute_equilibrium_source_rates inverted it the same way. The ion entry is now divided by Z_i, and compute_equilibrium_source_rates and its linearization multiply it back. All three take Z_i as an argument. The rates and the Jacobian are unchanged, equisource.dat holds (2/3) S_w,i again, and for Z_i /= 1 the stored sources, also those in periodic checkpoints, have the same meaning as before this PR. test_equilibrium_sinks checks with Z_i = 2 that the ion entry is d(n_i T_i)/dt, that converting it back gives the original rates, and that the linearization matches a finite difference. The first check fails without the fix. Review: #435 (comment) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XJ76D1CKoSGf4y1nvjwisM
marjohma
left a comment
There was a problem hiding this comment.
Thanks for the quick follow-ups: cf8551a, 1e5c6b8, 1bfe526 and 5921f39 resolve the four comments from the first review. The new checks introduce three issues that should be fixed before merge, plus some smaller items.
Required before merge
- The Boozer s-range check aborts at the inner end with no workaround (
time_evolution_ntv.f90:135).s_tor(1) = s(rmin)must match a tabulated Boozer surface exactly, but&NEORTonly hass_maxfor the outer end. Half-grid Boozer files whose first surface lies above s(rmin) now always abort; the only way out is changingrmin, which changes the whole balance grid. - The checkpoint meaning changes without a version bump (
rhs_balance_m.f90:453).dery_equisource(4)is now divided byZ_i, but it is checkpointed asequilibrium_sourcesandperiodic_normalization_versionstays at 2. AZ_i = 2checkpoint frommainresumes with twice the ion heating source. - The equilibrium check can abort consistent equilibria near the axis (
time_evolution_ntv.f90:129).dr/dsfrom a spline ofr(s) ~ sqrt(s)is inaccurate at small s, and the 10 % max-mismatch check includes the innermost points.
Details and suggested fixes are in the inline comments.
Smaller items
- q is extrapolated at
rmin(inline at line 127). - The 10 % tolerance is hard-coded and not reviewed (inline,
neort_interface.f90:426). balance_conf.nml:88is 108 columns, over the 100-column limit (inline).- Pre-existing, outside this diff:
QL-Balance/src/stellarator/rhs_balance_stell.f90lines 312-313 and 644-645 still computedT_i/dtwithout theZ_ifactor that this PR adds incompute_dot_params_at_point. Fine as a follow-up. - PR description: the "Not in this PR" section still says the s grid isn't checked against the Boozer range and that
s_maxcomes after the range check; both have changed.
Not built or run; this is a reading-only review.
| s_evaluated(i) = surface%s | ||
| dpsi_pol_ds_neort(i) = surface%poloidal_flux_derivative | ||
| end do | ||
| if (len_trim(boozer_s_range_problem(s_tor, s_evaluated)) > 0) then |
There was a problem hiding this comment.
Required before merge. boozer_s_range_problem requires every s_tor point to match the s that NEO-RT actually evaluates, and s_tor(1) is always s(rmin). If the first Boozer surface lies above s(rmin) (common for half-grid .bc files, e.g. first surface s = 0.005 against s(rmin) ≈ 0.0036 for rmin = 3 cm, a ≈ 50 cm), NEO-RT clamps and the run aborts with "does not cover s = ...". Before this PR NEO-RT clamped to the edge surface and the run continued.
There is no setting that moves the inner end; s_max only cuts the outer end, so the only workaround is changing rmin, which changes the whole balance grid.
Suggested fix: add an &NEORT s_min (mirroring s_max), or raise the inner end of the grid to the first Boozer surface, or at least document that rmin must map inside the Boozer range.
|
|
||
| sources(1:2) = rates(1:2) | ||
| sources(3:4) = params_point(1) * rates(3:4) + params_point(3:4) * rates(1) | ||
| sources(4) = sources(4) / Z_i |
There was a problem hiding this comment.
Required before merge. This changes what dery_equisource(4) means (now divided by Z_i, multiplied back in compute_equilibrium_source_rates). The array is checkpointed as equilibrium_sources (periodic_checkpoint_m.f90:227, read at :388), but periodic_normalization_version in periodic_amplitude_state_m.f90 stays at 2, so old checkpoints are accepted unchanged.
Failure: a Z_i = 2 run checkpointed on main and resumed with this PR reads the old d(n_e T_i)/dt as d(n_i T_i)/dt, multiplies by Z_i, and doubles the ion heating source; T_i drifts away from the equilibrium it should hold.
Suggested fix: bump periodic_normalization_version, or rescale or refuse old checkpoints when Z_i /= 1.
| allocate (dpsi_pol_ds_neort(s_size), dpsi_pol_ds_kamel(s_size), s_evaluated(s_size)) | ||
| q_of_r_coeffs = spline_coeff(rc, qsaf) | ||
| q_splined = spline_val(q_of_r_coeffs, r) | ||
| dpsi_pol_ds_kamel = dpsi_pol_dr(r, q_splined(:, 1)) * r_splined(:, 2) |
There was a problem hiding this comment.
Required before merge. dpsi_pol_ds_kamel uses r_splined(:, 2) = dr/ds from a cubic spline of r(s). Near the axis r ~ sqrt(s), so dr/ds ~ 1/(2 sqrt(s)) is steep and poorly represented by the spline, especially when the equilibrium file is coarse near the axis and s_min falls in its first interval. The product r * dr/ds is smooth, but its spline estimate can be 10-20 % off at s_tor(1), and poloidal_flux_problem takes the max mismatch over all points, so it can abort a consistent equilibrium.
Suggested fix: compute r dr/ds as 0.5 d(r^2)/ds (spline r^2 against s, or use psi_tor' directly), or exclude the innermost points from the comparison.
| ! sign_theta * iota * psi_pr: the denominator NEO-RT itself uses. | ||
| allocate (q_of_r_coeffs(size(rc) - 1, 5), q_splined(s_size, 3)) | ||
| allocate (dpsi_pol_ds_neort(s_size), dpsi_pol_ds_kamel(s_size), s_evaluated(s_size)) | ||
| q_of_r_coeffs = spline_coeff(rc, qsaf) |
There was a problem hiding this comment.
Minor: q_of_r_coeffs is built on the cell centres rc, but r(1) = rmin < rc(1), so q_splined(1, 1) is extrapolated. With a steep q gradient near rmin this adds to the mismatch at s_tor(1) in the 10 % check. Suggest splining qsafb on rb (which includes rmin), or clamping r to [rc(1), rc(end)].
| real(dp), dimension(:), intent(in) :: dpsi_pol_ds_neort, dpsi_pol_ds_kamel | ||
| character(len=256) :: problem | ||
|
|
||
| real(dp), parameter :: tolerance = 0.1_dp |
There was a problem hiding this comment.
Minor: this 10 % tolerance aborts runs but has no stated reference; the PR's own AUG case fails at 10.9 %. AGENTS.md asks that comparison tolerances not be guessed. Suggest making it configurable in &NEORT, or documenting where the value comes from.
| boozer_file = "" | ||
| boozer_pert_file = "" ! required, e.g. a GPEC perturbation .bc file | ||
| amount_of_s = 100 | ||
| s_max = 1.0 ! outer end of the s grid (0 < s_max <= 1), used if below s(rsepar); NTV torque is zero beyond |
There was a problem hiding this comment.
Nit: this line is 108 columns, over the 100-column limit in AGENTS.md. Suggest moving the comment to its own line.
marjohma
left a comment
There was a problem hiding this comment.
Requesting changes for the three items marked Required before merge in the previous review (#435 (review)):
- Boozer s-range check aborts at the inner end with no way to move it (
time_evolution_ntv.f90:135). equilibrium_sourceschanges meaning forZ_i /= 1without bumpingperiodic_normalization_version(rhs_balance_m.f90:453).- Near-axis
dr/dserror can make the 10 % equilibrium check abort consistent equilibria (time_evolution_ntv.f90:129).
The smaller items in that review are not blocking.
The Boozer s-range check stops a run whose s grid leaves the Boozer file. At the outer end s_max moves the grid inward, but the inner end was always s(rmin), so a Boozer file whose first surface lies above s(rmin) could only be used by changing rmin and with it the whole balance grid. &NEORT s_min (0 <= s_min < s_max, default 0) starts the grid at max(s(rmin), s_min). Inside the first NEO-RT surface the NTV torque is zero, as it already is beyond the last one. Without a cut the grid starts at rmin itself, so the torque at rb(1) = rmin is kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XJ76D1CKoSGf4y1nvjwisM
…tart 5921f39 stores the ion entry of the equilibrium sources as d(n_i T_i)/dt with n_i = n_e/Z_i. Checkpoints written before hold d(n_e T_i)/dt from an ion energy balance without n_i = n_e/Z_i, and a restart would read it in the new meaning and multiply it by Z_i: for Z_i = 2 twice the ion heating source. The checkpoint now records equilibrium_sources_version = 2. A checkpoint without it is version 1, and apply_periodic_restart refuses it unless Z_i = 1, where both versions agree. periodic_normalization_version is left alone, since the amplitude contract has not changed and Z_i = 1 checkpoints stay usable. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XJ76D1CKoSGf4y1nvjwisM
…of r^2 The equilibrium check compared NEO-RT's dpsi_pol/ds with KAMEL's psi'_pol dr/ds, where dr/ds came from a cubic spline of r(s). Near the axis r ~ sqrt(s), so dr/ds diverges and the spline misses it when the equilibrium file is coarse there; the check takes the largest mismatch over all points and could then stop a consistent equilibrium. r^2 = 2 psi_tor / B_ref is linear in s, so r dr/ds is now 0.5 d(r^2)/ds from a spline of r^2 against s (r_dr_ds, dpsi_pol_ds). For the AUG 33353 file (10000 rows) both forms agree to 5e-10. Thinned to 25 rows, the file gives a mismatch of 30 % at s(rmin = 3 cm) with the old form and at most 0.7 % with the new one (estimated with scipy cubic splines). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XJ76D1CKoSGf4y1nvjwisM
…mes from The tolerance is a sanity bound between consistent files and gross errors. Record the AUG 33353 values on either side of it: about 0.5 % up to r = 61 cm, 11 % at the last row of the equilibrium file (cut off with NEORT s_max), 1e8 for a flux in T m^2 against G cm^2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XJ76D1CKoSGf4y1nvjwisM
…in 100 columns Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XJ76D1CKoSGf4y1nvjwisM
This PR fixes how QL-Balance passes flux-surface labels, rotation and ion densities to NEO-RT for the NTV torque, adds an equilibrium consistency check at init and an optional cut of the NEO-RT s grid.
Fixes
neort_get_equilibrium.rminorrseparoutside 0 < s <= 1 is rejected, and so isbfac /= 1, because NEO-RT's boundary flux carriesbfacand the equilibrium file does not.rtor(165 cm). Every rotation frequency NEO-RT saw was 2.4 % too small. M_t now uses NEO-RT's R0.rhs_balancethen differs between the count and fill passes. The value itself is unchanged.New behaviour
&NEORT s_max(0 < s_max <= 1, default 1): ends the NEO-RT grid at min(s_max, s(rsepar)). For AUG 33353 at 2.9 s, withrseparat the last row of the equilibrium file (s = 0.9973), the Boozer and input q profiles diverge near the edge and the check aborts (mismatch 0.109). Beyond the last grid point the NTV torque stays zero, as before. The default leaves the grid unchanged.neort_get_flux_surface..gitignore: anchored/.worktrees/, addeduv.lock.Verification
test_ion_temperature_rate_with_charge(Z_i = 2, intest_rhs_balance) and a Z_i = 2 case intest_ntv_feedback. Each fails with its fix reverted. Extended:test_neort_config(s_max validation) andtest_neort_grid(s normalization, s_max cut).ctestpasses 197/197 at the tip and at every fix commit.s_max = 0.99. The total NTV torque at the first step is within 1.4 % of an independent reference.Not in this PR
neort_libto export the constants.s_maxcut is applied after the 0 < s <= 1 check.🤖 Generated with Claude Code
https://claude.ai/code/session_01XJ76D1CKoSGf4y1nvjwisM