Skip to content

A second mesh is not handed the first mesh's coordinate symbols (extract_meshes, rotated normal) - #1220

Merged
lmoresi merged 6 commits into
developmentfrom
bugfix/multi-mesh-coordinate-identity
Nov 16, 2026
Merged

lmoresi merged 6 commits into
developmentfrom
bugfix/multi-mesh-coordinate-identity

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

What changes for users: a second mesh in one process, after a model reset or in the same model, is no longer refused when it uses coordinates the first mesh's expressions also used: no more "analytic normal … not mesh coordinates" or "foreign mesh" for a correct problem.

There is no issue for this one; this body is the report. It was found in the review of #1205 and characterised by H1 and H3 (H1 is the parallel review desk, H3 the units desk; both run on a 96-thread Linux workstation (single system image; Intel Xeon Gold 6240R, AVX-512; MPICH)).

The defect

Take a first model with an expression over its own mesh's coordinates, a viscosity 1 + 20(x² + y²) say. Then, after a reset, or simply in the same model, build a FreeSurface on a new mesh with a coordinate-dependent viscosity. Its solve was refused, either way:

  • "analytic normal for boundary 'Upper' contains symbols ['N.x', 'N.y'] that are not mesh coordinates";
  • "Solver expressions contain MeshVariable symbols from 1 foreign mesh(es)".

It happened with and without units, and whatever the first model did: built, solved, refused, or a plain Stokes solve. A constant viscosity on either side passed.

Which objects are equal across meshes, and why

Measured on development, for two meshes made one after the other:

object equal across meshes?
raw coordinate BaseScalars (mesh.N.x) equal, same hash, different objects. Both coordinate systems are "N" with equal arguments, so SymPy compares them equal. This is #501's canonicalisation.
UWCoordinates (mesh.X[0]) unequal: each equals only its own mesh's raw scalar, by identity
a variable's function (T(N.x, N.y)) unequal: a different class per mesh
an expression over raw scalars (1 + 20x²) equal, and SymPy's cache returns the first mesh's object

unwrap returns the raw scalars. So an expression rebuilt over the coordinates on a second mesh comes back from SymPy's cache holding the first mesh's scalars. That needs no reset: two meshes in one model meet it too.

Where the second mesh is handed the first's objects: a cache, not a shared name

unwrap of a named expression over the coordinates returns one object for two meshes: the same object, holding one mesh's UWCoordinates. This is a cache keyed on structure, not a named expression shared between meshes. Measured:

  • The two named expressions are distinct objects with different names.
  • Unwrapping the second mesh's expression first flips which mesh's coordinates both results hold.

The same holds in a units model, and with #1209.

A derived solver's copied viscosity (FreeSurface's unwrap(...).subs(rebind)) therefore carries the first mesh's coordinates into the second mesh's solver.

Does that change the answer? No. The compiled kernels name coordinates by their names, and evaluation passes positions, so the coordinates' mesh does not enter the numbers. Two steps of a FreeSurface on a second mesh, after a first model (a solved Stokes, or a FreeSurface built and never solved), give DM coordinates identical to a fresh process, bit for bit.

