I’d like to pyvista-plot my mesh, when running on multiple processors, with a flag for showing mesh partitioning or not (the ‘or not’ is where I struggle).
I am following How to collect the global mesh without writing a file - #3 by edgar and Pyvista, how to gather data to plot on rank=0 - #3 by LiborKudela, but would like to avoid creating a FunctionSpace if not necessary. So I got the following MWE:
import pyvista,dolfinx
show_partitioning = True
domain = dolfinx.mesh.create_rectangle(MPI.COMM_WORLD,[(0,0),(1,1)], (10,10))
topology, cell_types, geometry = dolfinx.plot.vtk_mesh(domain)
num_cells_local = domain.topology.index_map(domain.topology.dim).size_local
num_cells_local_geom = domain.geometry.index_map().size_local
# Gather data
num_dofs_per_cell = topology[0]
global_topology = domain.comm.gather(
topology[: (num_dofs_per_cell + 1) * num_cells_local], root=0
)
if show_partitioning:
global_geometry = domain.comm.gather(geometry[:, :], root=0)
else:
# FAULTY LINE IS HERE:
global_geometry = domain.comm.gather(geometry[:num_cells_local_geom, :], root=0)
global_ct = domain.comm.gather(cell_types[:num_cells_local], root=0)
if domain.comm.rank == 0:
root_geoms = [np.vstack(global_geometry)] if not show_partitioning else global_geometry
root_tops = [np.concatenate(global_topology)] if not show_partitioning else global_topology
root_cts = [np.concatenate(global_ct)] if not show_partitioning else global_ct
plotter = pyvista.Plotter()
colors = ['k','r','b','g']
for color, root_top, root_ct, root_geom in zip(colors,root_tops, root_cts, root_geoms):
grid = pyvista.UnstructuredGrid(root_top, root_ct, root_geom)
plotter.add_mesh(grid, style="wireframe", color=color, line_width=2)
plotter.view_xy()
plotter.show()
This works nicely for --np 1
(obviously) and works nicely with --np 2
and show_partitioning = True
, but shows something a wrong with --np 2
and show_partitioning = False
.
I know the error must be in one particular line (highlighted in the MWE), but can’t figure out what it should be. A hint is much appreciated!