Error: Implementation of f.vector()[:] in C++

I am having trouble with implementing f.vector()[:] in C++.

In my calculation, instead of an analytical source f=Expression(‘function of x[0] and x[1]’) , I want this vector as a source for all my N nodal points. So I use the following script in python to implement the above problem

F = FunctionSpace(mesh, ‘CG’, 1) # so that dofs are only in mesh vertices
f = Function(F)
n_vertices=mesh.num_vertices()#number of vertices in a mesh
vertex_values = np.zeros(mesh.num_vertices())#Putting zeros to the number of vertics
for vert in vertices(mesh):
vertex_values[vert.index()] = vert.index()
f.vector()[:] = vertex_values[dof_to_vertex_map(F)]

plot(f,“Source Term”)

I have tried to implement the same with C++, So I have used the following script

int n_vertices1=mesh.num_vertices();
Array vect(n_vertices1);
for( VertexIterator vert(mesh); !vert.end(); ++vert){
vect[vert->index()]= vert->index();
for(int i=0; i<n_vertices1; i++){
plot(f,“Source term”);

But I am not able to match the source term plot. I think there is problem in converting f.vector()[:] to C++.

Could you please help in that


Did you find any solution of your problem? I am working on the same topic.

Yes Rishi I found the solution.

There are coupe of things you need to do.

  1. Use docker to compile your code, if you are using Fenics.
  2. Use an arrow dot operator(->) to assign to your object such as ‘f.vector()->set_local(vect1);’.

Then it will work perfectly.


Great! Thank you! :slight_smile: