Pyvista, how to gather data to plot on rank=0

Hello,

I am trying to plot function self.T calculated in parallel, but I am not able to gather the data correctly to rank=0. When run in parallel each rank produces correct chunk of the plot, and the solution is also correct when viewed in Paraview through xdmf file.

I have this function (domain is mesh):

def plot(self):
        cells, types, x = plot.create_vtk_mesh(self.V)
        cells = self.domain.comm.gather(cells, root=0)
        types = self.domain.comm.gather(types, root=0)
        x = self.domain.comm.gather(x, root=0)
        Txarray = self.domain.comm.gather(self.T.x.array, root=0)

        if MPI.COMM_WORLD.rank == 0:
            cells = np.hstack(cells)
            types = np.hstack(types)
            x = np.vstack(x)
            Txarray = np.hstack(Txarray)
            grid = pyvista.UnstructuredGrid(cells, types, x)
            grid.point_data["u"] = Txarray
            plotter = pyvista.Plotter()
            plotter.add_mesh(grid, show_edges=True)
            plotter.view_xy()
            plotter.show()

The plot it produces looks like this, I think I need to reorder something, but I do not not which of the arrays is wrong.

It should look like this (run with mpirun -n 1):

What I am missing?
Thank you, for any help.

.

See: How to collect the global mesh without writing a file - #4 by dokken

1 Like

Thank you very much @dokken. It works perfectly.

def plot(self):
        cells, types, x = plot.create_vtk_mesh(self.V)
        num_cells_local = self.domain.topology.index_map(self.domain.topology.dim).size_local
        num_dofs_local = self.V.dofmap.index_map.size_local * self.V.dofmap.index_map_bs
        num_dofs_per_cell = cells[0]
        cells_dofs = (np.arange(len(cells)) % (num_dofs_per_cell+1)) != 0
        global_dofs = self.V.dofmap.index_map.local_to_global(cells[cells_dofs].copy())
        cells[cells_dofs] = global_dofs

        root = 0
        global_cells = self.domain.comm.gather(cells[:(num_dofs_per_cell+1)*num_cells_local], root=root)
        global_types = self.domain.comm.gather(types[:num_cells_local])
        global_x = self.domain.comm.gather(x[:self.V.dofmap.index_map.size_local,:], root=root)
        global_vals = self.domain.comm.gather(self.T.x.array[:num_dofs_local], root=root)

        if MPI.COMM_WORLD.rank == 0:
            root_x = np.vstack(global_x)
            root_cells = np.concatenate(global_cells)
            root_types = np.concatenate(global_types)
            root_vals = np.concatenate(global_vals)

            grid = pyvista.UnstructuredGrid(root_cells, root_types, root_x)
            grid.point_data["u"] = root_vals
            grid.set_active_scalars("u")
            plotter = pyvista.Plotter()
            plotter.add_mesh(grid, show_edges=False)
            plotter.view_xy()
            plotter.show()