# Problems with C++ Expression

**URL:** <https://fenicsproject.discourse.group/t/problems-with-c-expression/7465>\
**Category:** Errors\
**Created:** [January 12, 2022, 5:47pm UTC](https://fenicsproject.discourse.group/t/problems-with-c-expression/7465 "2022-01-12T17:47:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francesca\_Ballatore](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/francesca_ballatore/32/2510_2.png) [@Francesca\_Ballatore](https://fenicsproject.discourse.group/u/Francesca_Ballatore)\
**Post date:** [January 12, 2022, 5:47pm UTC](https://fenicsproject.discourse.group/t/problems-with-c-expression/7465/1 "2022-01-12T17:47:16Z")

</div>

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:

```auto
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”

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gebauer](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/emanuel_gebauer/32/3004_2.png) [@Emanuel\_Gebauer](https://fenicsproject.discourse.group/u/Emanuel_Gebauer)\
**Post date:** [January 13, 2022, 7:42am UTC](https://fenicsproject.discourse.group/t/problems-with-c-expression/7465/2 "2022-01-13T07:42:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gebauer](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/emanuel_gebauer/32/3004_2.png) [@Emanuel\_Gebauer](https://fenicsproject.discourse.group/u/Emanuel_Gebauer)\
**Post date:** [January 13, 2022, 9:08am UTC](https://fenicsproject.discourse.group/t/problems-with-c-expression/7465/3 "2022-01-13T09:08:43Z")

</div>

I tested the code: The following code

```auto
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.

---

<div class="post-metadata">

**Author:** ![Francesca\_Ballatore](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/francesca_ballatore/32/2510_2.png) [@Francesca\_Ballatore](https://fenicsproject.discourse.group/u/Francesca_Ballatore)\
**Post date:** [January 13, 2022, 9:47am UTC](https://fenicsproject.discourse.group/t/problems-with-c-expression/7465/4 "2022-01-13T09:47:45Z")

</div>

I use it in this way:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gebauer](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/emanuel_gebauer/32/3004_2.png) [@Emanuel\_Gebauer](https://fenicsproject.discourse.group/u/Emanuel_Gebauer)\
**Post date:** [January 13, 2022, 11:33am UTC](https://fenicsproject.discourse.group/t/problems-with-c-expression/7465/5 "2022-01-13T11:33:05Z")

</div>

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

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![Francesca\_Ballatore](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/francesca_ballatore/32/2510_2.png) [@Francesca\_Ballatore](https://fenicsproject.discourse.group/u/Francesca_Ballatore)\
**Post date:** [January 13, 2022, 11:36am UTC](https://fenicsproject.discourse.group/t/problems-with-c-expression/7465/6 "2022-01-13T11:36:45Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gebauer](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/emanuel_gebauer/32/3004_2.png) [@Emanuel\_Gebauer](https://fenicsproject.discourse.group/u/Emanuel_Gebauer)\
**Post date:** [January 13, 2022, 11:44am UTC](https://fenicsproject.discourse.group/t/problems-with-c-expression/7465/7 "2022-01-13T11:44:35Z")

</div>

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

> <https://stackoverflow.com/questions/4581305/error-while-loading-shared-libraries-libboost-system-so-1-45-0-cannot-open-sha>

I suspect the underlying reason is the same 🙂
