Skip to content

Commit 2cc32a5

Browse files
committed
fix reorder_unknowns
Close #3840
1 parent 55479a1 commit 2cc32a5

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/linearization.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ function reorder_unknowns(sys::NamedTuple, old, new)
852852
issorted(perm) && return sys # shortcut return, no reordering
853853
P = zeros(Int, nx, nx)
854854
for i in 1:nx # Build permutation matrix
855-
P[i, perm[i]] = 1
855+
P[perm[i], i] = 1
856856
end
857857
similarity_transform(sys, P; unitary = true)
858858
end

test/linearize.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
using ModelingToolkit, ADTypes, Test
22
using CommonSolve: solve
33

4+
5+
# Test reorder_unknowns
6+
# sys = ssrand(1,1,4);
7+
mats = let
8+
A = [-1.617708540405859 0.14199864151523162 1.8120551022076838 -1.246419696614408; 0.6704209450894298 -2.4251566699889575 0.6782529705706082 -1.3731519847672025; -0.09336677360807291 -0.11211714788917712 -3.6877851408229523 -0.7073967284605489; -1.1743200892334098 1.1808779444006103 1.5721685015907167 -0.10858833182921268]
9+
B = [-0.3286766047686936; -1.8473436385672866; -2.4092567234250954; -0.06371974677173559;;]
10+
C = [-0.7144567541084362 0.18898849455229796 0.023473101245754475 1.0369097263843963]
11+
D = [0.6397583934617636;;]
12+
(; A, B, C, D)
13+
end
14+
@variables x1 x2 x3 x4
15+
new = [x4,x1,x3,x2]
16+
old = [x1,x2,x3,x4]
17+
lsys = ModelingToolkit.reorder_unknowns(mats, old, new)
18+
P = [0 1 0 0; 0 0 0 1; 0 0 1 0; 1 0 0 0]
19+
@test isequal(P*new, old)
20+
@test lsys.A == ModelingToolkit.similarity_transform(mats, P).A
21+
22+
423
# r is an input, and y is an output.
524
@independent_variables t
625
@variables x(t)=0 y(t)=0 u(t)=0 r(t)=0

0 commit comments

Comments
 (0)