Skip to content

Commit 4f2385e

Browse files
authored
Merge pull request #3035 from SciML/revert-3027-double_ss_error
Throw an error if structural simplification is applied twice (take 2)
2 parents 5386832 + d1dbfcc commit 4f2385e

File tree

20 files changed

+90
-71
lines changed

20 files changed

+90
-71
lines changed

.github/workflows/Tests.yml

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

src/structural_transformation/StructuralTransformations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ using ModelingToolkit: ODESystem, AbstractSystem, var_from_nested_derivative, Di
2424
invalidate_cache!, Substitutions, get_or_construct_tearing_state,
2525
filter_kwargs, lower_varname_with_unit, setio, SparseMatrixCLIL,
2626
get_fullvars, has_equations, observed,
27-
Schedule
27+
Schedule, schedule
2828

2929
using ModelingToolkit.BipartiteGraphs
3030
import .BipartiteGraphs: invview, complete

src/structural_transformation/symbolics_tearing.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ function tearing_substitution(sys::AbstractSystem; kwargs...)
125125
neweqs = full_equations(sys::AbstractSystem; kwargs...)
126126
@set! sys.eqs = neweqs
127127
@set! sys.substitutions = nothing
128+
@set! sys.schedule = nothing
128129
end
129130

130131
function tearing_assignments(sys::AbstractSystem)
@@ -615,6 +616,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching,
615616
if sys isa ODESystem
616617
@set! sys.schedule = Schedule(var_eq_matching, dummy_sub)
617618
end
619+
sys = schedule(sys)
618620
@set! state.sys = sys
619621
@set! sys.tearing_state = state
620622
return invalidate_cache!(sys)

src/systems/abstractsystem.jl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,33 @@ iscomplete(sys::AbstractSystem) = isdefined(sys, :complete) && getfield(sys, :co
849849
"""
850850
$(TYPEDSIGNATURES)
851851
852+
Mark a system as scheduled. It is only intended in compiler internals. A system
853+
is scheduled after tearing based simplifications where equations are converted
854+
into assignments.
855+
"""
856+
function schedule(sys::AbstractSystem)
857+
has_schedule(sys) ? sys : (@set! sys.isscheduled = true)
858+
end
859+
860+
"""
861+
$(TYPEDSIGNATURES)
862+
863+
If a system is scheduled, then changing its equations, variables, and
864+
parameters is no longer legal.
865+
"""
866+
function isscheduled(sys::AbstractSystem)
867+
if has_schedule(sys)
868+
get_schedule(sys) !== nothing
869+
elseif has_isscheduled(sys)
870+
get_isscheduled(sys)
871+
else
872+
false
873+
end
874+
end
875+
876+
"""
877+
$(TYPEDSIGNATURES)
878+
852879
Mark a system as completed. If a system is complete, the system will no longer
853880
namespace its subsystems or variables, i.e. `isequal(complete(sys).v.i, v.i)`.
854881
"""
@@ -907,7 +934,8 @@ for prop in [:eqs
907934
:split_idxs
908935
:parent
909936
:index_cache
910-
:is_scalar_noise]
937+
:is_scalar_noise
938+
:isscheduled]
911939
fname_get = Symbol(:get_, prop)
912940
fname_has = Symbol(:has_, prop)
913941
@eval begin

src/systems/diffeqs/sdesystem.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,15 @@ struct SDESystem <: AbstractODESystem
133133
be `true` when `noiseeqs isa Vector`.
134134
"""
135135
is_scalar_noise::Bool
136+
isscheduled::Bool
136137

137138
function SDESystem(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed,
138139
tgrad,
139140
jac,
140141
ctrl_jac, Wfact, Wfact_t, name, systems, defaults, connector_type,
141142
cevents, devents, parameter_dependencies, metadata = nothing, gui_metadata = nothing,
142-
complete = false, index_cache = nothing, parent = nothing, is_scalar_noise = false;
143+
complete = false, index_cache = nothing, parent = nothing, is_scalar_noise = false,
144+
isscheduled = false;
143145
checks::Union{Bool, Int} = true)
144146
if checks == true || (checks & CheckComponents) > 0
145147
check_independent_variables([iv])
@@ -162,7 +164,8 @@ struct SDESystem <: AbstractODESystem
162164
new(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed, tgrad, jac,
163165
ctrl_jac,
164166
Wfact, Wfact_t, name, systems, defaults, connector_type, cevents, devents,
165-
parameter_dependencies, metadata, gui_metadata, complete, index_cache, parent, is_scalar_noise)
167+
parameter_dependencies, metadata, gui_metadata, complete, index_cache, parent, is_scalar_noise,
168+
isscheduled)
166169
end
167170
end
168171

src/systems/discrete_system/discrete_system.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,16 @@ struct DiscreteSystem <: AbstractTimeDependentSystem
9191
The hierarchical parent system before simplification.
9292
"""
9393
parent::Any
94+
isscheduled::Bool
9495

