How to save solution in .txt format?

from dolfin import *
mesh = UnitSquareMesh(2, 2)
V = FunctionSpace(mesh, "CG", 1)
u = project(Expression("x[0]*x[1]", degree=1), V)
coords = V.tabulate_dof_coordinates()
vec = u.vector().get_local()
outfile = open("output.txt", "w")
for coord, val in zip(coords, vec):
    print(coord[0], coord[1], val, file=outfile)

gives a file called output.txt the following data

0.0 0.5 4.57388497314e-17
0.5 1.0 0.5
0.0 0.0 -2.08166817117e-17
0.5 0.5 0.25
1.0 1.0 1.0
0.5 0.0 4.46071750965e-17
1.0 0.5 0.5
1.0 0.0 -4.16333634234e-17
1 Like