def explore_mesh(mesh, name): print("Nb vertices:", mesh.nb_vertices) print("Nb cells:", mesh.nb_cells) print("Nb faces:", mesh.nb_faces) print("Vertices") print_collection(mesh.vertices) print("Cell nodes") print_collection(mesh.connectivity.cells.nodes) print("Cell face") print_collection(mesh.connectivity.cells.faces) print("Face nodes") print_collection(mesh.connectivity.faces.nodes) print("Boundary faces:", mesh.boundary_faces()) fcenters = MT.as_coordinate_array(mesh.face_centers()) print("All face centers: (with shape", fcenters.shape, ")") print(fcenters) ccenters = MT.as_coordinate_array(mesh.cell_centers()) print("All cell centers: (with shape", ccenters.shape, ")") print(ccenters) vertices = MT.as_coordinate_array(mesh.vertices) cellnodes = np.array( [np.array(cell) for cell in mesh.connectivity.cells.nodes]) print(cellnodes) vtkw.write_vtu(vtkw.vtu_doc(vertices, cellnodes), name + ".vtu") vtkw.write_vtu( vtkw.vtu_doc(fcenters, np.reshape(np.arange(fcenters.shape[0]), (-1, 1))), name + "_face_centers.vtu", )
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},
def retrieve(filename, dtype): return np.loadtxt(os.path.join(dirname, filename), dtype=dtype) vertices = retrieve("vertices", np.double) cells = retrieve("cells", MT.idtype()) faces = retrieve("faces", MT.idtype()) fault_faces = retrieve("faces_fault", MT.idtype()) fault_faces_nodes = faces[fault_faces] mesh = MT.tetmesh(vertices, cells) fault_faces_id = mesh.faces_ids(fault_faces_nodes) vtkw.write_vtu(vtkw.vtu_doc(vertices, cells), os.path.join(dirname, "mesh.vtu")) ffcenters = mesh.face_centers(fault_faces_id) vtkw.write_vtu( vtkw.vtu_doc(ffcenters, np.reshape(np.arange(ffcenters.shape[0]), (-1, 1))), os.path.join(dirname, "fault_faces_centers.vtu"), ) fcenters = mesh.face_centers(mesh.faces_ids(faces)) patches = retrieve("patches", np.int) assert fcenters.shape[0] == patches.shape[0] vtkw.write_vtu( vtkw.vtu_doc(
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]) 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
import numpy as np import vtkwriters as vtkw vtkw.write_vtu( vtkw.polyhedra_vtu_doc( vertices=np.array( [ (-1, -1, 0), (1, -1, 0), (0, 1, 0), (0, 0, 1), (0, -1, 0), ], dtype="d", ), cells_faces=[[ (0, 4, 1, 2), (0, 1, 3), (1, 2, 3), (2, 0, 3), ]], ), "tet_as_polyhedron", )
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 = MeshTools.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", )
import numpy as np import MeshTools as MT import GridTools as GT import vtkwriters as vtkw vertices, cells = GT.grid2hexs((3, 2, 4)) cells = np.ascontiguousarray(cells, dtype=MT.idtype()) mesh = MT.HexMesh.make(vertices, cells) vertices = MT.as_coordinate_array(mesh.vertices) cellnodes = np.array( [np.array(nodes) for nodes in mesh.connectivity.cells.nodes]) vtkw.write_vtu(vtkw.vtu_doc(vertices, cellnodes), "hexs.vtu")
vertices[j * nr:(j + 1) * nr, 1] = y[0] for k, yk in enumerate(y[1:]): vertices[(k + 1) * (nr * ntheta):(k + 2) * (nr * ntheta)] = vertices[k * (nr * ntheta):(k + 1) * (nr * ntheta)] vertices[(k + 1) * (nr * ntheta):(k + 2) * (nr * ntheta), 1] = yk ncr, ncth, ncy = nr - 1, nsectors, nslices ncells = ncr * ncth * ncy cells = np.zeros((ncells, 8), dtype=MT.idtype()) tmp = np.arange(ncr) cells[:ncr, 0] = tmp cells[:ncr, 1] = tmp + 1 cells[:ncr, 2] = tmp + 1 + nr cells[:ncr, 3] = tmp + nr for j in range(1, ncth - 1): cells[j * ncr:(j + 1) * ncr, :4] = cells[(j - 1) * ncr:j * ncr, :4] + nr cells[(ncth - 1) * ncr:ncth * ncr, :2] = cells[(ncth - 2) * ncr:(ncth - 1) * ncr, 3:1:-1] cells[(ncth - 1) * ncr:ncth * ncr, 2:4] = cells[:ncr, 1::-1] cells[:(ncr * ncth), 4:] = cells[:(ncr * ncth), :4] + nr * ntheta for k in range(1, ncy): cells[k * (ncr * ncth):(k + 1) * (ncr * ncth), :] = (cells[(k - 1) * (ncr * ncth):k * (ncr * ncth), :] + nr * ntheta) mesh = MT.hexmesh(vertices, cells) vtkw.write_vtu(vtkw.vtu_doc(mesh.vertices, mesh.cellnodes), "andra.vtu")
mesh = MT.TetMesh.make(vertices, MT.idarray(tets)) print("Nb vertices:", mesh.nb_vertices) print("Nb cells:", mesh.nb_cells) print("Nb faces:", mesh.nb_faces) def print_collection(iterable): for item in iterable: print(item) print("Vertices") print_collection(mesh.vertices) print("Cell face") print_collection(mesh.connectivity.cells.faces) print("Cell nodes") print_collection(mesh.connectivity.cells.nodes) print("Face nodes") print_collection(mesh.connectivity.faces.nodes) print("All cell centers:") print_collection(mesh.cell_centers()) vtkw.write_vtu( vtkw.vtu_doc(vertices, tets, celldata={"id": np.arange(1, tets.shape[0] + 1)}), "cubes.vtu", )
import numpy as np import vtkwriters as vtkw vertices = np.array([ [0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], ], dtype="d") triangles = np.array([ [0, 1, 2], [0, 2, 3], ], dtype="i") vtkw.write_vtu( vtkw.vtu_doc( vertices, triangles, celldata={"cell property": np.arange(2)}, fielddata={"color": (0, 255, 0)}, # green ), "two_triangles", )
import vtkwriters as vtkw import gmsh_reader filename = "case-tests/andra/andra_gallery.msh" nodes, elements = gmsh_reader.retrieve_mesh_elements(filename) # select heaxedra only hexaedra = [elt for elt, tags in elements if type(elt) is MT.Hexahedron] mesh = MT.HexMesh.create(nodes, hexaedra) vertices = MT.as_coordinate_array(mesh.vertices) cellnodes = np.array([np.array(nodes) for nodes in mesh.connectivity.cells.nodes]) vtkw.write_vtu( vtkw.vtu_doc(vertices, cellnodes), filename.replace(".msh", "_hexaedra_only.vtu") ) # filter out 2D elements elements_3D = [ (elt, tags) for elt, tags in elements if type(elt) in (MT.Tetrahedron, MT.Wedge, MT.Hexahedron) ] mesh = MT.HybridMesh.create(nodes, [elt for elt, tags in elements_3D]) physical = np.array([tags[0] for elt, tags in elements_3D]) offsets, cellnodes = mesh.cells_nodes_as_COC() vtkw.write_vtu(