Skip to content

Commit 59963d6

Browse files
authored
Merge pull request #1816 from SciML/fb/lin_dae
fix matrix sizes and input order in linearize
2 parents 03d8b04 + 15396a9 commit 59963d6

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/inputoutput.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu
218218
f, dvs, ps
219219
end
220220

221-
function inputs_to_parameters!(state::TransformationState, check_bound = true)
221+
function inputs_to_parameters!(state::TransformationState, io)
222+
check_bound = io === nothing
222223
@unpack structure, fullvars, sys = state
223224
@unpack var_to_diff, graph, solvable_graph = structure
224225
@assert solvable_graph === nothing
@@ -274,6 +275,17 @@ function inputs_to_parameters!(state::TransformationState, check_bound = true)
274275
@set! sys.eqs = map(Base.Fix2(substitute, input_to_parameters), equations(sys))
275276
@set! sys.states = setdiff(states(sys), keys(input_to_parameters))
276277
ps = parameters(sys)
278+
279+
if io !== nothing
280+
# Change order of new parameters to correspond to user-provided order in argument `inputs`
281+
d = Dict{Any, Int}()
282+
for (i, inp) in enumerate(new_parameters)
283+
d[inp] = i
284+
end
285+
permutation = [d[i] for i in io.inputs]
286+
new_parameters = new_parameters[permutation]
287+
end
288+
277289
@set! sys.ps = [ps; new_parameters]
278290

279291
@set! state.sys = sys

src/systems/abstractsystem.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ function structural_simplify(sys::AbstractSystem, io = nothing; simplify = false
968968
state = TearingState(sys)
969969
has_io = io !== nothing
970970
has_io && markio!(state, io...)
971-
state, input_idxs = inputs_to_parameters!(state, !has_io)
971+
state, input_idxs = inputs_to_parameters!(state, io)
972972
sys = alias_elimination!(state)
973973
# TODO: avoid construct `TearingState` again.
974974
state = TearingState(sys)
@@ -984,7 +984,7 @@ end
984984

985985
function io_preprocessing(sys::AbstractSystem, inputs,
986986
outputs; simplify = false, kwargs...)
987-
sys, input_idxs = structural_simplify(sys, (inputs, outputs); simplify, kwargs...)
987+
sys, input_idxs = structural_simplify(sys, (; inputs, outputs); simplify, kwargs...)
988988

989989
eqs = equations(sys)
990990
alg_start_idx = findfirst(!isdiffeq, eqs)
@@ -1201,6 +1201,8 @@ function linearize(sys, lin_fun; t = 0.0, op = Dict(), allow_input_derivatives =
12011201
nz = size(f_z, 2)
12021202
ny = size(h_x, 1)
12031203

1204+
D = h_u
1205+
12041206
if isempty(g_z)
12051207
A = f_x
12061208
B = f_u
@@ -1216,20 +1218,20 @@ function linearize(sys, lin_fun; t = 0.0, op = Dict(), allow_input_derivatives =
12161218
A = [f_x f_z
12171219
gzgx*f_x gzgx*f_z]
12181220
B = [f_u
1219-
zeros(nz, nu)]
1221+
gzgx * f_u] # The cited paper has zeros in the bottom block, see derivation in https://github.com/SciML/ModelingToolkit.jl/pull/1691 for the correct formula
1222+
12201223
C = [h_x h_z]
12211224
Bs = -(gz \ g_u) # This equation differ from the cited paper, the paper is likely wrong since their equaiton leads to a dimension mismatch.
12221225
if !iszero(Bs)
12231226
if !allow_input_derivatives
12241227
der_inds = findall(vec(any(!=(0), Bs, dims = 1)))
1225-
error("Input derivatives appeared in expressions (-g_z\\g_u != 0), the following inputs appeared differentiated: $(inputs(sys)[der_inds]). Call `linear_staespace` 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.")
1228+
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.")
12261229
end
1227-
B = [B Bs]
1230+
B = [B [zeros(nx, nu); Bs]]
1231+
D = [D zeros(ny, nu)]
12281232
end
12291233
end
12301234

1231-
D = h_u
1232-
12331235
(; A, B, C, D)
12341236
end
12351237

0 commit comments

Comments
 (0)