Beispiel #1
0
def test_save_and_load_1d_mesh(tempdir, encoding):
    filename = os.path.join(tempdir, "mesh.xdmf")
    mesh = UnitIntervalMesh(MPI.COMM_WORLD, 32)
    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
Beispiel #2
0
def test_save_points_3D(tempdir, encoding, cell_type):
    mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4, cell_type)
    points = mesh.geometry.points
    vals = np.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)
Beispiel #3
0
    def write_output(self, pb=None, writemesh=False, N=1, t=0):

        if writemesh:

            if self.write_results_every > 0:

                self.resultsfiles = {}
                for res in self.results_to_write:
                    outfile = XDMFFile(
                        self.comm, self.output_path + '/results_' +
                        pb.simname + '_' + res + '.xdmf', 'w')
                    outfile.write_mesh(self.mesh)
                    self.resultsfiles[res] = outfile

            return

        else:

            # write results every write_results_every steps
            if self.write_results_every > 0 and N % self.write_results_every == 0:

                # save solution to XDMF format
                for res in self.results_to_write:

                    if res == 'velocity':
                        self.resultsfiles[res].write_function(pb.v, t)
                    elif res == 'acceleration':  # passed in a is not a function but form, so we have to project
                        a_proj = project(pb.acc,
                                         pb.V_v,
                                         pb.dx_,
                                         nm="Acceleration")
                        self.resultsfiles[res].write_function(a_proj, t)
                    elif res == 'pressure':
                        self.resultsfiles[res].write_function(pb.p, t)
                    elif res == 'cauchystress':
                        stressfuncs = []
                        for n in range(pb.num_domains):
                            stressfuncs.append(pb.ma[n].sigma(pb.v, pb.p))
                        cauchystress = project(stressfuncs,
                                               pb.Vd_tensor,
                                               pb.dx_,
                                               nm="CauchyStress")
                        self.resultsfiles[res].write_function(cauchystress, t)
                    elif res == 'reynolds':
                        reynolds = project(re,
                                           pb.Vd_scalar,
                                           pb.dx_,
                                           nm="Reynolds")
                        self.resultsfiles[res].write_function(reynolds, t)
                    else:
                        raise NameError(
                            "Unknown output to write for fluid mechanics!")
Beispiel #4
0
def test_append_and_load_mesh_functions(tempdir, encoding, data_type):
    dtype_str, dtype = data_type
    meshes = [
        UnitSquareMesh(MPI.comm_world, 12, 12),
        UnitCubeMesh(MPI.comm_world, 2, 2, 2),
        UnitSquareMesh(MPI.comm_world, 12, 12, CellType.quadrilateral),
        UnitCubeMesh(MPI.comm_world, 2, 2, 2, CellType.hexahedron)
    ]

    for mesh in meshes:
        dim = mesh.topology.dim

        vf = MeshFunction(dtype_str, mesh, 0, 0)
        vf.name = "vertices"
        ff = MeshFunction(dtype_str, mesh, mesh.topology.dim - 1, 0)
        ff.name = "facets"
        cf = MeshFunction(dtype_str, mesh, mesh.topology.dim, 0)
        cf.name = "cells"

        # vf.values[:] = mesh.topology.global_indices(0)[:]
        map = mesh.topology.index_map(0)
        vf.values[:] = map.global_indices(True)

        map = mesh.topology.index_map(dim - 1)
        ff.values[:] = map.global_indices(True)

        map = mesh.topology.index_map(dim)
        cf.values[:] = map.global_indices(True)

        filename = os.path.join(
            tempdir, "appended_mf_{0:d}_{1:s}.xdmf".format(
                dim, str(mesh.topology.cell_type)))
        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_vf = vf_in.values - vf.values
        diff_ff = ff_in.values - ff.values
        diff_cf = cf_in.values - cf.values

        assert np.all(diff_vf == 0)
        assert np.all(diff_ff == 0)
        assert np.all(diff_cf == 0)
