# Mimi and Unitful

**URL:** <https://forum.mimiframework.org/t/mimi-and-unitful/182>\
**Category:** Uncategorized\
**Created:** [July 10, 2021, 8:21pm UTC](https://forum.mimiframework.org/t/mimi-and-unitful/182 "2021-07-10T20:21:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdossgollin](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/jdossgollin/32/33_2.png) [@jdossgollin](https://forum.mimiframework.org/u/jdossgollin)\
**Post date:** [July 10, 2021, 8:21pm UTC](https://forum.mimiframework.org/t/mimi-and-unitful/182/1 "2021-07-10T20:21:43Z")

</div>

I’m starting to build a model where keeping track of physical units gets a bit messy. Using a package like [`Unitful.jl`](https://painterqubits.github.io/Unitful.jl/stable/) would be really helpful, as it would let the model builder specify that some length is 5ft or 2m, rather than making the user convert things.

One way I can think of to do this is to define a variable type for each type of unit that something can have, and then to specify it in the parameter/variable definitions

```auto
const LengthVar = Quantity{<:Any, Unitful.𝐋}

```

```auto
@defcomp MyStuff begin
my_param{LengthVar} = Parameter()
...

end

```

Another is to create a wrapper function that takes in all the model parameters as unitful quantities and then converts them into scalars (representing the desired units) to feed it into the model.

Is there a better way?

EDIT: [Proposal: Adding units to Mimi · Issue #25 · mimiframework/Mimi.jl · GitHub](https://github.com/mimiframework/Mimi.jl/issues/25) suggests this is not a new thought!

---

<div class="post-metadata">

**Author:** ![lrennels](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/lrennels/32/6_2.png) [@lrennels](https://forum.mimiframework.org/u/lrennels)\
**Post date:** [July 14, 2021, 4:52pm UTC](https://forum.mimiframework.org/t/mimi-and-unitful/182/2 "2021-07-14T16:52:34Z")

</div>

Hi @jdossgollin thank you for your question!

Yes, you’re right we’ve certainly vaguely discussed this one for a long time but have not come down on a design decision yet. I will bump the linked Issue [Proposal: Adding units to Mimi · Issue #25 · mimiframework/Mimi.jl · GitHub](https://github.com/mimiframework/Mimi.jl/issues/25) towards the top of our stack for work by me, or by an outside contributor. For now I would probably recommend your second suggestion, writing a wrapper outside of the Mimi `@defcomp` macro, as I can’t promise right now that defining your own variable type won’t get messy. To be clear the syntax would be `p1 = Parameter{Bool}()` , but I would need to look into how that would work with custom types.

You can put units into your parameter definitions with the `units` keyword argument as described here [5 Parameters + Variables · Mimi.jl](https://www.mimiframework.org/Mimi.jl/stable/howto/howto_5/#Units-1) , but that won’t handle conversions, just add some safety rails for you and some built in documentation.

---

<div class="post-metadata">

**Author:** ![jdossgollin](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/jdossgollin/32/33_2.png) [@jdossgollin](https://forum.mimiframework.org/u/jdossgollin)\
**Post date:** [July 14, 2021, 4:58pm UTC](https://forum.mimiframework.org/t/mimi-and-unitful/182/3 "2021-07-14T16:58:06Z")

</div>

Thanks Lisa! Helpful just to know what I should be doing.

---

<div class="post-metadata">

**Author:** ![lrennels](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/lrennels/32/6_2.png) [@lrennels](https://forum.mimiframework.org/u/lrennels)\
**Post date:** [July 14, 2021, 4:59pm UTC](https://forum.mimiframework.org/t/mimi-and-unitful/182/4 "2021-07-14T16:59:08Z")

</div>

@davidanthoff @jrising do you two have any opinions here or ideas in terms of what you’ve done in the past? I know James had run into this in practice and might have a good suggestion for @jdossgollin … I have not personally employed any strategies in practice.

---

<div class="post-metadata">

**Author:** ![jrising](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/jrising/32/7_2.png) [@jrising](https://forum.mimiframework.org/u/jrising)\
**Post date:** [July 15, 2021, 7:29pm UTC](https://forum.mimiframework.org/t/mimi-and-unitful/182/5 "2021-07-15T19:29:47Z")

</div>

Thanks @jdossgollin for raising this! I think that we will need automatic unit conversion as part of Mimi if it’s ever going to reach its potential of letting people mix-and-match components from multiple sources. And I don’t think it’s a big lift internally (but I haven’t tried).

In practice, I’ve sometimes made little converter components, when I want to connect together two components with different units. But if you can do all your conversions before you put the data into your model, definitely do that.

---

<div class="post-metadata">

**Author:** ![jdossgollin](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/jdossgollin/32/33_2.png) [@jdossgollin](https://forum.mimiframework.org/u/jdossgollin)\
**Post date:** [July 15, 2021, 8:09pm UTC](https://forum.mimiframework.org/t/mimi-and-unitful/182/6 "2021-07-15T20:09:04Z")

</div>

Thus far, I can do that so I won’t sweat it. Happy to provide feedback as a potential user for future changes.
