def test_distribution(): nx, ny, nz = 4, 4, 1 nbparts = 4 mesh = MT.HexMesh.make(*GT.grid2hexs(shape=(nx, ny, nz))) cell_color = np.zeros(mesh.nb_cells, dtype="i") for i in range(1, nbparts): cell_color[i * (mesh.nb_cells // nbparts):] += 1 distribution = mesh.distribute(cell_color) node_color = np.empty(mesh.nb_vertices, dtype="i") face_color = np.empty(mesh.nb_faces, dtype="i") for proc, dist in enumerate(distribution): cells, nodes, faces = [v.array_view() for v in dist[0]] ghost_cells_index, ghost_nodes_index, ghost_faces_index = dist[1] node_color[nodes[:ghost_nodes_index]] = proc face_color[faces[:ghost_faces_index]] = proc offsets, cellsnodes = mesh.cells_nodes_as_COC() vtkw.write_vtu( vtkw.vtu_doc_from_COC( mesh.vertices_array(), np.array(offsets[1:], copy=False), # no first zero offset for wtk np.array(cellsnodes, copy=False), mesh.cells_vtk_ids(), pointdata={"color": node_color}, celldata={"color": cell_color}, ), "distribution.vtu", ) offsets, facesnodes = mesh.faces_nodes_as_COC() vtkw.write_vtu( vtkw.vtu_doc_from_COC( mesh.vertices_array(), np.array(offsets[1:], copy=False), # no first zero offset for wtk np.array(facesnodes, copy=False), mesh.faces_vtk_ids(), pointdata={"color": node_color}, celldata={"color": face_color}, ), "distribution_faces.vtu", )
def write_vtu_mesh(simulation, filename, pointdata, celldata, ofmt): nb_own_cells = simulation.number_of_own_cells() vertices, offsets, cellnodes, celltypes = mesh_description( simulation, nb_own_cells) assert all([a.shape[0] == vertices.shape[0] for a in pointdata.values()]) assert all([a.shape[0] >= nb_own_cells for a in celldata.values()]) celldata = {name: a[:nb_own_cells] for name, a in celldata.items()} vtkw.write_vtu( vtkw.vtu_doc_from_COC( vertices, offsets, cellnodes, celltypes, pointdata=pointdata, celldata=celldata, ofmt=ofmt, ), filename, ) return vertices.dtype
def test_hexaedron(): Point = MT.Point Hexa = MT.Hexahedron pts = [ Point((0.0, 0.0, 0.0)), Point((1.0, 0.0, 0.0)), Point((1.0, 1.0, 0.0)), Point((0.0, 1.0, 0.0)), Point((0.0, 0.0, 1.0)), Point((1.0, 0.0, 1.0)), Point((1.0, 1.0, 1.0)), Point((0.0, 1.0, 1.0)), ] hexas = [ Hexa((0, 1, 2, 3, 4, 5, 6, 7)), ] mesh = MT.HybridMesh.Mesh() vertices = mesh.vertices for P in pts: vertices.append(P) cellnodes = mesh.connectivity.cells.nodes for elt in hexas: cellnodes.append(elt) mesh.connectivity.update_from_cellnodes() offsets, cellsnodes = mesh.cells_nodes_as_COC() vtkw.write_vtu( vtkw.vtu_doc_from_COC( mesh.vertices_array(), np.array(offsets[1:], copy=False), # no first zero offset for vtk np.array(cellsnodes, copy=False), mesh.cells_vtk_ids(), ), "hexa.vtu", )
def create_and_distribute_mesh(mesh_constructor, constructor_arguments, coloring_algorithm): # retrieve mesh class from underlying MeshTools module mesh_module_name = mesh_constructor.__module__.split(".")[-1] assert hasattr(MT, mesh_module_name) start = datetime.datetime.now() if rank == 0: global_mesh = mesh_constructor(*constructor_arguments) print("coloring after", (datetime.datetime.now() - start).total_seconds()) cell_color = coloring_algorithm(global_mesh) print("distributing after", (datetime.datetime.now() - start).total_seconds()) distribution = global_mesh.distribute(cell_color) node_color = np.empty(global_mesh.nb_vertices, dtype="i") face_color = np.empty(global_mesh.nb_faces, dtype="i") face_location = [] # we make a first loop to locate faces with relatively to cell for proc, dist in enumerate(distribution): cells, nodes, faces = [v.array_view() for v in dist[0]] # ghost_cells_index, ghost_nodes_index, ghost_faces_index = dist[1] # node_color[nodes[:ghost_nodes_index]] = proc # face_color[faces[:ghost_faces_index]] = proc face_location.append( global_mesh.locate_faces_with_cell(cells, faces)) vertices = global_mesh.vertices_array() cellnodes = global_mesh.connectivity.cells.nodes.raw_array() print( "starting distribution after", (datetime.datetime.now() - start).total_seconds(), ) nbparts = len(distribution) assert nbparts == comm.size for proc in range(1, nbparts): cells, nodes, faces = [ v.array_view() for v in distribution[proc][0] ] # ?? send owns or deduce owns from color ? comm.send( (cells.shape[0], nodes.shape[0], faces.shape[0], cellnodes.shape[1]), dest=proc, tag=11, ) comm.send(distribution[proc][1], dest=proc, tag=111) # send global ids -> assert cells.dtype == MT.idtype() comm.Send([cells, mpi_id_type], dest=proc, tag=12) assert nodes.dtype == MT.idtype() comm.Send([nodes, mpi_id_type], dest=proc, tag=13) assert faces.dtype == MT.idtype() comm.Send([faces, mpi_id_type], dest=proc, tag=14) # send part elements -> assert vertices.dtype == np.double comm.Send([vertices[nodes], np2mpi(np.double)], dest=proc, tag=15) comm.Send([cellnodes[cells], MPI.BYTE], dest=proc, tag=16) face_cell, face_position = face_location[proc] assert face_cell.dtype == MT.idtype() comm.Send([face_cell, mpi_id_type], dest=proc, tag=17) assert face_position.dtype == np.uint8 comm.Send([face_position, np2mpi(np.uint8)], dest=proc, tag=18) # part that stays on master proc cells_gid, nodes_gid, unsorted_faces_gid = [ v.array_view() for v in distribution[0][0] ] ghost_indexes = distribution[0][1] vertices = np.copy(vertices[nodes_gid]) cellnodes = cellnodes[cells_gid] face_cell, face_position = face_location[0] else: nbcells, nbnodes, nbfaces, raw_cell_size = comm.recv(source=0, tag=11) ghost_indexes = comm.recv(source=0, tag=111) # -> receive global ids cells_gid = np.empty((nbcells, ), dtype=MT.idtype()) comm.Recv([cells_gid, mpi_id_type], source=0, tag=12) nodes_gid = np.empty((nbnodes, ), dtype=MT.idtype()) comm.Recv([nodes_gid, mpi_id_type], source=0, tag=13) unsorted_faces_gid = np.empty((nbfaces, ), dtype=MT.idtype()) comm.Recv([unsorted_faces_gid, mpi_id_type], source=0, tag=14) # -> receive part elements vertices = np.empty((nbnodes, 3), dtype=np.double) comm.Recv([vertices, np2mpi(np.double)], source=0, tag=15) # print('proc', comm.rank, ':', vertices.min(axis=0), vertices.max(axis=0)) cellnodes = np.empty((nbcells, raw_cell_size), dtype=np.byte) comm.Recv([cellnodes, MPI.BYTE], source=0, tag=16) face_cell = np.empty((nbfaces, ), dtype=MT.idtype()) comm.Recv([face_cell, mpi_id_type], source=0, tag=17) face_position = np.empty((nbfaces, ), dtype=np.uint8) comm.Recv([face_position, np2mpi(np.uint8)], source=0, tag=18) # Rebuild local meshes print( "rebuilding on", rank, "after", (datetime.datetime.now() - start).total_seconds(), ) Mesh = getattr(MT, mesh_module_name) mesh = Mesh.create_from_remap(vertices, cellnodes, nodes_gid) # CHECKME: invert cell_gid, would this be faster in C++? gid2lid = {cells_gid[k]: k for k in range(cells_gid.shape[0])} local_face_cell = np.array([gid2lid[gci] for gci in face_cell], dtype=MT.idtype()) faces_gid_order = mesh.identify_faces_from_positions( local_face_cell, face_position) faces_gid = unsorted_faces_gid[faces_gid_order] sys.stdout.flush() comm.Barrier() print( "total processing time on", rank, ":", (datetime.datetime.now() - start).total_seconds(), ) offsets, cellsnodes = mesh.cells_nodes_as_COC() ghost_tag = np.zeros(mesh.nb_cells, dtype="i") ghost_tag[ghost_indexes[0]:] = 1 vtkw.write_vtu( vtkw.vtu_doc_from_COC( mesh.vertices_array(), np.array(offsets[1:], copy=False), # no first zero offset for vtk np.array(cellsnodes, copy=False), mesh.cells_vtk_ids(), celldata={"ghost": ghost_tag}, ), "localmesh-%03d.vtu" % rank, ) return mesh
def write_mesh_vtu( self, proc, basename, own_only=True, dump_procid=False, nodedata=None, celldata=None, fracdata=None, ): assert proc < self.distribution.nb_procs nb_own_cells = self.distribution.nb_own_cells[proc] nb_own_fractures = self.distribution.nb_own_fractures[proc] if dump_procid: own_only = True assert nodedata is None and celldata is None and fracdata is None celldata = { "proc": np.array(np.tile(proc, nb_own_cells), dtype=self.proc_id_type) } if nb_own_fractures > 0: fracdata = { "proc": np.array( np.tile(proc, nb_own_fractures), dtype=self.proc_id_type ) } if nodedata is None: nodedata = {} if celldata is None: celldata = {} if fracdata is None: fracdata = {} meshfile = self.mesh_filename(proc) assert os.path.exists(meshfile) mesh = np.load(meshfile) if self.vertices_type is None: self.vertices_type = mesh["vertices"].dtype assert self.vertices_type == mesh["vertices"].dtype proc_label = self.dumper.proc_label(proc) piecefile = self.to_vtu_directory("%s_%s.vtu" % (basename, proc_label)) cell_nodes_offsets = mesh["cellnodes_offsets"] cell_nodes = mesh["cellnodes_values"] cell_types = mesh["celltypes"] if own_only: cell_nodes_offsets = cell_nodes_offsets[:nb_own_cells] cell_nodes = cell_nodes[: cell_nodes_offsets[-1]] cell_types = cell_types[:nb_own_cells] vtkw.write_vtu( vtkw.vtu_doc_from_COC( mesh["vertices"], cell_nodes_offsets, cell_nodes, cell_types, pointdata=nodedata, celldata=celldata, ), piecefile, ) if fracdata: fracdata_size = int( np.unique([a.shape[0] for a in fracdata.values()]) ) # will triger a TypeError if array sizes are not the same if fracdata_size > 0: fracpiecefile = self.to_vtu_directory( "fracture_%s_%s.vtu" % (basename, proc_label) ) cell_nodes_offsets = mesh["fracturenodes_offsets"] cell_nodes = mesh["fracturenodes_values"] cell_types = mesh["fracture_types"] if own_only: cell_nodes_offsets = cell_nodes_offsets[:nb_own_fractures] cell_nodes = cell_nodes[: cell_nodes_offsets[-1]] cell_types = cell_types[:nb_own_fractures] vtkw.write_vtu( vtkw.vtu_doc_from_COC( mesh["vertices"], cell_nodes_offsets, cell_nodes, cell_types, celldata=fracdata, ), fracpiecefile, ) return piecefile, fracpiecefile return piecefile, None
paris["faces_nodes_top"] = arnul.faces_nodes_affl paris["faces_nodes_bot"] = arnul.faces_nodes_bot paris["cells_layers"] = arnul.cells_layers paris["cells_top"] = arnul.cells_affls paris["cells_bot"] = arnul.cells_bots with open("bassin_paris/meshs.pkl", "bw") as fichier: pickle.dump(paris, fichier) # Représentation sous vtk : for poly in paris["cells_nodes"]: mesh = MT.HybridMesh.Mesh() vertices = mesh.vertices for P in paris["vertices"]: vertices.append(MT.Point(P)) cellnodes = mesh.connectivity.cells.nodes for elt in paris["cells_nodes"][poly]: cellnodes.append(typ[poly](MT.idarray(elt))) mesh.connectivity.update_from_cellnodes() offsets, cellsnodes = mesh.cells_nodes_as_COC() vtkw.write_vtu( vtkw.vtu_doc_from_COC( mesh.vertices_array(), np.array(offsets[1:], copy=False), # no first zero offset for vtk np.array(cellsnodes, copy=False), mesh.cells_vtk_ids(), ), "{0}.vtu".format(poly), ) # python postprocess_snapshots -s output-test-bass_paris