← Back to team overview

dolfin team mailing list archive

Re: C++-function code in Python interface

 

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()


Johan


Follow ups

References