Re: [DOLFIN-dev] Expression subclass
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) 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@fenics.org > http://www.fenics.org/mailman/listinfo/dolfin-dev > ___ DOLFIN-dev mailing list DOLFIN-dev@fenics.org http://www.fenics.org/mailman/listinfo/dolfin-dev
Re: [DOLFIN-dev] 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@fenics.org >> http://www.fenics.org/mailman/listinfo/dolfin-dev >> ___ DOLFIN-dev mailing list DOLFIN-dev@fenics.org http://www.fenics.org/mailman/listinfo/dolfin-dev
Re: [DOLFIN-dev] Expression subclass
On Saturday 17 October 2009 14:23:27 Garth N. Wells wrote: > 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. No this is a sharp corner, which has bugged me a while. The point is that the __init__ method you define is _not_ the real __init__ method, but it is called inside the generated __init__ function, which of course is not that intuitive, especially not now when we have forced the V/element argument to be a kwarg. Johan > 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@fenics.org > >> http://www.fenics.org/mailman/listinfo/dolfin-dev > ___ DOLFIN-dev mailing list DOLFIN-dev@fenics.org http://www.fenics.org/mailman/listinfo/dolfin-dev