Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply Diriclet boundary on the Cofunction RHS. #3754

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions firedrake/variational_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
DEFAULT_SNES_PARAMETERS
)
from firedrake.function import Function
from firedrake.cofunction import Cofunction
from firedrake.functionspace import RestrictedFunctionSpace
from firedrake.ufl_expr import TrialFunction, TestFunction
from firedrake.bcs import DirichletBC, EquationBC
Expand Down Expand Up @@ -305,6 +306,10 @@ def solve(self, bounds=None):

for dbc in problem.dirichlet_bcs():
dbc.apply(problem.u_restrict)
for coeff in coefficients:
if isinstance(coeff, Cofunction):
# Apply the DirichletBC to the right hand side of the equation.
dbc.apply(coeff)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not look right. The residual form might contain many cofunctions in function spaces different from the one of the solution. This should be done before calling the solver, and only on the Cofunction RHS. Also it is likely that you need dbc.zero() instead of dbc.apply(). Also see the comment I left in #3662

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will have side effects on Cofunctions in the problem definition.

u = Function(space, name="u")
b = Cofunction(space.dual(), name="b")
...
problem = LinearVariationalProblem(
    inner(trial, test) * dx, b, u, DirichletBC(space, 1, "on_boundary"))
assert b in problem.F.coefficients()  # passes


if bounds is not None:
lower, upper = bounds
Expand Down
40 changes: 40 additions & 0 deletions tests/regression/test_cofunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,43 @@ def test_scalar_cofunction_zero_with_subset(V):
assert f is g
assert np.allclose(f.dat.data_ro[:2], 0.0)
assert np.allclose(f.dat.data_ro[2:], 1.0)


def test_diriclet_bc_rhs(V):
# Issue https://github.com/firedrakeproject/firedrake/issues/3498
# Apply DirichletBC to RHS (Cofunction) in LinearVariationalSolver
mesh = UnitIntervalMesh(2)
space = FunctionSpace(mesh, "Lagrange", 1)
test, trial = TestFunction(space), TrialFunction(space)

# Form RHS
u = Function(space, name="u")
problem = LinearVariationalProblem(
inner(trial, test) * dx, inner(Constant(1.0), test) * dx, u,
DirichletBC(space, 0.0, "on_boundary"))
solver = LinearVariationalSolver(problem)
solver.solve()

assert np.allclose(assemble(inner(u, u) * ds), 0.0)

# Cofunction RHS
b = assemble(inner(Constant(1.0), test) * dx)
u = Function(space, name="u")
problem = LinearVariationalProblem(
inner(trial, test) * dx, b, u,
DirichletBC(space, 0.0, "on_boundary"))
solver = LinearVariationalSolver(problem)
solver.solve()

assert np.allclose(assemble(inner(u, u) * ds), 0.0)

# FormSum RHS
b = assemble(inner(Constant(0.5), test) * dx) + inner(Constant(0.5), test) * dx
u = Function(space, name="u")
problem = LinearVariationalProblem(
inner(trial, test) * dx, b, u,
DirichletBC(space, 0.0, "on_boundary"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should have non-homogeneous bcs

solver = LinearVariationalSolver(problem)
solver.solve()

assert np.allclose(assemble(inner(u, u) * ds), 0.0)
Loading