dolfin team mailing list archive
-
dolfin team
-
Mailing list archive
-
Message #15484
Re: C++-function code in Python interface
On Mon, Sep 21, 2009 at 08:50:05AM +0200, Johan Hake wrote:
> On Monday 21 September 2009 08:42:21 Anders Logg wrote:
> > On Mon, Sep 21, 2009 at 07:58:52AM +0200, Johan Hake wrote:
> > > On Sunday 20 September 2009 23:02:31 Anders Logg wrote:
> > > > Is it possible to add member functions to JIT-compiled C++ functions
> > > > that will be accessible in Python?
> > > >
> > > > I want to do something like
> > > >
> > > > code = """
> > > > class MyFunction : public Function
> > > > {
> > > > public:
> > > >
> > > > std::vector<double> _values;
> > > >
> > > > MyFunction(FunctionSpace& V) : Function(V) {}
> > > >
> > > > void eval(double* values, const Data& data) const
> > > > {
> > > > values[0] = _values[data.cell().index()];
> > > > }
> > > >
> > > > void update(stuff)
> > > > {
> > > > // Recompute _values here
> > > > }
> > > >
> > > > };"""
> > > >
> > > > When I try this, the update() function is not accessible from Python.
> > >
> > > This works like a charm!
> > >
> > > from dolfin import *
> > >
> > > code = """
> > > class MyFunction : public Function
> > > {
> > > public:
> > >
> > > std::vector<double> _values;
> > >
> > > MyFunction(boost::shared_ptr<FunctionSpace> V) : Function(V) {}
> > >
> > > void eval(double* values, const Data& data) const
> > > {
> > > values[0] = _values[data.cell().index()];
> > > }
> > >
> > > void update()
> > > {
> > > std::cout << "done" << std::endl;
> > > // Recompute _values here
> > > }
> > >
> > > };
> > > """
> > >
> > > mesh = UnitSquare(2,2)
> > > V = FunctionSpace(mesh,'CG',1)
> > > f = Function(V,code)
> > > f.update()
> >
> > Thanks!
> >
> > The problem was I couldn't get it to compile (missing the shared_ptr
> > stuff) so I tried doing
> >
> > f = Function(V, code=code)
> >
> > which seemed to work. Only it didn't... The code=code keyword argument
> > was just ignored (or perhaps used to set an attribute?). And then my
> > update() function was missing.
> >
> > Should there be some check for keyword arguments like the one above?
>
> Ahh!
>
> I think the **kwargs is an artifact from some deprecated functionality.
>
> It is removed now.
Nice!
--
Anders
Attachment:
signature.asc
Description: Digital signature
References