Skip to content

Projecting a discontinuous field to a continuous one averages the cells at a node (#905) - #973

Merged
lmoresi merged 2 commits into
developmentfrom
bugfix/project-dg-average-905
Nov 7, 2026
Merged

lmoresi merged 2 commits into
developmentfrom
bugfix/project-dg-average-905

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 6, 2026

Copy link
Copy Markdown
Member

Projecting a discontinuous field to a continuous one averages the cells that meet at a node, instead of summing them (#905).

The bug

uw.function.project_to_degree(var, 1, continuous=True) on a discontinuous variable returned each node value multiplied by the number of cells meeting at that node. That is 2 to 7 times the value at P1 vertices. project_to_vertices and field_projection.write_vertices_to_viewer call it, so they did the same. The interpolation matrix gives a continuous node shared by k cells one copy of each cell's interpolant, in different source columns, so its row summed to k.

The XDMF writer projects a discontinuous field to DG0, so it was not affected.

The change

For a discontinuous source, each row of the interpolation matrix is divided by its sum. Each cell's rows sum to 1, so this averages the cells meeting at the node. A continuous source's rows already sum to 1, and its matrix is left untouched, so those results are bit-for-bit as before.

Tests

tests/test_0005_project_discontinuous_to_continuous.py (tier B, shard 4's test_00[0-4]* batch):

  • a DG1 field that is linear everywhere projects to its exact P1 values, and project_to_vertices agrees exactly;
  • a DG0 field of 1 projects to 1;
  • a continuous P2 source projects exactly as before.

On development eddab70 the first two fail and the third passes. On this branch all three pass. The XDMF and save/load tests (test_0003, test_0005_xdmf_*) give 35 passed.

Issues

Underworld development team with AI support from Claude Code

…he cells at a node

project_to_degree (and project_to_vertices and write_vertices_to_viewer,
which call it) returned each node value of a discontinuous variable times
the number of cells meeting there: the interpolation matrix gives a
continuous node shared by k cells one copy of each cell's interpolant, in
different source columns, so its row summed to k (2 to 7 at P1 vertices).
For a discontinuous source each row is now divided by its sum, which
averages the cells. A continuous source's rows already sum to 1 and the
matrix is left untouched.

test_0005_project_discontinuous_to_continuous (tier B): a linear DG1 field
projects to its exact P1 values and a DG0 constant to the constant (both fail
on development eddab70); a continuous P2 source projects exactly as before.
The XDMF and save/load tests pass.

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

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 of #973

Verdict: ready with the changes below; no defect found in the fix. Head 0168c2d, base eddab70, np 1, 2, 3, 4 and 8 through the supervisor.

Findings

  1. Low, side effect, no wrong value. The scaling runs whenever the source is discontinuous, even when the target is discontinuous too (mesh_var.continuous is tested, not continuous). Those rows already sum to 1, so the divide only adds rounding. Against base at np 2: DG3 to DG0 differs by 2.2e-16, DG3 to DG3 by 1.2e-15 (quad 1.1e-15), DG2 to DG2 by up to 2.1e-15 (quad), DG1 to DG1 by 2.2e-16. DG1 and DG2 to DG0 are bitwise identical. The XDMF /visualization path (discretisation_mesh.py, near line 9919) projects DG of degree 2 or more to DG0, so a DG3 dataset on disk can change in the last bit. The docstring's "the identity and the result matches the source exactly" is now true only to roundoff. Fix: if not mesh_var.continuous and continuous:, and every non-targeted path stays bitwise unchanged.
  2. Low, test strength. All three new tests use fields exact under any normalisation that makes the row a partition of unity: a globally linear DG1 and a constant DG0. A version averaging only some of the cells at a node, or normalising by a different positive scale, would still pass. They are serial on a 66-cell mesh, so they never reach ghost nodes, include_ghosts=False, partition seams, a vector or tensor field, or a quad mesh. A per-cell-offset field compared against an independent global mean would make the test bite, plus one vector variable. It also passes under mpirun -n 2.
  3. Docs. The result is the unweighted arithmetic mean of the cell limits at a node. For a truly discontinuous field, such as a jump or DG0 material ids, it is not volume-weighted. At a vertex the weight is the cell count: 2 to 7 on triangles, up to 92 on our tetrahedral box, 32 on the shell. For globally linear data it does not matter. Volume weighting would need a different matrix, so we do not ask for it; please add a Notes line saying so to the project_to_degree docstring.

Attacks that failed

  • Row sums, read with getRowSum before scaling: every sum was exactly the number of cells meeting at the node, never zero, negative or small (minimum 1.0). Triangles 2 to 7 (DG1 to P1), 1 to 7 (to P2); quads 1 to 4; hexes 1 to 8; tetrahedra 4 to 92; shell 5 to 32 (to P1), 1 to 32 (to P2). Each cell's rows sum to 1, so negative interpolation weights (DG2 or DG3 to P1, DG1 to P2, DG2 to P2, DG3 to P3) do not change the denominator: the row sum is the right divisor. DG to DG0 sums are 1.0 to roundoff.
  • Exact projection on every rank, ghosts included: max error 1e-15 to 1e-14 in 2-D (tetrahedra and hexes reach 2.5e-14), on tri, quad, tet, hex, annulus, a shell with degree-2 coordinates and a graded annulus; DG1 to P1 and P2, DG2 to P1 and P2, DG3 to P1 and P3, DG1 and DG2 to DG0, DG1 to DG1; np 1, 2, 3, 4, 8; include_ghosts True and False, with owned row counts matching serial (np 2: 19+25=44; np 4: 7+11+11+15=44).
  • Seams: a per-cell offset field against an independent global mean over all cells: max error 2.2e-16 to 6.7e-16 on tri, quad, tet and annulus at np 1, 2, 3, 4, 8. Seam nodes see every cell.
  • Ranks with no cells: a 14-cell mesh at np 8 with two empty ranks: no hang, errors of 1e-15 elsewhere.
  • Vector, symmetric tensor and tensor fields: DG1 to P1, DG2 to DG2 and DG2 to P1 at np 1, 4, 8: errors 1e-15 to 1.2e-14. Block size needs no handling.
  • Mat reuse: petsc_dm_create_interpolation_within_plex builds a fresh Mat on each call and it is destroyed in the function, so double scaling is not possible.
  • Continuous source: the branch is skipped; P2 to P1 passes as before.
  • Other callers: project_to_vertices and write_vertices_to_viewer are reached only through this function, and the latter has no callers inside UW3. write_projected_field_to_viewer and the XDMF path use a DG0 target for discontinuous variables. Direct DG1 output does not call this. Apart from finding 1's roundoff, results do not move.
  • Failing on old code: on eddab70 the linear DG1 and constant DG0 tests fail with values 2 to 7 where 1 was expected, which is the project_to_degree from a discontinuous field to a continuous one sums the adjacent cells #905 summing. The continuous-source test passes on both. On the head, 3 passed at np 1 and 2.

Issues

#905: fixed by this PR for its stated reach, with finding 3's caveat. None else found in the area. A rank with no cells worked here, which depends on #902's interpolator; #902 itself was not checked.

Merge conditions

  • Guard the scaling with and continuous, or state that DG to DG is unchanged only to roundoff. We prefer the guard.
  • Add a per-cell-offset case and a vector case to test_0005.
  • Add the unweighted-mean Notes line.

Could not test

The new test is serial, so CI's reach on the parallel behaviour rests on probes run by hand under mpirun. No code mutant for finding 2. The full XDMF write_timestep, save and load, and visualisation pipelines: "no change on disk" rests on reading the callers and comparing project_to_degree outputs.

Underworld development team with AI support from Claude Code

… cases

From hyperion's review of #973:
- The row scaling applies only when the target is continuous. A discontinuous
  target's nodes each lie in one cell, and scaling moved a DG-to-DG
  projection by up to 2e-15, enough to change an XDMF dataset in the last bit.
- test_0005 adds a DG0 field with a different value in every cell, whose
  projection must be the unweighted mean of the cells at each node (a linear
  or constant field cannot tell a full average from a partial one), and a
  vector DG1 field. Both fail on development; all five pass here, with
  test_0005_xdmf_dg1.
- The docstring says the result at a shared node is the cells' unweighted mean.

Underworld development team with AI support from Claude Code
@lmoresi

lmoresi commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

Final word on #973 at b8399ce: ready

All three merge conditions from the review are met, and CI is green on this head. The fix itself is unchanged.

  • Guard: with and continuous, DG to DG and DG to DG0 no longer move. Output of 12 projections at np 2, triangles and quads, DG3 to DG0 and DG3, DG2 to DG2 and DG0, DG1 to DG1 and DG0: byte-identical to eddab70.
  • Tests: 5 passed here; 4 failed, 1 passed on eddab70, with the continuous-source case the one that passes. The per-cell case has the power it was added for: a mutant keeping only two cells at each DG0 node fails it, and also the constant case.
  • Docs: the unweighted-mean note is in the project_to_degree docstring.

One note, not a condition. The new per-cell test fails if run under mpirun -n 2, on its own line 85: it builds the expected mean from local cells, so a node on a seam misses the other rank's. CI runs this file serially, so that doesn't arise there, and the code is right at the seam: against a mean over all cells gathered across ranks the error is 3.3e-10 at np 2 and 4, the size of the 1e-10 rounding in that probe of mine, and the review's unrounded probe at 0168c2d gave 6.7e-16 or better. The parallel evidence is still these hand-run probes.

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