Skip to content

Commit 2f518c4

Browse files
Merge pull request #3027 from SciML/double_ss_error
Throw an error if structural simplification is applied twice
2 parents 2937584 + d84a211 commit 2f518c4

File tree

12 files changed

+66
-35
lines changed

12 files changed

+66
-35
lines changed

.github/workflows/Tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ jobs:
2828
group:
2929
- InterfaceI
3030
- InterfaceII
31+
- SymbolicIndexingInterface
32+
- Extended
3133
- Extensions
3234
- Downstream
3335
- RegressionI

src/systems/systems.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ function System(eqs::AbstractVector{<:Equation}, iv, args...; name = nothing,
33
ODESystem(eqs, iv, args...; name, kw..., checks = false)
44
end
55

6+
const REPEATED_SIMPLIFICATION_MESSAGE = "Structural simplification cannot be applied to a completed system. Double simplification is not allowed."
7+
8+
struct RepeatedStructuralSimplificationError <: Exception end
9+
10+
function Base.showerror(io::IO, e::RepeatedStructuralSimplificationError)
11+
print(io, REPEATED_SIMPLIFICATION_MESSAGE)
12+
end
13+
614
"""
715
$(SIGNATURES)
816
@@ -21,6 +29,7 @@ function structural_simplify(
2129
sys::AbstractSystem, io = nothing; simplify = false, split = true,
2230
allow_symbolic = false, allow_parameter = true, conservative = false, fully_determined = true,
2331
kwargs...)
32+
iscomplete(sys) && throw(RepeatedStructuralSimplificationError())
2433
newsys′ = __structural_simplify(sys, io; simplify,
2534
allow_symbolic, allow_parameter, conservative, fully_determined,
2635
kwargs...)

test/downstream/linearize.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ matrices = linfun([1.0], Dict(p => 3.0), 1.0)
312312
@parameters p
313313
eqs = [0 ~ x * log(y) - p]
314314
@named sys = ODESystem(eqs, t; defaults = [p => 1.0])
315-
sys = complete(sys)
316315
@test_nowarn linearize(
317316
sys, [x], []; op = Dict(x => 1.0), allow_input_derivatives = true)
318317
end

test/initial_values.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,6 @@ end
114114
@variables x(t)
115115
@parameters p
116116
@mtkbuild sys = ODESystem([D(x) ~ p], t; defaults = [x => t, p => 2t])
117-
prob = ODEProblem(structural_simplify(sys), [], (1.0, 2.0), [])
117+
prob = ODEProblem(sys, [], (1.0, 2.0), [])
118118
@test prob[x] == 1.0
119119
@test prob.ps[p] == 2.0

test/input_output_handling.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,12 @@ disturbed_input = ins[1]
362362
dist_integ,
363363
ins)
364364

365-
augmented_sys = complete(augmented_sys)
365+
getter_sys = complete(augmented_sys)
366366
matrices, ssys = linearize(augmented_sys,
367367
[
368-
augmented_sys.u,
369-
augmented_sys.input.u[2],
370-
augmented_sys.d
368+
getter_sys.u,
369+
getter_sys.input.u[2],
370+
getter_sys.d
371371
], outs)
372372
@test matrices.A [A [1; 0]; zeros(1, 2) -0.001]
373373
@test matrices.B == I

test/mtkparameters.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ function level1()
144144
eqs = [D(x) ~ p1 * x - p2 * x * y
145145
D(y) ~ -p3 * y + p4 * x * y]
146146

147-
sys = structural_simplify(complete(ODESystem(
148-
eqs, t, tspan = (0, 3.0), name = :sys, parameter_dependencies = [y0 => 2p4])))
147+
sys = structural_simplify(ODESystem(
148+
eqs, t, tspan = (0, 3.0), name = :sys, parameter_dependencies = [y0 => 2p4]))
149149
prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys)
150150
end
151151

@@ -158,8 +158,8 @@ function level2()
158158
eqs = [D(x) ~ p1 * x - p23[1] * x * y
159159
D(y) ~ -p23[2] * y + p4 * x * y]
160160

161-
sys = structural_simplify(complete(ODESystem(
162-
eqs, t, tspan = (0, 3.0), name = :sys, parameter_dependencies = [y0 => 2p4])))
161+
sys = structural_simplify(ODESystem(
162+
eqs, t, tspan = (0, 3.0), name = :sys, parameter_dependencies = [y0 => 2p4]))
163163
prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys)
164164
end
165165

@@ -172,8 +172,8 @@ function level3()
172172
eqs = [D(x) ~ p1 * x - p23[1] * x * y
173173
D(y) ~ -p23[2] * y + p4 * x * y]
174174

175-
sys = structural_simplify(complete(ODESystem(
176-
eqs, t, tspan = (0, 3.0), name = :sys, parameter_dependencies = [y0 => 2p4])))
175+
sys = structural_simplify(ODESystem(
176+
eqs, t, tspan = (0, 3.0), name = :sys, parameter_dependencies = [y0 => 2p4]))
177177
prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys)
178178
end
179179

test/nonlinearsystem.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ end
251251
0 ~ z - cos(x),
252252
0 ~ x * y]
253253
@named ns = NonlinearSystem(eqs, [x, y, z], [])
254-
ns = complete(ns)
255254
vs = [unknowns(ns); parameters(ns)]
256255
ss_mtk = structural_simplify(ns)
257256
prob = NonlinearProblem(ss_mtk, vs .=> 1.0)

test/reduction.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,6 @@ __x = x
7575
# Reduced Flattened System
7676

7777
reduced_system = structural_simplify(connected)
78-
reduced_system2 = structural_simplify(tearing_substitution(structural_simplify(tearing_substitution(structural_simplify(connected)))))
79-
80-
@test isempty(setdiff(unknowns(reduced_system), unknowns(reduced_system2)))
81-
@test isequal(equations(tearing_substitution(reduced_system)), equations(reduced_system2))
82-
@test isequal(observed(reduced_system), observed(reduced_system2))
8378
@test setdiff(unknowns(reduced_system),
8479
[s
8580
a

test/runtests.jl

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,57 +33,62 @@ end
3333
@safetestset "Unitful Quantities Test" include("units.jl")
3434
@safetestset "LabelledArrays Test" include("labelledarrays.jl")
3535
@safetestset "Mass Matrix Test" include("mass_matrix.jl")
36-
@safetestset "SteadyStateSystem Test" include("steadystatesystems.jl")
37-
@safetestset "SDESystem Test" include("sdesystem.jl")
38-
@safetestset "DDESystem Test" include("dde.jl")
39-
@safetestset "NonlinearSystem Test" include("nonlinearsystem.jl")
4036
@safetestset "InitializationSystem Test" include("initializationsystem.jl")
4137
@safetestset "Guess Propagation" include("guess_propagation.jl")
4238
@safetestset "Hierarchical Initialization Equations" include("hierarchical_initialization_eqs.jl")
43-
@safetestset "PDE Construction Test" include("pde.jl")
44-
@safetestset "JumpSystem Test" include("jumpsystem.jl")
45-
@safetestset "Constraints Test" include("constraints.jl")
4639
@safetestset "Reduction Test" include("reduction.jl")
4740
@safetestset "Split Parameters Test" include("split_parameters.jl")
4841
@safetestset "StaticArrays Test" include("static_arrays.jl")
4942
@safetestset "Components Test" include("components.jl")
5043
@safetestset "Model Parsing Test" include("model_parsing.jl")
51-
@safetestset "print_tree" include("print_tree.jl")
5244
@safetestset "Error Handling" include("error_handling.jl")
5345
@safetestset "StructuralTransformations" include("structural_transformation/runtests.jl")
5446
@safetestset "State Selection Test" include("state_selection.jl")
5547
@safetestset "Symbolic Event Test" include("symbolic_events.jl")
5648
@safetestset "Stream Connect Test" include("stream_connectors.jl")
5749
@safetestset "Domain Connect Test" include("domain_connectors.jl")
5850
@safetestset "Lowering Integration Test" include("lowering_solving.jl")
59-
@safetestset "Test Big System Usage" include("bigsystem.jl")
6051
@safetestset "Dependency Graph Test" include("dep_graphs.jl")
6152
@safetestset "Function Registration Test" include("function_registration.jl")
6253
@safetestset "Precompiled Modules Test" include("precompile_test.jl")
63-
@safetestset "Variable Utils Test" include("variable_utils.jl")
64-
@safetestset "Variable Metadata Test" include("test_variable_metadata.jl")
6554
@safetestset "DAE Jacobians Test" include("dae_jacobian.jl")
6655
@safetestset "Jacobian Sparsity" include("jacobiansparsity.jl")
6756
@safetestset "Modelingtoolkitize Test" include("modelingtoolkitize.jl")
68-
@safetestset "OptimizationSystem Test" include("optimizationsystem.jl")
6957
@safetestset "FuncAffect Test" include("funcaffect.jl")
7058
@safetestset "Constants Test" include("constants.jl")
7159
@safetestset "Parameter Dependency Test" include("parameter_dependencies.jl")
7260
@safetestset "Generate Custom Function Test" include("generate_custom_function.jl")
7361
@safetestset "Initial Values Test" include("initial_values.jl")
74-
@safetestset "Discrete System" include("discrete_system.jl")
7562
@safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl")
7663
@safetestset "Equations with complex values" include("complex.jl")
7764
end
7865
end
7966

80-
if GROUP == "All" || GROUP == "InterfaceI" || GROUP == "SymbolicIndexingInterface"
67+
if GROUP == "All" || GROUP == "InterfaceII"
68+
@testset "InterfaceII" begin
69+
@safetestset "Variable Utils Test" include("variable_utils.jl")
70+
@safetestset "Variable Metadata Test" include("test_variable_metadata.jl")
71+
@safetestset "OptimizationSystem Test" include("optimizationsystem.jl")
72+
@safetestset "Discrete System" include("discrete_system.jl")
73+
@safetestset "SteadyStateSystem Test" include("steadystatesystems.jl")
74+
@safetestset "SDESystem Test" include("sdesystem.jl")
75+
@safetestset "DDESystem Test" include("dde.jl")
76+
@safetestset "NonlinearSystem Test" include("nonlinearsystem.jl")
77+
@safetestset "PDE Construction Test" include("pde.jl")
78+
@safetestset "JumpSystem Test" include("jumpsystem.jl")
79+
@safetestset "print_tree" include("print_tree.jl")
80+
@safetestset "Constraints Test" include("constraints.jl")
81+
end
82+
end
83+
84+
if GROUP == "All" || GROUP == "SymbolicIndexingInterface"
8185
@safetestset "SymbolicIndexingInterface test" include("symbolic_indexing_interface.jl")
8286
@safetestset "SciML Problem Input Test" include("sciml_problem_inputs.jl")
8387
@safetestset "MTKParameters Test" include("mtkparameters.jl")
8488
end
8589

86-
if GROUP == "All" || GROUP == "InterfaceII"
90+
if GROUP == "All" || GROUP == "Extended"
91+
@safetestset "Test Big System Usage" include("bigsystem.jl")
8792
println("C compilation test requires gcc available in the path!")
8893
@safetestset "C Compilation Test" include("ccompile.jl")
8994
@testset "Distributed Test" include("distributed.jl")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using ModelingToolkit
2+
using ModelingToolkit: t_nounits as t, D_nounits as D
3+
using Test
4+
5+
@mtkmodel FOL begin
6+
@parameters begin
7+
τ = 3.0 # parameters
8+
end
9+
@variables begin
10+
x(t) = 0.0 # dependent variables
11+
end
12+
@equations begin
13+
D(x) ~ (1 - x) / τ
14+
end
15+
end
16+
17+
@named rc_model = FOL()
18+
sys = structural_simplify(rc_model)
19+
@test_throws ModelingToolkit.RepeatedStructuralSimplificationError structural_simplify(sys)

0 commit comments

Comments
 (0)