Skip to content

Commit 39f0d92

Browse files
Merge pull request #4402 from SciML/as/better-alias-elim
feat: eliminate perfect aliases in favor of higher priority variables
2 parents a4bf0bd + 87a277f commit 39f0d92

13 files changed

Lines changed: 267 additions & 143 deletions

File tree

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ LinearSolve = "3.66"
9090
Logging = "1"
9191
ModelingToolkitBase = "1.27.0"
9292
ModelingToolkitStandardLibrary = "2.20"
93-
ModelingToolkitTearing = "1.9.0"
93+
ModelingToolkitTearing = "1.12.1"
9494
Moshi = "0.3.6"
9595
NonlinearSolve = "4.3"
9696
OffsetArrays = "1"
@@ -113,7 +113,7 @@ Serialization = "1"
113113
Setfield = "0.7, 0.8, 1"
114114
SimpleNonlinearSolve = "0.1.0, 1, 2"
115115
SparseArrays = "1"
116-
StateSelection = "1.5"
116+
StateSelection = "1.9.1"
117117
StaticArrays = "1.9.14"
118118
StochasticDelayDiffEq = "1.11"
119119
StochasticDiffEq = "6.82.0"

lib/ModelingToolkitBase/src/systems/callbacks.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function AffectSystem(
140140
iv = t_nounits
141141
@warn "No independent variable specified. Defaulting to t_nounits."
142142
end
143-
append!(affect, extra_eqs)
143+
affect = [affect; extra_eqs]
144144

145145
discrete_parameters = SymbolicAffect(affect; discrete_parameters).discrete_parameters
146146

lib/ModelingToolkitBase/test/analysis_points.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ModelingToolkitBase, ModelingToolkitStandardLibrary.Blocks, ControlSystemsBase
22
using ModelingToolkitStandardLibrary.Mechanical.Rotational
33
using ModelingToolkitStandardLibrary.Blocks
4+
using SymbolicIndexingInterface
45
using OrdinaryDiffEq, LinearAlgebra
56
using Test
67
using ModelingToolkitBase: t_nounits as t, D_nounits as D, AnalysisPoint, AbstractSystem
@@ -618,6 +619,16 @@ if @isdefined(ModelingToolkit)
618619
@test SciMLBase.successful_retcode(solve(prob, Rodas5P()))
619620
matrices_normal, _ = get_sensitivity(sys_normal, sys_normal.normal_inner.ap)
620621

622+
function compare_matrices(reference, value)
623+
@assert size(reference.A) == (2, 2) "This testing function is only valid for `2x2` systems"
624+
@test isequal(reference.C, value.C) || isequal(reverse(reference.C), value.C)
625+
colorder = isequal(reference.C, value.C) ? [1, 2] : [2, 1]
626+
@test isequal(reference.B, value.B) || isequal(reverse(reference.B), value.B)
627+
roworder = isequal(reference.B, value.B) ? [1, 2] : [2, 1]
628+
@test isequal(reference.D, value.D)
629+
@test isequal(reference.A[roworder, colorder], value.A)
630+
end
631+
621632
@testset "Analysis point overriding part of connection - normal connect" begin
622633
@named F1 = FirstOrder(k = 1, T = 1)
623634
@named F2 = FirstOrder(k = 1, T = 1)
@@ -643,7 +654,7 @@ if @isdefined(ModelingToolkit)
643654
@test SciMLBase.successful_retcode(solve(prob, Rodas5P()))
644655

645656
matrices, _ = get_sensitivity(sys, sys.ap)
646-
@test matrices == matrices_normal
657+
compare_matrices(matrices_normal, matrices)
647658
end
648659

649660
@testset "Analysis point overriding part of connection - variable connect" begin
@@ -671,7 +682,7 @@ if @isdefined(ModelingToolkit)
671682
@test SciMLBase.successful_retcode(solve(prob, Rodas5P()))
672683

673684
matrices, _ = get_sensitivity(sys, sys.ap)
674-
@test matrices == matrices_normal
685+
compare_matrices(matrices_normal, matrices)
675686
end
676687

677688
@testset "Analysis point overriding part of connection - mixed connect" begin
@@ -699,7 +710,7 @@ if @isdefined(ModelingToolkit)
699710
@test SciMLBase.successful_retcode(solve(prob, Rodas5P()))
700711

701712
matrices, _ = get_sensitivity(sys, sys.ap)
702-
@test matrices == matrices_normal
713+
compare_matrices(matrices_normal, matrices)
703714
end
704715

705716
@testset "Ignored analysis points only affect relevant connection sets" begin

lib/ModelingToolkitBase/test/initial_values.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ end
133133
end
134134

135135
@testset "Cyclic dependency checking and substitution limits" begin
136-
@variables x(t) y(t)
136+
# `irreducible` to make sure they are unknowns of the initialization system
137+
@variables x(t) [irreducible = true] y(t) [irreducible = true]
137138
@mtkcompile sys = System(
138139
[D(x) ~ x, D(y) ~ y], t; initialization_eqs = [x ~ 2y + 3, y ~ 2x],
139140
guesses = [x => 2y, y => 2x]

lib/ModelingToolkitBase/test/initializationsystem.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ conditions = getfield.(equations(initprob.f.sys), :rhs)
331331
if @isdefined(ModelingToolkit)
332332
initsol = solve(initprob, reltol = 1.0e-12, abstol = 1.0e-12)
333333
@test SciMLBase.successful_retcode(initsol)
334-
@test maximum(abs.(initsol[conditions])) < 1.0e-8
334+
@test maximum(abs.(initsol[conditions])) < 2.0e-8
335335
end
336336

337337
@test_throws ERRMOD.ExtraEquationsSystemException ModelingToolkitBase.InitializationProblem(
@@ -1343,7 +1343,7 @@ if @isdefined(ModelingToolkit)
13431343
model = dc_motor()
13441344
sys = mtkcompile(model)
13451345

1346-
prob = ODEProblem(sys, [sys.L1.i => 0.0], (0, 6.0))
1346+
prob = ODEProblem(sys, [sys.L1.i => 0.0], (0, 6.0); guesses = [sys.emf.flange.phi => 0])
13471347

13481348
@test_nowarn remake(prob, p = prob.p)
13491349
end

lib/ModelingToolkitBase/test/input_output_handling.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,16 @@ if @isdefined(ModelingToolkit)
141141
model_inputs = [torque.tau.u]
142142
op = Dict(torque.tau.u => 0.0)
143143
matrices, ssys = linearize(
144-
model, model_inputs, model_outputs; op
144+
model, model_inputs, model_outputs; op,
145+
guesses = [inertia2.flange_a.phi => 0.0, inertia1.flange_b.phi => 0.0]
145146
)
146147
@test length(ModelingToolkit.outputs(ssys)) == 4
147148

148149
let # Just to have a local scope for D
149-
matrices, ssys = linearize(model, model_inputs, [y]; op)
150+
matrices, ssys = linearize(
151+
model, model_inputs, [y]; op,
152+
guesses = [inertia2.flange_a.phi => 0.0, inertia1.flange_b.phi => 0.0]
153+
)
150154
A, B, C, D = matrices
151155
obsf = ModelingToolkit.build_explicit_observed_function(
152156
ssys,

lib/ModelingToolkitBase/test/split_parameters.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ if @isdefined(ModelingToolkit)
164164
inputs = [model.torque.tau.u]
165165
op = [model.torque.tau.u => 0.0]
166166
matrices, ssys = ModelingToolkit.linearize(
167-
wr(model), inputs, model_outputs; op
167+
wr(model), inputs, model_outputs; op,
168+
guesses = [model.inertia2.flange_a.phi => 0.0, model.inertia1.flange_b.phi => 0.0]
168169
)
169170

170171
# Design state-feedback gain using LQR

0 commit comments

Comments
 (0)