Defining BC in Mixed formulation for Poisson equation

Hi,
I’m trying to adapt this tutorial from fenics to dolfinx, but I’m struggling in the definition of the Dirichlet BCs.
Please, find below the code:

from dolfinx import mesh, fem
import ufl
from mpi4py import MPI
import numpy as np
from petsc4py import PETSc

domain = mesh.create_unit_square(MPI.COMM_WORLD, 8, 8, mesh.CellType.triangle)

# Mixed function space
BDMe = ufl.FiniteElement("BDM", domain.ufl_cell(), 1)
DGe = ufl.FiniteElement("DG", domain.ufl_cell(), 1)

BDM = fem.FunctionSpace(domain, BDMe)
DG = fem.FunctionSpace(domain, DGe)
W = fem.FunctionSpace(domain, ufl.MixedElement([BDMe, DGe]))

# Define test and trial functions
(sigma, u) = ufl.TrialFunctions(W)
(tau, v) = ufl.TestFunctions(W)

# define source term
x = ufl.SpatialCoordinate(domain)
f =  10 * ufl.exp(-((x[0] - 0.5)**2 + (x[1] - 0.5)**2) / 0.02)

# variational form
a = (ufl.dot(sigma, tau) + ufl.div(tau)*u + ufl.div(sigma)*v)*ufl.dx
L = - f*v*ufl.dx

# Define boundary space
class BoundarySource(fem.Expression):
    def __init__(self, mesh):
        self.mesh = mesh
    def eval_cell(self, values, x, ufc_cell):
        cell = ufl.Cell(self.mesh, ufc_cell.index)
        n = cell.normal(ufc_cell.local_facet)
        g = np.sin(5*x[0])
        values[0] = g*n[0]
        values[1] = g*n[1]
    def value_shape(self):
        return (2,)

G = BoundarySource(domain)

side_facets = mesh.locate_entities_boundary(domain, 1, lambda x: (np.isclose(x[1], 0.0) | np.isclose(x[1], 1.0)))
bdofs = fem.locate_dofs_topological(BDM, 1, side_facets)

bc = fem.dirichletbc(G, bdofs)

Does anybody know what I am doing wrong?
I guess that the problem is the definition of the BoundarySource class, but I don’t know how to modify it correctly.

Thanks in advance!

Have you had a look at: Unable to apply dirichlet BC in mixed formulation - #2 by dokken
and the subsequent example?

1 Like