Ejemplo n.º 1
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)[:]
        ff.values[:] = mesh.topology.global_indices(dim - 1)[:]
        cf.values[:] = mesh.topology.global_indices(dim)[:]
        filename = os.path.join(
            tempdir,
            "appended_mf_{0:d}_{1:s}.xdmf".format(dim, str(mesh.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)
Ejemplo n.º 2
0
def test_save_3d_vector(tempfile, file_options):
    mesh = UnitCubeMesh(MPI.COMM_WORLD, 8, 8, 8)
    u = Function(VectorFunctionSpace(mesh, "Lagrange", 2))
    u.vector.set(1)
    VTKFile(tempfile + "u.pvd").write(u)
    f = VTKFile(tempfile + "u.pvd")
    f.write(u, 0.)
    f.write(u, 1.)
    for file_option in file_options:
        VTKFile(tempfile + "u.pvd", file_option).write(u)
Ejemplo n.º 3
0
def test_save_3d_scalar(tempdir, cell_type):
    mesh = UnitCubeMesh(MPI.COMM_WORLD, 8, 8, 8, cell_type=cell_type)
    u = Function(FunctionSpace(mesh, ("Lagrange", 2)))
    with u.vector.localForm() as loc:
        loc.set(1.0)

    filename = os.path.join(tempdir, "u.pvd")
    with VTKFile(MPI.COMM_WORLD, filename, "w") as vtk:
        vtk.write_function(u, 0.)
        vtk.write_function(u, 1.)
Ejemplo n.º 4
0
def test_save_3d_tensor(tempfile, file_options):
    mesh = UnitCubeMesh(MPI.comm_world, 8, 8, 8)
    u = Function(TensorFunctionSpace(mesh, ("Lagrange", 2)))
    u.vector.set(1)
    VTKFile(tempfile + "u.pvd").write(u)
    f = VTKFile(tempfile + "u.pvd")
    f.write(u, 0.)
    f.write(u, 1.)
    for file_option in file_options:
        VTKFile(tempfile + "u.pvd", file_option).write(u)
Ejemplo n.º 5
0
def test_RefineUnitCubeMesh_repartition():
    """Refine mesh of unit cube."""
    mesh = UnitCubeMesh(MPI.COMM_WORLD, 5, 7, 9, ghost_mode=GhostMode.none)
    mesh.topology.create_entities(1)
    mesh = refine(mesh, redistribute=True)
    assert mesh.topology.index_map(0).size_global == 3135
    assert mesh.topology.index_map(3).size_global == 15120

    mesh = UnitCubeMesh(MPI.COMM_WORLD,
                        5,
                        7,
                        9,
                        ghost_mode=GhostMode.shared_facet)
    mesh.topology.create_entities(1)
    mesh = refine(mesh, redistribute=True)
    assert mesh.topology.index_map(0).size_global == 3135
    assert mesh.topology.index_map(3).size_global == 15120

    Q = FunctionSpace(mesh, ("CG", 1))
    assert (Q)
Ejemplo n.º 6
0
def test_distance_tetrahedron():
    mesh = UnitCubeMesh(MPI.COMM_SELF, 1, 1, 1)
    assert cpp.geometry.squared_distance(mesh, mesh.topology.dim, 5,
                                         numpy.array([-1.0, -1.0, -1.0
                                                      ])) == pytest.approx(3.0)
    assert cpp.geometry.squared_distance(mesh, mesh.topology.dim, 5,
                                         numpy.array([-1.0, 0.5, 0.5
                                                      ])) == pytest.approx(1.0)
    assert cpp.geometry.squared_distance(mesh, mesh.topology.dim, 5,
                                         numpy.array([0.5, 0.5, 0.5
                                                      ])) == pytest.approx(0.0)
Ejemplo n.º 7
0
def mesh3d():
    """Create 3D mesh with regular tetrahedron and degenerate cells"""
    mesh3d = UnitCubeMesh(MPI.COMM_WORLD, 1, 1, 1)
    i1 = np.where((mesh3d.geometry.x
                   == (0, 1, 0)).all(axis=1))[0][0]
    i2 = np.where((mesh3d.geometry.x
                   == (1, 1, 1)).all(axis=1))[0][0]

    mesh3d.geometry.x[i1][0] = 1.0
    mesh3d.geometry.x[i2][1] = 0.0
    return mesh3d
Ejemplo n.º 8
0
def test_surface_bbtree_collision():
    """Compute collision between two meshes, where only one cell of each mesh are colliding"""
    tdim = 3
    mesh1 = UnitCubeMesh(MPI.COMM_WORLD, 3, 3, 3, cpp.mesh.CellType.hexahedron)
    mesh2 = UnitCubeMesh(MPI.COMM_WORLD, 3, 3, 3, cpp.mesh.CellType.hexahedron)
    mesh2.geometry.x[:, :] += numpy.array([0.9, 0.9, 0.9])

    sf = cpp.mesh.exterior_facet_indices(mesh1)
    f_to_c = mesh1.topology.connectivity(tdim - 1, tdim)

    # Compute unique set of cells (some will be counted multiple times)
    cells = list(set([f_to_c.links(f)[0] for f in sf]))
    bbtree1 = BoundingBoxTree(mesh1, tdim, cells)

    sf = cpp.mesh.exterior_facet_indices(mesh2)
    f_to_c = mesh2.topology.connectivity(tdim - 1, tdim)
    cells = list(set([f_to_c.links(f)[0] for f in sf]))
    bbtree2 = BoundingBoxTree(mesh2, tdim, cells)

    collisions = compute_collisions(bbtree1, bbtree2)
    assert len(collisions) == 1
Ejemplo n.º 9
0
def test_surface_bbtree():
    """Test creation of BBTree on subset of entities(surface cells)"""
    mesh = UnitCubeMesh(MPI.COMM_WORLD, 8, 8, 8)
    sf = cpp.mesh.exterior_facet_indices(mesh)
    tdim = mesh.topology.dim
    f_to_c = mesh.topology.connectivity(tdim - 1, tdim)
    cells = [f_to_c.links(f)[0] for f in sf]
    bbtree = BoundingBoxTree(mesh, tdim, cells)

    # test collision (should not collide with any)
    p = numpy.array([0.5, 0.5, 0.5])
    assert len(compute_collisions_point(bbtree, p)) == 0
Ejemplo n.º 10
0
def test_compute_collisions_tree_3d(point):
    mesh_A = UnitCubeMesh(MPI.COMM_WORLD, 2, 2, 2)
    mesh_B = UnitCubeMesh(MPI.COMM_WORLD, 2, 2, 2)

    bgeom = mesh_B.geometry.x
    bgeom += point

    tree_A = BoundingBoxTree(mesh_A, mesh_A.topology.dim)
    tree_B = BoundingBoxTree(mesh_B, mesh_B.topology.dim)
    entities = compute_collisions(tree_A, tree_B)

    entities_A = numpy.sort(numpy.unique([q[0] for q in entities]))
    entities_B = numpy.sort(numpy.unique([q[1] for q in entities]))

    cells_A = find_colliding_cells(mesh_A,
                                   tree_B.get_bbox(tree_B.num_bboxes - 1))
    cells_B = find_colliding_cells(mesh_B,
                                   tree_A.get_bbox(tree_A.num_bboxes - 1))

    assert numpy.allclose(entities_A, cells_A)
    assert numpy.allclose(entities_B, cells_B)
Ejemplo n.º 11
0
def test_distance_tetrahedron():
    mesh = UnitCubeMesh(MPI.comm_self, 1, 1, 1)
    cell = MeshEntity(mesh, mesh.topology.dim, 5)
    assert cpp.geometry.squared_distance(cell,
                                         numpy.array([-1.0, -1.0, -1.0
                                                      ])) == pytest.approx(3.0)
    assert cpp.geometry.squared_distance(cell,
                                         numpy.array([-1.0, 0.5, 0.5
                                                      ])) == pytest.approx(1.0)
    assert cpp.geometry.squared_distance(cell,
                                         numpy.array([0.5, 0.5, 0.5
                                                      ])) == pytest.approx(0.0)
Ejemplo n.º 12
0
def test_small_mesh():
    mesh3d = UnitCubeMesh(MPI.COMM_WORLD, 1, 1, 1)
    gdim = mesh3d.geometry.dim
    assert mesh3d.topology.index_map(gdim).size_global == 6

    mesh2d = UnitSquareMesh(MPI.COMM_WORLD, 1, 1)
    gdim = mesh2d.geometry.dim
    assert mesh2d.topology.index_map(gdim).size_global == 2

    mesh1d = UnitIntervalMesh(MPI.COMM_WORLD, 2)
    gdim = mesh1d.geometry.dim
    assert mesh1d.topology.index_map(gdim).size_global == 2
Ejemplo n.º 13
0
def test_compute_entity_collisions_tree_3d():
    references = [[
        set([18, 19, 20, 21, 22, 23, 42, 43, 44, 45, 46, 47]),
        set([0, 1, 2, 3, 4, 5, 24, 25, 26, 27, 28, 29])
    ], [set([7, 8, 30, 31, 32]),
        set([15, 16, 17, 39, 41])]]

    points = [numpy.array([0.52, 0.51, 0.3]), numpy.array([0.9, -0.9, 0.3])]
    for i, point in enumerate(points):

        mesh_A = UnitCubeMesh(MPI.comm_world, 2, 2, 2)
        mesh_B = UnitCubeMesh(MPI.comm_world, 2, 2, 2)

        bgeom = mesh_B.geometry.points
        bgeom += point

        tree_A = BoundingBoxTree(mesh_A, mesh_A.topology.dim)
        tree_B = BoundingBoxTree(mesh_B, mesh_B.topology.dim)
        entities_A, entities_B = geometry.compute_entity_collisions_bb(
            tree_A, mesh_A, tree_B, mesh_B)
        assert set(entities_A) == references[i][0]
        assert set(entities_B) == references[i][1]
Ejemplo n.º 14
0
def test_save_3d_meshfunctions(tempfile, mesh_function_types, file_options,
                               type_conv, cell_types_3D):
    mesh = UnitCubeMesh(MPI.comm_world, 8, 8, 8)
    for d in range(mesh.topology.dim + 1):
        for t in mesh_function_types:
            for cell_type in cell_types_3D:
                mf = MeshFunction(t, mesh, mesh.topology.dim - d, type_conv[t](1))
                VTKFile(tempfile + "mf_{0:d}_{1:s}.pvd".format(mesh.topology.dim - d,
                                                               str(cell_type).split(".")[-1])).write(mf)
                f = VTKFile(tempfile + "mf{0:d}_{1:s}.pvd".format(mesh.topology.dim - d,
                                                                  str(cell_type).split(".")[-1]))
                f.write(mf, 0.)
                f.write(mf, 1.)
Ejemplo n.º 15
0
def test_block_size(mesh):
    meshes = [
        UnitSquareMesh(8, 8),
        UnitCubeMesh(4, 4, 4),
        UnitSquareMesh(8, 8, CellType.quadrilateral),
        UnitCubeMesh(4, 4, 4, CellType.hexahedron)
    ]
    for mesh in meshes:
        P2 = FiniteElement("Lagrange", mesh.ufl_cell(), 2)

        V = FunctionSpace(mesh, P2)
        assert V.dofmap.block_size == 1

        V = FunctionSpace(mesh, P2 * P2)
        assert V.dofmap.index_map.block_size == 2

        for i in range(1, 6):
            W = FunctionSpace(mesh, MixedElement(i * [P2]))
            assert W.dofmap.index_map.block_size == i

        V = VectorFunctionSpace(mesh, ("Lagrange", 2))
        assert V.dofmap.index_map.block_size == mesh.geometry.dim
def test_scalar_p1_scaled_mesh():
    # Make coarse mesh smaller than fine mesh
    meshc = UnitCubeMesh(MPI.comm_world, 2, 2, 2)
    meshc.geometry.points *= 0.9

    meshf = UnitCubeMesh(MPI.comm_world, 3, 4, 5)

    Vc = FunctionSpace(meshc, ("CG", 1))
    Vf = FunctionSpace(meshf, ("CG", 1))

    def u(x):
        return x[0] + 2.0 * x[1] + 3.0 * x[2]
    uc, uf = Function(Vc), Function(Vf)
    uc.interpolate(u)
    uf.interpolate(u)

    mat = PETScDMCollection.create_transfer_matrix(Vc._cpp_object,
                                                   Vf._cpp_object)
    Vuc = Function(Vf)
    mat.mult(uc.vector, Vuc.vector)

    diff = Vuc.vector
    diff.axpy(-1, uf.vector)

    assert diff.norm() < 1.0e-12

    # Now make coarse mesh larger than fine mesh
    meshc.geometry.points *= 1.5
    uc.interpolate(u)

    mat = PETScDMCollection.create_transfer_matrix(Vc._cpp_object,
                                                   Vf._cpp_object)
    mat.mult(uc.vector, Vuc.vector)

    diff = Vuc.vector
    diff.axpy(-1, uf.vector)

    assert diff.norm() < 1.0e-12
def test_scalar_p2():
    meshc = UnitCubeMesh(MPI.comm_world, 2, 2, 2)
    meshf = UnitCubeMesh(MPI.comm_world, 3, 4, 5)

    Vc = FunctionSpace(meshc, ("CG", 2))
    Vf = FunctionSpace(meshf, ("CG", 2))

    def u(x):
        return x[0] + 2.0 * x[1] + 3.0 * x[2]

    uc, uf = Function(Vc), Function(Vf)
    uc.interpolate(u)
    uf.interpolate(u)

    mat = PETScDMCollection.create_transfer_matrix(Vc._cpp_object,
                                                   Vf._cpp_object)
    Vuc = Function(Vf)
    mat.mult(uc.vector, Vuc.vector)

    diff = Vuc.vector
    diff.axpy(-1, uf.vector)

    assert diff.norm() < 1.0e-12
Ejemplo n.º 18
0
def test_argument_equality(mesh, V, V2, W, W2):
    """Placed this test here because it's mainly about detecting differing
    function spaces.

    """
    mesh2 = UnitCubeMesh(MPI.COMM_WORLD, 8, 8, 8)
    V3 = FunctionSpace(mesh2, ('CG', 1))
    W3 = VectorFunctionSpace(mesh2, ('CG', 1))

    for TF in (TestFunction, TrialFunction):
        v = TF(V)
        v2 = TF(V2)
        v3 = TF(V3)
        assert v == v2
        assert v2 == v
        assert V != V3
        assert V2 != V3
        assert not v == v3
        assert not v2 == v3
        assert v != v3
        assert v2 != v3
        assert v != v3
        assert v2 != v3

        w = TF(W)
        w2 = TF(W2)
        w3 = TF(W3)
        assert w == w2
        assert w2 == w
        assert w != w3
        assert w2 != w3

        assert v != w
        assert w != v

        s1 = set((v, w))
        s2 = set((v2, w2))
        s3 = set((v, v2, w, w2))
        assert len(s1) == 2
        assert len(s2) == 2
        assert len(s3) == 2
        assert s1 == s2
        assert s1 == s3
        assert s2 == s3

        # Test that the dolfinx implementation of Argument.__eq__ is
        # triggered when comparing ufl expressions
        assert grad(v) == grad(v2)
        assert grad(v) != grad(v3)
Ejemplo n.º 19
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
Ejemplo n.º 20
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)
Ejemplo n.º 21
0
def test_compute_collisions_point_3d():
    reference = {
        1: set([1364]),
        2: set([1967, 1968, 1970, 1972, 1974, 1976]),
        3: set([876, 877, 878, 879, 880, 881])
    }
    p = numpy.array([0.3, 0.3, 0.3])
    mesh = UnitCubeMesh(MPI.comm_world, 8, 8, 8)
    tree = BoundingBoxTree(mesh, mesh.topology.dim)
    for dim in range(1, 4):
        entities, _ = geometry.compute_collisions_point(tree, p)

        # FIXME: Face and edges tests are excluded because test
        # mistakenly relies on the face and edge indices
        tdim = mesh.topology.dim
        if dim != tdim - 1 and dim != tdim - 2:
            assert set(entities) == reference[dim]
Ejemplo n.º 22
0
def test_compute_first_collision_3d():
    # FIXME: This test should not use facet indices as there are no
    # guarantees on how DOLFINX numbers facets
    reference = {
        1: [1364],
        2: [1967, 1968, 1970, 1972, 1974, 1976],
        3: [876, 877, 878, 879, 880, 881]
    }

    p = numpy.array([0.3, 0.3, 0.3])
    mesh = UnitCubeMesh(MPI.comm_world, 8, 8, 8)
    for dim in range(1, 4):
        tree = BoundingBoxTree(mesh, dim)
        first = cpp.geometry.compute_first_collision(tree._cpp_object, p)

        # FIXME: Face and test is excluded because it mistakenly relies
        # in the facet indices
        tdim = mesh.topology.dim
        if dim != tdim - 1 and dim != tdim - 2:
            assert first in reference[dim]
Ejemplo n.º 23
0
def get_mesh(cell_type, datadir):
    if MPI.COMM_WORLD.size == 1:
        # If running in serial, use a small mesh
        if cell_type in [CellType.triangle, CellType.quadrilateral]:
            return UnitSquareMesh(MPI.COMM_WORLD, 2, 1, cell_type)
        else:
            return UnitCubeMesh(MPI.COMM_WORLD, 2, 1, 1, cell_type)
    else:
        # In parallel, use larger meshes
        if cell_type == CellType.triangle:
            filename = "UnitSquareMesh_triangle.xdmf"
        elif cell_type == CellType.quadrilateral:
            filename = "UnitSquareMesh_quad.xdmf"
        elif cell_type == CellType.tetrahedron:
            filename = "UnitCubeMesh_tetra.xdmf"
        elif cell_type == CellType.hexahedron:
            filename = "UnitCubeMesh_hexahedron.xdmf"
        with XDMFFile(MPI.COMM_WORLD,
                      os.path.join(datadir, filename),
                      "r",
                      encoding=XDMFFile.Encoding.ASCII) as xdmf:
            return xdmf.read_mesh(name="Grid")
Ejemplo n.º 24
0
def test_sub_bbtree_box(ct, N):
    """
    Test that the bounding box of the stem of the bounding box tree is what we expect
    """
    mesh = UnitCubeMesh(MPI.COMM_WORLD, N, N, N, cell_type=ct)
    tdim = mesh.topology.dim
    fdim = tdim - 1

    def marker(x):
        return numpy.isclose(x[1], 1.0)

    facets = locate_entities_boundary(mesh, fdim, marker)
    f_to_c = mesh.topology.connectivity(fdim, tdim)
    cells = numpy.unique([f_to_c.links(f)[0] for f in facets])
    bbtree = BoundingBoxTree(mesh, tdim, cells)
    num_boxes = bbtree.num_bboxes
    if num_boxes > 0:
        bbox = bbtree.get_bbox(num_boxes - 1)
        assert numpy.isclose(bbox[0][1], (N - 1) / N)

    tree = BoundingBoxTree(mesh, tdim)
    assert num_boxes < tree.num_bboxes
Ejemplo n.º 25
0
def test_compute_closest_entity_3d(dim):
    ref_distance = 0.135
    p = numpy.array([0.9, 0, 1 + ref_distance])
    mesh = UnitCubeMesh(MPI.COMM_WORLD, 8, 8, 8)
    mesh.topology.create_entities(dim)

    tree = BoundingBoxTree(mesh, dim)
    entity, distance = compute_closest_entity(tree, p, mesh)
    min_distance = MPI.COMM_WORLD.allreduce(distance, op=MPI.MIN)
    assert min_distance == pytest.approx(ref_distance, 1.0e-12)

    # Find which entity is colliding with known closest point on mesh
    p_c = numpy.array([0.9, 0, 1])
    entities = compute_collisions_point(tree, p_c)

    # Refine search by checking for actual collision if the entities are
    # cells
    # NOTE: Could be done for all entities if we generalize
    # select_colliding_cells to select_colliding_entities
    if dim == mesh.topology.dim:
        entities = select_colliding_cells(mesh, entities, p_c, len(entities))

    if len(entities) > 0:
        assert numpy.isin(entity, entities)
Ejemplo n.º 26
0
        # Build rotational null space basis
        x = V.tabulate_dof_coordinates()
        dofs_block = V.dofmap.list.array
        x0, x1 = x[dofs_block, 0], x[dofs_block, 1]
        basis[2][dofs[0]] = -x1
        basis[2][dofs[1]] = x0

        # Add vector that is not in nullspace
        basis[3][dofs[1]] = x1

    return la.VectorSpaceBasis(nullspace_basis)


@pytest.mark.parametrize("mesh", [
    UnitSquareMesh(MPI.COMM_WORLD, 12, 13),
    UnitCubeMesh(MPI.COMM_WORLD, 12, 18, 15)
])
@pytest.mark.parametrize("degree", [1, 2])
def test_nullspace_orthogonal(mesh, degree):
    """Test that null spaces orthogonalisation"""
    V = VectorFunctionSpace(mesh, ('Lagrange', degree))
    null_space = build_elastic_nullspace(V)
    assert not null_space.is_orthogonal()
    assert not null_space.is_orthonormal()

    null_space.orthonormalize()
    assert null_space.is_orthogonal()
    assert null_space.is_orthonormal()


@pytest.mark.parametrize("mesh", [
Ejemplo n.º 27
0
def test_distance_tetrahedron():
    mesh = UnitCubeMesh(MPI.COMM_SELF, 1, 1, 1)
    assert cpp.geometry.squared_distance(mesh, mesh.topology.dim, 5,
                                         numpy.array([-1.0, -1.0, -1.0])) == pytest.approx(3.0)
    assert cpp.geometry.squared_distance(mesh, mesh.topology.dim, 5,
                                         numpy.array([-1.0, 0.5, 0.5])) == pytest.approx(1.0)
    assert cpp.geometry.squared_distance(mesh, mesh.topology.dim, 5, numpy.array([0.5, 0.5, 0.5])) == pytest.approx(0.0)


@pytest.mark.skip("volume_entities needs fixing")
@pytest.mark.parametrize(
    'mesh', [
        UnitIntervalMesh(MPI.COMM_WORLD, 8),
        UnitSquareMesh(MPI.COMM_WORLD, 8, 9, CellType.triangle),
        UnitSquareMesh(MPI.COMM_WORLD, 8, 9, CellType.quadrilateral),
        UnitCubeMesh(MPI.COMM_WORLD, 8, 9, 5, CellType.tetrahedron)
    ])
def test_volume_cells(mesh):
    tdim = mesh.topology.dim
    map = mesh.topology.index_map(tdim)
    num_cells = map.size_local
    v = cpp.mesh.volume_entities(mesh, range(num_cells), mesh.topology.dim)
    assert mesh.mpi_comm().allreduce(v.sum(), mpi4py.MPI.SUM) == pytest.approx(1.0, rel=1e-9)


@pytest.mark.skip("volume_entities needs fixing")
def test_volume_quadrilateralR2():
    mesh = UnitSquareMesh(MPI.COMM_SELF, 1, 1, CellType.quadrilateral)
    assert cpp.mesh.volume_entities(mesh, [0], mesh.topology.dim) == 1.0

Ejemplo n.º 28
0
def cube():
    return UnitCubeMesh(MPI.COMM_WORLD, 5, 5, 5)
Ejemplo n.º 29
0
def test_wrong_dim():
    mesh = UnitCubeMesh(MPI.COMM_WORLD, 2, 2, 2)
    c = Constant(mesh, [1.0, 2.0])
    assert c.value.shape == (2, )
    with pytest.raises(ValueError):
        c.value = [1.0, 2.0, 3.0]
Ejemplo n.º 30
0
def test_reshape():
    mesh = UnitCubeMesh(MPI.COMM_WORLD, 2, 2, 2)
    c = Constant(mesh, 1.0)
    with pytest.raises(ValueError):
        c.value.resize(100)