Skip to content

Arithmetic on a derivative; non_dimensionalise takes expressions (tutorials 3, 12, 15) - #1304

Open
lmoresi wants to merge 1 commit into
developmentfrom
fix/derivative-arithmetic-and-nondim-expressions
Open

lmoresi wants to merge 1 commit into
developmentfrom
fix/derivative-arithmetic-and-nondim-expressions

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 20, 2026

Copy link
Copy Markdown
Member

What changes for users:

  • s.diff(x) + s.diff(y) and other arithmetic on a variable's derivative work, as in tutorial 3.
  • uw.non_dimensionalise and uw.scaling.non_dimensionalise take a uw.expression, and an expression or Matrix built from fields. So a mesh takes its lengths as expressions, as in tutorial 15's annulus, and tutorial 12's uw.non_dimensionalise(temperature / velocity[0]) gives its model-unit form.

UNFILED 40 and 41, found in the tutorials run and fixed by the units desk (a Claude session). Runs were on a 96-thread Linux workstation (single system image; Intel Xeon Gold 6240R, AVX-512; MPICH), on development da35d14.

UNFILED 40: arithmetic on a derivative

MeshVariable.diff() returns a UnitAwareDerivativeMatrix. It indexes into unit-aware elements, but had no arithmetic, with or without a units model: s.diff(x) + s.diff(y) and s.diff(x) + 1 raised TypeError. It now adds, subtracts, multiplies, divides and negates. The result is the plain sympy Matrix of the same arithmetic on the underlying derivatives, so it equals the s.sym.diff(x) form, and get_units reads its units from the derivatives in it. A scalar applies to every entry; sympy itself refuses a Matrix plus a scalar, and for a scalar field's 1×1 derivative this is plain addition.

UNFILED 41: non-dimensionalising an expression

  • uw.scaling.non_dimensionalise, which the mesh constructors use, refused a uw.expression holding a quantity. It looked for .unitless, then multiplied by a pint Unit, giving DimensionalityError from km to dimensionless. A named expression holding a quantity or a number now takes that value's path.
  • uw.non_dimensionalise refused an expression or Matrix built from fields ("Cannot non-dimensionalise object of type MutableDenseMatrix"). Such an expression now gives its model-unit form, uw.unwrap(expr, keep_constants=False): every constant as its model value, every field as the model-unit value it stores. The scaling entry point hands these to it.
  • A named expression holding a symbolic value used to return that value unchanged through .data. It now returns the value's model-unit form.

Tests

  • tests/test_0776_derivative_matrix_arithmetic.py, 3 tests: every operation against the s.sym form as a sympy Matrix; a scalar on every entry; and the units of a sum of two derivatives (K/m).
  • tests/test_0777_non_dimensionalise_expressions.py, 6 tests:
    • a named expression holding a quantity, through both entry points;
    • an Annulus with its radii as expressions, against one with quantities;
    • tutorial 12's temperature / velocity[0];
    • a symbolic named expression;
    • a round trip through dimensionalise.

On the previous build, 7 of the 9 fail. The two that pass are uw.non_dimensionalise of a named expression holding a quantity, which worked already, and the round trip.

The tutorials

  • Tutorial 15 now runs to the end: 25 timesteps, given its output folder html5/. Without the folder it stops at the last cell, which writes an HTML file there.
  • Tutorial 3 gets past the derivative sum, and stops later, at b.sym on line 299: uw.function.derivative(..., evaluate=False) returns a sympy Derivative, which has no .sym. That is outside this PR.
  • Tutorial 12 stops on its own unit error, which docs/tutorials: four beginner tutorials updated to run as notebooks (#944) #1293 fixes.

Regression runs

  • Serial: on 3d9ea0b, the bands that read units, evaluate and meshes (test_05* to test_08*), 6 xdist workers: 1661 passed, 0 failed.
  • Parallel: scripts/test.sh --parallel-only --p 2 on 3d9ea0b: all passed.

Checklist (UW3 Style Charter)

  • Followed the Style Charter over the surrounding code (S2)
  • No drive-by refactors (S9)
  • Regression tests written first, with level_* and tier_* markers (S8)
  • No hedging names, no commented-out code (S3, S4)
  • No new exception swallow (S4)
  • No dependency changes
  • AI-assisted work carries the attribution line

Underworld development team with AI support from Claude Code

UNFILED 40: MeshVariable.diff() returned a UnitAwareDerivativeMatrix with
no arithmetic, so tutorial 3's s.diff(x) + s.diff(y) raised TypeError. It
now adds, subtracts, multiplies, divides and negates as its sympy Matrix
does; a scalar applies to every entry; get_units reads the result's units
from the derivatives.

UNFILED 41: uw.scaling.non_dimensionalise refused a uw.expression holding a
quantity (DimensionalityError, km to dimensionless), so an Annulus with its
radii as expressions failed (tutorial 15), and uw.non_dimensionalise refused
an expression or Matrix built from fields (tutorial 12's
temperature / velocity[0]). A named expression holding a quantity or number
takes that value's path; any other expression, or a Matrix, gives its
model-unit form, uw.unwrap(expr, keep_constants=False).

Underworld development team with AI support from Claude Code
Copilot AI balanced review requested due to automatic review settings November 20, 2026 16:16

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 insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
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.

3 participants