Beispiel #5
0
def test_read_write_p2_mesh(tempdir, encoding):
    if MPI.COMM_WORLD.rank == 0:
        gmsh = pytest.importorskip("gmsh")
        gmsh.initialize()
        gmsh.model.occ.addSphere(0, 0, 0, 1, tag=1)
        gmsh.option.setNumber("Mesh.CharacteristicLengthMin", 0.3)
        gmsh.option.setNumber("Mesh.CharacteristicLengthMax", 0.4)
        gmsh.model.occ.synchronize()
        gmsh.model.mesh.generate(3)
        gmsh.model.mesh.setOrder(2)

        idx, points, _ = gmsh.model.mesh.getNodes()
        points = points.reshape(-1, 3)
        idx -= 1
        srt = np.argsort(idx)
        assert np.all(idx[srt] == np.arange(len(idx)))
        x = points[srt]

        element_types, element_tags, node_tags = gmsh.model.mesh.getElements(
            dim=3)
        name, dim, order, num_nodes, local_coords, num_first_order_nodes = gmsh.model.mesh.getElementProperties(
            element_types[0])
        cells = node_tags[0].reshape(-1, num_nodes) - 1
        num_nodes, gmsh_cell_id = MPI.COMM_WORLD.bcast(
            [cells.shape[1],
             gmsh.model.mesh.getElementType("tetrahedron", 2)],
            root=0)
        gmsh.finalize()

    else:
        num_nodes, gmsh_cell_id = MPI.COMM_WORLD.bcast([None, None], root=0)
        cells, x = np.empty([0, num_nodes]), np.empty([0, 3])

    domain = ufl_mesh_from_gmsh(gmsh_cell_id, 3)
    cell_type = cpp.mesh.to_type(str(domain.ufl_cell()))
    cells = cells[:, perm_gmsh(cell_type, cells.shape[1])]

    mesh = create_mesh(MPI.COMM_WORLD, cells, x, domain)

    filename = os.path.join(tempdir, "tet10_mesh.xdmf")
    with XDMFFile(mesh.mpi_comm(), filename, "w", encoding=encoding) as xdmf:
        xdmf.write_mesh(mesh)
    with XDMFFile(mesh.mpi_comm(), filename, "r", encoding=encoding) as xdmf:
        mesh2 = xdmf.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_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, "w", encoding=encoding) as file:
        file.write_mesh(mesh)
        u.vector.set(1.0 + (1j if has_petsc_complex else 0))
        file.write_function(u, 0.1)
        u.vector.set(2.0 + (2j if has_petsc_complex else 0))
        file.write_function(u, 0.2)

    with XDMFFile(mesh.mpi_comm(), filename, "a", encoding=encoding) as file:
        u.vector.set(3.0 + (3j if has_petsc_complex else 0))
        file.write_function(u, 0.3)
Beispiel #7
0
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
    dim = mesh.topology.dim
    assert mesh.topology.index_map(
        dim).size_global == mesh2.topology.index_map(dim).size_global
Beispiel #8
0
def test_append_and_load_mesh_value_collections(tempdir, encoding, data_type,
                                                cell_type):
    dtype_str, dtype = data_type
    mesh = UnitCubeMesh(MPI.comm_world, 2, 2, 2, cell_type)
    mesh.create_connectivity_all()

    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)
            map = mesh.topology.index_map(mvc.dim)
            global_indices = map.global_indices(True)

            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 np.all(diff == 0)
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(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)
Beispiel #10
0
def test_save_and_load_2d_mesh(tempdir, encoding, cell_type):
    filename = os.path.join(tempdir, "mesh.xdmf")
    mesh = create_unit_square(MPI.COMM_WORLD, 12, 12, cell_type)
    mesh.name = "square"

    with XDMFFile(mesh.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(name="square")

    assert mesh2.name == mesh.name
    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
Beispiel #11
0
def test_3d(tempdir, cell_type, encoding):
    filename = os.path.join(tempdir, "meshtags_3d.xdmf")
    comm = MPI.COMM_WORLD
    mesh = UnitCubeMesh(comm, 4, 4, 4, cell_type)

    bottom_facets = locate_entities(mesh, 2, lambda x: np.isclose(x[1], 0.0))
    bottom_values = np.full(bottom_facets.shape, 1, dtype=np.intc)
    left_facets = locate_entities(mesh, 2, lambda x: np.isclose(x[0], 0.0))
    left_values = np.full(left_facets.shape, 2, dtype=np.intc)

    indices, pos = np.unique(np.hstack((bottom_facets, left_facets)), return_index=True)
    mt = MeshTags(mesh, 2, indices, np.hstack((bottom_values, left_values))[pos])
    mt.name = "facets"

    top_lines = locate_entities(mesh, 1, lambda x: np.isclose(x[2], 1.0))
    top_values = np.full(top_lines.shape, 3, dtype=np.intc)
    right_lines = locate_entities(mesh, 1, lambda x: np.isclose(x[0], 1.0))
    right_values = np.full(right_lines.shape, 4, dtype=np.intc)

    indices, pos = np.unique(np.hstack((top_lines, right_lines)), return_index=True)
    mt_lines = MeshTags(mesh, 1, indices, np.hstack((top_values, right_values))[pos])
    mt_lines.name = "lines"

    with XDMFFile(comm, filename, "w", encoding=encoding) as file:
        mesh.topology.create_connectivity_all()
        file.write_mesh(mesh)
        file.write_meshtags(mt)
        file.write_meshtags(mt_lines)

    with XDMFFile(comm, filename, "r", encoding=encoding) as file:
        mesh_in = file.read_mesh()
        mesh_in.topology.create_connectivity_all()
        mt_in = file.read_meshtags(mesh_in, "facets")
        mt_lines_in = file.read_meshtags(mesh_in, "lines")
        assert mt_in.name == "facets"
        assert mt_lines_in.name == "lines"

    with XDMFFile(comm, os.path.join(tempdir, "meshtags_3d_out.xdmf"), "w", encoding=encoding) as file:
        file.write_mesh(mesh_in)
        file.write_meshtags(mt_lines_in)
        file.write_meshtags(mt_in)

    # Check number of owned and marked entities
    lines_local = comm.allreduce((mt_lines.indices < mesh.topology.index_map(1).size_local).sum(), op=MPI.SUM)
    lines_local_in = comm.allreduce(
        (mt_lines_in.indices < mesh_in.topology.index_map(1).size_local).sum(), op=MPI.SUM)

    assert lines_local == lines_local_in
Beispiel #12
0
def test_save_3d_tensor(tempdir, encoding, cell_type):
    filename = os.path.join(tempdir, "u3t.xdmf")
    mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4, cell_type)
    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)
