Hi,everyone !
I have implemented serialization to output grid points, function values, and grid topology as CSV files.
code are as follows:
import dolfinx
import numpy as np
from mpi4py import MPI
msh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 2, 2)
V = dolfinx.fem.VectorFunctionSpace(msh, ("Lagrange", 1))
u = dolfinx.fem.Function(V)
u.interpolate(lambda x: (x[0], 2*x[1]))
###############################################################插值结果的输出 结构为: x y u v
dof_coordinates =V.tabulate_dof_coordinates()
num_local_dofs = V.dofmap.index_map.size_local
out_filename = 'OUT.csv'
out = open(out_filename, "w")
for x, val in zip(dof_coordinates, u.x.array.reshape(-1,2)):
print(f"{x[0]:.5f},{x[1]:.5f},{val[0]:.8f},{val[1]:.8f} ", file=out)
out.close()
##############################
msh.topology.create_connectivity(2,0)
msh.topology.create_connectivity(1,0)
msh.topology.create_connectivity(2,1)
msh.topology.create_connectivity(1,2)
>
out_filename = 'topology2_0.csv'
topology_data=msh.topology.connectivity(2,0).links
out = open(out_filename, "w")
for nodes in range(msh.topology.connectivity(2,0).num_nodes):
print(f" {nodes},{','.join(map(str,topology_data(nodes)))} ", file=out)
out.close()
results:
But parallel operation encountered a problem: the nodes were arranged randomly:
This causes the topology structure to be unusable.Because the rows represent the node numbers
code are as follows:
import dolfinx
import numpy as np
from mpi4py import MPI
msh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 2, 2)
V = dolfinx.fem.VectorFunctionSpace(msh, ("Lagrange", 1))
u = dolfinx.fem.Function(V)
u.interpolate(lambda x: (x[0], 2*x[1]))
size =MPI.COMM_WORLD.Get_size()
dof_coordinates = V.tabulate_dof_coordinates()
num_local_dofs = V.dofmap.index_map.size_local
rank=MPI.COMM_WORLD.Get_rank()
out_filename = 'out'+'rank_'+str(rank)+'.csv'
out = open(out_filename, "w")
for x, val in zip(dof_coordinates[:num_local_dofs, :], u.x.array.reshape(-1,2)[:num_local_dofs, :]):
print(f"{x[0]:.5f},{x[1]:.5f},{val[0]:.8f},{val[1]:.8f} ", file=out)
out.close()
results: