Skip to content

Commit 1d9a036

Browse files
committed
Make checks optional & opt out of them for internal operations that should not need to validate user inputs. Add tests for structural_simplify that motivated this change.
1 parent c7ad26b commit 1d9a036

File tree

11 files changed

+81
-34
lines changed

11 files changed

+81
-34
lines changed

src/systems/abstractsystem.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,10 @@ Setfield.get(obj::AbstractSystem, ::Setfield.PropertyLens{field}) where {field}
192192
:(getfield(obj, $(Meta.quot(fn))))
193193
end
194194
end
195+
kwarg = :($(Expr(:kw, :checks, false))) # Inputs should already be checked
195196
return Expr(:block,
196197
Expr(:meta, :inline),
197-
Expr(:call,:(constructorof($obj)), args...)
198+
Expr(:call, :(constructorof($obj)), args..., kwarg)
198199
)
199200
else
200201
error("This should never happen. Trying to set $(typeof(obj)) with $patch.")

src/systems/control/controlsystem.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@ struct ControlSystem <: AbstractControlSystem
7272
parameters are not supplied in `ODEProblem`.
7373
"""
7474
defaults::Dict
75-
function ControlSystem(loss, deqs, iv, dvs, controls, ps, observed, name, systems, defaults)
76-
check_variables(dvs, iv)
77-
check_parameters(ps, iv)
78-
check_equations(deqs, iv)
79-
check_equations(observed, iv)
80-
check_units(deqs)
75+
function ControlSystem(loss, deqs, iv, dvs, controls, ps, observed, name, systems, defaults; checks::Bool = true)
76+
if checks
77+
check_variables(dvs, iv)
78+
check_parameters(ps, iv)
79+
check_equations(deqs, iv)
80+
check_equations(observed, iv)
81+
check_units(deqs)
82+
end
8183
new(loss, deqs, iv, dvs, controls, ps, observed, name, systems, defaults)
8284
end
8385
end

src/systems/diffeqs/odesystem.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,13 @@ struct ODESystem <: AbstractODESystem
8484
"""
8585
connection_type::Any
8686

87-
function ODESystem(deqs, iv, dvs, ps, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, systems, defaults, structure, connection_type)
88-
check_variables(dvs,iv)
89-
check_parameters(ps,iv)
90-
check_equations(deqs,iv)
91-
check_units(deqs)
87+
function ODESystem(deqs, iv, dvs, ps, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, systems, defaults, structure, connection_type; checks::Bool = true)
88+
if checks
89+
check_variables(dvs,iv)
90+
check_parameters(ps,iv)
91+
check_equations(deqs,iv)
92+
check_units(deqs)
93+
end
9294
new(deqs, iv, dvs, ps, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, systems, defaults, structure, connection_type)
9395
end
9496
end

src/systems/diffeqs/sdesystem.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,13 @@ struct SDESystem <: AbstractODESystem
8686
"""
8787
connection_type::Any
8888

89-
function SDESystem(deqs, neqs, iv, dvs, ps, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, systems, defaults, connection_type)
90-
check_variables(dvs,iv)
91-
check_parameters(ps,iv)
92-
check_equations(deqs,iv)
93-
check_units(deqs,neqs)
89+
function SDESystem(deqs, neqs, iv, dvs, ps, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, systems, defaults, connection_type; checks::Bool = true)
90+
if checks
91+
check_variables(dvs,iv)
92+
check_parameters(ps,iv)
93+
check_equations(deqs,iv)
94+
check_units(deqs,neqs)
95+
end
9496
new(deqs, neqs, iv, dvs, ps, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, systems, defaults, connection_type)
9597
end
9698
end

src/systems/discrete_system/discrete_system.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ struct DiscreteSystem <: AbstractSystem
5454
in `DiscreteSystem`.
5555
"""
5656
default_p::Dict
57-
function DiscreteSystem(discreteEqs, iv, dvs, ps, var_to_name, ctrls, observed, name, systems, default_u0, default_p)
58-
check_variables(dvs,iv)
59-
check_parameters(ps,iv)
60-
check_units(discreteEqs)
57+
function DiscreteSystem(discreteEqs, iv, dvs, ps, var_to_name, ctrls, observed, name, systems, default_u0, default_p; checks::Bool = true)
58+
if checks
59+
check_variables(dvs, iv)
60+
check_parameters(ps, iv)
61+
check_units(discreteEqs)
62+
end
6163
new(discreteEqs, iv, dvs, ps, var_to_name, ctrls, observed, name, systems, default_u0, default_p)
6264
end
6365
end

