Skip to content

Commit 1853567

Browse files
authored
bug fixes (#273)
* filter kwargs * fix failing simulation reset * fix reset! * enable simulaiton re-run without reset * formatter * fix base tests * fix tests
1 parent 3eddbc3 commit 1853567

File tree

5 files changed

+161
-45
lines changed

5 files changed

+161
-45
lines changed

src/base/definitions.jl

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ const SIMULATION_ACCEPTED_KWARGS = [
124124
const GLOBAL_VAR_SYS_FREQ_INDEX = 1
125125
get_vars_ix() = Dict{Int, Int}(GLOBAL_VAR_SYS_FREQ_INDEX => -1)
126126

127-
const SMALL_SIGNAL_ACCEPTED_KWARGS = [:reset_simulation!]
128127
const RELAXED_NLSOLVE_F_TOLERANCE = :1e-6
129128
const STRICT_NLSOLVE_F_TOLERANCE = :1e-9
130129
const NLSOLVE_X_TOLERANCE = :1e-9
@@ -136,6 +135,68 @@ const BOUNDS_TOLERANCE = 1e-6
136135
const SIMULATION_LOG_FILENAME = "power-simulations-dynamics.log"
137136

138137
const ACCEPTED_CONTROL_REFS = [:V_ref, :ω_ref, :P_ref, :Q_ref]
138+
const DIFFEQ_SOLVE_KWARGS = [
139+
:dense,
140+
:saveat,
141+
:save_idxs,
142+
:tstops,
143+
:d_discontinuities,
144+
:save_everystep,
145+
:save_on,
146+
:save_start,
147+
:save_end,
148+
:initialize_save,
149+
:adaptive,
150+
:abstol,
151+
:reltol,
152+
:dt,
153+
:dtmax,
154+
:dtmin,
155+
:force_dtmin,
156+
:internalnorm,
157+
:controller,
158+
:gamma,
159+
:beta1,
160+
:beta2,
161+
:qmax,
162+
:qmin,
163+
:qsteady_min,
164+
:qsteady_max,
165+
:qoldinit,
166+
:failfactor,
167+
:calck,
168+
:alias_u0,
169+
:maxiters,
170+
:callback,
171+
:isoutofdomain,
172+
:unstable_check,
173+
:verbose,
174+
:merge_callbacks,
175+
:progress,
176+
:progress_steps,
177+
:progress_name,
178+
:progress_message,
179+
:timeseries_errors,
180+
:dense_errors,
181+
:weak_timeseries_errors,
182+
:weak_dense_errors,
183+
:calculate_errors,
184+
:initializealg,
185+
:alg,
186+
:save_noise,
187+
:delta,
188+
:seed,
189+
:alg_hints,
190+
:kwargshandle,
191+
:trajectories,
192+
:batch_size,
193+
:sensealg,
194+
:advance_to_tstop,
195+
:stop_at_next_tstop,
196+
:default_set,
197+
:second_time,
198+
:prob_choice,
199+
]
139200
"""
140201
Defines the status of the simulation object
141202
"""

src/base/jacobian.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ function get_jacobian(
152152
) where {T <: SimulationModel}
153153
# Deepcopy avoid system modifications
154154
simulation_system = deepcopy(system)
155-
inputs = SimulationInputs(T, simulation_system, ReferenceBus)
155+
inputs = SimulationInputs(T, simulation_system, ReferenceBus())
156156
x0_init = get_flat_start(inputs)
157157
set_operating_point!(x0_init, inputs, system)
158158
return get_jacobian(T, inputs, x0_init, sparse_retrieve_loop)

src/base/simulation.jl

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mutable struct Simulation{T <: SimulationModel}
1414
console_level::Base.CoreLogging.LogLevel
1515
file_level::Base.CoreLogging.LogLevel
1616
multimachine::Bool
17+
frequency_reference::Union{ConstantFrequency, ReferenceBus}
1718
end
1819

1920
get_system(sim::Simulation) = sim.sys
@@ -31,6 +32,7 @@ function Simulation(
3132
simulation_folder,
3233
console_level,
3334
file_level,
35+
frequency_reference,
3436
) where {T <: SimulationModel}
3537
PSY.set_units_base_system!(sys, "DEVICE_BASE")
3638

@@ -50,6 +52,7 @@ function Simulation(
5052
console_level,
5153
file_level,
5254
false,
55+
frequency_reference,
5356
)
5457
end
5558

@@ -123,6 +126,7 @@ function Simulation!(
123126
perturbations = perturbations,
124127
console_level = get(kwargs, :console_level, Logging.Warn),
125128
file_level = get(kwargs, :file_level, Logging.Info),
129+
frequency_reference = get(kwargs, :frequency_reference, ReferenceBus()),
126130
)
127131

128132
build!(sim; kwargs...)
@@ -181,6 +185,7 @@ function Simulation(
181185
perturbations = perturbations,
182186
console_level = get(kwargs, :console_level, Logging.Warn),
183187
file_level = get(kwargs, :file_level, Logging.Info),
188+
frequency_reference = get(kwargs, :frequency_reference, ReferenceBus()),
184189
)
185190
build!(sim; kwargs...)
186191
if get(kwargs, :system_to_file, false)
@@ -191,7 +196,8 @@ end
191196

192197
function reset!(sim::Simulation{T}) where {T <: SimulationModel}
193198
@info "Rebuilding the simulation after reset"
194-
sim.inputs = SimulationInputs(T(), get_system(sim), sim.inputs.tspan)
199+
sim.inputs = SimulationInputs(T, get_system(sim), sim.frequency_reference)
200+
sim.status = BUILD_INCOMPLETE
195201
build!(sim)
196202
@info "Simulation reset to status $(sim.status)"
197203
return
@@ -212,12 +218,9 @@ function configure_logging(sim::Simulation, file_mode; kwargs...)
212218
)
213219
end
214220

215-
function _build_inputs!(
216-
sim::Simulation{T},
217-
frequency_reference,
218-
) where {T <: SimulationModel}
221+
function _build_inputs!(sim::Simulation{T}) where {T <: SimulationModel}
219222
simulation_system = get_system(sim)
220-
sim.inputs = SimulationInputs(T, simulation_system, frequency_reference)
223+
sim.inputs = SimulationInputs(T, simulation_system, sim.frequency_reference)
221224
@debug "Simulation Inputs Created"
222225
return
223226
end
@@ -268,9 +271,7 @@ function _pre_initialize_simulation!(sim::Simulation)
268271
)
269272
end
270273
else
271-
@warn(
272-
"No Pre-initialization conducted. If this is unexpected, check the initialization keywords"
273-
)
274+
@warn("Using existing initial conditions value for simulation initialization")
274275
sim.status = SIMULATION_INITIALIZED
275276
end
276277
return
@@ -386,9 +387,7 @@ function _build!(sim::Simulation{T}; kwargs...) where {T <: SimulationModel}
386387
end
387388
end
388389
TimerOutputs.@timeit BUILD_TIMER "Build Simulation Inputs" begin
389-
f_ref = get(kwargs, :frequency_reference, ReferenceBus)
390-
_build_inputs!(sim, f_ref)
391-
# TODO: Update and store f_ref somewhere.
390+
_build_inputs!(sim)
392391
sim.multimachine =
393392
get_global_vars_update_pointers(sim.inputs)[GLOBAL_VAR_SYS_FREQ_INDEX] !=
394393
0
@@ -445,18 +444,19 @@ function build!(sim; kwargs...)
445444
return sim.status
446445
end
447446

