# How to get the solution obtained by fenics as a vector?

How to get the solution obatained by fenics as a vector if it is possible ?

Consider the following minimal example:

``````from dolfin import *

mesh = UnitSquareMesh(2, 2)
V = FunctionSpace(mesh, "CG", 2)
dof_coordinates = V.tabulate_dof_coordinates()

expr = Expression("x[0]", degree=1)
uh = Function(V)
uh.interpolate(expr)

print(dof_coordinates)
print(uh.vector().get_local())
``````

Mustapha

Hello,
i tried but the problem is how to get uh vector from the solution u obtained by: solve(a == f, u, bc) without using expression “expr = Expression(“x[0]”, degree=1)”?
Thank you

`print(u.vector().get_local())`

yes, but it does not work for 2D!! i want to store the coefficients in a vector

Please make a minimal example, as your original question is not clear on what kind of vector you would like to use. What you can do is to get each of the components of a `VectorFunctionSpace` by collapsing each of its components.

``````from dolfin import *
import numpy as np
mesh = UnitSquareMesh(10, 10)
V = VectorFunctionSpace(mesh, "CG", 1)
u = Function(V)
u.interpolate(Expression(("3*x[0]", "2*x[1]"), degree=1))
num_dofs_per_component = int(V.dim()/V.num_sub_spaces())
num_sub_spaces = V.num_sub_spaces()
print(num_dofs_per_component, num_sub_spaces)
vector = np.zeros((num_sub_spaces, num_dofs_per_component))
for i in range(num_sub_spaces):
vector[i] = u.sub(i, deepcopy=True).vector().get_local()
x = V.sub(0).collapse().tabulate_dof_coordinates()
vector = vector.T
for coord, vec in zip(x, vector):
print(coord, vec)
``````
1 Like

Ok, thank you. I solved the problem.

Mustapha

Hi, is there a way to do this in parallel as well? If I try: `mpirun -n 2 python fenics_example.py`, where fenics_example.py is your working example I get the following error:

``````  File "fenics_example.py", line 19, in <module>
vector[i] = u.sub(0).sub(i, deepcopy=True).vector().get_local()
ValueError: could not broadcast input array from shape (62,) into shape (121,)
Traceback (most recent call last):
File "fenics_example.py", line 19, in <module>
vector[i] = u.sub(0).sub(i, deepcopy=True).vector().get_local()
ValueError: could not broadcast input array from shape (59,) into shape (121,)

``````

Replace

with

``````num_dofs_per_component = int(len(u.vector().get_local())/V.num_sub_spaces())
``````
1 Like

I am trying to reproduce the result for a mixed function space. I am interested only on the vector on the first function space. This code runs, is it also correct?

``````from dolfin import *
import numpy as np
mesh = UnitSquareMesh(10, 10)
V = VectorElement("CG", mesh.ufl_cell(),1)
M = MixedElement([V,V])
VV = FunctionSpace(mesh, M)
# V = VectorFunctionSpace(mesh, "CG", 1)
u = Function(VV)
expr = Expression(("3*x[0]", "2*x[1]"), degree=1)
u0 = interpolate(expr, VV.sub(0).collapse())
u1 = interpolate(expr, VV.sub(1).collapse())
assign(u, [u0,u1])
num_dofs_per_component = int(len(u.sub(0, deepcopy=True).vector().get_local())/VV.sub(0).num_sub_spaces())
# num_dofs_per_component = int(VV.sub(0).dim()/VV.sub(0).num_sub_spaces())
num_sub_spaces = VV.sub(0).num_sub_spaces()
print(num_dofs_per_component, num_sub_spaces)
# print(VV.sub(0).sub(0).dim(), 2)
vector = np.zeros((num_sub_spaces, num_dofs_per_component))
for i in range(num_sub_spaces):
vector[i] = u.sub(0).sub(i, deepcopy=True).vector().get_local()
x = VV.sub(0).sub(0).collapse().tabulate_dof_coordinates()
vector = vector.T
for coord, vec in zip(x, vector):
print(coord, vec)
``````

I think it is correct, but it would be easier to do:
`vel= u.sub(0,deepcopy=True)` and treat `vel` and `vel.function_space()` as above.

1 Like

thanks ,but i already do this and i recieve a message like that:

“ZeroDivisionError: division by zero” i don’t know how can i do

Without a minimal code no one can give you any guidance

1 Like