The fix

  1. function/expressions.extract_meshes (behind the solver's foreign-mesh check) identifies meshes from variable functions only. Coordinates are not counted:

    • a coordinate is a position, not mesh-owned data: another mesh's x evaluated here is the same place;
    • two meshes' coordinates are hash-equal and structurally equal, so they cannot say which mesh an expression belongs to.

    Variables carry their mesh in their function class (A re-declared mesh variable refuses to change; helper variables named after their owner; uw.unwrap expands completely (#1118, #824) #1209), so a genuine mixture of two meshes' variables is still refused.

  2. utilities/rotated_bc._normal_source re-tags the unwrapped normal's coordinates by name onto the solver's mesh before its check that the normal's only free symbols are this mesh's coordinates. fault_contact.py already does the same. One consequence: a normal written in another mesh's x and y is accepted. That follows from the rule of matching coordinates by name.

No cache is cleared, and the coordinates are left as they are. An earlier draft cleared SymPy's cache in reset_default_model. That treated the symptom, slowed everything after a reset, and would not have covered two meshes in one model. Making each mesh's coordinates distinct to SymPy, hash included, would change equality for every cross-mesh expression: child meshes, adapt copies, and #501's name-matching consumers. W1 reviewed this choice, and reviews both hunks.

Tests

  • tests/test_0745_two_models_in_one_process.py, 8 cases: a first model (a solved Stokes on a box with 1 + 20(x² + y²), or a FreeSurface built and never solved), then a FreeSurface on an annulus with the same form of viscosity, with and without a reset between, with and without units.
    • On development 35ac139 all 8 fail ("analytic normal … not mesh coordinates").
    • Here all 8 pass.
  • tests/test_1070_free_surface_in_a_units_model.py. Its ordering note is gone, and the drift test runs before the warm-start test again, the order that failed. It passes in any order.

Verification (every tests/test_*.py excluding tier C, 6 xdist workers):

  • Serial: 4355 passed, 1 failed. The failure is test_1086_mmpde_seam_matches_serial::…_with_slip_matches_serial[4], whose spawned reference goes silent under load; it passes alone.
  • scripts/test.sh --parallel-only --p 2: all passed.

These ran at 573baab, before the last merge of development (232902c); CI runs the merged head.

CHANGELOG

Two lines H1 noted were missing from #1205's entry: SwarmVariable.array assignment converts or refuses (#993), and coordinates marked dimensionless are taken as model coordinates whatever their size (#1081).

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)
  • Parallel safety: no collective added; the np 2 pass is in the verification run (S11)
  • No dependency changes
  • AI-assisted work carries the attribution line

Underworld development team with AI support from Claude Code

… is re-tagged onto its mesh

Two meshes' coordinate systems are both "N" with equal arguments, so their raw
coordinate BaseScalars compare equal (#501), and an expression rebuilt over
the coordinates on a second mesh comes back from SymPy's cache holding the
first mesh's scalars. UWCoordinates and variable functions are unequal across
meshes; raw scalars are not. Two consumers took a raw scalar for evidence of
its mesh:
- extract_meshes read a raw scalar's .mesh, so a solver on the second mesh
  was refused as using a foreign mesh. It now identifies meshes from
  UWCoordinates and variable functions only.
- rotated_bc._normal_source compared the unwrapped normal's raw scalars with
  mesh.X, so it called a correct normal 'not mesh coordinates'. It re-tags
  them by name onto the solver's mesh first, as fault_contact does.
No cache is cleared. The test runs two meshes in one process, with and
without a model reset between, with and without units.

Underworld development team with AI support from Claude Code
…sionless coordinates

With the coordinate-identity fix the drift test no longer leaves anything for a
later FreeSurface to trip over, so its ordering note is gone and it runs before
the warm-start test again.

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

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.

@lmoresi

lmoresi commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

Code review of the two hunks (W1, the integration session; read at the head, not run)

Both hunks are right and contained.

extract_meshes. Counting a mesh only from UWCoordinates and variable functions is correct, since a raw coordinate scalar is no evidence of a mesh. I checked the four callers, at the head, for what an empty set or a smaller set now does:

  • petsc_generic_snes_solvers.pyx, the foreign-mesh refusal. It now refuses only real foreign symbols (another mesh's UWCoordinate or variable). Before, a raw scalar from SymPy's cache could make it refuse a correct expression. That is the defect fixed.
  • functions_unit_system.py, the mixed-mesh check in evaluate. The same: fewer false refusals, and no genuine mixed-mesh expression slips through, because a variable's mesh still comes from its function class.
  • functions_unit_system.py, the monotone path, and remesh.py's monotone eligibility. Both need exactly one mesh, from a single MeshVariable. A coordinate-only expression, which unwrapping turns into raw scalars, now counts zero meshes. Monotone refuses it, and remesh's eligibility test returns False. Both are the right answers for "not backed by one variable".

_normal_source. Re-tagging by name onto solver.mesh.X before the stray check is fault_contact's pattern. It is safe: only symbols whose name is one of this mesh's coordinate names, and that are not already its objects, are replaced. A genuinely foreign symbol, such as another mesh's UWCoordinate with a different name, or t, stays stray and is still refused.

Interaction with #1209: none. #1209 changed UnderworldFunction's identity, the mesh plus the variable's clean name, which affects mesh-variable functions only. Coordinate BaseScalars, raw or UWCoordinate, are untouched by it. If anything the two agree: #1209 stops two variables that share a display name from being one function, as this stops a raw scalar standing in for a mesh.

The grep for other consumers of a raw scalar's .mesh. Over every src/ file that handles BaseScalars, extract_meshes was the only one. The .mesh properties in coordinates.py belong to the coordinate accessors, read through their coordinate system, not to BaseScalars.

Stray-coordinate checks against mesh.X exist in three places:

  • fault_contact, which already re-tags;
  • _normal_source, fixed here;
  • Mesh.nullspace_rotations' setter, in discretisation_mesh.py.

The setter checks m.free_symbols <= set(self.X) with no re-tag. It is not reached through unwrap: modes come from the user's mesh.X or _inherit_rotation_modes, which maps parent.X to self.X by xreplace. A user who builds a mode from an unwrapped expression would meet the same false refusal. If you want the one rule everywhere, the re-tag could be a shared helper used by all three; that is not needed for this PR.

Underworld development team with AI support from Claude Code

@lmoresi

lmoresi commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

Review from the Underworld development team with AI support from Claude Code (H1: review and parallel test runs on a 96-thread Linux workstation (Intel Xeon Gold 6240R, AVX-512; MPICH))
(H1 is the session that reviews pull requests and runs the parallel tests.)

Verdict on head 0ad71c3: changes requested. The PR fixes the leak when the first model has solved: a solved FreeSurface or a plain Stokes solve, followed by a FreeSurface on a new mesh. The second model then gives exactly the answer of a fresh process. The PR does not fix the leak when the first FreeSurface was built and never solved, or had its solve refused. That is the case the drift test in test_1070 creates, and test_1070 fails in its own file order on this head.

Build: only .py files change. The merge base is development 04d6f84, whose compiled sources match the aa3c49d build, so all runs used a Python overlay on that build. The development arm is 04d6f84.

Leak matrix

Each run builds a first model, then a FreeSurface on a new annulus mesh with viscosity model_units(1, "Pa*s") * (1 + 20 (x² + y²)), and solves it and advances it one step. Each first-model kind ran four ways: with and without units, and with and without reset_default_model between the two models.

first model development 04d6f84 (4 runs each) this PR (4 runs each)
none (fresh process) ok ok
plain Stokes solve, coordinate-dependent viscosity, on a box fails: "analytic normal … not mesh coordinates" ok, and equal to the fresh process (max difference 0 in DM coordinates, u and p)
FreeSurface built and solved fails: "analytic normal …" ok, and equal to the fresh process (difference 0)
FreeSurface built, never solved fails: "foreign mesh" fails: "foreign mesh"
FreeSurface whose solve was refused (drift) fails: "foreign mesh" fails: "foreign mesh"

Giving the viscosity a uniquely named uw.expression scale, as test_1070 does, still fails in the two remaining rows.

Findings

1. High: the leak remains for a FreeSurface that was built but not solved, and test_1070 fails in file order

Cause: in the failing rows, extract_meshes flags the second FreeSurface's held and consistent solvers. Their copied shear_viscosity_0, 20.0*N.x**2 + 20.0*N.y**2 + 1.0, holds the first mesh's UWCoordinates, not raw BaseScalars. The copy goes through unwrap(…).subs(rebind), and SymPy's cache hands back an object built on the first mesh. The PR stops trusting .mesh on raw BaseScalars only. UWCoordinates of two meshes compare equal too (#501), so the same cache hit gets past the change through them.

test_1070: the PR moves the drift test ahead of the warm-start test and says the file runs in any order. Here the file's own order fails test_the_consistent_solve_is_warm_started_with_the_stored_velocity with the foreign-mesh error, three times out of three:

  • the file run alone, in file order;
  • the file run with test_0745;
  • inside the 87-file xdist run.

Reversed order, and two seeded shuffles of test_0745 with test_1070, pass. In one shuffle the drift test ran immediately before the warm-start test, so whether the failure appears depends on the earlier history, not only on adjacency.

Fix: treat the derived solvers' copied coordinates the way rotated_bc now treats the normal. Re-tag coordinates by name onto solver.mesh.X in _copy_constitutive_model, or in general wherever an unwrapped expression is re-attached to a solver. Alternatively, have extract_meshes identify a mesh from variable functions only. Then a direct mesh_A.X term in a solver on mesh B is no longer refused; that is harmless, since a Cartesian coordinate is a position, but it changes behaviour. Either way, a test_0745 case with a first FreeSurface that is built and never solved would cover this row.

2. Low: a normal written in another mesh's coordinates is now accepted

With a 2-D solver, _normal_source now accepts a normal written in a 3-D mesh's x, y, re-tagged by name. Development refuses it. A Cartesian x is the same position on any mesh, so this is acceptable. A component that uses z in a 2-D solver is still refused, as only N.z stray, so the re-tag cannot mis-tag across dimensions. Mention it in the changelog line if it matters to anyone.

Confirmed correct

  • Answers, not just no error: every first-model case that now passes gives the second model bit for bit the answer of a fresh process. The DM coordinates after one step, u and p all have max difference 0, with and without units, with and without a reset.
  • Two meshes in one model, no reset: two Stokes solvers on two boxes, with structurally identical coefficients 1 + 20 (x² + y²), solved alternately twice. Each matches its solo run to 0, with and without units. Development also passes this case.
  • Foreign expressions are still refused: for a Poisson solver on mesh B, a coefficient using mesh A's mesh.X, mesh A's raw unwrapped coordinates, or a MeshVariable on mesh A is refused, as on development. Its own coordinates are accepted. I found no case that a hidden acceptance would let through.
  • Re-tag across dimensions: a 3-D box and a 2-D annulus in one process.
    • The 2-D solver's own radial normal, built after the 3-D mesh, now works ([0.6, 0.8] at (0.6, 0.8)); development refuses it.
    • A 3-D solver with a normal written on the 2-D x, y gives [0.6, 0.8, 0].
    • A z component in a 2-D solver is refused.
  • test_0745: fails 4 of 4 on development and passes 4 of 4 here.
  • CHANGELOG: the SwarmVariable.array and dimensionless-coordinate lines are in.

What was run

  • Leak matrix: 18 runs on each of this PR and development, as in the table above.
  • Probes:
    • two meshes alternating, compared with their solo runs;
    • foreign-expression refusal;
    • the 2-D and 3-D re-tag.
  • Orders, serial:
    • test_1070 in file order and reversed;
    • test_0745 with test_1070 in reversed order and in two seeded shuffles (8 passed each);
    • test_1070's file order failed as described in finding 1.
  • Serial, this branch: the 87 units, expression, rotated-BC, free-surface and two-model test files, with 4 xdist workers, excluding tier C. Result: 742 passed, 9 skipped, 10 xfailed, 1 failed (the test_1070 failure in finding 1).
  • Parallel, np 2, this branch: 0770, 0785, 0992, 1063, 1064, 1066, 1068, 1131, 1140 and 1142. Result: 54 passed, 3 skipped, 0 failed.
  • Not run:
    • the full suite;
    • np 4;
    • this branch's serial set on development as a whole (only test_0745 and test_1070 were run there).

Logs and probe scripts are kept by the reviewer.

Underworld development team with AI support from Claude Code

…nd H1's review of #1220)

Two meshes' UWCoordinates are unequal under == but hash-equal and structurally
equal, and unwrap of an expression over the coordinates returns one object for
both meshes, holding the first mesh's UWCoordinates: a derived solver's copied
viscosity carried them into the second mesh's solver. A coordinate is a
position, not mesh-owned data, so coordinates no longer identify a mesh at all;
variables carry their mesh in their function class (#1209). The rotated
normal's re-tag stays, for its own check (a normal written in another mesh's x
and y is accepted, by name). test_0745 adds a first FreeSurface that is built
and never solved.

Underworld development team with AI support from Claude Code
@lmoresi

lmoresi commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

Review from the Underworld development team with AI support from Claude Code (H1: review and parallel test runs on a 96-thread Linux workstation (Intel Xeon Gold 6240R, AVX-512; MPICH))
(H1 is the session that reviews pull requests and runs the parallel tests.)

Re-check of head 232902c: ready to merge on green from this review. The state leak is gone in every case I tried, including a first FreeSurface that was built but never solved and one whose solve was refused. In each, the second model's answer is bit for bit the answer of a fresh process. test_1070 passes in every order I tried. A variable from another mesh is still refused.

Build: the merge base is development 35ac139. Between the aa3c49d build and this head only .py files differ, so both arms are Python overlays on that build. The development arm is 35ac139. The 0189b21 build was not usable, because _function.pyx differs.

item at 0ad71c3 at 232902c
Leak matrix: first model (Stokes solved, FreeSurface solved, built only, or refused), with and without units, with and without a reset, then a FreeSurface on a new annulus with a coordinate-dependent viscosity Fixed only when the first model had solved. Built-only and refused still failed with "foreign mesh". All 16 runs pass, and all are bit-identical to the fresh-process run in DM coordinates, u and p. Development 35ac139 fails all 16.
test_1070 order Failed in its own file order Passes in 7 orders: file order alone, before test_0745, after test_0745, reversed, three seeded shuffles. Also passes inside the 87-file xdist run.
test_0745 4 cases 8 cases, all passing here. On development all 8 fail when run on their own. In a whole-file run on development, 1 of the 8 passes, depending on what ran before it.
Foreign expression, solver on mesh B: mesh A's mesh.X Refused Accepted, with an answer identical to the same solve with mesh B's own coordinates. This is harmless: a Cartesian coordinate is a position.
Foreign expression: mesh A's raw coordinates Refused Accepted, with an identical answer.
Foreign expression: a MeshVariable on mesh A Refused Refused, as on development.
Two meshes in one model, structurally identical coefficients 1 + 20 (x² + y²), solved alternately Matched the solo runs Bit-identical to the solo runs, with and without units.
Other extract_meshes callers (evaluate's mixed-mesh check, the monotone source check, remesh) (unchanged) All of them are about variables, so dropping coordinates as evidence does not change what they decide.

Findings

  1. Low: a coordinate from another mesh is now accepted wherever the mesh check runs: in a solver, in evaluate's mixed-mesh check, and, since the previous head, in the rotated normal. Every case I measured gives the same answer as the solver mesh's own coordinates. The CHANGELOG could say in one line that only variables tie an expression to a mesh.

Nothing blocks the merge.

What was run at 232902c

run result
Leak matrix, 36 runs (this head and development) this head: 18 of 18 ok, bit-identical to the fresh process; development: 16 of 16 non-fresh runs fail
Probes: foreign expression, two meshes alternating as in the table above
test_1070 and test_0745, serial, in 7 orders 12 passed in each order (4 for test_1070 alone)
Serial slice of 87 files (units, expressions, rotated BC, free surface, two models), 4 xdist workers, excluding tier C 747 passed, 9 skipped, 10 xfailed, 0 failed
np 2 slice: 0770, 0785, 0992, 1063, 1064, 1066, 1068, 1131, 1140, 1142 and 1074 (free surface on an empty rank) 54 passed, 4 skipped, 0 failed

Not run: the full suite, and np 4.

Logs and probe scripts are kept by the reviewer.

Underworld development team with AI support from Claude Code

@lmoresi
lmoresi merged commit bf584c0 into development Nov 16, 2026
10 checks passed
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