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_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
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) mp = cpp.mesh.midpoints(mesh, i, range(mesh.num_entities(i))) # NB choose a value to set which will be the same on every # process for each entity mf.values[:] = mp[:, 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) assert numpy.all(meshfunctions[i].values == mf2.values) mf_file.close()
def test_save_3D_vertex_function(tempdir, encoding, data_type, cell_type): dtype_str, dtype = data_type filename = os.path.join(tempdir, "mf_vertex_3D_%s.xdmf" % dtype_str) mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4, cell_type) mf = MeshFunction(dtype_str, mesh, 0, 0) mf.values[:] = np.arange(mesh.num_entities(0), dtype=dtype) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write(mf)
def test_append_and_load_mesh_value_collections(tempdir, encoding, data_type, cell_type): dtype_str, dtype = data_type mesh = UnitCubeMesh(MPI.comm_world, 2, 2, 2, cell_type) mesh.create_connectivity_all() mvc_v = MeshValueCollection(dtype_str, mesh, 0) mvc_v.name = "vertices" mvc_e = MeshValueCollection(dtype_str, mesh, 1) mvc_e.name = "edges" mvc_f = MeshValueCollection(dtype_str, mesh, 2) mvc_f.name = "facets" mvc_c = MeshValueCollection(dtype_str, mesh, 3) mvc_c.name = "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: # global_indices = mesh.topology.global_indices(mvc.dim) map = mesh.topology.index_map(mvc.dim) global_indices = map.global_indices(True) for ent in range(mesh.num_entities(mvc.dim)): assert (mvc.set_value(ent, global_indices[ent])) 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 = mf_in.values - mf.values assert np.all(diff == 0)
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_UnitCubeMeshLocal(): """Create mesh of unit cube.""" mesh = UnitCubeMesh(MPI.comm_self, 5, 7, 9) assert mesh.num_entities(0) == 480 assert mesh.num_cells() == 1890 assert mesh.geometry.dim == 3