Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How should input/output with units work? #3225

Open
hersle opened this issue Nov 19, 2024 · 1 comment
Open

How should input/output with units work? #3225

hersle opened this issue Nov 19, 2024 · 1 comment

Comments

@hersle
Copy link
Contributor

hersle commented Nov 19, 2024

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?

@ChrisRackauckas
Copy link
Member

It's what we would want to get to. We don't have a way to automatically do the unit versions in the compiler right now though, so that would need to be added. Definitely a good idea to do it, just needs bodies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants