This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Cannot use containers of Constants in Forms

0 votes

Dear all,

I am trying to use C++ containers of dolfin::Container in a form, but I am not succeeding.

This is the self-contained example code

So straight to the culprit. Here are the lines that you can analyze. I create a local variable, a hashmap of constants, and a vector of constants. The only working thing is the local variable.

  // Bulk loads
  dolfin::Constant f_bulk(0.0, 0.0);

  // Map of constants
  std::unordered_map<std::string, dolfin::Constant> maptesting;

  // Vector of constants
  std::vector<dolfin::Constant> vectesting;

  maptesting.emplace("abc", dolfin::Constant(-3.1415));
  vectesting.emplace_back(dolfin::Constant(-3.1415));

  L.f = f_bulk;
  //L.f = maptesting["abc"];
  //L.f = vectesting.back();

When using the unordered_map I obtain a compilation error:

// LET'S USE THIS
L.f = maptesting["abc"];

memory:1731:31: error: 
      no matching constructor for initialization of 'dolfin::Constant'
            ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
[...]

I've tried const dolfin::Constant, but nothing helps. You can find here the complete log.

Also, if I use a std::vector, it compiles but fails to run:

// LET'S USE THIS
L.f = vectesting.back();

sensei@Senseis-MacBook-Pro:tmp$ make
Scanning dependencies of target fenics_cpp
[100%] Building CXX object CMakeFiles/fenics_cpp.dir/main.cpp.o
Linking CXX executable fenics_cpp
[100%] Built target fenics_cpp

sensei@Senseis-MacBook-Pro:tmp$ ./fenics_cpp 
NUM PARAMETERS: 1
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: 

*** -------------------------------------------------------------------------
*** 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@fenicsproject.org
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error:   Unable to assemble form.
*** Reason:  Invalid value rank for coefficient 0 (got 0 but expecting 1). You might have forgotten to specify the value rank correctly in an Expression subclass.
*** Where:   This error was encountered inside AssemblerBase.cpp.
*** Process: unknown
*** 
*** DOLFIN version: 1.5.0
*** Git changeset:  f467b66dcfd821ec20e9f9070c7cef5a991dbc42
*** -------------------------------------------------------------------------

Is it possible to use containers with forms? I am using containers to define DirichletBC objects, and they seem to work.

Thanks!

asked Mar 24, 2016 by senseiwa FEniCS User (2,620 points)

1 Answer

0 votes
 
Best answer

The same bug using temporaries applies to this, with a std::shared_ptr should work (I filed a bug report).

answered Mar 30, 2016 by senseiwa FEniCS User (2,620 points)
...