Skip to content

Add computation of cell-orientation as stand-alone function - #4668

Open
jorgensd wants to merge 44 commits into
mainfrom
dokken/cell-normal-orientation
Open

jorgensd wants to merge 44 commits into
mainfrom
dokken/cell-normal-orientation

Conversation

@jorgensd

@jorgensd jorgensd commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

After discussions with @jhale.
Required for a consistently oriented CellNormal.
This adds a demo illustrating where an oriented CellNormal is useful.

Summary

Adds dolfinx.mesh.compute_cell_orientations(topology) (C++:
mesh::compute_cell_orientations). It returns, for each owned and ghost
cell of an orientable surface, the sign (1 or -1) that makes the normals
of all cells agree. Nothing is stored on the topology. Multiplying
ufl.CellNormal by the signs, through a DG0 function, gives a consistent
normal, as needed for the surface curl dot(n, curl(w)) in vorticity
formulations. The new demo demo_manifold_vorticity.py solves mixed Poisson
on the sphere with a rotated Nédélec flux and reproduces the Raviart-Thomas
solution to rounding.

#4574 computed the same orientation, stored it on the topology, and used it
for RT spaces. #4625 replaced that use with facet signs and removed the
function. This PR brings it back as a free function, with a new parallel
algorithm.

Algorithm compared with #4574

The result is the same, and so is the convention: each surface, i.e. each
set of cells connected through shared edges, is oriented by its cell with the
lowest global index, which gets 1. On every mesh benchmarked below, both
versions return identical arrays. The difference is in how the processes are
joined.

#4574 This PR
Result stored on the topology returned, not stored
Connectivity needs edge-to-cell, which stays on the topology builds a temporary list of each edge's owned cells from cell-to-edge
Local step walk over owned cells, with conflict checks and an Allreduce the same walk, without checks; all checks happen in one place
Finding the cell across a process boundary each (owned cell, shared edge) pair is sent to every process sharing the edge, over a new graph communicator built with compute_sharing_neighbourhood one reduction over the edge Scatterer (reverse, then forward) of [max code, -min code, count], with code = 2 * label + direction
Joining the parts of each process rounds of label propagation, each a Neighbor_alltoall + Neighbor_alltoallv + Allreduce; the number of rounds grows with the length of the chain of parts each process reduces its links to a spanning forest with a union-find that tracks relative orientation; the forests are gathered on process 0, which solves them and returns [status, part signs] with a Scatterv
Collectives 2 Allreduce + 3 per round + the final check a fixed number: 2 edge scatters, Gather, 2 Gatherv, Scatterv, and the ghost scatter

The reduction gives the owner of each edge both of its cells, their parts and
their directions:

  • more than two cells: not a manifold;
  • both cells in one part and running the edge the same way: not orientable;
  • two different parts: a link (a, b, r), meaning that part b has r times
    the orientation of part a.

A link that contradicts the others in the union-find also means the surface
is not orientable. The error status travels in the Scatterv reply, so every
process raises together in all build types. Process 0 receives at most one
link per part, typically a few per process, rather than one per edge between
processes.

