Skip to content

Commit df97dab

Browse files
fix: ignore system initial conditions when using LinearizationOpPoint
1 parent 0968944 commit df97dab

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(),
114115
warn_empty_op = true,
115116
t = 0.0,
117+
ignore_system_initial_conditions = true,
116118
kwargs...
117119
)
118120
op = Dict(op)
@@ -128,6 +130,11 @@ function linearization_function(
128130
symbolic_type(var) == ArraySymbolic() ? collect(var) : [var]
129131
end
130132
ssys = mtkcompile(sys; inputs, outputs, simplify, kwargs...)
133+
if ignore_system_initial_conditions
134+
ics = copy(initial_conditions(ssys))
135+
filter!(Base.Fix2(SU.hasmetadata, MTKBase.AnalysisVariable) first, ics)
136+
@set! ssys.initial_conditions = ics
137+
end
131138
diff_idxs, alge_idxs = eq_idxs(ssys)
132139
if zero_dummy_der
133140
dummyder = setdiff(unknowns(ssys), unknowns(sys))
@@ -882,16 +889,19 @@ function linearize(
882889
# and Jacobian preparation work.
883890
lin_fun, ssys = linearization_function(
884891
sys, inputs, outputs;
885-
zero_dummy_der, op = ops[1], t = ts[1], kwargs...
892+
zero_dummy_der, op = ops[1], t = ts[1],
893+
ignore_system_initial_conditions = true, kwargs...
886894
)
887895
results = map(zip(ops, ts)) do (op_i, ti)
888896
linearize(ssys, lin_fun; op = op_i, t = ti, allow_input_derivatives)
889897
end
890898
return first.(results), ssys, last.(results)
891899
end
900+
ignore_system_ics = false
892901
if op isa LinearizationOpPoint
893902
t = op.t
894903
op = _build_op_from_solution(op)
904+
ignore_system_ics = true
895905
end
896906
lin_fun,
897907
ssys = linearization_function(
@@ -900,6 +910,7 @@ function linearize(
900910
outputs;
901911
zero_dummy_der,
902912
op, t,
913+
ignore_system_initial_conditions = ignore_system_ics,
903914
kwargs...
904915
)
905916
mats, extras = linearize(ssys, lin_fun; op, t, allow_input_derivatives)

0 commit comments

Comments
 (0)