Skip to content

The level set's mass correction is one shift on every rank - #1234

Merged
lmoresi merged 3 commits into
developmentfrom
bugfix/level-set-mass-correction-one-shift
Nov 19, 2026
Merged

lmoresi merged 3 commits into
developmentfrom
bugfix/level-set-mass-correction-one-shift

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 19, 2026

Copy link
Copy Markdown
Member

A follow-up to the review of #1211. No issue is filed; the defect was found in that review.

What changes

LevelSetSolver._correct_mass finds the uniform shift that restores the enclosed volume, by a secant iteration. It starts from the band's area, estimated as the fraction of nodes inside the band times the domain area.

  • Before: the fraction was taken over each rank's own nodes. The volumes the iteration matches are global integrals, so each rank followed its own iterates and ended at its own shift.
  • Measured by the reviewer at np 2, on development and on The boundary-condition wrapper checks its label and PETSc's answer; a rewire replaces boundary kernels (#1138, #1069) #1211's annulus far-field case: the walls held 2.75e-4 on one rank and 3.33e-4 on the other, against 3.13e-4 serially. The level-set field was discontinuous at the partition boundary by the difference.
  • Now: the band and node counts are summed over the ranks. Every rank starts from the same guess and takes the same iterates.

This changes SLCN level-set results in parallel only. Serially the fraction is the same number as before.

Tolerances and coordinate matches

  • The test takes one SLCN step without the correction, then applies the correction. On the nodes neither field clips, the change must be one number on every node of every rank, to 1e-14 relative to max(|shift|, 1). It must also be nonzero, above 1e-8.
  • Coordinate matches: none. The nodes are selected by value (strictly inside (0, 1)).

Tests

tests/parallel/test_1105_level_set_mass_shift_is_one_constant_parallel.py. scripts/test.sh reaches it through its tests/parallel/test_*.py glob.

  • Serially: it passes on this branch.
  • In parallel: we do not run MPI on this machine (macOS, 12-core Apple M4). The reviewer runs it at np 2 and 3, on this branch and on development, where it should fail.

Serial level-set and material files on this branch: test_0071, 0073, 0116, 0132 and 1105. 49 passed, 1 skipped (shapely is not installed).

Issue state

No issue is open for this.

Underworld development team with AI support from Claude Code

