Repository navigation
[held for Louis] Solver resets by scope: solver.reset.conditions/nullspace/discretisation/all (#1169) - #1203
Conversation
…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
Louis approved the scoped reset convention (2026-11-19). Underworld development team with AI support from Claude Code
|
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)) 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. Findings1. High:
|
| 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;
_buildreplaces 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 asreset.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.scopesis("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 ofsrc,testsanddocsfound 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
development7 fail and 1 is skipped. For the behaviour, the issue's reproducer above showsdevelopmentfailing. -
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.pyran to the end through itsreset.all(), with no FutureWarning. Three others stop before they reach the changed line, from causes unrelated to this PR:Ex_Stokes_Cartesian_SolC.pystops at its first solve (PETSc "Could not locate matching functional for injection"), the same ondevelopment;Ex_Stokes_Annulus_Kernels.pystops on an array-shape error at line 193;AnnulusND_FS.pystops 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
|
Answers to H1's review at 94cd37d (H1 is the session that reviews pull requests) are in b5c04ac.
Underworld development team with AI support from Claude Code |
|
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)) Recheck of head b5c04ac: ready to merge on green from this review. Both findings from the review of 94cd37d are fixed, and nothing regressed.
About item 1b: I first tried a change that renumbers, with rotated free slip on two walls of an annulus and then a third, The count in item 2: it covers objects reachable from Python. It does not count objects held only inside PETSc. Nothing regressed.
Nit (non-blocking): Not rerun at this head: the 211-test serial neighbour set and the np 2 parallel slice. The change is confined to Underworld development team with AI support from Claude Code |
Fixes #1169.
Approved by Louis (2026-11-19): the scoped reset interface (
solver.reset.conditions/nullspace/discretisation/all, with_reset()a deprecated alias ofall()). 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.resetsolver.reset.conditions()solver.reset.nullspace()solver.reset.discretisation()solver.reset.all()solver.reset.scopeslists what a solver has:("conditions", "nullspace", "discretisation", "all")on a Stokes solver,("conditions", "discretisation", "all")on the scalar and vector solvers.view()anduw.capabilities()can show it; that wiring is not in this PR.petsc_use_pressure_nullspace,petsc_velocity_nullspace_basis). Fields keep their values.SolverResetis attached toSolverBaseClassas theresetproperty, so the scalar, vector and Stokes solver families all have it. The nullspace scope and the constraint part ofconditionsappear where those parts exist.One change to the proposed shape. C1's sketch had
_reset()become a deprecated alias ofreset.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 ofreset.all(), which is what its callers mean, with a FutureWarning namingreset.all()andreset.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
resetbecomes an interface expectation, a mesh's scopes might be:mesh.reset.coordinates()ephemeral_coords), with the field transfer a move makesmesh.reset.caches()_dminterpolation_cache, the location capability, evaluation hashesmesh.reset.hierarchy()mesh.reset.all()The variables, their values and the labels would be configuration in this sense. Open questions for Louis:
coordinates()should transfer fields, or only move nodes;hierarchy();Style Charter section 6: the reset convention (in this PR)
A new row in
docs/developer/UW3_STYLE_CHARTER.md, section 6 (API Conventions):obj.resetwith named scopes andreset.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.scopeslists the scopes. A private_reset()is never the public way to do it; where one exists it is a deprecated alias ofreset.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 earliertest_1169_reset_forgets_the_whole_problem.py.conditions, not todiscretisation;reset.all()(1e-8 and 1e-10);_reset()warns and does whatreset.all()does;reset.all()with a rank without cells, at np 2: the box of two cells, re-registered and solved, gives the same answer.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)
level_*andtier_*markers (S8)maybe_/try_/do_) and no commented-out code (S3, S4).array/mesh.X.coords— nowith ....access(...), nomesh.data(S7)pixi.toml/pixi.lockor other dependency changes riding in an unrelated PRUnderworld development team with AI support from Claude Code