Repository navigation
Conversation
51dda7d to
5631a68
Compare
| Ftrial = problem.compute_bc_lifting(self.J, trial, L=problem.L) | ||
| self.F = ufl.replace(Ftrial, {trial: self._x - self._bc_residual}) | ||
| else: | ||
| self.F -= problem.compute_bc_lifting(self.J, self._bc_residual) |
There was a problem hiding this comment.
With pre_apply_bcs=False, we are adding this extra term to the residual F (more code to compile and execute). However this can be prevented in the linear case, as I explain in the comment.
For the nonlinear case, there is a way to reduce execution time by assuming that bc_residual is only supported on cells that along the boundary. We could restrict the integrals in J onto that subset of cells.
There was a problem hiding this comment.
Note that pre_apply_bcs=True implies that self._bc_residual = 0 throughout the solve (if the Newton updates satisfy homogenous BCs by solving the bc identity matrix exactly), and this is why we did not have this term before.
There was a problem hiding this comment.
We could probably lazily assemble this individual term only when bc_residual is nonzero.
8e15f0b to
83292ee
Compare
| self.F = ufl.replace(self.F, {self._x: ufl.zero(self._x.ufl_shape)}) | ||
|
|
||
| self.F -= problem.compute_bc_lifting(self.J, self._bc_residual) | ||
| if problem.is_linear and hasattr(problem, "L"): |
There was a problem hiding this comment.
Ideally we would be testing isinstance(problem, LinearVariationProblem), but by doing this I am avoiding a cyclic import. I can have a go at fixing the cyclic import if that is preferred.
83292ee to
7f7a302
Compare
| if bcs and any(isinstance(bc, EquationBC) for bc in bcs): | ||
| restrict = False | ||
| self.restrict = restrict and bcs | ||
| self.restrict = restrict and bool(bcs) |
There was a problem hiding this comment.
This was setting self.restrict = bcs
There was a problem hiding this comment.
I am casting bool(bcs) because the default is bcs=None, and it's too early to extract_bcs(bcs), which parses bcs into an iterable
| problem.u_restrict.assign(problem.u) | ||
|
|
||
| if self._ctx.pre_apply_bcs: | ||
| if self.pre_apply_bcs or problem.restrict: |
There was a problem hiding this comment.
In the deprecation era, pre_apply_bcs=True only applies bcs at the beginning of the solve, and _SNESContext becomes independent of pre_apply_bcs, which means that the residual lifting term is always computed (it will be zero when the bcs are satisfied)
|
@pbrubeck What happened with this MR? I want to stress again that pre_apply_bcs=True is simply the wrong approach with inexact Newton, because you cannot expect approximate linear solves to exactly resolve the diagonal equations of the BC dofs |
1107748 to
28ab38f
Compare
DirichletBC.apply(r, u=u) computed u - g in UFL, which fails when a vector-valued space has a scalar value, for example bc.set_value(7). Copy u and subtract the scalar in place with the Assigner, which broadcasts it to every component. Keep the single assignment of u - g when the shapes match, so that no new kernels are compiled. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Change the default of pre_apply_bcs to False in solve and in NonlinearVariationalSolver, and deprecate pre_apply_bcs=True. On an unrestricted space the solver now always lifts the Dirichlet conditions around the current state u: it assembles F(u) - J(u) r, where r = u - g on the Dirichlet nodes and r = 0 elsewhere. This is F(u - r) to first order, and exactly F(u - r) for a linear problem, so the linear case uses the same code. pre_apply_bcs=True only applies the boundary conditions to the initial guess. On a restricted space PETSc cannot update the excluded nodes, so the solver always applies the boundary conditions to the initial guess and adds no lifting term. The DirichletBCs inside an EquationBC follow the same model: the solver linearises around the supplied initial guess for every Dirichlet condition. The multigrid coarsening and AuxiliaryOperatorSNES no longer apply the boundary conditions themselves. assemble(a, tensor=A, bcs=bcs) now sets A.bcs to bcs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The H1 error is 1.9e-12 at the tolerance of the Krylov solver, which is above the earlier limit of 1e-12. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
28ab38f to
efb5e04
Compare
AI assistance: Claude Code (Claude Opus 5.5) helped to write the changes for
EquationBCand this description.Summary
Fixes #5177
Stacked on #5766, which eliminates the same Dirichlet columns from the domain rows and from each EquationBC row, and lifts each EquationBC residual with its own Jacobian.
This PR changes the default model for DirichletBC to
pre_apply_bcs=False, deprecatingpre_apply_bcs=True.In the deprecation era,
pre_apply_bcs=Trueonly applies bcs at the beginning of the solve, and_SNESContextbecomes independent ofpre_apply_bcs, which means that the residual lifting term is always computed (it will be zero when the bcs are satisfied).We also make sure that bcs are always pre applied if
restrict=True, and for this case we never compute the residual lifting term.On an unrestricted space, the solver lifts the Dirichlet conditions around the current state$u$ :
This is$F(u - r)$ to first order, and $u - r$ takes the boundary values $g$ . For a linear problem it is exactly $F(u - r)$ , so the linear case uses the same code.
The same model now also applies to the Dirichlet conditions inside an
EquationBC:Changes for users
pre_apply_bcsinsolveandNonlinearVariationalSolverTrueFalsepre_apply_bcs=TrueDeprecationWarningDirichletBCinside anEquationBCDirichletBCvalue on a vector-valued spaceImplementation details
_SNESContextno longer takespre_apply_bcs. It adds the lifting term whenproblem.restrictisFalse, and it zeroes the boundary rows of the residual whenproblem.restrictisTrue. The solver applies the Dirichlet conditions to the initial guess only forpre_apply_bcs=Trueor on a restricted space, because PETSc cannot update excluded degrees of freedom.firedrake/mg/ufl_utils.py) andAuxiliaryOperatorSNESno longer apply the boundary conditions themselves.DirichletBC.apply(r, u=u)subtracts a scalar value with theAssigner, which broadcasts it to every component.Limitations
Evidence
test_nested_dirichletbc_linearises_at_initial_guesssolves one Newton step (snes_type: ksponly) fromAt$u = 0$ the
u**2terms do not change the linearised equations, so the exact solution of the linearised problem is the constant 1.errornorm(Constant(1), u) = 0.0528for bothaijandmatfree, so the test fails.After:
errornorm(Constant(1), u) < 1e-12for bothaijandmatfree, on 1 and 3 processes.tests/firedrake/equation_bcs/test_equation_bcs.py: 22 passed, 2 skipped on 1 process, and 5 passed on 3 processes.Merge Danger
Door: two-way
pre_apply_bcs=Trueis deprecated, not removed, so users can get the old behaviour back for one solve.Blast Radius: solvers
Every nonlinear solve with a
DirichletBCon an unrestricted space now evaluates the lifted residual. Iteration counts and the first residual norm can change, most for initial guesses that do not satisfy the boundary conditions.🤖 Drafted with Claude Code