Hi everyone! I’ve been playing with ModelingToolkit for several weeks, but there’s still one issue I haven’t been able to fully understand.
In the NEWS.md
file of the github repo, it states:
- ModelingToolkit.jl now exports common definitions of
t
(time independent variable) andD
(the first derivative with respect tot
). Any models made using ModelingToolkit.jl should leverage these common definitions. There are three variants:t
andD
use DynamicQuantities.jl units. This is the default for standard library components.t_unitful
andD_unitful
use Unitful.jl units.t_nounits
andD_nounits
are unitless.
In every example of the docs of ModelingToolkit t_nounits
and D_nounits
is used, e.g.:
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@mtkmodel FOL begin
@parameters begin
τ = 3.0 # parameters
end
@variables begin
u(t) = 0.0 # dependent variables
end
@equations begin
D(u) ~ (1 - u) / τ
end
end
using DifferentialEquations: solve
@mtkbuild fol = FOL()
prob = ODEProblem(fol, [], (0.0, 10.0), [])
sol = solve(prob)
Reading all of this, and also this answer of the forums, I’d understand that the name t
and D
is just a placeholder of t_nounits
and D_nounits
and one could use, for example, t_nounits as x
and D_nounits as Dx
. However, if we do so and execute the same code:
using ModelingToolkit
using ModelingToolkit: t_nounits as x, D_nounits as Dx
@mtkmodel FOL begin
@parameters begin
τ = 3.0 # parameters
end
@variables begin
u(x) = 0.0 # dependent variables
end
@equations begin
Dx(u) ~ (1 - u) / τ
end
end
using DifferentialEquations: solve
@mtkbuild fol = FOL()
prob = ODEProblem(fol, [], (0.0, 10.0), [])
sol = solve(prob)
we get the following error
ERROR: ArgumentError: Differential w.r.t. variable (t) other than the independent variable (x) are not allowed.
I think this happens because Dx is still interpreted as Differential(t). I would have expected that D_nounits = Differential(t_nounits) so importing them with a different name was just a label and ModelingToolkit translated everything under the hood.
Could someone enlighten me about this issue, please?
PS: if one want’s to really use x, a possible solution is to use the @inependent_variables
macro and then define the derivative, i.e.,
using ModelingToolkit
@independent_variables x
D = Differential(x)
@mtkmodel FOL begin
@parameters begin
τ = 3.0 # parameters
end
@variables begin
u(x) = 0.0 # dependent variables
end
@equations begin
D(u) ~ (1 - u) / τ
end
end
using DifferentialEquations: solve
@mtkbuild fol = FOL()
prob = ODEProblem(fol, [], (0.0, 10.0), [])
sol = solve(prob)
This, however, keeps raising the warning
┌ Warning: Independent variable x should be defined with @independent_variables x.
└ @ ModelingToolkit ~/.julia/packages/ModelingToolkit/GJiqn/src/utils.jl:119