Skip to content

Commit

Permalink
Merge pull request #18 from ProjectTorreyPines/mxhequilibrium
Browse files Browse the repository at this point in the history
Equilibrium --> MXHEquilibrium
  • Loading branch information
orso82 committed Feb 8, 2023
2 parents ec07e3d + 6f8c2cb commit 65f24f4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 33 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ Contour = "d38c429a-6771-53c6-b99e-75d170b6e991"
CoordinateConventions = "7204ce3a-f536-43d2-be4a-fbed74e90d86"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
EFIT = "cda752c5-6b03-55a3-9e33-132a441b0c17"
Equilibrium = "187daef3-cca3-527a-8725-7379bc760182"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MXHEquilibrium = "8efa4e5a-7b9a-4eec-876e-01d13d5b5d75"
MillerExtendedHarmonic = "c82744c2-dc08-461a-8c37-87ab04d0f9b8"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
3 changes: 2 additions & 1 deletion src/VacuumFields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ __precompile__()

module VacuumFields

using Equilibrium
import MillerExtendedHarmonic
using MXHEquilibrium
using Plots
using Trapz
using Unzip
Expand Down
53 changes: 23 additions & 30 deletions src/coil_currents.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
abstract type AbstractCoil end

mutable struct PointCoil{R1<:Real, R2<:Real, R3<:Real} <: AbstractCoil
mutable struct PointCoil{R1<:Real,R2<:Real,R3<:Real} <: AbstractCoil
R::R1
Z::R2
current::R3
Expand All @@ -14,7 +14,7 @@ PointCoil(R, Z) = PointCoil(R, Z, 0.0)
Parallelogram coil with the R, Z, ΔR, ΔZ, θ₁, θ₂ formalism (as used by EFIT, for example)
Here θ₁ and θ₂ are the shear angles along the x- and y-axes, respectively, in degrees.
"""
mutable struct ParallelogramCoil{R1<:Real, R2<:Real, R3<:Real, R4<:Real, R5<:Real, R6<:Real, UNR<:Union{Nothing,Real}, R7<:Real} <: AbstractCoil
mutable struct ParallelogramCoil{R1<:Real,R2<:Real,R3<:Real,R4<:Real,R5<:Real,R6<:Real,UNR<:Union{Nothing,Real},R7<:Real} <: AbstractCoil
R::R1
Z::R2
ΔR::R3
Expand All @@ -31,7 +31,7 @@ function ParallelogramCoil(R::Real, Z::Real, ΔR::Real, ΔZ::Real, θ₁::Real,
return ParallelogramCoil(R, Z, ΔR, ΔZ, θ₁, θ₂, spacing)
end

mutable struct DistributedCoil{AVR1<:AbstractVector{<:Real}, AVR2<:AbstractVector{<:Real}, R1<:Real} <: AbstractCoil
mutable struct DistributedCoil{AVR1<:AbstractVector{<:Real},AVR2<:AbstractVector{<:Real},R1<:Real} <: AbstractCoil
R::AVR1
Z::AVR2
current::R1
Expand Down Expand Up @@ -172,8 +172,8 @@ end
# ========== #
# Physics #
# ========== #
function fixed_boundary(EQfixed::Equilibrium.AbstractEquilibrium)
Sb = boundary(EQfixed)
function fixed_boundary(EQfixed::MXHEquilibrium.AbstractEquilibrium)
Sb = MXHEquilibrium.plasma_boundary(EQfixed; precision=0.0)
Rb, Zb = Sb.r, Sb.z
Lb = cumlength(Rb, Zb)

Expand All @@ -195,15 +195,15 @@ end
Calculate ψ from image currents on boundary at surface p near boundary
"""
function ψp_on_fixed_eq_boundary(
EQfixed::Equilibrium.AbstractEquilibrium,
EQfixed::MXHEquilibrium.AbstractEquilibrium,
fixed_coils::AbstractVector{<:AbstractCoil}=AbstractCoil[],
ψbound::Real=0.0;
Rx::AbstractVector{<:Real}=Real[],
Zx::AbstractVector{<:Real}=Real[],
fraction_inside::Union{Nothing,Real}=0.999)

ψ0, ψb = psi_limits(EQfixed)
ψb = psi_boundary(EQfixed)
ψb = psi_boundary(EQfixed; precision=0.0)

