How to get the solution obatained 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)


Thank you so much for your answer

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


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)

Ok, thank you. I solved the problem.