src/systems/jumps/jumpsystem.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractSystem
5353
type: type of the system
5454
"""
5555
connection_type::Any
56-
function JumpSystem{U}(ap::U, iv, states, ps, var_to_name, observed, name, systems, defaults, connection_type) where U <: ArrayPartition
57-
check_variables(states, iv)
58-
check_parameters(ps, iv)
56+
function JumpSystem{U}(ap::U, iv, states, ps, var_to_name, observed, name, systems, defaults, connection_type; checks::Bool = true) where U <: ArrayPartition
57+
if checks
58+
check_variables(states, iv)
59+
check_parameters(ps, iv)
60+
end
5961
new{U}(ap, iv, states, ps, var_to_name, observed, name, systems, defaults, connection_type)
6062
end
6163
end

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ struct NonlinearSystem <: AbstractSystem
5454
type: type of the system
5555
"""
5656
connection_type::Any
57-
function NonlinearSystem(eqs, states, ps, var_to_name, observed, jac, name, systems, defaults, structure, connection_type)
58-
check_units(eqs)
57+
function NonlinearSystem(eqs, states, ps, var_to_name, observed, jac, name, systems, defaults, structure, connection_type; checks::Bool = true)
58+
if checks
59+
check_units(eqs)
60+
end
5961
new(eqs, states, ps, var_to_name, observed, jac, name, systems, defaults, structure, connection_type)
6062
end
6163
end

src/systems/optimization/optimizationsystem.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ struct OptimizationSystem <: AbstractSystem
4141
parameters are not supplied in `ODEProblem`.
4242
"""
4343
defaults::Dict
44+
function OptimizationSystem(op, states, ps, var_to_name, observed, equality_constraints, inequality_constraints, name, systems, defaults; checks::Bool = true)
45+
new(op, states, ps, var_to_name, observed, equality_constraints, inequality_constraints, name, systems, defaults)
46+
end
4447
end
4548

4649
function OptimizationSystem(op, states, ps;

src/systems/pde/pdesystem.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ struct PDESystem <: ModelingToolkit.AbstractSystem
5959
@add_kwonly function PDESystem(eqs, bcs, domain, indvars, depvars,
6060
ps=SciMLBase.NullParameters();
6161
defaults=Dict(),
62-
connection_type=nothing,
62+
connection_type = nothing,
63+
checks::Bool = true
6364
)
64-
check_units(eqs)
65+
if checks
66+
check_units(eqs)
67+
end
6568
new(eqs, bcs, domain, indvars, depvars, ps, defaults, connection_type)
6669
end
6770
end

src/systems/reaction/reactionsystem.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,14 @@ struct ReactionSystem <: AbstractSystem
155155
connection_type::Any
156156

157157

158-
function ReactionSystem(eqs, iv, states, ps, observed, name, systems, defaults, connection_type)
159-
iv′ = value(iv)
160-
states′ = value.(states)
161-
ps′ = value.(ps)
162-
check_variables(states′, iv′)
163-
check_parameters(ps′, iv′)
158+
function ReactionSystem(eqs, iv, states, ps, observed, name, systems, defaults, connection_type; checks::Bool = true)
159+
if checks
160+
iv′ = value(iv)
161+
states′ = value.(states)
162+
ps′ = value.(ps)
163+
check_variables(states′, iv′)
164+
check_parameters(ps′, iv′)
165+
end
164166
new(collect(eqs), iv′, states′, ps′, observed, name, systems, defaults, connection_type)
165167
end
166168
end

0 commit comments

Comments
 (0)