Skip to content

Commit 5c5731e

Browse files
committed
We cannot fold unitful constants
Fix #2988
1 parent 690a659 commit 5c5731e

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/utils.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,11 @@ function fold_constants(ex)
836836
maketerm(typeof(ex), operation(ex), map(fold_constants, arguments(ex)),
837837
metadata(ex))
838838
elseif issym(ex) && isconstant(ex)
839-
getdefault(ex)
839+
if (unit = getmetadata(ex, VariableUnit, nothing); unit !== nothing)
840+
ex # we cannot fold constant with units
841+
else
842+
getdefault(ex)
843+
end
840844
else
841845
ex
842846
end

test/dq_units.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,16 @@ DD = Differential(tt)
226226
eqs = [DD(X) ~ p - d * X + d * X]
227227
@test ModelingToolkit.validate(eqs)
228228

229+
@constants begin
230+
to_m = 1, [unit = u"m"]
231+
end
232+
@variables begin
233+
L(t), [unit = u"m"]
234+
L_out(t), [unit = u"1"]
235+
end
236+
@test to_m in ModelingToolkit.vars(ModelingToolkit.fold_constants(Symbolics.unwrap(L_out *
237+
-to_m)))
238+
229239
# test units for registered functions
230240
let
231241
mm(X, v, K) = v * X / (X + K)

0 commit comments

Comments
 (0)