Beispiel #13
0
def test_read_mesh_data(tempdir, tdim, n):
    filename = os.path.join(tempdir, "mesh.xdmf")
    mesh = mesh_factory(tdim, n)
    encoding = XDMFFile.Encoding.HDF5
    with XDMFFile(mesh.mpi_comm(), filename, "w", encoding) as file:
        file.write_mesh(mesh)

    with XDMFFile(MPI.COMM_WORLD, filename, "r") as file:
        cell_type = file.read_cell_type()
        cells = file.read_topology_data()
        x = file.read_geometry_data()

    assert cell_type[0] == mesh.topology.cell_type
    assert cell_type[1] == 1
    assert mesh.topology.index_map(tdim).size_global == mesh.mpi_comm().allreduce(cells.shape[0], op=MPI.SUM)
    assert mesh.geometry.index_map().size_global == mesh.mpi_comm().allreduce(x.shape[0], op=MPI.SUM)
Beispiel #14
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[:] = np.arange(mesh.num_entities(1))
    with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file:
        file.write(mf)
Beispiel #15
0
def test_save_3D_cell_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, mesh.topology.dim, 0)
    mf.name = "cells"

    mf.values[:] = np.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 np.all(diff == 0)
Beispiel #16
0
def test_multiple_datasets(tempdir, encoding, cell_type):
    mesh = UnitSquareMesh(MPI.comm_world, 4, 4, cell_type)
    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_2d_tensor(tempdir, encoding, cell_type):
    filename = os.path.join(tempdir, "tensor.xdmf")
    mesh = UnitSquareMesh(MPI.COMM_WORLD, 16, 16, cell_type)
    u = Function(TensorFunctionSpace(mesh, ("Lagrange", 2)))
    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)
Beispiel #18
0
def test_save_3d_vector_series(tempdir, encoding, cell_type):
    filename = os.path.join(tempdir, "u_3D.xdmf")
    mesh = create_unit_cube(MPI.COMM_WORLD, 2, 2, 2, cell_type)
    u = Function(VectorFunctionSpace(mesh, ("Lagrange", 2)))
    with XDMFFile(mesh.comm, filename, "w", encoding=encoding) as file:
        file.write_mesh(mesh)
        u.vector.set(1.0 + (
            1j if np.issubdtype(PETSc.ScalarType, np.complexfloating) else 0))
        file.write_function(u, 0.1)
        u.vector.set(2.0 + (
            2j if np.issubdtype(PETSc.ScalarType, np.complexfloating) else 0))
        file.write_function(u, 0.2)

    with XDMFFile(mesh.comm, filename, "a", encoding=encoding) as file:
        u.vector.set(3.0 + (
            3j if np.issubdtype(PETSc.ScalarType, np.complexfloating) else 0))
        file.write_function(u, 0.3)
Beispiel #19
0
def test_save_2D_vertex_function(tempdir, encoding, data_type, cell_type):
    dtype_str, dtype = data_type
    mesh = UnitSquareMesh(MPI.comm_world, 32, 32, cell_type)
    mf = MeshFunction(dtype_str, mesh, 0, 0)
    mf.name = "vertices"

    global_indices = mesh.topology.index_map(0).global_indices(False)
    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 np.all(diff == 0)
Beispiel #20
0
def test_save_3D_vertex_function(tempdir, encoding, data_type, cell_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, cell_type)
    mf = MeshFunction(dtype_str, mesh, 0, 0)
    mf.values[:] = np.arange(mesh.num_entities(0), dtype=dtype)
    with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file:
        file.write(mf)
