Skip to content

[held for Louis] Solver resets by scope: solver.reset.conditions/nullspace/discretisation/all (#1169) - #1203

Merged
lmoresi merged 6 commits into
developmentfrom
bugfix/reset-clears-the-problem-1169
Nov 20, 2026
Merged

lmoresi merged 6 commits into
developmentfrom
bugfix/reset-clears-the-problem-1169

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Fixes #1169.

Approved by Louis (2026-11-19): the scoped reset interface (solver.reset.conditions/nullspace/discretisation/all, with _reset() a deprecated alias of all()). The Charter section 6 row for the convention is in this PR. The mesh scopes stay a sketch, below.

Louis, on the two options this PR first offered: "why not give it options so that this kind of call does not need to be different in different contexts reset.all() or something similar that could also become an interface expectation for meshes etc." This version does that.

The interface: solver.reset

call clears solvers
solver.reset.conditions() every registered boundary condition: essential, natural (Nitsche included), rotated free slip, and the constraint conditions with their multipliers. The discretisation is rebuilt on the next solve, because the conditions are compiled into it. all; constraints exist only on the Stokes family
solver.reset.nullspace() the nullspace objects built on the current numbering; rebuilt from the settings on the next solve Stokes family only
solver.reset.discretisation() the DM, its hierarchy and the SNES, rebuilt on the next solve; the conditions are kept all
solver.reset.all() the union: a new boundary-value problem on the same solver and fields all
  • solver.reset.scopes lists what a solver has: ("conditions", "nullspace", "discretisation", "all") on a Stokes solver, ("conditions", "discretisation", "all") on the scalar and vector solvers. view() and uw.capabilities() can show it; that wiring is not in this PR.
  • The configuration is never reset by any scope: the constitutive model and its parameters, tolerances, PETSc options, preconditioner choice, and the nullspace settings (petsc_use_pressure_nullspace, petsc_velocity_nullspace_basis). Fields keep their values.
  • Every scope is idempotent, and changes only Python state that is the same on every rank. So it is safe on every rank, an empty one included, and takes no collective.
  • Where: SolverReset is attached to SolverBaseClass as the reset property, so the scalar, vector and Stokes solver families all have it. The nullspace scope and the constraint part of conditions appear where those parts exist.

One change to the proposed shape. C1's sketch had _reset() become a deprecated alias of reset.discretisation(). But _reset() has always also cleared the essential and natural conditions, and its callers reset in order to register different ones. An alias that kept the conditions would make every unmigrated script hold the old and new conditions together, silently. So _reset() is a deprecated alias of reset.all(), which is what its callers mean, with a FutureWarning naming reset.all() and reset.scopes.

The six examples (three Stokes, three diffusion) reset in order to pose another problem: they swap free slip for penalty conditions, change normals, or set new radial-velocity data. They now call reset.all(). None of them uses rotated or constraint conditions, so their results do not change. The examples' parse gate passes.

Why (#1169's measurements, 4 × 4 Stokes box, one rank)

_reset() kept three things the next solve used:

After reset.all() all three match a new solver.

A mesh's reset scopes: a sketch, not implemented

If reset becomes an interface expectation, a mesh's scopes might be:

scope would clear
mesh.reset.coordinates() back to the as-built coordinates (the DM's original coordinates are kept today for ephemeral_coords), with the field transfer a move makes
mesh.reset.caches() the derived caches: kd-trees and the point locator, cell radii and centroids, _dminterpolation_cache, the location capability, evaluation hashes
mesh.reset.hierarchy() adapt children, add-on levels and the multigrid hierarchy built on top of the base; possibly the refinement history
mesh.reset.all() the union

The variables, their values and the labels would be configuration in this sense. Open questions for Louis:

  • whether coordinates() should transfer fields, or only move nodes;
  • whether a mesh with registered solvers should refuse hierarchy();
  • whether swarms registered on the mesh follow.

Style Charter section 6: the reset convention (in this PR)

A new row in docs/developer/UW3_STYLE_CHARTER.md, section 6 (API Conventions):

Topic Rule
Resetting an object An object that can be returned to an earlier state exposes obj.reset with named scopes and reset.all(), the union. Each scope clears exactly what it names and is idempotent. It never resets configuration (options, constitutive model, parameters). It takes the same path on every rank and needs no communication, so a rank without cells takes part like any other. obj.reset.scopes lists the scopes. A private _reset() is never the public way to do it; where one exists it is a deprecated alias of reset.all(). Solvers have it (conditions, nullspace, discretisation, all; #1169). (maintainer decision 2026-11-19)

Tests

tests/parallel/test_1169_solver_reset_scopes_parallel.py, tier B, serial and parallel. It replaces the earlier test_1169_reset_forgets_the_whole_problem.py.

The #1169 cases fail on development. Merged with development (94cd37d), the test passes at np 1, 2 and 3; at np 3 the two-cell box leaves a rank without cells. test_1013's reset case calls reset.all(). The Stokes, Poisson, rotated, constraint, nullspace and reset test files pass after the merge (309 passed, 2 skipped: a gmsh mesh that differs on this host, and the empty-rank case, which needs MPI). The coverage gate and the examples' parse gate pass. Measured on a 96-thread Linux workstation (single system image; Intel Xeon Gold 6240R, AVX-512; MPICH).

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): every scope is rank-local Python state, the same on every rank; np 2 with an empty rank 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

…held for Louis's decision]

_reset() emptied essential_bcs and natural_bcs and dropped the DM and SNES, but kept
the Stokes nullspace objects (built on the old walls' numbering), the rotated
free-slip conditions, and the constraint conditions with their multipliers. It now
clears those too; the solver's configuration (constitutive model, tolerances,
options, nullspace settings) stays.

test_1169: the issue's three cases against a new solver. Before: a solve after a
reset with other walls diverges or stops with PETSc error 75; rotated and constraint
conditions survive the reset. After: each matches the new solver.

Underworld development team with AI support from Claude Code
…ion/all (#1169) [held for Louis]

Louis asked for one reset call that means the same everywhere, with options,
instead of a choice between two fixes. `solver.reset` gives scopes that each clear
exactly what they name: conditions (essential, natural, rotated free slip, and on the
Stokes family the constraint conditions with their multipliers), nullspace (the
Stokes family's built nullspace objects), discretisation (the DM, its hierarchy and
the SNES: what _reset() dropped) and all. The configuration is never reset. Each
scope is idempotent and changes only Python state that is the same on every rank;
`solver.reset.scopes` lists the ones a solver has. _reset() is a deprecated alias
of reset.all(), which is what its callers meant; the six examples call reset.all().

tests/parallel/test_1169_solver_reset_scopes_parallel.py (replaces
test_1169_reset_forgets_the_whole_problem.py): each scope clears what it names and
the configuration survives; #1169's cases match a new solver after reset.all();
the alias warns; reset.all() with a rank without cells at np 2. test_1013 calls
reset.all().

Underworld development team with AI support from Claude Code
@lmoresi lmoresi changed the title [held for Louis] _reset() forgets the whole boundary-value problem (#1169) [held for Louis] Solver resets by scope: solver.reset.conditions/nullspace/discretisation/all (#1169) Nov 16, 2026
Louis approved the scoped reset convention (2026-11-19).

Underworld development team with AI support from Claude Code
@lmoresi
lmoresi marked this pull request as ready for review November 19, 2026 06:27
Copilot AI balanced review requested due to automatic review settings November 19, 2026 06:27

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

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 94cd37d: changes requested. This is one fix, small and well-contained, after which I would expect this to be ready.

Merge base: 4ab522a.

Findings

1. High: reset.conditions() followed by other walls reproduces #1169 (reason -6, PETSc error 75, and a parallel abort on some ranks only)

conditions() drops the DM and the SNES through _reset_discretisation(), and its docstring says the discretisation is rebuilt on the next solve. But the Stokes nullspace objects (_stokes_nullspace, _stokes_nullspace_basis, _velocity_rotation_nullspace) are numbered on the discretisation being dropped. _reset_discretisation() does not clear them. The block in _build that does clear them runs only if self.dm is not None:, and after any reset the DM is None. So _attach_stokes_nullspace reuses the old vector. This is #1169's mechanism; only reset.all() escapes it, because it also calls nullspace().

I used #1169's own 4 × 4 box with petsc_use_pressure_nullspace = True. The solver was solved, then reset.conditions() was called, the second wall set was registered, and the solve was repeated:

case after reset.conditions() new solver
other walls, same number of unknowns, np 1 reason -6; velocity 16 % off (relative norm) reason 2
more unknowns, np 1 PETSc error 75 reason 2
other walls, same number of unknowns, np 2 PETSc error 75 reason 2
more unknowns, np 2 PETSc error 75 reason 2
more unknowns, np 3 PETSc error 75 reason 2
other walls, same number of unknowns, np 3 ranks 0 and 2 raise "Incompatible vector local lengths"; rank 1's local sizes happen to match, so it goes on into the next collective, and the run hangs until the supervisor stops it reason 2

Calling reset.conditions() and then reset.discretisation() fails the same way (error 75 at np 1), since neither touches the nullspace.

The same gap applies to reset.discretisation() whenever the numbering changes underneath it. It also applies to the rotation nullspace, which is built from node coordinates. I did not construct that case.

Suggested fix: clear the three numbering-bound nullspace objects in _reset_discretisation(), which both conditions() and discretisation() call. Alternatively, clear them in _build whether or not a DM is present, as the field index sets already are. nullspace() then stays as the explicit scope, and the nullspace settings are untouched as now. A test would pin it: #1169's two nullspace cases through reset.conditions(), at np 1 and 3.

2. Low: the old field decomposition is not destroyed after a reset

After any scope that drops the DM, _subdict (the per-field index sets and sub-DMs) keeps the old entries. _build destroys them only inside if self.dm is not None:, so they are overwritten rather than destroyed on the next build. Re-solves are correct, so this is a small leak per reset, not a wrong answer. Destroying them in _reset_discretisation() would close it.

Confirmed correct

Each scope clears exactly what it names. I compared the solver's state before and after each scope. I used a solved Stokes_Constrained with a natural condition and two constraints, and a solved Stokes with a rotated free-slip wall, both with the pressure nullspace on:

scope changed unchanged
conditions() essential, natural, rotated and constraint conditions; multipliers and their screening entries; the multiplier entries in fields; DM, hierarchy and SNES nullspace objects (see finding 1)
nullspace() _stokes_nullspace and its basis only the DM is kept
discretisation() DM, hierarchy and SNES only conditions kept
all() the union
  • In every case a second call changes nothing, so each scope is idempotent.
  • The cached field index sets are untouched by every scope; _build replaces them on the next solve.

Configuration survives every scope: the tolerance, the PETSc options (the full option list compared), the constitutive model (same object), the viscosity, the pressure-nullspace and velocity-basis settings, the preconditioner, and the saddle preconditioner. The custom geometric multigrid set with set_custom_fmg also survives. After nullspace() and after discretisation() it rebuilds: still mg with 3 levels, the same 4 velocity iterations, and the same answer to round-off.

Scopes are collective-safe, and the answer matches serial. Each scope was called on a solved solver, the conditions were re-registered where they had been cleared, and the solver re-solved. There was no hang, and the result matches the first solve to round-off (at most 2.6e-13) with the same peak speed as serial. This was checked at np 1, 2 and 3 with the default partition, and at np 2 and 3 with the last rank holding no cells (cells per rank 36/0 and 18/18/0). It held for all four scopes on three solvers:

  • Stokes with free slip and the pressure nullspace;
  • Stokes_Constrained with three constraint walls;
  • Stokes with three rotated free-slip walls.

reset.discretisation() followed by a solve rebuilds correctly at np 2, both with and without an empty rank.

#1169's cases are fixed. I ran the issue's reproducer unchanged; its _reset() now means reset.all().

case on development at 94cd37d
other walls, same number of unknowns reason -6, speed 2.6297e-02 reason 2, speed 2.2740e-02, as a new solver
more unknowns error 75 reason 2, speed 5.3519e-02, as a new solver
rotated and constraint conditions left after the reset 1 and 1 0 and 0

The PR's test checks the match with a new solver to 1e-8 and 1e-10.

  • The deprecated alias: _reset() raises one FutureWarning across four calls, and leaves the same state as reset.all(): no conditions or multipliers, no nullspace, no DM. The checks covered np 1, 2 and 3.
  • Repeated reset cycles: three cycles of reset.all() and re-registering one constraint gave reason 2 each time, and no multiplier variables piled up on the mesh.
  • The scalar solver: on Poisson, reset.scopes is ("conditions", "discretisation", "all").
    • discretisation() re-solves to the same answer.
    • conditions() followed by new conditions matches a new solver exactly, at np 1, 2 and 3.
    • Its constant nullspace is not tied to the numbering, so the lack of a nullspace scope there is harmless.
  • No other _reset() callers: a search of src, tests and docs found no solver _reset() callers left outside the six converted examples. The constitutive models' own _reset() is a different method.

What was run

  • The PR's test file: 7 passed and 1 skipped serially (the skip is the empty-rank case, which needs MPI). 8 passed on every rank at np 2 and at np 3. On development 7 fail and 1 is skipped. For the behaviour, the issue's reproducer above shows development failing.

  • Serial, pytest -n 4: 211 passed. The files cover Stokes (1010–1014, 1017, 0786, 1066 layout, 1145), the rotated condition (1018, 1065, 1066, 1085, 1115, 1131, 1141, 1157, 1158), constraints (1024, 1061, 1062, 1063, 0062), the nullspace tests (1013 ×2, 1014 shell, 1056 constant) and Nitsche (1060).

  • Parallel slice at np 2: test_0786, 1062, 1063, 1064, 1131, 1144, 1167 and 1179 gave 24 passed and 3 skipped on every rank. The skips are test_0786's own np 2 case and 3-D accuracy anchors recorded on other meshes.

  • Examples: Ex_Stokes_Ellipse_Cartesian.py ran to the end through its reset.all(), with no FutureWarning. Three others stop before they reach the changed line, from causes unrelated to this PR:

    • Ex_Stokes_Cartesian_SolC.py stops at its first solve (PETSc "Could not locate matching functional for injection"), the same on development;
    • Ex_Stokes_Annulus_Kernels.py stops on an array-shape error at line 193;
    • AnnulusND_FS.py stops on "Unsupported BC conds" at line 103.

    The changed lines are a one-word rename in each.

  • Reviewer probe scripts: the state comparison, the re-solve per scope, the nullspace-through-conditions cases, the alias and the custom multigrid, all at the rank counts above. Logs are kept by the reviewer.

Not run:

  • reset.discretisation() after a mesh deformation or remesh;
  • the other two free-surface examples;
  • 3-D solvers through each scope;
  • the full scripts/test.sh.

Underworld development team with AI support from Claude Code

…it (#1169)

reset.conditions() and reset.discretisation() dropped the DM but kept the
Stokes nullspace objects numbered on it, because _build forgets them only
when it finds a DM to tear down. #1169's walls cases through those scopes
diverged, stopped with PETSc error 75, or hung at np 3. _reset_discretisation
now forgets the nullspace objects, and destroys the old _subdict's index
sets and sub-DMs, which leaked once per reset.

Underworld development team with AI support from Claude Code
@lmoresi

lmoresi commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

Answers to H1's review at 94cd37d (H1 is the session that reviews pull requests) are in b5c04ac.

  • High: nullspace objects survived conditions() and discretisation().
    • Fix: _reset_discretisation now forgets the Stokes nullspace objects (_reset_stokes_nullspace, and the constant nullspace object) every time it runs. Before, it relied on _build, which forgets them only when it finds a DM to tear down.
    • Tests:
    • On the previous build: all three fail at np 1. At np 3 they give "Incompatible vector local/global lengths", and one case ended with exit 142 instead of finishing.
    • With the fix: the file passes at np 1 (10 passed, 1 skipped), np 2 (11) and np 3 (11). At np 3 a rank holds no cells in the empty-rank case.
  • Low: _subdict leaked. The old field split's index sets and sub-DMs are destroyed whenever the DM is dropped.
  • Serial solver files (Stokes, Poisson, rotated, constraint, nullspace, reset): 312 passed, 2 skipped.

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.)

Recheck of head b5c04ac: ready to merge on green from this review. Both findings from the review of 94cd37d are fixed, and nothing regressed.

# Item (review of 94cd37d) Before At b5c04ac
1 High: reset.conditions() then other walls (#1169's two cases, pressure nullspace on) reason -6 at np 1; PETSc error 75 at np 1, 2 and 3; at np 3 an error on two ranks and a hang Fixed. Both walls cases now converge, through conditions() alone and through conditions() then discretisation(). That is 12 runs over np 1, 2 and 3, every one with reason 2 and no hang. Each matches a new solver to at most 1.3e-12 (relative), most exactly.
1b The rotation nullspace, which is built from node coordinates not tested Fixed. The annulus uses penalty free slip on both walls and the declared rotation mode; the mode was cached after the first solve. I scaled the mesh by 1.1 about its centre with mesh.deform, called reset.discretisation() and re-solved. The result matches a new solver on the deformed mesh exactly at np 1 and 2, and to 9e-14 at np 3.
2 Low: the old _subdict is never destroyed overwritten on the next build Fixed. Over 20 cycles of each scope (discretisation(), conditions() with re-registration, all()) followed by a solve, the live PETSc objects held from Python do not grow: 4 IS, 5 DM, 6 Vec and 0 Mat after cycles 1, 2, 5, 10 and 20, at np 1 and 2.

About item 1b: I first tried a change that renumbers, with rotated free slip on two walls of an annulus and then a third, Internal. The rotated path keeps the global size (3428 before and after) and does not use the cached rotation nullspace. So that case cannot exercise the cache, although it too matches a new solver. The deformation case above is the one that does.

The count in item 2: it covers objects reachable from Python. It does not count objects held only inside PETSc.

Nothing regressed.

  • Every scope with an empty rank: each scope was called on a solved solver at np 2, with the second rank holding no cells (cells per rank 36/0), and the solver re-solved. This covered free-slip Stokes, Stokes_Constrained and rotated free-slip Stokes, through all four scopes. Every case returns reason 2 and matches the first solve to round-off, with the same peak speeds as serial.
  • test_1169 (now 11 tests): 10 passed and 1 skipped at np 1; the skip is the empty-rank case, which needs MPI. 11 passed on every rank at np 2 and at np 3.

Nit (non-blocking): discretisation() now also drops the nullspace objects, which are rebuilt from the settings on the next solve. One line saying so in the SolverReset docstring and its table would keep the "clears exactly what it names" contract literal.

Not rerun at this head: the 211-test serial neighbour set and the np 2 parallel slice. The change is confined to _reset_discretisation(), which runs only on a reset, and the reset paths are covered above.

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