@@ -74,18 +74,28 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps;
74
74
ODESystem (deqs, iv′, dvs′, ps′, tgrad, jac, Wfact, Wfact_t, name, systems)
75
75
end
76
76
77
- var_from_nested_derivative (x) = var_from_nested_derivative (x,0 )
78
77
var_from_nested_derivative (x:: Constant ) = (missing , missing )
79
- var_from_nested_derivative (x,i) = x. op isa Differential ? var_from_nested_derivative (x. args[1 ],i+ 1 ) : (x. op,i)
78
+ var_from_nested_derivative (x,i= 0 ) = x. op isa Differential ? var_from_nested_derivative (x. args[1 ],i+ 1 ) : (x. op,i)
80
79
81
- function extract_eqs_states_ps (eqs:: AbstractArray{<:Equation} , iv)
80
+ iv_from_nested_derivative (x) = x. op isa Differential ? iv_from_nested_derivative (x. args[1 ]) : x. args[1 ]. op
81
+ iv_from_nested_derivative (x:: Constant ) = missing
82
+
83
+ function ODESystem (eqs; kwargs... )
82
84
# NOTE: this assumes that the order of algebric equations doesn't matter
83
85
diffvars = OrderedSet {Variable} ()
84
86
allstates = OrderedSet {Variable} ()
85
87
ps = OrderedSet {Variable} ()
86
88
# reorder equations such that it is in the form of `diffeq, algeeq`
87
89
diffeq = Equation[]
88
90
algeeq = Equation[]
91
+ # initial loop for finding `iv`
92
+ iv = nothing
93
+ for eq in eqs
94
+ if ! (eq. lhs isa Constant) # assume eq.lhs is either Differential or Constant
95
+ iv = iv_from_nested_derivative (eq. lhs)
96
+ end
97
+ end
98
+ iv === nothing && throw (ArgumentError (" No differential variable detected." ))
89
99
for eq in eqs
90
100
for var in vars (eq. rhs for eq ∈ eqs)
91
101
var isa Variable || continue
@@ -99,25 +109,15 @@ function extract_eqs_states_ps(eqs::AbstractArray{<:Equation}, iv)
99
109
push! (algeeq, eq)
100
110
else
101
111
diffvar = first (var_from_nested_derivative (eq. lhs))
112
+ iv == iv_from_nested_derivative (eq. lhs) || throw (ArgumentError (" An ODESystem can only have one independent variable." ))
102
113
diffvar in diffvars && throw (ArgumentError (" The differential variable $diffvar is not unique in the system of equations." ))
103
114
push! (diffvars, diffvar)
104
115
push! (diffeq, eq)
105
116
end
106
117
end
107
118
algevars = setdiff (allstates, diffvars)
108
119
# the orders here are very important!
109
- return append! (diffeq, algeeq), vcat (collect (diffvars), collect (algevars)), ps
110
- end
111
-
112
- iv_from_nested_derivative (x) = x. op isa Differential ? iv_from_nested_derivative (x. args[1 ]) : x. args[1 ]. op
113
- iv_from_nested_derivative (x:: Constant ) = missing
114
-
115
- function ODESystem (eqs; kwargs... )
116
- ivs = unique (skipmissing (iv_from_nested_derivative (eq. lhs) for eq ∈ eqs))
117
- length (ivs) == 1 || throw (ArgumentError (" An ODESystem can only have one independent variable." ))
118
- iv = first (ivs)
119
- eqs, dvs, ps = extract_eqs_states_ps (eqs, iv)
120
- return ODESystem (eqs, iv, dvs, ps; kwargs... )
120
+ return ODESystem (append! (diffeq, algeeq), iv, vcat (collect (diffvars), collect (algevars)), ps; kwargs... )
121
121
end
122
122
123
123
Base.:(== )(sys1:: ODESystem , sys2:: ODESystem ) =
0 commit comments