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

How to convert a numpy array to a Vector in dolfin ?

+1 vote
element = MixedElement([P, Q, W])
V = FunctionSpace(mesh, element)
a # a bilinear form
L # a linear form
u = Function(V)**
solve(a == L, u, bc)

But now if I use some other way to solve, like below:
A = assemble(a)
b = assemble(L)
If I get the solution to the problem A*x = b, how can I get u and p,q,w = split(u), plot(u) ?

Thanks very much !!!

asked May 15, 2017 by fanronghong FEniCS User (1,680 points)
edited Jul 1, 2017 by fanronghong

Please format your code by indenting it with four spaces as in my answer

1 Answer

+2 votes
 
Best answer

You can create a Vector from a numpy array 'x' with:

 u = Function(V)
 u.vector().set_local(x)

This assumes that the ordering of the dofs in x is correct. Note that the suffix _local refers to 'local to this process', so you have to be careful when using this function in parallel computations, since the indices of the dofs will be, uh, local ;)

Also see the methods add() (uses global indices), add_local() and apply().

answered May 15, 2017 by mdbenito FEniCS User (4,530 points)
edited May 16, 2017 by mdbenito

Thank you a lot.
It works.

Glad it helped. I've added a couple of comments to the answer.

Thank you for your excellent answer !

Another question: how could I get the more information like add_local() ? I want to learn FeniCS deeply. I have already read the tutorial book.

Thanks again !

I don't know of any comprehensive sources. There is the FEniCS book, of course, but it's a bit outdated and often lacks many details. I read that, google, search these forums and read as much code from others as possible. Many users here provide tons of valuable examples (like user MiroK, I learn a lot from his answers).

...