I want to load an external tetrahedral mesh into dolfinx by loading an array containing the vertex coordinates as well as an array specifying the connectivity. For my application it is important, that the order of the vertices is preserved, while loading them into dolfinx. I.e., that the vertices are stored in mesh.geometry.x
in the same order as in the input array.
But unfortunately, dolfinx seems to change the order. Cf. the following minimal working example:
import numpy as np
from mpi4py import MPI
import dolfinx as dfx # type: ignore
import basix.ufl
#Function for generating a dolfinx mesh from connectivity
def mesh_from_connectivity(cells, vertices):
cell_type = "tetrahedron"
degree = 1
domain = dfx.mesh.ufl.Mesh(basix.ufl.element("Lagrange", cell_type, degree, basix.LagrangeVariant.equispaced, shape=(3, )))
return dfx.mesh.create_mesh(MPI.COMM_WORLD, cells, vertices, domain)
#Connectivity array of 5-tetrahedra unitcube, see e.g. https://cecas.clemson.edu/cvel/modeling/EMAG/EMAP/emap4/Meshing.html
UC_vertices=np.array([[1,0,0],[1,1,0],[0,0,0],[0,1,0],[1,0,1],[1,1,1],[0,0,1],[0,1,1]], dtype = float)
UC_cells = np.array([[0,1,3,5],[0,5,6,4],[0,3,2,6],[6,7,5,3],[6,5,0,3]])
#Generate dolfinx mesh for 5-tetrahedra unitcube
mesh = mesh_from_connectivity(UC_cells, UC_vertices)
print("Vertex coordinates of dolfinx mesh:")
print(mesh.geometry.x)
print("Original vertex coordinate array:")
print(UC_vertices)
Which produces the output
Vertex coordinates of dolfinx mesh:
[[1. 0. 0.]
[1. 1. 0.]
[0. 1. 0.]
[1. 1. 1.]
[0. 0. 1.]
[0. 1. 1.]
[0. 0. 0.]
[1. 0. 1.]]
Original vertex coordinate array:
[[1. 0. 0.]
[1. 1. 0.]
[0. 0. 0.]
[0. 1. 0.]
[1. 0. 1.]
[1. 1. 1.]
[0. 0. 1.]
[0. 1. 1.]]
Is there a way to preserve the order of the vertices, when one loads a dolfinx-mesh from arrays?