Skip to content

How should input/output with units work?Β #3225

@hersle

Description

@hersle

Consider a car that drives with constant velocity:

using ModelingToolkit, OrdinaryDiffEq
@independent_variables t [unit = u"s"]
@variables x(t) [unit = u"m"]
@parameters v [unit = u"m/s"]
@named car = ODESystem([Differential(t)(x) ~ v], t) # or x ~ v * t
car = structural_simplify(car)

Currently, the user is locked to passing dimensionless numbers as input, which can (implicitly) only be in the units declared above:

prob = ODEProblem(car, [x => 0.0], (0.0, 1800.0), [v => 1.0])
sol = solve(prob)
distance = sol[x][end] # should be 1800.0

I think it would be more helpful if the input (and output) were also given with units. For example:

prob = ODEProblem(car, [x => 0.0u"m"], (0.0u"s", 30.0u"minute"), [v => 3.6u"km/hr"])
sol = solve(prob)
distance = sol[x][end] # should be 1800.0u"m"

Then the user could pass any units they like as input (as long as they have the right dimension): 1.0u"m/s", 3.6u"km/hr", 2.24u"mile/hr", etc. would all be equivalent. Internally, the code could automatically convert to a common set of units used to run the simulation. And the solution could also output dimensionful numbers, which the user can easily convert to whichever units they prefer.

Would this be more helpful/convenient unit behavior? Is something like this within the reach/scope of MTK?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions