In lots of differential equations applications, we want to solve a ‘forced’ DE whose evolution depends upon some input u(t). For instance we can consider a harmonic oscillator with a forcing term. The equation below makes a function create(), that takes an input u(t), and outputs an ODEProblem of the form
\dot{x}(t) = f(x,p, u(t)),
representing a harmonic oscillator being forced by your choice of input u(t), with parameters p.
function create(input)
@parameters t
@parameters k,c,m
@derivatives D'~t
@variables pos(t) vel(t) inp(t)
eqs = [D(pos) ~ vel,
D(vel) ~ input(t) - (1/m)*(c*vel + k*pos),
]
ps = [k,c,m] .=> [1.,4.,1.]
ics = [pos, vel] .=> [1.,0.]
od = ODESystem(eqs, t, first.(ics),first.(ps))
tspan = (0.,100.)
prob = ODEProblem(od, ics, tspan, ps)
return prob
end
If I take ‘nice’ inputs, everything works great. For instance,
prob = create(sin)
However, in a lot of important applications (eg pharmacokinetic modelling), inputs come as nondifferentiable functions, such as pulses or steps. E.g.
nasty(t) = t>1 ? 1 : 0
Now I can understand that functions with if statements and the like, are not convertable to symbolic code by ModelingToolkit.jl. Indeed, the code:
@parameters t
nasty(t)
gives an error:
ERROR: TypeError: non-boolean (Operation) used in boolean context
But it would be really nice if ModelingToolkit.jl could generate one of its’ awesome, compiled vector fields from eqs using inputs like nasty(t), without ever forming an (impossible) symbolic representation of the nasty bit. Basically I want some way of doing
prob = create(nasty)
Since a function like nasty(t) depends only on t, we don’t need to do anything symbolic (jacobians etc) with it when converting the overall system of equations eqs into the vector field of an ODEProblem.
Is there any way of doing something like this using ModelingToolkit.jl? Is it even reasonable?
Many thanks, and also a big thumbs up to everyone working on ModelingToolkit.jl. Although I’m not enough of a programmer to directly contribute, I’m really enjoying seeing it develop: it’s very cool. Hopefully I can contribute a few implementations of ODE models from the systems biology literature in the fullness of time.