Add Dirichlet boundary condition for vector field

Dear all,
Following this fenics example, I want to impose a Dirichlet boundary condition (BC) on a vector valued function.

I defined

class BoundarySource(UserExpression): # UserExpression instead of Expression
    def __init__(self, markers, **kwargs):
        super().__init__(**kwargs) # This part is new!
        self.markers = markers
    def eval_cell(self, values, x, cell):
        values[0] = 1
        values[1] = 2
    def value_shape(self):
        return (2,)

and then

g_D = BoundarySource(mesh)
bc_v = DirichletBC([...], g_D, boundary)

Proceeding along the lines of the example I would like to set \bf g equal to a vector proportional to the normal \bf n to the boundary of the mesh, defined as

n = FacetNormal(mesh).

For example, I want to set {\bf g} = \sin(x) * \bf n. How should I modify BoundarySource to achieve this? If I just copy and paste the BoundarySource definition from the example, the code fails

$ python3 example.py 
Traceback (most recent call last):
  File "example.py", line 126, in <module>
    g_D = BoundarySource(mesh)
  File "example.py", line 96, in __init__
    self.mesh = mesh
  File "/usr/local/lib/python3.6/dist-packages/dolfin/function/expression.py", line 438, in __setattr__
    elif name in self._parameters:
  File "/usr/local/lib/python3.6/dist-packages/dolfin/function/expression.py", line 432, in __getattr__
    return self._parameters[name]
  File "/usr/local/lib/python3.6/dist-packages/dolfin/function/expression.py", line 432, in __getattr__
    return self._parameters[name]
  File "/usr/local/lib/python3.6/dist-packages/dolfin/function/expression.py", line 432, in __getattr__
    return self._parameters[name]
  [Previous line repeated 327 more times]
RecursionError: maximum recursion depth exceeded

Thank you

Please note that you have not provided a minimal example.
The code that you have provided above does not produce your error message, as the following code returns no error:

from dolfin import *


class BoundarySource(UserExpression): # UserExpression instead of Expression
    def __init__(self, markers, **kwargs):
        super().__init__(**kwargs) # This part is new!
        self.markers = markers
    def eval_cell(self, values, x, cell):
        values[0] = 1
        values[1] = 2
    def value_shape(self):
        return (2,)



mesh = UnitSquareMesh(10, 10)
ft = MeshFunction("size_t", mesh, mesh.topology().dim()-1, 0)
bc = BoundarySource(markers=ft, degree=1)

Please provide a reproducible example, such that people can understand what modifications are implied by

Sure,
Here is a minimal example:

from __future__ import print_function
from fenics import *
import matplotlib.pyplot as plt
import argparse
import numpy as np
from mshr import *
import matplotlib.pyplot as plt
import meshio
import ufl as ufl


# Create mesh
cylinder = Circle(Point(0.0, 0.0), 1.0)
domain = cylinder
mesh = generate_mesh(domain, 64)

boundary = 'on_boundary'

n = FacetNormal(mesh)

# Define function spaces
P_U = FiniteElement('P', triangle, 2)
P_V = VectorElement('P', triangle, 2)
element = MixedElement([P_U, P_V])
UV = FunctionSpace(mesh, element)
U = UV.sub(0).collapse()
V = UV.sub(1).collapse()


class g_expression(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,)
    
g_D = g_expression(mesh)
bc_v = DirichletBC(UV.sub(1), g_D, boundary)

when I run it I get

$ python3 example.py 
Traceback (most recent call last):
  File "example.py", line 42, in <module>
    g_D = g_expression(mesh)
  File "example.py", line 32, in __init__
    self.mesh = mesh
  File "/usr/local/lib/python3.6/dist-packages/dolfin/function/expression.py", line 438, in __setattr__
    elif name in self._parameters:
  File "/usr/local/lib/python3.6/dist-packages/dolfin/function/expression.py", line 432, in __getattr__
    return self._parameters[name]
  File "/usr/local/lib/python3.6/dist-packages/dolfin/function/expression.py", line 432, in __getattr__
    return self._parameters[name]
  File "/usr/local/lib/python3.6/dist-packages/dolfin/function/expression.py", line 432, in __getattr__
    return self._parameters[name]
  [Previous line repeated 327 more times]
RecursionError: maximum recursion depth exceeded

Note that I tried to include this minimal working example in the original post, but I could not modify it.

Please note that

  1. You do not need to use mshr to make a minimal reproducible example.
  2. That you haven’t used any of the adaptations from the previous post, i.e. using UserExpression or super().__init__(**kwargs).

A working minimal example is as follows:

from fenics import *


# Create mesh
mesh = UnitSquareMesh(10, 10)
boundary = 'on_boundary'

n = FacetNormal(mesh)

# Define function spaces
P_U = FiniteElement('P', triangle, 2)
P_V = VectorElement('P', triangle, 2)
element = MixedElement([P_U, P_V])
UV = FunctionSpace(mesh, element)
U = UV.sub(0).collapse()
V = UV.sub(1).collapse()


class g_expression(UserExpression):
       def __init__(self, mesh, **kwargs):
           super().__init__(**kwargs)
           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,)
    
g_D = g_expression(mesh)
bc_v = DirichletBC(UV.sub(1), g_D, boundary)