Skip to content

Stokes_Constrained with a discontinuous pressure: copy every unknown of each field back (#1167) - #1200

Merged
lmoresi merged 1 commit into
developmentfrom
bugfix/constrained-discontinuous-pressure-1167
Nov 16, 2026
Merged

lmoresi merged 1 commit into
developmentfrom
bugfix/constrained-discontinuous-pressure-1167

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 16, 2026

Copy link
Copy Markdown
Member

Fixes #1167.

The defect. get_local_field_is, inside SNES_Stokes_SaddlePt._ensure_local_field_index_sets, builds the local index sets that copy the solution back into the variables. It decided whether to take every unknown of a field from the pressure's continuity, whichever field it was asked for. When it did take them all, it took one index per point.

The fix. With unconstrained=False, every unknown of each point is taken, for any field. For a field with one unknown per point, the continuous pressure and P1 multipliers, that is the same index set as before, so the continuous solve is bit-identical (max |v| 0.06878602773825046 before and after). There are no other callers.

Measured on a 96-thread Linux workstation (single system image; Intel Xeon Gold 6240R, AVX-512; MPICH), on #1167's case:

pressure development this branch
continuous P1 converges (reason 2), max v
discontinuous P1 ValueError (broadcast) after the solve converges (reason 2), max

Test. tests/parallel/test_1167_constrained_stokes_discontinuous_pressure_parallel.py, tier B. It runs the issue's case, and asserts that the walls' normal velocity is below 1e-5 and that the solution agrees with the continuous-pressure one to 1e-3. Development raises; it passes at np 1 and 2, and runs in batch 3 and under --p N. The Stokes and constraint test files pass (199); the coverage gate passes.

#1154 (set_active_region) does not share this code and comes as its own PR.

Checklist (UW3 Style Charter — docs/developer/UW3_STYLE_CHARTER.md)

  • I have read the Style Charter and followed it over the surrounding code (S2)
  • No drive-by refactors, renames, or "while I was here" cleanups (S9)
  • Bug fixes ship the regression test, written first, with level_* and tier_* markers (S8)
  • No hedging names (maybe_ / try_ / do_) and no commented-out code (S3, S4)
  • Every exception swallow states its sanctioned failure mode in a comment (S4)
  • New data access uses .array / mesh.X.coords — no with ....access(...), no mesh.data (S7)
  • Parallel safety considered (S11): rank-local index sets; np 1 and 2 checked
  • No mesh entity or rank identified by coordinates; any remaining tolerance is named, relative and says what it bounds (S12)
  • No pixi.toml / pixi.lock or other dependency changes riding in an unrelated PR
  • AI-assisted work carries the attribution line below

Underworld development team with AI support from Claude Code

…of each field back (#1167)

get_local_field_is took all of a field's unknowns only when the PRESSURE was
continuous, whatever field it was asked for, and then one index per point. With a
discontinuous pressure the multipliers went through the unconstrained branch and
lost their pinned interior unknowns, the velocity's complement grew by them (306
entries for a variable of 162), and the copy-back stopped with a broadcast error
after the solve. Now every unknown of each point is taken for any field; the
continuous case takes the same indices as before (its solve is bit-identical).

tests/parallel/test_1167_constrained_stokes_discontinuous_pressure_parallel.py: the
issue's case solves, the walls' normal velocity is below 1e-5, and the solution
agrees with the continuous-pressure one to 1e-3; development raises the broadcast
error. np 1 and 2; also in batch 3.

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

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 aa49911: ready to merge on green from this review (a comment, not a GitHub approval). The index sets are right for every field and every number of unknowns per point. Every continuous-pressure case that ran before gives the same index sets and the same answer. The discontinuous-pressure constrained solve is correct: it matches plain Stokes on the same discretisation to solver tolerance. There are no blocking findings.

How the builds were compared

The PR's base is development at fafbecb. Between the development build used here (176958a) and fafbecb there are only Python changes (discretisation_mesh.py, meshing/smoothing/graph.py). Those two files from fafbecb were laid over the development install. So the two builds differ only in this PR's change to the .pyx. Both use the same lock file and the same set of conda packages (PETSc 3.25.3, MPICH 5.0.1).

Findings

No blocking findings.

Minor / informational

  1. Continuous pressure also changes in one case, from a crash to a correct answer. On development, a constraint multiplier with more than one unknown per point fails even with a continuous pressure. An example is add_constraint_bc(..., degree=3), which puts two unknowns on each edge. The old code took one index per point.

    The failure case: P3 velocity, continuous P2 pressure, and P3 multipliers on Left and Right. On development it fails with ValueError: could not broadcast input array from shape (514,) into shape (338,). On this branch it converges (reason 2), with wall normal velocity 9.6e-9. The copied-back velocity equals, to 0.0, the velocity read from the SNES solution through the velocity sub-DM. This is the route _gather_fields_to_global inverts.

    So "the continuous solve is bit-identical" holds for every case that ran before. This one did not run before. It is worth a line in the PR text or the docstring, since it is a second thing the PR fixes.

  2. The unconstrained=True branch of get_local_field_is has no caller now. Both calls (pressure, multipliers) use the default. You could drop the branch, or keep it with a note on what it is for. This is a nit.

  3. Unrelated to this PR, seen while checking item 1: Stokes_Constrained with a P3 velocity disagrees with the essential-wall solution, on development as well. Case: a 4x4 quad box, Bottom no-slip, Left and Right held normal by the constraint, compared with the same walls held by add_essential_bc((0.0, None), ...).

    • With a P2 velocity the two agree: max |v| 0.0687867179 against 0.0687867226.
    • With a P3 velocity the constrained max |v| is 0.0506917 (P2 multiplier) or 0.0506918 (P3 multiplier), against 0.0685829 for the essential walls, about 26% lower.
    • The P2-multiplier figure is the same on development and on this branch. The copy-back agrees with the sub-DM route to 0.0, so the index sets are not the cause. It looks like the constraint assembly at velocity degree 3.
    • We suggest a separate issue rather than holding this PR.
  4. Run-to-run reproducibility on development at np 4, not this PR. Two runs of the same development build at np 4 differ by up to 9.5e-14 in plain Stokes, constrained Stokes, the 3-D box and the refined (FMG) case. The rotated free-slip annulus differs by up to 4.7e-16 between two development runs even at np 1 and 2. That is why "exactly 0" could not be asked of np 4; see below.

Bit-identity: continuous pressure, PR against development

Script: one run solves eight consumer cases and saves every rank's raw arrays. A given rank count gives the same partition, so no gather was needed. Each build was run twice at each of np 1, 2 and 4. The cases are:

case what it exercises
A Stokes, simplex box, P2/P1, no-slip; post-solve copy-back, then compute_volume_residual_fields and adjoint_solve(target=...) (both consumers of the cached index sets)
B Stokes, quad box, free slip by essential components
C Stokes_Constrained, continuous P1 pressure, constraints on Left and Right (two multiplier fields)
C2 Stokes_Constrained, P3 velocity, continuous P2 pressure, constraint on Top
D plain Stokes with a discontinuous P1 pressure (the old code's other branch)
E 3-D Stokes, P2/P1, three-component velocity
F rotated free slip on an annulus (rotated_bc goes through _scatter_global_to_fields)
G Stokes on a refined simplex box: the custom FMG / fieldsplit hierarchy path

Saved per rank: velocity, pressure, multipliers, coordinates, residual fields, adjoint fields and the cached index sets themselves (velocity, pressure and each multiplier). Also recorded: the SNES reason and iteration counts, and the index-set sizes against each variable's local vector.

np index sets, PR against development solution arrays, PR against development development against itself
1 19/19 identical 39/39 bit-identical (run 1); 37/39 in run 2, case F only, 4.7e-16 case F differs, 4.7e-16
2 19/19 identical, every rank 39/39 bit-identical, max abs diff 0 (both runs) case F differs, 3.2e-16
4 19/19 identical, every rank max abs diff 4.4e-14 up to 9.5e-14 across A to G
  • At np 1 and 2, every case is bit-identical, PR against development, except F. F is not bit-reproducible on development itself.
  • At np 4, development does not repeat itself bit for bit. The PR-against-development difference (4.4e-14) is inside development's own spread (9.5e-14).
  • At every rank count, the index sets are identical in every case, and so are the SNES reasons and iteration counts. Every index set's size equals its variable's local vector. Velocity, pressure and multipliers cover the local vector exactly once.

The index sets are what the PR changes. Since they are identical, the copy-back is the same operation on both builds.

Discontinuous pressure: is the solve right?

Same mesh family as #1167, 8x8 quads, P2 velocity. We compared Stokes_Constrained against plain Stokes with the side walls held by add_essential_bc((0.0, None), ...), with discontinuous P1 pressure and with continuous P1:

pair velocity max abs diff (scale 0.0687) pressure, mean-free (scale 0.576)
constrained dP1 vs plain dP1 8.9e-9 (rel 1.3e-7) 1.4e-7 (rel 2.4e-7), np 1
constrained P1 vs plain P1 8.9e-9 (rel 1.3e-7) 1.4e-7 (rel 2.5e-7), np 1
  • The discontinuous constrained solve agrees with plain Stokes exactly as closely as the continuous pair does.
  • The wall normal velocity is 8.9e-9.
  • The velocity agreement is identical at np 1, 2 and 4.
  • At every rank count the index-set sizes match the variables: velocity 578/578, pressure 256/256 (64 cells x 4), each multiplier 289/289 at np 1, and per rank at np 2 and 4.
  • On development the same constrained dP1 case fails with could not broadcast input array from shape (1122,) into shape (578,).

Vector fields and multi-dof points:

  • The velocity, two or three components with up to four unknowns per edge point at P3, is the complement of the others. Its size matches in every case, in 2-D and 3-D.
  • The discontinuous pressure has four unknowns on each cell point and is taken whole.
  • The P3 multiplier of item 1 has two unknowns per edge. It is taken whole, and its copy-back equals the sub-DM route.

What was run

run result
eight-case consumer comparison, both builds, np 1, 2, 4, two runs each 12 runs, all exit 0; results above
test_1167_constrained_stokes_discontinuous_pressure_parallel.py, this branch, np 1, 2, 4 1 passed at each
the same test, development, np 2 1 failed (ValueError: ... shape (171,) into shape (90,)), as expected
constrained against plain Stokes, dP1 and P1: this branch np 1, 2, 4; development np 1 as tabled; development fails on the dP1 constrained case
multi-dof multiplier probe: this branch np 1 and 2; development np 1 as in item 1

Every parallel run was under the MPI supervisor with a timeout, pinned to four cores.

Not run:

  • np above 4 (core budget on a shared machine);
  • the full Stokes and constraint test files (the author reports 199 pass);
  • Navier-Stokes and VE-Stokes. They go through the same _ensure_local_field_index_sets, but no separate solve was run.
  • macOS.

Logs kept by the reviewer.

Underworld development team with AI support from Claude Code

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