← Back to team overview

dolfin team mailing list archive

Re: [Question #97842]: Extra parameters in pyDolfin Expression

 

Question #97842 on DOLFIN changed:
https://answers.launchpad.net/dolfin/+question/97842

    Status: Open => Answered

Johan Hake proposed the following answer:
On Monday 18 January 2010 08:29:03 dbeacham wrote:
> New question #97842 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/97842
> 
> Is there a less hacked way to add extra parameter values to be used in an
>  Expression in python than:
> 
> class MyExp(Expression):
>     def __new__(cls, *args, **kwargs):
>         obj = Expression.__new__(cls, **kwargs)
>         return obj
> 
>     def __init__(self, *args):
>         ....
>         ....

It has been possible to pass user defined optional arguments to the __init__ 
method. But it looks like a regression has kicked in.

I have fixed the regression in dolfin dev, and added a doc string
example.

You should now be able to do:

   class MyExp(Expression):
       def __init__(self, **kwargs):   
           self._mesh = kwargs['mesh'] 
           self._domain = kwargs['domain']
       def eval(self, values, x):         
          info(self._mesh, 1)             
          info(self._domain, 1)

   mesh = UnitCube(1,1,1)
   domain = MeshFunction("uint", mesh, 0)
   e = MyExp(mesh=mesh, domain=domain)
   e(0,0,0)

Do not use *args and do _not_ call the base class __init__. Yes, it is counter 
intuitive but it is a result of having the Expression class do both compiled 
Expressions and python derived Expressions. 

I prefer having two classes, CompiledExpression and Expression, so we could 
say goodbye to this kind of troubles ;)

Johan


> where *args are my extra parameters (ie mesh, mesh functions etc.) and
>  __new__ might have to be better worked if I had included my own kwargs.
>  The above seems to work ok, but let me know of any major problems with it.
> 
> I could always use a MyExp.init(...) function after I've created an
>  instance to set up class variables, but just curious as to whether I
>  missed something obvious for a one line initiation.
> 
> David.
>

-- 
You received this question notification because you are a member of
DOLFIN Team, which is an answer contact for DOLFIN.