Skip to content

A degree-2 mesh builds with a rank holding no cells, and its cells have true sizes - #983

Merged
lmoresi merged 5 commits into
developmentfrom
bugfix/degree2-cell-size-908
Nov 7, 2026
Merged

lmoresi merged 5 commits into
developmentfrom
bugfix/degree2-cell-size-908

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

A degree-2 mesh now builds with a rank holding no cells, and its cell sizes are the true ones. This changes cell sizes on meshes of degree 2 or more, in serial too; see below.

What was wrong

petsc_fvm_get_local_cell_sizes and petsc_fvm_get_min_radius call DMPlexComputeGeometryFVM and ignored its error code. On a degree-2 coordinate field it fails with "Edge has 6 coordinates != 4", on every rank with cells and not on a rank without, so the ranks then disagreed at the next collective: MPI "Message truncated", then a hang. The numbers it returned were also wrong: it read edge midpoints as corners.

The fix

  • Above degree 1, cell volume comes from the coordinate map's Jacobian through DMFieldCreateFEGeom, on a rule set by the coordinate degree, enough to integrate det J exactly, not by qdegree. Cells go through in blocks of at most 200,000 quadrature points, so memory stays bounded: a 195k-cell degree-2 hex mesh at qdegree 3 peaked 2.09 GB above base on the first version of this PR, and peaks under 3 MB now. Degree 1 stays on the FVM route, unchanged.
  • Return codes are checked on every rank, on the verdict communicator. The FVM wrappers refuse degree above 1 on every rank before the call.

What changes for anyone running a mesh of degree 2 or more

Cell radii, get_min/max/mean_radius and cell_size() change, in serial too. They now equal the documented volume**(1/dim), and match degree 1 to 3.0e-15 on triangles, quads, tets, hexes, an annulus, a manifold and a shell. Old to new:

triangles quads tets hexes
degree 2 x2 x2 x2 x3.63
degree 3 x2.59 about x3e6 x3 x4.06

Those hex factors are for straight hexes. On warped hexes (CubedSphere, RegionalSphericalBox) degree 2 is x3.74 to x3.80, so h^2 callers move x14.0 to x14.4, and degree 2 differs from degree 1 per cell by up to 6.06e-3 while totals agree to 1e-10: FVM's volume of a hex with non-planar faces is not the trilinear integral.

Degree 1 is unaffected. No production or campaign path builds a mesh of degree 2 or more, from a scan of src, tests, docs, scripts, campaigns and drivers: the literal builders are this PR's tests, test_1107, and two files in docs/examples/submesh_investigation/, which pin no radius.

Every caller of these numbers, on a degree-2 straight-sided mesh. Factors are for triangles, quads and tets; on straight hexes x2 is x3.63, and x4 is x13.2. "Measured" is old then new on a triangle mesh.

