Skip to content

Support string subdomain_id in ufl.Measure, DirichletBC and Submesh - #5199

Open
pbrubeck wants to merge 16 commits into
mainfrom
pbrubeck/feature/region-names
Open

pbrubeck wants to merge 16 commits into
mainfrom
pbrubeck/feature/region-names

Conversation

@pbrubeck

@pbrubeck pbrubeck commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Users can now refer to a subdomain by its name instead of its integer subdomain id. The names come from the mesh file, or from mesh.rename_subdomain.

from firedrake import *
import netgen.occ as ngocc

# 1. Generate geometry & name boundaries natively in Netgen
wp = ngocc.WorkPlane()
box = wp.Rectangle(1, 1).Face()
box.edges.Max(ngocc.Y).name = "inlet"
box.faces.name = "fluid_domain"
ngmesh = ngocc.OCCGeometry(box, dim=2).GenerateMesh(maxh=0.1)

# 2. Native Firedrake Mesh captures the names automatically
mesh = Mesh(ngmesh)

# 3. Explicitly map a single string name to multiple disjoint entity IDs

# 4. Clean, human-readable API usage
V = FunctionSpace(mesh, "CG", 1)

# DirichletBC automatically maps "inlet" to the correct face ID
bc = DirichletBC(V, 0.0, "inlet")  

# SubMesh automatically maps "fluid_domain" using the correct keyword argument
submesh = Submesh(mesh, subdomain_id="fluid_domain")

# Form language integration: UFL Measures accept string region names directly
f = Function(V)
L = f * dx(subdomain_id="fluid_domain")

The mesh readers put each name on the DMPlex as a label, and Firedrake stores the names on the mesh topology. Firedrake changes names into subdomain ids where each API reads them:

Mesh(meshfile)
  read the DMPlex
    Gmsh, Netgen    one label for each name (PETSc use_regions, ngsPETSc)
    Exodus          one label for each side set name
    CGNS            one label for each facet group (-dm_plex_cgns_parallel)
  topology.subdomain_names[dim][name] = [subdomain ids]

mesh.rename_subdomain(dim, subdomain_id, name)    # add a name by hand

mesh.parse_subdomain_id(dim, subdomain_id)        # change names into subdomain ids
  called by DirichletBC(V, g, "inlet")
            RestrictedFunctionSpace(V, boundary_set=["inlet"])
            Submesh(mesh, subdomain_id="fluid_domain")
            compile_form, for ds("inlet") and dx("fluid_domain")
  • on_boundary, everywhere, otherwise, top and bottom keep their meaning.
  • A name that is not on the mesh raises a ValueError that lists the available names.
  • A name can map to several subdomain ids. In a nested tuple, which is an intersection of subdomain ids, the name stays one entry.
  • compile_form changes names into subdomain ids before the cache lookup. So the cache key holds the subdomain ids that the names map to when the form is compiled.
  • The Netgen and Boussinesq demos now use names instead of GetRegionNames.

Implementation details

  • Gmsh files are now read with -dm_plex_gmsh_use_regions -dm_plex_gmsh_use_generic. The generic labels (Cell Sets, Face Sets and so on) supply the subdomain ids as before. The name labels are removed before distribution.
  • The CGNS parallel reader gives the subdomain ids of facet groups. Firedrake gives each named group an unused positive subdomain id, in alphabetical order.
  • Netgen names come from the labels that Add a label for each named Netgen region to the DMPlex NGSolve/ngsPETSc#134 adds. The same PR adds a Cell Sets label when all cells are in one named region. Without it, dx("fluid_domain") in the code block above resolves to dx(1) and integrates over nothing.

Limitations

  • CheckpointFile does not keep the names, because they are not on the DMPlex after distribution.
  • Netgen vertex names are not imported, because ngsPETSc puts no labels on vertices.
  • A Gmsh element in several physical groups is only in the subdomain id of its first group. So the name of a later group does not include that element.

Evidence

The tests compare assembled values or subdomain ids with values computed in a different way:

  • Before: each test fails, because the API does not accept a string or the mesh has no names.
    After: each test passes.
    • test_dirichletbc_accepts_subdomain_name
    • test_restricted_function_space_accepts_subdomain_name
    • test_submesh_accepts_subdomain_name
    • test_measure_accepts_subdomain_name_with_domain, ..._with_inferred_domain
    • test_gmsh_subdomain_names_are_loaded, test_cgns_subdomain_names, test_netgen_subdomain_names_are_loaded, test_exodus_mesh

test_netgen_subdomain_names_are_loaded fails with ngsPETSc main and passes with the ngsPETSc branch.

Merge Danger

Door: two-way

Integer subdomain ids work as before. Names are new API. They can stay, or they can be removed later.

Blast Radius: mesh-loading

  • Gmsh: the generic labels, and so the integer subdomain ids, do not change. PETSc inserts only the first tag of an element in them.
  • Gmsh: a physical name that is also the name of a PETSc label (depth, celltype, marker, Cell Sets, ...) is not imported.
  • The commit DROP BEFORE MERGE: Install the ngsPETSc branch with region labels installs the ngsPETSc branch in CI. Drop it after Add a label for each named Netgen region to the DMPlex NGSolve/ngsPETSc#134 merges, and run CI again.

Disclaimer: This PR was implemented with Codex and Claude Code (Claude Opus 5.5).

🤖 Generated with Claude Code

pbrubeck and others added 4 commits November 26, 2026 23:31
…on-names

# Conflicts:
#	demos/netgen/netgen_mesh.py.rst
#	firedrake/bcs.py
#	firedrake/mesh.py
#	firedrake/tsfc_interface.py
FEniCS/ufl#493 has been merged, so CI's UFL main already supports
string subdomain ids.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Resolve region names once in compile_form, before the cached
  _compile_form, instead of in both the hashkey and the body.
- Use UFL's integral type tables to find the entity dimension.
- Share one entity dimension check between rename_subdomain and
  parse_subdomain_id, and flatten the parse_subdomain_id helpers.
- Read Gmsh physical names on rank 0 only, stop at $Entities/$Nodes
  when there is no $PhysicalNames block, and gather names on the
  internal communicator.
- Declare region_names on ExtrudedMeshTopology, which does not call
  AbstractMeshTopology.__init__.
- Remove a leftover print(labels) from the Netgen demo.
- Move the tests from test_region_names.py (deleted in the previous
  commit) into the existing test files for each feature.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pbrubeck pbrubeck added the LLM used An LLM was used in the production of this PR label Nov 27, 2026
Share one helper between the Gmsh and Exodus readers, give every rank
the full CGNS name list, store region names in a plain dict, and read
Netgen names next to the other readers. Wrap a string subdomain before
the names are resolved in DirichletBC and RestrictedFunctionSpace, so
that a name given to an already restricted space is also resolved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pbrubeck
pbrubeck marked this pull request as ready for review November 27, 2026 01:52
Comment thread firedrake/tsfc_interface.py Resolved
pbrubeck and others added 5 commits November 27, 2026 17:22
Use the terms of the rest of the codebase: subdomain names and
subdomain ids, not region names, tags or markers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ngsPETSc now marks each named Netgen region in a DMPlex label with that
name, like the PETSc Gmsh reader does. Read these labels in the same way
for Gmsh and Netgen meshes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drop this commit once NGSolve/ngsPETSc#134 merges.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Convert point coordinates to a tuple before passing them to DirichletBC, and demonstrate vector-valued Constants in both demos.

Assisted-by: OpenAI Codex

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

LLM used An LLM was used in the production of this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant