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

Interpolate expression on function space in C++

0 votes

Hi,

Can anyone kindly advise on how would one write the following expression in C++:

V = FunctionSpace(mesh, "CG", 2)
u0 = Function(V)
ui  = Expression('sin(pi*x[0])*sin(pi*x[1])*exp(-350.*(pow(x[0]-0.5,2)+pow(x[1]-0.5,2) - a*t))', a=a, t=0.0)
u0 = interpolate(ui, V)
asked Aug 11, 2016 by Mark FEniCS Novice (310 points)
reshown Sep 25, 2016 by Mark

1 Answer

0 votes
 
Best answer

Hi,
in this demo you can find the answer.

answered Aug 14, 2016 by hernan_mella FEniCS Expert (19,460 points)
selected Oct 28, 2016 by Mark

One can write the lines in question like this, assuming other standard definitions of mesh, etc:

UFL.py

V = FunctionSpace(mesh, "CG", 2)

main.cpp

class MyExpression : public Expression
{
public:
  void eval(Array<double>& values, const Array<double>& x) const
  {
    values[0] = sin(pi*x[0])*sin(pi*x[1])*exp(-350.*(pow(x[0]-0.5,2)+pow(x[1]-0.5,2) - a*t));
  }
};

u0 = Function(V)
MyExpression ui;
u0.interpolate(ui);

Will this be the same as python code in question? I ask because, here the expression is being assigned to values[0] and then being interpolated, while in python, it is a std. expression being interpolated over the mesh.

Is the same. In the python interface the advantage of use a JIT-compiled expression is that is more efficient that subclassing (see here for example). In the cpp interface this isn't a problem.

Thank you everyone!

...