You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Copyright (C) 2023 Jørgen S. Dokken## SPDX-License-Identifier: MIT## Code to plot mesh with pyvista in parallel by gather data on a root process## Last changed: 2023-10-23importdolfinxfrommpi4pyimportMPIimportpyvistaimportnumpyasnppyvista.start_xvfb(0.2)
mesh=dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
V=dolfinx.fem.functionspace(mesh, ("Lagrange", 2))
u=dolfinx.fem.Function(V)
u.interpolate(lambdax: x[0] +2*x[1]**2)
# Create local VTK mesh data structurestopology, cell_types, geometry=dolfinx.plot.vtk_mesh(V)
num_cells_local=mesh.topology.index_map(mesh.topology.dim).size_localnum_dofs_local=V.dofmap.index_map.size_local*V.dofmap.index_map_bs# VTK topology stored as (num_dofs_cell_0, dof_0,.... dof_(num_dofs_cell_0, num_dofs_cell_1, ....))# We assume we only have one cell type and one dofmap, thus every `num_dofs_per_cell` is the samenum_dofs_per_cell=topology[0]
# Get only dof indicestopology_dofs= (np.arange(len(topology)) % (num_dofs_per_cell+1)) !=0# Map to global dof indicesglobal_dofs=V.dofmap.index_map.local_to_global(topology[topology_dofs].copy())
# Overwrite topologytopology[topology_dofs] =global_dofs# Choose rootroot=0# Gather dataglobal_topology=mesh.comm.gather(topology[:(num_dofs_per_cell+1)*num_cells_local], root=root)
global_geometry=mesh.comm.gather(geometry[:V.dofmap.index_map.size_local,:], root=root)
global_ct=mesh.comm.gather(cell_types[:num_cells_local])
global_vals=mesh.comm.gather(u.x.array[:num_dofs_local])
ifmesh.comm.rank==root:
# Stack dataroot_geom=np.vstack(global_geometry)
root_top=np.concatenate(global_topology)
root_ct=np.concatenate(global_ct)
root_vals=np.concatenate(global_vals)
# Plot as in serialpyvista.OFF_SCREEN=Truegrid=pyvista.UnstructuredGrid(root_top, root_ct, root_geom)
grid.point_data["u"] =root_valsgrid.set_active_scalars("u")
u_plotter=pyvista.Plotter()
u_plotter.add_mesh(grid, show_edges=True)
u_plotter.view_xy()
u_plotter.screenshot("figure.png")
The text was updated successfully, but these errors were encountered:
Gather all data on one process, as shown in:
https://fenicsproject.discourse.group/t/how-to-collect-the-global-mesh-without-writing-a-file/12445/4?u=dokken
The text was updated successfully, but these errors were encountered: