Skip to content

Commit 2b90f68

Browse files
authored
Merge pull request #3036 from SciML/myb/const_unit
We cannot fold unitful constants
2 parents 2bf3401 + 5178eb1 commit 2b90f68

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/structural_transformation/utils.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ function find_eq_solvables!(state::TearingState, ieq, to_rm = Int[], coeffs = no
203203
all_int_vars = false
204204
if !allow_symbolic
205205
if allow_parameter
206-
all(ModelingToolkit.isparameter, vars(a)) || continue
206+
all(
207+
x -> ModelingToolkit.isparameter(x) || ModelingToolkit.isconstant(x),
208+
vars(a)) || continue
207209
else
208210
continue
209211
end

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)