448-
function simulation_pre_step!(sim::Simulation, reset_sim::Bool)
447+
function simulation_pre_step!(sim::Simulation)
449448
if sim.status == BUILD_FAILED
450449
error(
451450
"The Simulation status is $(sim.status). Can not continue, correct your inputs and build the simulation again.",
452451
)
453-
elseif sim.status != BUILT && !reset_sim
454-
error(
455-
"The Simulation status is $(sim.status). Use keyword argument reset_simulation = true",
456-
)
452+
elseif sim.status == BUILT
453+
@debug "Simulation status is $(sim.status)."
454+
elseif sim.status == SIMULATION_FINALIZED
455+
reset!(sim)
456+
@info "The Simulation status is $(sim.status). Resetting the simulation"
457+
else
458+
error("Simulation status is $(sim.status). Can't continue.")
457459
end
458-
459-
reset_sim && reset!(sim)
460460
return
461461
end
462462

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

469+
function _filter_kwargs(kwargs)
470+
return Dict(k => v for (k, v) in kwargs if in(k, DIFFEQ_SOLVE_KWARGS))
471+
end
472+
469473
function _execute!(sim::Simulation, solver; kwargs...)
470474
@debug "status before execute" sim.status
471-
simulation_pre_step!(sim, get(kwargs, :reset_simulation, false))
475+
simulation_pre_step!(sim)
472476
sim.status = SIMULATION_STARTED
473477
time_log = Dict{Symbol, Any}()
474478
if get(kwargs, :auto_abstol, false)
@@ -490,7 +494,7 @@ function _execute!(sim::Simulation, solver; kwargs...)
490494
progress_steps = 1,
491495
advance_to_tstop = !isempty(sim.tstops),
492496
initializealg = SciMLBase.NoInit(),
493-
kwargs...,
497+
_filter_kwargs(kwargs)...,
494498
)
495499
if solution.retcode == :Success
496500
sim.status = SIMULATION_FINALIZED

src/base/simulation_inputs.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct SimulationInputs
2020

2121
function SimulationInputs(
2222
sys::PSY.System,
23-
::Type{T},
23+
::T,
2424
) where {T <: Union{ConstantFrequency, ReferenceBus}}
2525
n_buses = get_n_buses(sys)
2626
Ybus, lookup = _get_ybus(sys)
@@ -125,14 +125,22 @@ end
125125
"""
126126
SimulationInputs build function for MassMatrixModels
127127
"""
128-
function SimulationInputs(::Type{MassMatrixModel}, sys::PSY.System, frequency_reference)
128+
function SimulationInputs(
129+
::Type{MassMatrixModel},
130+
sys::PSY.System,
131+
frequency_reference::Union{ConstantFrequency, ReferenceBus},
132+
)
129133
return SimulationInputs(sys, frequency_reference)
130134
end
131135

132136
"""
133137
SimulationInputs build function for ResidualModels
134138
"""
135-
function SimulationInputs(::Type{ResidualModel}, sys::PSY.System, frequency_reference)
139+
function SimulationInputs(
140+
::Type{ResidualModel},
141+
sys::PSY.System,
142+
frequency_reference::Union{ConstantFrequency, ReferenceBus},
143+
)
136144
return SimulationInputs(sys, frequency_reference)
137145
end
138146

0 commit comments

Comments
 (0)