From 3376d22af8b78f145bb475e53ca6dc5cb4c4b74e Mon Sep 17 00:00:00 2001 From: Nan Shi Date: Wed, 11 Nov 2026 15:49:59 -0800 Subject: [PATCH 1/2] Fix fast-ion density folding when modify_electron_density=true fast_particles_profiles! folded the previous density_fast into density_thermal on every call and never undid it, so thermal ion densities (and n_e via quasi-neutrality) grew with each call. Now the thermal density of source-fed ions is restored before quasi-neutrality is enforced; unfed ions keep the old behavior. --- src/physics/fast.jl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/physics/fast.jl b/src/physics/fast.jl index 27d421d9..358f18f9 100644 --- a/src/physics/fast.jl +++ b/src/physics/fast.jl @@ -343,6 +343,13 @@ function fast_particles_profiles!(cs::IMAS.core_sources, cp1d::IMAS.core_profile for s in cs.source) has_fast_source || return + # with modify_electron_density the thermal density of every ion fed by a fast-ion source must come + # out of this call unchanged, so remember it before the old fast-ion contribution is folded in below + if modify_electron_density + density_thermal_in = [copy(ion.density_thermal) for ion in cp1d.ion] + fed_by_source = falses(length(cp1d.ion)) + end + for ion in cp1d.ion ion.pressure_fast_parallel = zeros(Npsi) ion.pressure_fast_perpendicular = zeros(Npsi) @@ -392,6 +399,7 @@ function fast_particles_profiles!(cs::IMAS.core_sources, cp1d::IMAS.core_profile if modify_electron_density # leave the thermal ion density unchanged: the electron density is adjusted # below to satisfy quasi-neutrality once all fast-ion sources are accounted for + fed_by_source[cindex] = true IMAS.unfreeze!(cion, :density) else # subtract the fast ion density from the thermal density of the same species, @@ -405,6 +413,14 @@ function fast_particles_profiles!(cs::IMAS.core_sources, cp1d::IMAS.core_profile end if modify_electron_density + # undo the fold above for ions fed by a source: without this, every call moves the previous + # fast-ion population into the thermal density, and thermal ions (and the electrons) grow on + # each call. Ions with no source this call keep the fold: their old fast ions thermalize. + for (k, ion) in enumerate(cp1d.ion) + fed_by_source[k] || continue + ion.density_thermal = density_thermal_in[k] + IMAS.unfreeze!(ion, :density) + end enforce_quasi_neutrality!(cp1d, :electrons) end end From acc7ed6d908e0ca1ddf8bcdd2b733b2f6e6754e1 Mon Sep 17 00:00:00 2001 From: tomneiser Date: Sat, 14 Nov 2026 13:02:30 -0800 Subject: [PATCH 2/2] fast-ion density cap from the thermal density on entry when modify_electron_density=true The limit was taken from a thermal density that still held the fast ions of the previous call, so with a source strong enough to reach the cap the fast-ion density rose from call to call (to about twice the intended limit). Test: repeated calls leave the thermal ions alone, respect the cap and settle. Co-Authored-By: Claude Fable 5.2 --- src/physics/fast.jl | 8 ++++- test/runtests_fast.jl | 70 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/src/physics/fast.jl b/src/physics/fast.jl index 358f18f9..4f5fc1ed 100644 --- a/src/physics/fast.jl +++ b/src/physics/fast.jl @@ -394,7 +394,13 @@ function fast_particles_profiles!(cs::IMAS.core_sources, cp1d::IMAS.core_profile cion.pressure_fast_parallel .+= pressa ./ 3.0 cion.pressure_fast_perpendicular .+= pressa ./ 3.0 cion.density_fast .+= sion_particles .* taut - limit = max_fast_frac .* cion.density_thermal # here density_thermal includes the old fast-ion contribution + if modify_electron_density + # the thermal density on entry: at this point density_thermal also holds the fast ions of + # the previous call, and a limit taken from it would rise with every call + limit = max_fast_frac .* density_thermal_in[cindex] + else + limit = max_fast_frac .* cion.density_thermal # here density_thermal includes the old fast-ion contribution + end fast_density_cap!(cion.density_fast, limit, fast_density_knee) if modify_electron_density # leave the thermal ion density unchanged: the electron density is adjusted diff --git a/test/runtests_fast.jl b/test/runtests_fast.jl index 4d5cfc44..1dbe7e0e 100644 --- a/test/runtests_fast.jl +++ b/test/runtests_fast.jl @@ -35,3 +35,73 @@ end y = IMAS.fast_density_cap!(collect(range(0.0, 3.0, 3001)), ones(3001), 0.7) @test maximum(abs, diff(diff(y))) < 1e-5 end + +@testset "fast_particles_profiles! called repeatedly" begin + # a deuterium + carbon plasma with one deuterium fast-ion source of amplitude S0 + function plasma_with_fast_source(S0) + dd = IMAS.dd() + rho = collect(range(0.0, 1.0, 21)) + n = 5e19 .* (1 .- 0.8 .* rho .^ 2) + T = 3e3 .* (1 .- 0.9 .* rho .^ 2) + cp1d = resize!(dd.core_profiles.profiles_1d, 1)[1] + cp1d.time = 0.0 + dd.core_profiles.time = [0.0] + dd.global_time = 0.0 + cp1d.grid.rho_tor_norm = rho + cp1d.electrons.density_thermal = copy(n) + cp1d.electrons.temperature = copy(T) + resize!(cp1d.ion, 2) + for (k, (label, z, a, fraction)) in enumerate((("D", 1.0, 2.0, 0.88), ("C", 6.0, 12.0, 0.02))) + ion = cp1d.ion[k] + ion.label = label + resize!(ion.element, 1) + ion.element[1].z_n = z + ion.element[1].a = a + ion.density_thermal = fraction .* n + ion.temperature = copy(T) + end + source1d = resize!(resize!(dd.core_sources.source, 1)[1].profiles_1d, 1)[1] + source1d.time = 0.0 + dd.core_sources.time = [0.0] + source1d.grid.rho_tor_norm = rho + sion = resize!(source1d.ion, 1)[1] + sion.label = "D" + resize!(sion.element, 1) + sion.element[1].z_n = 1.0 + sion.element[1].a = 2.0 + sion.particles = S0 .* (1 .- rho .^ 2) .+ 1e15 + sion.fast_particles_energy = 80e3 + return dd + end + + # S0 = 2e19: fast ions well below the cap; S0 = 2e21: the cap sets the fast-ion density + for S0 in (2e19, 2e21) + # electron density from quasi-neutrality: the thermal ions are left alone, however often this is called + dd = plasma_with_fast_source(S0) + cp1d = dd.core_profiles.profiles_1d[1] + thermal0 = [copy(ion.density_thermal) for ion in cp1d.ion] + fast = Vector{Float64}[] + for _ in 1:8 + IMAS.fast_particles_profiles!(dd.core_sources, cp1d; modify_electron_density=true) + D = cp1d.ion[1] + @test all(ion.density_thermal == thermal0[k] for (k, ion) in enumerate(cp1d.ion)) + @test all(D.density_fast .<= 0.8 .* thermal0[1] .* (1 + 1e-12)) + # quasi-neutrality with the fast ions (carbon is not quite fully stripped at the edge) + @test cp1d.electrons.density ≈ D.density_thermal .+ D.density_fast .+ 6.0 .* cp1d.ion[2].density_thermal rtol = 1e-3 + push!(fast, copy(D.density_fast)) + end + @test fast[end] ≈ fast[end-1] rtol = 1e-3 + S0 == 2e21 && @test fast[end][1] ≈ 0.8 * thermal0[1][1] + + # default: the electron and total ion densities are left alone + dd = plasma_with_fast_source(S0) + cp1d = dd.core_profiles.profiles_1d[1] + ne0 = copy(cp1d.electrons.density_thermal) + total0 = copy(cp1d.ion[1].density_thermal) + for _ in 1:3 + IMAS.fast_particles_profiles!(dd.core_sources, cp1d) + @test cp1d.electrons.density ≈ ne0 + @test cp1d.ion[1].density ≈ total0 + end + end +end