CompiledExpression can't be initialized with a dolfin.Constant

Issue #1041 resolved
Jørgen Dokken created an issue

As dolfin.Constant no longer inherits dolfin.cpp.Constant one can no longer create a CompiledExpression using dolfin.Constant as argument. Example:

from dolfin import *

base_code = '''
#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
namespace py = pybind11;

#include <dolfin/function/Expression.h>
#include <dolfin/function/Constant.h>
class MyCppExpression : public dolfin::Expression
{
public:
      std::shared_ptr<dolfin::Constant> a;
  MyCppExpression() : dolfin::Expression() {}

  // Function for evaluating expression
  void eval(Eigen::Ref<Eigen::VectorXd> values, Eigen::Ref<const Eigen::VectorXd> x) const override
  {
    double a_ = (double) *a;
    values[0] = x[0] - a_;
  }
  };

PYBIND11_MODULE(SIGNATURE, m)
{
py::class_<MyCppExpression, std::shared_ptr<MyCppExpression>, dolfin::Expression>
(m, "MyCppExpression")
.def(py::init<>())
.def_readwrite("a", &MyCppExpression::a);
}

'''
mesh = UnitSquareMesh(10, 10)
V = FunctionSpace(mesh, "CG", 1)

a = Constant(0.2, name="a")
f = CompiledExpression(compile_cpp_code(base_code).MyCppExpression(),
                       degree=1, a=a)

Raises:

Traceback (most recent call last):
  File "test_compiledexpression.py", line 38, in <module>
    degree=1, a=a)
  File "/usr/local/lib/python3.5/dist-packages/dolfin/function/expression.py", line 321, in __init__
    setattr(self._cpp_object, k, val)
TypeError: (): incompatible function arguments. The following argument types are supported:
    1. (self: dolfin_cpp_module_cbce86d2894a54e9afd84a250ca2190b.MyCppExpression, arg0: dolfin.cpp.function.Constant) -> None

Invoked with: <dolfin_cpp_module_cbce86d2894a54e9afd84a250ca2190b.MyCppExpression object at 0x7f165b7be2d0>, Coefficient(FunctionSpace(None, FiniteElement('Real', None, 0)), 5)

Comments (7)

  1. Jørgen Dokken reporter

    So that works, but it is not user intuitive. I've made a five line fix for this and will submit a PR in a minute.

  2. Log in to comment