 # Unitful + DiffEq + different physical quantities + derivatives?

I’m working on model using Differential Equation that has two state variables, current and temperature. So when using DifferentialEquations.jl I define something like

f(du, u, p, t) = ...

u is a vector with two elements, each of which I’d like to have different units and therefore different types. And du has yet different units from u since we add 1/s when we take the time derivative.

Any advice or packages on how to deal with this in a type stable way? For now I strip all the units off and pass floats to f, but I’d rather propagate the units all the way through if possible.

RecursiveArrayTools.jl has an ArrayPartition which usually works for this. For Example:

using Unitful, DifferentialEquations, RecursiveArrayTools, Plots, UnitfulRecipes

function f!(du, u, p, t)
du = p * sin(2π * t * p)
du = u
end

function setupProblem(amplitude, frequency)
u0 = ArrayPartition([0.0u"m/s"], [0.0u"m"])
p = [amplitude, frequency]
problem = ODEProblem(f!, u0, (0.0u"s", 10.0u"s"), p)
end

prob = setupProblem(0.1u"m/s^2", 1.0u"Hz")
soln = solve(prob)
plot(soln, vars=(0, 2))

3 Likes