Initial condition based on subdomain label (GMSH / dolfin-convert)

27 days ago by
I had the following code working in 2017.2.0 to define my initial conditions for different subdomains on a mesh containing regions labelled 1 and 2 by GMSH, then converted using dolfin-convert.
class InitialConditions(Expression):
    def __init__(self, subdomains, c_in, c_out, **kwargs):
        self.subdomains = subdomains
        self.c_in = c_in
        self.c_out = c_out

    def eval_cell(self, values, x, cell):
        if self.subdomains[cell.index] == 1: # inside circle
            values[0] = self.c_in
        elif self.subdomains[cell.index] == 2: # outside circle
            values[0] = self.c_out​
In the 2018.1.0 version, I get the error "RecursionError: maximum recursion depth exceeded while calling a Python object" when I try to run the following code:
interpolate(InitialConditions(subdomains, 0.1, 0.2, degree=0), V)​

How would I modify the class above to correctly apply my initial conditions?

Community: FEniCS Project

3 Answers

25 days ago by
I'm not sure why this is an error in 2018.1, but it can be related to UserExpression and the transition from SWIG to pybind11. Also, shouldn't it be cell.index()?

Something like this should work and be just as fast, but the transition between the subdomains may change a bit based on what type of function space V is, since some dofs belong to several cells in continuous function spaces.
dm = V.dofmap()
arr = f.vector().get_local()

for cell in cells(mesh):
    idx = cell.index()
    value = some_expression_based_on_cell(idx)
    for dof in dm.cell_dofs(cell.index()):
        arr[dof] = val

24 days ago by


I had the same problem but not while using an Expression function... what solved my problem was to find what is the maximum recursion depth of my system and setting it to the highest possible value.

you can find your limit using sys.getrecursionlimit()

or from, there is a script to find out the machine's highest recursion depth:

After finding the value, I just set it using


The standard limit is a bit conservative, this is naturally low to avoid an infinite recursions resulting in stack overflow... I hope it was useful...

Regards, Leonardo

14 days ago by
This seems to work with Fenics 2018.1, with a harmless warning... I hope that this helps!!

class InitialConditions(UserExpression):
    def __init__(self, subdomains, c_in, c_out, **kwargs):
        super(InitialConditions, self).__init__(**kwargs)
        self.subdomains = subdomains
        self.c_in  = c_in
        self.c_out = c_out

    def eval_cell(self, values, x, ufc_cell):
        if self.subdomains[ufc_cell.index] == 1: # inside circle
            values[0] = self.c_in
        elif self.subdomains[ufc_cell.index] == 2: # outside circle
            values[0] = self.c_out
Please login to add an answer/comment or follow this question.

Similar posts:
Search »