# Heterogeneous units and ModelingToolkit

**URL:** <https://discourse.julialang.org/t/heterogeneous-units-and-modelingtoolkit/37922>\
**Category:** Modelling & Simulations\
**Tags:** diffeq\
**Created:** [April 20, 2020, 8:23pm UTC](https://discourse.julialang.org/t/heterogeneous-units-and-modelingtoolkit/37922 "2020-04-20T20:23:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mefob](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@mefob](https://discourse.julialang.org/u/mefob)\
**Post date:** [April 20, 2020, 8:23pm UTC](https://discourse.julialang.org/t/heterogeneous-units-and-modelingtoolkit/37922/1 "2020-04-20T20:23:58Z")

</div>

I am trying to use the ModelingToolkit together with Unitful to model a coupled system of two ODEs with different units. The following code fails on ODEProblem with a DimensionError unless both units in u0 are the same:

```julia
using ModelingToolkit
using DifferentialEquations
using Plots
using Unitful
@parameters t k1 k2
@variables y(t) z(t)
@derivatives D'~t
eqs = [D(y) ~ k1*y, D(z) ~ k2*z]
de = ODESystem(eqs)
u0 = [y => 1.5u"V", z => 3.1u"N"]
p = [k1 => 1.0u"1/s", k2 => 1.0u"1/s"]
tspan = (0.0u"s",1e-5u"s")
prob = ODEProblem(de,u0,tspan, p, jac=false) # ERROR: DimensionError: V and 3.1 N are not dimensionally compatible.
sol = solve(prob, Euler(), dt=1e-6u"s")

```

I know that heterogeneous variables are an issue, as discussed here: [DifferentialEquations.jl and systems with heterogenous units](https://discourse.julialang.org/t/differentialequations-jl-and-systems-with-heterogenous-units/2952)  
The problem in that case was the adaptive stepping that can’t calculate a vector norm if the vector has heterogeneous units.  
The suggested solution was to use ArrayPartition:  
[ODE systems with heterogenous units · Issue #147 · SciML/DifferentialEquations.jl · GitHub](https://github.com/SciML/DifferentialEquations.jl/issues/147)

It suspect that I am running into a similar problem, even though in my case the code fails at ODEProblem(), not on solve().

What is the best approach to use units in my case?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 20, 2020, 8:38pm UTC](https://discourse.julialang.org/t/heterogeneous-units-and-modelingtoolkit/37922/2 "2020-04-20T20:38:59Z")

</div>

The solution here is to type the variables and generate the code as un-united. This isn’t documented yet but will be documented once the front end part of it is ready.

---

<div class="post-metadata">

**Author:** ![mefob](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@mefob](https://discourse.julialang.org/u/mefob)\
**Post date:** [April 20, 2020, 10:24pm UTC](https://discourse.julialang.org/t/heterogeneous-units-and-modelingtoolkit/37922/3 "2020-04-20T22:24:34Z")

</div>

Thanks, Chris!

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [July 14, 2020, 10:19pm UTC](https://discourse.julialang.org/t/heterogeneous-units-and-modelingtoolkit/37922/4 "2020-07-14T22:19:22Z")

</div>

Did you get this working, if so, would you post your code?

---

<div class="post-metadata">

**Author:** ![mefob](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@mefob](https://discourse.julialang.org/u/mefob)\
**Post date:** [July 15, 2020, 7:42pm UTC](https://discourse.julialang.org/t/heterogeneous-units-and-modelingtoolkit/37922/5 "2020-07-15T19:42:49Z")

</div>

I didn’t get it to work, but I haven’t checked the latest versions. At the time, it still needed some changes to ModelingToolkit.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 15, 2020, 9:04pm UTC](https://discourse.julialang.org/t/heterogeneous-units-and-modelingtoolkit/37922/6 "2020-07-15T21:04:58Z")

</div>

We still want to finish the union of SymbolicUtils and MTK first before heading back and making an interface to the unit parts (which exist, but are buried)
