def test_mesh_function_assign_2D_facets(): mesh = UnitSquareMesh(MPI.comm_world, 3, 3) mesh.create_entities(1) tdim = mesh.topology.dim num_cell_facets = cpp.mesh.cell_num_entities(mesh.topology.cell_type, tdim - 1) f = MeshFunction("int", mesh, tdim - 1, 25) connectivity = mesh.topology.connectivity(tdim, tdim - 1) for c in range(mesh.num_cells()): facets = connectivity.links(c) for i in range(num_cell_facets): assert 25 == f.values[facets[i]] g = MeshValueCollection("int", mesh, 1) g.assign(f) assert mesh.num_entities(tdim - 1) == len(f.values) assert mesh.num_cells() * 3 == g.size() for c in range(mesh.num_cells()): for i in range(num_cell_facets): assert 25 == g.get_value(c, i) f2 = MeshFunction("int", mesh, g, 0) connectivity = mesh.topology.connectivity(tdim, tdim - 1) for c in range(mesh.num_cells()): facets = connectivity.links(c) for i in range(num_cell_facets): assert f2.values[facets[i]] == g.get_value(c, i)
def test_save_2D_cell_function(tempdir, encoding, data_type, cell_type): dtype_str, dtype = data_type filename = os.path.join(tempdir, "mf_2D_%s.xdmf" % dtype_str) mesh = UnitSquareMesh(MPI.comm_world, 32, 32, cell_type) mf = MeshFunction(dtype_str, mesh, mesh.topology.dim, 0) mf.name = "cells" mf.values[:] = np.arange(mesh.num_entities(2), dtype=dtype) 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_mesh_function_assign_2D_vertices(): mesh = UnitSquareMesh(MPI.comm_world, 3, 3) mesh.create_entities(0) f = MeshFunction("int", mesh, 0, 25) g = MeshValueCollection("int", mesh, 0) g.assign(f) assert mesh.num_entities(0) == len(f.values) assert mesh.num_cells() * 3 == g.size() f2 = MeshFunction("int", mesh, g, 0) num_cell_vertices = cpp.mesh.cell_num_vertices(mesh.topology.cell_type) tdim = mesh.topology.dim connectivity = mesh.topology.connectivity(tdim, 0) for c in range(mesh.num_cells()): vertices = connectivity.links(c) for i in range(num_cell_vertices): assert 25 == g.get_value(c, i) assert f2.values[vertices[i]] == g.get_value(c, i)
def test_save_and_read_meshfunction_2D(tempdir): filename = os.path.join(tempdir, "meshfn-2d.h5") # Write to file mesh = UnitSquareMesh(MPI.comm_world, 20, 20) with HDF5File(mesh.mpi_comm(), filename, "w") as mf_file: # save meshfuns to compare when reading back meshfunctions = [] for i in range(0, 3): mf = MeshFunction('double', mesh, i, 0.0) # NB choose a value to set which will be the same on every # process for each entity mf.values[:] = cpp.mesh.midpoints(mesh, i, range(mesh.num_entities(i)))[:, 0] meshfunctions.append(mf) mf_file.write(mf, "/meshfunction/meshfun%d" % i) # Read back from file with HDF5File(mesh.mpi_comm(), filename, "r") as mf_file: for i in range(0, 3): mf2 = mf_file.read_mf_double(mesh, "/meshfunction/meshfun%d" % i) assert numpy.all(meshfunctions[i].values == mf2.values)
def test_UnitSquareMeshLocal(): """Create mesh of unit square.""" mesh = UnitSquareMesh(MPI.comm_self, 5, 7) assert mesh.num_entities(0) == 48 assert mesh.num_cells() == 70 assert mesh.geometry.dim == 2