Skip to content

Commit 4985926

Browse files
fix: ignore system initial conditions when using LinearizationOpPoint
1 parent 43d4481 commit 4985926

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

src/linearization.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ The `simplified_sys` has undergone [`mtkcompile`](@ref) and had any occurring in
9292
- `initialize`: If true, a check is performed to ensure that the operating point is consistent (satisfies algebraic equations). If the op is not consistent, initialization is performed.
9393
- `initialization_solver_alg`: A NonlinearSolve algorithm to use for solving for a feasible set of state and algebraic variables that satisfies the specified operating point.
9494
- `autodiff`: An `ADType` supported by DifferentiationInterface.jl to use for calculating the necessary jacobians. Defaults to using `AutoForwardDiff()`
95+
- `ignore_system_initial_conditions`: Whether to ignore `initial_conditions(sys)` and only use `op`.
9596
- `kwargs`: Are passed on to `find_solvables!`
9697
9798
See also [`linearize`](@ref) which provides a higher-level interface.
@@ -113,6 +114,7 @@ function linearization_function(
113114
guesses = Dict{SymbolicT, SymbolicT}(),
114115
warn_empty_op = true,
115116
t = 0.0,
117+
ignore_system_initial_conditions = false,
116118
kwargs...
117119
)
118120
op = Dict(op)
@@ -122,6 +124,11 @@ function linearization_function(
122124
inputs isa AbstractVector || (inputs = [inputs])
123125
outputs isa AbstractVector || (outputs = [outputs])
124126
ssys = mtkcompile(sys; inputs, outputs, simplify, kwargs...)
127+
if ignore_system_initial_conditions
128+
ics = copy(initial_conditions(ssys))
129+
filter!(Base.Fix2(SU.hasmetadata, MTKBase.AnalysisVariable) first, ics)
130+
@set! ssys.initial_conditions = ics
131+
end
125132
diff_idxs, alge_idxs = eq_idxs(ssys)
126133
if zero_dummy_der
127134
dummyder = setdiff(unknowns(ssys), unknowns(sys))
@@ -896,16 +903,19 @@ function linearize(
896903
# and Jacobian preparation work.
897904
lin_fun, ssys = linearization_function(
898905
sys, inputs, outputs;
899-
zero_dummy_der, op = ops[1], t = ts[1], kwargs...
906+
zero_dummy_der, op = ops[1], t = ts[1],
907+
ignore_system_initial_conditions = true, kwargs...
900908
)
901909
results = map(zip(ops, ts)) do (op_i, ti)
902910
linearize(ssys, lin_fun; op = op_i, t = ti, allow_input_derivatives)
903911
end
904912
return first.(results), ssys, last.(results)
905913
end
914+
ignore_system_ics = false
906915
if op isa LinearizationOpPoint
907916
t = op.t
908917
op = _build_op_from_solution(op)
918+
ignore_system_ics = true
909919
end
910920
lin_fun,
911921
ssys = linearization_function(
@@ -914,6 +924,7 @@ function linearize(
914924
outputs;
915925
zero_dummy_der,
916926
op, t,
927+
ignore_system_initial_conditions = ignore_system_ics,
917928
kwargs...
918929
)
919930
mats, extras = linearize(ssys, lin_fun; op, t, allow_input_derivatives)

0 commit comments

Comments
 (0)