Skip to content

Commit

Permalink
Merge pull request #3036 from SciML/myb/const_unit
Browse files Browse the repository at this point in the history
We cannot fold unitful constants
  • Loading branch information
YingboMa authored Sep 10, 2024
2 parents 2bf3401 + 5178eb1 commit 2b90f68
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/structural_transformation/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ function find_eq_solvables!(state::TearingState, ieq, to_rm = Int[], coeffs = no
all_int_vars = false
if !allow_symbolic
if allow_parameter
all(ModelingToolkit.isparameter, vars(a)) || continue
all(
x -> ModelingToolkit.isparameter(x) || ModelingToolkit.isconstant(x),
vars(a)) || continue
else
continue
end
Expand Down
6 changes: 5 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,11 @@ function fold_constants(ex)
maketerm(typeof(ex), operation(ex), map(fold_constants, arguments(ex)),
metadata(ex))
elseif issym(ex) && isconstant(ex)
getdefault(ex)
if (unit = getmetadata(ex, VariableUnit, nothing); unit !== nothing)
ex # we cannot fold constant with units
else
getdefault(ex)
end
else
ex
end
Expand Down
10 changes: 10 additions & 0 deletions test/dq_units.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@ DD = Differential(tt)
eqs = [DD(X) ~ p - d * X + d * X]
@test ModelingToolkit.validate(eqs)

@constants begin
to_m = 1, [unit = u"m"]
end
@variables begin
L(t), [unit = u"m"]
L_out(t), [unit = u"1"]
end
@test to_m in ModelingToolkit.vars(ModelingToolkit.fold_constants(Symbolics.unwrap(L_out *
-to_m)))

# test units for registered functions
let
mm(X, v, K) = v * X / (X + K)
Expand Down

0 comments on commit 2b90f68

Please sign in to comment.