def test_save_and_read_mesh_value_collection(tempdir): ndiv = 2 filename = os.path.join(tempdir, "mesh_value_collection.h5") mesh = UnitCubeMesh(MPI.comm_world, ndiv, ndiv, ndiv) def point2list(p): return [p[0], p[1], p[2]] # write to file with HDF5File(mesh.mpi_comm(), filename, 'w') as f: for dim in range(mesh.topology.dim): mvc = MeshValueCollection("size_t", mesh, dim) mesh.create_entities(dim) for e in MeshEntities(mesh, dim): # this can be easily computed to the check the value val = int(ndiv * sum(point2list(e.midpoint()))) + 1 mvc.set_value(e.index(), val) f.write(mvc, "/mesh_value_collection_{}".format(dim)) # read from file with HDF5File(mesh.mpi_comm(), filename, 'r') as f: for dim in range(mesh.topology.dim): mvc = f.read_mvc_size_t(mesh, "/mesh_value_collection_{}".format(dim)) # check the values for (cell, lidx), val in mvc.values().items(): eidx = Cell(mesh, cell).entities(dim)[lidx] mid = point2list(MeshEntity(mesh, dim, eidx).midpoint()) assert val == int(ndiv * sum(mid)) + 1
def test_save_and_read_mesh_value_collection(tempdir): ndiv = 2 filename = os.path.join(tempdir, "mesh_value_collection.h5") mesh = UnitCubeMesh(MPI.comm_world, ndiv, ndiv, ndiv) # write to file with HDF5File(mesh.mpi_comm(), filename, 'w') as f: for dim in range(mesh.topology.dim): mvc = MeshValueCollection("size_t", mesh, dim) mesh.create_entities(dim) mp = cpp.mesh.midpoints(mesh, dim, range(mesh.num_entities(dim))) for e in range(mesh.num_entities(dim)): # this can be easily computed to the check the value val = int(ndiv * mp[e].sum()) + 1 mvc.set_value(e, val) f.write(mvc, "/mesh_value_collection_{}".format(dim)) # read from file with HDF5File(mesh.mpi_comm(), filename, 'r') as f: for dim in range(mesh.topology.dim): mvc = f.read_mvc_size_t(mesh, "/mesh_value_collection_{}".format(dim)) mp = cpp.mesh.midpoints(mesh, dim, range(mesh.num_entities(dim))) # check the values for (cell, lidx), val in mvc.values().items(): eidx = MeshEntity(mesh, mesh.topology.dim, cell).entities(dim)[lidx] mid = mp[eidx] assert val == int(ndiv * mid.sum()) + 1
mesh_ele_size = .5 p0 = geom.add_point([0, 0, 0], lcar=mesh_ele_size) p1 = geom.add_point([1, 0, 0], lcar=mesh_ele_size) p2 = geom.add_point([1, 1, 0], lcar=mesh_ele_size) p3 = geom.add_point([0, 1, 0], lcar=mesh_ele_size) l0 = geom.add_line(p0, p1) l1 = geom.add_line(p1, p2) l2 = geom.add_line(p2, p3) l3 = geom.add_line(p3, p0) ll = geom.add_line_loop(lines=[l0, l1, l2, l3]) ps = geom.add_plane_surface(ll) # Tag line and surface geom.add_physical(l3, label="LINE") geom.add_physical(ps, label="SURFACE") pygmsh_mesh = pygmsh.generate_mesh(geom) points, cells, cell_data = pygmsh_mesh.points, pygmsh_mesh.cells, pygmsh_mesh.cell_data mesh = cpp.mesh.Mesh(MPI.comm_world, cpp.mesh.CellType.Type.triangle, points, cells['triangle'], [], cpp.mesh.GhostMode.none) assert mesh.degree() == 1 assert mesh.geometry.dim == 3 assert mesh.topology.dim == 2 f = MeshValueCollection("size_t", mesh, 1, cells["line"], cell_data["line"]['gmsh:physical']) print(f.values())
element_type, element_tag, node_tag = gmsh.model.mesh.getElements( dim, tag) for ele_num in element_tag[0]: element_tags[dim - 1][element_tags[dim - 1] == ele_num] = tag cell_data[name] = element_tags[dim - 1] # gmsh.write("unit_square.geo_unrolled") # End Generate mesh gmsh.finalize() meshio.write( "unit_square_gmsh.xdmf", meshio.Mesh(points=points, cells={"triangle": cells["triangle"]}), ) mesh = cpp.mesh.Mesh( MPI.comm_world, cpp.mesh.CellType.triangle, points, cells["triangle"], [], cpp.mesh.GhostMode.none, ) mvc_from_array = MeshValueCollection("size_t", mesh, 1, cells["line"], cell_data["line"]) if MPI.comm_world.rank == 0: print(mvc_from_array.values())