Skip to content

Commit

Permalink
Merge pull request #21 from ProjectTorreyPines/optimize_regularization
Browse files Browse the repository at this point in the history
add optimizaiton of the \lambda regularization
  • Loading branch information
orso82 committed Sep 21, 2023
2 parents ddcb530 + e22f781 commit fbda817
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/coil_currents.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function ψp_on_fixed_eq_boundary(
ψbound::Real=0.0;
Rx::AbstractVector{T}=Float64[],
Zx::AbstractVector{T}=Float64[],
fraction_inside::Union{Nothing,<:Real}=0.999) where {T<:Real}
fraction_inside::Union{Nothing,<:Real}) where {T<:Real}

ψ0, ψb = MXHEquilibrium.psi_limits(EQfixed)
ψb, Sb = MXHEquilibrium.plasma_boundary_psi(EQfixed; precision=0.0)
Expand Down Expand Up @@ -243,7 +243,7 @@ end
function ψp_on_fixed_eq_boundary(shot::TEQUILA.Shot, fixed_coils::AbstractVector{<:AbstractCoil}=AbstractCoil[], ψbound::Real=0.0;
Rx::AbstractVector{T}=Float64[],
Zx::AbstractVector{T}=Float64[],
fraction_inside::Union{Nothing,<:Real}=0.999) where {T<:Real}
fraction_inside::Union{Nothing,<:Real}) where {T<:Real}

Sb = @views MillerExtendedHarmonic.MXH(shot.surfaces[:, end])

Expand All @@ -253,7 +253,7 @@ function ψp_on_fixed_eq_boundary(shot::TEQUILA.Shot, fixed_coils::AbstractVecto
# ψ₁ = ψp + ψcoil
# ψcoil = (ψ₁ - ψ₀) + ψim
Sp = MillerExtendedHarmonic.MXH(shot, fraction_inside)
Np = 100 * length(Sp.c)
Np = 99
Nx = length(Rx)

Rp = zeros(Np + Nx)
Expand Down Expand Up @@ -384,10 +384,11 @@ function fixed_eq_currents(
fixed_coils::AbstractVector{<:AbstractCoil}=AbstractCoil[],
ψbound::Real=0.0;
λ_regularize::Float64=1E-16,
return_cost::Bool=false)
return_cost::Bool=false,
fraction_inside::Union{Nothing,<:Real}=1.0 - 1E-6)

Bp_fac, ψp, Rp, Zp = ψp_on_fixed_eq_boundary(EQfixed, fixed_coils, ψbound)
return currents_to_match_ψp(Bp_fac, ψp, Rp, Zp, coils; λ_regularize, return_cost)
return currents_to_match_ψp(Bp_fac, ψp, Rp, Zp, coils; fraction_inside, λ_regularize, return_cost)
end


Expand All @@ -408,7 +409,7 @@ function fixed2free(
n_coils::Integer;
Rx::AbstractVector{T}=Float64[],
Zx::AbstractVector{T}=Float64[],
fraction_inside::Union{Nothing,<:Real}=0.999,
fraction_inside::Union{Nothing,<:Real}=1.0 - 1E-6,
Rgrid::AbstractVector{Float64}=EQfixed.r,
Zgrid::AbstractVector{Float64}=EQfixed.z) where {T<:Real}

Expand All @@ -419,10 +420,20 @@ function fixed2free(
ψbound = MXHEquilibrium.psi_boundary(EQfixed)
end
Bp_fac, ψp, Rp, Zp = ψp_on_fixed_eq_boundary(EQfixed, coils, ψbound; fraction_inside, Rx, Zx)
currents_to_match_ψp(Bp_fac, ψp, Rp, Zp, coils; λ_regularize=1E-14)

# Finds the λ_regularize to match the boundary the closest in a simple scan
λ_range_exp = collect(-20:0.5:-10)
cost_λ = [costfixed2free(λ, Bp_fac, ψp, Rp, Zp, coils) for λ in λ_range_exp]

currents_to_match_ψp(Bp_fac, ψp, Rp, Zp, coils; λ_regularize=10^λ_range_exp[argmin(cost_λ)])
return transpose(fixed2free(EQfixed, coils, Rgrid, Zgrid))
end

function costfixed2free(λ_reg::Float64, Bp_fac, ψp, Rp, Zp, coils)
c = currents_to_match_ψp(Bp_fac, ψp, Rp, Zp, coils; λ_regularize=10^(λ_reg), return_cost=true)[2]
return c^2
end

function fixed2free(shot::TEQUILA.Shot, n_coils::Integer; n_grid=10 * length(shot.ρ), kwargs...)
R0 = shot.surfaces[1, end]
Z0 = shot.surfaces[2, end]
Expand All @@ -443,12 +454,18 @@ function fixed2free(
Zgrid::AbstractVector{Float64};
Rx::AbstractVector{T}=Float64[],
Zx::AbstractVector{T}=Float64[],
fraction_inside::Union{Nothing,<:Real}=0.999,
fraction_inside::Union{Nothing,<:Real}=1.0 - 1E-6,
ψbound::Real=0.0) where {T<:Real}

coils = encircling_coils(shot, n_coils)
Bp_fac, ψp, Rp, Zp = ψp_on_fixed_eq_boundary(shot, coils, ψbound; fraction_inside, Rx, Zx)
currents_to_match_ψp(Bp_fac, ψp, Rp, Zp, coils; λ_regularize=1E-14)

# Finds the λ_regularize to match the boundary the closest in a simple scan
λ_range_exp = collect(-20:0.5:-10)
cost_λ = [costfixed2free(λ, Bp_fac, ψp, Rp, Zp, coils) for λ in λ_range_exp]

currents_to_match_ψp(Bp_fac, ψp, Rp, Zp, coils; λ_regularize=10^λ_range_exp[argmin(cost_λ)])

return transpose(fixed2free(shot, coils, Rgrid, Zgrid; ψbound))
end

Expand Down

0 comments on commit fbda817

Please sign in to comment.