Skip to content

Commit 7601b48

Browse files
Merge pull request #4491 from SciML/as/fix-lts
fix: fix LTS tests
2 parents 072d49b + e551e0e commit 7601b48

4 files changed

Lines changed: 27 additions & 26 deletions

File tree

lib/ModelingToolkitBase/src/problems/linearproblem.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ function LinearFunction{iip}(
3131
A, b = calculate_A_b(sys; sparse)
3232
A = Moshi.Match.@match structural_hint begin
3333
StructuralHint.NoHint() => A
34-
StructuralHint.Diagonal() => Diagonal{SymbolicT, Vector{SymbolicT}}(A)
34+
# `Diagonal{T, Vector{T}}(::AbstractMatrix)` does not exist on 1.10
35+
StructuralHint.Diagonal() => Diagonal{SymbolicT, Vector{SymbolicT}}(diag(A))
3536
StructuralHint.Banded(; lower_band_size, upper_band_size) => begin
3637
BandedMatrix{SymbolicT, Matrix{SymbolicT}}(A, (lower_band_size, upper_band_size))
3738
end

lib/ModelingToolkitBase/test/mtkparameters.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ end
4343
@test ps2.tunable isa SVector
4444
@test ps2.initials isa SVector
4545
@test ps2.discrete isa Tuple{<:BlockedVector{Float64, <:SVector}}
46-
@test ps2.constant isa Tuple{<:SVector, <:SVector, <:SVector{1, <:SMatrix}}
46+
@test all(Base.Fix2(isa, SVector), ps2.constant)
47+
@test any(Base.Fix2(isa, SVector{1, <:SMatrix}), ps2.constant)
4748
@test ps2.nonnumeric isa Tuple{<:SVector}
4849
end
4950

test/linearize.jl

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function compare_matrices(reference, value)
126126
@assert size(reference.A) == (2, 2) "This testing function is only valid for `2x2` systems"
127127
@test isapprox(reference.C, value.C) || isapprox(reverse(reference.C), value.C)
128128
colorder = isapprox(reference.C, value.C) ? [1, 2] : [2, 1]
129-
@test isapprox(reference.B, value.B) || isapprox(reverse(reference.B), value.B)
129+
@test isapprox(reference.B, value.B) || isapprox(reference.B[[2, 1], :], value.B)
130130
roworder = isapprox(reference.B, value.B) ? [1, 2] : [2, 1]
131131
@test isapprox(reference.D, value.D)
132132
@test isapprox(reference.A[roworder, colorder], value.A)
@@ -164,10 +164,8 @@ lsys,
164164
)
165165
@unpack int, der = pid
166166

167-
@test lsys.A == [0 0; 0 -10]
168-
@test lsys.B == [2 -2; 10 -10]
169-
@test lsys.C == [400 -4000]
170-
@test lsys.D == [4400 -4400]
167+
refmats = (; A = [0 0; 0 -10], B = [2 -2; 10 -10], C = [400 -4000], D = [4400 -4400])
168+
compare_matrices(refmats, lsys)
171169

172170
lsyss0,
173171
ssys2 = ModelingToolkit.linearize_symbolic(
@@ -183,10 +181,7 @@ compare_matrices(lsys, lsyss)
183181
# Test with the reverse desired unknown order as well to verify that similarity transform and reoreder_unknowns really works
184182
lsys = ModelingToolkit.reorder_unknowns(lsys, unknowns(ssys), reverse(unknowns(ssys)))
185183

186-
@test lsys.A == [-10 0; 0 0]
187-
@test lsys.B == [10 -10; 2 -2]
188-
@test lsys.C == [-4000 400]
189-
@test lsys.D == [4400 -4400]
184+
compare_matrices(refmats, lsys)
190185

191186
## Test that there is a warning when input is misspecified
192187
@test_throws ["inputs provided to `mtkcompile`", "not found"] linearize(

test/structural_transformation/tearing.jl

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import SymbolicUtils as SU
1313
### Nonlinear system
1414
###
1515
@constants h = 1
16-
@variables u1(t) u2(t) u3(t) u4(t) u5(t)
16+
@variables u1(t) u2(t) u3(t) u4(t) u5(t) [state_priority = 10]
1717
eqs = [
1818
0 ~ u1 - sin(u5) * h,
1919
0 ~ u2 - cos(u1),
@@ -65,7 +65,8 @@ graph2vars(graph) = map(is -> Set(map(i -> int2var[i], is)), graph.fadjlist)
6565

6666
newsys = tearing(sys)
6767
@test length(equations(newsys)) == 1
68-
@test issetequal(SU.search_variables(equations(newsys)), [u1, u4, u5])
68+
vars = SU.search_variables(equations(newsys))
69+
@test issetequal(vars, [u1, u4, u5]) || issetequal(vars, [h, u1, u5])
6970

7071
# Before:
7172
# u1 u2 u3 u4 u5
@@ -100,16 +101,19 @@ newsys = tearing(sys)
100101
# --------------------|-----
101102
# e5 [ 1 1 | 1 ]
102103

103-
let state = TearingState(sys)
104-
result, = tearing(state)
105-
S = StructuralTransformations.reordered_matrix(sys, result.var_eq_matching)
106-
@test S == [
107-
1 0 0 0 1
108-
1 1 0 0 0
109-
1 1 1 0 0
110-
0 1 1 1 0
111-
1 0 0 1 1
112-
]
104+
# Variable and equation ordering changes with version
105+
if v"1.12" <= VERSION < v"1.13"
106+
let state = TearingState(sys)
107+
result, = tearing(state)
108+
S = StructuralTransformations.reordered_matrix(sys, result.var_eq_matching)
109+
@test S == [
110+
1 0 0 0 1
111+
1 1 0 0 0
112+
1 1 1 0 0
113+
0 1 1 1 0
114+
1 0 0 1 1
115+
]
116+
end
113117
end
114118

115119
# unknowns: u5
@@ -124,8 +128,8 @@ end
124128
# solve for
125129
# 0 = u5 - hypot(sin(u5), hypot(cos(sin(u5)), hypot(sin(u5), cos(sin(u5)))))
126130
tornsys = complete(tearing(sys))
127-
@test isequal(equations(tornsys), [0 ~ u5 - hypot(u4, u1)])
128-
prob = NonlinearProblem(tornsys, [u5 => 1.0])
131+
@test isequal(equations(tornsys), [0 ~ u5 - hypot(u4, u1)]) || isequal(equations(tornsys), [0 ~ u1 - h * sin(u5)])
132+
prob = NonlinearProblem(tornsys, [u5 => 1.0, u1 => 1.0])
129133
sol = solve(prob, NewtonRaphson())
130134
@test norm(prob.f(sol.u, sol.prob.p)) < 1.0e-10
131135

@@ -260,7 +264,7 @@ end
260264
@test length(equations(sys3)) == 1
261265
@test isequal(only(unknowns(sys3)), sys3.capacitor.v)
262266

263-
idx = findfirst(isequal(sys3.capacitor.p.i), observables(sys3))
267+
idx = findfirst(isequal(sys3.resistor1.v), observables(sys3))
264268
rhs = observed(sys3)[idx].rhs
265269
@test operation(rhs) === getindex
266270
@test operation(arguments(rhs)[1]) === (\)

0 commit comments

Comments
 (0)