This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How can I copy part of a mesh while preserving cell orientations

0 votes

Hello,

When I copy the local part of a distributed mesh, I notice that the cell orientations do not match those of the original mesh. The following is a code snippet that demonstrates the effect.

from dolfin import *

tdim = 2
comm = mpi_comm_world()
mpirank = MPI.rank(comm)

ny = 4
mesh = UnitSquareMesh(ny-1, ny-1)

# Copy local parts of mesh
mesh_local = Mesh(mpi_comm_self())
mesh_editor = MeshEditor()
mesh_editor.open(mesh_local, tdim, tdim)
mesh_editor.init_vertices(mesh.num_vertices())
mesh_editor.init_cells(mesh.num_cells())
used_verts = []
for ci in range(mesh.num_cells()):

    c = Cell(mesh,ci)

    vs = []
    for v in vertices(c):
        if v.index() not in used_verts:
            mesh_editor.add_vertex(v.index(), v.x(0), v.x(1))
            used_verts.append(v.index())
        vs.append(v.index())        
    mesh_editor.add_cell(ci, vs[0], vs[1], vs[2])

mesh_editor.close()

for ci in range(mesh_local.num_cells()):

    cg = Cell(mesh, ci)
    cl = Cell(mesh_local, ci)

    if mpirank == 0:
        print mpirank, cg.orientation(), cl.orientation()

Is there a different way to get the local mesh while preserving the orientations? Or a way to change the orientations after copying?

Thanks,
Ben

asked Nov 24, 2016 by brk888 FEniCS Novice (990 points)

It's really not clear to me what you are doing with mesh_local. When running in parallel the mesh object is automatically split so that on each process the mesh object is already the local parts. If you really want to create a copy of this you could also just use

mesh_local = Mesh(mesh)

rather than copying the vertices and cells manually via the mesh editor.

That said, it is strange that your manual copying of the mesh does seem to end up with different orientations. If I go through and print the cell index's of mesh_local after you have done the copy it would appear that the add_cell function re-orders the indices in ascending order. The orientation then changes according to the new vertex ordering. Thus the orientations aren't really different, it just reflects the ordering of the vertices stored in the cell. I personally feel the fact that add_cell doesn't preserve the vertex ordering it is passed is a bad design choice, but perhaps it is done for performance reasons.

Hello,

Thank you for your answer, I found a way to preserve the orientations by using add_vertex_global, since the orientations are based on the global numbering scheme.

...