def test_append_and_load_mesh_functions(tempdir, encoding, data_type): if invalid_config(encoding): pytest.skip("XDMF unsupported in current configuration") dtype_str, dtype = data_type meshes = [ UnitSquareMesh(MPI.comm_world, 12, 12), UnitCubeMesh(MPI.comm_world, 2, 2, 2) ] for mesh in meshes: dim = mesh.topology.dim vf = MeshFunction(dtype_str, mesh, 0, 0) vf.rename("vertices") ff = MeshFunction(dtype_str, mesh, mesh.topology.dim - 1, 0) ff.rename("facets") cf = MeshFunction(dtype_str, mesh, mesh.topology.dim, 0) cf.rename("cells") if (MPI.size(mesh.mpi_comm()) == 1): for vertex in Vertices(mesh): vf[vertex] = dtype(vertex.index()) for facet in Facets(mesh): ff[facet] = dtype(facet.index()) for cell in Cells(mesh): cf[cell] = dtype(cell.index()) else: for vertex in Vertices(mesh): vf[vertex] = dtype(vertex.global_index()) for facet in Facets(mesh): ff[facet] = dtype(facet.global_index()) for cell in Cells(mesh): cf[cell] = dtype(cell.global_index()) filename = os.path.join(tempdir, "appended_mf_%dD.xdmf" % dim) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as xdmf: xdmf.write(mesh) xdmf.write(vf) xdmf.write(ff) xdmf.write(cf) with XDMFFile(mesh.mpi_comm(), filename) as xdmf: read_function = getattr(xdmf, "read_mf_" + dtype_str) vf_in = read_function(mesh, "vertices") ff_in = read_function(mesh, "facets") cf_in = read_function(mesh, "cells") diff = 0 for vertex in Vertices(mesh): diff += (vf_in[vertex] - vf[vertex]) for facet in Facets(mesh): diff += (ff_in[facet] - ff[facet]) for cell in Cells(mesh): diff += (cf_in[cell] - cf[cell]) assert diff == 0
def test_vertex_iterators(): "Iterate over vertices" mesh = UnitCubeMesh(MPI.comm_world, 5, 5, 5) for i in range(4): mesh.init(0, i) # Test connectivity cons = [(i, mesh.topology.connectivity(0, i)) for i in range(4)] # Test writability for i, con in cons: def assign(con, i): con(i)[0] = 1 with pytest.raises(Exception): assign(con, i) n = 0 for i, v in enumerate(Vertices(mesh)): n += 1 for j, con in cons: assert numpy.all(con(i) == v.entities(j)) assert n == mesh.num_vertices()
def test_save_3D_vertex_function(tempdir, encoding, data_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) mf = MeshFunction(dtype_str, mesh, 0, 0) for vertex in Vertices(mesh): mf[vertex] = dtype(vertex.index()) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write(mf)
def test_save_2D_vertex_function(tempdir, encoding, data_type): dtype_str, dtype = data_type mesh = UnitSquareMesh(MPI.comm_world, 32, 32) mf = MeshFunction(dtype_str, mesh, 0, 0) mf.rename("vertices") for vertex in Vertices(mesh): mf[vertex] = dtype(vertex.global_index()) filename = os.path.join(tempdir, "mf_vertex_2D_%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, "vertices") diff = 0 for v in Vertices(mesh): diff += (mf_in[v] - mf[v]) assert diff == 0
def test_save_points_3D(tempdir, encoding): mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4) points, values = [], [] for v in Vertices(mesh): points.append(v.point()) values.append(v.point().norm()) vals = numpy.array(values) with XDMFFile(mesh.mpi_comm(), os.path.join(tempdir, "points_3D.xdmf"), encoding=encoding) as file: file.write(points) with XDMFFile(mesh.mpi_comm(), os.path.join(tempdir, "points_values_3D.xdmf"), encoding=encoding) as file: file.write(points, vals)
def test_save_points_3D(tempdir, encoding): if invalid_config(encoding): pytest.skip("XDMF unsupported in current configuration") import numpy mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4) points, values = [], [] for v in Vertices(mesh): points.append(v.point()) values.append(v.point().norm()) vals = numpy.array(values) with XDMFFile(mesh.mpi_comm(), os.path.join(tempdir, "points_3D.xdmf"), encoding=encoding) as file: file.write(points) with XDMFFile(mesh.mpi_comm(), os.path.join(tempdir, "points_values_3D.xdmf"), encoding=encoding) as file: file.write(points, vals)