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

Units don't seem to work correctly #287

Open
ctessum opened this issue Jun 25, 2023 · 4 comments
Open

Units don't seem to work correctly #287

ctessum opened this issue Jun 25, 2023 · 4 comments

Comments

@ctessum
Copy link
Contributor

ctessum commented Jun 25, 2023

Hello,

Here is a small example with units:

using ModelingToolkit, MethodOfLines, OrdinaryDiffEq, DomainSets
using Unitful

function ModelingToolkit.get_unit(a::Pair{Num, Float64})
    ModelingToolkit.get_unit(a[1])
end

@parameters x [unit=u"m"] 
@parameters t [unit=u"s"]
@variables u(..) [unit=u"kg"]
Dt = Differential(t)
Dx = Differential(x)

x_min = t_min = 0.0
x_max = t_max = 1.0

@parameters α = 10. [unit=u"m/s"]

eq = [Dt(u(x,t)) ~ α * Dx(u(x,t))]

@assert ModelingToolkit.get_unit(eq[1].lhs) == u"kg/s"
@assert ModelingToolkit.get_unit(eq[1].rhs) == u"kg/s"

domains = [x  Interval(x_min, x_max),
              t  Interval(t_min, t_max)]


bcs = [u(x,t_min) ~ 0.0,
       u(x_min,t) ~ 0.0,
       u(x_max,t) ~ sin(1.0),
] 

@named pdesys = PDESystem(eq,bcs,domains,[x,t],[u(x,t)], [α => 10.])

discretization = MOLFiniteDifference([x=>6], t)
@time prob = discretize(pdesys,discretization)

It works if there aren't any units, and as you can see the units on the two sides of the equation match. However, running the code to the end gives this error: ERROR: ModelingToolkit.ValidationError("Some equations had invalid units. See warnings for details.")

Here are the warnings in question:

┌ Warning:  in eq. #1left, in comparison >, units [m s⁻¹] and [] do not match.
└ @ ModelingToolkit ~/.julia/packages/ModelingToolkit/rrbUl/src/systems/validation.jl:152
┌ Warning:  in eq. #2left, in comparison >, units [m s⁻¹] and [] do not match.
└ @ ModelingToolkit ~/.julia/packages/ModelingToolkit/rrbUl/src/systems/validation.jl:152
┌ Warning:  in eq. #3left, in comparison >, units [m s⁻¹] and [] do not match.
└ @ ModelingToolkit ~/.julia/packages/ModelingToolkit/rrbUl/src/systems/validation.jl:152
┌ Warning:  in eq. #4left, in comparison >, units [m s⁻¹] and [] do not match.
└ @ ModelingToolkit ~/.julia/packages/ModelingToolkit/rrbUl/src/systems/validation.jl:152

It looks like what is happening is that the upwind differencing is comparing the velocity to zero, but the zero doesn't have any units, which causes the problem. I also tried advection_scheme=WENOScheme(), but it gave a similar error.

ctessum added a commit to EarthSciML/EarthSciMLBase.jl that referenced this issue Jun 26, 2023
Unit tracking doesn't currently work with PDEs. Waiting for
SciML/MethodOfLines.jl#286
and
SciML/MethodOfLines.jl#287
to fix
@aditya-sengupta
Copy link

Running into the same issue - unit validation passes on the PDESystem but not on the discretized system. I can post an MWE for my case if it'd help

@xtalax
Copy link
Member

xtalax commented Aug 9, 2023

What would be the successful case, that the result array has the right units?

@aditya-sengupta
Copy link

Potentially it'd be running unit validation on the PDESystem and trusting that that correctness is preserved on the discretization. The units in the result array being correct is ensured by the user declaring it as a variable with those units and it passing the PDESystem validation - it seems like it'd run just as correctly and with fewer hurdles if the discretized system didn't have to be unit checked (or we figure out autopromotion rules for constants to unit aware constants, but that seems harder)

@ctessum
Copy link
Contributor Author

ctessum commented Aug 14, 2023

I guess the immediate successful case would be that the example above would run without error. As far as I can tell it's just an issue of the constants that are added being units (e.g. when the upwind scheme checks for u > 0, the 0 is unitless but u is not).

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

3 participants