Skip to content

Commit a7fe61f

Browse files
Merge pull request #135 from AayushSabharwal/as/mtkv10
chore: update to ModelingToolkit@10
2 parents 7c45d5d + c5df331 commit a7fe61f

File tree

8 files changed

+18
-18
lines changed

8 files changed

+18
-18
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Aqua = "0.8"
1616
EzXML = "1.1"
1717
MathML = "0.1.14"
1818
Memoize = "0.4.2"
19-
ModelingToolkit = "9"
19+
ModelingToolkit = "10"
2020
OrdinaryDiffEq = "6.56"
2121
SafeTestsets = "0.1"
2222
Setfield = "1"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ The models directory contains a few CellML model examples. Let's start with a si
6969
ml = CellModel("models/lorenz.cellml.xml")
7070
```
7171

72-
Now, `ml` is a `CellModel` structure that contains both a list of the loaded XML files and their components (accessible as `ml.doc`) and a ModelingToolkit `ODESystem` that defines variables and dynamics and can be accessed as `getsys(ml)`.
72+
Now, `ml` is a `CellModel` structure that contains both a list of the loaded XML files and their components (accessible as `ml.doc`) and a ModelingToolkit `System` that defines variables and dynamics and can be accessed as `getsys(ml)`.
7373

7474
The next step is to convert `ml` into an `ODEProblem`, ready to be solved.
7575

src/CellMLToolkit.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ using MathML: extract_mathml, parse_node
55
using Memoize: @memoize
66
using SymbolicUtils: operation
77
using ModelingToolkit: ModelingToolkit, @parameters, @variables, Differential,
8-
Equation, ODEProblem, ODESystem,
9-
Symbolics, equations, parameters, structural_simplify,
8+
Equation, ODEProblem, System,
9+
Symbolics, equations, parameters, mtkcompile,
1010
substitute, unknowns
1111
using Setfield: @set!
1212

@@ -50,7 +50,7 @@ import ModelingToolkit.ODEProblem
5050
function ODEProblem(ml::CellModel, tspan;
5151
jac = false, level = 1, p = last.(list_params(ml)),
5252
u0 = last.(list_states(ml)))
53-
ODEProblem(ml.sys, u0, tspan, p; jac = jac)
53+
ODEProblem(ml.sys, Pair[unknowns(ml.sys) .=> u0; parameters(ml.sys) .=> p], tspan; jac = jac)
5454
end
5555

5656
function update_list!(l, sym, val)

src/analysis.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function simplify_systems(systems)
77
sys = last(x)
88
print("simplifying $(nameof(sys))")
99
try
10-
sys = structural_simplify(sys)
10+
sys = mtkcompile(sys)
1111
k = max(1, 50 - length(string(nameof(sys))))
1212
printstyled(repeat(" ", k) * "OK!"; color = :green)
1313
println()

src/components.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ end
168168

169169
"""
170170
pre_substitution generates the substitution rules to be applied to
171-
individual systems before structural_simplify merges them together
171+
individual systems before mtkcompile merges them together
172172
173173
it morphes variables and parameters to their correct form for ModelingToolkit
174174
based on the global CellML doc information
@@ -193,7 +193,7 @@ end
193193

194194
"""
195195
post_substitution generates the substitution rules to be applied to
196-
the merged system after structural_simplify is applied
196+
the merged system after mtkcompile is applied
197197
198198
if changes the names of the independent variable (iv) in each system
199199
to the global iv name
@@ -212,7 +212,7 @@ substitute_eqs(eqs, s) = [substitute(eq.lhs, s) ~ substitute(eq.rhs, s) for eq i
212212

213213
"""
214214
process_components is the main entry point
215-
it processes an doc document and returns a merged ODESystem
215+
it processes an doc document and returns a merged System
216216
217217
use simplify=false only for debugging purposes!
218218
"""
@@ -224,13 +224,13 @@ function process_components(doc::Document; simplify = true)
224224
systems = subsystems(doc, class)
225225
post_sub = post_substitution(doc, systems)
226226

227-
sys = ODESystem(translate_connections(doc, systems, class),
227+
sys = System(Vector{Equation}(translate_connections(doc, systems, class)),
228228
get_ivₚ(doc),
229229
systems = collect(values(systems)),
230230
name = gensym(:cellml))
231231

232232
if simplify
233-
sys = structural_simplify(sys)
233+
sys = mtkcompile(sys)
234234
@set! sys.eqs = substitute_eqs(equations(sys), post_sub)
235235

236236
# Defaults need to be set after simplifying as otherwise parameters and
@@ -250,12 +250,12 @@ end
250250
class is the output of classify_variables
251251
"""
252252
function subsystems(doc::Document, class)
253-
Dict{Symbol, ODESystem}(to_symbol(comp) => process_component(doc, comp, class)
253+
Dict{Symbol, System}(to_symbol(comp) => process_component(doc, comp, class)
254254
for comp in components(doc))
255255
end
256256

257257
"""
258-
process_component converts a single CellML component to an ODESystem
258+
process_component converts a single CellML component to an System
259259
260260
comp in the name of the component
261261
class is the output of classify_variables
@@ -270,7 +270,7 @@ function process_component(doc::Document, comp, class)
270270
eqs = vcat(parse_node.(math)...)
271271
eqs = substitute_eqs(eqs, pre_sub)
272272
sub_rhs = remove_rhs_diff(eqs)
273-
eqs = [eq.lhs ~ substitute(eq.rhs, sub_rhs) for eq in eqs]
273+
eqs = Equation[eq.lhs ~ substitute(eq.rhs, sub_rhs) for eq in eqs]
274274
end
275275

276276
ivₚ = get_ivₚ(doc)
@@ -280,7 +280,7 @@ function process_component(doc::Document, comp, class)
280280
states = [last(x)
281281
for x in values(pre_sub)
282282
if !ModelingToolkit.isparameter(last(x)) && !isequal(last(x), ivₚ)]
283-
ODESystem(eqs, ivₚ, states, ps; name = to_symbol(comp))
283+
System(eqs, ivₚ, states, ps; name = to_symbol(comp))
284284
end
285285

286286
###############################################################################

src/structures.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ connections(doc::Document) = doc.conns
3232

3333
struct CellModel
3434
doc::Document
35-
sys::ODESystem
35+
sys::System
3636
end

test/beeler.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ err = sum(abs.(V1 .- V2)) / length(V1)
2424
# Ensure defaults are set and that generating an `ODEProblem` directly from the
2525
# `ODESystem` is equivalent to doing so from a `CellModel`
2626
@test length(ModelingToolkit.defaults(ml.sys)) > 0
27-
sys_prob = ODEProblem(ml.sys; tspan = (0, 10000.0))
27+
sys_prob = ODEProblem(ml.sys, nothing, (0, 10000.0))
2828
sol3 = solve(prob, Euler(), dt = 0.01, saveat = 1.0)
2929
V3 = map(x -> x[1], sol3.u)
3030
err2 = sum(abs.(V1 .- V3)) / length(V1)

test/noble_1962.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ err1 = sum(abs.(V1 .- V2)) / length(V1)
1515
# Ensure defaults are set and that generating an `ODEProblem` directly from the
1616
# `ODESystem` is equivalent to doing so from a `CellModel`
1717
@test length(ModelingToolkit.defaults(ml.sys)) > 0
18-
sys_prob = ODEProblem(ml.sys; tspan = (0, 10000.0))
18+
sys_prob = ODEProblem(ml.sys, nothing, (0, 10000.0))
1919
sol3 = solve(prob, Euler(), dt = 0.01, saveat = 1.0)
2020
V3 = map(x -> x[2], sol3.u)
2121
err2 = sum(abs.(V1 .- V3)) / length(V1)

0 commit comments

Comments
 (0)