This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

pass arguments to custom Expression ctor

0 votes

Is it possible to pass arguments to a custom ctor of a subclass of Expression from within python without resorting to shared ptrs in the public section of the class? Consider this expression class

namespace dolfin {
class MyFun : public Expression
{
    public:

    MyFun(const Mesh& mesh): Expression(3), mesh_(mesh) {};

    void eval(Array<double>& values, const Array<double>& x, const ufc::cell& ufc_cell) const {
        dolfin_assert(ufc_cell.local_facet >= 0);
    }
    private:
    const Mesh& mesh_;
};
}

If the above cpp code is passed as str to dolfin.Expression I get the following error message:

>>> dolfin.Expression(ccode)
TypeError: new_MyFun() takes exactly one argument (0 given)
asked Nov 24, 2014 by thisch FEniCS Novice (580 points)

1 Answer

+1 vote
 
Best answer

Untested, but wrapping it as an extension module like this should work:

from dolfin import *
code="""
namespace dolfin {
class MyFun : public Expression
{
    public:

        MyFun(const Mesh& mesh): Expression(3), mesh_(mesh) {};

        void eval(Array<double>& values, const Array<double>& x, const ufc::cell& ufc_cell) const {
            dolfin_assert(ufc_cell.local_facet >= 0);
        }
        private:
            const Mesh& mesh_;
        };
 }
 """
compiled_module = compile_extension_module(code)
myfun = compiled_module.MyFun(UnitSquareMesh(4,4))
answered Nov 24, 2014 by Øyvind Evju FEniCS Expert (17,700 points)
selected Nov 24, 2014 by thisch

I guess support for Expression(cppcode, mesh=mesh) is not implemented. Anyway your solution works. Thx

...