You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A DimensionError is returned when physical units defined for a model are validated during @mtkbuild operation, where the model passes a model variable to a user defined function.
The error message appears to suggest that the validation is trying to compare the units of a model variable with a symbolic representation of the Quantity returned by the user defined function, rather than the units of this quantity.
Expected behavior
The unit validation of the model check to be applied to the unit of the quantity returned by a user defined function, rather than a symbolic representation of the returned Quantity.
Minimal Reproducible Example 👇
For a version without any Unitful.jl units, but otherwise modeling the same thing without a problem, please see the MWE below with title 'MWE no units'.
MWE with units
using ModelingToolkit, OrdinaryDiffEq
using Unitful
using ModelingToolkit: t_unitful as t, D_unitful as D
# Use function for remvoing units from ModelingToolkit documentationremove_units(p::Dict) =Dict(k =>ustrip(v) for (k, v) in p)
functionCSA_of_cone(
fill_height;
cone_height =1u"m",
cone_angle = (π/12)u"rad", # angle between cone base and vertex
)
r = (cone_height - fill_height)/tan(uconvert(u"rad", cone_angle))
returnuconvert(u"m^2", π*(r^2))
end# Define components like in ModelingToolkitStandardLibrary, but with Unitful units defined@connector HydraulicPort begin@variablesbeginp(t), [unit =upreferred(u"Pa")]
ṁ(t), [unit =upreferred(u"kg/s"), connect = Flow]
endend@mtkmodel PressureReference begin@parametersbegin
p =101325, [unit =upreferred(u"Pa"), description ="Fixed reference pressure"]
end@componentsbegin
port =HydraulicPort(name =:port)
end@equationsbegin
port.p ~ p
endend@mtkmodel FixedMassFlow begin@parametersbegin
ṁ =1, [unit =upreferred(u"kg/s"), description ="Fixed massflow"]
end@componentsbegin
port =HydraulicPort(name =:port)
end@equationsbegin
port.ṁ ~-ṁ # negative sign because it flows out of hereendend# Define a cone shape which we can fill with fluid@mtkmodel Cone begin@parametersbegin
cone_height =1, [unit =upreferred(u"m"), description ="Cone height"]
cone_angle = (π/12), [unit =upreferred(u"rad"), description ="Cone angle"]
end@componentsbegin
atm_port =HydraulicPort(name =:atm_port)
fill_port =HydraulicPort(name =:fill_port)
end@constantsbegin
ρ =1000, [unit =upreferred(u"kg/(m^3)"), description ="Fluid density"]
g =9.81, [unit =upreferred(u"m/(s^2)"), description ="Gravitational acceleration"]
end@variablesbeginm(t), [unit =upreferred(u"kg"), description ="Fluid mass in cone"]
h(t), [unit =upreferred(u"m"), description ="Fluid level in cone"]
p(t), [unit =upreferred(u"Pa"), description ="Pressure at cone base"]
A(t), [unit =upreferred(u"m^2"), description ="Cone cross section area at fluid level in cone"]
end@equationsbegin
A ~CSA_of_cone(h; cone_height = cone_height, cone_angle = cone_angle)
D(m) ~ fill_port.ṁ
D(h) ~D(m)/(ρ*A)
p ~ atm_port.p + (ρ*g*h)
endend@mtkmodel ConeFill begin@componentsbegin
pressure_reference =PressureReference(name =:pressure_reference)
fixed_mass_flow =FixedMassFlow(name =:fixed_mass_flow)
cone =Cone(name =:cone)
end@equationsbeginconnect(pressure_reference.port, cone.atm_port)
connect(fixed_mass_flow.port, cone.fill_port)
endend@mtkbuild sys =ConeFill()
prob =ODEProblem(sys,
remove_units(Dict(
sys.cone.m =>0u"m",
sys.cone.h =>0u"m",
)),
(0, 1),
remove_units(Dict(
sys.fixed_mass_flow.ṁ =>15u"kg/s",
sys.cone.cone_angle => (π/24)u"rad",
)),
)
sol =solve(prob; saveat =0.1)
MWE no units
using ModelingToolkit, OrdinaryDiffEq
using ModelingToolkit: t_nounits as t, D_nounits as D
functionCSA_of_right_cone(
fill_height; # m
cone_height =1, # m
cone_angle = π/12, # rad, angle between cone base and vertex
)
r = (cone_height - fill_height)/tan(cone_angle) # mreturn π*(r^2) # m²endfunctioncone_volume(;
cone_height =1, # m
cone_angle = π/12, # rad
)
return1//3* π * cone_height * (cone_height/tan(cone_angle))^2end# Define components like in ModelingToolkitStandardLibrary, but with Unitful units defined@connector HydraulicPort begin@variablesbeginp(t) # Paṁ(t), [connect = Flow] # kg/sendend@mtkmodel PressureReference begin@parametersbegin
p =101325, [description ="Fixed reference pressure"] # Paend@componentsbegin
port =HydraulicPort(name =:port)
end@equationsbegin
port.p ~ p
endend@mtkmodel FixedMassFlow begin@parametersbegin
ṁ =1, [description ="Fixed massflow"] # kg/send@componentsbegin
port =HydraulicPort(name =:port)
end@equationsbegin
port.ṁ ~-ṁ
endend# Define a cone shape which we can fill with fluid@mtkmodel Cone begin@parametersbegin
cone_height =1, [description ="Cone height"] # m
cone_angle = π/12, [description ="Cone angle"] # radend@componentsbegin
atm_port =HydraulicPort(name =:atm_port)
fill_port =HydraulicPort(name =:fill_port)
end@constantsbegin
ρ =1000, [description ="Fluid density"] # kg/m³
g =9.81, [description ="Gravitational acceleration"] # m/s²end@variablesbeginm(t), [description ="Fluid mass in cone"] # kgh(t), [description ="Fluid level in cone"] # mp(t), [description ="Pressure at cone base"] # PaA(t), [description ="Cone cross section area at fluid level in cone"] # m²end@equationsbegin
A ~CSA_of_cone(h; cone_height = cone_height, cone_angle = cone_angle)
D(m) ~ fill_port.ṁ
D(h) ~D(m)/(ρ*A)
p ~ atm_port.p + (ρ*g*h)
endend@mtkmodel ConeFill begin@componentsbegin
pressure_reference =PressureReference(name =:pressure_reference)
fixed_mass_flow =FixedMassFlow(name =:fixed_mass_flow)
cone =Cone(name =:cone)
end@equationsbeginconnect(pressure_reference.port, cone.atm_port)
connect(fixed_mass_flow.port, cone.fill_port)
endend@mtkbuild sys =ConeFill()
prob =ODEProblem(
sys,
Pair[
sys.cone.m =>0,
sys.cone.h =>0,
],
(0, 1),
Pair[
sys.fixed_mass_flow.ṁ =>15,
sys.cone.cone_angle => π/24,
],
)
sol =solve(prob; saveat =0.1)
julia>versioninfo()
Julia Version 1.11.4
Commit 8561cc3d68d (2025-03-1011:36 UTC)
Build Info:
Official https://julialang.org/ release
Platform Info:
OS: macOS (x86_64-apple-darwin24.0.0)
CPU:4×Intel(R) Core(TM) i5-5250U CPU @ 1.60GHz
WORD_SIZE:64
LLVM: libLLVM-16.0.6 (ORCJIT, broadwell)
Threads:1 default, 0 interactive, 1 GC (on 4 virtual cores)
Environment:
JULIA_EDITOR = code
JULIA_NUM_THREADS =0
Additional context
I am still teaching myself how to use ModelingToolkit - if I overlooked something in the documentation which would resolve the DimensionError, please let me know where to look and this issue can be closed. Thank you!
The text was updated successfully, but these errors were encountered:
Describe the bug 🐞
A
DimensionError
is returned when physical units defined for a model are validated during@mtkbuild
operation, where the model passes a model variable to a user defined function.The error message appears to suggest that the validation is trying to compare the units of a model variable with a symbolic representation of the
Quantity
returned by the user defined function, rather than the units of this quantity.Expected behavior
The unit validation of the model check to be applied to the unit of the quantity returned by a user defined function, rather than a symbolic representation of the returned
Quantity
.Minimal Reproducible Example 👇
For a version without any
Unitful.jl
units, but otherwise modeling the same thing without a problem, please see the MWE below with title 'MWE no units'.MWE with units
MWE no units
Error & Stacktrace⚠️
Environment (please complete the following information):
using Pkg; Pkg.status()
using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
versioninfo()
Additional context
I am still teaching myself how to use ModelingToolkit - if I overlooked something in the documentation which would resolve the
DimensionError
, please let me know where to look and this issue can be closed. Thank you!The text was updated successfully, but these errors were encountered: