Skip to content

Commit

Permalink
make compatible with delay changes
Browse files Browse the repository at this point in the history
  • Loading branch information
m-bossart committed Mar 30, 2024
1 parent 35e2623 commit 80bfa14
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/base/jacobian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ end

function JacobianFunctionWrapper(
m!::SystemModel{MassMatrixModel, HasDelays},
x0_guess::Vector{Float64};
x0_guess::Vector{Float64},
p::Vector{Float64};
# Improve the heuristic to do sparsity detection
sparse_retrieve_loop::Int = 0, #max(3, length(x0_guess) ÷ 100),
)
Expand All @@ -178,7 +179,7 @@ function JacobianFunctionWrapper(
Jf =
(Jv, x, h, t) -> begin
@debug "Evaluating Jacobian Function"
m_ = (residual, x) -> m!(residual, x, h, nothing, t)
m_ = (residual, x) -> m!(residual, x, h, p, t)
jconfig =
ForwardDiff.JacobianConfig(m_, similar(x0), x0, ForwardDiff.Chunk(x0))
ForwardDiff.jacobian!(Jv, m_, zeros(n), x, jconfig)
Expand Down
3 changes: 2 additions & 1 deletion src/base/simulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ function _get_diffeq_problem(
)
simulation_inputs = get_simulation_inputs(sim)
h = get_history_function(sim)
p = get_parameters(simulation_inputs)
sim.problem = SciMLBase.DDEProblem(
SciMLBase.DDEFunction{true}(
model;
Expand All @@ -407,7 +408,7 @@ function _get_diffeq_problem(
sim.x0_init,
h,
get_tspan(sim),
simulation_inputs;
p;
constant_lags = filter!(x -> x != 0, unique(simulation_inputs.delays)),
)
sim.status = BUILT
Expand Down
2 changes: 1 addition & 1 deletion src/initialization/generator_components/init_tg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function initialize_tg!(
#Get mechanical torque to SyncMach
τm0 = inner_vars[τm_var]
PSY.set_P_ref!(tg, τm0)
set_P_ref(dynamic_device, τm0)
device_parameters[P_ref_ix] = τm0
#Update states
tg_ix = get_local_state_ix(dynamic_device, typeof(tg))
tg_states = @view device_states[tg_ix]
Expand Down
20 changes: 18 additions & 2 deletions src/models/device.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ function device!(
)

#Obtain ODEs for AVR
mdl_avr_ode!(device_states, output_ode, device_parameters, inner_vars, dynamic_device, h, t)
mdl_avr_ode!(
device_states,
output_ode,
device_parameters,
inner_vars,
dynamic_device,
h,
t,
)

#Obtain ODEs for Machine
mdl_machine_ode!(
Expand Down Expand Up @@ -250,7 +258,15 @@ function device!(
)

#Obtain inner controller ODEs and modulation commands
mdl_inner_ode!(device_states, output_ode, device_parameters, inner_vars, dynamic_device, h, t)
mdl_inner_ode!(
device_states,
output_ode,
device_parameters,
inner_vars,
dynamic_device,
h,
t,
)

#Obtain converter relations
mdl_converter_ode!(
Expand Down
3 changes: 2 additions & 1 deletion src/models/generator_models/tg_models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ end
function mdl_tg_ode!(
device_states::AbstractArray{<:ACCEPTED_REAL_TYPES},
output_ode::AbstractArray{<:ACCEPTED_REAL_TYPES},
device_parameters::AbstractArray{<:ACCEPTED_REAL_TYPES},
inner_vars::AbstractArray{<:ACCEPTED_REAL_TYPES},
ω_sys::ACCEPTED_REAL_TYPES,
device::DynamicWrapper{PSY.DynamicGenerator{M, S, A, PSY.DEGOV, P}},
Expand All @@ -341,7 +342,7 @@ function mdl_tg_ode!(
) where {M <: PSY.Machine, S <: PSY.Shaft, A <: PSY.AVR, P <: PSY.PSS}

#Obtain references
P_ref = get_P_ref(device)
P_ref = device_parameters[P_ref_ix]

#Obtain indices for component w/r to device
local_ix = get_local_state_ix(device, PSY.DEGOV)
Expand Down
2 changes: 1 addition & 1 deletion src/models/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function (m::SystemModel{MassMatrixModel, NoDelays, C})(
p,
t,
) where {C <: Cache, U <: ACCEPTED_REAL_TYPES, T <: ACCEPTED_REAL_TYPES}
system_mass_matrix!(du, u, nothing, m.inputs, m.cache, t)
system_mass_matrix!(du, u, nothing, p, m.inputs, m.cache, t)
end

function (m::SystemModel{MassMatrixModel, HasDelays, C})(
Expand Down
9 changes: 0 additions & 9 deletions src/utils/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -709,15 +709,6 @@ get_params_symbol(::PSY.SteamTurbineGov1) = [:R_tg,
:T2_tg, #modified to make unique
:T3_tg, #modified to make unique
:D_T]
get_params_symbol(::PSY.TGTypeI) = [:R_tg,
:Ts,
:Tc,
:T3_tg, #modified to make unique
:T4_tg, #modified to make unique
:T5_tg, #modified to make unique
:valve_position_min,
:valve_position_max,
]
get_n_params(::PSY.HydroTurbineGov) = 12
get_params(x::PSY.HydroTurbineGov) = [PSY.get_R(x),
PSY.get_r(x),
Expand Down

0 comments on commit 80bfa14

Please sign in to comment.