Skip to content

Commit 337921e

Browse files
committed
fixed pedestal finder function
mention @TimSlendebroek
1 parent 55d2b68 commit 337921e

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

src/physics/pedestal.jl

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -186,25 +186,46 @@ Finds the pedetal height and width using the EPED1 definition
186186
returns ped_height, ped_width
187187
"""
188188
function pedestal_finder(profile::Vector{T}, psi_norm::Vector{T}) where {T<:Real}
189-
function cost_function(profile, weight_func, params)
190-
if any(x -> (x < 0.0), params)
191-
return 1e10
192-
end
193-
profile_fit = Hmode_profiles(profile[end], params[1], profile[1], length(profile), 2.0, 2.0, params[2])
194-
return sqrt(sum(((profile .- profile_fit) .^ 2 ./ profile[1]^2) .* weight_func))
195-
end
196-
ngrid = length(profile)
197-
half_grid = Int(floor(ngrid / 2))
189+
function cost_function(profile, params)
190+
params = abs.(params)
191+
height = params[1]
192+
width = min(max(0.0, params[2]), 0.5)
193+
expin = params[3]
194+
expout = params[4]
195+
psi_norm_fit = range(0, 1, length(profile))
198196

199-
inversion_point = argmin(gradient(psi_norm[half_grid:end], profile[half_grid:end])) + half_grid
200-
inversion_point_margin = inversion_point - Int(floor(0.1 * ngrid))
197+
tmp = Hmode_profiles(profile[end], height, profile[1], length(profile), expin, expout, width)
198+
profile_fit = interp1d(psi_norm_fit, tmp).(psi_norm)
201199

202-
weight_func = zeros(ngrid)
203-
weight_func[inversion_point_margin:end] .+= 1.0
200+
p1 = abs.(profile .- profile_fit) ./ maximum(profile)
201+
p2 = abs.(gradient(psi_norm, profile) .- gradient(psi_norm, profile_fit)) ./ maximum(abs.(gradient(psi_norm, profile)))
204202

205-
width0 = 1.0 - psi_norm[inversion_point]
206-
guess = [interp1d(psi_norm, profile)(1.0 - 2.0 * width0), width0]
207-
res = Optim.optimize(params -> cost_function(profile, weight_func, params), guess, Optim.NelderMead(), Optim.Options(; g_tol=1E-5))
203+
return sum(((p1 .+ p2) .* weight) .^ 2.0)
204+
end
208205

209-
return res.minimizer
206+
weight = gradient(psi_norm)
207+
208+
width0 = 0.05
209+
height0 = interp1d(psi_norm, profile)(1.0 - width0)
210+
expin0 = 1.0
211+
expout0 = 1.0
212+
guess = [height0, width0, expin0, expout0]
213+
res = Optim.optimize(params -> cost_function(profile, params), guess, Optim.NelderMead(), Optim.Options(; g_tol=1E-5))
214+
215+
params = abs.(res.minimizer)
216+
height = params[1]
217+
width = min(max(0.0, params[2]), 0.5)
218+
expin = params[3]
219+
expout = params[4]
220+
221+
# psi_norm_fit = range(0, 1, length(profile_fit))
222+
# profile_fit = Hmode_profiles(profile[end], height, profile[1], length(profile), expin, expout, width)
223+
# p = plot(psi_norm, profile)
224+
# plot!(p, psi_norm_fit, profile_fit)
225+
# hline!([height])
226+
# vline!([1.0 .- width])
227+
# scatter!(p, [1.0 - width], [interp1d(psi_norm_fit, profile_fit)(1.0 - width)])
228+
# display(p)
229+
230+
return height, width
210231
end

0 commit comments

Comments
 (0)