Esempio n. 1
0
def _create_tempdir(request):
    # Get directory name of test_foo.py file
    testfile = request.module.__file__
    testfiledir = os.path.dirname(os.path.abspath(testfile))

    # Construct name test_foo_tempdir from name test_foo.py
    testfilename = os.path.basename(testfile)
    outputname = testfilename.replace(".py", "_tempdir_{}".format(
        worker_id(request)))

    # Get function name test_something from test_foo.py
    function = request.function.__name__

    # Join all of these to make a unique path for this test function
    basepath = os.path.join(testfiledir, outputname)
    path = os.path.join(basepath, function)

    # Add a sequence number to avoid collisions when tests are
    # otherwise parameterized
    if MPI.rank(MPI.comm_world) == 0:
        _create_tempdir._sequencenumber[path] += 1
        sequencenumber = _create_tempdir._sequencenumber[path]
        sequencenumber = MPI.sum(MPI.comm_world, sequencenumber)
    else:
        sequencenumber = MPI.sum(MPI.comm_world, 0)
    path += "__" + str(sequencenumber)

    # Delete and re-create directory on root node
    if MPI.rank(MPI.comm_world) == 0:
        # First time visiting this basepath, delete the old and create
        # a new
        if basepath not in _create_tempdir._basepaths:
            _create_tempdir._basepaths.add(basepath)
            if os.path.exists(basepath):
                shutil.rmtree(basepath)
            # Make sure we have the base path test_foo_tempdir for
            # this test_foo.py file
            if not os.path.exists(basepath):
                os.mkdir(basepath)

        # Delete path from old test run
        if os.path.exists(path):
            shutil.rmtree(path)
        # Make sure we have the path for this test execution:
        # e.g. test_foo_tempdir/test_something__3
        if not os.path.exists(path):
            os.mkdir(path)
    MPI.barrier(MPI.comm_world)

    return path
def test_mesh_function_assign_2D_cells():
    mesh = UnitSquareMesh(MPI.comm_world, 3, 3)
    ncells = mesh.num_cells()
    f = MeshFunction("int", mesh, mesh.topology.dim, 0)
    for c in range(ncells):
        f.values[c] = ncells - c

    g = MeshValueCollection("int", mesh, 2)
    g.assign(f)
    assert ncells == len(f.values)
    assert ncells == g.size()

    f2 = MeshFunction("int", mesh, g, 0)

    for c in range(mesh.num_cells()):
        value = ncells - c
        assert value == g.get_value(c, 0)
        assert f2.values[c] == g.get_value(c, 0)

    h = MeshValueCollection("int", mesh, 2)
    global_indices = mesh.topology.index_map(2).global_indices(True)

    ncells_global = mesh.num_entities_global(2)
    for c in range(mesh.num_cells()):
        if global_indices[c] in [5, 8, 10]:
            continue
        value = ncells_global - global_indices[c]
        h.set_value(c, int(value))

    f3 = MeshFunction("int", mesh, h, 0)

    values = f3.values
    values[values > ncells_global] = 0.

    assert MPI.sum(mesh.mpi_comm(), values.sum() * 1.0) == 140.
Esempio n. 3
0
def test_UnitSquareMeshDistributed():
    """Create mesh of unit square."""
    mesh = UnitSquareMesh(MPI.comm_world, 5, 7)
    assert mesh.num_entities_global(0) == 48
    assert mesh.num_entities_global(2) == 70
    assert mesh.geometry.dim == 2
    assert MPI.sum(mesh.mpi_comm(), mesh.topology.index_map(0).size_local) == 48
Esempio n. 4
0
def test_UnitCubeMeshDistributed():
    """Create mesh of unit cube."""
    mesh = UnitCubeMesh(MPI.comm_world, 5, 7, 9)
    assert mesh.num_entities_global(0) == 480
    assert mesh.num_entities_global(3) == 1890
    assert mesh.geometry.dim == 3
    assert MPI.sum(mesh.mpi_comm(), mesh.topology.index_map(0).size_local) == 480
Esempio n. 5
0
def test_UnitQuadMesh():
    mesh = UnitSquareMesh(MPI.comm_world, 5, 7, CellType.quadrilateral)
    assert mesh.num_entities_global(0) == 48
    assert mesh.num_entities_global(2) == 35
    assert mesh.geometry.dim == 2
    assert MPI.sum(mesh.mpi_comm(),
                   mesh.topology.index_map(0).size_local) == 48
Esempio n. 6
0
def test_gmsh_input_quad(order):
    pygmsh = pytest.importorskip("pygmsh")

    # Parameterize test if gmsh gets wider support
    R = 1
    res = 0.2 if order == 2 else 0.2
    algorithm = 2 if order == 2 else 5
    element = "quad{0:d}".format(int((order + 1)**2))

    geo = pygmsh.opencascade.Geometry()
    geo.add_raw_code("Mesh.ElementOrder={0:d};".format(order))
    geo.add_ball([0, 0, 0], R, char_length=res)
    geo.add_raw_code("Recombine Surface {1};")
    geo.add_raw_code("Mesh.Algorithm = {0:d};".format(algorithm))

    msh = pygmsh.generate_mesh(geo, verbose=True, dim=2)

    if order > 2:
        # Quads order > 3 have a gmsh specific ordering, and has to be permuted.
        msh_to_dolfin = np.array([0, 3, 11, 10, 1, 2, 6, 7, 4, 9, 12, 15, 5, 8, 13, 14])
        cells = np.zeros(msh.cells_dict[element].shape)
        for i in range(len(cells)):
            for j in range(len(msh_to_dolfin)):
                cells[i, j] = msh.cells_dict[element][i, msh_to_dolfin[j]]
    else:
        # XDMF does not support higher order quads
        cells = permute_cell_ordering(msh.cells_dict[element], permutation_vtk_to_dolfin(
            CellType.quadrilateral, msh.cells_dict[element].shape[1]))

    mesh = Mesh(MPI.comm_world, CellType.quadrilateral, msh.points, cells,
                [], GhostMode.none)
    surface = assemble_scalar(1 * dx(mesh))

    assert MPI.sum(mesh.mpi_comm(), surface) == pytest.approx(4 * np.pi * R * R, rel=1e-5)
