예제 #1
0
def test_manifold_point_search():
    # Simple two-triangle surface in 3d
    vertices = [(0.0, 0.0, 1.0), (1.0, 1.0, 1.0), (1.0, 0.0, 0.0),
                (0.0, 1.0, 0.0)]
    cells = [(0, 1, 2), (0, 1, 3)]
    mesh = Mesh(MPI.COMM_WORLD, CellType.triangle,
                numpy.array(vertices, dtype=numpy.float64),
                numpy.array(cells, dtype=numpy.int32), [])

    bb = BoundingBoxTree(mesh, mesh.topology.dim)
    p = numpy.array([0.5, 0.25, 0.75])
    assert geometry.compute_first_entity_collision(bb, mesh, p) == 0

    p = numpy.array([0.25, 0.5, 0.75])
    assert geometry.compute_first_entity_collision(bb, mesh, p) == 1
예제 #2
0
def test_compute_first_entity_collision_3d():
    reference = [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)
    first = geometry.compute_first_entity_collision(tree, mesh, p)
    assert first in reference
예제 #3
0
def test_compute_first_entity_collision_2d():
    reference = [136, 137]
    p = numpy.array([0.3, 0.3, 0.0])
    mesh = UnitSquareMesh(MPI.comm_world, 16, 16)
    tree = BoundingBoxTree(mesh, mesh.topology.dim)
    first = geometry.compute_first_entity_collision(tree, mesh, p)
    assert first in reference
예제 #4
0
def test_compute_first_entity_collision_1d():
    reference = [4]
    p = numpy.array([0.3, 0, 0])
    mesh = UnitIntervalMesh(MPI.comm_world, 16)
    tree = BoundingBoxTree(mesh, mesh.topology.dim)
    first = geometry.compute_first_entity_collision(tree, mesh, p)
    assert first in reference
예제 #5
0
def test_eval_multiple(W):
    u = Function(W)
    u.vector.set(1.0)
    mesh = W.mesh
    x0 = (mesh.geometry.x[0] + mesh.geometry.x[1]) / 2.0
    x = np.array([x0, x0 + 1.0e8])
    tree = geometry.BoundingBoxTree(mesh, W.mesh.geometry.dim)
    cells = geometry.compute_first_entity_collision(tree, mesh, x)
    u.eval(x[0], cells[0])
예제 #6
0
def test_manufactured_vector2(family, degree, filename, datadir):
    """Projection into H(div/curl) spaces"""

    # Skip slowest tests
    if "tetra" in filename and degree > 2:
        return

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

    # FIXME: these test are currently failing on unordered meshes
    if "tetra" in filename:
        if family == "N1curl":
            Ordering.order_simplex(mesh)

    V = FunctionSpace(mesh, (family, degree + 1))
    u, v = ufl.TrialFunction(V), ufl.TestFunction(V)
    a = inner(u, v) * dx

    xp = np.array([0.33, 0.33, 0.0])
    tree = geometry.BoundingBoxTree(mesh, mesh.geometry.dim)
    cells = geometry.compute_first_entity_collision(tree, mesh, xp)

    # Source term
    x = SpatialCoordinate(mesh)
    u_ref = x[0]**degree
    L = inner(u_ref, v[0]) * dx

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

    A = assemble_matrix(a)
    A.assemble()

    # Create LU linear solver (Note: need to use a solver that
    # re-orders to handle pivots, e.g. not the PETSc built-in LU
    # solver)
    solver = PETSc.KSP().create(MPI.comm_world)
    solver.setType("preonly")
    solver.getPC().setType('lu')
    solver.setOperators(A)

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

    up = uh.eval(xp, cells[0])
    print("test0:", up)
    print("test1:", xp[0]**degree)

    u_exact = np.zeros(mesh.geometry.dim)
    u_exact[0] = xp[0]**degree
    assert np.allclose(up, u_exact)
예제 #7
0
def test_manufactured_vector1(family, degree, filename, datadir):
    """Projection into H(div/curl) spaces"""

    with XDMFFile(MPI.COMM_WORLD,
                  os.path.join(datadir, filename),
                  "r",
                  encoding=XDMFFile.Encoding.ASCII) as xdmf:
        mesh = xdmf.read_mesh(name="Grid")

    V = FunctionSpace(mesh, (family, degree))
    u, v = ufl.TrialFunction(V), ufl.TestFunction(V)
    a = inner(u, v) * dx

    # Source term
    x = SpatialCoordinate(mesh)
    u_ref = x[0]**degree
    L = inner(u_ref, v[0]) * dx

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

    A = assemble_matrix(a)
    A.assemble()

    # Create LU linear solver (Note: need to use a solver that
    # re-orders to handle pivots, e.g. not the PETSc built-in LU
    # solver)
    solver = PETSc.KSP().create(MPI.COMM_WORLD)
    solver.setType("preonly")
    solver.getPC().setType('lu')
    solver.setOperators(A)

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

    xp = np.array([0.33, 0.33, 0.0])
    tree = geometry.BoundingBoxTree(mesh, mesh.geometry.dim)
    cells = geometry.compute_first_entity_collision(tree, mesh, xp)

    up = uh.eval(xp, cells[0])
    print("test0:", up)
    print("test1:", xp[0]**degree)

    u_exact = np.zeros(mesh.geometry.dim)
    u_exact[0] = xp[0]**degree
    assert np.allclose(up, u_exact)
예제 #8
0
def test_eval(R, V, W, Q, mesh):
    u0 = Function(R)
    u1 = Function(V)
    u2 = Function(W)
    u3 = Function(Q)

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

    def e2(x):
        values = np.empty((3, x.shape[1]))
        values[0] = x[0] + x[1] + x[2]
        values[1] = x[0] - x[1] - x[2]
        values[2] = x[0] + x[1] + x[2]
        return values

    def e3(x):
        values = np.empty((9, x.shape[1]))
        values[0] = x[0] + x[1] + x[2]
        values[1] = x[0] - x[1] - x[2]
        values[2] = x[0] + x[1] + x[2]
        values[3] = x[0]
        values[4] = x[1]
        values[5] = x[2]
        values[6] = -x[0]
        values[7] = -x[1]
        values[8] = -x[2]
        return values

    u0.vector.set(1.0)
    u1.interpolate(e1)
    u2.interpolate(e2)
    u3.interpolate(e3)

    x0 = (mesh.geometry.x(0) + mesh.geometry.x(1)) / 2.0
    tree = geometry.BoundingBoxTree(mesh, mesh.geometry.dim)
    cells = geometry.compute_first_entity_collision(tree, mesh, x0)
    assert np.allclose(u3.eval(x0, cells)[:3],
                       u2.eval(x0, cells),
                       rtol=1e-15,
                       atol=1e-15)
    with pytest.raises(ValueError):
        u0.eval([0, 0, 0, 0], 0)
    with pytest.raises(ValueError):
        u0.eval([0, 0], 0)