def test_save_3d_vector(tempdir, encoding, cell_type): filename = os.path.join(tempdir, "u_3Dv.xdmf") mesh = UnitCubeMesh(MPI.COMM_WORLD, 2, 2, 2, cell_type) u = Function(VectorFunctionSpace(mesh, ("Lagrange", 1))) u.vector.set(1.0 + (1j if has_petsc_complex else 0)) with XDMFFile(mesh.mpi_comm(), filename, "w", encoding=encoding) as file: file.write_mesh(mesh) file.write_function(u)
def test_UnitCubeMeshDistributed(): """Create mesh of unit cube.""" mesh = UnitCubeMesh(MPI.COMM_WORLD, 5, 7, 9) assert mesh.topology.index_map(0).size_global == 480 assert mesh.topology.index_map(3).size_global == 1890 assert mesh.geometry.dim == 3 assert mesh.mpi_comm().allreduce( mesh.topology.index_map(0).size_local, MPI.SUM) == 480
def test_save_3d_scalar(tempdir, encoding, cell_type): filename = os.path.join(tempdir, "u3.xdmf") mesh = UnitCubeMesh(MPI.COMM_WORLD, 4, 3, 4, cell_type) V = FunctionSpace(mesh, ("Lagrange", 2)) u = Function(V) u.vector.set(1.0) with XDMFFile(mesh.mpi_comm(), filename, "w", encoding=encoding) as file: file.write_mesh(mesh) file.write_function(u)
def test_save_3D_facet_function(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 mf = MeshFunction(dtype_str, mesh, tdim - 1, 0) mf.name = "facets" global_indices = mesh.topology.global_indices(tdim - 1) mf.values[:] = global_indices[:] filename = os.path.join(tempdir, "mf_facet_3D_%s.xdmf" % dtype_str) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as xdmf: xdmf.write(mf) with XDMFFile(mesh.mpi_comm(), filename) as xdmf: read_function = getattr(xdmf, "read_mf_" + dtype_str) mf_in = read_function(mesh, "facets") diff = mf_in.values - mf.values assert np.all(diff == 0)
def test_save_3d_tensor(tempdir): mesh = UnitCubeMesh(MPI.COMM_WORLD, 8, 8, 8) u = Function(TensorFunctionSpace(mesh, ("Lagrange", 2))) with u.vector.localForm() as loc: loc.set(1.0) filename = os.path.join(tempdir, "u.pvd") with VTKFile(mesh.mpi_comm(), filename, "w") as vtk: vtk.write_function(u, 0.)
def test_save_and_load_3d_mesh(tempdir, encoding, cell_type): filename = os.path.join(tempdir, "mesh_3D.xdmf") mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4, cell_type) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write(mesh) with XDMFFile(MPI.comm_world, filename) as file: mesh2 = file.read_mesh(cpp.mesh.GhostMode.none) assert mesh.num_entities_global(0) == mesh2.num_entities_global(0) dim = mesh.topology.dim assert mesh.num_entities_global(dim) == mesh2.num_entities_global(dim)
def test_save_3D_edge_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, 1, 0) mf.name = "edges" mf.values[:] = np.arange(mesh.num_entities(1), dtype=dtype) filename = os.path.join(tempdir, "mf_edge_3D_%s.xdmf" % dtype_str) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write(mf)
def test_save_3d_vector_series(tempdir, encoding, cell_type): filename = os.path.join(tempdir, "u_3D.xdmf") mesh = UnitCubeMesh(MPI.comm_world, 2, 2, 2, cell_type) u = Function(VectorFunctionSpace(mesh, ("Lagrange", 2))) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: u.vector.set(1.0 + (1j if has_petsc_complex else 0)) file.write(u, 0.1) u.vector.set(2.0 + (2j if has_petsc_complex else 0)) file.write(u, 0.2) u.vector.set(3.0 + (3j if has_petsc_complex else 0)) file.write(u, 0.3)
def test_save_and_read_mesh_value_collection_with_only_one_marked_entity( tempdir): ndiv = 2 filename = os.path.join(tempdir, "mesh_value_collection.h5") mesh = UnitCubeMesh(MPI.comm_world, ndiv, ndiv, ndiv) mvc = MeshValueCollection("size_t", mesh, 3) mesh.create_entities(3) if MPI.rank(mesh.mpi_comm()) == 0: mvc.set_value(0, 1) # write to file with HDF5File(mesh.mpi_comm(), filename, 'w') as f: f.write(mvc, "/mesh_value_collection") # read from file with HDF5File(mesh.mpi_comm(), filename, 'r') as f: mvc = f.read_mvc_size_t(mesh, "/mesh_value_collection") assert MPI.sum(mesh.mpi_comm(), mvc.size()) == 1 if MPI.rank(mesh.mpi_comm()) == 0: assert mvc.get_value(0, 0) == 1
def test_save_and_load_3d_mesh(tempdir, encoding, cell_type): filename = os.path.join(tempdir, "mesh.xdmf") mesh = UnitCubeMesh(MPI.COMM_WORLD, 12, 12, 8, cell_type) with XDMFFile(mesh.mpi_comm(), filename, "w", encoding=encoding) as file: file.write_mesh(mesh) with XDMFFile(MPI.COMM_WORLD, filename, "r", encoding=encoding) as file: mesh2 = file.read_mesh() assert mesh.topology.index_map(0).size_global == mesh2.topology.index_map(0).size_global assert mesh.topology.index_map(mesh.topology.dim).size_global == mesh2.topology.index_map( mesh.topology.dim).size_global
def test_xdmf_timeseries_write_to_closed_hdf5_using_with(tempdir, cell_type): mesh = UnitCubeMesh(MPI.comm_world, 2, 2, 2, cell_type) V = FunctionSpace(mesh, ("CG", 1)) u = Function(V) filename = os.path.join(tempdir, "time_series_closed_append.xdmf") with XDMFFile(mesh.mpi_comm(), filename) as xdmf: xdmf.write(u, float(0.0)) xdmf.write(u, float(1.0)) xdmf.close() with xdmf: xdmf.write(u, float(2.0))
def test_UnitHexMesh_assemble(): mesh = UnitCubeMesh(MPI.COMM_WORLD, 6, 7, 5, CellType.hexahedron) vol = assemble_scalar(1 * dx(mesh)) vol = mesh.mpi_comm().allreduce(vol, MPI.SUM) assert (vol == pytest.approx(1, rel=1e-9))
def test_UnitHexMesh(): mesh = UnitCubeMesh(MPI.COMM_WORLD, 5, 7, 9, CellType.hexahedron) assert mesh.topology.index_map(0).size_global == 480 assert mesh.topology.index_map(3).size_global == 315 assert mesh.geometry.dim == 3 assert mesh.mpi_comm().allreduce(mesh.topology.index_map(0).size_local, MPI.SUM) == 480
def test_UnitHexMesh_assemble(): mesh = UnitCubeMesh(MPI.comm_world, 6, 7, 5, CellType.hexahedron) vol = assemble_scalar(1 * dx(mesh)) vol = MPI.sum(mesh.mpi_comm(), vol) assert (vol == pytest.approx(1, rel=1e-9))
def test_UnitHexMesh(): mesh = UnitCubeMesh(MPI.comm_world, 5, 7, 9, CellType.hexahedron) assert mesh.num_entities_global(0) == 480 assert mesh.num_entities_global(3) == 315 assert mesh.geometry.dim == 3 assert MPI.sum(mesh.mpi_comm(), mesh.topology.index_map(0).size_local) == 480