# ψp is the flux from the image currents on the plasma boundary
# which is equal and opposite to the flux from the plasma current
Expand Down Expand Up @@ -264,7 +264,7 @@ function field_null_on_boundary(ψp_constant::Real,
# add in desired boundary flux
ψbound != 0.0 && (ψp_constant .+= ψbound)

Bp_fac = Equilibrium.cocos(cocos).sigma_Bp * (2π)^Equilibrium.cocos(cocos).exp_Bp
Bp_fac = MXHEquilibrium.cocos(cocos).sigma_Bp * (2π)^MXHEquilibrium.cocos(cocos).exp_Bp

# Compute flux from fixed coils and subtract from ψp to match
ψfixed = zeros(length(Rp))
Expand Down Expand Up @@ -328,7 +328,7 @@ function currents_to_match_ψp(
end

function fixed_eq_currents(
EQfixed::Equilibrium.AbstractEquilibrium,
EQfixed::MXHEquilibrium.AbstractEquilibrium,
coils::AbstractVector{<:AbstractCoil},
fixed_coils::AbstractVector{<:AbstractCoil}=AbstractCoil[],
ψbound::Real=0.0;
Expand All @@ -345,15 +345,15 @@ end
# ***************************************************
"""
fixed2free(
EQfixed::Equilibrium.AbstractEquilibrium,
EQfixed::MXHEquilibrium.AbstractEquilibrium,
n_point_coils::Integer,
R::AbstractVector,
Z::AbstractVector)
Distribute n point coils around fixed boundary plasma to get a free boundary ψ map
"""
function fixed2free(
EQfixed::Equilibrium.AbstractEquilibrium,
EQfixed::MXHEquilibrium.AbstractEquilibrium,
n_coils::Integer;
Rx::AbstractVector{<:Real}=Real[],
Zx::AbstractVector{<:Real}=Real[],
Expand All @@ -363,35 +363,28 @@ function fixed2free(

coils = encircling_coils(EQfixed, n_coils)
Bp_fac, ψp, Rp, Zp = ψp_on_fixed_eq_boundary(EQfixed, coils; fraction_inside, Rx, Zx)
currents_to_match_ψp(Bp_fac, ψp, Rp, Zp, coils; λ_regularize=1E-15)
currents_to_match_ψp(Bp_fac, ψp, Rp, Zp, coils; λ_regularize=1E-12)
return transpose(fixed2free(EQfixed, coils, Rgrid, Zgrid))
end

function encircling_coils(EQfixed::Equilibrium.AbstractEquilibrium, n_coils::Integer)
bnd = Equilibrium.boundary(EQfixed)
R0 = sum(bnd.r) / length(bnd.r)
Z0 = sum(bnd.z) / length(bnd.z)
t = LinRange(0, 2π, n_coils + 1)[1:n_coils]
a = R0 * 0.99
b = (maximum(bnd.z) - minimum(bnd.z)) * 1.5
b = max(a, b)
a = min(a, b)
coils_r = a .* cos.(t) .+ R0
coils_z = b .* sin.(t) .+ Z0
return [PointCoil(r, z) for (r, z) in zip(coils_r, coils_z)]
function encircling_coils(EQfixed::MXHEquilibrium.AbstractEquilibrium, n_coils::Integer)
bnd = MXHEquilibrium.plasma_boundary(EQfixed; precision=0.0)
mxh = MillerExtendedHarmonic.MXH(bnd.r, bnd.z, 2)
mxh.ϵ = 0.9
Θ = LinRange(0, 2π, n_coils)[1:end-1]
return [PointCoil(r, z) for (r, z) in mxh.(Θ)]
end

function fixed2free(
EQfixed::Equilibrium.AbstractEquilibrium,
EQfixed::MXHEquilibrium.AbstractEquilibrium,
coils::AbstractVector{<:AbstractCoil},
R::AbstractVector{<:Real},
Z::AbstractVector{<:Real};
tp=Float64)

ψb = psi_boundary(EQfixed)
ψb, Sb = MXHEquilibrium.plasma_boundary_psi(EQfixed; precision=0.0)
Bp_fac = EQfixed.cocos.sigma_Bp * (2π)^EQfixed.cocos.exp_Bp

Sb = boundary(EQfixed)
ψ_f2f = [in_boundary(Sb, (r, z)) ? tp(EQfixed(r, z)) : ψb for z in Z, r in R]

Rb, Zb, Lb, dψdn_R = fixed_boundary(EQfixed)
Expand All @@ -414,7 +407,7 @@ function fixed2free(
end

function fixed2free(
EQfixed::Equilibrium.AbstractEquilibrium,
EQfixed::MXHEquilibrium.AbstractEquilibrium,
coils::AbstractVector{<:ParallelogramCoil},
R::AbstractVector{<:Real},
Z::AbstractVector{<:Real};
Expand Down Expand Up @@ -456,7 +449,7 @@ function check_fixed_eq_currents(
# ψ from fixed-boundary gEQDSK
# make ψ at boundary zero, and very small value outside for plotting
ψ0_fix, ψb_fix = psi_limits(EQfixed)
ψb_fix = psi_boundary(EQfixed)
ψb_fix = psi_boundary(EQfixed; precision=0.0)
σ₀ = sign(ψ0_fix - ψb_fix)
ψ_fix = [EQfixed(r, z) for z in Z, r in R] .- ψb_fix
ψ_fix = ifelse.(σ₀ * ψ_fix .> 0, ψ_fix, 1e-6 * ψ_fix)
Expand Down Expand Up @@ -487,7 +480,7 @@ function check_fixed_eq_currents(
else
# Heat maps for free, fix, fix->free, and difference
# ψ from free-boundary gEQDSK
ψb_free = psi_boundary(EQfree)
ψb_free = psi_boundary(EQfree; precision=0.0)
ψ_free = [EQfree(r, z) for z in Z, r in R]
offset = ψb_free - ψb_fix

Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using VacuumFields
using Test
using EFIT
using Equilibrium
using MXHEquilibrium
import DelimitedFiles
using Plots

Expand Down

0 comments on commit 65f24f4

Please sign in to comment.