Skip to content

Commit

Permalink
bug fixes (#273)
Browse files Browse the repository at this point in the history
* filter kwargs

* fix failing simulation reset

* fix reset!

* enable simulaiton re-run without reset

* formatter

* fix base tests

* fix tests
  • Loading branch information
jd-lara authored Sep 8, 2022
1 parent 3eddbc3 commit 1853567
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 45 deletions.
63 changes: 62 additions & 1 deletion src/base/definitions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ const SIMULATION_ACCEPTED_KWARGS = [
const GLOBAL_VAR_SYS_FREQ_INDEX = 1
get_vars_ix() = Dict{Int, Int}(GLOBAL_VAR_SYS_FREQ_INDEX => -1)

const SMALL_SIGNAL_ACCEPTED_KWARGS = [:reset_simulation!]
const RELAXED_NLSOLVE_F_TOLERANCE = :1e-6
const STRICT_NLSOLVE_F_TOLERANCE = :1e-9
const NLSOLVE_X_TOLERANCE = :1e-9
Expand All @@ -136,6 +135,68 @@ const BOUNDS_TOLERANCE = 1e-6
const SIMULATION_LOG_FILENAME = "power-simulations-dynamics.log"

const ACCEPTED_CONTROL_REFS = [:V_ref, :ω_ref, :P_ref, :Q_ref]
const DIFFEQ_SOLVE_KWARGS = [
:dense,
:saveat,
:save_idxs,
:tstops,
:d_discontinuities,
:save_everystep,
:save_on,
:save_start,
:save_end,
:initialize_save,
:adaptive,
:abstol,
:reltol,
:dt,
:dtmax,
:dtmin,
:force_dtmin,
:internalnorm,
:controller,
:gamma,
:beta1,
:beta2,
:qmax,
:qmin,
:qsteady_min,
:qsteady_max,
:qoldinit,
:failfactor,
:calck,
:alias_u0,
:maxiters,
:callback,
:isoutofdomain,
:unstable_check,
:verbose,
:merge_callbacks,
:progress,
:progress_steps,
:progress_name,
:progress_message,
:timeseries_errors,
:dense_errors,
:weak_timeseries_errors,
:weak_dense_errors,
:calculate_errors,
:initializealg,
:alg,
:save_noise,
:delta,
:seed,
:alg_hints,
:kwargshandle,
:trajectories,
:batch_size,
:sensealg,
:advance_to_tstop,
:stop_at_next_tstop,
:default_set,
:second_time,
:prob_choice,
]
"""
Defines the status of the simulation object
"""
Expand Down
2 changes: 1 addition & 1 deletion src/base/jacobian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function get_jacobian(
) where {T <: SimulationModel}
# Deepcopy avoid system modifications
simulation_system = deepcopy(system)
inputs = SimulationInputs(T, simulation_system, ReferenceBus)
inputs = SimulationInputs(T, simulation_system, ReferenceBus())
x0_init = get_flat_start(inputs)
set_operating_point!(x0_init, inputs, system)
return get_jacobian(T, inputs, x0_init, sparse_retrieve_loop)
Expand Down
46 changes: 25 additions & 21 deletions src/base/simulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mutable struct Simulation{T <: SimulationModel}
console_level::Base.CoreLogging.LogLevel
file_level::Base.CoreLogging.LogLevel
multimachine::Bool
frequency_reference::Union{ConstantFrequency, ReferenceBus}
end

get_system(sim::Simulation) = sim.sys
Expand All @@ -31,6 +32,7 @@ function Simulation(
simulation_folder,
console_level,
file_level,
frequency_reference,
) where {T <: SimulationModel}
PSY.set_units_base_system!(sys, "DEVICE_BASE")

Expand All @@ -50,6 +52,7 @@ function Simulation(
console_level,
file_level,
false,
frequency_reference,
)
end

Expand Down Expand Up @@ -123,6 +126,7 @@ function Simulation!(
perturbations = perturbations,
console_level = get(kwargs, :console_level, Logging.Warn),
file_level = get(kwargs, :file_level, Logging.Info),
frequency_reference = get(kwargs, :frequency_reference, ReferenceBus()),
)

build!(sim; kwargs...)
Expand Down Expand Up @@ -181,6 +185,7 @@ function Simulation(
perturbations = perturbations,
console_level = get(kwargs, :console_level, Logging.Warn),
file_level = get(kwargs, :file_level, Logging.Info),
frequency_reference = get(kwargs, :frequency_reference, ReferenceBus()),
)
build!(sim; kwargs...)
if get(kwargs, :system_to_file, false)
Expand All @@ -191,7 +196,8 @@ end

function reset!(sim::Simulation{T}) where {T <: SimulationModel}
@info "Rebuilding the simulation after reset"
sim.inputs = SimulationInputs(T(), get_system(sim), sim.inputs.tspan)
sim.inputs = SimulationInputs(T, get_system(sim), sim.frequency_reference)
sim.status = BUILD_INCOMPLETE
build!(sim)
@info "Simulation reset to status $(sim.status)"
return
Expand All @@ -212,12 +218,9 @@ function configure_logging(sim::Simulation, file_mode; kwargs...)
)
end

function _build_inputs!(
sim::Simulation{T},
frequency_reference,
) where {T <: SimulationModel}
function _build_inputs!(sim::Simulation{T}) where {T <: SimulationModel}
simulation_system = get_system(sim)
sim.inputs = SimulationInputs(T, simulation_system, frequency_reference)
sim.inputs = SimulationInputs(T, simulation_system, sim.frequency_reference)
@debug "Simulation Inputs Created"
return
end
Expand Down Expand Up @@ -268,9 +271,7 @@ function _pre_initialize_simulation!(sim::Simulation)
)
end
else
@warn(
"No Pre-initialization conducted. If this is unexpected, check the initialization keywords"
)
@warn("Using existing initial conditions value for simulation initialization")
sim.status = SIMULATION_INITIALIZED
end
return
Expand Down Expand Up @@ -386,9 +387,7 @@ function _build!(sim::Simulation{T}; kwargs...) where {T <: SimulationModel}
end
end
TimerOutputs.@timeit BUILD_TIMER "Build Simulation Inputs" begin
f_ref = get(kwargs, :frequency_reference, ReferenceBus)
_build_inputs!(sim, f_ref)
# TODO: Update and store f_ref somewhere.
_build_inputs!(sim)
sim.multimachine =
get_global_vars_update_pointers(sim.inputs)[GLOBAL_VAR_SYS_FREQ_INDEX] !=
0
Expand Down Expand Up @@ -445,18 +444,19 @@ function build!(sim; kwargs...)
return sim.status
end

