Skip to content

Commit 5461595

Browse files
committed
permute inputs in inputs_to_parameters!
to correspond to user-provided input ordering. Otherwise subsequent linear algebra operations will make no sense.
1 parent f229fe1 commit 5461595

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/inputoutput.jl

Lines changed: 11 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,15 @@ 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+
param_permutation = map(io.inputs) do inp
282+
findfirst(isequal(inp), new_parameters)
283+
end
284+
new_parameters = new_parameters[param_permutation]
285+
end
286+
277287
@set! sys.ps = [ps; new_parameters]
278288

279289
@set! state.sys = sys

src/systems/abstractsystem.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ function structural_simplify(sys::AbstractSystem, io = nothing; simplify = false
965965
state = TearingState(sys)
966966
has_io = io !== nothing
967967
has_io && markio!(state, io...)
968-
state, input_idxs = inputs_to_parameters!(state, !has_io)
968+
state, input_idxs = inputs_to_parameters!(state, io)
969969
sys = alias_elimination!(state)
970970
# TODO: avoid construct `TearingState` again.
971971
state = TearingState(sys)
@@ -981,7 +981,7 @@ end
981981

982982
function io_preprocessing(sys::AbstractSystem, inputs,
983983
outputs; simplify = false, kwargs...)
984-
sys, input_idxs = structural_simplify(sys, (inputs, outputs); simplify, kwargs...)
984+
sys, input_idxs = structural_simplify(sys, (; inputs, outputs); simplify, kwargs...)
985985

986986
eqs = equations(sys)
987987
alg_start_idx = findfirst(!isdiffeq, eqs)
@@ -1215,7 +1215,7 @@ function linearize(sys, lin_fun; t = 0.0, op = Dict(), allow_input_derivatives =
12151215
A = [f_x f_z
12161216
gzgx*f_x gzgx*f_z]
12171217
B = [f_u
1218-
gzgx*f_u]
1218+
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
12191219

12201220
C = [h_x h_z]
12211221
Bs = -(gz \ g_u) # This equation differ from the cited paper, the paper is likely wrong since their equaiton leads to a dimension mismatch.

0 commit comments

Comments
 (0)