C++ Expressions


I’m trying to speed up an Expression by using C++ code, as suggested in the tutorial Using C++ code snippets to define subdomains .

A minimal example derived from this tutorial

from fenics import *
cppcode = “”"
class K : public Expression

void eval(Array& values,
const Array& x,
const ufc::cell& cell) const
values[0] = k_0;
double k_0;


kappa = Expression(cppcode=cppcode, degree=0)
kappa.k_0 = 0

already returns the error (with Fenics 2019.1.0):

Must supply C++ code to Expression. You may want to use UserExpression

What am I missing here?

A similar question has been asked in Cpp-based Expression.
The answer suggest that the string should work as R-value to values[0]. I wouldn’t be happy with this, because I would need to load different values (like k_0) into the C++ class and return them depending on x.

Thanks & Regards,

I think you are using an old Expression framework. See new framework e.g. in https://bitbucket.org/fenics-project/dolfin/src/master/python/demo/documented/tensor-weighted-poisson/demo_tensor-weighted-poisson.py.

If you wonder about the error message:
At least in dolfin 2019.1 the Expression argument is cpp_code and not cppcode.

kappa = Expression(cpp_code=cppcode, degree=0)

will give a different error.