Skip to content

fix(units): meshvar arithmetic with a UWQuantity operand (#282)#283

Merged
lmoresi merged 1 commit into
developmentfrom
bugfix/meshvar-quantity-arithmetic-282
Jun 24, 2026
Merged

fix(units): meshvar arithmetic with a UWQuantity operand (#282)#283
lmoresi merged 1 commit into
developmentfrom
bugfix/meshvar-quantity-arithmetic-282

Conversation

@lmoresi

@lmoresi lmoresi commented Jun 24, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #282T - uw.quantity(500, 'K') (and +, *) raised TypeError: Cannot subtract UWQuantity from EnhancedMeshVariable even for compatible units, blocking the buoyancy form T - T_ref.

Cause

A MeshVariable's .sym is non-dimensional; the MathematicalMixin operators passed a UWQuantity (Pint-backed) straight into sympy, which can't operate on it.

Fix

Add a coercion branch to __add__/__sub__/__rsub__/__mul__/__rmul__/__truediv__/__rtruediv__: a unit-bearing scalar (.magnitude + .units) is reduced to its non-dimensional value via uw.non_dimensionalise before composing with .sym. Behaviour-preserving for all non-quantity operands.

Verification

  • T ± quantity, T * quantity compose with exact ND values (T - 500K == T.sym - 0.5); kK prefix reduces consistently.
  • No regression: test_0700/0750/0754 unchanged (the meshvar - sympy_scalar matrix quirk is pre-existing, confirmed on clean development).
  • Regression: tests/test_0756_meshvar_quantity_arithmetic.py.

Scope: variable on the LEFT. quantity - meshvar (quantity on the left) raises inside the UWQuantity class (doesn't defer to __rsub__) — a related but separate gap, noted for follow-up. Helps unblock the buoyancy in #263.

Closes #282.

Underworld development team with AI support from Claude Code

A MeshVariable's .sym lives in non-dimensional (model-unit) space, so the
MathematicalMixin arithmetic operators could not compose with a unit-bearing
scalar operand: T - uw.quantity(500, 'K') raised
  TypeError: Cannot subtract UWQuantity from EnhancedMeshVariable
even for compatible units (the Pint quantity reached sympy and failed to
sympify). This blocks the fundamental buoyancy form T - T_ref.

Add a coercion branch to the operand handling in __add__/__sub__/__rsub__/
__mul__/__rmul__/__truediv__/__rtruediv__: a UWQuantity (anything with
.magnitude and .units) is reduced to its non-dimensional value via
uw.non_dimensionalise before composing with the variable's .sym. Compatible
units (incl. prefixes like kK) reduce consistently via the model scaling; the
change is behaviour-preserving for all non-quantity operands (sympy / variable /
plain scalar hit the same branches as before — verified the sympy-scalar matrix
quirk is pre-existing, not introduced here).

Scope: variable on the LEFT (meshvar ± / * quantity). 'quantity - meshvar'
(quantity on the left) is handled by the UWQuantity class and is out of scope.

Verified: T ±/* quantity compose with exact ND values (T - 500K == T.sym - 0.5),
kK prefix consistent, no regression (test_0700/0750/0754 unchanged). Regression:
tests/test_0756_meshvar_quantity_arithmetic.py.

Helps unblock the buoyancy expression in PR #263.

Closes #282.

Underworld development team with AI support from Claude Code
Copilot AI review requested due to automatic review settings June 24, 2026 03:26
@lmoresi lmoresi merged commit d430ea3 into development Jun 24, 2026
@lmoresi lmoresi deleted the bugfix/meshvar-quantity-arithmetic-282 branch June 24, 2026 03:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants