Skip to content

Commit 01d2daf

Browse files
committed
Fix #2959
1 parent be151e3 commit 01d2daf

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

src/structural_transformation/StructuralTransformations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using SymbolicUtils: maketerm, iscall
1111

1212
using ModelingToolkit
1313
using ModelingToolkit: ODESystem, AbstractSystem, var_from_nested_derivative, Differential,
14-
unknowns, equations, vars, Symbolic, diff2term, value,
14+
unknowns, equations, vars, Symbolic, diff2term_with_unit, value,
1515
operation, arguments, Sym, Term, simplify, symbolic_linear_solve,
1616
isdiffeq, isdifferential, isirreducible,
1717
empty_substitutions, get_substitutions,

src/structural_transformation/symbolics_tearing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching,
274274
dv === nothing && continue
275275
if var_eq_matching[var] !== SelectedState()
276276
dd = fullvars[dv]
277-
v_t = setio(diff2term(unwrap(dd)), false, false)
277+
v_t = setio(diff2term_with_unit(unwrap(dd), unwrap(iv)), false, false)
278278
for eq in 𝑑neighbors(graph, dv)
279279
dummy_sub[dd] = v_t
280280
neweqs[eq] = fast_substitute(neweqs[eq], dd => v_t)

src/utils.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,3 +859,13 @@ function eval_or_rgf(expr::Expr; eval_expression = false, eval_module = @__MODUL
859859
return drop_expr(RuntimeGeneratedFunction(eval_module, eval_module, expr))
860860
end
861861
end
862+
863+
function diff2term_with_unit(x, t)
864+
x = diff2term(x)
865+
if hasmetadata(x, VariableUnit) && (t isa Symbolic && hasmetadata(t, VariableUnit))
866+
xu = getmetadata(x, VariableUnit)
867+
tu = getmetadata(t, VariableUnit)
868+
x = setmetadata(x, VariableUnit, xu / tu)
869+
end
870+
return x
871+
end

0 commit comments

Comments
 (0)