RecursionError: maximum recursion depth exceeded in the ft11_magnetostatics.py example of the "Solving PDEs in Python - The FEniCS tutorial Part 1"

Hi, I get the following error when I try to run the ft11_magnetostatics.py example of the “Solving PDEs in Python -The FEniCS tutorial Part 1”. It happens with the default installation of FEniCS for Ubuntu as well as with the docker container. I have run the example in a Jupyter notebook and the error happens just after the declaration the Permeability class:

The error is the following:

Traceback (most recent call last):
  File "ft11_magnetostatics.py", line 74, in <module>
    mu = Permeability(markers, degree=1)
  File "ft11_magnetostatics.py", line 65, in __init__
    self.markers = markers
  File "/usr/lib/python3/dist-packages/dolfin/function/expression.py", line 438, in __setattr__
    elif name in self._parameters:
  File "/usr/lib/python3/dist-packages/dolfin/function/expression.py", line 432, in __getattr__
    return self._parameters[name]
  File "/usr/lib/python3/dist-packages/dolfin/function/expression.py", line 432, in __getattr__
    return self._parameters[name]
  File "/usr/lib/python3/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
2 Likes

The tutorial is out of date. Change your Permeability class to the following:

class Permeability(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):
        if self.markers[cell.index] == 0:
            values[0] = 4*pi*1e-7 # vacuum
        elif self.markers[cell.index] == 1:
            values[0] = 1e-5      # iron (should really be 6.3e-3)
        else:
            values[0] = 1.26e-6   # copper
11 Likes

Thank you very much for that. It is a year since you gave that answer, but still needed that. Lots of Fenics tutorials are out of date. Do you know who should I tell about that?

What exactly does this line do?
super().__init__(**kwargs)

Note that the FEniCS-tutorial is a book by Hans-Petter Langtangen and Anders Logg, and comments about it being out of date should be addressed as described in my link:

# Comments and corrections

Comments and corrections can be reported as issues for the Git repository of the book  or via email to logg@chalmers.se. Note that fixes will be incorporated directly into the PDF and HTML versions of the book accessible above, but will take a longer time to propagate to the Springer print and eBook versions.

The tutorial was created for FEniCS 2016.2.0, and since then alot of API changes has happened. However, FEniCS has a large variety of demos, that illustrate the newest syntax.

We are currently preparing a tutorial for the newest version of dolfin, dolfinx, which we hopefully will release before April next year.

See for instance: Supercharge Your Classes With Python super() – Real Python (for the long explanation) or What does this to in python super().__init__(**kwargs) - Stack Overflow for the short explanation

1 Like