Esempio n. 7
0
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
Esempio n. 8
0
def test_topology_surface(cube):
    surface_vertex_markers = cube.topology.on_boundary(0)
    assert surface_vertex_markers
    n = 3
    cube.create_entities(1)
    cube.create_connectivity(2, 1)
    surface_edge_markers = cube.topology.on_boundary(1)
    assert surface_edge_markers
    surface_facet_markers = cube.topology.on_boundary(2)
    sf_count = np.count_nonzero(np.array(surface_facet_markers))
    assert MPI.sum(cube.mpi_comm(), sf_count) == n * n * 12
Esempio n. 9
0
def test_third_order_tri():
    #  *---*---*---*   3--11--10--2
    #  | \         |   | \        |
    #  *   *   *   *   8   7  15  13
    #  |     \     |   |    \     |
    #  *  *    *   *   9  14  6   12
    #  |         \ |   |        \ |
    #  *---*---*---*   0--4---5---1
    for H in (1.0, 2.0):
        for Z in (0.0, 0.5):
            L = 1
            points = np.array([
                [0, 0, 0],
                [L, 0, 0],
                [L, H, Z],
                [0, H, Z],  # 0, 1, 2, 3
                [L / 3, 0, 0],
                [2 * L / 3, 0, 0],  # 4, 5
                [2 * L / 3, H / 3, 0],
                [L / 3, 2 * H / 3, 0],  # 6, 7
                [0, 2 * H / 3, 0],
                [0, H / 3, 0],  # 8, 9
                [2 * L / 3, H, Z],
                [L / 3, H, Z],  # 10, 11
                [L, H / 3, 0],
                [L, 2 * H / 3, 0],  # 12, 13
                [L / 3, H / 3, 0],  # 14
                [2 * L / 3, 2 * H / 3, 0]
            ])  # 15
            cells = np.array([[0, 1, 3, 4, 5, 6, 7, 8, 9, 14],
                              [1, 2, 3, 12, 13, 10, 11, 7, 6, 15]])
            cells = permute_cell_ordering(
                cells,
                permutation_vtk_to_dolfin(CellType.triangle, cells.shape[1]))
            mesh = Mesh(MPI.comm_world, CellType.triangle, points, cells, [],
                        GhostMode.none)

            def e2(x):
                return x[2] + x[0] * x[1]

            degree = mesh.degree()
            # Interpolate function
            V = FunctionSpace(mesh, ("CG", degree))
            u = Function(V)
            cmap = fem.create_coordinate_map(mesh.ufl_domain())
            mesh.geometry.coord_mapping = cmap
            u.interpolate(e2)

            intu = assemble_scalar(u * dx(metadata={"quadrature_degree": 40}))
            intu = MPI.sum(mesh.mpi_comm(), intu)

            nodes = [0, 9, 8, 3]
            ref = sympy_scipy(points, nodes, L, H)
            assert ref == pytest.approx(intu, rel=1e-6)
Esempio n. 10
0
def test_third_order_quad(L, H, Z):
    """Test by comparing integration of z+x*y against sympy/scipy integration
    of a quad element. Z>0 implies curved element.

      *---------*   3--8--9--2-22-23-17
      |         |   |        |       |
      |         |   11 14 15 7 26 27 21
      |         |   |        |       |
      |         |   10 12 13 6 24 25 20
      |         |   |        |       |
      *---------*   0--4--5--1-18-19-16

    """
    points = np.array([[0, 0, 0], [L, 0, 0], [L, H, Z], [0, H, Z],        # 0  1 2 3
                       [L / 3, 0, 0], [2 * L / 3, 0, 0],                  # 4  5
                       [L, H / 3, 0], [L, 2 * H / 3, 0],                  # 6  7
                       [L / 3, H, Z], [2 * L / 3, H, Z],                  # 8  9
                       [0, H / 3, 0], [0, 2 * H / 3, 0],                  # 10 11
                       [L / 3, H / 3, 0], [2 * L / 3, H / 3, 0],          # 12 13
                       [L / 3, 2 * H / 3, 0], [2 * L / 3, 2 * H / 3, 0],  # 14 15
                       [2 * L, 0, 0], [2 * L, H, Z],                      # 16 17
                       [4 * L / 3, 0, 0], [5 * L / 3, 0, 0],              # 18 19
                       [2 * L, H / 3, 0], [2 * L, 2 * H / 3, 0],          # 20 21
                       [4 * L / 3, H, Z], [5 * L / 3, H, Z],              # 22 23
                       [4 * L / 3, H / 3, 0], [5 * L / 3, H / 3, 0],           # 24 25
                       [4 * L / 3, 2 * H / 3, 0], [5 * L / 3, 2 * H / 3, 0]])  # 26 27

    # Change to multiple cells when matthews dof-maps work for quads
    cells = np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
                      [1, 16, 17, 2, 18, 19, 20, 21, 22, 23, 6, 7, 24, 25, 26, 27]])

    cells = permute_cell_ordering(cells, permutation_vtk_to_dolfin(CellType.quadrilateral, cells.shape[1]))
    mesh = Mesh(MPI.comm_world, CellType.quadrilateral, points, cells,
                [], GhostMode.none)

    def e2(x):
        return x[2] + x[0] * x[1]

    # Interpolate function
    V = FunctionSpace(mesh, ("CG", 3))
    u = Function(V)
    cmap = fem.create_coordinate_map(mesh.ufl_domain())

    mesh.geometry.coord_mapping = cmap

    u.interpolate(e2)

    intu = assemble_scalar(u * dx(mesh))
    intu = MPI.sum(mesh.mpi_comm(), intu)

    nodes = [0, 3, 10, 11]
    ref = sympy_scipy(points, nodes, 2 * L, H)
    assert ref == pytest.approx(intu, rel=1e-6)
Esempio n. 11
0
def test_fourth_order_tri():
    L = 1
    #  *--*--*--*--*   3-21-20-19--2
    #  | \         |   | \         |
    #  *   *  * *  *   10 9 24 23  18
    #  |     \     |   |    \      |
    #  *  *   *  * *   11 15  8 22 17
    #  |       \   |   |       \   |
    #  *  * *   *  *   12 13 14 7  16
    #  |         \ |   |         \ |
    #  *--*--*--*--*   0--4--5--6--1
    for H in (1.0, 2.0):
        for Z in (0.0, 0.5):
            points = np.array(
                [[0, 0, 0], [L, 0, 0], [L, H, Z], [0, H, Z],   # 0, 1, 2, 3
                 [L / 4, 0, 0], [L / 2, 0, 0], [3 * L / 4, 0, 0],  # 4, 5, 6
                 [3 / 4 * L, H / 4, Z / 2], [L / 2, H / 2, 0],         # 7, 8
                 [L / 4, 3 * H / 4, 0], [0, 3 * H / 4, 0],         # 9, 10
                 [0, H / 2, 0], [0, H / 4, Z / 2],                     # 11, 12
                 [L / 4, H / 4, Z / 2], [L / 2, H / 4, Z / 2], [L / 4, H / 2, 0],  # 13, 14, 15
                 [L, H / 4, Z / 2], [L, H / 2, 0], [L, 3 * H / 4, 0],          # 16, 17, 18
                 [3 * L / 4, H, Z], [L / 2, H, Z], [L / 4, H, Z],          # 19, 20, 21
                 [3 * L / 4, H / 2, 0], [3 * L / 4, 3 * H / 4, 0],         # 22, 23
                 [L / 2, 3 * H / 4, 0]]                                    # 24
            )

            cells = np.array([[0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
                              [1, 2, 3, 16, 17, 18, 19, 20, 21, 9, 8, 7, 22, 23, 24]])
            cells = permute_cell_ordering(cells, permutation_vtk_to_dolfin(CellType.triangle, cells.shape[1]))

            mesh = Mesh(MPI.comm_world, CellType.triangle, points, cells,
                        [], GhostMode.none)

            def e2(x):
                return x[2] + x[0] * x[1]
            degree = mesh.degree()
            # Interpolate function
            V = FunctionSpace(mesh, ("CG", degree))
            u = Function(V)
            cmap = fem.create_coordinate_map(mesh.ufl_domain())
            mesh.geometry.coord_mapping = cmap
            u.interpolate(e2)

            intu = assemble_scalar(u * dx(metadata={"quadrature_degree": 50}))
            intu = MPI.sum(mesh.mpi_comm(), intu)
            nodes = [0, 3, 10, 11, 12]
            ref = sympy_scipy(points, nodes, L, H)
            assert ref == pytest.approx(intu, rel=1e-4)
Esempio n. 12
0
def test_second_order_tri():
    # Test second order mesh by computing volume of two cells
    #  *-----*-----*   3----6-----2
    #  | \         |   | \        |
    #  |   \       |   |   \      |
    #  *     *     *   7     8    5
    #  |       \   |   |      \   |
    #  |         \ |   |        \ |
    #  *-----*-----*   0----4-----1
    for H in (1.0, 2.0):
        for Z in (0.0, 0.5):
            L = 1
            points = np.array([[0, 0, 0], [L, 0, 0], [L, H, Z], [0, H, Z],
                               [L / 2, 0, 0], [L, H / 2, 0], [L / 2, H, Z],
                               [0, H / 2, 0], [L / 2, H / 2, 0]])

            cells = np.array([[0, 1, 3, 4, 8, 7], [1, 2, 3, 5, 6, 8]])
            cells = permute_cell_ordering(
                cells,
                permutation_vtk_to_dolfin(CellType.triangle, cells.shape[1]))
            mesh = Mesh(MPI.comm_world, CellType.triangle, points, cells, [],
                        GhostMode.none)

            def e2(x):
                return x[2] + x[0] * x[1]

            degree = mesh.degree()
            # Interpolate function
            V = FunctionSpace(mesh, ("CG", degree))
            u = Function(V)
            cmap = fem.create_coordinate_map(mesh.ufl_domain())

            mesh.geometry.coord_mapping = cmap
            u.interpolate(e2)

            intu = assemble_scalar(
                u * dx(mesh, metadata={"quadrature_degree": 20}))
            intu = MPI.sum(mesh.mpi_comm(), intu)

            nodes = [0, 3, 7]
            ref = sympy_scipy(points, nodes, L, H)
            assert ref == pytest.approx(intu, rel=1e-6)
Esempio n. 13
0
def test_second_order_quad(L, H, Z):
    """ Test by comparing integration of z+x*y against sympy/scipy
    integration of a quad element. Z>0 implies curved element.

      *-----*   3--6--2
      |     |   |     |
      |     |   7  8  5
      |     |   |     |
      *-----*   0--4--1

    """

    points = np.array([[0, 0, 0], [L, 0, 0], [L, H, Z], [0, H, Z],
                       [L / 2, 0, 0], [L, H / 2, 0], [L / 2, H, Z],
                       [0, H / 2, 0], [L / 2, H / 2, 0], [2 * L, 0, 0],
                       [2 * L, H, Z]])
    cells = np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8]])
    cells = permute_cell_ordering(
        cells, permutation_vtk_to_dolfin(CellType.quadrilateral,
                                         cells.shape[1]))

    mesh = Mesh(MPI.comm_world, CellType.quadrilateral, points, cells, [],
                GhostMode.none)

    def e2(x):
        return x[2] + x[0] * x[1]

    # Interpolate function
    V = FunctionSpace(mesh, ("CG", 2))
    u = Function(V)
    cmap = fem.create_coordinate_map(mesh.ufl_domain())

    mesh.geometry.coord_mapping = cmap

    u.interpolate(e2)

    intu = assemble_scalar(u * dx(mesh))
    intu = MPI.sum(mesh.mpi_comm(), intu)

    nodes = [0, 3, 7]
    ref = sympy_scipy(points, nodes, L, H)
    assert ref == pytest.approx(intu, rel=1e-6)
