Skip to content

Commit 859e4d6

Browse files
Merge pull request #3833 from SciML/sim_trans
fix `reorder_unknowns`
2 parents 55479a1 + 73227f0 commit 859e4d6

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
using ModelingToolkit, ADTypes, Test
22
using CommonSolve: solve
33

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

0 commit comments

Comments
 (0)