# MTK & units: how to specify unit for time?

**URL:** https://discourse.julialang.org/t/mtk-units-how-to-specify-unit-for-time/121138
**Category:** Modelling & Simulations
**Created:** [October 10, 2024, 11:02am UTC](https://discourse.julialang.org/t/mtk-units-how-to-specify-unit-for-time/121138 "2024-10-10T11:02:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [October 10, 2024, 11:02am UTC](https://discourse.julialang.org/t/mtk-units-how-to-specify-unit-for-time/121138/1 "2024-10-10T11:02:49Z")

</div>

I’ve started to experiments with units for models. My question is probably related to how I can specify units for time when I do:

```julia
using ModelingToolkit: t_nounits as t, D_nounits as Dt

```

- How can I specify unit for `t` so that this unit is also used in possible components from, e.g., ModelingToolkitStandardLibrary?
- How can I ensure that the same time unit is used for the time derivative `Dt`?

More concretely – consider the following case:

```julia
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as Dt
using DynamicQuantities
using OrdinaryDiffEq: solve
#
@mtkmodel Pendulum_8 begin
    @constants begin
        g = 9.81, [unit = u"m/s^2", description = "Gravity"]
    end
    @parameters begin
        ℓ = 2, [unit = u"m", description = "Pendulum length"]
    end
    @variables begin
        θ(t) = 1, [unit=u"rad", description="Angle"]
        ω(t) = 1, [unit=u"rad/s", description="Angular velocity"]
    end
    @equations begin
        Dt(θ) ~ ω
        Dt(ω) ~ -g/ℓ*sin(θ)
    end
end
#
@mtkbuild pen_8 = Pendulum_8()

```

This gives warnings on:

- eq.#1: units [1.0] for lhs, units [1.0/s] for rhs
- eq.#2: units [1.0/s] for lhs, units [1/s^2] for rhs.

This must be related to no unit defined for time.

**Question** : how do I define unit for time when I import time etc. from `ModelingToolkit.jl` as in

```julia
using ModelingToolkit: t_nounits as t, D_nounits as Dt

```

?  
There is an example in the documentation:

```julia
@independent_variables t [unit = u"ms"]

```

but this example is for a case where time and derivative are not imported from ModelingToolkit…

---

<div class="post-metadata">

### Author: ![hexaeder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexaeder/32/24403_2.png) [@hexaeder](https://discourse.julialang.org/u/hexaeder)
#### Post date: [October 10, 2024, 11:07am UTC](https://discourse.julialang.org/t/mtk-units-how-to-specify-unit-for-time/121138/2 "2024-10-10T11:07:30Z")

</div>

There are also options with units you can import:

> <https://github.com/SciML/ModelingToolkit.jl/blob/816fde74ed572cbb0201e67f42abd32afc4594d0/src/ModelingToolkit.jl#L191-L203>

So `using ModelingToolkit: t_unitful as t` or `using ModelingToolkit: t` might do the trick depending on your unit package. Analogous for `D`.

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [October 10, 2024, 11:41am UTC](https://discourse.julialang.org/t/mtk-units-how-to-specify-unit-for-time/121138/3 "2024-10-10T11:41:30Z")

</div>

This partially solved my problem. Now, I do:

```julia
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as Dt
using ModelingToolkit: t as tu, D as Dtu
using DynamicQuantities

```

I import both nounits and DynamicQuantities versions, and simply give different names to the two cases. This allows me to have models without and with units in the same file…

So, now:

```julia
@mtkmodel Pendulum_8 begin
    @constants begin
        g = 9.81, [unit = u"m/s^2", description = "Gravity"]
    end
    @parameters begin
        ℓ = 2, [unit = u"m", description = "Pendulum length"]
    end
    @variables begin
        θ(t) = 1, [unit=u"rad", description="Angle"]
        ω(t) = 1, [unit=u"rad/s", description="Angular velocity"]
    end
    @equations begin
        Dtu(θ) ~ ω
        Dtu(ω) ~ -g/ℓ*sin(θ)
    end
end

@mtkbuild pen_8 = Pendulum_8()

```

builds the model, and there is no warning wrt. units. NOTE: if I use time variable name `tu` in the variable definition, e.g., `θ(tu) = 1, ...`, I get an error message when I apply `@mtkbuild`.

HOWEVER, if I explicitly `validate` the model, the “old” warnings reappear…???

```julia
julia> ModelingToolkit.validate(equations(pen_8))
Warning: in eq. #1: units [1.0] for left and [1.0 s⁻¹] for right do not match.
└ @ ModelingToolkit C:\Users\Bernt_Lie\.julia\packages\ModelingToolkit\OYoU3\src\systems\unit_check.jl:209
┌ Warning: in eq. #2: units [1.0 s⁻¹] for left and [1.0 s⁻²] for right do not match.
└ @ ModelingToolkit C:\Users\Bernt_Lie\.julia\packages\ModelingToolkit\OYoU3\src\systems\unit_check.jl:209

```

NOTE: if I only import the DynamicQuantities version, validate works.
