Skip to content

Commit

Permalink
fixed and improved get_time_dependent()
Browse files Browse the repository at this point in the history
  • Loading branch information
orso82 committed Jan 31, 2025
1 parent 179206a commit 095c9d1
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/ddinit/init_pulse_schedule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,15 @@ function get_time_dependent(par::AbstractParameters, field::Symbol)
end

function get_time_dependent(pars::ParametersVector, field::Symbol)
if isempty(pars)
return Float64[], Float64[]
else
all_times = getfield(pars[1], field).value.time
data = [getfield(par, field).value.data for par in pars]
return all_times, data
all_times = Float64[]

for par in pars
time, data = get_time_dependent(par, field)
append!(all_times, time)
all_times = sort!(unique(all_times))
end

return all_times, [get_time_dependent(par, field, all_times) for par in pars]
end

function get_time_dependent(par::AbstractParameters, fields::Vector{Symbol})
Expand All @@ -220,24 +222,22 @@ function get_time_dependent(par::AbstractParameters, fields::Vector{Symbol})
for field in fields
time, data = get_time_dependent(par, field)
append!(all_times, time)
all_times = sort!(unique(all_times))
end

all_times = sort!(unique(all_times))

return all_times, NamedTuple{Tuple(fields)}([get_time_dependent(par, field, all_times) for field in fields])
end

function get_time_dependent(par::AbstractParameters, field::Symbol, all_times::Vector{Float64})
value = getfield(par, field).value

if typeof(value) <: Union{Function, TimeData}
time = collect(SimulationParameters.time_range(par))
data = value.(time)
if typeof(value) <: TimeData && value.time == all_times
all_data = value.data
elseif typeof(value) <: Union{Function,TimeData}
all_data = data = value.(all_times)
if !(eltype(data) <: Number)
data, mapping = SimulationParameters.encode_array(data)
all_data = [mapping[Int(d)] for d in IMAS.interp1d(time, Float64.(data), :constant).(all_times)]
else
all_data = IMAS.interp1d(time, data, :constant).(all_times)
all_data = [mapping[d] for d in Int.(data)]
end
else
all_data = fill(value, size(all_times))
Expand Down

0 comments on commit 095c9d1

Please sign in to comment.