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

convert eigen::vector into dolfin::function

0 votes

Hi !

I am programming in C++ and using FEniCS for the FE discretization.
Now, I have given an Eigen::VectorXd and I want to transform this vector into a dolfin::function. How can I handle this?

I am very thankful for any help,
frieder

asked Nov 30, 2015 by frieder FEniCS Novice (340 points)

2 Answers

0 votes

I do it this way:

std::shared_ptr<FunctionSpace> V; // this is  your FunctionSpace
Eigen:: VectorXd eigenvec; // this is your vector

EigenVector evec(eigenvec); // convert it into dolfin EigenVector
Vector vec(*(evec.copy())); // convert it into dolfin Vector   
Function u(V,vec.copy()); // convert it into dolfin function 
answered Nov 30, 2015 by str FEniCS User (1,600 points)
0 votes

If you create a dolfin::Function with the function space you want (continuous/discontinuous, correct polynomial degree etc) you can then get the underlying data by calling the vector() method on the function object. The function's FunctionSpace will have a dofmap() method that you can query to find out which element owns which indices in the vector and hence where to put the data you have into a vector that dolfin will know how to handle as a function. Do not assume in your code that the elements of the function vector are ordered in a geometric or other "sensible" way. This will most likely not be the case.

answered Nov 30, 2015 by Tormod Landet FEniCS User (5,130 points)
...