Esempio n. 14
0
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
Esempio n. 15
0
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))
Esempio n. 16
0

# "Exact" solution expression
def solution(values, x):
    values[:, 0] = A * np.cos(k0 * x[:, 0]) * np.cos(k0 * x[:, 1])


# Function space for exact solution - need it to be higher than deg
V_exact = FunctionSpace(mesh, ("Lagrange", deg + 3))
u_exact = Function(V_exact)
u_exact.interpolate(lambda x: A * np.cos(k0 * x[0]) * np.cos(k0 * x[1]))

# best approximation from V
# u_BA = project(u_exact, V)

# H1 errors
diff = u - u_exact
# diff_BA = u_BA - u_exact
H1_diff = MPI.sum(mesh.mpi_comm(), assemble_scalar(inner(grad(diff), grad(diff)) * dx))
# H1_BA = MPI.sum(mesh.mpi_comm(), assemble_scalar(inner(grad(diff_BA), grad(diff_BA)) * dx))
H1_exact = MPI.sum(mesh.mpi_comm(), assemble_scalar(inner(grad(u_exact), grad(u_exact)) * dx))
# print("Relative H1 error of best approximation:", abs(np.sqrt(H1_BA) / np.sqrt(H1_exact)))
print("Relative H1 error of FEM solution:", abs(np.sqrt(H1_diff) / np.sqrt(H1_exact)))

# L2 errors
L2_diff = MPI.sum(mesh.mpi_comm(), assemble_scalar(inner(diff, diff) * dx))
# L2_BA = MPI.sum(mesh.mpi_comm(), assemble_scalar(inner(diff_BA, diff_BA) * dx))
L2_exact = MPI.sum(mesh.mpi_comm(), assemble_scalar(inner(u_exact, u_exact) * dx))
# print("Relative L2 error  of best approximation:", abs(np.sqrt(L2_BA) / np.sqrt(L2_exact)))
print("Relative L2 error of FEM solution:", abs(np.sqrt(L2_diff) / np.sqrt(L2_exact)))
Esempio n. 17
0
def test_GetCells():
    """Get cells of mesh"""
    mesh = UnitSquareMesh(MPI.comm_world, 5, 5)
    assert MPI.sum(mesh.mpi_comm(), len(mesh.cells())) == 50
Esempio n. 18
0
def test_manufactured_poisson_dg(degree, filename, datadir):
    """ Manufactured Poisson problem, solving u = x[component]**n, where n is the
    degree of the Lagrange function space.

    """
    with XDMFFile(MPI.comm_world, os.path.join(datadir, filename)) as xdmf:
        if MPI.size(MPI.comm_world) == 1:  # Serial
            mesh = xdmf.read_mesh(GhostMode.none)
        else:
            mesh = xdmf.read_mesh(GhostMode.shared_facet)

    V = FunctionSpace(mesh, ("DG", degree))
    u, v = TrialFunction(V), TestFunction(V)

    # Exact solution
    x = SpatialCoordinate(mesh)
    u_exact = x[1] ** degree

    # Coefficient
    k = Function(V)
    k.vector.set(2.0)
    k.vector.ghostUpdate(addv=PETSc.InsertMode.INSERT, mode=PETSc.ScatterMode.FORWARD)

    # Source term
    f = - div(k * grad(u_exact))

    # Mesh normals and element size
    n = FacetNormal(mesh)
    h = CellDiameter(mesh)
    h_avg = (h("+") + h("-")) / 2.0

    # Penalty parameter
    alpha = 32

    dx_ = dx(metadata={"quadrature_degree": -1})
    ds_ = ds(metadata={"quadrature_degree": -1})
    dS_ = dS(metadata={"quadrature_degree": -1})

    a = inner(k * grad(u), grad(v)) * dx_ \
        - k("+") * inner(avg(grad(u)), jump(v, n)) * dS_ \
        - k("+") * inner(jump(u, n), avg(grad(v))) * dS_ \
        + k("+") * (alpha / h_avg) * inner(jump(u, n), jump(v, n)) * dS_ \
        - inner(k * grad(u), v * n) * ds_ \
        - inner(u * n, k * grad(v)) * ds_ \
        + (alpha / h) * inner(k * u, v) * ds_
    L = inner(f, v) * dx_ - inner(k * u_exact * n, grad(v)) * ds_ \
        + (alpha / h) * inner(k * u_exact, v) * ds_

    for integral in a.integrals():
        integral.metadata()["quadrature_degree"] = ufl.algorithms.estimate_total_polynomial_degree(a)
    for integral in L.integrals():
        integral.metadata()["quadrature_degree"] = ufl.algorithms.estimate_total_polynomial_degree(L)

    b = assemble_vector(L)
    b.ghostUpdate(addv=PETSc.InsertMode.ADD, mode=PETSc.ScatterMode.REVERSE)

    A = assemble_matrix(a, [])
    A.assemble()

    # Create LU linear solver
    solver = PETSc.KSP().create(MPI.comm_world)
    solver.setType(PETSc.KSP.Type.PREONLY)
    solver.getPC().setType(PETSc.PC.Type.LU)
    solver.setOperators(A)

    # Solve
    uh = Function(V)
    solver.solve(b, uh.vector)
    uh.vector.ghostUpdate(addv=PETSc.InsertMode.INSERT,
                          mode=PETSc.ScatterMode.FORWARD)

    error = assemble_scalar((u_exact - uh)**2 * dx)
    error = MPI.sum(mesh.mpi_comm(), error)

    assert np.absolute(error) < 1.0e-14
Esempio n. 19
0
def test_volume_cells(mesh):
    num_cells = mesh.num_entities(mesh.topology.dim)
    v = cpp.mesh.volume_entities(mesh, range(num_cells), mesh.topology.dim)
    v = MPI.sum(mesh.mpi_comm(), v.sum())
    assert v == pytest.approx(1.0, rel=1e-9)
def test_diff_then_integrate():

    # Define 1D geometry
    n = 21
    mesh = UnitIntervalMesh(MPI.comm_world, n)

    # Shift and scale mesh
    x0, x1 = 1.5, 3.14
    mesh.coordinates()[:] *= (x1 - x0)
    mesh.coordinates()[:] += x0

    x = SpatialCoordinate(mesh)[0]
    xs = 0.1 + 0.8 * x / x1  # scaled to be within [0.1,0.9]

    # Define list of expressions to test, and configure
    # accuracies these expressions are known to pass with.
    # The reason some functions are less accurately integrated is
    # likely that the default choice of quadrature rule is not perfect
    F_list = []

    def reg(exprs, acc=10):
        for expr in exprs:
            F_list.append((expr, acc))

    # FIXME: 0*dx and 1*dx fails in the ufl-ffcx-jit framework somewhere
    # reg([Constant(0.0, cell=cell)])
    # reg([Constant(1.0, cell=cell)])
    monomial_list = [x**q for q in range(2, 6)]
    reg(monomial_list)
    reg([2.3 * p + 4.5 * q for p in monomial_list for q in monomial_list])
    reg([x**x])
    reg([x**(x**2)], 8)
    reg([x**(x**3)], 6)
    reg([x**(x**4)], 2)
    # Special functions:
    reg([atan(xs)], 8)
    reg([sin(x), cos(x), exp(x)], 5)
    reg([ln(xs), pow(x, 2.7), pow(2.7, x)], 3)
    reg([asin(xs), acos(xs)], 1)
    reg([tan(xs)], 7)

    try:
        import scipy
    except ImportError:
        scipy = None

    if hasattr(math, 'erf') or scipy is not None:
        reg([erf(xs)])
    else:
        print("Warning: skipping test of erf, old python version and no scipy.")

    # if 0:
    #     print("Warning: skipping tests of bessel functions, doesn't build on all platforms.")
    # elif scipy is None:
    #     print("Warning: skipping tests of bessel functions, missing scipy.")
    # else:
    #     for nu in (0, 1, 2):
    #         # Many of these are possibly more accurately integrated,
    #         # but 4 covers all and is sufficient for this test
    #         reg([bessel_J(nu, xs), bessel_Y(nu, xs), bessel_I(nu, xs), bessel_K(nu, xs)], 4)

    # To handle tensor algebra, make an x dependent input tensor
    # xx and square all expressions
    def reg2(exprs, acc=10):
        for expr in exprs:
            F_list.append((inner(expr, expr), acc))
    xx = as_matrix([[2 * x**2, 3 * x**3], [11 * x**5, 7 * x**4]])
    x3v = as_vector([3 * x**2, 5 * x**3, 7 * x**4])
    cc = as_matrix([[2, 3], [4, 5]])
    reg2([xx])
    reg2([x3v])
    reg2([cross(3 * x3v, as_vector([-x3v[1], x3v[0], x3v[2]]))])
    reg2([xx.T])
    reg2([tr(xx)])
    reg2([det(xx)])
    reg2([dot(xx, 0.1 * xx)])
    reg2([outer(xx, xx.T)])
    reg2([dev(xx)])
    reg2([sym(xx)])
    reg2([skew(xx)])
    reg2([elem_mult(7 * xx, cc)])
    reg2([elem_div(7 * xx, xx + cc)])
    reg2([elem_pow(1e-3 * xx, 1e-3 * cc)])
    reg2([elem_pow(1e-3 * cc, 1e-3 * xx)])
    reg2([elem_op(lambda z: sin(z) + 2, 0.03 * xx)], 2)  # pretty inaccurate...

    # FIXME: Add tests for all UFL operators:
    # These cause discontinuities and may be harder to test in the
    # above fashion:
    # 'inv', 'cofac',
    # 'eq', 'ne', 'le', 'ge', 'lt', 'gt', 'And', 'Or', 'Not',
    # 'conditional', 'sign',
    # 'jump', 'avg',
    # 'LiftingFunction', 'LiftingOperator',

    # FIXME: Test other derivatives: (but algorithms for operator
    # derivatives are the same!):
    # 'variable', 'diff',
    # 'Dx', 'grad', 'div', 'curl', 'rot', 'Dn', 'exterior_derivative',

    # Run through all operators defined above and compare integrals
    debug = 0
    for F, acc in F_list:
        # Apply UFL differentiation
        f = diff(F, SpatialCoordinate(mesh))[..., 0]
        if debug:
            print(F)
            print(x)
            print(f)

        # Apply integration with DOLFINX
        # (also passes through form compilation and jit)
        M = f * dx
        f_integral = assemble_scalar(M)  # noqa
        f_integral = MPI.sum(mesh.mpi_comm(), f_integral)

        # Compute integral of f manually from anti-derivative F
        # (passes through pybind11 interface and uses UFL evaluation)
        F_diff = F((x1,)) - F((x0,))

        # Compare results. Using custom relative delta instead
        # of decimal digits here because some numbers are >> 1.
        delta = min(abs(f_integral), abs(F_diff)) * 10**-acc
        assert f_integral - F_diff <= delta