Beispiel #21
0
def test_save_2d_vector(tempdir, encoding, cell_type):
    filename = os.path.join(tempdir, "u_2dv.xdmf")
    mesh = UnitSquareMesh(MPI.comm_world, 16, 16, cell_type)
    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)
Beispiel #22
0
def xtest_read_write_p2_function(tempdir):
    mesh = cpp.generation.UnitDiscMesh.create(MPI.comm_world, 3,
                                              cpp.mesh.GhostMode.none)
    gdim = mesh.geometry.dim
    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:

        def expr_eval(x):
            return x[0] + 1.0j * x[0]

        F.interpolate(expr_eval)
    else:

        def expr_eval(x):
            return x[0]

        F.interpolate(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:

        def expr_eval(x):
            return x[:gdim] + 1.0j * x[:gdim]

        F.interpolate(expr_eval)
    else:

        def expr_eval(x):
            return x[:gdim]

        F.interpolate(expr_eval)
    filename = os.path.join(tempdir, "tri6_vector_function.xdmf")
    with XDMFFile(mesh.mpi_comm(), filename,
                  encoding=XDMFFile.Encoding.HDF5) as xdmf:
        xdmf.write(F)
Beispiel #23
0
def test_save_2d_scalar(tempdir, encoding, cell_type):
    filename = os.path.join(tempdir, "u2.xdmf")
    mesh = UnitSquareMesh(MPI.comm_world, 16, 16, cell_type)
    # FIXME: This randomly hangs in parallel
    V = FunctionSpace(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)
def test_save_2d_scalar(tempdir, encoding, cell_type):
    filename = os.path.join(tempdir, "u2.xdmf")
    mesh = UnitSquareMesh(MPI.COMM_WORLD, 12, 12, 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_1d_scalar(tempdir, encoding):
    filename2 = os.path.join(tempdir, "u1_.xdmf")
    mesh = UnitIntervalMesh(MPI.COMM_WORLD, 32)
    V = FunctionSpace(mesh, ("Lagrange", 2))
    u = Function(V)
    u.vector.set(1.0 + (1j if has_petsc_complex else 0))
    with XDMFFile(mesh.mpi_comm(), filename2, "w", encoding=encoding) as file:
        file.write_mesh(mesh)
        file.write_function(u)
Beispiel #26
0
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)
Beispiel #27
0
def test_save_3d_tensor(tempdir, encoding, cell_type):
    filename = os.path.join(tempdir, "u3t.xdmf")
    mesh = create_unit_cube(MPI.COMM_WORLD, 4, 4, 4, cell_type)
    u = Function(TensorFunctionSpace(mesh, ("Lagrange", 2)))
    u.vector.set(1.0 + (
        1j if np.issubdtype(PETSc.ScalarType, np.complexfloating) else 0))
    with XDMFFile(mesh.comm, filename, "w", encoding=encoding) as file:
        file.write_mesh(mesh)
        file.write_function(u)
Beispiel #28
0
def test_save_3d_scalar(tempdir, encoding, cell_type):
    filename = os.path.join(tempdir, "u3.xdmf")
    mesh = create_unit_cube(MPI.COMM_WORLD, 4, 3, 4, cell_type)
    V = FunctionSpace(mesh, ("Lagrange", 2))
    u = Function(V)
    u.vector.set(1.0)
    with XDMFFile(mesh.comm, filename, "w", encoding=encoding) as file:
        file.write_mesh(mesh)
        file.write_function(u)
Beispiel #29
0
def test_save_1d_scalar(tempdir, encoding):
    filename2 = os.path.join(tempdir, "u1_.xdmf")
    mesh = UnitIntervalMesh(MPI.comm_world, 32)
    # FIXME: This randomly hangs in parallel
    V = FunctionSpace(mesh, ("Lagrange", 2))
    u = Function(V)
    u.vector.set(1.0 + (1j if has_petsc_complex else 0))
    with XDMFFile(mesh.mpi_comm(), filename2, encoding=encoding) as file:
        file.write(u)
def xtest_submesh(tempdir, d, n, codim, ghost_mode, encoding):
    mesh = mesh_factory(d, n, ghost_mode)
    edim = d - codim
    entities = locate_entities(mesh, edim, lambda x: x[0] >= 0.5)
    submesh = create_submesh(mesh, edim, entities)[0]

    filename = os.path.join(tempdir, "submesh.xdmf")
    # Check writing the mesh doesn't cause a segmentation fault
    with XDMFFile(mesh.comm, filename, "w", encoding=encoding) as xdmf:
        xdmf.write_mesh(submesh)