Problems with C++ Expression

Good evening,
I have a problem with a C++ Expression, who helps me to construct a tensor for each cell starting from MeshFunctions. I have always used this expression but today it doesn not work. Could you tell me why?

Here is the code:

defineMatrix_code = """

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

#include <dolfin/function/Expression.h>
#include <dolfin/mesh/MeshFunction.h>

class Components_DT_D : public dolfin::Expression

{
public:

     // Create expression with 6 components
     Components_DT_D() : dolfin::Expression(6) {}

     // Function for evaluating expression on each cell
     void eval(Eigen::Ref<Eigen::VectorXd> values, Eigen::Ref<const Eigen::VectorXd> x, const ufc::cell& cell) const override
    {
     const uint topDim = cell.topological_dimension;
     const uint cell_index = cell.index;
     values[0] = (*d11)[cell_index];
     values[1] = (*d12)[cell_index];
     values[2] = (*d13)[cell_index];
     values[3] = (*d22)[cell_index];
     values[4] = (*d23)[cell_index];
     values[5] = (*d33)[cell_index];
    }

    // The data stored in mesh functions
    std::shared_ptr<dolfin::MeshFunction<double> > d11;
    std::shared_ptr<dolfin::MeshFunction<double> > d12;
    std::shared_ptr<dolfin::MeshFunction<double> > d13;
    std::shared_ptr<dolfin::MeshFunction<double> > d22;
    std::shared_ptr<dolfin::MeshFunction<double> > d23;
    std::shared_ptr<dolfin::MeshFunction<double> > d33;

};

PYBIND11_MODULE(SIGNATURE, m)
{
   py::class_<Components_DT_D, std::shared_ptr<Components_DT_D>, dolfin::Expression>
     (m, "Components_DT_D")
     .def(py::init<>())
     .def_readwrite("d11", &Components_DT_D::d11)
     .def_readwrite("d12", &Components_DT_D::d12)
     .def_readwrite("d13", &Components_DT_D::d13)
         .def_readwrite("d22", &Components_DT_D::d22)
         .def_readwrite("d23", &Components_DT_D::d23)
         .def_readwrite("d33", &Components_DT_D::d33);
}

"""

and this is the error I get:
ImportError: generic_type: type “Components_DT_D” referenced unknown base type “dolfin::Expression”

Hey, can you please edit your question to make the code copy-paste-able? You used Blockquote to paste your code, however theres a dedicated type calledperformated text (also accesible via Str + E) to paste code. When copying your code the " are totally messed up.

1 Like

I tested the code: The following code

from fenics import *



defineMatrix_code = """

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

#include <dolfin/function/Expression.h>
#include <dolfin/mesh/MeshFunction.h>

class Components_DT_D : public dolfin::Expression

{
public:

     // Create expression with 6 components
     Components_DT_D() : dolfin::Expression(6) {}

     // Function for evaluating expression on each cell
     void eval(Eigen::Ref<Eigen::VectorXd> values, Eigen::Ref<const Eigen::VectorXd> x, const ufc::cell& cell) const override
    {
     const uint topDim = cell.topological_dimension;
     const uint cell_index = cell.index;
     values[0] = (*d11)[cell_index];
     values[1] = (*d12)[cell_index];
     values[2] = (*d13)[cell_index];
     values[3] = (*d22)[cell_index];
     values[4] = (*d23)[cell_index];
     values[5] = (*d33)[cell_index];
    }

    // The data stored in mesh functions
    std::shared_ptr<dolfin::MeshFunction<double> > d11;
    std::shared_ptr<dolfin::MeshFunction<double> > d12;
    std::shared_ptr<dolfin::MeshFunction<double> > d13;
    std::shared_ptr<dolfin::MeshFunction<double> > d22;
    std::shared_ptr<dolfin::MeshFunction<double> > d23;
    std::shared_ptr<dolfin::MeshFunction<double> > d33;

};

PYBIND11_MODULE(SIGNATURE, m)
{
   py::class_<Components_DT_D, std::shared_ptr<Components_DT_D>, dolfin::Expression>
     (m, "Components_DT_D")
     .def(py::init<>())
     .def_readwrite("d11", &Components_DT_D::d11)
     .def_readwrite("d12", &Components_DT_D::d12)
     .def_readwrite("d13", &Components_DT_D::d13)
         .def_readwrite("d22", &Components_DT_D::d22)
         .def_readwrite("d23", &Components_DT_D::d23)
         .def_readwrite("d33", &Components_DT_D::d33);
}

"""

test = CompiledExpression(compile_cpp_code(defineMatrix_code).Components_DT_D(), degree=2)

works fine for me. Can you please provide a minimal example and maybe add the version of fenics you use.

1 Like

I use it in this way:

