Mixed Formulation with Neumann Essential Boundary Condition

I’m trying to reproduce the code for Mixed Formulation from the link:

https://fenicsproject.org/olddocs/dolfin/1.6.0/python/demo/documented/mixed-poisson/python/documentation.html

But since it is from an older version, it does not work anymore. Specifically, the class

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

Returns some a Recursion error, which I corrected by using the following class:

class BoundarySource(UserExpression):
    def __init__(self, mesh):
        self._mesh = mesh
        self._ufl_shape = (2,)
    def eval_cell(self, values, x, ufc_cell):
        cell = Cell(self.mesh, ufc_cell.index)
        n = cell.normal(ufc_cell.local_facet)
        g = sin(5*x[0])
        values[0] = g*n[0]
        values[1] = g*n[1]

But when I take G as an instance of the class:

G = BoundarySource(mesh)

And apply it to the Dirichlet Boundary Condition:

# Define essential boundary
def boundary(x):
    return x[1] < DOLFIN_EPS or x[1] > 1.0 - DOLFIN_EPS

bc = DirichletBC(W.sub(0), G, boundary)

It returns the strangest error:

AttributeError: _hash

Can someone please help me?

You didn’t state what version of Fenics you are using. The documentation you refered to is for a much older version of Fenics. If you are using the most recent version of Fenics( 2019.10 ). You should refer to the corresponding documentation.

https://fenicsproject.org/olddocs/dolfin/2019.1.0/python/demos/mixed-poisson/demo_mixed-poisson.py.html

… where you will see the definition of the BoundarySource class has changed.

# Define function G such that G \cdot n = g
class BoundarySource(UserExpression):
    def __init__(self, mesh, **kwargs):
        self.mesh = mesh
        super().__init__(**kwargs)
    def eval_cell(self, values, x, ufc_cell):
        cell = Cell(self.mesh, ufc_cell.index)
        n = cell.normal(ufc_cell.local_facet)
        g = sin(5*x[0])
        values[0] = g*n[0]
        values[1] = g*n[1]
    def value_shape(self):
        return (2,)

G = BoundarySource(mesh, degree=2)

2 Likes

Yes, I’m using version 2019.1.0. Thanks a lot! A didn’t know that they had the same examples for different versions. That sure would have spared me some time! rs. Thanks again.