diff --git a/src/ForceFreeStates/Surfaces/ResistEval.jl b/src/ForceFreeStates/Surfaces/ResistEval.jl index d3ec7b32fc..56d4e9f7a1 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,9 +87,10 @@ 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 extrema of B (f_trap) and R (ε). ntheta = length(equil.rzphi_ys) ff = zeros(Float64, ntheta, 8) + weight = zeros(Float64, ntheta) B_max = -Inf B_min = Inf R_max = -Inf @@ -127,8 +129,12 @@ 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 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, :] @@ -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..f466fbf56a 100644 --- a/src/Utilities/NeoclassicalResistivity.jl +++ b/src/Utilities/NeoclassicalResistivity.jl @@ -16,29 +16,32 @@ 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` | Lin-Liu & Miller 1995 f_t from ⟨B⟩, ⟨B²⟩, B_min, B_max | -| `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 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,73 @@ end # Trapped fraction """ - trapped_fraction(avg_B, avg_Bsq, B_min, B_max) -> Float64 + trapped_fraction_nodes(h_min; n=16, ratio=8) -> (lambda, weight) -Lin-Liu & Miller 1995, Phys. Plasmas **2**, 1666, Eq. (6): +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 + +""" + 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..a33090566f 100644 --- a/test/runtests_slayer_params.jl +++ b/test/runtests_slayer_params.jl @@ -301,4 +301,37 @@ @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 an adaptive QuadGK integral in λ. + theta = range(0, 2π; length=4097)[1:end-1] + avg(f) = sum(f) / length(f) + 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 + 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