Esempio n. 21
0
def test_fourth_order_quad(L, H, Z):
    """Test by comparing integration of z+x*y against sympy/scipy integration
    of a quad element. Z>0 implies curved element.

      *---------*   20-21-22-23-24-41--42--43--44
      |         |   |           |              |
      |         |   15 16 17 18 19 37  38  39  40
      |         |   |           |              |
      |         |   10 11 12 13 14 33  34  35  36
      |         |   |           |              |
      |         |   5  6  7  8  9  29  30  31  32
      |         |   |           |              |
      *---------*   0--1--2--3--4--25--26--27--28

    """
    points = np.array([
        [0, 0, 0],
        [L / 4, 0, 0],
        [L / 2, 0, 0],  # 0 1 2
        [3 * L / 4, 0, 0],
        [L, 0, 0],  # 3 4
        [0, H / 4, -Z / 3],
        [L / 4, H / 4, -Z / 3],
        [L / 2, H / 4, -Z / 3],  # 5 6 7
        [3 * L / 4, H / 4, -Z / 3],
        [L, H / 4, -Z / 3],  # 8 9
        [0, H / 2, 0],
        [L / 4, H / 2, 0],
        [L / 2, H / 2, 0],  # 10 11 12
        [3 * L / 4, H / 2, 0],
        [L, H / 2, 0],  # 13 14
        [0, (3 / 4) * H, 0],
        [L / 4, (3 / 4) * H, 0],  # 15 16
        [L / 2, (3 / 4) * H, 0],
        [3 * L / 4, (3 / 4) * H, 0],  # 17 18
        [L, (3 / 4) * H, 0],
        [0, H, Z],
        [L / 4, H, Z],  # 19 20 21
        [L / 2, H, Z],
        [3 * L / 4, H, Z],
        [L, H, Z],  # 22 23 24
        [(5 / 4) * L, 0, 0],
        [(6 / 4) * L, 0, 0],  # 25 26
        [(7 / 4) * L, 0, 0],
        [2 * L, 0, 0],  # 27 28
        [(5 / 4) * L, H / 4, -Z / 3],
        [(6 / 4) * L, H / 4, -Z / 3],  # 29 30
        [(7 / 4) * L, H / 4, -Z / 3],
        [2 * L, H / 4, -Z / 3],  # 31 32
        [(5 / 4) * L, H / 2, 0],
        [(6 / 4) * L, H / 2, 0],  # 33 34
        [(7 / 4) * L, H / 2, 0],
        [2 * L, H / 2, 0],  # 35 36
        [(5 / 4) * L, 3 / 4 * H, 0],  # 37
        [(6 / 4) * L, 3 / 4 * H, 0],  # 38
        [(7 / 4) * L, 3 / 4 * H, 0],
        [2 * L, 3 / 4 * H, 0],  # 39 40
        [(5 / 4) * L, H, Z],
        [(6 / 4) * L, H, Z],  # 41 42
        [(7 / 4) * L, H, Z],
        [2 * L, H, Z]
    ])  # 43 44

    # VTK ordering
    cells = np.array([[
        0, 4, 24, 20, 1, 2, 3, 9, 14, 19, 21, 22, 23, 5, 10, 15, 6, 7, 8, 11,
        12, 13, 16, 17, 18
    ],
                      [
                          4, 28, 44, 24, 25, 26, 27, 32, 36, 40, 41, 42, 43, 9,
                          14, 19, 29, 30, 31, 33, 34, 35, 37, 38, 39
                      ]])

    cells = permute_cell_ordering(
        cells, permutation_vtk_to_dolfin(CellType.quadrilateral,
                                         cells.shape[1]))
    mesh = Mesh(MPI.comm_world, CellType.quadrilateral, points, cells, [],
                GhostMode.none)

    def e2(x):
        return x[2] + x[0] * x[1]

    V = FunctionSpace(mesh, ("CG", 4))
    u = Function(V)
    cmap = fem.create_coordinate_map(mesh.ufl_domain())

    mesh.geometry.coord_mapping = cmap

    u.interpolate(e2)

    intu = assemble_scalar(u * dx(mesh))
    intu = MPI.sum(mesh.mpi_comm(), intu)

    nodes = [0, 5, 10, 15, 20]
    ref = sympy_scipy(points, nodes, 2 * L, H)
    assert ref == pytest.approx(intu, rel=1e-5)
Esempio n. 22
0
def test_xdmf_input_tri(datadir):
    with XDMFFile(MPI.comm_world, os.path.join(datadir, "mesh.xdmf")) as xdmf:
        mesh = xdmf.read_mesh(GhostMode.none)
    surface = assemble_scalar(1 * dx(mesh))
    assert MPI.sum(mesh.mpi_comm(), surface) == pytest.approx(4 * np.pi,
                                                              rel=1e-4)
Esempio n. 23
0
def test_manufactured_poisson(degree, filename, datadir):
    """ Manufactured Poisson problem, solving u = x[1]**p, where p is the
    degree of the Lagrange function space.

    """

    with XDMFFile(MPI.comm_world, os.path.join(datadir, filename)) as xdmf:
        mesh = xdmf.read_mesh(GhostMode.none)

    V = FunctionSpace(mesh, ("Lagrange", degree))
    u, v = TrialFunction(V), TestFunction(V)
    a = inner(grad(u), grad(v)) * dx

    # Get quadrature degree for bilinear form integrand (ignores effect
    # of non-affine map)
    a = inner(grad(u), grad(v)) * dx(metadata={"quadrature_degree": -1})
    a.integrals()[0].metadata(
    )["quadrature_degree"] = ufl.algorithms.estimate_total_polynomial_degree(a)

    # Source term
    x = SpatialCoordinate(mesh)
    u_exact = x[1]**degree
    f = -div(grad(u_exact))

    # Set quadrature degree for linear form integrand (ignores effect of
    # non-affine map)
    L = inner(f, v) * dx(metadata={"quadrature_degree": -1})
    L.integrals()[0].metadata(
    )["quadrature_degree"] = ufl.algorithms.estimate_total_polynomial_degree(L)

    t0 = time.time()
    L = fem.Form(L)
    t1 = time.time()
    print("Linear form compile time:", t1 - t0)

    u_bc = Function(V)
    u_bc.interpolate(lambda x: x[1]**degree)

    # Create Dirichlet boundary condition
    mesh.create_connectivity_all()
    facetdim = mesh.topology.dim - 1
    bndry_facets = np.where(
        np.array(mesh.topology.on_boundary(facetdim)) == 1)[0]
    bdofs = locate_dofs_topological(V, facetdim, bndry_facets)
    assert (len(bdofs) < V.dim())
    bc = DirichletBC(u_bc, bdofs)

    t0 = time.time()
    b = assemble_vector(L)
    apply_lifting(b, [a], [[bc]])
    b.ghostUpdate(addv=PETSc.InsertMode.ADD, mode=PETSc.ScatterMode.REVERSE)
    set_bc(b, [bc])
    t1 = time.time()
    print("Vector assembly time:", t1 - t0)

    t0 = time.time()
    a = fem.Form(a)
    t1 = time.time()
    print("Bilinear form compile time:", t1 - t0)

    t0 = time.time()
    A = assemble_matrix(a, [bc])
    A.assemble()
    t1 = time.time()
    print("Matrix assembly time:", t1 - t0)

    # Create LU linear solver
    solver = PETSc.KSP().create(MPI.comm_world)
    solver.setType(PETSc.KSP.Type.PREONLY)
    solver.getPC().setType(PETSc.PC.Type.LU)
    solver.setOperators(A)
    # Solve
    t0 = time.time()
    uh = Function(V)
    solver.solve(b, uh.vector)
    uh.vector.ghostUpdate(addv=PETSc.InsertMode.INSERT,
                          mode=PETSc.ScatterMode.FORWARD)

    t1 = time.time()
    print("Linear solver time:", t1 - t0)

    M = (u_exact - uh)**2 * dx
    t0 = time.time()
    M = fem.Form(M)
    t1 = time.time()
    print("Error functional compile time:", t1 - t0)

    t0 = time.time()
    error = assemble_scalar(M)
    error = MPI.sum(mesh.mpi_comm(), error)
    t1 = time.time()

    print("Error assembly time:", t1 - t0)
    assert np.absolute(error) < 1.0e-14
