def test_save_2D_facet_function(tempdir, encoding, data_type): if invalid_config(encoding): pytest.skip("XDMF unsupported in current configuration") dtype_str, dtype = data_type mesh = UnitSquareMesh(MPI.comm_world, 32, 32) mf = MeshFunction(dtype_str, mesh, mesh.topology.dim - 1, 0) mf.rename("facets") if (MPI.size(mesh.mpi_comm()) == 1): for facet in Facets(mesh): mf[facet] = dtype(facet.index()) else: for facet in Facets(mesh): mf[facet] = dtype(facet.global_index()) filename = os.path.join(tempdir, "mf_facet_2D_%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 = 0 for facet in Facets(mesh): diff += (mf_in[facet] - mf[facet]) assert diff == 0
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.init(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_and_checkpoint_vector(tempdir, encoding, fe_degree, fe_family, mesh_tdim, mesh_n): if invalid_config(encoding): pytest.skip("XDMF unsupported in current configuration") if invalid_fe(fe_family, fe_degree): pytest.skip("Trivial finite element") filename = os.path.join(tempdir, "u2_checkpoint.xdmf") mesh = mesh_factory(mesh_tdim, mesh_n) FE = VectorElement(fe_family, mesh.ufl_cell(), fe_degree) V = FunctionSpace(mesh, FE) u_in = Function(V) u_out = Function(V) if mesh.geometry.dim == 1: u_out.interpolate(Expression(("x[0]", ), degree=1)) elif mesh.geometry.dim == 2: u_out.interpolate(Expression(("x[0]*x[1]", "x[0]"), degree=2)) elif mesh.geometry.dim == 3: u_out.interpolate(Expression(("x[0]*x[1]", "x[0]", "x[2]"), degree=2)) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write_checkpoint(u_out, "u_out", 0) with XDMFFile(mesh.mpi_comm(), filename) as file: u_in = file.read_checkpoint(V, "u_out", 0) u_in.vector().axpy(-1.0, u_out.vector()) assert u_in.vector().norm(cpp.la.Norm.l2) < 1.0e-12
def test_save_3D_facet_function(tempdir, encoding, data_type): dtype_str, dtype = data_type mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4) mf = MeshFunction(dtype_str, mesh, mesh.topology.dim - 1, 0) mf.rename("facets") if (MPI.size(mesh.mpi_comm()) == 1): for facet in Facets(mesh): mf[facet] = dtype(facet.index()) else: for facet in Facets(mesh): mf[facet] = dtype(facet.global_index()) 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 = 0 for facet in Facets(mesh): diff += (mf_in[facet] - mf[facet]) assert diff == 0
def test_save_and_checkpoint_scalar(tempdir, encoding, fe_degree, fe_family, mesh_tdim, mesh_n): if invalid_fe(fe_family, fe_degree): pytest.skip("Trivial finite element") filename = os.path.join(tempdir, "u1_checkpoint.xdmf") mesh = mesh_factory(mesh_tdim, mesh_n) FE = FiniteElement(fe_family, mesh.ufl_cell(), fe_degree) V = FunctionSpace(mesh, FE) u_in = Function(V) u_out = Function(V) if has_petsc_complex: u_out.interpolate(Expression("x[0] + j*x[0]", degree=1)) else: u_out.interpolate(Expression("x[0]", degree=1)) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write_checkpoint(u_out, "u_out", 0) with XDMFFile(mesh.mpi_comm(), filename) as file: u_in = file.read_checkpoint(V, "u_out", 0) u_in.vector().axpy(-1.0, u_out.vector()) assert u_in.vector().norm(cpp.la.Norm.l2) < 1.0e-12
def test_save_and_checkpoint_timeseries(tempdir, encoding): mesh = UnitSquareMesh(MPI.comm_world, 16, 16) filename = os.path.join(tempdir, "u2_checkpoint.xdmf") FE = FiniteElement("CG", mesh.ufl_cell(), 2) V = FunctionSpace(mesh, FE) times = [0.5, 0.2, 0.1] u_out = [None] * len(times) u_in = [None] * len(times) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: for i, p in enumerate(times): u_out[i] = interpolate(Expression("x[0]*p", p=p, degree=1), V) file.write_checkpoint(u_out[i], "u_out", p) with XDMFFile(mesh.mpi_comm(), filename) as file: for i, p in enumerate(times): u_in[i] = file.read_checkpoint(V, "u_out", i) for i, p in enumerate(times): u_in[i].vector().axpy(-1.0, u_out[i].vector()) assert u_in[i].vector().norm(cpp.la.Norm.l2) < 1.0e-12 # test reading last with XDMFFile(mesh.mpi_comm(), filename) as file: u_in_last = file.read_checkpoint(V, "u_out", -1) u_out[-1].vector().axpy(-1.0, u_in_last.vector()) assert u_out[-1].vector().norm(cpp.la.Norm.l2) < 1.0e-12
def test_distribute_mesh(subset_comm, tempdir, mesh_factory): func, args = mesh_factory mesh = func(*args) if not is_simplex(mesh.cell_type): return # Order mesh cpp.mesh.Ordering.order_simplex(mesh) encoding = XDMFFile.Encoding.HDF5 ghost_mode = cpp.mesh.GhostMode.none filename = os.path.join(tempdir, "mesh.xdmf") comm = mesh.mpi_comm() parts = comm.size partitioner = cpp.mesh.Partitioner.scotch with XDMFFile(mesh.mpi_comm(), filename, encoding) as file: file.write(mesh) # Use the subset_comm to read and partition mesh, then distribute to all # available processes with XDMFFile(subset_comm, filename) as file: cell_type, points, cells, indices = file.read_mesh_data(subset_comm) partition_data = cpp.mesh.partition_cells(subset_comm, parts, cell_type, cells, partitioner) dist_mesh = cpp.mesh.build_from_partition(MPI.comm_world, cell_type, points, cells, indices, ghost_mode, partition_data) assert(mesh.cell_type == dist_mesh.cell_type) assert mesh.num_entities_global(0) == dist_mesh.num_entities_global(0) dim = dist_mesh.topology.dim assert mesh.num_entities_global(dim) == dist_mesh.num_entities_global(dim)
def test_custom_partition(tempdir, mesh_factory): func, args = mesh_factory mesh = func(*args) if not is_simplex(mesh.cell_type): return comm = mesh.mpi_comm() filename = os.path.join(tempdir, "mesh.xdmf") encoding = XDMFFile.Encoding.HDF5 ghost_mode = cpp.mesh.GhostMode.none with XDMFFile(comm, filename, encoding) as file: file.write(mesh) with XDMFFile(comm, filename) as file: cell_type, points, cells, global_indices = file.read_mesh_data(comm) # Create a custom partition array part = numpy.zeros(cells.shape[0], dtype=numpy.int32) part[:] = comm.rank ghost_procs = cpp.mesh.ghost_cell_mapping(comm, part, cell_type, cells) cell_partition = cpp.mesh.PartitionData(part, ghost_procs) dist_mesh = cpp.mesh.build_from_partition(comm, cell_type, points, cells, global_indices, ghost_mode, cell_partition) assert(mesh.cell_type == dist_mesh.cell_type) assert mesh.num_entities_global(0) == dist_mesh.num_entities_global(0) dim = dist_mesh.topology.dim assert mesh.num_entities_global(dim) == dist_mesh.num_entities_global(dim)
def compute_rates(): "Compute convergence rates for degrees 1 and 2." gdim = 2 tdim = gdim for coordinate_degree in (1, 2): for element_degree in (1, 2): print("\nUsing coordinate degree %d, element degree %d" % (coordinate_degree, element_degree)) ufile = XDMFFile(MPI.comm_world, "poisson-disc-degree-x%d-e%d.xdmf" % (coordinate_degree, element_degree)) encoding = XDMFFile.Encoding.HDF5 if has_hdf5() else XDMFFile.Encoding.ASCII preverr = None prevh = None for i, nsteps in enumerate((1, 8, 64)): err, h, area, num_cells, u = compute(nsteps, coordinate_degree, element_degree, gdim) if preverr is None: conv = 0.0 print("conv = N/A, h = %.3e, err = %.3e, area = %.16f, num_cells = %d" % (h, err, area, num_cells)) else: conv = math.log(preverr/err, prevh/h) print("conv = %1.2f, h = %.3e, err = %.3e, area = %.16f, num_cells = %d" % (conv, h, err, area, num_cells)) preverr = err prevh = h # Save solution to file u.rename('u') if MPI.size(MPI.comm_world) > 1 and encoding == XDMFFile.Encoding.ASCII: print("XDMF file output not supported in parallel without HDF5") else: ufile.write(u, encoding=encoding)
def test_save_and_checkpoint_scalar(tempdir, encoding, fe_degree, fe_family, mesh_tdim, mesh_n): if invalid_fe(fe_family, fe_degree): pytest.skip("Trivial finite element") filename = os.path.join(tempdir, "u1_checkpoint.xdmf") mesh = mesh_factory(mesh_tdim, mesh_n) FE = FiniteElement(fe_family, mesh.ufl_cell(), fe_degree) V = FunctionSpace(mesh, FE) u_in = Function(V) u_out = Function(V) if has_petsc_complex: def expr_eval(values, x): values[:, 0] = x[:, 0] + 1.0j * x[:, 0] u_out.interpolate(expr_eval) else: def expr_eval(values, x): values[:, 0] = x[:, 0] u_out.interpolate(expr_eval) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write_checkpoint(u_out, "u_out", 0) with XDMFFile(mesh.mpi_comm(), filename) as file: u_in = file.read_checkpoint(V, "u_out", 0) u_in.vector.axpy(-1.0, u_out.vector) assert u_in.vector.norm() < 1.0e-12
def test_read_write_p2_function(tempdir): mesh = cpp.generation.UnitDiscMesh.create(MPI.comm_world, 3, cpp.mesh.GhostMode.none) cmap = fem.create_coordinate_map(mesh.ufl_domain()) mesh.geometry.coord_mapping = cmap Q = FunctionSpace(mesh, ("Lagrange", 2)) F = Function(Q) if has_petsc_complex: F.interpolate(Expression("x[0] + j*x[0]", degree=1)) else: F.interpolate(Expression("x[0]", degree=1)) filename = os.path.join(tempdir, "tri6_function.xdmf") with XDMFFile(mesh.mpi_comm(), filename, encoding=XDMFFile.Encoding.HDF5) as xdmf: xdmf.write(F) Q = VectorFunctionSpace(mesh, ("Lagrange", 1)) F = Function(Q) if has_petsc_complex: F.interpolate(Expression(("x[0] + j*x[0]", "x[1] + j*x[1]"), degree=1)) else: F.interpolate(Expression(("x[0]", "x[1]"), degree=1)) filename = os.path.join(tempdir, "tri6_vector_function.xdmf") with XDMFFile(mesh.mpi_comm(), filename, encoding=XDMFFile.Encoding.HDF5) as xdmf: xdmf.write(F)
def test_save_and_checkpoint_timeseries(tempdir, encoding): mesh = UnitSquareMesh(MPI.comm_world, 16, 16) filename = os.path.join(tempdir, "u2_checkpoint.xdmf") FE = FiniteElement("CG", mesh.ufl_cell(), 2) V = FunctionSpace(mesh, FE) times = [0.5, 0.2, 0.1] u_out = [None] * len(times) u_in = [None] * len(times) p = 0.0 def expr_eval(values, x): values[:, 0] = x[:, 0] * p with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: for i, p in enumerate(times): u_out[i] = interpolate(expr_eval, V) file.write_checkpoint(u_out[i], "u_out", p) with XDMFFile(mesh.mpi_comm(), filename) as file: for i, p in enumerate(times): u_in[i] = file.read_checkpoint(V, "u_out", i) for i, p in enumerate(times): u_in[i].vector.axpy(-1.0, u_out[i].vector) assert u_in[i].vector.norm() < 1.0e-12 # test reading last with XDMFFile(mesh.mpi_comm(), filename) as file: u_in_last = file.read_checkpoint(V, "u_out", -1) u_out[-1].vector.axpy(-1.0, u_in_last.vector) assert u_out[-1].vector.norm() < 1.0e-12
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_save_3D_cell_function(tempdir, encoding, data_type): if invalid_config(encoding): pytest.skip("XDMF unsupported in current configuration") dtype_str, dtype = data_type mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4) mf = MeshFunction(dtype_str, mesh, mesh.topology.dim, 0) mf.rename("cells") for cell in Cells(mesh): mf[cell] = dtype(cell.index()) filename = os.path.join(tempdir, "mf_3D_%s.xdmf" % dtype_str) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write(mf) # mf_in = MeshFunction(dtype_str, mesh, mesh.topology.dim, 0) with XDMFFile(mesh.mpi_comm(), filename) as xdmf: read_function = getattr(xdmf, "read_mf_" + dtype_str) mf_in = read_function(mesh, "cells") diff = 0 for cell in Cells(mesh): diff += (mf_in[cell] - mf[cell]) assert diff == 0
def test_Write(f): """Construct and save a simple meshfunction.""" f = f f[0] = 1 f[1] = 2 file = XDMFFile(f.mesh().mpi_comm(), "saved_mesh_function.xdmf") file.write(f)
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_save_and_load_2d_mesh(tempdir, encoding): filename = os.path.join(tempdir, "mesh_2D.xdmf") mesh = UnitSquareMesh(MPI.comm_world, 32, 32) 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_points_3D(tempdir, encoding): mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4) points = mesh.geometry.points vals = numpy.linalg.norm(points, axis=1) 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_append_and_load_mesh_value_collections(tempdir, encoding, data_type): if invalid_config(encoding): pytest.skip("XDMF unsupported in current configuration") dtype_str, dtype = data_type mesh = UnitCubeMesh(MPI.comm_world, 2, 2, 2) mesh.init() for d in range(mesh.geometry.dim + 1): mesh.init_global(d) mvc_v = MeshValueCollection(dtype_str, mesh, 0) mvc_v.rename("vertices") mvc_e = MeshValueCollection(dtype_str, mesh, 1) mvc_e.rename("edges") mvc_f = MeshValueCollection(dtype_str, mesh, 2) mvc_f.rename("facets") mvc_c = MeshValueCollection(dtype_str, mesh, 3) mvc_c.rename("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: for ent in MeshEntities(mesh, mvc.dim): assert (mvc.set_value(ent.index(), dtype(ent.global_index()))) 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 = 0 for ent in MeshEntities(mesh, mf.dim): diff += (mf_in[ent] - mf[ent]) assert (diff == 0)
def test_save_and_load_1d_mesh(tempdir, encoding): if invalid_config(encoding): pytest.skip("XDMF unsupported in current configuration") filename = os.path.join(tempdir, "mesh.xdmf") mesh = UnitIntervalMesh(MPI.comm_world, 32) 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(MPI.comm_world, 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_read_write_p2_function(tempdir): mesh = cpp.generation.UnitDiscMesh.create(MPI.comm_world, 3, cpp.mesh.GhostMode.none) cmap = fem.create_coordinate_map(mesh.ufl_domain()) mesh.geometry.coord_mapping = cmap Q = FunctionSpace(mesh, ("Lagrange", 2)) F = Function(Q) if has_petsc_complex: @function.expression.numba_eval def expr_eval(values, x, cell_idx): values[:, 0] = x[:, 0] + 1.0j * x[:, 0] F.interpolate(Expression(expr_eval)) else: @function.expression.numba_eval def expr_eval(values, x, cell_idx): values[:, 0] = x[:, 0] F.interpolate(Expression(expr_eval)) filename = os.path.join(tempdir, "tri6_function.xdmf") with XDMFFile(mesh.mpi_comm(), filename, encoding=XDMFFile.Encoding.HDF5) as xdmf: xdmf.write(F) Q = VectorFunctionSpace(mesh, ("Lagrange", 1)) F = Function(Q) if has_petsc_complex: @function.expression.numba_eval def expr_eval(values, x, cell_idx): values[:, 0] = x[:, 0] + 1.0j * x[:, 0] values[:, 1] = x[:, 1] + 1.0j * x[:, 1] F.interpolate(Expression(expr_eval, shape=(2, ))) else: @function.expression.numba_eval def expr_eval(values, x, cell_idx): values[:, 0] = x[:, 0] values[:, 1] = x[:, 1] F.interpolate(Expression(expr_eval, shape=(2, ))) filename = os.path.join(tempdir, "tri6_vector_function.xdmf") with XDMFFile(mesh.mpi_comm(), filename, encoding=XDMFFile.Encoding.HDF5) as xdmf: xdmf.write(F)
def test_read_write_p2_mesh(tempdir): mesh = cpp.generation.UnitDiscMesh.create(MPI.comm_world, 3, cpp.mesh.GhostMode.none) filename = os.path.join(tempdir, "tri6_mesh.xdmf") with XDMFFile(mesh.mpi_comm(), filename, encoding=XDMFFile.Encoding.HDF5) as xdmf: xdmf.write(mesh) with XDMFFile(mesh.mpi_comm(), filename) as xdmf: mesh2 = xdmf.read_mesh(mesh.mpi_comm(), cpp.mesh.GhostMode.none) assert mesh2.num_entities_global( mesh.topology.dim) == mesh.num_entities_global(mesh.topology.dim) assert mesh2.num_entities_global(0) == mesh.num_entities_global(0)
def test_multiple_datasets(tempdir, encoding): mesh = UnitSquareMesh(MPI.comm_world, 2, 2) cf0 = MeshFunction('size_t', mesh, 2, 11) cf0.name = 'cf0' cf1 = MeshFunction('size_t', mesh, 2, 22) cf1.name = 'cf1' filename = os.path.join(tempdir, "multiple_mf.xdmf") with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as xdmf: xdmf.write(mesh) xdmf.write(cf0) xdmf.write(cf1) with XDMFFile(mesh.mpi_comm(), filename) as xdmf: mesh = xdmf.read_mesh(cpp.mesh.GhostMode.none) cf0 = xdmf.read_mf_size_t(mesh, "cf0") cf1 = xdmf.read_mf_size_t(mesh, "cf1") assert (cf0.values[0] == 11 and cf1.values[0] == 22)
def test_save_3D_cell_function(tempdir, encoding, data_type): dtype_str, dtype = data_type mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4) mf = MeshFunction(dtype_str, mesh, mesh.topology.dim, 0) mf.name = "cells" mf.values[:] = numpy.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 numpy.all(diff == 0)
def test_save_1d_mesh(tempdir, encoding): filename = os.path.join(tempdir, "mf_1D.xdmf") mesh = UnitIntervalMesh(MPI.comm_world, 32) mf = MeshFunction("size_t", mesh, mesh.topology.dim, 0) mf.values[:] = numpy.arange(mesh.num_entities(1)) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write(mf)
def test_save_3d_tensor(tempdir, encoding): filename = os.path.join(tempdir, "u3t.xdmf") mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4) u = Function(TensorFunctionSpace(mesh, ("Lagrange", 2))) u.vector.set(1.0 + (1j if has_petsc_complex else 0)) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write(u)
def test_append_and_load_mesh_value_collections(tempdir, encoding, data_type): dtype_str, dtype = data_type mesh = UnitCubeMesh(MPI.comm_world, 2, 2, 2) mesh.create_connectivity_all() for d in range(mesh.geometry.dim + 1): mesh.create_global_indices(d) 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) 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 numpy.all(diff == 0)
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) mf.values[:] = numpy.arange(mesh.num_entities(0), dtype=dtype) 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.name = "vertices" global_indices = mesh.topology.global_indices(0) mf.values[:] = global_indices[:] 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 = mf_in.values - mf.values assert numpy.all(diff == 0)
def test_save_2d_vector(tempdir, encoding): filename = os.path.join(tempdir, "u_2dv.xdmf") mesh = UnitSquareMesh(MPI.comm_world, 16, 16) V = VectorFunctionSpace(mesh, ("Lagrange", 2)) u = Function(V) u.vector.set(1.0 + (1j if has_petsc_complex else 0)) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write(u)