def test_save_and_read_meshfunction_3D(tempdir): filename = os.path.join(tempdir, "meshfn-3d.h5") # Write to file mesh = UnitCubeMesh(MPI.comm_world, 2, 2, 2) mf_file = HDF5File(mesh.mpi_comm(), filename, "w") # save meshfuns to compare when reading back meshfunctions = [] for i in range(0, 4): mf = MeshFunction('double', mesh, i, 0.0) # NB choose a value to set which will be the same # on every process for each entity for cell in MeshEntities(mesh, i): mf[cell] = cell.midpoint()[0] meshfunctions.append(mf) mf_file.write(mf, "/meshfunction/group/%d/meshfun" % i) mf_file.close() # Read back from file mf_file = HDF5File(mesh.mpi_comm(), filename, "r") for i in range(0, 4): mf2 = mf_file.read_mf_double(mesh, "/meshfunction/group/%d/meshfun" % i) for cell in MeshEntities(mesh, i): assert meshfunctions[i][cell] == mf2[cell] mf_file.close()
def test_append_and_load_mesh_value_collections(tempdir, encoding, data_type): if invalid_config(encoding): pytest.skip("XDMF unsupported in current configuration") dtype_str, dtype = data_type mesh = UnitCubeMesh(MPI.comm_world, 2, 2, 2) mesh.init() for d in range(mesh.geometry.dim + 1): mesh.init_global(d) mvc_v = MeshValueCollection(dtype_str, mesh, 0) mvc_v.rename("vertices") mvc_e = MeshValueCollection(dtype_str, mesh, 1) mvc_e.rename("edges") mvc_f = MeshValueCollection(dtype_str, mesh, 2) mvc_f.rename("facets") mvc_c = MeshValueCollection(dtype_str, mesh, 3) mvc_c.rename("cells") mvcs = [mvc_v, mvc_e, mvc_f, mvc_c] filename = os.path.join(tempdir, "appended_mvcs.xdmf") with XDMFFile(mesh.mpi_comm(), filename) as xdmf: for mvc in mvcs: for ent in MeshEntities(mesh, mvc.dim): assert (mvc.set_value(ent.index(), dtype(ent.global_index()))) xdmf.write(mvc) mvc_v_in = MeshValueCollection(dtype_str, mesh, 0) mvc_e_in = MeshValueCollection(dtype_str, mesh, 1) mvc_f_in = MeshValueCollection(dtype_str, mesh, 2) mvc_c_in = MeshValueCollection(dtype_str, mesh, 3) with XDMFFile(mesh.mpi_comm(), filename) as xdmf: read_function = getattr(xdmf, "read_mvc_" + dtype_str) mvc_v_in = read_function(mesh, "vertices") mvc_e_in = read_function(mesh, "edges") mvc_f_in = read_function(mesh, "facets") mvc_c_in = read_function(mesh, "cells") mvcs_in = [mvc_v_in, mvc_e_in, mvc_f_in, mvc_c_in] for (mvc, mvc_in) in zip(mvcs, mvcs_in): mf = MeshFunction(dtype_str, mesh, mvc, 0) mf_in = MeshFunction(dtype_str, mesh, mvc_in, 0) diff = 0 for ent in MeshEntities(mesh, mf.dim): diff += (mf_in[ent] - mf[ent]) assert (diff == 0)
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_shared_entities(mesh_factory): func, args = mesh_factory # xfail_ghosted_quads_hexes(func, ghost_mode) mesh = func(*args) dim = mesh.topology.dim # FIXME: Implement a proper test for shared_dim in range(dim + 1): # Initialise global indices (if not already) mesh.init_global(shared_dim) assert isinstance(mesh.topology.shared_entities(shared_dim), dict) assert isinstance(mesh.topology.global_indices(shared_dim), numpy.ndarray) if mesh.topology.have_shared_entities(shared_dim): for e in MeshEntities(mesh, shared_dim): sharing = e.sharing_processes() assert isinstance(sharing, set) assert (len(sharing) > 0) == e.is_shared() shared_entities = mesh.topology.shared_entities(shared_dim) # Check that sum(local-shared) = global count rank = MPI.rank(mesh.mpi_comm()) ct = sum(1 for val in shared_entities.values() if list(val)[0] < rank) num_entities_global = MPI.sum(mesh.mpi_comm(), mesh.num_entities(shared_dim) - ct) assert num_entities_global == mesh.num_entities_global(shared_dim)
def test_save_mesh_value_collection(tempdir, encoding, data_type): dtype_str, dtype = data_type mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4) tdim = mesh.topology.dim meshfn = MeshFunction(dtype_str, mesh, mesh.topology.dim, False) meshfn.rename("volume_marker") for c in Cells(mesh): if c.midpoint()[1] > 0.1: meshfn[c] = dtype(1) if c.midpoint()[1] > 0.9: meshfn[c] = dtype(2) for mvc_dim in range(0, tdim + 1): mvc = MeshValueCollection(dtype_str, mesh, mvc_dim) tag = "dim_%d_marker" % mvc_dim mvc.rename(tag) mesh.init(mvc_dim, tdim) for e in MeshEntities(mesh, mvc_dim): if (e.midpoint()[0] > 0.5): mvc.set_value(e.index(), dtype(1)) filename = os.path.join(tempdir, "mvc_%d.xdmf" % 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)