fix(units): meshvar arithmetic with a UWQuantity operand (#282)#283
Merged
Conversation
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
This was referenced Jun 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #282 —
T - uw.quantity(500, 'K')(and+,*) raisedTypeError: Cannot subtract UWQuantity from EnhancedMeshVariableeven for compatible units, blocking the buoyancy formT - T_ref.Cause
A MeshVariable's
.symis non-dimensional; theMathematicalMixinoperators passed aUWQuantity(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 viauw.non_dimensionalisebefore composing with.sym. Behaviour-preserving for all non-quantity operands.Verification
T ± quantity,T * quantitycompose with exact ND values (T - 500K == T.sym - 0.5);kKprefix reduces consistently.test_0700/0750/0754unchanged (themeshvar - sympy_scalarmatrix quirk is pre-existing, confirmed on clean development).tests/test_0756_meshvar_quantity_arithmetic.py.Scope: variable on the LEFT.
quantity - meshvar(quantity on the left) raises inside theUWQuantityclass (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