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.create_connectivity(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_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.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_face_iterator(): """Iterate over faces""" mesh = UnitCubeMesh(MPI.comm_world, 5, 5, 5) for i in range(4): mesh.create_connectivity(2, i) connectivities = [(i, mesh.topology.connectivity(2, i)) for i in range(4)] # Test writability for i, connectivity in connectivities: def assign(con, i): connectivity.connections(i)[0] = 1 with pytest.raises(Exception): assign(connectivity, i) n = 0 for i, f in enumerate(Faces(mesh)): n += 1 for j, connectivity in connectivities: assert numpy.all(connectivity.connections(i) == f.entities(j)) assert n == mesh.num_entities(2)