Esempio n. 24
0
def test_nth_order_triangle(order):
    num_nodes = (order + 1) * (order + 2) / 2
    cells = np.array([range(int(num_nodes))])
    cells = permute_cell_ordering(
        cells, permutation_vtk_to_dolfin(CellType.triangle, cells.shape[1]))

    if order == 1:
        points = np.array([[0.00000, 0.00000, 0.00000],
                           [1.00000, 0.00000, 0.00000],
                           [0.00000, 1.00000, 0.00000]])
    elif order == 2:
        points = np.array([[0.00000, 0.00000, 0.00000],
                           [1.00000, 0.00000, 0.00000],
                           [0.00000, 1.00000, 0.00000],
                           [0.50000, 0.00000, 0.00000],
                           [0.50000, 0.50000, -0.25000],
                           [0.00000, 0.50000, -0.25000]])

    elif order == 3:
        points = np.array([[0.00000, 0.00000, 0.00000],
                           [1.00000, 0.00000, 0.00000],
                           [0.00000, 1.00000, 0.00000],
                           [0.33333, 0.00000, 0.00000],
                           [0.66667, 0.00000, 0.00000],
                           [0.66667, 0.33333, -0.11111],
                           [0.33333, 0.66667, 0.11111],
                           [0.00000, 0.66667, 0.11111],
                           [0.00000, 0.33333, -0.11111],
                           [0.33333, 0.33333, -0.11111]])
    elif order == 4:
        points = np.array([[0.00000, 0.00000, 0.00000],
                           [1.00000, 0.00000, 0.00000],
                           [0.00000, 1.00000, 0.00000],
                           [0.25000, 0.00000, 0.00000],
                           [0.50000, 0.00000, 0.00000],
                           [0.75000, 0.00000, 0.00000],
                           [0.75000, 0.25000, -0.06250],
                           [0.50000, 0.50000, 0.06250],
                           [0.25000, 0.75000, -0.06250],
                           [0.00000, 0.75000, -0.06250],
                           [0.00000, 0.50000, 0.06250],
                           [0.00000, 0.25000, -0.06250],
                           [0.25000, 0.25000, -0.06250],
                           [0.50000, 0.25000, -0.06250],
                           [0.25000, 0.50000, 0.06250]])

    elif order == 5:
        points = np.array([[0.00000, 0.00000, 0.00000],
                           [1.00000, 0.00000, 0.00000],
                           [0.00000, 1.00000, 0.00000],
                           [0.20000, 0.00000, 0.00000],
                           [0.40000, 0.00000, 0.00000],
                           [0.60000, 0.00000, 0.00000],
                           [0.80000, 0.00000, 0.00000],
                           [0.80000, 0.20000, -0.04000],
                           [0.60000, 0.40000, 0.04000],
                           [0.40000, 0.60000, -0.04000],
                           [0.20000, 0.80000, 0.04000],
                           [0.00000, 0.80000, 0.04000],
                           [0.00000, 0.60000, -0.04000],
                           [0.00000, 0.40000, 0.04000],
                           [0.00000, 0.20000, -0.04000],
                           [0.20000, 0.20000, -0.04000],
                           [0.60000, 0.20000, -0.04000],
                           [0.20000, 0.60000, -0.04000],
                           [0.40000, 0.20000, -0.04000],
                           [0.40000, 0.40000, 0.04000],
                           [0.20000, 0.40000, 0.04000]])

    elif order == 6:
        points = np.array([[0.00000, 0.00000, 0.00000],
                           [1.00000, 0.00000, 0.00000],
                           [0.00000, 1.00000, 0.00000],
                           [0.16667, 0.00000, 0.00000],
                           [0.33333, 0.00000, 0.00000],
                           [0.50000, 0.00000, 0.00000],
                           [0.66667, 0.00000, 0.00000],
                           [0.83333, 0.00000, 0.00000],
                           [0.83333, 0.16667, -0.00463],
                           [0.66667, 0.33333, 0.00463],
                           [0.50000, 0.50000, -0.00463],
                           [0.33333, 0.66667, 0.00463],
                           [0.16667, 0.83333, -0.00463],
                           [0.00000, 0.83333, -0.00463],
                           [0.00000, 0.66667, 0.00463],
                           [0.00000, 0.50000, -0.00463],
                           [0.00000, 0.33333, 0.00463],
                           [0.00000, 0.16667, -0.00463],
                           [0.16667, 0.16667, -0.00463],
                           [0.66667, 0.16667, -0.00463],
                           [0.16667, 0.66667, 0.00463],
                           [0.33333, 0.16667, -0.00463],
                           [0.50000, 0.16667, -0.00463],
                           [0.50000, 0.33333, 0.00463],
                           [0.33333, 0.50000, -0.00463],
                           [0.16667, 0.50000, -0.00463],
                           [0.16667, 0.33333, 0.00463],
                           [0.33333, 0.33333, 0.00463]])
    elif order == 7:
        points = np.array([[0.00000, 0.00000, 0.00000],
                           [1.00000, 0.00000, 0.00000],
                           [0.00000, 1.00000, 0.00000],
                           [0.14286, 0.00000, 0.00000],
                           [0.28571, 0.00000, 0.00000],
                           [0.42857, 0.00000, 0.00000],
                           [0.57143, 0.00000, 0.00000],
                           [0.71429, 0.00000, 0.00000],
                           [0.85714, 0.00000, 0.00000],
                           [0.85714, 0.14286, -0.02041],
                           [0.71429, 0.28571, 0.02041],
                           [0.57143, 0.42857, -0.02041],
                           [0.42857, 0.57143, 0.02041],
                           [0.28571, 0.71429, -0.02041],
                           [0.14286, 0.85714, 0.02041],
                           [0.00000, 0.85714, 0.02041],
                           [0.00000, 0.71429, -0.02041],
                           [0.00000, 0.57143, 0.02041],
                           [0.00000, 0.42857, -0.02041],
                           [0.00000, 0.28571, 0.02041],
                           [0.00000, 0.14286, -0.02041],
                           [0.14286, 0.14286, -0.02041],
                           [0.71429, 0.14286, -0.02041],
                           [0.14286, 0.71429, -0.02041],
                           [0.28571, 0.14286, -0.02041],
                           [0.42857, 0.14286, -0.02041],
                           [0.57143, 0.14286, -0.02041],
                           [0.57143, 0.28571, 0.02041],
                           [0.42857, 0.42857, -0.02041],
                           [0.28571, 0.57143, 0.02041],
                           [0.14286, 0.57143, 0.02041],
                           [0.14286, 0.42857, -0.02041],
                           [0.14286, 0.28571, 0.02041],
                           [0.28571, 0.28571, 0.02041],
                           [0.42857, 0.28571, 0.02041],
                           [0.28571, 0.42857, -0.02041]])
    # Higher order tests are too slow
    elif order == 8:
        points = np.array([[0.00000, 0.00000, 0.00000],
                           [1.00000, 0.00000, 0.00000],
                           [0.00000, 1.00000, 0.00000],
                           [0.12500, 0.00000, 0.00000],
                           [0.25000, 0.00000, 0.00000],
                           [0.37500, 0.00000, 0.00000],
                           [0.50000, 0.00000, 0.00000],
                           [0.62500, 0.00000, 0.00000],
                           [0.75000, 0.00000, 0.00000],
                           [0.87500, 0.00000, 0.00000],
                           [0.87500, 0.12500, -0.00195],
                           [0.75000, 0.25000, 0.00195],
                           [0.62500, 0.37500, -0.00195],
                           [0.50000, 0.50000, 0.00195],
                           [0.37500, 0.62500, -0.00195],
                           [0.25000, 0.75000, 0.00195],
                           [0.12500, 0.87500, -0.00195],
                           [0.00000, 0.87500, -0.00195],
                           [0.00000, 0.75000, 0.00195],
                           [0.00000, 0.62500, -0.00195],
                           [0.00000, 0.50000, 0.00195],
                           [0.00000, 0.37500, -0.00195],
                           [0.00000, 0.25000, 0.00195],
                           [0.00000, 0.12500, -0.00195],
                           [0.12500, 0.12500, -0.00195],
                           [0.75000, 0.12500, -0.00195],
                           [0.12500, 0.75000, 0.00195],
                           [0.25000, 0.12500, -0.00195],
                           [0.37500, 0.12500, -0.00195],
                           [0.50000, 0.12500, -0.00195],
                           [0.62500, 0.12500, -0.00195],
                           [0.62500, 0.25000, 0.00195],
                           [0.50000, 0.37500, -0.00195],
                           [0.37500, 0.50000, 0.00195],
                           [0.25000, 0.62500, -0.00195],
                           [0.12500, 0.62500, -0.00195],
                           [0.12500, 0.50000, 0.00195],
                           [0.12500, 0.37500, -0.00195],
                           [0.12500, 0.25000, 0.00195],
                           [0.25000, 0.25000, 0.00195],
                           [0.50000, 0.25000, 0.00195],
                           [0.25000, 0.50000, 0.00195],
                           [0.37500, 0.25000, 0.00195],
                           [0.37500, 0.37500, -0.00195],
                           [0.25000, 0.37500, -0.00195]])

    mesh = Mesh(MPI.comm_world, CellType.triangle, points, cells, [],
                GhostMode.none)

    # Find nodes corresponding to y axis
    nodes = []
    for j in range(points.shape[0]):
        if np.isclose(points[j][0], 0):
            nodes.append(j)

    def e2(x):
        return x[2] + x[0] * x[1]

    # For solution to be in functionspace
    V = FunctionSpace(mesh, ("CG", max(2, order)))
    u = Function(V)
    cmap = fem.create_coordinate_map(mesh.ufl_domain())
    mesh.geometry.coord_mapping = cmap
    u.interpolate(e2)

    quad_order = 30
    intu = assemble_scalar(u * dx(metadata={"quadrature_degree": quad_order}))
    intu = MPI.sum(mesh.mpi_comm(), intu)

    ref = scipy_one_cell(points, nodes)
    assert ref == pytest.approx(intu, rel=3e-3)