mvc_FWxx = MeshValueCollection("double", mesh, "meshFWxx.xml")
dNFW11 = MeshFunction("double", mesh, mvc_FWxx)

mvc_FWyy = MeshValueCollection("double", mesh, "meshFWyy.xml")
dNFW22 = MeshFunction("double", mesh, mvc_FWyy)

mvc_FWzz = MeshValueCollection("double", mesh, "meshFWzz.xml")
dNFW33 = MeshFunction("double", mesh, mvc_FWzz)

mvc_FWxy = MeshValueCollection("double", mesh, "meshFWxy.xml")
dNFW12 = MeshFunction("double", mesh, mvc_FWxy)

mvc_FWxz = MeshValueCollection("double", mesh, "meshFWxz.xml")
dNFW13 = MeshFunction("double", mesh, mvc_FWxz)

mvc_FWyz = MeshValueCollection("double", mesh, "meshFWyz.xml")
dNFW23 = MeshFunction("double", mesh, mvc_FWyz)

defineMatrix_code = """

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

#include <dolfin/function/Expression.h>
#include <dolfin/mesh/MeshFunction.h>

class Components_DT_D : public dolfin::Expression

{
public:

     // Create expression with 6 components
     Components_DT_D() : dolfin::Expression(6) {}

     // Function for evaluating expression on each cell
     void eval(Eigen::Ref<Eigen::VectorXd> values, Eigen::Ref<const Eigen::VectorXd> x, const ufc::cell& cell) const override
    {
     const uint topDim = cell.topological_dimension;
     const uint cell_index = cell.index;
     values[0] = (*d11)[cell_index];
     values[1] = (*d12)[cell_index];
     values[2] = (*d13)[cell_index];
     values[3] = (*d22)[cell_index];
     values[4] = (*d23)[cell_index];
     values[5] = (*d33)[cell_index];
    }

    // The data stored in mesh functions
    std::shared_ptr<dolfin::MeshFunction<double> > d11;
    std::shared_ptr<dolfin::MeshFunction<double> > d12;
    std::shared_ptr<dolfin::MeshFunction<double> > d13;
    std::shared_ptr<dolfin::MeshFunction<double> > d22;
    std::shared_ptr<dolfin::MeshFunction<double> > d23;
    std::shared_ptr<dolfin::MeshFunction<double> > d33;

};

PYBIND11_MODULE(SIGNATURE, m)
{
   py::class_<Components_DT_D, std::shared_ptr<Components_DT_D>, dolfin::Expression>
     (m, "Components_DT_D")
     .def(py::init<>())
     .def_readwrite("d11", &Components_DT_D::d11)
     .def_readwrite("d12", &Components_DT_D::d12)
     .def_readwrite("d13", &Components_DT_D::d13)
         .def_readwrite("d22", &Components_DT_D::d22)
         .def_readwrite("d23", &Components_DT_D::d23)
         .def_readwrite("d33", &Components_DT_D::d33);
}

"""

d0 = CompiledExpression(compile_cpp_code(defineMatrix_code).Components_DT_D(), d11 = dNFW11, d12 = dNFW12, d13 = dNFW13, d22 = dNFW22, d23 = dNFW23, d33 = dNFW33, degree=2)

I use Fenics version 2019.1.0
Now it gives me the error:

Traceback (most recent call last):
  File "anisotropo_pnc.py", line 504, in <module>
    d0 = CompiledExpression(compile_cpp_code(defineMatrix_code).Components_DT_D(), d11 = dNFW11, d12 = dNFW12, d13 = dNFW13, d22 = dNFW22, d23 = dNFW23, d33 = dNFW33, degree=2)
  File "/data/ballatore/miniconda3/lib/python3.8/site-packages/dolfin/jit/pybind11jit.py", line 95, in compile_cpp_code
    module, signature = dijitso_jit(cpp_code, module_name, params,
  File "/data/ballatore/miniconda3/lib/python3.8/site-packages/dolfin/jit/jit.py", line 47, in mpi_jit
    return local_jit(*args, **kwargs)
  File "/data/ballatore/miniconda3/lib/python3.8/site-packages/dolfin/jit/jit.py", line 103, in dijitso_jit
    return dijitso.jit(*args, **kwargs)
  File "/data/ballatore/miniconda3/lib/python3.8/site-packages/dijitso/jit.py", line 153, in jit
    lib = lookup_lib(signature, cache_params)
  File "/data/ballatore/miniconda3/lib/python3.8/site-packages/dijitso/cache.py", line 391, in lookup_lib
    lib = load_library(lib_signature, cache_params)
  File "/data/ballatore/miniconda3/lib/python3.8/site-packages/dijitso/cache.py", line 363, in load_library
    lib = __import__(signature)
ImportError: libboost_timer.so.1.74.0: cannot open shared object file: No such file or directory

I installed libboost_timer.so.1.74.0 but it does not change.
It has always worked with version 1.70, but now everything changed and I do not know why.

I can’t really test your example, as I dont have the files you’re linking. I suspect something is missing in your “meshFW__.xml”-files.
The following minimal example worked fine for me. Please test it on our computer. If it works, then something is off with your files in the MeshValueCollection-s

from fenics import *

mesh = UnitSquareMesh(10, 10)
dNFW11 = MeshFunction("double", mesh, 1)

dNFW22 = MeshFunction("double", mesh, 1)

dNFW33 = MeshFunction("double", mesh, 1)

dNFW12 = MeshFunction("double", mesh, 1)

dNFW13 = MeshFunction("double", mesh, 1)

dNFW23 = MeshFunction("double", mesh, 1)

defineMatrix_code = """

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

#include <dolfin/function/Expression.h>
#include <dolfin/mesh/MeshFunction.h>

class Components_DT_D : public dolfin::Expression

{
public:

     // Create expression with 6 components
     Components_DT_D() : dolfin::Expression(6) {}

     // Function for evaluating expression on each cell
     void eval(Eigen::Ref<Eigen::VectorXd> values, Eigen::Ref<const Eigen::VectorXd> x, const ufc::cell& cell) const override
    {
     const uint topDim = cell.topological_dimension;
     const uint cell_index = cell.index;
     values[0] = (*d11)[cell_index];
     values[1] = (*d12)[cell_index];
     values[2] = (*d13)[cell_index];
     values[3] = (*d22)[cell_index];
     values[4] = (*d23)[cell_index];
     values[5] = (*d33)[cell_index];
    }

    // The data stored in mesh functions
    std::shared_ptr<dolfin::MeshFunction<double> > d11;
    std::shared_ptr<dolfin::MeshFunction<double> > d12;
    std::shared_ptr<dolfin::MeshFunction<double> > d13;
    std::shared_ptr<dolfin::MeshFunction<double> > d22;
    std::shared_ptr<dolfin::MeshFunction<double> > d23;
    std::shared_ptr<dolfin::MeshFunction<double> > d33;

};

PYBIND11_MODULE(SIGNATURE, m)
{
   py::class_<Components_DT_D, std::shared_ptr<Components_DT_D>, dolfin::Expression>
     (m, "Components_DT_D")
     .def(py::init<>())
     .def_readwrite("d11", &Components_DT_D::d11)
     .def_readwrite("d12", &Components_DT_D::d12)
     .def_readwrite("d13", &Components_DT_D::d13)
         .def_readwrite("d22", &Components_DT_D::d22)
         .def_readwrite("d23", &Components_DT_D::d23)
         .def_readwrite("d33", &Components_DT_D::d33);
}

"""

d0 = CompiledExpression(compile_cpp_code(defineMatrix_code).Components_DT_D(), d11 = dNFW11, d12 = dNFW12, d13 = dNFW13, d22 = dNFW22, d23 = dNFW23, d33 = dNFW33, degree=2)

No, it does not work, it gives me the same error:

Traceback (most recent call last):
  File "prova.py", line 71, in <module>
    d0 = CompiledExpression(compile_cpp_code(defineMatrix_code).Components_DT_D(), d11 = dNFW11, d12 = dNFW12, d13 = dNFW13, d22 = dNFW22, d23 = dNFW23, d33 = dNFW33, degree=2)
  File "/data/ballatore/miniconda3/lib/python3.8/site-packages/dolfin/jit/pybind11jit.py", line 95, in compile_cpp_code
    module, signature = dijitso_jit(cpp_code, module_name, params,
  File "/data/ballatore/miniconda3/lib/python3.8/site-packages/dolfin/jit/jit.py", line 47, in mpi_jit
    return local_jit(*args, **kwargs)
  File "/data/ballatore/miniconda3/lib/python3.8/site-packages/dolfin/jit/jit.py", line 103, in dijitso_jit
    return dijitso.jit(*args, **kwargs)
  File "/data/ballatore/miniconda3/lib/python3.8/site-packages/dijitso/jit.py", line 153, in jit
    lib = lookup_lib(signature, cache_params)
  File "/data/ballatore/miniconda3/lib/python3.8/site-packages/dijitso/cache.py", line 391, in lookup_lib
    lib = load_library(lib_signature, cache_params)
  File "/data/ballatore/miniconda3/lib/python3.8/site-packages/dijitso/cache.py", line 363, in load_library
    lib = __import__(signature)
ImportError: libboost_timer.so.1.74.0: cannot open shared object file: No such file or directory

I have no idea how to obtain libboost_timer.so.1.74.0 and I have always used this Expression without problems.

mhm, I dont think its a fenics specific error. Try this solution maybe:

I suspect the underlying reason is the same :slight_smile:

1 Like