I am trying write a custom user expression with a c++ code snippet by following the simple tutorial in tutorial.
Here is a simple working example using the poisson demo from poisson.
I am using dolphin version 2019.2.0.dev0.
from dolfin import *
# Create mesh and define function space
mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, "Lagrange", 1)
# Define Dirichlet boundary (x = 0 or x = 1)
def boundary(x):
return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS
# Define boundary condition
u0 = Constant(0.0)
bc = DirichletBC(V, u0, boundary)
materials = MeshFunction('double', mesh,2)
cppcode = """
class K : public Expression
{
public:
void eval(Array<double>& values,
const Array<double>& x,
const ufc::cell& cell) const
{
values[0] = 1;
}
};
"""
kappa = UserExpression(cppcode, degree=0)
# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)",degree=2)
g = Expression("sin(5*x[0])",degree=2)
a = kappa*inner(grad(u), grad(v))*dx
L = f*v*dx + g*v*ds
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Plot solution
plot(u)
I get the following error, despite my best efforts.
RuntimeError:
*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
*** fenics-support@googlegroups.com
*** Remember to include the error message listed below and, if possible,
*** include a minimal running example to reproduce the error.
*** -------------------------------------------------------------------------
*** Error: Unable to evaluate expression.
*** Reason: Missing eval() function (must be overloaded).
*** Where: This error was encountered inside Expression.cpp.
*** Process: 0
*** DOLFIN version: 2019.2.0.dev0
*** Git changeset: ubuntu
*** -------------------------------------------------------------------------
I have simplified the c++ snippet as much as possible. I also had to change the demo from Expression
to UserExpression
. For some reason it cannot find the Eval
method. I have also tried the examples with the pybind11 signatures, but I get a different type error about Dolfin::Expression
.
If anyone can point me in the right direction about simple c++ snippets, that would be an immense help. Eventually I need to evaluate a BC term that will need to include facet information. The expression will be very similar to FacetArea() with some slight modifications.