Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/physics/fast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -387,11 +394,18 @@ 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
# 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,
Expand All @@ -405,6 +419,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
Expand Down
70 changes: 70 additions & 0 deletions test/runtests_fast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading