@@ -74,18 +74,28 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps;
7474 ODESystem (deqs, iv′, dvs′, ps′, tgrad, jac, Wfact, Wfact_t, name, systems)
7575end
7676
77- var_from_nested_derivative (x) = var_from_nested_derivative (x,0 )
7877var_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)
8079
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... )
8284 # NOTE: this assumes that the order of algebric equations doesn't matter
8385 diffvars = OrderedSet {Variable} ()
8486 allstates = OrderedSet {Variable} ()
8587 ps = OrderedSet {Variable} ()
8688 # reorder equations such that it is in the form of `diffeq, algeeq`
8789 diffeq = Equation[]
8890 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." ))
8999 for eq in eqs
90100 for var in vars (eq. rhs for eq ∈ eqs)
91101 var isa Variable || continue
@@ -99,25 +109,15 @@ function extract_eqs_states_ps(eqs::AbstractArray{<:Equation}, iv)
99109 push! (algeeq, eq)
100110 else
101111 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." ))
102113 diffvar in diffvars && throw (ArgumentError (" The differential variable $diffvar is not unique in the system of equations." ))
103114 push! (diffvars, diffvar)
104115 push! (diffeq, eq)
105116 end
106117 end
107118 algevars = setdiff (allstates, diffvars)
108119 # 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... )
121121end
122122
123123Base.:(== )(sys1:: ODESystem , sys2:: ODESystem ) =
0 commit comments