diff --git a/src/physics/fast.jl b/src/physics/fast.jl index 68d20b7b..27d421d9 100644 --- a/src/physics/fast.jl +++ b/src/physics/fast.jl @@ -286,7 +286,34 @@ end push!(document[Symbol("Physics fast")], :fast_ion_thermalization_time) """ - fast_particles_profiles!(cs::IMAS.core_sources, cp1d::IMAS.core_profiles__profiles_1d; verbose::Bool=false, max_fast_frac::Float64=0.8, modify_electron_density::Bool=false) + fast_density_cap!(nf::AbstractVector, limit::AbstractVector, knee::Real) + +Limit the fast-ion density `nf` to `limit` in place. + +`knee >= 1` is a hard clamp, `min(nf, limit)`. With `knee < 1`, `nf` is unchanged up to `knee * limit` and saturates smoothly +above it, `limit * (knee + (1 - knee) * tanh((x - knee) / (1 - knee)))` with `x = nf / limit`, so that profiles where the cap +engages over part of the radius have no slope break. +""" +function fast_density_cap!(nf::AbstractVector, limit::AbstractVector, knee::Real) + for i in eachindex(nf, limit) + L = limit[i] + if knee >= 1 || L <= 0 + nf[i] > L && (nf[i] = L) + else + x = nf[i] / L + if x > knee + nf[i] = L * (knee + (1 - knee) * tanh((x - knee) / (1 - knee))) + end + end + end + return nf +end + +@compat public fast_density_cap! +push!(document[Symbol("Physics fast")], :fast_density_cap!) + +""" + fast_particles_profiles!(cs::IMAS.core_sources, cp1d::IMAS.core_profiles__profiles_1d; verbose::Bool=false, max_fast_frac::Float64=0.8, fast_density_knee::Real=1.0, modify_electron_density::Bool=false) Fills the core_profiles fast ion densities and pressures that result from fast ion sources (eg. fusion and nbi) @@ -295,8 +322,10 @@ This calculation is done based on the `slowing_down_time` and `thermalization_ti By default the fast ion density is subtracted from the thermal density of the same ion species, keeping the total ion density (and thus the electron density) unchanged. If `modify_electron_density` is `true`, the thermal ion density is instead left unchanged and the electron density is recomputed to satisfy quasi-neutrality. + +The fast ion density is capped at `max_fast_frac` times the thermal density, see [`fast_density_cap!`](@ref) for `fast_density_knee`. """ -function fast_particles_profiles!(cs::IMAS.core_sources, cp1d::IMAS.core_profiles__profiles_1d; verbose::Bool=false, max_fast_frac::Float64=0.8, modify_electron_density::Bool=false) +function fast_particles_profiles!(cs::IMAS.core_sources, cp1d::IMAS.core_profiles__profiles_1d; verbose::Bool=false, max_fast_frac::Float64=0.8, fast_density_knee::Real=1.0, modify_electron_density::Bool=false) ne = cp1d.electrons.density_thermal Te = cp1d.electrons.temperature @@ -359,8 +388,7 @@ function fast_particles_profiles!(cs::IMAS.core_sources, cp1d::IMAS.core_profile 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 - mask = cion.density_fast .> limit - cion.density_fast[mask] .= limit[mask] + 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 @@ -381,8 +409,8 @@ function fast_particles_profiles!(cs::IMAS.core_sources, cp1d::IMAS.core_profile end end -function fast_particles_profiles!(dd::IMAS.DD; verbose::Bool=false, modify_electron_density::Bool=false) - return fast_particles_profiles!(dd.core_sources, dd.core_profiles.profiles_1d[]; verbose, modify_electron_density) +function fast_particles_profiles!(dd::IMAS.DD; verbose::Bool=false, fast_density_knee::Real=1.0, modify_electron_density::Bool=false) + return fast_particles_profiles!(dd.core_sources, dd.core_profiles.profiles_1d[]; verbose, fast_density_knee, modify_electron_density) end @compat public fast_particles_profiles! diff --git a/src/physics/sources.jl b/src/physics/sources.jl index 1b499948..fbefa96d 100644 --- a/src/physics/sources.jl +++ b/src/physics/sources.jl @@ -143,7 +143,7 @@ end push!(document[Symbol("Physics sources")], :radiation_source!) """ - intrinsic_sources!(dd::IMAS.DD; bootstrap::Bool=true, DD_fusion::Bool=false, fast_ion_densities::Bool=true) + intrinsic_sources!(dd::IMAS.DD; bootstrap::Bool=true, DD_fusion::Bool=false, fast_ion_densities::Bool=true, modify_electron_density::Bool=false, fast_density_knee::Real=1.0) Calculates intrinsic sources and sinks, and adds them to `dd.core_sources` @@ -151,8 +151,9 @@ Calculates intrinsic sources and sinks, and adds them to `dd.core_sources` - `DD_fusion`: Include D-D fusion reactions (only relevant for D+D plasmas) - `fast_ion_densities`: Update fast ion density profiles after fusion source calculation - `modify_electron_density`: When updating fast ion densities, adjust the electron density to satisfy quasi-neutrality instead of subtracting the fast density from the thermal ion density + - `fast_density_knee`: Soft cap of the fast ion density (see [`fast_density_cap!`](@ref)); 1.0 is a hard clamp """ -function intrinsic_sources!(dd::IMAS.DD; bootstrap::Bool=true, DD_fusion::Bool=false, fast_ion_densities::Bool=true, modify_electron_density::Bool=false) +function intrinsic_sources!(dd::IMAS.DD; bootstrap::Bool=true, DD_fusion::Bool=false, fast_ion_densities::Bool=true, modify_electron_density::Bool=false, fast_density_knee::Real=1.0) collisional_exchange_source!(dd) # electron and ion energy @@ -160,7 +161,7 @@ function intrinsic_sources!(dd::IMAS.DD; bootstrap::Bool=true, DD_fusion::Bool=f fusion_source!(dd; DD_fusion) # electron and ion energy, particles - fast_ion_densities && fast_particles_profiles!(dd; modify_electron_density) # update fast ion densities + fast_ion_densities && fast_particles_profiles!(dd; modify_electron_density, fast_density_knee) # update fast ion densities bootstrap && bootstrap_source!(dd) # current diff --git a/test/runtests_fast.jl b/test/runtests_fast.jl index 8aac49bd..4d5cfc44 100644 --- a/test/runtests_fast.jl +++ b/test/runtests_fast.jl @@ -22,3 +22,16 @@ using Test @test ec_roots ≈ 93662.596 rtol = 1e-5 end end + +@testset "fast_density_cap!" begin + L = [1.0, 1.0, 1.0, 1.0, 0.0] + nf = [0.5, 0.9, 1.5, 10.0, 0.3] + @test IMAS.fast_density_cap!(copy(nf), L, 1.0) == min.(nf, L) + soft = IMAS.fast_density_cap!(copy(nf), L, 0.7) + @test soft[1] == 0.5 + @test all(0.7 .< soft[2:4] .<= 1.0) && issorted(soft[2:4]) + @test soft[5] == 0.0 + # no slope break at the knee + 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