Cannot create domain-dependent expression

Issue #1056 invalid
AJ Lawrence created an issue

Script from the FEniCS tutorial is broken in 2018. Particularly this snippet:

class Permeability(Expression):
    def __init__(self, markers, **kwargs):
        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

When instantiated, it throws a RecursionError on the third line into the Expression.py code.

Comments (3)

  1. Markus Wegmann

    Hi there

    We also ran into the same problem:

    https://bitbucket.org/fenics-project/dolfin/src/f8cd6c0b2722bef82f1d429a9e838a4491b30154/python/dolfin/function/expression.py#lines-426

    seems bogus, as __setattr__ and __getattr__ needs to be implemented carefully to prevent infinite recursion. See https://stackoverflow.com/questions/16237659/python-how-to-implement-getattr

    The tutorial example ft11_magnetostatics.py also fails:

    class Permeability(Expression):
        def init(self, **kwargs):
            print('Permeability created')
            sys.stdout.flush()
    
        def eval_cell(self, values, x, cell):
            print('values {}'.format(values))
            sys.stdout.flush()
            if markers[cell.index] == 0:
                values[0] = 4*pi*1e-7 # vacuum
            elif markers[cell.index] == 1:
                values[0] = 1e-5      # iron (should really be 6.3e-3)
            else:
                values[0] = 1.26e-6   # copper
    
    mu = Permeability(degree=1) # FAILS!
    

    Is reimplementing __setattr__ and __getattr__ required, or is our way of using the API wrong/outdated?

    Best regard ;)

  2. Log in to comment