Skip to content

Commit 18f08dc

Browse files
Merge pull request #1064 from lamorton/fix_1063
Ensure independent variable is not in parameters
2 parents a7c387a + 6383cde commit 18f08dc

File tree

9 files changed

+52
-13
lines changed

9 files changed

+52
-13
lines changed

src/systems/control/controlsystem.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ 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+
new(loss, deqs, iv, dvs, controls,ps, observed, name, systems, defaults)
79+
end
7580
end
7681

7782
function ControlSystem(loss, deqs::AbstractVector{<:Equation}, iv, dvs, controls, ps;

src/systems/diffeqs/odesystem.jl

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,12 @@ struct ODESystem <: AbstractODESystem
7575
connection_type::Any
7676

7777
function ODESystem(deqs, iv, dvs, ps, observed, tgrad, jac, Wfact, Wfact_t, name, systems, defaults, structure, connection_type)
78-
check_dependence(dvs,iv)
78+
check_variables(dvs,iv)
79+
check_parameters(ps,iv)
7980
new(deqs, iv, dvs, ps, observed, tgrad, jac, Wfact, Wfact_t, name, systems, defaults, structure, connection_type)
8081
end
8182
end
8283

83-
function check_dependence(dvs,iv)
84-
for dv in dvs
85-
isequal(iv, iv_from_nested_derivative(dv)) || throw(ArgumentError("Variable $dv is not a function of independent variable $iv."))
86-
end
87-
end
88-
8984
function ODESystem(
9085
deqs::AbstractVector{<:Equation}, iv, dvs, ps;
9186
observed = Num[],
@@ -117,10 +112,6 @@ function ODESystem(
117112
ODESystem(deqs, iv′, dvs′, ps′, observed, tgrad, jac, Wfact, Wfact_t, name, systems, defaults, nothing, connection_type)
118113
end
119114

120-
iv_from_nested_derivative(x::Term) = operation(x) isa Differential ? iv_from_nested_derivative(arguments(x)[1]) : arguments(x)[1]
121-
iv_from_nested_derivative(x::Sym) = x
122-
iv_from_nested_derivative(x) = missing
123-
124115
vars(x::Sym) = Set([x])
125116
vars(exprs::Symbolic) = vars([exprs])
126117
vars(exprs) = foldl(vars!, exprs; init = Set())

src/systems/diffeqs/sdesystem.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ struct SDESystem <: AbstractODESystem
7575
type: type of the system
7676
"""
7777
connection_type::Any
78+
79+
function SDESystem(deqs, neqs, iv, dvs, ps, observed, tgrad, jac, Wfact, Wfact_t, name, systems, defaults, connection_type)
80+
check_variables(dvs,iv)
81+
check_parameters(ps,iv)
82+
new(deqs, neqs, iv, dvs, ps, observed, tgrad, jac, Wfact, Wfact_t, name, systems, defaults, connection_type)
83+
end
7884
end
7985

8086
function SDESystem(deqs::AbstractVector{<:Equation}, neqs, iv, dvs, ps;

src/systems/discrete_system/discrete_system.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ struct DiscreteSystem <: AbstractSystem
4949
in `DiscreteSystem`.
5050
"""
5151
default_p::Dict
52+
function DiscreteSystem(discreteEqs, iv, dvs, ps, observed, name, systems, default_u0, default_p)
53+
check_variables(dvs,iv)
54+
check_parameters(ps,iv)
55+
new(discreteEqs, iv, dvs, ps, observed, name, systems, default_u0, default_p)
56+
end
5257
end
5358

5459
"""

src/systems/jumps/jumpsystem.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractSystem
5151
type: type of the system
5252
"""
5353
connection_type::Any
54+
function JumpSystem{U}(ap::U, iv, states, ps, observed, name, systems, defaults, connection_type) where U <: ArrayPartition
55+
check_variables(states,iv)
56+
check_parameters(ps,iv)
57+
new{U}(ap, iv, states, ps, observed, name, systems, defaults, connection_type)
58+
end
5459
end
5560

5661
function JumpSystem(eqs, iv, states, ps;

src/systems/reaction/reactionsystem.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,12 @@ struct ReactionSystem <: AbstractSystem
151151
systems::Vector
152152

153153
function ReactionSystem(eqs, iv, states, ps, observed, name, systems)
154-
new(eqs, value(iv), value.(states), value.(ps), observed, name, systems)
154+
iv′ = value(iv)
155+
states′ = value.(states)
156+
ps′ = value.(ps)
157+
check_variables(states′,iv′)
158+
check_parameters(ps′,iv′)
159+
new(eqs, iv′, states′, ps′, observed, name, systems)
155160
end
156161
end
157162

src/utils.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,20 @@ function _readable_code(ex)
104104
expr
105105
end
106106
readable_code(expr) = JuliaFormatter.format_text(string(Base.remove_linenums!(_readable_code(expr))))
107+
108+
function check_parameters(ps,iv)
109+
for p in ps
110+
isequal(iv,p) && throw(ArgumentError("Independent variable $iv not allowed in parameters."))
111+
end
112+
end
113+
114+
function check_variables(dvs,iv)
115+
for dv in dvs
116+
isequal(iv,dv) && throw(ArgumentError("Independent variable $iv not allowed in dependent variables."))
117+
isequal(iv, iv_from_nested_derivative(dv)) || throw(ArgumentError("Variable $dv is not a function of independent variable $iv."))
118+
end
119+
end
120+
121+
iv_from_nested_derivative(x::Term) = operation(x) isa Differential ? iv_from_nested_derivative(arguments(x)[1]) : arguments(x)[1]
122+
iv_from_nested_derivative(x::Sym) = x
123+
iv_from_nested_derivative(x) = missing

test/jumpsystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ MT = ModelingToolkit
33

44
# basic MT SIR model with tweaks
55
@parameters β γ t
6-
@variables S I R
6+
@variables S(t) I(t) R(t)
77
rate₁ = β*S*I
88
affect₁ = [S ~ S - 1, I ~ I + 1]
99
rate₂ = γ*I+t

test/odesystem.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ eqs = [
328328
]
329329
@test_throws ArgumentError ODESystem(eqs,t,vars,pars)
330330

331+
#Issue 1063/998
332+
pars =[t]
333+
vars = @variables((u1(t),))
334+
@test_throws ArgumentError ODESystem(eqs,t,vars,pars)
335+
331336
@variables x(t)
332337
D = Differential(t)
333338
@parameters M b k

0 commit comments

Comments
 (0)