CompiledExpression can't be initialized with a dolfin.Constant
Issue #1041
resolved
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)
-
-
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.
-
reporter - changed status to resolved
Proposal for fixing issue 1041
→ <<cset ce744dddedc5>>
-
reporter @michalhabera See PR #495
-
reporter Merge branch 'master' of https://bitbucket.org/fenics-project/dolfin into dokken/fixissue1041
→ <<cset 39ca32123487>>
-
Merged in dokken/fixissue1041 (pull request #495)
Proposal for fixing issue 1041
→ <<cset a2bf15c41212>>
-
Merged in dokken/fixissue1041 (pull request #495)
Proposal for fixing issue 1041
→ <<cset a2bf15c41212>>
- Log in to comment
And if you do
a=a.cpp_object()
? See my answer here https://www.allanswered.com/post/avxbm/how-to-express-time-dependent-compiled-cpp-expression-with-fenics-2018-1-0-dev0pybind11/