function simulation_pre_step!(sim::Simulation, reset_sim::Bool)
function simulation_pre_step!(sim::Simulation)
if sim.status == BUILD_FAILED
error(
"The Simulation status is $(sim.status). Can not continue, correct your inputs and build the simulation again.",
)
elseif sim.status != BUILT && !reset_sim
error(
"The Simulation status is $(sim.status). Use keyword argument reset_simulation = true",
)
elseif sim.status == BUILT
@debug "Simulation status is $(sim.status)."
elseif sim.status == SIMULATION_FINALIZED
reset!(sim)
@info "The Simulation status is $(sim.status). Resetting the simulation"
else
error("Simulation status is $(sim.status). Can't continue.")
end

reset_sim && reset!(sim)
return
end

Expand All @@ -466,9 +466,13 @@ function _prog_meter_enabled()
(get(ENV, "RUNNING_PSID_TESTS", nothing) != "true")
end

function _filter_kwargs(kwargs)
return Dict(k => v for (k, v) in kwargs if in(k, DIFFEQ_SOLVE_KWARGS))
end

function _execute!(sim::Simulation, solver; kwargs...)
@debug "status before execute" sim.status
simulation_pre_step!(sim, get(kwargs, :reset_simulation, false))
simulation_pre_step!(sim)
sim.status = SIMULATION_STARTED
time_log = Dict{Symbol, Any}()
if get(kwargs, :auto_abstol, false)
Expand All @@ -490,7 +494,7 @@ function _execute!(sim::Simulation, solver; kwargs...)
progress_steps = 1,
advance_to_tstop = !isempty(sim.tstops),
initializealg = SciMLBase.NoInit(),
kwargs...,
_filter_kwargs(kwargs)...,
)
if solution.retcode == :Success
sim.status = SIMULATION_FINALIZED
Expand Down
14 changes: 11 additions & 3 deletions src/base/simulation_inputs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct SimulationInputs

function SimulationInputs(
sys::PSY.System,
::Type{T},
::T,
) where {T <: Union{ConstantFrequency, ReferenceBus}}
n_buses = get_n_buses(sys)
Ybus, lookup = _get_ybus(sys)
Expand Down Expand Up @@ -125,14 +125,22 @@ end
"""
SimulationInputs build function for MassMatrixModels
"""
function SimulationInputs(::Type{MassMatrixModel}, sys::PSY.System, frequency_reference)
function SimulationInputs(
::Type{MassMatrixModel},
sys::PSY.System,
frequency_reference::Union{ConstantFrequency, ReferenceBus},
)
return SimulationInputs(sys, frequency_reference)
end

"""
SimulationInputs build function for ResidualModels
"""
function SimulationInputs(::Type{ResidualModel}, sys::PSY.System, frequency_reference)
function SimulationInputs(
::Type{ResidualModel},
sys::PSY.System,
frequency_reference::Union{ConstantFrequency, ReferenceBus},
)
return SimulationInputs(sys, frequency_reference)
end

Expand Down
Loading

0 comments on commit 1853567

Please sign in to comment.