9596
function DiscreteSystem(tag, discreteEqs, iv, dvs, ps, tspan, var_to_name,
9697
observed,
9798
name,
9899
systems, defaults, preface, connector_type, parameter_dependencies = Equation[],
99100
metadata = nothing, gui_metadata = nothing,
100101
tearing_state = nothing, substitutions = nothing,
101-
complete = false, index_cache = nothing, parent = nothing;
102+
complete = false, index_cache = nothing, parent = nothing,
103+
isscheduled = false;
102104
checks::Union{Bool, Int} = true)
103105
if checks == true || (checks & CheckComponents) > 0
104106
check_independent_variables([iv])
@@ -113,7 +115,7 @@ struct DiscreteSystem <: AbstractTimeDependentSystem
113115
systems,
114116
defaults,
115117
preface, connector_type, parameter_dependencies, metadata, gui_metadata,
116-
tearing_state, substitutions, complete, index_cache, parent)
118+
tearing_state, substitutions, complete, index_cache, parent, isscheduled)
117119
end
118120
end
119121

src/systems/jumps/jumpsystem.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,13 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem
114114
Cached data for fast symbolic indexing.
115115
"""
116116
index_cache::Union{Nothing, IndexCache}
117+
isscheduled::Bool
117118

118119
function JumpSystem{U}(tag, ap::U, iv, unknowns, ps, var_to_name, observed, name,
119120
systems,
120121
defaults, connector_type, devents, parameter_dependencies,
121122
metadata = nothing, gui_metadata = nothing,
122-
complete = false, index_cache = nothing;
123+
complete = false, index_cache = nothing, isscheduled = false;
123124
checks::Union{Bool, Int} = true) where {U <: ArrayPartition}
124125
if checks == true || (checks & CheckComponents) > 0
125126
check_independent_variables([iv])
@@ -132,7 +133,7 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem
132133
end
133134
new{U}(tag, ap, iv, unknowns, ps, var_to_name, observed, name, systems, defaults,
134135
connector_type, devents, parameter_dependencies, metadata, gui_metadata,
135-
complete, index_cache)
136+
complete, index_cache, isscheduled)
136137
end
137138
end
138139
function JumpSystem(tag, ap, iv, states, ps, var_to_name, args...; kwargs...)

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,22 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem
8989
The hierarchical parent system before simplification.
9090
"""
9191
parent::Any
92+
isscheduled::Bool
9293

9394
function NonlinearSystem(tag, eqs, unknowns, ps, var_to_name, observed, jac, name,
9495
systems,
9596
defaults, connector_type, parameter_dependencies = Equation[], metadata = nothing,
9697
gui_metadata = nothing,
9798
tearing_state = nothing, substitutions = nothing,
98-
complete = false, index_cache = nothing, parent = nothing; checks::Union{
99-
Bool, Int} = true)
99+
complete = false, index_cache = nothing, parent = nothing,
100+
isscheduled = false; checks::Union{Bool, Int} = true)
100101
if checks == true || (checks & CheckUnits) > 0
101102
u = __get_unit_type(unknowns, ps)
102103
check_units(u, eqs)
103104
end
104105
new(tag, eqs, unknowns, ps, var_to_name, observed, jac, name, systems, defaults,
105106
connector_type, parameter_dependencies, metadata, gui_metadata, tearing_state,
106-
substitutions, complete, index_cache, parent)
107+
substitutions, complete, index_cache, parent, isscheduled)
107108
end
108109
end
109110

src/systems/optimization/optimizationsystem.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ struct OptimizationSystem <: AbstractOptimizationSystem
6464
The hierarchical parent system before simplification.
6565
"""
6666
parent::Any
67+
isscheduled::Bool
6768

6869
function OptimizationSystem(tag, op, unknowns, ps, var_to_name, observed,
6970
constraints, name, systems, defaults, metadata = nothing,
70-
gui_metadata = nothing, complete = false, index_cache = nothing, parent = nothing;
71+
gui_metadata = nothing, complete = false, index_cache = nothing, parent = nothing,
72+
isscheduled = false;
7173
checks::Union{Bool, Int} = true)
7274
if checks == true || (checks & CheckUnits) > 0
7375
u = __get_unit_type(unknowns, ps)
@@ -77,7 +79,7 @@ struct OptimizationSystem <: AbstractOptimizationSystem
7779
end
7880
new(tag, op, unknowns, ps, var_to_name, observed,
7981
constraints, name, systems, defaults, metadata, gui_metadata, complete,
80-
index_cache, parent)
82+
index_cache, parent, isscheduled)
8183
end
8284
end
8385

src/systems/systems.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function structural_simplify(
2929
sys::AbstractSystem, io = nothing; simplify = false, split = true,
3030
allow_symbolic = false, allow_parameter = true, conservative = false, fully_determined = true,
3131
kwargs...)
32-
iscomplete(sys) && throw(RepeatedStructuralSimplificationError())
32+
isscheduled(sys) && throw(RepeatedStructuralSimplificationError())
3333
newsys′ = __structural_simplify(sys, io; simplify,
3434
allow_symbolic, allow_parameter, conservative, fully_determined,
3535
kwargs...)

0 commit comments

Comments
 (0)