How to save solution in .txt format?

I wrote the following code. I am wondering how do I save the solutions in .txt format? Can anyone please help me ?

from __future__ import print_function
from fenics import *
from dolfin import *
import matplotlib.pyplot as plt
import numpy as np

# Create mesh and define the function space
mesh = RectangleMesh(Point(0, 0), Point(1, 2), 10, 10)
V = FunctionSpace(mesh, "CG", 1)
dof_coordinates = V.tabulate_dof_coordinates()

# Define Boundary Condition
def top_boundary(x, on_boundary):
    return on_boundary and near(2, x[1])


def bottom_boundary(x, on_boundary):
    return on_boundary and near(0, x[1])


def left_boundary(x, on_boundary):
    return on_boundary and near(x[0], 0)


def right_boundary(x, on_boundary):
    return on_boundary and near(x[0], 1)


# Define boundary conditions
u_D_x_0 = Expression('x[0]*x[0]', degree=2)
u_D_x_2 = Expression('(x[0]-2)*(x[0]-2)', degree=2)
u_D_0_y = Expression('x[1]*x[1]', degree=2)
u_D_1_y = Expression('(x[1]-1)*(x[1]-1)', degree=2)

bc_top = DirichletBC(V, u_D_x_2, top_boundary)
bc_bottom = DirichletBC(V, u_D_x_0, bottom_boundary)
bc_left = DirichletBC(V, u_D_0_y, left_boundary)
bc_right = DirichletBC(V, u_D_1_y, right_boundary)
bcs = [bc_left, bc_right, bc_bottom, bc_top]

# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f= Constant(-4)
a = dot(grad(u), grad(v))*dx
L = f*v*dx


# Compute solution
u = Function(V)
solve(a == L, u, bcs)
#print('Solution=',u)
print(dof_coordinates)
print(u.vector().get_local())

u_nodal_values = u.vector()
u_array = u.vector()

coor = mesh.coordinates()
if mesh.num_vertices() == len(u_array):
    for i in range(mesh.num_vertices()):
        print ('u(%f,%f)=%f',coor[i][0], coor[i][2], u_array[i])


# Plot solution and mesh

plot(u)
plot(mesh, title="Solution to Poisson Equation")
plt.show()


# Save solution to file in VTK format
vtkfile = File('poisson/solution.pvd')
vtkfile << u

What do you imply by saving the solution in txt format?
As txt format is simply a file with text in it, you can use python’s built in functions to write the data as you wnant to order it. See for instance
https://docs.python.org/3.6/tutorial/inputoutput.html#reading-and-writing-files

Basically, I want to save the text file which contain the coordinate of the mesh and the solution corresponding to that coordinate.

See for instance: How to get the solution obatained by fenics as a vector? - #7 by dokken

What is V.num_sub_space?
num_dofs_per_component = int(V.dim()/V.num_sub_spaces())

ZeroDivisionError: division by zero

If you are using a scalar space, simply look at:

the $i$th entry in dof_coordinates corresponds to the coordinate of the $i$th degree of freedom, which is the $i$th entry of u.vector().get_local()

I already printed the dof_coordinates and u.vector().get_local(). I want to save them in the .txt file format. Like first coordinate and then solution corresponding to that coordinate.

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

Thank you, it’s working. I also want to calculate L2 error by using error norm function.
In this code, I have different boundaries, so how can I calculate the L2 error by using
error_L2 = errornorm(u_D, u, 'L2'). ?

What do you mean by different boundaries? Please supply a minimal code example as described in Read before posting: How do I get my question answered?