-
-
Notifications
You must be signed in to change notification settings - Fork 210
/
odesystem.jl
315 lines (287 loc) · 10.3 KB
/
odesystem.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
"""
$(TYPEDEF)
A system of ordinary differential equations.
# Fields
$(FIELDS)
# Example
```julia
using ModelingToolkit
@parameters σ ρ β
@variables t x(t) y(t) z(t)
D = Differential(t)
eqs = [D(x) ~ σ*(y-x),
D(y) ~ x*(ρ-z)-y,
D(z) ~ x*y - β*z]
de = ODESystem(eqs,t,[x,y,z],[σ,ρ,β])
```
"""
struct ODESystem <: AbstractODESystem
"""The ODEs defining the system."""
eqs::Vector{Equation}
"""Independent variable."""
iv::Sym
"""Dependent (state) variables. Must not contain the independent variable."""
states::Vector
"""Parameter variables. Must not contain the independent variable."""
ps::Vector
"""Array variables."""
var_to_name
"""Control parameters (some subset of `ps`)."""
ctrls::Vector
"""Observed states."""
observed::Vector{Equation}
"""
Time-derivative matrix. Note: this field will not be defined until
[`calculate_tgrad`](@ref) is called on the system.
"""
tgrad::RefValue{Vector{Num}}
"""
Jacobian matrix. Note: this field will not be defined until
[`calculate_jacobian`](@ref) is called on the system.
"""
jac::RefValue{Any}
"""
Control Jacobian matrix. Note: this field will not be defined until
[`calculate_control_jacobian`](@ref) is called on the system.
"""
ctrl_jac::RefValue{Any}
"""
`Wfact` matrix. Note: this field will not be defined until
[`generate_factorized_W`](@ref) is called on the system.
"""
Wfact::RefValue{Matrix{Num}}
"""
`Wfact_t` matrix. Note: this field will not be defined until
[`generate_factorized_W`](@ref) is called on the system.
"""
Wfact_t::RefValue{Matrix{Num}}
"""
Name: the name of the system
"""
name::Symbol
"""
systems: The internal systems. These are required to have unique names.
"""
systems::Vector{ODESystem}
"""
defaults: The default values to use when initial conditions and/or
parameters are not supplied in `ODEProblem`.
"""
defaults::Dict
"""
structure: structural information of the system
"""
structure::Any
"""
connection_type: type of the system
"""
connection_type::Any
"""
preface: injuect assignment statements before the evaluation of the RHS function.
"""
preface::Any
function ODESystem(deqs, iv, dvs, ps, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, systems, defaults, structure, connection_type, preface; checks::Bool = true)
if checks
check_variables(dvs,iv)
check_parameters(ps,iv)
check_equations(deqs,iv)
check_units(deqs)
end
new(deqs, iv, dvs, ps, var_to_name, ctrls, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, systems, defaults, structure, connection_type, preface)
end
end
function ODESystem(
deqs::AbstractVector{<:Equation}, iv, dvs, ps;
controls = Num[],
observed = Num[],
systems = ODESystem[],
name=nothing,
default_u0=Dict(),
default_p=Dict(),
defaults=_merge(Dict(default_u0), Dict(default_p)),
connection_type=nothing,
preface=nothing,
checks = true,
)
name === nothing && throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro"))
deqs = collect(deqs)
@assert all(control -> any(isequal.(control, ps)), controls) "All controls must also be parameters."
iv′ = value(scalarize(iv))
dvs′ = value.(scalarize(dvs))
ps′ = value.(scalarize(ps))
ctrl′ = value.(scalarize(controls))
if !(isempty(default_u0) && isempty(default_p))
Base.depwarn("`default_u0` and `default_p` are deprecated. Use `defaults` instead.", :ODESystem, force=true)
end
defaults = todict(defaults)
defaults = Dict(value(k) => value(v) for (k, v) in pairs(defaults))
iv′ = value(scalarize(iv))
dvs′ = value.(scalarize(dvs))
ps′ = value.(scalarize(ps))
var_to_name = Dict()
process_variables!(var_to_name, defaults, dvs′)
process_variables!(var_to_name, defaults, ps′)
tgrad = RefValue(Vector{Num}(undef, 0))
jac = RefValue{Any}(Matrix{Num}(undef, 0, 0))
ctrl_jac = RefValue{Any}(Matrix{Num}(undef, 0, 0))
Wfact = RefValue(Matrix{Num}(undef, 0, 0))
Wfact_t = RefValue(Matrix{Num}(undef, 0, 0))
sysnames = nameof.(systems)
if length(unique(sysnames)) != length(sysnames)
throw(ArgumentError("System names must be unique."))
end
ODESystem(deqs, iv′, dvs′, ps′, var_to_name, ctrl′, observed, tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, systems, defaults, nothing, connection_type, preface, checks = checks)
end
function ODESystem(eqs, iv=nothing; kwargs...)
eqs = collect(eqs)
# NOTE: this assumes that the order of algebric equations doesn't matter
diffvars = OrderedSet()
allstates = OrderedSet()
ps = OrderedSet()
# reorder equations such that it is in the form of `diffeq, algeeq`
diffeq = Equation[]
algeeq = Equation[]
# initial loop for finding `iv`
if iv === nothing
for eq in eqs
if !(eq.lhs isa Number) # assume eq.lhs is either Differential or Number
iv = iv_from_nested_derivative(eq.lhs)
break
end
end
end
iv = value(iv)
iv === nothing && throw(ArgumentError("Please pass in independent variables."))
for eq in eqs
collect_vars!(allstates, ps, eq.lhs, iv)
collect_vars!(allstates, ps, eq.rhs, iv)
if isdiffeq(eq)
diffvar, _ = var_from_nested_derivative(eq.lhs)
isequal(iv, iv_from_nested_derivative(eq.lhs)) || throw(ArgumentError("An ODESystem can only have one independent variable."))
diffvar in diffvars && throw(ArgumentError("The differential variable $diffvar is not unique in the system of equations."))
push!(diffvars, diffvar)
push!(diffeq, eq)
else
push!(algeeq, eq)
end
end
algevars = setdiff(allstates, diffvars)
# the orders here are very important!
return ODESystem(append!(diffeq, algeeq), iv, vcat(collect(diffvars), collect(algevars)), ps; kwargs...)
end
# NOTE: equality does not check cached Jacobian
function Base.:(==)(sys1::ODESystem, sys2::ODESystem)
iv1 = get_iv(sys1)
iv2 = get_iv(sys2)
isequal(iv1, iv2) &&
isequal(nameof(sys1), nameof(sys2)) &&
_eq_unordered(get_eqs(sys1), get_eqs(sys2)) &&
_eq_unordered(get_states(sys1), get_states(sys2)) &&
_eq_unordered(get_ps(sys1), get_ps(sys2)) &&
all(s1 == s2 for (s1, s2) in zip(get_systems(sys1), get_systems(sys2)))
end
function flatten(sys::ODESystem)
systems = get_systems(sys)
if isempty(systems)
return sys
else
return ODESystem(
equations(sys),
get_iv(sys),
states(sys),
parameters(sys),
observed=observed(sys),
defaults=defaults(sys),
name=nameof(sys),
)
end
end
ODESystem(eq::Equation, args...; kwargs...) = ODESystem([eq], args...; kwargs...)
"""
$(SIGNATURES)
Build the observed function assuming the observed equations are all explicit,
i.e. there are no cycles.
"""
function build_explicit_observed_function(
sys, syms;
expression=false,
output_type=Array,
checkbounds=true)
if (isscalar = !(syms isa Vector))
syms = [syms]
end
syms = value.(syms)
obs = observed(sys)
observed_idx = Dict(map(x->x.lhs, obs) .=> 1:length(obs))
output = similar(syms, Any)
# FIXME: this is a rather rough estimate of dependencies.
maxidx = 0
for (i, s) in enumerate(syms)
idx = get(observed_idx, s, nothing)
idx === nothing && throw(ArgumentError("$s is not an observed variable."))
idx > maxidx && (maxidx = idx)
output[i] = obs[idx].rhs
end
dvs = DestructuredArgs(states(sys), inbounds=!checkbounds)
ps = DestructuredArgs(parameters(sys), inbounds=!checkbounds)
ivs = independent_variables(sys)
args = [dvs, ps, ivs...]
pre = get_postprocess_fbody(sys)
ex = Func(
args, [],
pre(Let(
map(eq -> eq.lhs←eq.rhs, obs[1:maxidx]),
isscalar ? output[1] : MakeArray(output, output_type)
))
) |> toexpr
expression ? ex : @RuntimeGeneratedFunction(ex)
end
function _eq_unordered(a, b)
length(a) === length(b) || return false
n = length(a)
idxs = Set(1:n)
for x ∈ a
idx = findfirst(isequal(x), b)
idx === nothing && return false
idx ∈ idxs || return false
delete!(idxs, idx)
end
return true
end
# We have a stand-alone function to convert a `NonlinearSystem` or `ODESystem`
# to an `ODESystem` to connect systems, and we later can reply on
# `structural_simplify` to convert `ODESystem`s to `NonlinearSystem`s.
"""
$(TYPEDSIGNATURES)
Convert a `NonlinearSystem` to an `ODESystem` or converts an `ODESystem` to a
new `ODESystem` with a different independent variable.
"""
function convert_system(::Type{<:ODESystem}, sys, t; name=nameof(sys))
isempty(observed(sys)) || throw(ArgumentError("`convert_system` cannot handle reduced model (i.e. observed(sys) is non-empty)."))
t = value(t)
varmap = Dict()
sts = states(sys)
newsts = similar(sts, Any)
for (i, s) in enumerate(sts)
if istree(s)
args = arguments(s)
length(args) == 1 || throw(InvalidSystemException("Illegal state: $s. The state can have at most one argument like `x(t)`."))
arg = args[1]
if isequal(arg, t)
newsts[i] = s
continue
end
ns = operation(s)(t)
newsts[i] = ns
varmap[s] = ns
else
ns = variable(getname(s); T=FnType)(t)
newsts[i] = ns
varmap[s] = ns
end
end
sub = Base.Fix2(substitute, varmap)
neweqs = map(sub, equations(sys))
defs = Dict(sub(k) => sub(v) for (k, v) in defaults(sys))
return ODESystem(neweqs, t, newsts, parameters(sys); defaults=defs, name=name)
end