I would like to save the mesh connectivity as a .txt file. I tried to gather the information to the main processor then tried to save from there. But it turns out I have made some mistakes, some rows are repeated in my output files. What mistakes did I make?
from __future__ import print_function
from dolfin import *
from ufl import nabla_div
import numpy as np
from mpi4py import MPI
mesh = BoxMesh(Point(-1, -1, -1), Point(1, 1, 1), 10, 10, 10)
V = VectorFunctionSpace(mesh, 'P', 1)
connectivity = mesh.cells()
comm = MPI.COMM_WORLD
gathered_connectivity = comm.gather(connectivity, root=0)
global_eldex = comm.gather(mesh.topology().global_indices(3))
if comm.rank==0:
num_elements = mesh.num_entities_global(3)
all_connectivity = np.zeros((num_elements, connectivity.shape[1]))
for conn, eldex in zip(gathered_connectivity, global_eldex):
all_connectivity[eldex] = conn
np.savetxt('mesh_connectivity_test1.txt', all_connectivity, fmt='%8i')
And some rows from my output txt file are:
0 1 2 3
0 1 4 3
0 5 4 3
0 6 2 3
0 5 7 3
0 6 7 3
1 8 9 10
1 8 11 10
1 4 11 10
1 2 9 10
1 4 3 10
1 2 3 10
8 12 13 14
8 12 15 14
8 11 15 14
8 9 13 14
8 11 10 14
8 9 10 14
12 16 17 18
12 16 19 18
12 15 19 18
12 13 17 18
12 15 14 18
12 13 14 18
16 20 21 22
16 20 23 22
16 19 23 22
16 17 21 22
16 19 18 22
16 17 18 22
0 1 2 3 (repeat the first row)
0 1 4 3
0 5 4 3
…