OK – I’m trying again – with a MWE. Consider this an observation (I’m not saying this should be changed in MTK). This post is more meant as a request for advice on best practice.
Consider the DAE
where b=a/10 and c=a/20, and I want to study uncertainty in a.
Suppose I do the following:
using ModelingToolkit
@parameters a=10 b=a/10 c=a/20
@variables t
Dt = Differential(t)
@variables x(t)=1 z(t)
eqs = [Dt(x) ~ - b*(x-z),
0 ~ z - c*x]
@named sys = ODESystem(eqs)
sys_simp = structural_simplify(sys)
ModelingToolkit.defaults(sys_simp)
leading to
Observe that the default value a=10
seems to have disappeared. This is confirmed – kind of – when I try to make a numeric version of the model:
> tspan = (0.0, 10)
> prob = ODEProblem(sys_simp,[],tspan)
Output exceeds the size limit. Open the full output data in a text editorMethodError: no method matching AbstractFloat(::Type{SymbolicUtils.BasicSymbolic{Real}})
Closest candidates are:
(::Type{T})(!Matched::AbstractChar) where T<:Union{AbstractChar,
Suspicion: My guess is that MTK has stripped off all parametes in sys
/sys_simp
that are not used explicitly in the equations. Thus, information about the value of a
has disappeared.
OK – because I only have uncertainty in the single parameter a
, and not independently in b
and c
, I cannot really drop a
and replace the parameters by b=1
and c=1/2
.
So what is the “correct” way to get around this? I have tried to define a
as a constant instead, in the hope that the constant will be carried over to the system sys
/sys_simp
. But no dice – seems like also constants must appear explicitly to be included in the system (sys
/sys_simp
).
Possible solution: keep a
as a parameter, and instead specify b
and c
as variables:
using ModelingToolkit
using DifferentialEquations
@parameters a=10.0
@variables t
Dt = Differential(t)
@variables x(t)=1.0 z(t) b(t) c(t)
eqs = [Dt(x) ~ - b*(x-z),
0 ~ z - c*x,
b ~ a/10,
c ~ a/20]
@named sys = ODESystem(eqs)
sys_simp = structural_simplify(sys)
tspan = (0.0,10)
prob = ODEProblem(sys_simp,[],tspan)
sol = solve(prob)
sol(5, idxs=x)
sol(5, idxs=z)
sol(5, idxs=b)
sol(5, idxs=c)
With this strategy, the model runs and produces sensible results.
QUESTION: is this strategy of making parameters into variables the recommended strategy?