Skip to content

Commit e07cbaf

Browse files
committed
make initialization optional in linearize
1 parent 9aa0e0f commit e07cbaf

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/systems/abstractsystem.jl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,7 @@ function io_preprocessing(sys::AbstractSystem, inputs,
12501250
end
12511251

12521252
"""
1253-
lin_fun, simplified_sys = linearization_function(sys::AbstractSystem, inputs, outputs; simplify = false, kwargs...)
1253+
lin_fun, simplified_sys = linearization_function(sys::AbstractSystem, inputs, outputs; simplify = false, initialize = true, kwargs...)
12541254
12551255
Return a function that linearizes the system `sys`. The function [`linearize`](@ref) provides a higher-level and easier to use interface.
12561256
@@ -1274,12 +1274,14 @@ The `simplified_sys` has undergone [`structural_simplify`](@ref) and had any occ
12741274
- `inputs`: A vector of variables that indicate the inputs of the linearized input-output model.
12751275
- `outputs`: A vector of variables that indicate the outputs of the linearized input-output model.
12761276
- `simplify`: Apply simplification in tearing.
1277+
- `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.
12771278
- `kwargs`: Are passed on to `find_solvables!`
12781279
12791280
See also [`linearize`](@ref) which provides a higher-level interface.
12801281
"""
12811282
function linearization_function(sys::AbstractSystem, inputs,
12821283
outputs; simplify = false,
1284+
initialize = true,
12831285
kwargs...)
12841286
sys, diff_idxs, alge_idxs, input_idxs = io_preprocessing(sys, inputs, outputs; simplify,
12851287
kwargs...)
@@ -1295,9 +1297,14 @@ function linearization_function(sys::AbstractSystem, inputs,
12951297
if u !== nothing # Handle systems without states
12961298
length(sts) == length(u) ||
12971299
error("Number of state variables ($(length(sts))) does not match the number of input states ($(length(u)))")
1298-
prob = ODEProblem(fun, u, (t, t + 1), p)
1299-
integ = init(prob, Rodas5P())
1300-
u = integ.u
1300+
if initialize && !isempty(alge_idxs) # This is expensive and can be omitted if the user knows that the system is already initialized
1301+
residual = fun(u, p, t)
1302+
if norm(residual[alge_idxs]) > (eps(eltype(residual)))
1303+
prob = ODEProblem(fun, u, (t, t + 1), p)
1304+
integ = init(prob, Rodas5P())
1305+
u = integ.u
1306+
end
1307+
end
13011308
uf = SciMLBase.UJacobianWrapper(fun, t, p)
13021309
fg_xz = ForwardDiff.jacobian(uf, u)
13031310
h_xz = ForwardDiff.jacobian(let p = p, t = t
@@ -1401,7 +1408,7 @@ function linearize_symbolic(sys::AbstractSystem, inputs,
14011408
if !allow_input_derivatives
14021409
der_inds = findall(vec(any(!iszero, Bs, dims = 1)))
14031410
@show typeof(der_inds)
1404-
error("Input derivatives appeared in expressions (-g_z\\g_u != 0), the following inputs appeared differentiated: $(ModelingToolkit.inputs(sys)[der_inds]). Call `linear_statespace` with keyword argument `allow_input_derivatives = true` to allow this and have the returned `B` matrix be of double width ($(2nu)), where the last $nu inputs are the derivatives of the first $nu inputs.")
1411+
error("Input derivatives appeared in expressions (-g_z\\g_u != 0), the following inputs appeared differentiated: $(ModelingToolkit.inputs(sys)[der_inds]). Call `linearize_symbolic` with keyword argument `allow_input_derivatives = true` to allow this and have the returned `B` matrix be of double width ($(2nu)), where the last $nu inputs are the derivatives of the first $nu inputs.")
14051412
end
14061413
B = [B [zeros(nx, nu); Bs]]
14071414
D = [D zeros(ny, nu)]
@@ -1580,7 +1587,7 @@ function linearize(sys, lin_fun; t = 0.0, op = Dict(), allow_input_derivatives =
15801587
if !iszero(Bs)
15811588
if !allow_input_derivatives
15821589
der_inds = findall(vec(any(!=(0), Bs, dims = 1)))
1583-
error("Input derivatives appeared in expressions (-g_z\\g_u != 0), the following inputs appeared differentiated: $(inputs(sys)[der_inds]). Call `linear_statespace` with keyword argument `allow_input_derivatives = true` to allow this and have the returned `B` matrix be of double width ($(2nu)), where the last $nu inputs are the derivatives of the first $nu inputs.")
1590+
error("Input derivatives appeared in expressions (-g_z\\g_u != 0), the following inputs appeared differentiated: $(inputs(sys)[der_inds]). Call `linearize` with keyword argument `allow_input_derivatives = true` to allow this and have the returned `B` matrix be of double width ($(2nu)), where the last $nu inputs are the derivatives of the first $nu inputs.")
15841591
end
15851592
B = [B [zeros(nx, nu); Bs]]
15861593
D = [D zeros(ny, nu)]

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,6 @@ function get_u0_p(sys,
687687
use_union = false,
688688
tofloat = !use_union,
689689
symbolic_u0 = false)
690-
eqs = equations(sys)
691690
dvs = states(sys)
692691
ps = parameters(sys)
693692

0 commit comments

Comments
 (0)