From 002db486e6fece454c520c569599d55844595093 Mon Sep 17 00:00:00 2001 From: d-burg Date: Thu, 5 Nov 2026 23:12:10 -0600 Subject: [PATCH 1/4] Utilities - BUGFIX! - Compute the trapped fraction from the exact pitch-angle integral Co-Authored-By: Claude Opus 5.5 --- src/ForceFreeStates/Surfaces/ResistEval.jl | 22 +++++--- src/Utilities/NeoclassicalResistivity.jl | 65 +++++++++++++++++----- src/Utilities/Utilities.jl | 2 +- test/runtests_slayer_params.jl | 19 +++++++ 4 files changed, 85 insertions(+), 23 deletions(-) diff --git a/src/ForceFreeStates/Surfaces/ResistEval.jl b/src/ForceFreeStates/Surfaces/ResistEval.jl index 9e3ae70790..ab646a4bde 100644 --- a/src/ForceFreeStates/Surfaces/ResistEval.jl +++ b/src/ForceFreeStates/Surfaces/ResistEval.jl @@ -3,7 +3,8 @@ # # θ-integrands, each weighted by `jac / v1` before integration (1-6 follow Fortran `resist_eval`): # 1: B² / |∇ψ|² 2: 1 / |∇ψ|² 3: 1 / B² 4: 1 / (B² · |∇ψ|²) 5: B² 6: |∇ψ|² / B² -# 7: B (for the Lin-Liu & Miller 1995 trapped fraction) 8: |∇ψ|² (toroidal critical-Δ, ⟨|∇V|²⟩ = v1²⟨|∇ψ_N|²⟩) +# 7: B 8: |∇ψ|² (toroidal critical-Δ, ⟨|∇V|²⟩ = v1²⟨|∇ψ_N|²⟩) +# 9…8+nλ: √(1 − λₖ h), h = B/B_max, at the pitch-angle nodes λₖ of the exact trapped-fraction integral """ ResistGeometry @@ -22,7 +23,7 @@ averages (⟨1⟩ = 1), and `‖∇ψ_N‖` is the gradient of the normalized po | `avg_bsq_over_dpsisq` | ⟨B²/‖∇ψ_N‖²⟩ — needed for τ_R | | `avg_bsq` | ⟨B²⟩ — needed for τ_R | | `avg_dpsisq` | ⟨‖∇ψ_N‖²⟩ — needed for the toroidal critical-Δ | -| `f_trap` | Lin-Liu & Miller 1995 trapped-particle fraction | +| `f_trap` | trapped-particle fraction, exact pitch-angle integral | | `R_major` | major radius (R_max + R_min)/2 over the surface, in m | | `eps_local` | (R_max − R_min)/2 / R_major — local inverse aspect ratio | | `p_local` | Plasma pressure at this surface [Pa] | @@ -86,11 +87,12 @@ function resist_geometry(equil::Equilibrium.PlasmaEquilibrium, v2 = profiles.dVdpsi_deriv(psi_f) q = profiles.q_spline(psi_f) - # θ-integrands (see file header) plus running extrema of B and R for f_trap and ε. + # θ-integrands (see file header) plus running B_max (f_trap) and R extrema (ε). ntheta = length(equil.rzphi_ys) - ff = zeros(Float64, ntheta, 8) + lambda, lambda_weight = Utilities.NeoclassicalResistivity.trapped_fraction_nodes() + ff = zeros(Float64, ntheta, 8 + length(lambda)) + weight = zeros(Float64, ntheta) B_max = -Inf - B_min = Inf R_max = -Inf R_min = Inf for itheta in 1:ntheta @@ -115,7 +117,6 @@ function resist_geometry(equil::Equilibrium.PlasmaEquilibrium, B_here = sqrt(bsq) B_max = max(B_max, B_here) - B_min = min(B_min, B_here) R_max = max(R_max, r) R_min = min(R_min, r) @@ -127,8 +128,13 @@ function resist_geometry(equil::Equilibrium.PlasmaEquilibrium, ff[itheta, 6] = dpsisq / bsq ff[itheta, 7] = B_here ff[itheta, 8] = dpsisq - @views ff[itheta, :] .*= jac / v1 + weight[itheta] = jac / v1 end + # The pitch-angle columns need B_max over the whole surface, so they are filled after the θ loop. + for itheta in 1:ntheta, (k, lam) in enumerate(lambda) + ff[itheta, 8+k] = sqrt(max(1.0 - lam * ff[itheta, 7] / B_max, 0.0)) + end + ff .*= weight # Snap the repeated endpoint exactly equal to the start @views ff[end, :] .= ff[1, :] @@ -138,7 +144,7 @@ function resist_geometry(equil::Equilibrium.PlasmaEquilibrium, avg_B = avg[7] R_major = 0.5 * (R_max + R_min) eps_local = R_major > 0 ? 0.5 * (R_max - R_min) / R_major : 0.0 - f_trap = Utilities.NeoclassicalResistivity.trapped_fraction(avg_B, avg[5], B_min, B_max) + f_trap = Utilities.NeoclassicalResistivity.trapped_fraction(avg[5] / B_max^2, avg[9:end], lambda_weight) # GGJ coefficients (Fortran RDCON resist_eval) E_coef = p1 * v1 / (q1 * chi1^2)^2 * avg[1] * diff --git a/src/Utilities/NeoclassicalResistivity.jl b/src/Utilities/NeoclassicalResistivity.jl index a4b15dde18..32e528fe25 100644 --- a/src/Utilities/NeoclassicalResistivity.jl +++ b/src/Utilities/NeoclassicalResistivity.jl @@ -27,7 +27,9 @@ physics when the same `NeoResistivityModel` is selected. | `eta_spitzer` | Sauter 18a Spitzer resistivity [Ω·m] | | `tau_ee_spitzer_harm` | electron-electron collision time τ_ee [s] | | `eta_spitzer_harm` | Fitzpatrick/TJ Spitzer-Härm resistivity 1/σ_∥ [Ω·m] | -| `trapped_fraction` | Lin-Liu & Miller 1995 f_t from ⟨B⟩, ⟨B²⟩, B_min, B_max | +| `trapped_fraction` | exact pitch-angle integral f_t from ⟨B²⟩/B_max² and ⟨√(1 − λB/B_max)⟩ | +| `trapped_fraction_nodes` | Gauss-Legendre pitch-angle nodes λ and weights for `trapped_fraction` | +| `trapped_fraction_lin_liu_miller` | Lin-Liu & Miller 1995 f_t (OMFIT `fluxSurfaces` form) | | `trapped_fraction_eps` | simple ε-only f_t fallback | | `nu_star_e` | Sauter 18b electron collisionality | | `eta_neoclassical` | dispatched: Spitzer(-Härm) or F_33 · Spitzer | @@ -35,10 +37,11 @@ physics when the same `NeoResistivityModel` is selected. module NeoclassicalResistivity using ..PhysicalConstants: EPS_0, M_E, E_CHG +using FastGaussQuadrature: gausslegendre export NeoResistivityModel, SpitzerModel, SpitzerHarmModel, SauterNeoModel, RedlNeoModel export coulomb_log_e, eta_spitzer, tau_ee_spitzer_harm, eta_spitzer_harm -export trapped_fraction, trapped_fraction_eps +export trapped_fraction, trapped_fraction_nodes, trapped_fraction_lin_liu_miller, trapped_fraction_eps export nu_star_e, eta_neoclassical """ @@ -176,24 +179,58 @@ end # Trapped fraction """ - trapped_fraction(avg_B, avg_Bsq, B_min, B_max) -> Float64 + trapped_fraction_nodes(n=16) -> (lambda, weight) -Lin-Liu & Miller 1995, Phys. Plasmas **2**, 1666, Eq. (6): +Pitch-angle nodes for [`trapped_fraction`](@ref): `n`-point Gauss-Legendre in x ∈ [0, 1] with λ = 1 − x², so that +∫₀¹ λ g(λ) dλ ≈ Σₖ weight[k] g(lambda[k]). The x map removes the √ edge behavior at λ → 1; 16 nodes give f_t to ~1e-9. +""" +function trapped_fraction_nodes(n::Integer=16) + x, w = gausslegendre(n) + x = 0.5 .* (x .+ 1.0) + w = 0.5 .* w + return 1.0 .- x .^ 2, w .* 2.0 .* x .* (1.0 .- x .^ 2) +end + +""" + trapped_fraction(avg_h2, avg_sqrt, weight) -> Float64 + +Exact trapped-particle fraction (Hirshman & Sigmar 1981, Nucl. Fusion **21**, 1079, Eq. 4.54): + +``` +f_t = 1 − ¾ ⟨h²⟩ ∫₀¹ λ dλ / ⟨√(1 − λh)⟩, h = B/B_max +``` + +`avg_h2` = ⟨B²⟩/B_max², `avg_sqrt[k]` = ⟨√(1 − λₖ h)⟩ at the nodes of [`trapped_fraction_nodes`](@ref), whose weights +are `weight`. +""" +function trapped_fraction(avg_h2::Real, avg_sqrt::AbstractVector{<:Real}, weight::AbstractVector{<:Real}) + length(avg_sqrt) == length(weight) || throw(ArgumentError("trapped_fraction: avg_sqrt and weight lengths differ")) + all(>(0), avg_sqrt) || throw(ArgumentError("trapped_fraction: ⟨√(1 − λh)⟩ must be > 0")) + return clamp(1.0 - 0.75 * avg_h2 * sum(weight ./ avg_sqrt), 0.0, 1.0) +end + +""" + trapped_fraction_lin_liu_miller(avg_B, avg_Bsq, B_max, avg_hf) -> Float64 + +Lin-Liu & Miller 1995, Phys. Plasmas **2**, 1666: the weighted mean (Eqs. 18-19) of the upper (Eq. 4) and +lower (Eq. 7) bounds, ``` -f_t = 1 − ⟨B⟩² / ⟨B²⟩ · (1 − √(1 − h) · (1 + h/2)), h = B_min / B_max +f_tu = 1 − ⟨h²⟩/h̄² · (1 − √(1 − h̄) · (1 + h̄/2)), h̄ = ⟨B⟩/B_max, ⟨h²⟩ = ⟨B²⟩/B_max² +f_tl = 1 − ⟨h²⟩ · ⟨(1 − √(1 − h) · (1 + h/2)) / h²⟩, h = B/B_max +f_t = 0.75 f_tu + 0.25 f_tl ``` -Arguments are flux-surface averages and extrema of |B| on the surface. +`avg_hf` is ⟨(1 − √(1 − h)(1 + h/2)) / h²⟩. Same form as OMFIT `fluxSurfaces`; within ~1e-3 of [`trapped_fraction`](@ref). """ -function trapped_fraction(avg_B::Real, avg_Bsq::Real, - B_min::Real, B_max::Real) - B_max > 0 || throw(ArgumentError("trapped_fraction: B_max must be > 0")) - avg_Bsq > 0 || throw(ArgumentError("trapped_fraction: avg_Bsq must be > 0")) - h = clamp(B_min / B_max, 0.0, 1.0) - factor = 1.0 - sqrt(1.0 - h) * (1.0 + 0.5 * h) - ft = 1.0 - (avg_B^2 / avg_Bsq) * factor - return clamp(ft, 0.0, 1.0) +function trapped_fraction_lin_liu_miller(avg_B::Real, avg_Bsq::Real, B_max::Real, avg_hf::Real) + B_max > 0 || throw(ArgumentError("trapped_fraction_lin_liu_miller: B_max must be > 0")) + avg_B > 0 || throw(ArgumentError("trapped_fraction_lin_liu_miller: avg_B must be > 0")) + h = clamp(avg_B / B_max, 0.0, 1.0) + h2 = avg_Bsq / B_max^2 + f_tu = 1.0 - h2 / h^2 * (1.0 - sqrt(1.0 - h) * (1.0 + 0.5 * h)) + f_tl = 1.0 - h2 * avg_hf + return clamp(0.75 * f_tu + 0.25 * f_tl, 0.0, 1.0) end """ diff --git a/src/Utilities/Utilities.jl b/src/Utilities/Utilities.jl index 466783d3fb..6c136a099c 100644 --- a/src/Utilities/Utilities.jl +++ b/src/Utilities/Utilities.jl @@ -53,7 +53,7 @@ using .NeoclassicalResistivity export NeoclassicalResistivity export NeoResistivityModel, SpitzerModel, SpitzerHarmModel, SauterNeoModel, RedlNeoModel export coulomb_log_e, eta_spitzer, tau_ee_spitzer_harm, eta_spitzer_harm -export trapped_fraction, trapped_fraction_eps +export trapped_fraction, trapped_fraction_nodes, trapped_fraction_lin_liu_miller, trapped_fraction_eps export nu_star_e, eta_neoclassical end # module Utilities diff --git a/test/runtests_slayer_params.jl b/test/runtests_slayer_params.jl index adc45783fc..ef29510c6a 100644 --- a/test/runtests_slayer_params.jl +++ b/test/runtests_slayer_params.jl @@ -301,4 +301,23 @@ @test_throws "chi_perp_Dc must be positive" slayer_parameters(; base..., chi_perp_Dc=0.0) @test_throws "chi_perp_Dc must be positive" slayer_parameters(; base..., chi_perp_Dc=Inf) end + + @testset "trapped_fraction: exact pitch-angle integral and Lin-Liu & Miller" begin + using GeneralizedPerturbedEquilibrium.Utilities.NeoclassicalResistivity: trapped_fraction, + trapped_fraction_nodes, trapped_fraction_lin_liu_miller + # Model surface |B| = 1/(1 + ε cos θ), uniform θ average; reference f_t from a 400-node Gauss-Legendre integral. + theta = range(0, 2π; length=4097)[1:end-1] + avg(f) = sum(f) / length(f) + lambda, weight = trapped_fraction_nodes() + @test sum(weight) ≈ 0.5 atol = 1e-12 # ∫₀¹ λ dλ + for (eps, ft_ref) in ((0.05, 0.318778), (0.124, 0.482295), (0.27, 0.659837), (0.36, 0.729882), (0.6, 0.852351)) + B = @. 1 / (1 + eps * cos(theta)) + B_max = maximum(B) + h = B ./ B_max + ft = trapped_fraction(avg(h .^ 2), [avg(@. sqrt(1 - l * h)) for l in lambda], weight) + @test isapprox(ft, ft_ref; atol=2e-6) + avg_hf = avg(@. (1 - sqrt(1 - h) * (1 + h / 2)) / h^2) + @test isapprox(trapped_fraction_lin_liu_miller(avg(B), avg(B .^ 2), B_max, avg_hf), ft; atol=1e-3) + end + end end From 34d8641de6c596b4706e15e1912a938067def86e Mon Sep 17 00:00:00 2001 From: logan-nc Date: Tue, 17 Nov 2026 09:14:54 -0500 Subject: [PATCH 2/4] Utilities - MINOR - Realign the neoclassical resistivity exports table for the formatter Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01BgNxCF8ZswFaPqyk7i111H --- src/Utilities/NeoclassicalResistivity.jl | 34 ++++++++++++------------ 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Utilities/NeoclassicalResistivity.jl b/src/Utilities/NeoclassicalResistivity.jl index 32e528fe25..3827c187bd 100644 --- a/src/Utilities/NeoclassicalResistivity.jl +++ b/src/Utilities/NeoclassicalResistivity.jl @@ -16,23 +16,23 @@ physics when the same `NeoResistivityModel` is selected. # Exports -| symbol | role | -|:---------------------- |:------------------------------------------------------ | -| `NeoResistivityModel` | abstract tag | -| `SpitzerModel` | plain Spitzer (no trapped-particle correction) | -| `SpitzerHarmModel` | Fitzpatrick/TJ Spitzer-Härm σ_∥ (legacy SLAYER τ_R) | -| `SauterNeoModel` | Sauter 1999 F_33 neoclassical correction | -| `RedlNeoModel` | Redl 2021 F_33 neoclassical correction | -| `coulomb_log_e` | ln Λ_e (NRL or Sauter form) | -| `eta_spitzer` | Sauter 18a Spitzer resistivity [Ω·m] | -| `tau_ee_spitzer_harm` | electron-electron collision time τ_ee [s] | -| `eta_spitzer_harm` | Fitzpatrick/TJ Spitzer-Härm resistivity 1/σ_∥ [Ω·m] | -| `trapped_fraction` | exact pitch-angle integral f_t from ⟨B²⟩/B_max² and ⟨√(1 − λB/B_max)⟩ | -| `trapped_fraction_nodes` | Gauss-Legendre pitch-angle nodes λ and weights for `trapped_fraction` | -| `trapped_fraction_lin_liu_miller` | Lin-Liu & Miller 1995 f_t (OMFIT `fluxSurfaces` form) | -| `trapped_fraction_eps` | simple ε-only f_t fallback | -| `nu_star_e` | Sauter 18b electron collisionality | -| `eta_neoclassical` | dispatched: Spitzer(-Härm) or F_33 · Spitzer | +| symbol | role | +|:--------------------------------- |:--------------------------------------------------------------------- | +| `NeoResistivityModel` | abstract tag | +| `SpitzerModel` | plain Spitzer (no trapped-particle correction) | +| `SpitzerHarmModel` | Fitzpatrick/TJ Spitzer-Härm σ_∥ (legacy SLAYER τ_R) | +| `SauterNeoModel` | Sauter 1999 F_33 neoclassical correction | +| `RedlNeoModel` | Redl 2021 F_33 neoclassical correction | +| `coulomb_log_e` | ln Λ_e (NRL or Sauter form) | +| `eta_spitzer` | Sauter 18a Spitzer resistivity [Ω·m] | +| `tau_ee_spitzer_harm` | electron-electron collision time τ_ee [s] | +| `eta_spitzer_harm` | Fitzpatrick/TJ Spitzer-Härm resistivity 1/σ_∥ [Ω·m] | +| `trapped_fraction` | exact pitch-angle integral f_t from ⟨B²⟩/B_max² and ⟨√(1 − λB/B_max)⟩ | +| `trapped_fraction_nodes` | Gauss-Legendre pitch-angle nodes λ and weights for `trapped_fraction` | +| `trapped_fraction_lin_liu_miller` | Lin-Liu & Miller 1995 f_t (OMFIT `fluxSurfaces` form) | +| `trapped_fraction_eps` | simple ε-only f_t fallback | +| `nu_star_e` | Sauter 18b electron collisionality | +| `eta_neoclassical` | dispatched: Spitzer(-Härm) or F_33 · Spitzer | """ module NeoclassicalResistivity From 3f636f84cbf811ee8d065579d5c69398d829fef3 Mon Sep 17 00:00:00 2001 From: d-burg Date: Mon, 16 Nov 2026 23:45:18 -0500 Subject: [PATCH 3/4] Utilities - BUGFIX - Grade the trapped-fraction pitch-angle nodes to B_min/B_max MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A single 16-node Gauss-Legendre panel in x (λ = 1 − x²) misses the integrand's structure at x ~ √(1 − B_min/B_max), so f_t lost accuracy below ε ~ 1e-3 (−1.1e-3 at ε = 1e-6, where f_t = 1.5e-3). trapped_fraction_nodes(h_min) now places 16 nodes on each geometric panel [0, s], [s, 8s], … , 1 with s = √(1 − h_min), holding f_t to 1e-10 for 0 ≤ ε ≤ 0.9 on the model surface. resist_geometry tracks B_min again to supply h_min. The test extends to ε = 1e-6 at atol 1e-10, checks node doubling, uniform |B|, and the independent small-ε limit f_t/√ε → 1.4624249524 from the boundary-layer integral. Co-Authored-By: Claude Opus 5.5 --- src/ForceFreeStates/Surfaces/ResistEval.jl | 14 ++++----- src/Utilities/NeoclassicalResistivity.jl | 35 +++++++++++++++------- test/runtests_slayer_params.jl | 26 ++++++++++++---- 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/src/ForceFreeStates/Surfaces/ResistEval.jl b/src/ForceFreeStates/Surfaces/ResistEval.jl index ab646a4bde..58d2ff9f1c 100644 --- a/src/ForceFreeStates/Surfaces/ResistEval.jl +++ b/src/ForceFreeStates/Surfaces/ResistEval.jl @@ -87,12 +87,12 @@ function resist_geometry(equil::Equilibrium.PlasmaEquilibrium, v2 = profiles.dVdpsi_deriv(psi_f) q = profiles.q_spline(psi_f) - # θ-integrands (see file header) plus running B_max (f_trap) and R extrema (ε). + # θ-integrands (see file header) plus running extrema of B (f_trap) and R (ε). ntheta = length(equil.rzphi_ys) - lambda, lambda_weight = Utilities.NeoclassicalResistivity.trapped_fraction_nodes() - ff = zeros(Float64, ntheta, 8 + length(lambda)) + ff = zeros(Float64, ntheta, 8) weight = zeros(Float64, ntheta) B_max = -Inf + B_min = Inf R_max = -Inf R_min = Inf for itheta in 1:ntheta @@ -117,6 +117,7 @@ function resist_geometry(equil::Equilibrium.PlasmaEquilibrium, B_here = sqrt(bsq) B_max = max(B_max, B_here) + B_min = min(B_min, B_here) R_max = max(R_max, r) R_min = min(R_min, r) @@ -130,10 +131,9 @@ function resist_geometry(equil::Equilibrium.PlasmaEquilibrium, ff[itheta, 8] = dpsisq weight[itheta] = jac / v1 end - # The pitch-angle columns need B_max over the whole surface, so they are filled after the θ loop. - for itheta in 1:ntheta, (k, lam) in enumerate(lambda) - ff[itheta, 8+k] = sqrt(max(1.0 - lam * ff[itheta, 7] / B_max, 0.0)) - end + # The pitch-angle nodes and columns need B_min and B_max over the whole surface, so they follow the θ loop. + lambda, lambda_weight = Utilities.NeoclassicalResistivity.trapped_fraction_nodes(B_min / B_max) + ff = hcat(ff, @. sqrt(max(1.0 - lambda' * ff[:, 7] / B_max, 0.0))) ff .*= weight # Snap the repeated endpoint exactly equal to the start @views ff[end, :] .= ff[1, :] diff --git a/src/Utilities/NeoclassicalResistivity.jl b/src/Utilities/NeoclassicalResistivity.jl index 3827c187bd..f466fbf56a 100644 --- a/src/Utilities/NeoclassicalResistivity.jl +++ b/src/Utilities/NeoclassicalResistivity.jl @@ -179,16 +179,31 @@ end # Trapped fraction """ - trapped_fraction_nodes(n=16) -> (lambda, weight) - -Pitch-angle nodes for [`trapped_fraction`](@ref): `n`-point Gauss-Legendre in x ∈ [0, 1] with λ = 1 − x², so that -∫₀¹ λ g(λ) dλ ≈ Σₖ weight[k] g(lambda[k]). The x map removes the √ edge behavior at λ → 1; 16 nodes give f_t to ~1e-9. -""" -function trapped_fraction_nodes(n::Integer=16) - x, w = gausslegendre(n) - x = 0.5 .* (x .+ 1.0) - w = 0.5 .* w - return 1.0 .- x .^ 2, w .* 2.0 .* x .* (1.0 .- x .^ 2) + trapped_fraction_nodes(h_min; n=16, ratio=8) -> (lambda, weight) + +Pitch-angle nodes for [`trapped_fraction`](@ref) on a surface with h_min = B_min/B_max, such that +∫₀¹ λ g(λ) dλ ≈ Σₖ weight[k] g(lambda[k]). With λ = 1 − x², the integrand varies on the scale x ~ √(1 − h_min) near +x = 0, so the nodes are `n`-point Gauss-Legendre on the panels [0, s], [s, ratio·s], … , 1 with s = √(1 − h_min). +On the model surface B ∝ 1/(1 + ε cos θ) the defaults give f_t to within 1e-10 for 0 ≤ ε ≤ 0.9, with 16 to 112 nodes. +""" +function trapped_fraction_nodes(h_min::Real; n::Integer=16, ratio::Real=8) + ratio > 1 || throw(ArgumentError("trapped_fraction_nodes: ratio must be > 1")) + edges = [0.0] + s = sqrt(clamp(1.0 - h_min, 0.0, 1.0)) + while 0 < s < 1 + push!(edges, s) + s *= ratio + end + push!(edges, 1.0) + xg, wg = gausslegendre(n) + lambda = Float64[] + weight = Float64[] + for (a, b) in zip(edges[1:end-1], edges[2:end]) + x = @. a + 0.5 * (b - a) * (xg + 1.0) + append!(lambda, @. 1.0 - x^2) + append!(weight, @. 0.5 * (b - a) * wg * 2.0 * x * (1.0 - x^2)) + end + return lambda, weight end """ diff --git a/test/runtests_slayer_params.jl b/test/runtests_slayer_params.jl index ef29510c6a..a33090566f 100644 --- a/test/runtests_slayer_params.jl +++ b/test/runtests_slayer_params.jl @@ -305,19 +305,33 @@ @testset "trapped_fraction: exact pitch-angle integral and Lin-Liu & Miller" begin using GeneralizedPerturbedEquilibrium.Utilities.NeoclassicalResistivity: trapped_fraction, trapped_fraction_nodes, trapped_fraction_lin_liu_miller - # Model surface |B| = 1/(1 + ε cos θ), uniform θ average; reference f_t from a 400-node Gauss-Legendre integral. + # Model surface |B| = 1/(1 + ε cos θ), uniform θ average; reference f_t from an adaptive QuadGK integral in λ. theta = range(0, 2π; length=4097)[1:end-1] avg(f) = sum(f) / length(f) - lambda, weight = trapped_fraction_nodes() - @test sum(weight) ≈ 0.5 atol = 1e-12 # ∫₀¹ λ dλ - for (eps, ft_ref) in ((0.05, 0.318778), (0.124, 0.482295), (0.27, 0.659837), (0.36, 0.729882), (0.6, 0.852351)) + ft_model(h, (lambda, weight)) = trapped_fraction(avg(h .^ 2), [avg(@. sqrt(max(1 - l * h, 0.0))) for l in lambda], weight) + refs = ((0.6, 0.852350745554), (0.36, 0.729882018970), (0.27, 0.659837024682), (0.124, 0.482295253813), + (0.05, 0.318778452240), (1e-2, 0.145562849918), (1e-4, 0.014623642822), (1e-6, 0.001462424358)) + for (eps, ft_ref) in refs B = @. 1 / (1 + eps * cos(theta)) B_max = maximum(B) h = B ./ B_max - ft = trapped_fraction(avg(h .^ 2), [avg(@. sqrt(1 - l * h)) for l in lambda], weight) - @test isapprox(ft, ft_ref; atol=2e-6) + nodes = trapped_fraction_nodes(minimum(h)) + @test sum(nodes[2]) ≈ 0.5 atol = 1e-14 # ∫₀¹ λ dλ + ft = ft_model(h, nodes) + @test isapprox(ft, ft_ref; atol=1e-10) + # Weak trapping (f_t ≈ 1.46√ε) needs the panels: doubling the nodes per panel must not move f_t. + @test isapprox(ft_model(h, trapped_fraction_nodes(minimum(h); n=32)), ft; atol=1e-10) + eps >= 0.05 || continue avg_hf = avg(@. (1 - sqrt(1 - h) * (1 + h / 2)) / h^2) @test isapprox(trapped_fraction_lin_liu_miller(avg(B), avg(B .^ 2), B_max, avg_hf), ft; atol=1e-3) end + # Independent small-ε limit f_t → C√ε + O(ε^3/2), C = ¾∫₀^∞ [y^(-1/2) − 1/⟨√(y + 1 + cos θ)⟩] dy from the layer at λ → 1. + for eps in (1e-5, 1e-6, 1e-7) + B = @. 1 / (1 + eps * cos(theta)) + h = B ./ maximum(B) + @test isapprox(ft_model(h, trapped_fraction_nodes(minimum(h))) / sqrt(eps), 1.4624249524405; atol=eps) + end + # Uniform |B|: no trapped particles, and a single panel integrates the polynomial integrand exactly. + @test ft_model(ones(length(theta)), trapped_fraction_nodes(1.0)) ≈ 0.0 atol = 1e-14 end end From 0f9f23d144fa4cab427557852147e0a69546fea6 Mon Sep 17 00:00:00 2001 From: d-burg Date: Sun, 22 Nov 2026 17:17:23 -0500 Subject: [PATCH 4/4] Repo - MINOR - Retrigger CI on develop with the CCC parity test fix Co-Authored-By: Claude Opus 5.5