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)
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)