From 039ba3312bc6ba355aeecbb734aa912a6164d7f2 Mon Sep 17 00:00:00 2001 From: Orso Meneghini Date: Mon, 28 Sep 2026 04:22:44 +0200 Subject: [PATCH] Identify pellet species by element, not by label or order dr_dt! chose the ablation model from exact labels ("D", "T", "Ne20", "C12") and, for Ne-D, assigned masses, charges and fractions by list position: the check species_list[1] == "Ne" never matches the label "Ne20" that selects the branch, so a layer listing Ne20 before D got the two masses swapped. Layers labelled "Ne" or "C" were rejected, and a carbon-deuterium mixture silently used the pure-carbon model. Species are now found by element with IMAS.ion_properties (hydrogen isotopes by mass number, neon Z = 10, carbon Z = 6), the model is chosen from the exact set of elements in the layer, and anything else raises an error that names the species. The formulas are unchanged. Adds a species-detection testset. Co-Authored-By: Claude Opus 5.5 --- src/PAM.jl | 65 +++++++++++++++++++----------------------------- test/runtests.jl | 29 +++++++++++++++++++++ 2 files changed, 55 insertions(+), 39 deletions(-) diff --git a/src/PAM.jl b/src/PAM.jl index 95d9276..cfd9710 100644 --- a/src/PAM.jl +++ b/src/PAM.jl @@ -348,24 +348,22 @@ function dr_dt!(pelt::Pellet, k::Int) Bt_exp = 0 end - # ablation model for the DT pellet - if ("D" in species_list) & ("T" in species_list) - if species_list[1] == "D" - AD = A[1] - AT = A[2] - FD = fractions[1] - FT = fractions[2] - ZD = Z[1] - ZT = Z[2] + # species are identified by element, whatever their label ("Ne" or "Ne20", "C" or "C12") or + # their order in the layer; the hydrogen isotopes are told apart by mass number + iD = findfirst(i -> Z[i] == 1 && round(A[i]) == 2, eachindex(Z)) + iT = findfirst(i -> Z[i] == 1 && round(A[i]) == 3, eachindex(Z)) + iNe = findfirst(==(10), Z) + iC = findfirst(==(6), Z) + elements(indices...) = length(Z) == length(indices) && all(!isnothing, indices) - else - AD = A[2] - AT = A[1] - FD = fractions[2] - FT = fractions[1] - ZD = Z[2] - ZT = Z[1] - end + # ablation model for the DT pellet + if elements(iD, iT) + AD = A[iD] + AT = A[iT] + FD = fractions[iD] + FT = fractions[iT] + ZD = Z[iD] + ZT = Z[iT] # according equation #1 in J.McClenaghan at al Nucl.Fusion 63 (2023) 036015 (two coefficients are missing) and normalization to FUSE units ρ_zero = (1 - FD + FD * AD / AT) * ((1 - FD) / pellet_mass_density("T") + (FD * AD / AT) / pellet_mass_density("D"))^(-1) #[g cm^-3] @@ -400,24 +398,13 @@ function dr_dt!(pelt::Pellet, k::Int) pelt.ablation_rate[k] = GT + GD - elseif ("Ne20" in species_list) & ("D" in species_list) - - if species_list[1] == "Ne" - ANe = A[1] - AD = A[2] - FNe = fractions[1] - FD = fractions[2] - ZNe = Z[1] - ZD = Z[2] - - else - ANe = A[2] - AD = A[1] - FD = fractions[1] - FNe = fractions[2] - ZNe = Z[2] - ZD = Z[1] - end + elseif elements(iNe, iD) + ANe = A[iNe] + AD = A[iD] + FNe = fractions[iNe] + FD = fractions[iD] + ZNe = Z[iNe] + ZD = Z[iD] Wratio = (1 - FD) * ANe / AD + FD @@ -456,11 +443,11 @@ function dr_dt!(pelt::Pellet, k::Int) pelt.ablation_rate[k] = GD + GNe - elseif "C12" in species_list + elseif elements(iC) C0 = 8.146777e-9 - AC = A[1] - ZC = Z[1] + AC = A[iC] + ZC = Z[iC] gamma = 5.0 / 3.0 ZstarPlus1C = 2.86 @@ -549,7 +536,7 @@ function dr_dt!(pelt::Pellet, k::Int) pelt.ablation_rate[k] = G * 6.022e23 / AC * ZC else - error("No ablation model is implemented for such combination of species") + error("No ablation model is implemented for the species $species_list: supported are D-T, D-Ne and C") end return diff --git a/test/runtests.jl b/test/runtests.jl index 6455ac9..f751b90 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -26,6 +26,35 @@ using Test end +@testset "species detection" begin + dd_D3D = IMAS.hdf2imas(joinpath(@__DIR__, "..", "examples", "template_D3D_1layer_2species.h5")) + dd_D3D.pellets.time_slice[].pellet[1].velocity_initial = 200.0 + + function ablation_rate(species) + dd = deepcopy(dd_D3D) + layer = dd.pellets.time_slice[].pellet[1].layer[1] + resize!(layer.species, length(species)) + for (s, (label, fraction)) in zip(layer.species, species) + s.label = label + s.fraction = fraction + end + pellet = PAM.run_PAM(dd; t_start=0.0, t_finish=0.0045, time_step=0.0001, drift_model=:none, Bt_dependance=true, update_plasma=false) + return pellet.ablation_rate + end + + # the order of the species in the layer does not matter + @test ablation_rate([("D", 0.6), ("T", 0.4)]) == ablation_rate([("T", 0.4), ("D", 0.6)]) + rate_DNe = ablation_rate([("D", 0.6), ("Ne20", 0.4)]) + @test maximum(rate_DNe) > 0 + @test ablation_rate([("Ne20", 0.4), ("D", 0.6)]) == rate_DNe + + # nor does the label of the element; "Ne" and "C" carry the natural isotope mix + @test isapprox(ablation_rate([("D", 0.6), ("Ne", 0.4)]), rate_DNe; rtol=2e-2) + @test isapprox(ablation_rate([("C", 1.0)]), ablation_rate([("C12", 1.0)]); rtol=1e-3) + + @test_throws ErrorException ablation_rate([("C12", 0.5), ("D", 0.5)]) +end + @testset "Comparison with OMFIT PAM" begin dd_D3D = IMAS.hdf2imas(joinpath(@__DIR__, "..", "examples", "template_D3D_1layer_2species.h5")) dd_D3D.pellets.time_slice[].pellet[1].velocity_initial = 200.0;