Skip to content

Commit 3ecae1c

Browse files
committed
Set known defaults for a CellModels ODESystem
CellML files contain `initial_value` fields specifying initial values for parameters and states in the system. Retrieving these for the `ODESystem` was already possible, but had to be done separately or as part of the `ODEProblem` constructor defined for `CellModel`s. Defaults are only set for simplified `ODESystem`s. Unsimplified `ODESystem`s may contain parameters and states for which no defaults are available resulting in errors trying to construct the `ODESystem`. These errors will still occur when using the result of calling `process_components` with `simplify = false`, but as noted in the code that should only be done for debugging purposes and this functionality is not exposed at the higher API levels. Fixes #56.
1 parent 2afeff5 commit 3ecae1c

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/components.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ function process_components(doc::Document; simplify = true)
234234
if simplify
235235
sys = structural_simplify(sys)
236236
@set! sys.eqs = substitute_eqs(equations(sys), post_sub)
237+
238+
# Defaults need to be set after simplifying as otherwise parameters and
239+
# states for which no defaults are available may still be present in
240+
# the system
241+
@set! sys.defaults = Dict(find_list_value(doc, vcat(parameters(sys), states(sys))))
237242
end
238243

239244
return sys

test/beeler.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ V2 = map(x -> x[1], sol2.u)
1717
err = sum(abs.(V1 .- V2)) / length(V1)
1818
@test err < 0.1
1919

20+
# Ensure defaults are set and that generating an `ODEProblem` directly from the
21+
# `ODESystem` is equivalent to doing so from a `CellModel`
22+
@test length(ModelingToolkit.defaults(ml.sys)) > 0
23+
sys_prob = ODEProblem(ml.sys; tspan = (0, 10000.0))
24+
sol3 = solve(prob, Euler(), dt = 0.01, saveat = 1.0)
25+
V3 = map(x -> x[1], sol3.u)
26+
err2 = sum(abs.(V1 .- V3)) / length(V1)
27+
@test err2 0
28+
2029
# prob = ODEProblem(ml, (0,10000.0); jac=true)
2130
# sol3 = solve(prob, TRBDF2(), dtmax=0.5, saveat=1.0)
2231
# V3 = map(x -> x[1], sol2.u)

test/noble_1962.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,14 @@ sol1 = solve(prob, Euler(), dt = 0.01, saveat = 1.0)
66
sol2 = solve(prob, TRBDF2(), dtmax = 0.5, saveat = 1.0)
77
V1 = map(x -> x[2], sol1.u)
88
V2 = map(x -> x[2], sol2.u)
9-
err = sum(abs.(V1 .- V2)) / length(V1)
10-
@test err < 1.0
9+
err1 = sum(abs.(V1 .- V2)) / length(V1)
10+
@test err1 < 1.0
11+
12+
# Ensure defaults are set and that generating an `ODEProblem` directly from the
13+
# `ODESystem` is equivalent to doing so from a `CellModel`
14+
@test length(ModelingToolkit.defaults(ml.sys)) > 0
15+
sys_prob = ODEProblem(ml.sys; tspan = (0, 10000.0))
16+
sol3 = solve(prob, Euler(), dt = 0.01, saveat = 1.0)
17+
V3 = map(x -> x[2], sol3.u)
18+
err2 = sum(abs.(V1 .- V3)) / length(V1)
19+
@test err2 0

0 commit comments

Comments
 (0)