Skip to content

Support restricted spaces with EquationBCs - #5767

Open
pbrubeck wants to merge 4 commits into
pbrubeck/default-pre-apply-bcsfrom
pbrubeck/restricted-equationbc
Open

pbrubeck wants to merge 4 commits into
pbrubeck/default-pre-apply-bcsfrom
pbrubeck/restricted-equationbc

Conversation

@pbrubeck

@pbrubeck pbrubeck commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

AI assistance: Claude Code (Claude Opus 5.5) helped to write these changes and this description.

Summary

Stacked on #5185, which is stacked on #5766.

NonlinearVariationalProblem turned restrict=True off without a warning when the boundary conditions contained an EquationBC. Now it restricts the spaces to exclude the nodes of every DirichletBC, including the DirichletBCs inside an EquationBC:

 NonlinearVariationalProblem(F, u, bcs=[EquationBC(G == 0, u, 1, bcs=[DirichletBC(V, g, ((1, 3), (1, 4)))])], restrict=True)
-  restrict = False
+  V_res = RestrictedFunctionSpace(V, boundary_set=[(1, 3), (1, 4)])
+  rebuild F, J and each boundary condition on V_res

A DirichletBC inside an EquationBC is usually on a corner or an edge, which is the intersection of two facet markers. So RestrictedFunctionSpace now accepts intersections:

boundary_set=[(1, 3)]   the common edge or corner of the facets marked 1 and 3
boundary_set=[1, 3]     both sets of facets

Changes for users

Change Before After
restrict=True with an EquationBC ignored restricts the spaces
RestrictedFunctionSpace(V, boundary_set=[(1, 3)]) ValueError excludes the nodes on the intersection
NonlinearVariationalProblem.rediscretise with an EquationBC AttributeError on bc.lhs rebuilds the EquationBC

Implementation details

  • DirichletBC, EquationBCSplit and EquationBC have a rediscretise method with the arguments of NonlinearVariationalProblem.rediscretise. Each class rebuilds itself with its constructor. This replaces the local function in NonlinearVariationalProblem.rediscretise, which read bc.lhs (which does not exist), dropped nested EquationBCs, and failed for a DirichletBC with a tuple of numbers as its value.
  • The multigrid reconstruction rebuilds an EquationBC from its F, J and Jp parts, because an EquationBC built from these parts has no equation attribute.
  • The adjoint solves drop the restrict option of the forward solve, because they use an assembled operator.

Evidence

  • test_restricted_solve_corner_bc solves the Laplace equation with u = 1 on the corner (1, 3) and restrict=True. The exact solution is the constant 1.
    • Before: RestrictedFunctionSpace raises ValueError for [(1, 3)].
    • After: errornorm(Constant(1), u) < 1e-12 on 1 and 3 processes.
  • test_EquationBC_poisson_matrix and test_equationbc_with_dirichletbc now run with restrict=True as well. The restricted cases converge at the rate of the unrestricted ones. Before this change, restrict=True was ignored, so these cases did not test a restricted space.
  • test_restricted_function_space.py passes on 1, 2 and 3 processes, test_equation_bcs.py on 1 and 3 processes, and adjoint/test_solving.py and adjoint/test_reduced_functional.py on 1 process.

Merge Danger

Door: two-way

Blast Radius: restricted solves

Solves with restrict=True and an EquationBC now use a restricted space, so their iteration counts can change.

🤖 Generated with Claude Code

@pbrubeck pbrubeck added base:main Run this PR using a main (dev) build LLM used An LLM was used in the production of this PR labels Dec 3, 2026
@pbrubeck
pbrubeck added this pull request to stack #5768 December 3, 2026 10:11
@pbrubeck
pbrubeck force-pushed the pbrubeck/restricted-equationbc branch from f14a5ce to cd068c4 Compare December 3, 2026 10:47
@pbrubeck
pbrubeck requested a review from leo-collins December 3, 2026 10:49
pbrubeck and others added 4 commits December 3, 2026 10:57
NonlinearVariationalProblem.rediscretise rebuilt each boundary condition
in a local function. For an EquationBC it read bc.lhs, which does not
exist, and it dropped any EquationBC nested inside another. For a
DirichletBC it called as_ufl on the value, which fails for a tuple of
numbers.

Add rediscretise to DirichletBC, EquationBCSplit and EquationBC, with the
same coefficient_mapping, form_transform and homogenize_bcs arguments as
the problem. Each class rebuilds itself with its constructor, so
reconstruct keeps only its own arguments. The multigrid reconstruction
also rebuilds an EquationBC from its F, J and Jp parts, because an
EquationBC built from these parts has no equation attribute.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Remove the docstring of EquationBC.rediscretise, which repeated
BCBase.rediscretise, and reduce the docstring of the multigrid
reconstruction of an EquationBCSplit to one line, as for the other
reconstruction functions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
NonlinearVariationalProblem turned off restrict=True when the boundary
conditions contained an EquationBC. Now it restricts the spaces to
exclude the nodes of every DirichletBC, also those inside an
EquationBC, and rebuilds each boundary condition on the restricted
space with rediscretise.

The adjoint solves drop the restrict option of the forward solve,
because they use an assembled operator.

Parametrize the tests over restrict, not pre_apply_bcs, and give the
EquationBC test with a DirichletBC a solution that is not constant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pbrubeck
pbrubeck force-pushed the pbrubeck/restricted-equationbc branch from cd068c4 to d9e2943 Compare December 3, 2026 11:05

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

base:main Run this PR using a main (dev) build enhancement 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