Skip to content

Commit

Permalink
fix: pass VariableUnit of diff var while converting it to a Term
Browse files Browse the repository at this point in the history
Units are defined only in MTK, so pass this piece of info to `Symbolics.diff2term` to add this to the returning term-var.
  • Loading branch information
ven-k committed May 1, 2024
1 parent a119d82 commit 7fd86ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ function default_toterm(x)
end
x = normalize_to_differential(op)(arguments(x)...)
end
Symbolics.diff2term(x)
term_unit = Dict(VariableUnit => safe_get_unit(
x, "Ignoring the unit while converting `$x` to a term.\n"))
Symbolics.diff2term(x, term_unit)
else
x
end
Expand Down
14 changes: 13 additions & 1 deletion test/variable_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ new = (((1 / β - 1) + δ) / γ)^(1 / (γ - 1))
using ModelingToolkit: isdifferential, vars, collect_differential_variables,
collect_ivs
@variables t u(t) y(t)
D = Differential(t)
using ModelingToolkit: D
eq = D(y) ~ u
v = vars(eq)
@test v == Set([D(y), u])
Expand All @@ -32,3 +32,15 @@ aov = ModelingToolkit.collect_applied_operators(eq, Differential)

ts = collect_ivs([eq])
@test ts == Set([t])

# Test units of diff vars of `Term` type.
using ModelingToolkit: t, get_unit, default_toterm
@variables k(t) [unit = u"kg"]
k2 = value(D(D(k)))
@test get_unit(default_toterm(k2)) == get_unit(k2)

@variables l(t) [unit = u"mg"]
l2 = value(D(D(l)))
@test_logs (:warn,
"""Ignoring the unit while converting `Differential(t)(Differential(t)(l(t)))` to a term.
1.0e-6 kg uses non SI unit. Please use SI unit only.""") default_toterm(value(l2))

0 comments on commit 7fd86ab

Please sign in to comment.