reduce_edge_codes, runs_up, edge_row and fold_codes are now shared
with compute_facet_orientations (#4625), which used the same reduction.

Benchmark

Each time is the minimum of 7 calls, taking the slowest process for each
call. The #4574 times exclude creating its edge-to-cell connectivity, a
one-off cost of about 0.4 ms in serial on the strip; this PR builds its list
in every call.

Mesh Processes #4574 (ms) This PR (ms)
Cube surface (submesh), 43k cells 1 / 2 / 4 / 8 0.77 / 0.44 / 0.29 / 0.38 0.92 / 0.50 / 0.34 / 0.29
Strip, 128k cells 1 / 2 / 4 / 8 1.94 / 1.01 / 0.57 / 0.52 2.74 / 1.61 / 0.75 / 0.51
Strip in 256 blocks dealt round-robin 1 / 2 / 4 / 8 1.92 / 4.72 / 3.21 / 4.07 2.41 / 1.65 / 0.86 / 0.52

On ordinary partitions #4574 is slightly faster, because it reuses the stored
connectivity. When the partition splits each process's cells into many parts
(the round-robin strip, or a surface submesh of a 3D partition), its rounds
add up. This PR's time stays flat, and it is 3–8 times faster.

Tests

  • The orientation agrees with the outward normal up to one global sign, on
    triangle and quadrilateral surfaces, with and without ghost cells. Ghosts
    are made by ghosting every cell on each process sharing one of its
    vertices, since a facet submesh has none.
  • Global cell 0 gets 1.
  • Cells with mixed vertex orders are flipped back.
  • Two cells meeting only at a vertex are oriented independently.
  • The recipe with CellNormal points outward.
  • A Möbius strip and T-joints are refused on every process.
  • The rotated N1curl solution matches RT, but not without the orientation
    (needs petsc4py, skipped otherwise).

AI disclosure statement

This PR has been created with the assistance of Claude (Opus 5.5). I have
revised the code and take responsibility for the contribution.

jorgensd and others added 23 commits October 19, 2026 09:52
…multiplier conserving fluxes.

Use all garth's demo.
compute_facet_orientations no longer needs GhostMode::shared_facet. Each
process reduces, per edge, the largest and smallest code
2 * (global cell index) + runs_up over its owned cells, and their count.
One reverse and one forward scatter over the edge index map give every
process holding an edge both of its cells, so owned and ghost cells are
signed locally. Errors (an edge with more than two cells) are raised
after the scatters, so no process is left waiting in them.

- Topology::facet_orientations checks for a surface
- FiniteElement::depends_on_cell_orientation -> depends_on_facet_orientations
- FiniteElement::permute/permute_inv use the cached orientation mask
- demo_manifold drops the vertex-ghosting partitioner; the eikonal and
  branching demos drop shared_facet; demo_mobius prints its -2 check
- Tests: independent recomputation of the signs and of ghost consistency,
  unghosted surfaces and T-joints, shape checks, signed-facet assertions

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…fy masks

- Document that the non-manifold edge error is raised on every rank only
  in Developer and Debug builds.
- Keep the full explanation once, in Topology::create_facet_orientations,
  and reference it elsewhere.
- Fix stale cell-orientation wording in FunctionSpace and the bit-range
  comment in compute_cell_permutations.
- Check the topological dimension once, in Topology; assert it in
  compute_facet_orientations.
- Drop the derived _applied_orientation_mask from FiniteElement.
- Use std::ranges::copy_n.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jorgensd
jorgensd requested a review from jhale October 19, 2026 14:46
Comment thread python/demo/demo_manifold_vorticity.py Resolved
Comment thread python/demo/demo_manifold_vorticity.py Resolved
Comment thread python/demo/demo_manifold_vorticity.py Resolved
Comment thread python/demo/demo_manifold_vorticity.py Resolved
Comment thread python/demo/demo_manifold_vorticity.py Resolved
Comment thread python/demo/demo_manifold_vorticity.py Resolved
Comment thread python/demo/demo_manifold_vorticity.py Resolved
Comment thread python/demo/demo_manifold_vorticity.py Resolved
Comment thread python/demo/demo_manifold_vorticity.py Resolved
Comment thread python/demo/demo_manifold_vorticity.py Resolved
…a's proper description of encoding and decoding
@jorgensd

Copy link
Copy Markdown
Member Author

Status update of PR

  • C++: mesh::compute_cell_orientations(const Topology&) returns 1 or -1 for each owned and ghost cell, making the normals of an orientable surface agree.
    • Nothing is stored on the topology.
    • It needs only the cell-to-edge connectivity and the edge permutations; edge-to-cell is built internally and discarded.
  • Shared with compute_facet_orientations: the helpers runs_up, edge_row, fold_codes and reduce_edge_codes.

Algorithm

  1. Local parts. A walk over the owned cells, using a temporary edge-to-owned-cell list, splits them into parts connected through edges with two owned cells. sign[c] is a cell's orientation relative to its part's root cell.
  2. Where parts meet.
    • Parts get global numbers from an IndexMap(comm, num_parts).
    • Each cell encodes, per edge, (2·part + (t > 0))·size + rank, where t is the direction it runs the edge once oriented.
    • reduce_edge_codes (a reverse and a forward scatter over the edge map) gives each edge its largest code, its smallest code and its number of cells.
    • Each owned cell decodes its edges into an adjacency list of neighbouring parts: global number, relative sign r = ±1, and owning process.
    • Errors recorded locally:
      • more than 2 cells on an edge → not a manifold;
      • equal t within one part → not orientable;
      • a pair of parts met with both signs → not orientable.
  3. Joining across processes.
    • An IndexMap over the parts, with neighbouring parts as ghosts, and a Scatterer.
    • Each round: scatter_fwd of (representative, orientation) rows; each part adopts the lowest representative among its neighbours'; one Allreduce carries the changed and error bits.
    • The loop is bounded by num_global_parts + 1 rounds, followed by assert(converged).
    • Links are checked in the round with no changes.
  4. Ghost cells get their owner's value through the cell Scatterer.

Properties:

  • No gather to one process.
  • No communicator built per round.
  • Every process raises errors together in all build types.
  • Rounds = longest chain of parts.

Benchmark (ms, 8 processes, minimum of 7 calls)

cube surface, 43k cells strip, 128k cells strip in 256 blocks, round-robin
#4574 0.38 0.52 4.07
this PR 0.47 0.80 2.72

This PR is faster on fragmented partitions. On ordinary ones it is slower, mainly because it builds the temporary edge-to-owned-cell list on every call; #4574 stored the connectivity.

Decisions made in review

  • Scatterer rather than la::Vector, so that mesh does not depend on la; noted in comments.

@jorgensd
jorgensd requested a review from jhale October 22, 2026 15:09
@jhale jhale added this to the 0.12.0 milestone Dec 1, 2026

This branch has not been deployed

No deployments
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