The secant iteration for the volume-restoring shift started from the band's
area, estimated as the fraction of a rank's own nodes inside the band. The
volumes it then matched are global, so each rank followed its own iterates
and ended at its own shift (2.75e-4 and 3.33e-4 at np 2 against 3.13e-4
serially, measured in the review of #1211). The fraction is now counted over
every rank, so every rank takes the same first guess and the same iterates.

Underworld development team with AI support from Claude Code
Copilot AI balanced review requested due to automatic review settings November 19, 2026 20: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.

@lmoresi

lmoresi commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

Adversarial review (author's pass)

Parallel rank asymmetry.

  • The new allreduce comes after the early return abs(residual) < tol. The residual is a global integral, the same on every rank, so either every rank reaches the allreduce or none does.
  • Every later branch depends only on global volumes and the shared first guess, so the ranks cannot part.

Determinism. The ranks agree only if Integral.evaluate returns the same float on every rank. It reduces over the ranks, and the rank-independent shift the test asserts depends on that.

Ghost nodes. Ghost nodes are counted on each rank that holds them, so the band fraction is not exactly the serial one. It only seeds the secant, and the bracket and the tolerance decide where it stops, so the shift agrees with serial to the mass-correction tolerance, not to the bit.

A test that cannot fail. Serially the test passes on development as well. It can fail only from np 2, which is where the reviewer's run matters.

CI reach. The tests/parallel/test_*.py glob in scripts/test.sh reaches the test.

No findings to fix.

Underworld development team with AI support from Claude Code

@lmoresi

lmoresi commented Nov 19, 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

Head fce7acd: changes requested.

The fix is right. The secant's first guess is now counted over every rank, so every rank takes the same iterates and applies one shift:

  • the new test fails on development at np 2 and 3 and passes here;
  • the wall probe gives one value on every rank, where development gave two or three;
  • the volume is conserved to round-off at np 1, 2 and 3.

The change itself is collective-safe:

  • The early return in _correct_mass tests a residual built from interface_volume(), a global integral, so it is the same number on every rank.
  • The new allreduce comes after that return, so either every rank reaches it or none does.
  • Every later branch (bracket, secant step, convergence) reads only global volumes.

No rank-local residual exists to force apart.

What stops it is one line away, in the same solve():

  • _clip_in_place() decides its early return on each rank's own nodes, and then calls the collective interface_volume().
  • When one rank has nothing to clip and another does, the ranks deadlock.
  • That happens at np 3 on a plain box, on the Annulus with The boundary-condition wrapper checks its label and PETSc's answer; a rewire replaces boundary kernels (#1138, #1069) #1211, and in test_1105_levelset_rotation[slcn] at np 2. It happens on development too.
  • With the early return decided over the ranks (finding 1), every case below passes. Without it, the PR's own goal, one shift on every rank, cannot be observed at np 3.

Build. The PR changes only level_set.py and adds a test. Its merge base is development 4ab522a.

Results

The box is UnstructuredSimplexBox(-1..1, cellSize 0.1); the Annulus is Annulus(0.3, 1, cellSize 0.1). Each probe takes one SLCN step with far field 0. The wall values are gathered from every rank, owned and ghost nodes alike.

"clip fix" means the stand-in for finding 1, applied in the probe.

case np development 4ab522a PR fce7acd serial
test_1105_level_set_mass_shift_is_one_constant_parallel, --with-mpi 2 fails on both ranks passes on both ranks passes on both builds
the same 3 fails on all ranks passes on all ranks
Box, wall values after one step 2 two: 1.3734e-4 and 1.5134e-4 one: 1.4433e-4 1.4350e-4
Box, wall values 3 hang (finding 1) hang (finding 1)
Box, wall values, clip fix 3 three: 1.0452e-4, 1.0667e-4, 2.2539e-4 one: 1.4428e-4
Box, interface off-centre, band layout, clip fix 3 three: 1.5022e-4, 1.5346e-4, 4.1346e-4 one: 2.4031e-4 2.4023e-4
Box, an empty rank (emptyband), clip fix 3 hang in the SLCN history (finding 3) hang, the same place
Annulus, #1211 + #1234 2 SEGV without #1211 (the Centre loop) one: 3.0425e-4 3.1265e-4
Annulus, #1211 + #1234 3 SEGV without #1211 hang (finding 1); with the clip fix, one: 3.1001e-4
Volume drift after 10 steps, box, clip fix 1, 2, 3 9.7e-16, 3.3e-11, -7.8e-16 9.7e-16, 7.8e-16, 0.0
test_1069_boundary_wrapper_checks_and_replaces.py::test_the_level_set_far_field_on_an_annulus, #1211 + #1234 1, 2 2 passed (np 2: on both ranks; in round 4 the SLCN case failed on one rank)
the same 3 MPI abort (finding 1); with the clip fix, 2 passed on all 3 ranks
test_1105_levelset_rotation 2 SUPG passes; SLCN fails on one rank, then hangs the same
the same, clip fix 2 no hang. The volume-drift checks pass on both ranks for all 200 steps (SUPG and SLCN). One rank fails the band-share assertion (finding 5)
Level-set and SLCN tests (6 files: test_1105, test_1056_units_slcn_traceback, test_0610_navier_stokes_slcn_projection, test_0066, test_1071_slcn_moving_mesh, test_1164) 1 46 passed, 1 skipped

Why the wall values do not equal serial exactly: the initial field itself depends on the rank count (finding 2).

  • Box: V0 = 1.425385e-1 at np 1, 1.425404e-1 at np 2, 1.425917e-1 at np 3.
  • Annulus: V0 = 1.301395e-1 at np 1, 1.303066e-1 at np 2.
  • The target volume and the transported field therefore differ by np, and so does the shift.
  • Within each run, every rank now applies the same shift, and the volume returns to its own target to round-off.

Findings

1. High: _clip_in_place() decides a collective on each rank's own nodes

values = np.asarray(self.phi.array[:, 0, 0])
clipped = np.clip(values, 0.0, 1.0)
if np.array_equal(values, clipped):        # this rank's nodes only
    return 0.0
before = self.interface_volume()           # collective

How it fails:

Proposed fix:

comm = self.mesh.dm.getComm().tompi4py()
if not comm.allreduce(not np.array_equal(values, clipped), op=uw.MPI.LOR):
    return 0.0

With this in place (as a monkeypatch), all three hangs are gone:

  • the box at np 3 gives one wall value;
  • the Annulus at np 3 gives one wall value;
  • test_1069's level-set tests pass on all 3 ranks;
  • test_1105 runs its 200 steps at np 2 with the volume checks passing.

It is older than this PR. We ask for it here because it sits in the same solve() and is the next thing a parallel level-set run meets. Without it the PR's result cannot be seen from np 3.

2. Medium (older than this PR, needs an issue): the initial level-set field depends on the partition

interface_thickness() carries ε to the nodes "from the nearest cell centroid", using each rank's own cells. Near a partition boundary the nearest local centroid is not the global one.

  • ε: its integral over the Annulus is 2.43684e-1 at np 1, 2.43782e-1 at np 2 and 2.43832e-1 at np 3 (4e-4 relative).
  • The volume of the initial ψ: it moves by 1.4e-5 (box, np 2), 3.7e-4 (box, np 3) and 1.3e-3 (Annulus, np 2), relative.
  • Effect: a parallel run starts from a different field and target volume than a serial one.
  • Possible fix: build the centroid search over all cells (a global KD-tree, or the ghost cells), so that ε at a node does not depend on which rank holds it.

3. Medium (older than this PR, needs an issue): an SLCN step hangs with a rank that holds no cells

Case: under emptyband at np 3 (the last rank has no cells), the first LevelSetSolver.solve hangs on both builds.

  • Where the ranks wait:
    • the empty rank is in update_lvec, under uw.function.evaluate in _record_current_field_into_history (systems/ddt.py);
    • the other two are in pack_raw_data_to_petsc, under the __setitem__ of _write_evaluated.
  • Why: the write into the history store is collective on the ranks with nodes and is skipped on the empty rank.
  • Scope: this is in the SLCN history, not the level set. The level set's mass correction is one shift on every rank #1234 does not touch it.

4. Low: the band fraction counts ghost nodes

nodes is the sum of data0.size over the ranks, so a ghost node counts once per rank that holds it. The first guess then differs slightly from serial, but it is the same number on every rank. The secant converges to the same root in a few more or fewer iterations. Harmless; a note in the comment would do.

5. Low: test_1105_levelset_rotation checks a rank-local share

in_band = np.mean(...) over the local array is a per-rank share. At np 2 the rank that holds the circle has 30 % band nodes, against the < 0.2 limit. The test is serial, and CI runs it serially, so this does not affect CI. It would need a global count to run in parallel.

What was run

Not run:

  • the PR built with the clip fix in its source (only the monkeypatch);
  • 3-D;
  • np 4;
  • a PETSc debug build.

Underworld development team with AI support from Claude Code

…es on every rank (review of #1234)

_clip_in_place returned early on a rank with nothing to clip, before the
collective volume integrals the other ranks entered: at np 2 and 3 the SLCN
step hung (box, Annulus, test_1105_levelset_rotation[slcn]). The early return
is now decided over every rank. The box wall correction skipped its .array
write on a rank with no wall node, and a write outside a deferred context is
collective; its writes now go through uw.synchronised_array_update.

Underworld development team with AI support from Claude Code
@lmoresi

lmoresi commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

Response to the review (head 2b0c1a2f)

1. The clip deadlock. _clip_in_place now decides its early return over every rank with a logical OR. A rank with nothing to clip still takes part in the two volume integrals when another rank clips.

Found alongside it: _apply_boundary_neumann, the box wall correction used when adv_solver_bc is given, skipped its .array write on a rank with no wall node, through a continue. An .array write outside a deferred context packs at once and syncs ghosts, so it is collective. The loop now runs inside uw.synchronised_array_update, which synchronises once at exit on every rank.

Test added: test_slcn_steps_with_the_clip_and_the_wall_correction_complete, in the same parallel file. On a box it runs four SLCN steps with the wall correction on and a reinitialisation every second step. The file's 300 s timeout makes a hang a failure. It then checks that the volume is within the correction's own tolerance. Serially it passes, which it did before the fix too; at np 2 and 3 it is the reviewer's run.

Serial runs here: the parallel file, test_1105_levelset_rotation and test_0073: 22 passed, 1 skipped.

The lows (the band fraction counts ghosts; test_1105's band-share check is rank-local) go in the PR that makes epsilon global.

Underworld development team with AI support from Claude Code

@lmoresi

lmoresi commented Nov 19, 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

Head b1b46d3: ready to merge on green from this review.

All four deadlocks from the first round complete. The mass shift is one value on every rank, and the volume is conserved to round-off at np 1, 2 and 3. The new parallel case hangs on the previous head fce7acd at np 2 and 3, and passes here.

Build. The PR is still .py-only on merge base 4ab522a, which does not contain #1211. Development is now 246d6ce: #1211 merged, with no source change from #1211's head.

Results

case np previous head fce7acd this head
Box, one SLCN step, far field 0 3 hang in _clip_in_place completes; one wall value, 1.4428e-4
Box, the same 2 one value, 1.4433e-4 one value, 1.4433e-4
Annulus(0.3, 1, 0.1), development with this PR 2 one value, 3.0425e-4
Annulus, the same 3 hang completes; one value, 3.1001e-4
Volume drift after 10 steps, box 1, 2, 3 np 3 hung without the stand-in fix 9.7e-16, 7.8e-16, 1.9e-16
test_1069_...::test_the_level_set_far_field_on_an_annulus (SUPG and SLCN), development with this PR 3 MPI abort 2 passed on all 3 ranks
test_1105_levelset_rotation 2 the SLCN case failed on one rank, then hung completes in 3 min. The volume checks pass on both ranks; one rank fails the band-share assertion (finding 2)
New test_slcn_steps_with_the_clip_and_the_wall_correction_complete 2, 3 hang in _apply_boundary_neumann (killed by the supervisor) passed on every rank
The new file (both tests) 1, 2, 3 2 passed on every rank
Wall correction (adv_solver_bc on four walls), StructuredQuadBox 16 x 16, 4 steps; np 2 and 3 each have a rank with no Top node 1, 2, 3 completes. Every Top node equals its neighbour in the next row. The volume is the same as serial (1.288165831327e-1), and the drift is 0
The same, max |ψ - serial| 2, 3 3.4e-4. That is the same difference as with the wall correction switched off (3.4e-4), so it does not come from this fix
The same on UnstructuredSimplexBox (the new test's mesh), band layout 3 completes. The middle rank has no Top node
Level-set and SLCN files, serial (test_1105, 1056_units_slcn_traceback, 0610_navier_stokes_slcn_projection, 0066, 1071_slcn_moving_mesh, 1164, the new file) 1 48 passed, 1 skipped
test_1069_boundary_wrapper_checks_and_replaces.py, development with this PR 1 8 passed

Findings

1. Low: on a simplex mesh the wall correction corrects nothing, so the new test reaches its code but not its effect

_apply_boundary_neumann pairs each wall node with the node at the same tangential coordinate in the next row. On UnstructuredSimplexBox no such node exists. At np 1 it warns "Wall correction on 'Top': 33 wall node(s) have no interior counterpart" for every wall, and leaves every wall node unchanged.

So the new parallel case exercises the collective write path, which is what hung, but not a correction. On StructuredQuadBox the rows align, there are no warnings, and the correction holds at np 2 and 3 (table above).

A structured mesh in the test, or a note in it, would let the test check the correction too. Whether the correction should do something on unstructured meshes is a separate question from this PR.

2. Low (older than this PR, as before): test_1105_levelset_rotation's band-share assertion is rank-local

At np 2 the rank that holds the circle has 30 % of its nodes in the band. CI runs the file serially, so CI is not affected.

Older than this PR, still open (first round)

  • The initial field depends on the partition (ε comes from the nearest local centroid). On the simplex box and the Annulus, V0 changes with np. The quad box has uniform ε, and there V0 is the same at every np.
  • An SLCN step hangs when one rank holds no cells. That is in ddt's history write, not here.

Both still need issues.

Logs are kept by the reviewer.

Underworld development team with AI support from Claude Code

@lmoresi
lmoresi merged commit 789f155 into development Nov 19, 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.

2 participants