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

Fix unit conversion for expressions #2998

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,14 @@ function convert_units(varunits::Unitful.FreeUnits, value::AbstractArray{T}) whe
Unitful.ustrip.(varunits, value)
end

function convert_units(varunits::Unitful.FreeUnits, value::Num)
value
end

function convert_units(varunits::DynamicQuantities.Quantity, value::Num)
value
end

function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types)
vv, def, metadata_with_exprs = parse_variable_def!(
dict, mod, arg, varclass, kwargs, where_types)
Expand Down
18 changes: 18 additions & 0 deletions test/units.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,21 @@ end

@variables x(t)
@test ModelingToolkit.get_unit(sin(x)) == ModelingToolkit.unitless

@mtkmodel ExpressionParametersTest begin
@parameters begin
v = 1.0, [unit = u"m/s"]
τ = 1.0, [unit = u"s"]
end
@components begin
pt = ParamTest(; a = v * τ)
end
end

@named sys = ExpressionParametersTest(; v = 2.0u"m/s", τ = 3.0u"s")
sys = complete(sys)
# TODO: Is there a way to evaluate this expression and compare to 6.0?
@test isequal(ModelingToolkit.getdefault(sys.pt.a), sys.v * sys.τ)
@test ModelingToolkit.getdefault(sys.v) ≈ 2.0
@test ModelingToolkit.getdefault(sys.τ) ≈ 3.0

Loading