def test_append_and_load_mesh_functions(tempdir, encoding, data_type): dtype_str, dtype = data_type meshes = [ UnitSquareMesh(MPI.comm_world, 12, 12), UnitCubeMesh(MPI.comm_world, 2, 2, 2), UnitSquareMesh(MPI.comm_world, 12, 12, CellType.quadrilateral), UnitCubeMesh(MPI.comm_world, 2, 2, 2, CellType.hexahedron) ] for mesh in meshes: dim = mesh.topology.dim vf = MeshFunction(dtype_str, mesh, 0, 0) vf.name = "vertices" ff = MeshFunction(dtype_str, mesh, mesh.topology.dim - 1, 0) ff.name = "facets" cf = MeshFunction(dtype_str, mesh, mesh.topology.dim, 0) cf.name = "cells" # vf.values[:] = mesh.topology.global_indices(0)[:] map = mesh.topology.index_map(0) vf.values[:] = map.global_indices(True) map = mesh.topology.index_map(dim - 1) ff.values[:] = map.global_indices(True) map = mesh.topology.index_map(dim) cf.values[:] = map.global_indices(True) filename = os.path.join( tempdir, "appended_mf_{0:d}_{1:s}.xdmf".format( dim, str(mesh.topology.cell_type))) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as xdmf: xdmf.write(mesh) xdmf.write(vf) xdmf.write(ff) xdmf.write(cf) with XDMFFile(mesh.mpi_comm(), filename) as xdmf: read_function = getattr(xdmf, "read_mf_" + dtype_str) vf_in = read_function(mesh, "vertices") ff_in = read_function(mesh, "facets") cf_in = read_function(mesh, "cells") diff_vf = vf_in.values - vf.values diff_ff = ff_in.values - ff.values diff_cf = cf_in.values - cf.values assert np.all(diff_vf == 0) assert np.all(diff_ff == 0) assert np.all(diff_cf == 0)
def test_multiple_datasets(tempdir, encoding, cell_type): mesh = UnitSquareMesh(MPI.comm_world, 4, 4, cell_type) cf0 = MeshFunction('size_t', mesh, 2, 11) cf0.name = 'cf0' cf1 = MeshFunction('size_t', mesh, 2, 22) cf1.name = 'cf1' filename = os.path.join(tempdir, "multiple_mf.xdmf") with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as xdmf: xdmf.write(mesh) xdmf.write(cf0) xdmf.write(cf1) with XDMFFile(mesh.mpi_comm(), filename) as xdmf: mesh = xdmf.read_mesh(cpp.mesh.GhostMode.none) cf0 = xdmf.read_mf_size_t(mesh, "cf0") cf1 = xdmf.read_mf_size_t(mesh, "cf1") assert (cf0.values[0] == 11 and cf1.values[0] == 22)
def test_save_mesh_value_collection(tempdir, encoding, data_type, cell_type): dtype_str, dtype = data_type mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4, cell_type) tdim = mesh.topology.dim meshfn = MeshFunction(dtype_str, mesh, mesh.topology.dim, False) meshfn.name = "volume_marker" mp = cpp.mesh.midpoints(mesh, tdim, range(mesh.num_entities(tdim))) for i in range(mesh.num_cells()): if mp[i, 1] > 0.1: meshfn.values[i] = 1 if mp[i, 1] > 0.9: meshfn.values[i] = 2 for mvc_dim in range(0, tdim + 1): mvc = MeshValueCollection(dtype_str, mesh, mvc_dim) tag = "dim_{}_marker".format(mvc_dim) mvc.name = tag mesh.create_connectivity(mvc_dim, tdim) mp = cpp.mesh.midpoints(mesh, mvc_dim, range(mesh.num_entities(mvc_dim))) for e in range(mesh.num_entities(mvc_dim)): if (mp[e, 0] > 0.5): mvc.set_value(e, dtype(1)) filename = os.path.join(tempdir, "mvc_{}.xdmf".format(mvc_dim)) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as xdmf: xdmf.write(meshfn) xdmf.write(mvc) with XDMFFile(mesh.mpi_comm(), filename) as xdmf: read_function = getattr(xdmf, "read_mvc_" + dtype_str) mvc = read_function(mesh, tag)
def test_save_3D_edge_function(tempdir, encoding, data_type, cell_type): dtype_str, dtype = data_type mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4, cell_type) mf = MeshFunction(dtype_str, mesh, 1, 0) mf.name = "edges" mf.values[:] = np.arange(mesh.num_entities(1), dtype=dtype) filename = os.path.join(tempdir, "mf_edge_3D_%s.xdmf" % dtype_str) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write(mf)
def test_save_3D_cell_function(tempdir, encoding, data_type, cell_type): dtype_str, dtype = data_type mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4, cell_type) mf = MeshFunction(dtype_str, mesh, mesh.topology.dim, 0) mf.name = "cells" mf.values[:] = np.arange(mesh.num_entities(3), dtype=dtype) filename = os.path.join(tempdir, "mf_3D_%s.xdmf" % dtype_str) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write(mf) with XDMFFile(mesh.mpi_comm(), filename) as xdmf: read_function = getattr(xdmf, "read_mf_" + dtype_str) mf_in = read_function(mesh, "cells") diff = mf_in.values - mf.values assert np.all(diff == 0)
def test_save_2D_vertex_function(tempdir, encoding, data_type, cell_type): dtype_str, dtype = data_type mesh = UnitSquareMesh(MPI.comm_world, 32, 32, cell_type) mf = MeshFunction(dtype_str, mesh, 0, 0) mf.name = "vertices" global_indices = mesh.topology.index_map(0).global_indices(False) mf.values[:] = global_indices[:] filename = os.path.join(tempdir, "mf_vertex_2D_%s.xdmf" % dtype_str) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write(mf) with XDMFFile(mesh.mpi_comm(), filename) as xdmf: read_function = getattr(xdmf, "read_mf_" + dtype_str) mf_in = read_function(mesh, "vertices") diff = mf_in.values - mf.values assert np.all(diff == 0)
def test_save_3D_facet_function(tempdir, encoding, data_type, cell_type): dtype_str, dtype = data_type mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4, cell_type) tdim = mesh.topology.dim mf = MeshFunction(dtype_str, mesh, tdim - 1, 0) mf.name = "facets" global_indices = mesh.topology.global_indices(tdim - 1) mf.values[:] = global_indices[:] filename = os.path.join(tempdir, "mf_facet_3D_%s.xdmf" % dtype_str) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as xdmf: xdmf.write(mf) with XDMFFile(mesh.mpi_comm(), filename) as xdmf: read_function = getattr(xdmf, "read_mf_" + dtype_str) mf_in = read_function(mesh, "facets") diff = mf_in.values - mf.values assert np.all(diff == 0)