← Back to team overview

dolfin team mailing list archive

Re: Expression subclass

 



Johan Hake wrote:
On Saturday 17 October 2009 11:48:57 Garth N. Wells wrote:
I'd like to create a sub-class of Expression,

   class my_expression(Expression):
       def __init__(self, V, x):
           self.x =x
           Expression.__init__(V = V)

but I'm having trouble with the constructor - I get the below error. Any
tips?

The following should work:

from dolfin import *

class MyExpr(Expression):
    def __init__(self, x, V=None):
        self.x = x

    def eval(self, values, x):
        pass

m = UnitSquare(2,2)
V = FunctionSpace(m,"CG",1)

MyExpr(0.0, V=V)


Thanks. Works fine. I wouldn't have guessed the solution.

Garth


This is somewhat inconsistent with how sub classing in Python works. Here you should _not_ call the __init__ method of the base class. The short reason is that this is done for you, while instantiating the dynamically created class.

This is something that could be fixed using the suggested new design for CompiledExpression, so one would require an initialization as you suggest above.

Johan

Garth


line 120, in __init__
     raise TypeError, "Expression need to be initialized using either a
'cpp.FunctionSpace', using kwarg 'V', or an 'ufl.FiniteElement' using
kwarg 'element'."
TypeError: Expression need to be initialized using either a
'cpp.FunctionSpace', using kwarg 'V', or an 'ufl.FiniteElement' using
kwarg 'element'.
_______________________________________________
DOLFIN-dev mailing list
DOLFIN-dev@xxxxxxxxxx
http://www.fenics.org/mailman/listinfo/dolfin-dev





Follow ups

References