Skip to content

Commit 032f8fc

Browse files
authored
Merge pull request #1789 from wsphillips/wsp/testselect
allow bit flagging for system checks
2 parents 2f56dd9 + e9abae3 commit 032f8fc

File tree

9 files changed

+43
-14
lines changed

9 files changed

+43
-14
lines changed

src/systems/diffeqs/odesystem.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,14 @@ struct ODESystem <: AbstractODESystem
120120
jac, ctrl_jac, Wfact, Wfact_t, name, systems, defaults,
121121
torn_matching, connector_type, connections, preface, cevents,
122122
devents, tearing_state = nothing, substitutions = nothing;
123-
checks::Bool = true)
124-
if checks
123+
checks::Union{Bool, Int} = true)
124+
if checks == true || (checks & CheckComponents) > 0
125125
check_variables(dvs, iv)
126126
check_parameters(ps, iv)
127127
check_equations(deqs, iv)
128128
check_equations(equations(cevents), iv)
129+
end
130+
if checks == true || (checks & CheckUnits) > 0
129131
all_dimensionless([dvs; ps; iv]) || check_units(deqs)
130132
end
131133
new(deqs, iv, dvs, ps, var_to_name, ctrls, observed, tgrad, jac,

src/systems/diffeqs/sdesystem.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,14 @@ struct SDESystem <: AbstractODESystem
9999

100100
function SDESystem(deqs, neqs, iv, dvs, ps, var_to_name, ctrls, observed, tgrad, jac,
101101
ctrl_jac, Wfact, Wfact_t, name, systems, defaults, connector_type,
102-
cevents, devents; checks::Bool = true)
103-
if checks
102+
cevents, devents; checks::Union{Bool, Int} = true)
103+
if checks == true || (checks & CheckComponents) > 0
104104
check_variables(dvs, iv)
105105
check_parameters(ps, iv)
106106
check_equations(deqs, iv)
107107
check_equations(equations(cevents), iv)
108+
end
109+
if checks == true || (checks & CheckUnits) > 0
108110
all_dimensionless([dvs; ps; iv]) || check_units(deqs, neqs)
109111
end
110112
new(deqs, neqs, iv, dvs, ps, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac,

src/systems/discrete_system/discrete_system.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ struct DiscreteSystem <: AbstractTimeDependentSystem
7171
function DiscreteSystem(discreteEqs, iv, dvs, ps, var_to_name, ctrls, observed, name,
7272
systems, defaults, preface, connector_type,
7373
tearing_state = nothing, substitutions = nothing;
74-
checks::Bool = true)
75-
if checks
74+
checks::Union{Bool, Int} = true)
75+
if checks == true || (checks & CheckComponents) > 0
7676
check_variables(dvs, iv)
7777
check_parameters(ps, iv)
78+
end
79+
if checks == true || (checks & CheckUnits) > 0
7880
all_dimensionless([dvs; ps; iv; ctrls]) || check_units(discreteEqs)
7981
end
8082
new(discreteEqs, iv, dvs, ps, var_to_name, ctrls, observed, name, systems, defaults,

src/systems/jumps/jumpsystem.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem
8686

8787
function JumpSystem{U}(ap::U, iv, states, ps, var_to_name, observed, name, systems,
8888
defaults, connector_type, devents;
89-
checks::Bool = true) where {U <: ArrayPartition}
90-
if checks
89+
checks::Union{Bool, Int} = true) where {U <: ArrayPartition}
90+
if checks == true || (checks & CheckComponents) > 0
9191
check_variables(states, iv)
9292
check_parameters(ps, iv)
93+
end
94+
if checks == true || (checks & CheckUnits) > 0
9395
all_dimensionless([states; ps; iv]) || check_units(ap, iv)
9496
end
9597
new{U}(ap, iv, states, ps, var_to_name, observed, name, systems, defaults,

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem
6565

6666
function NonlinearSystem(eqs, states, ps, var_to_name, observed, jac, name, systems,
6767
defaults, connector_type, connections, tearing_state = nothing,
68-
substitutions = nothing; checks::Bool = true)
69-
if checks
68+
substitutions = nothing; checks::Union{Bool, Int} = true)
69+
if checks == true || (checks & CheckUnits) > 0
7070
all_dimensionless([states; ps]) || check_units(eqs)
7171
end
7272
new(eqs, states, ps, var_to_name, observed, jac, name, systems, defaults,

src/systems/optimization/optimizationsystem.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ struct OptimizationSystem <: AbstractTimeIndependentSystem
4242
defaults::Dict
4343
function OptimizationSystem(op, states, ps, var_to_name, observed,
4444
constraints, name, systems, defaults;
45-
checks::Bool = true)
46-
if checks
45+
checks::Union{Bool, Int} = true)
46+
if checks == true || (checks & CheckUnits) > 0
4747
check_units(op)
4848
check_units(observed)
4949
all_dimensionless([states; ps]) || check_units(constraints)

src/systems/pde/pdesystem.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ struct PDESystem <: ModelingToolkit.AbstractMultivariateSystem
6969
defaults = Dict(),
7070
systems = [],
7171
connector_type = nothing,
72-
checks::Bool = true,
72+
checks::Union{Bool, Int} = true,
7373
name)
74-
if checks
74+
if checks == true || (checks & CheckUnits) > 0
7575
all_dimensionless([dvs; ivs; ps]) || check_units(eqs)
7676
end
7777
eqs = eqs isa Vector ? eqs : [eqs]

src/utils.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ function readable_code(expr)
122122
JuliaFormatter.format_text(string(expr), JuliaFormatter.SciMLStyle())
123123
end
124124

125+
# System validation enums
126+
const CheckNone = 0
127+
const CheckAll = 1 << 0
128+
const CheckComponents = 1 << 1
129+
const CheckUnits = 1 << 2
130+
125131
function check_parameters(ps, iv)
126132
for p in ps
127133
isequal(iv, p) &&

test/units.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ eqs = [D(E) ~ P - E / τ
4747
@test !MT.validate(D(D(E)) ~ P)
4848
@test !MT.validate(0 ~ P + E * τ)
4949

50+
# Disabling unit validation/checks selectively
51+
@test_throws MT.ArgumentError ODESystem(eqs, t, [E, P, t], [τ], name = :sys)
52+
ODESystem(eqs, t, [E, P, t], [τ], name = :sys, checks = MT.CheckUnits)
53+
eqs = [D(E) ~ P - E / τ
54+
0 ~ P + E * τ]
55+
@test_throws MT.ValidationError ODESystem(eqs, name = :sys, checks = MT.CheckAll)
56+
@test_throws MT.ValidationError ODESystem(eqs, name = :sys, checks = true)
57+
ODESystem(eqs, name = :sys, checks = MT.CheckNone)
58+
ODESystem(eqs, name = :sys, checks = false)
59+
@test_throws MT.ValidationError ODESystem(eqs, name = :sys,
60+
checks = MT.CheckComponents | MT.CheckUnits)
61+
@named sys = ODESystem(eqs, checks = MT.CheckComponents)
62+
@test_throws MT.ValidationError ODESystem(eqs, t, [E, P, t], [τ], name = :sys,
63+
checks = MT.CheckUnits)
64+
5065
# Array variables
5166
@variables t [unit = u"s"] x(t)[1:3] [unit = u"m"]
5267
@parameters v[1:3]=[1, 2, 3] [unit = u"m/s"]

0 commit comments

Comments
 (0)