Initial condition based on subdomain label (GMSH / dolfin-convert)
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:
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 = self.c_in elif self.subdomains[cell.index] == 2: # outside circle values = self.c_out
interpolate(InitialConditions(subdomains, 0.1, 0.2, degree=0), V)
How would I modify the class above to correctly apply my initial conditions?
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 f.vector().set_local(arr) f.vector().apply('insert')
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 https://stackoverflow.com/questions/3323001/what-is-the-maximum-recursion-depth-in-python-and-how-to-increase-it, there is a script to find out the machine's highest recursion depth: http://svn.python.org/projects/python/trunk/Tools/scripts/find_recursionlimit.py
After finding the value, I just set it using https://docs.python.org/3/library/sys.html#sys.setrecursionlimit
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...
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 = self.c_in elif self.subdomains[ufc_cell.index] == 2: # outside circle values = self.c_out