From ed6760beee5f03f8285aa4ee07c2208f98c6a730 Mon Sep 17 00:00:00 2001 From: Tom Neiser Date: Mon, 16 Nov 2026 05:37:08 -0800 Subject: [PATCH 1/5] magnetics!: IDS without data `AbstractTrees.Leaves` of an IDS without data yields the IDS itself (as it does for an empty element of an array of structures), which has no `field`: `magnetics!(mag, eq)` raised `type magnetics has no field field` on a dd without magnetics data instead of doing nothing. Only field leaves are looked at now, as in `time_groups` and `time_dependent_leaves`. Co-Authored-By: Claude Fable 5.2 --- src/physics/magnetics.jl | 3 ++- test/runtests_interpolations.jl | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/physics/magnetics.jl b/src/physics/magnetics.jl index d386ad6b..1cdf1ca4 100644 --- a/src/physics/magnetics.jl +++ b/src/physics/magnetics.jl @@ -7,7 +7,8 @@ Calculates synthetic magnetic probes and flux loops data at all time slices wher """ function magnetics!(mag::IMAS.magnetics, eq::IMAS.equilibrium) for leaf in IMASdd.AbstractTrees.Leaves(mag) - if leaf.field ∈ (:data_σ, :data, :time) + # an IDS (or an element of an array of structures) without data is a leaf itself, not a field + if leaf isa IMASdd.IMASnodeRepr && leaf.field ∈ (:data_σ, :data, :time) empty!(leaf.ids, leaf.field) end end diff --git a/test/runtests_interpolations.jl b/test/runtests_interpolations.jl index 0d01083b..c2d04cb3 100644 --- a/test/runtests_interpolations.jl +++ b/test/runtests_interpolations.jl @@ -113,6 +113,16 @@ const RTOL = 1e-2 # regression goldens @test probe.field.data[1] ≈ 0.09799427799636301 rtol = RTOL @test loop.flux.data[1] ≈ -2.1356278924110654 rtol = RTOL + + # a second call replaces the synthetic data + IMAS.magnetics!(dd.magnetics, dd.equilibrium) + @test length(probe.field.data) == length(dd.equilibrium.time_slice) + @test length(loop.flux.data) == length(dd.equilibrium.time_slice) + + # an IDS without probes and loops (no data at all) stays empty + mag = typeof(dd.magnetics)() + IMAS.magnetics!(mag, dd.equilibrium) + @test isempty(mag.b_field_pol_probe) && isempty(mag.flux_loop) end # sol → find_levels_from_P / find_levels_from_wall / find_psi_* / _deduplicate_knots! From e86ad3d33b632d7939a54a6c79316993c70dbc36 Mon Sep 17 00:00:00 2001 From: Tom Neiser Date: Mon, 16 Nov 2026 05:37:08 -0800 Subject: [PATCH 2/5] mesher_heat_flux: psi levels given as a vector - With `levels` a vector and `merge_wall = true` (the default) the function raised `UndefVarError: eqt2d`: the 2D equilibrium was looked up only in the branch that builds the levels from a number. - The first level is set to the separatrix; this was written into the caller's vector. Co-Authored-By: Claude Fable 5.2 --- src/physics/thermal_loads.jl | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/physics/thermal_loads.jl b/src/physics/thermal_loads.jl index dc8d9c2b..3d7f0c1d 100644 --- a/src/physics/thermal_loads.jl +++ b/src/physics/thermal_loads.jl @@ -216,10 +216,10 @@ function mesher_heat_flux(dd::IMAS.DD; reorder_flux_surface!(rwall, zwall, R0, Z0; force_close=true) # Parameters for particle heat flux + eqt2d = findfirst(:rectangular, eqt.profiles_2d) + _, _, PSI_interpolant = ψ_interpolant(eqt2d) #interpolation of PSI in equilirium at locations (r,z) if typeof(levels) <: Int #levels is an Int, build a vector of psi_levels of that size - eqt2d = findfirst(:rectangular, eqt.profiles_2d) - _, _, PSI_interpolant = ψ_interpolant(eqt2d) #interpolation of PSI in equilirium at locations (r,z) psi_levels, _, _ = find_levels_from_P(eqt, rwall, zwall, PSI_interpolant, r, q, levels) add_psi = find_levels_from_wall(eqt, rwall, zwall, PSI_interpolant) psi_sign = sign(psi_levels[end] - psi_levels[1]) @@ -230,8 +230,8 @@ function mesher_heat_flux(dd::IMAS.DD; psi_levels = reverse!(psi_levels) # if psi is decreasing, sort in descending order end else - #levels is a vector, use it as it is - psi_levels = levels + #levels is a vector, use it as it is (a copy: the first level is set to the separatrix below) + psi_levels = collect(levels) end psi_levels[1] = psi_boundaries.first_open @@ -340,8 +340,6 @@ function mesher_heat_flux(dd::IMAS.DD; end if merge_wall - _, _, PSI_interpolant = ψ_interpolant(eqt2d) #interpolation of PSI in equilirium at locations (r,z) - #! format: off if zwall[1] > Z0 # correction if first point is above midplane indexes[indexes .== 1 .&& Zwall.>Z0] .= length(rwall) # put it after index = end From d17e99090c7859d93c459211477859e633ed0f1e Mon Sep 17 00:00:00 2001 From: Tom Neiser Date: Mon, 16 Nov 2026 05:37:08 -0800 Subject: [PATCH 3/5] mesher_heat_flux: add the outer midplane point only if it is not in the mesh `(r_wall_midplane, Z0) in (Rwall, Zwall)` looks for the pair among the two vectors, so it was always false and the point was always prepended: when the mesh already has that point it was there twice. Co-Authored-By: Claude Fable 5.2 --- src/physics/thermal_loads.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/physics/thermal_loads.jl b/src/physics/thermal_loads.jl index 3d7f0c1d..8466d598 100644 --- a/src/physics/thermal_loads.jl +++ b/src/physics/thermal_loads.jl @@ -448,9 +448,8 @@ function mesher_heat_flux(dd::IMAS.DD; counter += 1 save_length = length(Rwall) end - # add OMP point - if (r_wall_midplane, Z0) in (Rwall, Zwall) - else + # add OMP point, unless it is in the mesh already + if !any((Rwall .== r_wall_midplane) .& (Zwall .== Z0)) Rwall = vcat(r_wall_midplane, Rwall) Zwall = vcat(Z0, Zwall) end From 6d7569e84c985cde845fcb6a705d49537230957f Mon Sep 17 00:00:00 2001 From: Tom Neiser Date: Mon, 16 Nov 2026 05:37:08 -0800 Subject: [PATCH 4/5] mesher_heat_flux: abscissa of the closing point The closing point was appended to the mesh before the length of the closing segment was computed, from the first and the (new) last point: the segment had zero length in `s`. Co-Authored-By: Claude Fable 5.2 --- src/physics/thermal_loads.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/physics/thermal_loads.jl b/src/physics/thermal_loads.jl index 8466d598..e444a4d6 100644 --- a/src/physics/thermal_loads.jl +++ b/src/physics/thermal_loads.jl @@ -465,9 +465,9 @@ function mesher_heat_flux(dd::IMAS.DD; # Make sure (Rwall,Zwall) is closed if !((Rwall[1], Zwall[1]) == (Rwall[end], Zwall[end])) # Mesh not closed! + push!(s, s[end] + sqrt((Rwall[1] - Rwall[end])^2 + (Zwall[1] - Zwall[end])^2)) push!(Rwall, Rwall[1]) push!(Zwall, Zwall[1]) - push!(s, s[end] + sqrt((Rwall[1] - Rwall[end])^2 + (Zwall[1] - Zwall[end])^2)) end return (Rwall=Rwall, Zwall=Zwall, s=s, SOL=SOL, r=r, q=q) From 80f0165f41f731ee67642234647dad299808862d Mon Sep 17 00:00:00 2001 From: Tom Neiser Date: Mon, 16 Nov 2026 05:37:08 -0800 Subject: [PATCH 5/5] Tests: wall mesh with psi levels given as a vector, closing point Co-Authored-By: Claude Fable 5.2 --- test/runtests_smoke.jl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/runtests_smoke.jl b/test/runtests_smoke.jl index 58a4d087..357e79d8 100644 --- a/test/runtests_smoke.jl +++ b/test/runtests_smoke.jl @@ -121,5 +121,25 @@ finite(x) = all(isfinite, x) @test length(mesh.Rwall) == length(mesh.Zwall) > length(fwD.r) hf = IMAS.particle_heat_flux(mesh.SOL, mesh.Rwall, mesh.Zwall, mesh.r, mesh.q) @test finite(hf[1]) && maximum(hf[1]) > 0.0 + + # the mesh is closed, and the abscissa of its closing point includes the closing segment + @test (mesh.Rwall[1], mesh.Zwall[1]) == (mesh.Rwall[end], mesh.Zwall[end]) + @test mesh.s[end] - mesh.s[end-1] ≈ hypot(mesh.Rwall[end] - mesh.Rwall[end-1], mesh.Zwall[end] - mesh.Zwall[end-1]) + # the outer midplane point (the first one) is in the mesh once + @test count((mesh.Rwall[1:end-1] .== mesh.Rwall[1]) .& (mesh.Zwall[1:end-1] .== mesh.Zwall[1])) == 1 + + # psi levels given as a vector: with the levels of the mesh above it is the same mesh, with and without + # the wall, and the vector is not changed + levels = [ofl.psi for ofl in vcat(mesh.SOL[:lfs], mesh.SOL[:lfs_far])] + levels[1] += 1e-3 * (levels[2] - levels[1]) # the first level is always the separatrix + levels0 = copy(levels) + mesh_levels = IMAS.mesher_heat_flux(ddD; r, q, levels) + @test levels == levels0 + @test mesh_levels.Rwall == mesh.Rwall && mesh_levels.Zwall == mesh.Zwall + mesh_sol = IMAS.mesher_heat_flux(ddD; r, q, merge_wall=false) + mesh_sol_levels = IMAS.mesher_heat_flux(ddD; r, q, levels, merge_wall=false) + @test levels == levels0 + @test mesh_sol_levels.Rwall == mesh_sol.Rwall && mesh_sol_levels.Zwall == mesh_sol.Zwall + @test length(mesh_sol.Rwall) < length(mesh.Rwall) end end