caller quantity moves
Swarm.estimate_dt, swarm.py:6058 Courant h/|v| x2, measured 0.0221 to 0.0442
_advective_diffusive_dt, solvers.py:399, resolution basis advective h/|v| x2, measured 0.0395 to 0.0791
same diffusive h^2/K x4, measured 0.195 to 0.781
same, direction_aware=True vertex extent unmoved, 0.1
AdvDiffusion.estimate_dt, accuracy basis no radii unmoved; raises on degree 2 (#978)
SNES_Stokes.estimate_dt, solvers.py:2889 h/|v| x2
SNES_Stokes_SaddlePt.estimate_dt, petsc_generic_snes_solvers.pyx:12577 h/|v| x2
SNES_TransientDarcy, solvers.py:1415 h^2/K x4
SNES_Diffusion, solvers.py:5423 h^2/K x4, measured 0.0488 to 0.195
SNES_NavierStokes, solvers.py:6013 both x2 and x4
solver_template.py:284 h^2/2K x4
free_surface.py:1453, :1468 surface and velocity CFL x2
EulerianSUPG.tau, ddt.py:2322 SUPG tau measured 0.01105 to 0.02208
Nitsche, petsc_generic_snes_solvers.pyx:7147, :7151, :9084, :9088 gamma mu/h x1/2; enforcement weaker than before and now matches degree 1
ddt.py:4472 reseeded particle departure, 2h x2
ddt.py:5265 store smoothing, c h^2 x4
ddt.py:5703, smoothing/api.py:716, faults.py:472, :726, analytic/_base.py:393, transport.py:118, :181 docstrings; user values x2
discretisation_mesh.py:3671 restore tolerance, 1e-3 h x2
discretisation_mesh.py:6383 locator near-boundary band, 2 h^2 max x4 in distance squared; cost only, 0 of 3249 wrong
Mesh.get_min_radius_old, discretisation_mesh.py:7112 old route now raises on degree 2 or more; printed a PETSc error and returned a wrong value before; no library caller
metric and adapt targets: _mesh_h0 (surfaces.py:3352), adaptivity.py h_min and h_max, smoothing/metrics.py do not read these numbers unmoved
computeCellGeometryFVM callers, including level_set.py:173 per-cell volume unmoved, still wrong; now disagree with _cell_radii (#978)

Essential-BC Stokes at degree 2 against degree 1: 1.9e-16. SUPG after 5 steps: 2.6439e-2 before, 2.6421e-2 now, equal to degree 1.

Tests

  • tests/test_0782_degree2_cell_size.py, serial: 15 failed, 2 passed on eddab70; 17 passed here. Sizes are compared per cell with closed forms; a curved cell's size at qdegree 1, 2 and 5 against an exact volume (out by 9.6e-8 at qdegree 1 on this PR's first version); and block splitting, where a slip at a block boundary fails.
  • tests/parallel/test_0782_degree2_mesh_empty_rank.py: exit 142 at np 2, 3, 4 on eddab70, with Edge has 6 coordinates != 4 then "Message truncated"; 4 passed at np 2, 3, 4, 8 here. It has power at CI's np 2, and asserts exactly one empty rank.
  • The review's mutants die at np 2: radii halved, radii averaged, the FVM route, a rank-local check, no refusal.

Left open

#909 and #965 follow in a separate PR. They were on one branch, and carving them apart lets this land while the remesh transfer goes through further review. #978: other degree-2 routes. A review could not exercise a failing DMFieldCreateFEGeom (the code check mutant survives), nor a negative Jacobian (abs mutant survives): a midside node pulled through its cell reports a plain size with no flag.

Closes #908

Underworld development team with AI support from Claude Code

…ve their true size (#908)

What was wrong. Mesh._get_cell_radii took each cell's volume from
DMPlexComputeGeometryFVM, which needs affine coordinates, and the error code was
ignored. On a coordinate field of degree 2 or more the routine read the edge
midpoints as corners and then stopped at its first face with "Edge has 6
coordinates != 4". A rank with no cells has no faces and carried on into a
collective its peers had left. At np 3 with cells [8, 8, 0], the ranks died in
the next collective. In serial the mesh built, printed the error, and reported
every cell length at half its true value.

The fix. Above degree 1 the volume is now the integral of the coordinate map's
Jacobian on the mesh's own quadrature rule, from DMFieldCreateFEGeom, the
geometry the assembler integrates with. It needs no communication, handles
curved cells and manifolds, and matches degree 1 to 1e-15 on triangles, quads,
tetrahedra, hexahedra, an annulus and a spherical manifold. Degree 1 stays on the
FVM routine and is unchanged. Error codes are now checked on every rank. The FVM
routine also refuses degree above 1 before the call, because the code check
alone hangs: the empty rank never leaves the routine to reach it (seen at np 2
and np 3).

Behaviour change. On every straight-sided mesh of degree 2 or more, in serial
too, the cell radii, get_min_radius, get_max_radius, get_mean_radius and
cell_size() double. The new values are the documented volume**(1/dim). No test
or source file builds such a mesh and pins the old value; test_1107 builds one
and is unaffected.

Tests.
tests/parallel/test_0782_degree2_mesh_empty_rank.py has power at every np >= 2:
before, np 2, 3 and the np 4 region extract abort with exit 142 and "Fatal error
in internal_Allreduce_c: Message truncated"; after, 4 passed at np 2, 3, 4, 8.
tests/test_0782_degree2_cell_size.py, serial: before, 8 failed, 2 passed; after,
10 passed.
Neighbours unchanged at np 2 and 4: test_0774, 0779, 0781, 0855, 1079, 1080,
1107. Serial adapt tests: 66 passed, 6 skipped (no adapter in conda-forge PETSc).

Underworld development team with AI support from Claude Code
…rrected (#908)

What was wrong. _check_error_code_collectively said it ran on a communicator
apart from PETSc's, but both call sites passed mesh.dm.comm.tompi4py(), which is
PETSc's duplicate. Its test passed uw.mpi.comm, so it could not see this.

The fix. The function takes no communicator and always uses uw.mpi.verdict_comm,
so the test now exercises the same exchange as the call sites.

Correction to 16689d1a. Its message says cell radii "double" on every
straight-sided mesh of degree 2 or more. The factor from old to new, as measured
in review, is:
  degree 2: triangles, quadrilaterals, tetrahedra 2; hexahedra 3.63.
  degree 3: triangles 2.59; quadrilaterals about 3e6; tetrahedra 3; hexahedra 4.06.
The docstring of tests/test_0782_degree2_cell_size.py now carries these.

Tests. test_0782 parallel: 4 passed at np 2, 3, 4, 8. Serial test_0782: 10 passed.

Underworld development team with AI support from Claude Code
Copilot AI balanced review requested due to automatic review settings November 6, 2026 11:45

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 6, 2026

Copy link
Copy Markdown
Member Author

Adversarial review record for this PR

Two independent passes on the branch these commits came from; #909's remesh work has moved to its own PR, so only this part applies here.

Verdict for this PR's code: fixed, no open must-fix. One must-fix applied to it: _check_error_code_collectively said it ran on comm itself while its callers passed PETSc's duplicate. It now takes no communicator and always uses uw.mpi.verdict_comm. The "double" in the first commit's message is corrected in the second commit and the PR body.

Attacks that failed

  • Agreement with degree 1: at most 3.0e-15 on triangles, quads, tets, hexes, annulus, manifold and shell; degrees 2 and 3 at most 1.8e-15 on the four box types.
  • Quadrature, degree 2 and 3: qdegree 0 (one point) to 5 are correct to 3e-12 on triangles; a curved quadratic map at qdegree 0 to 3, 5.9e-12.
  • A rank holding no cells, degree 2, np 2, 3, 4, 8: radii, cell_size, deform (area 0.58333 against exact 0.58333), a new variable, global_evaluate, points_in_domain, Integral and a swarm all pass.
  • verdict_comm on a sub-communicator: rank 0 alone building a Mesh on COMM_SELF at np 2 hangs on eddab70 too, so no regression; every rank building its own passes, degree 1 and 2. No library path builds a Mesh on a subset of ranks.
  • Caches and checkpoints: no recorded radius in a checkpoint, transcript or cache key.
  • Solves, 8x8 triangles: essential-BC Stokes, degree 2 against degree 1, 1.9e-16. Nitsche with normal= given: |v_n| 5.08e-5 on base, 9.46e-5 here, 9.60e-5 at degree 1, and enforcement is weaker than on base and now matches degree 1. Those Nitsche solves ended on SNES reason -6 or -3 on both builds, so they compare runs, not converged answers. SUPG after 5 steps: 2.6439e-2 on base, 2.6421e-2 here, equal to degree 1.

Findings left, not merge conditions

  • Other routes still read midpoints or refuse degree 2: Degree-2 meshes: computeCellGeometryFVM, boundary_normal and estimate_dt still read or refuse midpoints #978.
  • Mutants that survive: no abs on the Jacobian (a midside node pulled through its cell gives summed area 2.75 on a unit square, reported as a plain size with no flag); BASIC on a manifold, so the EMBEDDED branch is not shown necessary; the FEGeom code check removed, as no failure path is exercised.
  • Cost, degree 2 only, Jacobian against FVM: triangles x1.7 (2.4 us per cell), tets x3.5 (14 us), hexes x12 (75 us). No RSS growth over 4 repeat calls.
  • CHKERRQ(PetscQuadratureGetData) stays rank-local; not seen to fail.

Tests can fail

Serial test_0782: 8 failed, 2 passed on eddab70. Mutants: radii halved 8 failed, radii averaged 3, FVM route 8. Parallel at np 2: radii halved 2 failed, averaged 1, FVM route 3, rank-local check 1; no refusal, exit 124.

Issues

#908: fixed by this PR. #872, #831: still live, not touched.

Could not test

Meshes above about 33k cells; np above 8; truly curved degree-3 cells; the full suite.

Underworld development team with AI support from Claude Code

@lmoresi

lmoresi commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Adversarial review of #983 at a00566a

Second, independent pass. We judged the code before reading the author's record and list here only what that record does not already say. Built in a worktree, macOS, conda-forge PETSc 3.25.3, serial only. Development behaviour comes from an env that is eddab70 plus an unrelated rotated_bc.py change; petsc_discretisation.pyx and discretisation_mesh.py are byte-identical to eddab70 there.

Must fix

None.

Should fix

  1. Medium. src/underworld3/cython/petsc_discretisation.pyx:123, :138-146. The degree-2 cell size allocates geometry for every local cell at the solver's quadrature degree, all at once. DMFieldCreateFEGeom stores v, J, invJ and detJ at every quadrature point of every cell: 22 doubles per point in 3-D. The rule is mesh.petsc_fe's, so it is set by the user's qdegree. A volume needs only det J, which a rule fixed by the coordinate degree integrates exactly. The call runs in every nuke_coords_and_rebuild, so it runs on every _deform_mesh and every mover sweep. Scenario: a degree-2 hex mesh with qdegree=6 adds about 1.5 GB to peak memory at 27k cells and takes 5 s per rebuild. The author's record gives the time per cell at one rule; it does not give the peak or how it depends on qdegree.
    Probe: StructuredQuadBox(elementRes=(30,30,30), degree=2, qdegree=q), then petsc_fem_get_local_cell_sizes(mesh). Each q runs in its own process, so the build's peak includes the constructor's own call.

    qdegree 2: cells 27000 Nq 27  call 0.43 s (15.9 us/cell)  peak RSS after build 1541 MB; FEGeom size 122 MB
    qdegree 6: cells 27000 Nq 343 call 5.22 s (193.3 us/cell) peak RSS after build 3057 MB; FEGeom size 1554 MB
    

    Fix: take the cells in blocks of bounded size, and integrate on a rule set by the coordinate degree, not by qdegree.

  2. Low. tests/parallel/empty_rank_fixtures.py:18. Dead parameter dm_class=PETSc.DMPlex. Nothing on this branch passes it. Its only use is the default, at line 42 (grep -rn dm_class tests src). Charter §5: no parameters for forward compatibility. It is most likely left over from Mesh.remesh: a collective call sits behind a rank-local guard in the variable transfer #909's tests on the combined branch. Drop it here, and add it in the PR whose test needs it.

  3. Low. src/underworld3/cython/petsc_discretisation.pyx:29. A second implementation of an existing helper. utilities/fault_contact.py:61 _collective_raise already allgathers on uw.mpi.verdict_comm and raises the same error on every rank, and fault_mg and rotated_bc share it. Charter §5: look for the existing helper first. Use that helper here (move it to uw.mpi if the import direction is wrong), or extend it to report the code from every rank, which test_an_error_code_on_one_rank_raises_on_every_rank asserts.

  4. Low. tests/test_0782_degree2_cell_size.py:53-60 and :49-50. Two baselines that are not closed forms. The manifold test compares against uw.maths.Integral of one, which integrates on the same DMFieldCreateFEGeom geometry and the same EMBEDDED detJ. An error common to that route cancels. The closed form is there: on straight facets each cell's area is 0.5*|(x1-x0) x (x2-x0)| from the P1 vertex table. The unit-cube test asserts only the sum. On the regular tet mesh the per-cell volumes vary (radii 0.176 to 0.261), so a mutant that moves volume between cells passes. Per-cell triple products are closed form.
    Probe, SphericalManifold(radius=1, cellSize=0.4, degree=2), vertex table X[_cell_node_indices(1, True)] from _get_coords_for_basis(1, True):

    cells (324, 3) per-cell max |r^2/flat - 1| = 5.1e-15
    

Notes

  1. The PR body's factors, checked one by one. We ran one probe in both envs: 13 mesh families, degrees 1, 2 and 3 on the box types. Old-to-new factors on the branch:

    • Triangles x2.000 at degree 2, x2.590 at degree 3. Match.
    • Quads x2.000; at degree 3 x3.08e6 to x4.72e6, the body's "about x3e6". Match.
    • Tets x2.000 and x3.000. Match.
    • Hexes on the straight box x3.634 and x4.063. Match.
    • "x4 is x13.2" on hexes is 3.634² = 13.21. Match.
    • Degree 1: bit-identical to development on all 13 families.
    • Degree 2 against degree 1 on straight-sided cells: at most 2.8e-15 (triangles, unstructured triangles, quads, tets, hexes, annulus, quarter annulus, internal-boundary annulus, shell, manifold, internal-boundary box). Matches the body's 3.0e-15.

    Measured callers on UnstructuredSimplexBox(cellSize=1/16, regular=True) with |v| = 1:

    By reading, Nitsche (petsc_generic_snes_solvers.pyx:7147, :9084) is gamma mu / h, so x1/2 matches. SUPG tau 0.01105 to 0.02208 we did not probe; it is close to x2, as expected when the advective term dominates.

    One mismatch: the hex factors hold on straight hexes only. On CubedSphere(numElements=3) and RegionalSphericalBox(3,3,3), degree 2 differs from degree 1 per cell by up to 4.48e-3 and 6.06e-3, and the old-to-new factor is x3.74 to x3.80, so h² callers move x14.0 to x14.4. The totals agree to 1e-10. FVM's volume of a hex with non-planar faces is not the trilinear integral. This is a correction to the body's wording; the code needs no change.

  2. The carved branch against the combined branch. The combined branch is not on origin. No remote ref contains 16689d1a, the commit that a00566a's message corrects. git branch -r --contains 9114e0cf lists only this branch, and the PR body and the record do not name the combined branch. We could not diff against it.

  3. Reader of the changed accessors that the caller table omits. We grepped src/underworld3 for get_min_radius, get_max_radius, get_mean_radius, _cell_radii, cell_size(, petsc_fvm_get* and getMinRadius. Every reader is in the table except the public Mesh.get_min_radius_old (discretisation_mesh.py:7112). Through petsc_fvm_get_min_radius, that method now raises ValueError on degree 2 or more; before, it printed the PETSc error and returned a wrong value. No library code calls it. The message names Mesh._get_cell_radii, a private method (Charter §1); Mesh.get_min_radius() is the public pointer. Two other hits are not separate readers: swarm.py:4436 is the points_in_domain path the table lists at :6383, and visualisation.py:473 is a comment.

  4. pyx:33-35. The docstring of _check_error_code_collectively claims more than the exchange gives. A separate communicator stops a verdict from pairing with a PETSc message, but it does not stop a hang. If DMPlexComputeGeometryFVM fails on some ranks at degree 1, the ranks without the failure wait in the routine's closing Allreduce while the failed ranks wait in the verdict allgather. _compute_geometry_fvm's own docstring says this for degree 2. No degree-1 mesh trips it: across all 13 families, development prints no PETSc error at degree 1. The fix is to the docstring only. In the same file, pyx:119 says "No communication" and the function then makes the allgather.

  5. pyx:138. The num_cells > 0 guard is safe only because rebuild forces the coordinate field on every rank first. That happens in UW_DMForceCoordinateField (petsc_compat.h:186). The PETSc floor of 3.25 in pixi.toml always takes that path. Without a cached field, ranks with cells would create a DMField on the DM's communicator and an empty rank would not, which puts PETSc's per-communicator tag counter out of step. DMFieldCreateFEGeom on an empty IS is local, so the guard buys nothing, and dropping it removes the dependency.

  6. Docstrings. petsc_fem_get_local_cell_sizes is a raw string with doubled backslashes. help() shows \\mathrm, \\sum, \\det and \\xi, which LaTeX reads as line breaks. The module is not autodoc'd. petsc_fvm_get_local_cell_sizes (pyx:88) still says it returns the minimum centroid-to-face distance through DMPlexGetMinRadius; it returns per-cell radii and centroids. That error predates this PR, in a function the diff edits (Charter §2: flag it).

  7. test_a_degree_2_parent_extracts_a_region_some_ranks_hold_none_of has no stated empty-rank power at np 2. It does not assert an empty rank, by design. Its DEGREE2_SUBMESH count line is captured in CI, so we do not know whether np 2 leaves a rank without the "Outer" strip. Tests 1 and 2 assert the premise and carry the np 2 power.

  8. CI reach.

  9. PR text. The body carries Closes #908. The default branch is main, so it fires only at release, which is what adversarial-review.md asks for. Mesh(degree=2): the FVM cell-size call fails and its error is ignored, so a rank with no cells hangs the others #908 is already labelled fixed-in-PR. No change needed.

  10. Attacks that failed, beyond the author's record.

    • test_0782 serial on development: 8 failed, 2 passed. On the branch: 10 passed, 3.4 s warm.
    • Curved P2 tets with cubic det J (x' = x + a y², y' = y + a z², z' = z + a x², a = 0.5; exact volume 1 + a³): the summed volume errs by 4.4e-16 at qdegree 2 and 3, and 9.6e-8 at 1. The default rule is exact on this map.
    • No library path builds a Mesh on a sub-communicator.
    • DMFieldCreateFEGeom (evaluate, degree, complete) has no collectives, so a rank-local failure there reaches the verdict exchange.
    • Every assert in the parallel file follows its collectives and tests a gathered or reduced value, so assert outcomes are the same on every rank.
    • No 1-D or prism meshes exist in src or tests, so no degree-1 mesh is newly made fatal by the code check.
    • An AST scan of every .py for mesh builders with degree 2 or more finds this PR's tests, test_1107 and two files in docs/examples/submesh_investigation/. In .md and .ipynb: none. This matches the body.
    • SegmentedSphericalSurface2D fails with PETSc error 75 at degrees 1 and 2 on both builds. That is unrelated to this PR.

Issue verdicts

Not checked, and what the Linux server should run

  • The parallel file on a00566a itself at np 3, 4 and 8. The author's runs may have been on the combined branch. Record the DEGREE2_SUBMESH line at np 2 and 4:
    for N in 2 3 4 8; do OMP_NUM_THREADS=1 python scripts/mpi_supervisor.py --silence 300 -- mpirun -n $N python -m pytest --with-mpi -s tests/parallel/test_0782_degree2_mesh_empty_rank.py; done
  • Mesh(degree=2): the FVM cell-size call fails and its error is ignored, so a rank with no cells hangs the others #908's own case, np 3, Mesh(degree=2) with cells [16, 16, 0], on eddab70 and on a00566a.
  • The combined branch, on the machine that has it:
    git log --oneline eddab707..<combined>
    git diff <combined> a00566ac -- src/underworld3/cython/petsc_discretisation.pyx src/underworld3/discretisation/discretisation_mesh.py tests/parallel/empty_rank_fixtures.py tests/parallel/test_0782_degree2_mesh_empty_rank.py tests/test_0782_degree2_cell_size.py
    Flag any helper or call site there that this branch still references but lacks.
  • The neighbours named in 9114e0c's message, on a00566a at np 2 and 4: test_0774, 0779, 0781, 0855, 1079, 1080, 1107.
  • Peak RSS around _deform_mesh on a degree-2 hex or tet mesh with at least 200k cells per rank, at qdegree 2 and 3 (Should fix 1).
  • cell_size() diverges on a rank that owns no cells (np=8, tiny mesh) — and that mesh already hangs in construction on development #698 at np 8 on development: the 14-cell UnstructuredSimplexBox, then mesh.cell_size().
  • CI shards 1 to 3 on a00566a. They were queued when we wrote this; test_0782_degree2_cell_size.py runs in shard 2.

Underworld development team with AI support from Claude Code

…s at a time (#908)

The degree-2 cell size built geometry for every local cell at once, on the
solver's quadrature rule, on every coordinate rebuild. A degree-2 hex mesh of
27k cells at qdegree 6 peaked 1.55 GB above its base; 195k cells at qdegree 3,
2.09 GB. It also tied a size to qdegree: at qdegree 1 a curved tetrahedral cube
was out by 9.6e-8.

The rule now follows the coordinate degree, enough to integrate det J exactly,
and the cells go through in bounded blocks. Peak above base is now 3 MB or less
in those cases, and time falls: 8.7 s to 0.64 s, and 11.4 s to 4.4 s.

From the integration session's review of #983:
- the failure check reuses fault_contact._collective_raise, so it names the
  first failing rank rather than listing every rank;
- a rank with no cells now fetches the coordinate field like any other;
- test_0782 compares per cell against closed forms, in place of a sum and of
  an Integral on the same geometry route;
- the fixtures' unused dm_class goes;
- docstrings no longer claim the check prevents a hang, and the refusal names
  the public get_min_radius.

Underworld development team with AI support from Claude Code
@lmoresi

lmoresi commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Response to the second review, at d3f7647

All four should-fixes applied.

  1. Geometry is now built on a rule set by the coordinate degree, in bounded blocks. Peak above base and time for the call, one process each, before then after:
mesh, degree 2 qdegree peak above base call
hex, 195k cells 2 880 MB, then 0 5.0 s, then 4.5 s
hex, 195k cells 3 2090 MB, then 0 11.4 s, then 4.4 s
hex, 27k cells 6 1550 MB, then 3 MB 8.7 s, then 0.64 s
tet, 179k cells 2 414 MB, then 0 1.43 s, then 0.78 s
tet, 179k cells 3 716 MB, then 0 2.15 s, then 0.88 s

Peak is VmHWM reset just before the call; 0 means the call fitted in memory the process already held. Measured on the cell-size call itself, not around a whole _deform_mesh. New tests: a curved cell at qdegree 1, 2, 5 (tets at qdegree 1 failed on a00566a, out by 9.6e-8), and block splitting, which fails on a block-offset mutant.

  1. dm_class removed.
  2. The check reuses fault_contact._collective_raise. A consequence: the message now names the first failing rank, not every rank's code.
  3. Per-cell closed forms for tets, hexes and manifold facets. A hex tolerance is 1e-10, because this box's coordinates scatter by 2e-11 at degree 1 too.

Also done from the notes: the docstrings (4, 6), dropping the num_cells > 0 guard around the coordinate field (5), get_min_radius_old in the table with a public pointer in the message (3), and warped-hex factors in the body (1).

Not done: note 7. test_a_degree_2_parent_extracts... still reports its empty-rank count without asserting it. At np 2 there is no empty rank in it ([22, 20]), so tests 1 and 2 carry np 2, as you say.

The server runs you asked for, at d3f7647 unless stated

Underworld development team with AI support from Claude Code

@lmoresi

lmoresi commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

No #909 or #965 hunk in this PR

The combined branch is local to the Linux server, so the commands below need that clone. Run in the worktree of bugfix/parallel-collectives-908-909, or any clone holding 16689d1a..ed5f6253 and this PR.

git diff --stat eddab707 d3f7647e
git diff eddab707 d3f7647e -- src/underworld3/discretisation/discretisation_mesh.py | grep '^@@'
git diff eddab707 d3f7647e | grep -E '^[+-].*(remesh|global_evaluate|_transfer_values|_evaluated_in_storage|meshVariable_lookup)' | wc -l
git diff --stat ed5f6253 a00566ac -- src/underworld3/cython/petsc_discretisation.pyx src/underworld3/discretisation/discretisation_mesh.py tests/parallel/empty_rank_fixtures.py tests/parallel/test_0782_degree2_mesh_empty_rank.py tests/test_0782_degree2_cell_size.py

Results:

  1. This PR touches five files: petsc_discretisation.pyx, discretisation_mesh.py (9 lines), empty_rank_fixtures.py, and the two test_0782 files. ddt.py, swarm.py, remesh.py and _evaluation.py, where Mesh.remesh: a collective call sits behind a rank-local guard in the variable transfer #909's and global_evaluate on a submesh raises on empty ranks and its peers hang (np 4) #965's work sits, are not among them.
  2. discretisation_mesh.py has one hunk, at line 7036, in _get_cell_radii's area, not in Mesh.remesh.
  3. Lines changed naming remesh, global_evaluate, _transfer_values, _evaluated_in_storage or meshVariable_lookup: 0.
  4. Combined branch at ed5f6253 against this PR's a00566a, on those five files: only discretisation_mesh.py (94 lines) and empty_rank_fixtures.py (8 lines) differ. This is the removal of Mesh.remesh: a collective call sits behind a rank-local guard in the variable transfer #909's transfer and the dm_class parameter.

The check runs on names and files, so it would miss a #909 change under another name; the diff is 453 lines, read by two reviews.

Underworld development team with AI support from Claude Code

@lmoresi

lmoresi commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Adversarial review of #983 at d3f7647 (round 2)

This pass covers only d3f7647, the one commit since a00566a. We rebuilt the review worktree at d3f7647; uw.__file__ resolves into the worktree's own env. All runs are serial, on macOS with conda-forge PETSc 3.25.3. Development behaviour comes from the same env as round 1.

Round-1 should-fixes

  1. Geometry built for every cell at the solver's qdegree: resolved. On StructuredQuadBox(elementRes=(30,30,30), degree=2), with each qdegree in its own process:

    • The call takes 0.44 s at qdegree 2 and 0.43 s at 6; at a00566a it took 0.43 s and 5.22 s.
    • Peak RSS after the build is 1661 MB and 1579 MB; at a00566a it was 1541 MB and 3057 MB.

    Blocking does not change the answer: see the block note below.

  2. Dead dm_class: resolved. grep -rn dm_class tests src returns 0 lines.

  3. Second implementation of _collective_raise: resolved. pyx:44 now calls fault_contact._collective_raise. At np 1 the message reads X failed on rank 0 with error code 5, which is the form the parallel test expects. fault_contact is already in sys.modules after import underworld3, so the call-time import adds no cost and no import cycle.

  4. Baselines that are not closed forms: resolved. Tets are checked per cell against triple products, hexes against 1/27, and manifold cells against half the cross product of the flat facet's edges. On development, test_0782_degree2_cell_size.py gives 15 failed and 2 passed; at d3f7647, 17 passed in 3.5 s.

The round-1 notes are also addressed. No docstring has doubled backslashes; the FVM docstring and the docstring of the check are corrected; the FEM function says COLLECTIVE; the num_cells > 0 guard is gone; and the refusal names a public method.

Must fix

None.

Should fix

  1. Low. tests/test_0782_degree2_cell_size.py:101. The hexahedra rows of test_a_curved_cell_size_does_not_depend_on_the_solver_quadrature cannot fail on the defect they name.
    • On this map (x' = x + a y², and so on), det J = 1 + 8a³xyz has degree 1 in each variable. a00566a's lowest rule, 2 Gauss points per edge at qdegree=1, already integrates it exactly. Only the tetrahedra row at qdegree=1 has power, which round 1 measured at 9.6e-8.
    • A Q2 map with det J of degree 4 in x gives the hex rows power: x' = x + a y² z², y' = y + a z² x², z' = z + a x² y², with closed form 0.88125 at a = 0.5.
    • Probe: deform StructuredQuadBox((3,3,3), degree=2) by the map, then compare the summed volume against the closed form from sympy, once with the rule patched to 2 points per edge and once with the new rule.
    test's map  x+a y^2:     closed form 1.125000000000000; rel err of the sum: new rule 4.4e-16, 2 Gauss pts/edge 4.4e-16
    Q2 map      x+a y^2 z^2: closed form 0.881250000000000; rel err of the sum: new rule 2.2e-16, 2 Gauss pts/edge 5.8e-05
    

Notes

  1. The manifold rule on curved facets: the author's question.

    • Setup: we projected every node of SphericalManifold(degree=k) radially onto the unit sphere, so each facet is a curved P_k patch. Its area is up to 3% more than the flat facet's.
    • Comparison: each cell's area under the new rule against a 30-point-per-edge reference, by patching _quadrature_points_per_edge. The 20-point and 30-point references agree to 4.4e-15 or better, so the reference is converged.
    • We also ran the rule that takes only the determinant's degree, without the doubling.
    degree cellSize (cells) rule, points per edge max per-cell rel. error, new rule determinant-degree rule
    2 0.8 and 0.4 (324; the builder's floor) 3 1.5e-7 2.2e-5
    2 0.2 (804) 3 1.6e-8 5.5e-6
    2 0.1 (3152) 3 1.3e-10 2.1e-7
    3 0.8 and 0.4 (324) 5 1.9e-10 8.9e-7
    3 0.2 (804) 5 2.2e-12 8.5e-8
    3 0.1 (3152) 5 4.0e-15 6.7e-10

    The worst cell is out by 1.5e-7 in area, so 7.6e-8 in radius. A cell size is fine at that accuracy. Doubling the degree buys a factor of 100 or more over the determinant's degree, and the docstring at pyx:120 says what the rule does and does not integrate exactly.

  2. Curved volume cells, and the rule's degree at coordinate degrees 2 and 3. For each case we deform a unit box by a polynomial map whose det J has the full degree the formula allows (where we could build one), keeping det J positive. We compare each cell under the new rule with a 12-point-per-edge reference, and with the rule one point per edge smaller.

    cell, map points per edge max per-cell error one point fewer
    triangle P2, det degree 2 2 1.6e-15 1.1e-2
    triangle P3, det degree 4 3 1.9e-15 1.8e-6
    quad Q2, det degree 3 in x 2 7.8e-16 4.2e-3
    tet P2, det degree 3 (round-1 map) 2 4.0e-15 3.4e-3
    tet P3, det degree 6 4 3.0e-15 7.8e-9
    hex Q2, det degree 4 in x 3 4.2e-15 4.1e-4
    hex Q3, det degree 6 in x 5 3.3e-15 3.3e-15
    • The rule scales with the coordinate degree as the formula says: dim(k - 1) on simplices, dim·k - 1 per variable on tensor cells, doubled on a manifold.
    • The rule is exact in every case, and tight wherever the map reaches the full degree.
    • The Q3 hex map reaches degree 6 per variable, not 8, so it shows exactness but not tightness there.
    • The summed tet volumes equal the closed form 1 + a³ to 1e-15.
    • Degree 2 and 3 radii on all 13 round-1 families agree with a00566a to 3.1e-15 per cell. Degree-1 radii are still bit-identical to development. No PETSc errors are printed.
  3. A folded curved cell has no polynomial det J, and the size reports it plainly. At a = 0.5 the P3 triangle map x' = x + a y³ has det J = 1 - 2.25 x²y², which is negative near (1, 1). The new rule is then out by 4.1e-2 per cell against the reference, with no flag. This is the author's surviving abs mutant seen from the quadrature side. It is a note, not a request.

  4. The blocked loop. On curved, non-uniform cells (370 P2 tets with 369 distinct sizes; 60 Q2 hexes with 59), sizes are np.array_equal to the single-block result at every block size we tried:

    • 1, 2, 7 and 13 cells a block, which leaves remainders of 0, 0, 6 and 6 on the tets and 0, 0, 4 and 8 on the hexes;
    • n - 1 cells, which leaves one cell over;
    • a block larger than the rank.

    Setting _POINTS_PER_GEOMETRY_BLOCK to a string raises TypeError, so the constant is read at call time and the author's monkeypatched test does exercise the blocks. By reading, a rank with no cells:

    • builds and destroys the rule;
    • fetches the coordinate field (now unconditional);
    • skips the loop, since 0 < 0 is false;
    • reaches the check.

    The parallel np 2 run at d3f7647 is still pending in CI.

  5. The collective check, path by path.

    • Everything before _check_error_code_collectively in the FEM function returns an error code rather than raising: rule creation, PetscQuadratureGetData, DMGetCoordinateField, DMFieldCreateFEGeom and PetscFEGeomDestroy.
    • Any of them failing stops the loop and goes to the check.
    • max(1, ... // max(1, Nq)) covers Nq = 0 after a failed rule.
    • The only Python raise before the check is the COMM_SELF stride IS inside the loop.
    • The FVM path refuses on mesh.degree on every rank, then checks.
    • _collective_raise exchanges only when size is above 1 and raises the first failing rank's message on every rank.

    Every rank reaches the check on every path. The new docstring says the exchange cannot rescue a routine that fails inside its own collective.

  6. pyx:65. The refusal says "Mesh.get_min_radius handles any degree". It is raised from petsc_fvm_get_local_cell_sizes too, whose public equivalent is the per-cell Mesh.cell_size(). This is wording only.

  7. CI and reach. check_test_coverage.py reports "OK: all 391 test files are reachable", the same file set as round 1. At d3f7647, shards 1 to 4 were pending when we wrote this.

Issue verdicts

Not checked, and what the Linux server should run

  • The parallel file at d3f7647 at np 2, 3, 4 and 8. The author reports 4 passed at each; CI at np 2 is pending.
    for N in 2 3 4 8; do OMP_NUM_THREADS=1 python scripts/mpi_supervisor.py --silence 300 -- mpirun -n $N python -m pytest --with-mpi tests/parallel/test_0782_degree2_mesh_empty_rank.py; done
  • A rank-asymmetric failure of DMFieldCreateFEGeom. We know no way to provoke one, so the code check still has no exercised failure path.
  • Peak RSS across a whole _deform_mesh on a degree-2 mesh with at least 200k cells per rank. The author measured the cell-size call only.
  • cell_size() diverges on a rank that owns no cells (np=8, tiny mesh) — and that mesh already hangs in construction on development #698 with the reported 14-cell UnstructuredSimplexBox at np 8, which leaves one rank empty.

Underworld development team with AI support from Claude Code

…908)

The hex rows used a map whose det J has degree 1 in each variable, which two
points a side already integrate. x' = x + a y^2 z^2 and cyclic has degree 4:
forcing two points gives 0.881301 against the exact 0.88125, and all three rows
fail on that mutant. From the second review of #983. The refusal also names
Mesh.cell_size.

Underworld development team with AI support from Claude Code
@lmoresi

lmoresi commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Round 2's should-fix is applied at 7397bd1. The hex rows now use x' = x + a y^2 z^2 and cyclic; sympy gives det J degree 4 per variable (not the rule's worst case of 5) and volume 1 - 3a^2/5 + a^3/4, 0.88125 at a = 1/2. With the rule forced to two points the three hex rows fail at 0.881301; 17 passed on the real code; parallel test_0782 4 passed at np 2 and 3 on the same code. The refusal now also names Mesh.cell_size.

Left as is: a folded cell's error goes unflagged (the known abs limitation, in the first record).

Underworld development team with AI support from Claude Code

@lmoresi
lmoresi merged commit 145f95d into development Nov 7, 2026
8 checks passed
gthyagi pushed a commit to gthyagi/underworld3 that referenced this pull request Nov 13, 2026
…worldcode#978)

The degree argument of Mesh and of the uw.meshing constructors set the
degree of the coordinate field. Particle location, cell sizes, boundary
normals, mesh motion and the multigrid hierarchy all read the coordinates
as straight-sided cells, and a degree-2 mesh failed in each. It did not
curve a boundary either: the edge nodes sat on the chords.

- Mesh._require_straight_sided_cells raises ValueError unless degree is
  the integer 1. Mesh.__init__ calls it first, and each of the 20
  uw.meshing constructors calls it before generating anything, so every
  rank refuses together and rank 0 never starts gmsh.
- mesh.degree is a read-only property returning 1.
- Removed what existed only for degree-2 coordinates: the quadrature cell
  sizer petsc_fem_get_local_cell_sizes and its PETSc declarations (underworldcode#983),
  the degree guard in _compute_geometry_fvm, and the
  _finest_level_takes_moved_coordinates guard in _deform_mesh (underworldcode#1044).
- Docstrings of all constructors and docs/developer/subsystems/meshing.md
  say that the mesh degree is 1 and the variable carries the order.
- Tests: test_0782 (serial and parallel) now hold the refusal for every
  constructor found by signature, the strict check, the read-only
  property, and the degree-1 cell sizes that the degree-2 tests carried
  (manifold facets, empty rank, extracted region). The estimate_dt
  accuracy step keeps its closed-form baseline on a degree-1 mesh.

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