diff --git a/src/base/definitions.jl b/src/base/definitions.jl index 654b76001..e72214413 100644 --- a/src/base/definitions.jl +++ b/src/base/definitions.jl @@ -231,16 +231,6 @@ Defines the status of the simulation object SIMULATION_FAILED = 7 end -""" -Defines the level of building simulation inputs -""" -@enum BUILD_INPUTS_LEVEL begin - BUILD_ONE = 1 - BUILD_TWO = 2 - BUILD_THREE = 3 - BUILD_NONE = 4 -end - """ Defines the level of initializing simulation """ diff --git a/src/base/jacobian.jl b/src/base/jacobian.jl index a6c6eaa21..d0460ccf5 100644 --- a/src/base/jacobian.jl +++ b/src/base/jacobian.jl @@ -245,7 +245,7 @@ function get_jacobian( ) where {T <: SimulationModel} # Deepcopy avoid system modifications simulation_system = deepcopy(system) - inputs = SimulationInputs(T, simulation_system, ReferenceBus(), nothing, Val(BUILD_ONE)) + inputs = SimulationInputs(T, simulation_system, ReferenceBus()) p = get_parameters(inputs) x0_init = _get_flat_start(inputs) set_operating_point!(x0_init, inputs, system) diff --git a/src/base/simulation.jl b/src/base/simulation.jl index b03815b03..2cec48e34 100644 --- a/src/base/simulation.jl +++ b/src/base/simulation.jl @@ -10,7 +10,7 @@ mutable struct Simulation{T <: SimulationModel} tstops::Vector{Float64} callbacks::Vector simulation_folder::String - build_inputs_level::BUILD_INPUTS_LEVEL + build_inputs::Bool inputs::Union{Nothing, SimulationInputs} inputs_init::Union{Nothing, SimulationInputs} results::Union{Nothing, SimulationResults} @@ -61,7 +61,7 @@ function Simulation( Vector{Float64}(), Vector{SciMLBase.AbstractDiscreteCallback}(), simulation_folder, - BUILD_ONE, + true, nothing, nothing, nothing, @@ -231,20 +231,24 @@ function configure_logging(sim::Simulation, file_mode; kwargs...) ) end -function _build_inputs!(sim::Simulation{T}) where {T <: SimulationModel} +function _build_inputs!(sim::Simulation{T}, ::Val{true}) where {T <: SimulationModel} simulation_system = get_system(sim) sim.inputs = SimulationInputs( T, simulation_system, sim.frequency_reference, - sim.inputs_init, - Val(sim.build_inputs_level), ) sim.inputs_init = deepcopy(sim.inputs) @debug "Simulation Inputs Created" return end +function _build_inputs!(sim::Simulation{T}, ::Val{false}) where {T <: SimulationModel} + sim.inputs = deepcopy(sim.inputs_init) + @debug "Simulation Inputs Copied" + return +end + function _get_flat_start(inputs::SimulationInputs) bus_count = get_bus_count(inputs) var_count = get_variable_count(inputs) @@ -428,7 +432,7 @@ function _build!(sim::Simulation{T}; kwargs...) where {T <: SimulationModel} end end TimerOutputs.@timeit BUILD_TIMER "Build Simulation Inputs" begin - _build_inputs!(sim) + _build_inputs!(sim, Val(sim.build_inputs)) sim.multimachine = get_global_vars_update_pointers(sim.inputs)[GLOBAL_VAR_SYS_FREQ_INDEX] != 0 diff --git a/src/base/simulation_inputs.jl b/src/base/simulation_inputs.jl index 43bc16f3f..2a15e0260 100644 --- a/src/base/simulation_inputs.jl +++ b/src/base/simulation_inputs.jl @@ -1,7 +1,4 @@ -""" -LevelOneInputs are constant unless a device or model is changed within the simulation. -""" -mutable struct LevelOneInputs +mutable struct SimulationInputs dynamic_injectors::Vector{DynamicWrapper{<:PSY.DynamicInjection}} static_injectors::Vector static_loads::Vector @@ -18,41 +15,17 @@ mutable struct LevelOneInputs global_vars_update_pointers::Dict{Int, Int} global_state_map::MAPPING_DICT global_inner_var_map::Dict{String, Dict} -end - -""" -LevelTwoInputs are constant unless a network parameter is changed. -""" -mutable struct LevelTwoInputs ybus_rectangular::SparseArrays.SparseMatrixCSC{Float64, Int} total_shunts::SparseArrays.SparseMatrixCSC{Float64, Int} -end - -""" -LevelTwoInputs are constant unless any parameter is changed. -""" -mutable struct LevelThreeInputs DAE_vector::Vector{Bool} mass_matrix::LinearAlgebra.Diagonal{Float64} parameters::Vector{Float64} delays::Vector end -mutable struct SimulationInputs - level_one_inputs::LevelOneInputs - level_two_inputs::LevelTwoInputs - level_three_inputs::LevelThreeInputs -end - -function LevelOneInputs(sys, ::Any, inputs_init, ::Any) - return inputs_init.level_one_inputs -end - -function LevelOneInputs( - sys, - ::Type{T}, - inputs_init, - ::Val{BUILD_ONE}, +function SimulationInputs( + sys::PSY.System, + ::T, ) where {T <: Union{ConstantFrequency, ReferenceBus}} n_buses = get_n_buses(sys) _, lookup = _get_ybus(sys) @@ -88,101 +61,18 @@ function LevelOneInputs( break end end - - return LevelOneInputs( - wrapped_injectors, - wrapped_static_injectors, - wrapped_loads, - wrapped_branches, - n_vars - 2 * n_buses - n_branch_states, - n_branch_states, - n_vars, - inner_vars_count, - n_buses, - n_parameters, - injection_start:n_vars, - has_dyn_lines, - lookup, - global_vars, - MAPPING_DICT(), - Dict{String, Dict}(), - ) -end - -function LevelTwoInputs(sys, level_one_inputs, inputs_init, ::Any) - return inputs_init.level_two_inputs -end - -function LevelTwoInputs(sys, level_one_inputs, inputs_init, ::Val{BUILD_TWO}) - LevelTwoInputs(sys, level_one_inputs, inputs_init, Val(BUILD_ONE)) -end - -function LevelTwoInputs(sys, level_one_inputs, inputs_init, ::Val{BUILD_ONE}) Ybus, _ = _get_ybus(sys) - wrapped_branches = get_dynamic_branches(level_one_inputs) - n_buses = get_bus_count(level_one_inputs) total_shunts = _make_total_shunts(wrapped_branches, n_buses) - return LevelTwoInputs( - Ybus, - total_shunts, - ) -end -function LevelThreeInputs( - sys, - level_one_inputs::LevelOneInputs, - level_two_inputs::LevelTwoInputs, - inputs_init, - ::Val{BUILD_NONE}, -) - return inputs_init.level_three_inputs -end - -function LevelThreeInputs( - sys, - level_one_inputs::LevelOneInputs, - level_two_inputs::LevelTwoInputs, - inputs_init, - ::Val{BUILD_THREE}, -) - LevelThreeInputs(sys, level_one_inputs, level_two_inputs, inputs_init, Val(BUILD_ONE)) -end - -function LevelThreeInputs( - sys, - level_one_inputs::LevelOneInputs, - level_two_inputs::LevelTwoInputs, - inputs_init, - ::Val{BUILD_TWO}, -) - LevelThreeInputs(sys, level_one_inputs, level_two_inputs, inputs_init, Val(BUILD_ONE)) -end - -function LevelThreeInputs( - sys, - level_one_inputs::LevelOneInputs, - level_two_inputs::LevelTwoInputs, - inputs_init, - ::Val{BUILD_ONE}, -) TimerOutputs.@timeit BUILD_TIMER "Build initial parameters" begin - parameter_count = get_parameter_count(level_one_inputs) - wrapped_branches = get_dynamic_branches(level_one_inputs) - wrapped_injectors = get_dynamic_injectors(level_one_inputs) - wrapped_loads = get_static_loads(level_one_inputs) - wrapped_static_injectors = get_static_injectors(level_one_inputs) - + parameter_count = n_parameters initial_parameters = zeros(parameter_count) _update_initial_parameters!(initial_parameters, wrapped_branches) _update_initial_parameters!(initial_parameters, wrapped_injectors) _update_initial_parameters!(initial_parameters, wrapped_loads) _update_initial_parameters!(initial_parameters, wrapped_static_injectors) end - n_vars = get_variable_count(level_one_inputs) - n_buses = get_bus_count(level_one_inputs) - wrapped_injectors = get_dynamic_injectors(level_one_inputs) mass_matrix = _make_mass_matrix(wrapped_injectors, n_vars, n_buses) DAE_vector = _make_DAE_vector(mass_matrix, n_vars, n_buses) - total_shunts = get_total_shunts(level_two_inputs) _adjust_states!( DAE_vector, mass_matrix, @@ -196,89 +86,59 @@ function LevelThreeInputs( @info "System has delays. Use the correct solver for delay differential equations." end - return LevelThreeInputs( + return SimulationInputs( + wrapped_injectors, + wrapped_static_injectors, + wrapped_loads, + wrapped_branches, + n_vars - 2 * n_buses - n_branch_states, + n_branch_states, + n_vars, + inner_vars_count, + n_buses, + n_parameters, + injection_start:n_vars, + has_dyn_lines, + lookup, + global_vars, + MAPPING_DICT(), + Dict{String, Dict}(), + Ybus, + total_shunts, DAE_vector, mass_matrix, initial_parameters, delays, ) end - -function SimulationInputs( - sys::PSY.System, - ::T, - simulation_inputs_init::Union{Nothing, SimulationInputs}, - build_level, -) where {T <: Union{ConstantFrequency, ReferenceBus}} - level_one_inputs = LevelOneInputs(sys, T, simulation_inputs_init, build_level) - level_two_inputs = - LevelTwoInputs(sys, level_one_inputs, simulation_inputs_init, build_level) - level_three_inputs = LevelThreeInputs( - sys, - level_one_inputs, - level_two_inputs, - simulation_inputs_init, - build_level, - ) - - return SimulationInputs( - level_one_inputs, - level_two_inputs, - level_three_inputs, - ) -end #LEVEL ONE INPUTS -get_dynamic_injectors(inputs::SimulationInputs) = inputs.level_one_inputs.dynamic_injectors -get_dynamic_injectors(inputs::LevelOneInputs) = inputs.dynamic_injectors -get_dynamic_branches(inputs::SimulationInputs) = inputs.level_one_inputs.dynamic_branches -get_dynamic_branches(inputs::LevelOneInputs) = inputs.dynamic_branches -get_static_injectors(inputs::SimulationInputs) = inputs.level_one_inputs.static_injectors -get_static_injectors(inputs::LevelOneInputs) = inputs.static_injectors -get_static_loads(inputs::SimulationInputs) = inputs.level_one_inputs.static_loads -get_static_loads(inputs::LevelOneInputs) = inputs.static_loads -get_lookup(inputs::SimulationInputs) = inputs.level_one_inputs.lookup -get_lookup(inputs::LevelOneInputs) = inputs.lookup -has_dyn_lines(inputs::SimulationInputs) = inputs.level_one_inputs.dyn_lines -has_dyn_lines(inputs::LevelOneInputs) = inputs.dyn_lines +get_dynamic_injectors(inputs::SimulationInputs) = inputs.dynamic_injectors +get_dynamic_branches(inputs::SimulationInputs) = inputs.dynamic_branches +get_static_injectors(inputs::SimulationInputs) = inputs.static_injectors +get_static_loads(inputs::SimulationInputs) = inputs.static_loads +get_lookup(inputs::SimulationInputs) = inputs.lookup +has_dyn_lines(inputs::SimulationInputs) = inputs.dyn_lines get_global_vars_update_pointers(inputs::SimulationInputs) = - inputs.level_one_inputs.global_vars_update_pointers -get_global_vars_update_pointers(inputs::LevelOneInputs) = inputs.global_vars_update_pointers -get_global_state_map(inputs::SimulationInputs) = inputs.level_one_inputs.global_state_map -get_global_state_map(inputs::LevelOneInputs) = inputs.global_state_map -get_injection_n_states(inputs::SimulationInputs) = - inputs.level_one_inputs.injection_n_states -get_injection_n_states(inputs::LevelOneInputs) = inputs.injection_n_states -get_branches_n_states(inputs::SimulationInputs) = inputs.level_one_inputs.branches_n_states -get_branches_n_states(inputs::LevelOneInputs) = inputs.branches_n_states -get_variable_count(inputs::SimulationInputs) = inputs.level_one_inputs.variable_count -get_variable_count(inputs::LevelOneInputs) = inputs.variable_count -get_inner_vars_count(inputs::SimulationInputs) = inputs.level_one_inputs.inner_vars_count -get_inner_vars_count(inputs::LevelOneInputs) = inputs.inner_vars_count -get_ode_ouput_range(inputs::SimulationInputs) = inputs.level_one_inputs.ode_range -get_ode_ouput_range(inputs::LevelOneInputs) = inputs.ode_range -get_bus_count(inputs::SimulationInputs) = inputs.level_one_inputs.bus_count -get_bus_count(inputs::LevelOneInputs) = inputs.bus_count -get_parameter_count(inputs::SimulationInputs) = inputs.level_one_inputs.parameter_count -get_parameter_count(inputs::LevelOneInputs) = inputs.parameter_count -get_bus_range(inputs::SimulationInputs) = 1:(2 * inputs.level_one_inputs.bus_count) -get_bus_range(inputs::LevelOneInputs) = 1:(2 * inputs.bus_count) +get_global_state_map(inputs::SimulationInputs) = inputs.global_state_map +get_injection_n_states(inputs::SimulationInputs) = inputs.injection_n_states +get_branches_n_states(inputs::SimulationInputs) = inputs.branches_n_states +get_variable_count(inputs::SimulationInputs) = inputs.variable_count +get_inner_vars_count(inputs::SimulationInputs) = inputs.inner_vars_count +get_ode_ouput_range(inputs::SimulationInputs) = inputs.ode_range +get_bus_count(inputs::SimulationInputs) = inputs.bus_count +get_parameter_count(inputs::SimulationInputs) = inputs.parameter_count +get_bus_range(inputs::SimulationInputs) = 1:(2 * inputs.bus_count) #LEVEL TWO INPUTS -get_ybus(inputs::SimulationInputs) = inputs.level_two_inputs.ybus_rectangular -get_ybus(inputs::LevelTwoInputs) = inputs.ybus_rectangular -get_total_shunts(inputs::SimulationInputs) = inputs.level_two_inputs.total_shunts -get_total_shunts(inputs::LevelTwoInputs) = inputs.total_shunts +get_ybus(inputs::SimulationInputs) = inputs.ybus_rectangular +get_total_shunts(inputs::SimulationInputs) = inputs.total_shunts #LEVEL THREE INPUTS -get_DAE_vector(inputs::SimulationInputs) = inputs.level_three_inputs.DAE_vector -get_DAE_vector(inputs::LevelThreeInputs) = inputs.DAE_vector -get_mass_matrix(inputs::SimulationInputs) = inputs.level_three_inputs.mass_matrix -get_mass_matrix(inputs::LevelThreeInputs) = inputs.mass_matrix -get_parameters(inputs::SimulationInputs) = inputs.level_three_inputs.parameters -get_parameters(inputs::LevelThreeInputs) = inputs.parameters -get_delays(inputs::SimulationInputs) = inputs.level_three_inputs.delays -get_delays(inputs::LevelThreeInputs) = inputs.delays +get_DAE_vector(inputs::SimulationInputs) = inputs.DAE_vector +get_mass_matrix(inputs::SimulationInputs) = inputs.mass_matrix +get_parameters(inputs::SimulationInputs) = inputs.parameters +get_delays(inputs::SimulationInputs) = inputs.delays # Utility function not to be used for performance sensitive operations function get_voltage_buses_ix(inputs::SimulationInputs) @@ -299,10 +159,8 @@ function SimulationInputs( ::Type{MassMatrixModel}, sys::PSY.System, frequency_reference::Union{ConstantFrequency, ReferenceBus}, - simulation_inputs_init, - build_level, ) - return SimulationInputs(sys, frequency_reference, simulation_inputs_init, build_level) + return SimulationInputs(sys, frequency_reference) end """ @@ -312,10 +170,8 @@ function SimulationInputs( ::Type{ResidualModel}, sys::PSY.System, frequency_reference::Union{ConstantFrequency, ReferenceBus}, - simulation_inputs_init, - build_level, ) - return SimulationInputs(sys, frequency_reference, simulation_inputs_init, build_level) + return SimulationInputs(sys, frequency_reference) end function _update_initial_parameters!(initial_parameters, wrapped_devices) diff --git a/test/test_base.jl b/test/test_base.jl index 28bafe9ad..923c3b1c3 100644 --- a/test/test_base.jl +++ b/test/test_base.jl @@ -360,8 +360,6 @@ end ResidualModel, omib_sys, ConstantFrequency(), - nothing, - Val(PSID.BUILD_ONE), ) I_balance_sim = zeros(4) PSID.network_model(inputs, I_balance_sim, voltages) @@ -394,8 +392,6 @@ end ResidualModel, threebus_sys, ConstantFrequency(), - nothing, - Val(PSID.BUILD_ONE), ) for i in 1:3, j in 1:3 @@ -470,8 +466,6 @@ end ResidualModel, threebus_sys, ConstantFrequency(), - nothing, - Val(PSID.BUILD_ONE), ) integrator_for_test = MockIntegrator(inputs) cref_affect_f = PSID.get_affect(inputs, threebus_sys, cref) @@ -487,8 +481,6 @@ end ResidualModel, threebus_sys, ConstantFrequency(), - nothing, - Val(PSID.BUILD_ONE), ) integrator_for_test = MockIntegrator(inputs) @@ -532,8 +524,6 @@ end ResidualModel, threebus_sys, ConstantFrequency(), - nothing, - Val(PSID.BUILD_ONE), ) integrator_for_test = MockIntegrator(inputs) @@ -644,7 +634,7 @@ end # (0.0, 20.0), # # Not initialized to speed up the test # initialize_simulation = false, - # frequency_reference = ConstantFrequency(), Val(PSID.BUILD_ONE)), #time span + # frequency_reference = ConstantFrequency()), #time span # ) end @@ -850,16 +840,16 @@ end results_original = read_results(sim) series_original = get_state_series(results_original, ("generator-102-1", :δ); dt = 0.01) - for build_level in - [PSID.BUILD_ONE, PSID.BUILD_TWO, PSID.BUILD_THREE, PSID.BUILD_NONE] - sim.build_inputs_level = build_level + for build_inputs in + [true, false] + sim.build_inputs = build_inputs @test execute!(sim, IDA(); dtmax = 0.005, saveat = 0.005) == PSID.SIMULATION_FINALIZED results = read_results(sim) series = get_state_series(results, ("generator-102-1", :δ); dt = 0.01) @test series == series_original end - sim.build_inputs_level = PSID.BUILD_ONE + sim.build_inputs = true for initialize_level in [PSID.POWERFLOW_AND_DEVICES, PSID.INITIALIZED, PSID.DEVICES_ONLY] sim.initialize_level = initialize_level