Hi all,
I have a question how to write C++ class for a userExpression that takes the solution u as an argument. Thanks to @dokken (How to difine UserExpression that takes the solution u), I have a Python code (Version ‘2019.1.0’) similar to this.
N = 100
X = 1.0
mesh = dl.IntervalMesh(N, 0, X)
degree = 1 # linear basis functions
Vh = dl.FunctionSpace(mesh, "Lagrange", degree)
u = dl.interpolate(dl.Expression("x[0]", degree = 1), Vh)
alpha = 2.0
class user_expr(dl.UserExpression):
def set_alpha(self, u, alpha): # in the user_expr class
self.u = u
self.alpha = alpha
def eval(self, value, x): # in the user_expr class
if self.u(x) > 0.5:
value[0] = 0.5
else:
value[0] = self.alpha * self.u(x)* self.u(x)
def value_shape(self):
return ()
user_expr2 = user_expr(degree = 0)
user_expr2.set_alpha(u, alpha)
q = dl.interpolate(user_expr2, dl.FunctionSpace(mesh, "Lagrange", 1))
d_to_v = dl.dof_to_vertex_map(q.function_space())
print(q.vector()[:][d_to_v])
, and it works fine. I want to speed up this code by rewriting it into C++ codes. Regardless of my search, I could not make it work due to C++ compilation errors etc. the current C++ is like this.
cpp_code = """
#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
namespace py = pybind11;
#include <dolfin/function/Expression.h>
#include <dolfin/function/Function.h>
class user_expr : public dolfin::Expression
{
public:
std::shared_ptr<dolfin::Function> u;
double alpha;
user_expr(std::shared_ptr<dolfin::Function> u_) : dolfin::Expression(){
u = u_;
}
void eval(Eigen::Ref<Eigen::VectorXd> value, Eigen::Ref<const Eigen::VectorXd> x) const override
{
u->eval(value, x);
if u > 0.5
value[0] = 0.5
else
value[0] = alpha * u * u
}
};
PYBIND11_MODULE(SIGNATURE, m)
{
py::class_<user_expr, std::shared_ptr<user_expr>, dolfin::Expression>
(m, "user_expr")
.def(py::init<std::shared_ptr<dolfin::Function> >())
.def_readwrite("u", &user_expr::u);
}
"""
with
profile = dl.compile_cpp_code(cpp_code)
expr = dl.CompiledExpression(profile.user_expr(u.cpp_object()), degree=1)
expr.alpha = alpha
Could anyone help me? I appreciate any help.