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/src/physics/thermal_loads.jl b/src/physics/thermal_loads.jl index dc8d9c2b..e444a4d6 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 @@ -450,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 @@ -468,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) 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! 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