Skip to content

Commit 6706407

Browse files
committed
output extra information from linearization
do not output success
1 parent d7854ad commit 6706407

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/linearization.jl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ end
275275
"""
276276
$(TYPEDSIGNATURES)
277277
278-
Linearize the wrapped system at the point given by `(u, p, t)`.
278+
Linearize the wrapped system at the point given by `(unknowns, p, t)`.
279279
"""
280280
function (linfun::LinearizationFunction)(u, p, t)
281281
if eltype(p) <: Pair
@@ -301,7 +301,7 @@ function (linfun::LinearizationFunction)(u, p, t)
301301
linfun.prob, integ, fun, linfun.initializealg, Val(true);
302302
linfun.initialize_kwargs...)
303303
if !success
304-
error("Initialization algorithm $(linfun.initializealg) failed with `u = $u` and `p = $p`.")
304+
error("Initialization algorithm $(linfun.initializealg) failed with `unknowns = $u` and `p = $p`.")
305305
end
306306
fg_xz = linfun.uf_jac(u, DI.Constant(p), DI.Constant(t))
307307
h_xz = linfun.h_jac(u, DI.Constant(p), DI.Constant(t))
@@ -323,7 +323,10 @@ function (linfun::LinearizationFunction)(u, p, t)
323323
g_u = fg_u[linfun.alge_idxs, :],
324324
h_x = h_xz[:, linfun.diff_idxs],
325325
h_z = h_xz[:, linfun.alge_idxs],
326-
h_u = h_u)
326+
h_u = h_u,
327+
x = u,
328+
p,
329+
t)
327330
end
328331

329332
"""
@@ -436,7 +439,7 @@ function CommonSolve.solve(prob::LinearizationProblem; allow_input_derivatives =
436439
p = parameter_values(prob)
437440
t = current_time(prob)
438441
linres = prob.f(u0, p, t)
439-
f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u = linres
442+
f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u, x, p, t = linres
440443

441444
nx, nu = size(f_u)
442445
nz = size(f_z, 2)
@@ -473,7 +476,7 @@ function CommonSolve.solve(prob::LinearizationProblem; allow_input_derivatives =
473476
end
474477
end
475478

476-
(; A, B, C, D)
479+
(; A, B, C, D), (; x, p, t)
477480
end
478481

479482
"""
@@ -618,8 +621,8 @@ function markio!(state, orig_inputs, inputs, outputs, disturbances; check = true
618621
end
619622

620623
"""
621-
(; A, B, C, D), simplified_sys = linearize(sys, inputs, outputs; t=0.0, op = Dict(), allow_input_derivatives = false, zero_dummy_der=false, kwargs...)
622-
(; A, B, C, D) = linearize(simplified_sys, lin_fun; t=0.0, op = Dict(), allow_input_derivatives = false, zero_dummy_der=false)
624+
(; A, B, C, D), simplified_sys, extras = linearize(sys, inputs, outputs; t=0.0, op = Dict(), allow_input_derivatives = false, zero_dummy_der=false, kwargs...)
625+
(; A, B, C, D), extras = linearize(simplified_sys, lin_fun; t=0.0, op = Dict(), allow_input_derivatives = false, zero_dummy_der=false)
623626
624627
Linearize `sys` between `inputs` and `outputs`, both vectors of variables. Return a NamedTuple with the matrices of a linear statespace representation
625628
on the form
@@ -641,6 +644,8 @@ If `allow_input_derivatives = false`, an error will be thrown if input derivativ
641644
642645
`zero_dummy_der` can be set to automatically set the operating point to zero for all dummy derivatives.
643646
647+
The return value `extras` is a NamedTuple `(; x, p, t)` containing the result of the initialization problem that was solved to determine the operating point.
648+
644649
See also [`linearization_function`](@ref) which provides a lower-level interface, [`linearize_symbolic`](@ref) and [`ModelingToolkit.reorder_unknowns`](@ref).
645650
646651
See extended help for an example.
@@ -750,7 +755,8 @@ function linearize(sys, inputs, outputs; op = Dict(), t = 0.0,
750755
zero_dummy_der,
751756
op,
752757
kwargs...)
753-
linearize(ssys, lin_fun; op, t, allow_input_derivatives), ssys
758+
mats, extras = linearize(ssys, lin_fun; op, t, allow_input_derivatives)
759+
mats, ssys, extras
754760
end
755761

756762
"""

test/linearize.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ eqs = [u ~ kp * (r - y)
1414

1515
@named sys = System(eqs, t)
1616

17-
lsys, ssys = linearize(sys, [r], [y])
17+
lsys, ssys, extras = linearize(sys, [r], [y])
1818
lprob = LinearizationProblem(sys, [r], [y])
19-
lsys2 = solve(lprob)
19+
lsys2, extras2 = solve(lprob)
2020
lsys3, _ = linearize(sys, [r], [y]; autodiff = AutoFiniteDiff())
2121

2222
@test lsys.A[] == lsys2.A[] == lsys3.A[] == -2
2323
@test lsys.B[] == lsys2.B[] == lsys3.B[] == 1
2424
@test lsys.C[] == lsys2.C[] == lsys3.C[] == 1
2525
@test lsys.D[] == lsys2.D[] == lsys3.D[] == 0
26+
@test extras == extras2
2627

2728
lsys, ssys = linearize(sys, [r], [r])
2829

0 commit comments

Comments
 (0)