Mimi Framework

Mimi and Unitful

I’m starting to build a model where keeping track of physical units gets a bit messy. Using a package like Unitful.jl 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

const LengthVar = Quantity{<:Any, Unitful.𝐋}
@defcomp MyStuff begin
my_param{LengthVar} = Parameter()


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 suggests this is not a new thought!

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 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 , but that won’t handle conversions, just add some safety rails for you and some built in documentation.

1 Like

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

@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.

1 Like

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.

1 Like

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

1 Like