How do I import t_nounits with a different name than t in ModelingToolkit?

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) and D (the first derivative with respect to t). Any models made using ModelingToolkit.jl should leverage these common definitions. There are three variants:
    • t and D use DynamicQuantities.jl units. This is the default for standard library components.
    • t_unitful and D_unitful use Unitful.jl units.
    • t_nounits and D_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

This seems like a bug in the macro. Report it?

I’ll open an issue regarding the @independent_variables macro.

Regarding the behaviour of the first issue, using ModelingToolkit: t_nounits as x, D_nounits as Dx. Is it also a bug or I understood wrongly the use of t_nounits?

The error is new. A new check was added for independent var last month here: Check that independent variables are defined as @parameters · SciML/ModelingToolkit.jl@d95f966 · GitHub and Issue warning instead of error when independent variable is not a par… · SciML/ModelingToolkit.jl@2b67034 · GitHub.

I’ll suitably update the macro.

@independent_variables was introduced in Create independent variables with @independent_variables by hersle · Pull Request #2862 · SciML/ModelingToolkit.jl · GitHub as the standard way of doing it.

The warning you see is a small bug in the @mtkmodel macro. I’m trying to fix it in Fix @mtkmodel independent variable generation by hersle · Pull Request #3001 · SciML/ModelingToolkit.jl · GitHub.