Skip to content

Mesh coordinates are degree 1: a mesh refuses any other degree (#978) - #1088

Merged
lmoresi merged 3 commits into
developmentfrom
bugfix/mesh-degree-one-only-978
Nov 12, 2026
Merged

lmoresi merged 3 commits into
developmentfrom
bugfix/mesh-degree-one-only-978

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 12, 2026

Copy link
Copy Markdown
Member

What this does

Mesh coordinates are degree 1, and a mesh now says so. Mesh and every uw.meshing constructor raise ValueError for any degree other than the integer 1. The order of a field is set on the variable (MeshVariable(..., degree=2)), as it always was.

This is the ruling on #978: degree-2 coordinate meshes were never supported, and supporting them would be a campaign across particle location, cell geometry, boundary normals, mesh motion and the multigrid hierarchy. #978 is closed as not planned, with a link here.

Why refuse instead of fixing

  • A degree-2 mesh failed in each of those subsystems: FVM cell sizes read edge nodes as corners (Mesh(degree=2): the FVM cell-size call fails and its error is ignored, so a rank with no cells hangs the others #908), boundary_normal raised, estimate_dt raised, and deform broadcast the vertex coordinates into the larger degree-2 coordinate vector.
  • It did not curve the mesh either. The edge nodes sat on the straight chords: on an annulus at cellSize=0.2 the outer-wall edge nodes were at r = 0.995 between vertices at r = 1.
  • No production, test or campaign path built one, other than the tests written for the partial fixes.

Changes

Tests

  • tests/test_0782_mesh_coordinates_are_degree_one.py (level 1, tier B) replaces test_0782_degree2_cell_size.py:
    • every uw.meshing constructor found by signature refuses degree=2 before it starts gmsh or reaches Mesh;
    • Mesh(plex, degree=d) refuses 0, 2, 3, 1.0, True, "1", None;
    • a plex with degree-2 or degree-3 coordinates is refused, and one with a degree-1 coordinate space builds;
    • mesh.degree cannot be assigned;
    • a degree-2 variable on a degree-1 mesh builds with its 81 nodes;
    • each cell of a manifold has the area of its flat facet (the one degree-1 cell-size case the old file held alone).
  • tests/parallel/test_0782_mesh_degree_refused_on_every_rank.py (level 2, tier B) replaces test_0782_degree2_mesh_empty_rank.py: cell sizes with an empty rank, the refusal from Mesh and from a constructor on every rank, the collective error-code check, and the extracted-region area.
  • test_1153: the Eulerian accuracy step keeps its closed-form baseline 0.02/pi, now on a degree-1 mesh at 3 %.
  • test_1107: the degree-2 mesh case is removed; the degree-1 cases stay.

Mutants: with the call removed from SphericalShell and from Mesh.__init__, exactly the SphericalShell case and the seven argument cases fail (8 failed, 23 passed). With the plex check disabled and the call removed from Annulus, exactly the two plex cases and the Annulus case ("Annulus started gmsh") fail (3 failed, 31 passed).

Serial on macOS: pytest tests -m "level_1 and (tier_a or tier_b)" at 326ae83: 1965 passed, 65 skipped. A level-2 tier A/B subset on mesh-building routes (-k "submesh or extract or extrude or periodic or adapt or refine or checkpoint or manifold"): 102 passed, 49 skipped. Parallel on hyperion (Linux) at 326ae83, np 2, 3 and 4, every rank: test_0782_mesh_degree_refused_on_every_rank.py 5 passed, test_1107_interpolation_rank_owning_nothing_parallel.py 11 passed, test_0781_empty_rank_mesh_sequence.py 4 passed; no hangs. The empty-rank and submesh files 0774, 0779, 0783, 0768 and 0690 pass. test_0770_submesh_extract_mpi fails 2 tests at np 3 only, and fails the same 2 on development (5ce73fd).

Issues

Underworld development team with AI support from Claude Code

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 (#983),
  the degree guard in _compute_geometry_fvm, and the
  _finest_level_takes_moved_coordinates guard in _deform_mesh (#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
Underworld development team with AI support from Claude Code
…sal survives a replaced Mesh name (#978)

- Mesh(plex) accepted a plex that already carried a degree-2 coordinate
  space and left X.coords with 25 nodes over a dm with 9. The refusal now
  also reads the plex's coordinate space, a property every rank holds
  alike.
- The uw.meshing constructors call the refusal through
  uw.discretisation.Mesh, not their module's Mesh name:
  test_0502's files-only test replaces that name with a function and the
  call failed with AttributeError.
- test_0782: a constructor that starts gmsh fails the test, not only one
  that reaches Mesh; a plex with degree-2 or degree-3 coordinates is
  refused and one with degree-1 coordinates builds.
- Wording that still described degree-2 coordinates as a live case.

Underworld development team with AI support from Claude Code
Copilot AI balanced review requested due to automatic review settings November 12, 2026 03:02

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

Copy link
Copy Markdown
Member Author

Adversarial review, round 1 (fresh reviewer, before the PR opened)

Reviewed at the pre-commit working tree; fixes are in da43d75. No blocker.

Fixed in this branch

  1. tests/test_1153: removing the degree-2 estimate_dt test took the only absolute baseline on the Eulerian accuracy step (dt_accuracy is read by no other test; the remaining check compares a units model with its twin, so a common scale error passes). Restored on a degree-1 mesh: 0.006467 against 0.02/pi = 0.006366, asserted at 3 %.
  2. meshing.md said the test held the refusal "for each constructor family"; it covered four. The test now covers every uw.meshing constructor found by signature (20), and asserts the list is not short.
  3. Every constructor ran gmsh on rank 0 before Mesh refused: a serial user waited for the mesh, and under mpirun the other ranks raised while rank 0 was in gmsh. The refusal is now the first statement of each constructor. The test replaces Mesh.__init__ with a function that fails, so a constructor that gets that far fails it.
  4. Message and strictness: degree="1" printed "degree=1 ... mesh coordinates are degree 1"; 1.0 and True passed the check and died in PETSc with error 63. The check is now "the integer 1", the message uses repr.
  5. mesh.degree was writable and drove the coordinate rebuild; with the FVM guard gone, mesh.degree = 2 then a deform failed after the coordinates had moved. It is now a read-only property.
  6. Coverage that left with the degree-2 files and was held nowhere else: manifold cell areas against the flat facet (restored, serial), and the extracted-region area 0.15 with ranks holding none of it (restored, parallel). The tetrahedron and hexahedron cases are held by tier-A test_0010.
  7. The two closed-form radius tests in the new file repeated test_0010 and passed without the refusal: removed. assert mesh.degree == 1 after a default build: removed from that test.
  8. The parallel file's docstring claimed every test asserts the empty-rank premise; only the cell-size test does. Reworded.
  9. Stale wording: qdegree "at least 2 * degree" under a degree that is always 1; "a degree-2 coordinate mesh" in visualisation.py; "element degree" in SegmentofAnnulus; BoxInternalPatch had no degree entry; the r = 0.995 figure now names its cell size (0.2).

Not changed, with the reason

  • The removed _finest_level_takes_moved_coordinates also caught a stale dm_hierarchy[-1] on a variable-less submesh re-extracted after its parent is remeshed, then moved. The only caller of _re_extract_from_parent is remesh (mmg). At degree 1 the refined, coarsened and reload paths give dm_hierarchy[-1] and self.dm the same layout.
  • The within-plex coordinate helper (custom_mg at np 32 and 52: the nested interpolation fails on preallocation, the FMG build falls back and the ranks deadlock #902) keeps its comments about degree >= 2 spaces. At degree 1 it takes the copy branch; its interpolation branch is not reached for coordinates through the public API. It is left as it is.

Checked and sound

  • No call site in src, tests, docs, scripts passes a mesh degree other than 1 (syntax-tree scan binding arguments to signatures; positional, partial and **kwargs forwarding included).
  • The removed Cython pieces have no users; petsc_quadrature_fe.pyx declares its own externs.
  • The refusal precedes every collective in Mesh.__init__, and no constructor has a barrier or broadcast before it.
  • Both new test files are reached by scripts/test.sh (check_test_coverage.py: all 426 reachable).

Issue state

Adversarial review, round 2 (fresh reviewer, at 77bbbdb)

No blocker. Fixes are in 326ae83.

Fixed in this branch

  1. Mesh(plex) accepted a plex that already carried a degree-2 coordinate space: X.coords held 25 nodes, mesh.dm 9, and with refinement=1 a deform failed on a broadcast of (81, 2) into (25, 2). The refusal now reads the plex's coordinate space (degree 1 for every library-built plex, by probe on quad, triangle, tetrahedron, refined, annulus, manifold, shell and cubed-sphere meshes). An order-2 gmsh file was already refused by PETSc (error 62).
  2. The serial constructor test showed refusal before Mesh.__init__, not before gmsh: a refusal placed after the gmsh block passed it. It now also fails when gmsh.initialize is called.
  3. The parallel constructor test cannot tell an early refusal from the one in Mesh.__init__ (the ranks meet in the gather either way). Its docstring claimed it could; reworded, and the serial test holds the ordering.
  4. Stale wording: a degree-2 coordinate mesh in a test_1057 comment, "needed for higher-order (curved) coordinate meshes" in Mesh, and the history of the old hang in the parallel docstring.

Found by the serial suite after round 1's fixes

  • test_0502::test_spherical_internal_boundary_mesh_files_only replaces the name Mesh in meshing/spherical.py with a function, and the constructors' new first statement, Mesh._require_straight_sided_cells(degree), raised AttributeError. The constructors now call it as uw.discretisation.Mesh._require_straight_sided_cells(degree).

Not changed, with the reason

Checked and sound

  • Nothing in src assigns degree on a Mesh; there is no Mesh subclass, __setstate__, __reduce__ or attribute copy that would meet the read-only property. Checkpoints store variable degrees only.
  • No function in src that takes a degree and builds a Mesh lacks the refusal as its first statement.
  • The removed deform guard loses nothing for degree-1 input: every assignment of dm_hierarchy[-1] gives it self.dm's layout.
  • Parallel on hyperion at 326ae83, np 2, 3 and 4: the 0782, 1107 and 0781 parallel files pass on every rank, and no rank raised or hung in the coordinate-space read, including ranks holding no cells.

Underworld development team with AI support from Claude Code

@lmoresi
lmoresi merged commit 93e66ee into development Nov 12, 2026
10 checks passed
@lmoresi

lmoresi commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

PR #1088 (#978, mesh coordinates are degree 1): adversarial pass on what it removes

READY for 326ae83. No blocking finding. There are three non-blocking follow-ups, listed at the end.

Scope: only what the PR removes. That is the deform guard _finest_level_takes_moved_coordinates (#1044), the quadrature cell sizer petsc_fem_get_local_cell_sizes (#983) and the FVM degree guard. I also tested the new refusal that replaces them. Builds: the branch is wt-978 at 326ae83 and the base is wt-1152d at dd92c70, which is also the merge-base. The scripts are in this directory: q1_hierarchy.py, q2_cellsize.py, q2_np4.py, q3_refusal.py and q3_readfile.py. Tests ran from git archive copies (src-978, src-1152d). Anything marked [read] comes from reading the code and was not measured.

1. The deform guard (#1044)

What the guard checked. [read] It did not check the degree at all. It checked one thing: if dm_hierarchy[-1] is not mesh.dm, is the local coordinate vector the same size on both, combined across ranks with LAND? It then raised a RuntimeError, on every rank, before _deform_mesh changed anything. The message talked about degree 2, but the check catches any finest level whose layout differs from mesh.dm, including a stale DM of a different topology.

Where dm_hierarchy[-1] can differ from mesh.dm. [read] Three code paths replace self.dm:

  • _re_extract_from_parent. It sets self.dm = new_subdm and never touches dm_hierarchy. A submesh with no variables never goes through _setup_ds, the only place that sets dm_hierarchy[-1] = dm_new, so its hierarchy stays stale.
  • remesh. It also sets self.dm = new_dm. The parent always has the metric variable, so variable re-initialisation repairs the hierarchy.
  • Refinement and coarsening. These set self.dm to a clone of the finest level, so the two share a layout.

_re_extract_from_parent is called only from remesh, which needs mmg. This PETSc has no mmg, parmmg or pragmatic (measured with hasExternalPackage).

Measured. After each path I compared the finest level with mesh.dm (identity, local sizes, maximum coordinate difference) and ran a Laplace solve with T = x on the boundary after a 0.3 shift in x. A solve on stale geometry would be about 0.3 off.

These paths behave identically on the branch and the base, at np 1 and np 2:

Path Finest level after the move Solve error, max abs(T - x)
Submesh, parent moved is mesh.dm 2e-16 at np 1, 4e-6 at np 2
Submesh moved itself is mesh.dm 2e-16 at np 1, 6e-6 at np 2
Refined mesh, deform clone of mesh.dm, same size, difference 0 2e-5
Refined mesh, ephemeral_coords (inside and after exit) clone of mesh.dm, same size, difference 0 2e-5
Refined mesh, redistribute_nodes then relax clone of mesh.dm, same size, difference 0 1e-5
Adapt child, moved is mesh.dm 5e-7
Adapt child, base moved child still is its mesh.dm 5e-7
Checkpoint reload of a refined mesh, then deform clone of mesh.dm, same size, difference 0 2e-5

The errors of 1e-5 on the refined meshes are the iterative solver's tolerance: base and branch match bit for bit, and stale geometry would give 0.3. No non-mmg public path leaves a finest level of a different size, so none is left unguarded.

The mmg route, with the stand-in adapter. I used AdapterStandIn from tests/parallel/empty_rank_fixtures.py: a parent labelled Left with a variable-less submesh, remesh to a finer DM, then a move of the submesh. The submesh went from 12 cells to 48. Its finest level held 24 coordinates against 70 in mesh.dm at np 1, and (14,16) against (36,46) at np 2.

Base Branch
Move Guard raises a clean RuntimeError, rank-uniform VecCopy raises PETSc error 75 ("Incompatible vector local lengths") from _sync_hierarchy_finest_coordinates, on every rank
State at the error nothing changed yet mesh.dm already moved and rebuilt; _coords view, solver flags and submesh propagation skipped
Solve, with or without a move fails ("could not broadcast (12,) into (35,)"; at np 2 the messages differ by rank) fails the same way

So the stale hierarchy is a pre-existing bug, and the guard only made the move fail cleanly. I also called _re_extract_from_parent directly on an unchanged, moved parent at np 2, a private-API emulation of the same route. The new DM is not distributed like the old one: sizes (0,52) and (96,52). On the base, the guard raised and the later solve hung (diagnosed by the supervisor). On the branch, the move raised PETSc error 75 on both ranks. The next read of the finest level's coordinates then segfaulted on rank 0, ending in MPI_Abort 59. The removal turns a clean error on an mmg-only path into a partly mutated mesh and, at np 2, a crash. It is not blocking: the path needs mmg, it was already broken, and the solve fails on both builds. The proper fix is follow-up 1.

2. #983's cell sizer and the FVM degree guard

#908's collective error-code check holds. The parallel tests ran on the branch at np 2, 3 and 4, every rank, with no hangs: test_0782_mesh_degree_refused_on_every_rank.py, test_1107_interpolation_rank_owning_nothing_parallel.py and test_0781_empty_rank_mesh_sequence.py, 20 passed per rank. They include test_an_error_code_on_one_rank_raises_on_every_rank and the empty-rank cell-size test, which asserts exactly one empty rank.

Empty-rank cell sizes and FVM setup at np 4. Cells per rank were [6, 5, 5, 0]. I called petsc_fvm_get_local_cell_sizes directly, then get_min_radius. It returned 0.176776695296637, equal to the closed form sqrt(1/(2n^2)), on both builds with no hang. My first np 4 run did hang, but that was my script calling get_min_radius on rank 0 only; it is fixed and not a finding.

The degree-1 path is unchanged. [read] On the base, _get_cell_radii already took petsc_fvm_get_local_cell_sizes when degree == 1, and the branch keeps that call unconditionally. [measured] _cell_radii hash, get_min_radius and get_max_radius are bit-identical between base and branch on six meshes: Annulus, StructuredQuadBox, UnstructuredSimplexBox with refinement 1, SphericalShell, SphericalManifold, and BoxInternalBoundary's extract_region("Outer"). The Annulus also matches at np 4. No source, test or script still references the removed symbols.

3. The refusal

  • Constructors. All 20 uw.meshing callables with a degree parameter raise ValueError ("mesh coordinates are degree 1") for degree=2, in 0.00 s, without starting gmsh (I hooked gmsh.initialize).
  • Mesh(plex) with a degree-2 coordinate space is refused with "the plex carries coordinates of degree 2". I built such plexes three ways: setCoordinateDisc with a Q2 FE on a quad box, setFromOptions with dm_coord_petscspace_degree 2, and P2 on the simplex fixture. The refusal holds at np 1 and 2. With an empty rank it holds at np 2 [1,0], np 3 [8,8,0] and np 4 [6,5,5,0]. Every rank gave the same message and nothing hung. UnstructuredSimplexBox(degree=2) is rank-uniform at the same rank counts.
  • Derived meshes come out with degree 1 and a degree-1 coordinate FE: extract_region, adapt, SphericalManifold and extrude_sheet. remesh [read] passes the parent's degree, which is now always 1. mesh.degree = 2 raises AttributeError.
  • Checkpoint written by development with a degree-2 mesh. On the base, UnstructuredSimplexBox(degree=2) gives a coordinate vector of 101 nodes over 30 vertices. I wrote it with write_timestep and with write.
    • On the branch, Mesh(file) with the default degree 1 fails inside PETSc DMPlexCoordinatesLoad with error 62, "Number of coordinates loaded 101 does not match number of vertices 30". The base fails the same way. With degree=2 the branch refuses with the clean ValueError.
    • At np 2, Mesh(file) hangs inside _from_plexh5 on both builds (rank 0 in a barrier, rank 1 in an allreduce).
    • A degree-2 variable read from that file onto a degree-1 mesh (read_timestep) works, with error 0 against x.
    • So no file reaches a degree-2 coordinate space, but the file route bypasses the plex check (plex_or_meshfile is a str) and fails noisily, and at np 2 by hanging. This is pre-existing (follow-up 2).
  • Order-2 gmsh .msh (setOrder(2)): Mesh(file) fails with PETSc error 62 at np 1 on both builds. No degree-2 mesh results.

Blocking findings

None.

Non-blocking follow-ups

  1. _re_extract_from_parent leaves dm_hierarchy stale. It should reset dm_hierarchy = [self.dm] (and dm_h) when it replaces the DM. This is pre-existing and only mmg reaches it. Without the A solve after a move of a refined mesh runs on the moved geometry (#1037) #1044 guard, a move on that path now fails inside PETSc after a partial mutation, and segfaults at np 2 (private-API emulation). With the guard it failed cleanly first. A solve there fails on both builds. Worth an issue so the mmg route does not regress silently.
  2. Mesh from a file with higher-degree coordinates. Mesh("x.h5") and order-2 .msh fail in PETSc coordinatesLoad (error 62), and the .h5 load hangs at np 2, on base and branch alike. The new plex check does not run on the file route. This is pre-existing. A collective pre-check of the coordinate count in the h5 file would turn it into a rank-uniform ValueError.
  3. 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 still open. Its degree-2 trigger is now unreachable and its collective check is tested, so Mesh(degree=2): the FVM cell-size call fails and its error is ignored, so a rank with no cells hangs the others #908 could be closed with a link to Mesh coordinates are degree 1: a mesh refuses any other degree (#978) #1088. The PR body names only Degree-2 meshes: computeCellGeometryFVM, boundary_normal and estimate_dt still read or refuse midpoints #978 and Native higher-order rendering refuses valid high-aspect and k>=4 cells, and falls back to Delaunay silently (follow-up to #1057) #1083.

Not covered: a real mmg build (stand-in only); np 3 and 4 for the Q1 scenarios (np 1 and 2 as asked); 3-D submeshes.

Underworld development team with AI support from Claude Code

lmoresi added a commit that referenced this pull request Nov 15, 2026
…1088 lists

Underworld development team with AI support from Claude Code
jcgraciosa pushed a commit to jcgraciosa/underworld3 that referenced this pull request Nov 17, 2026
Records Louis's ruling of 2026-11-12 (underworldcode#1088, underworldcode#978 not planned) in the
Charter's "Things to Remember", with the reason: particle location is exact
only on planar faces, and links the UWTN article that explains it.

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.

2 participants