Beispiel #1
0
def test_prec_solve_2D(arpack_eigs, interface, x, num_evs, tol, atol, interactive=False):
    from fvm import JadaHYMLSInterface

    from jadapy import EpetraInterface
    from jadapy import jdqz

    numpy.random.seed(1234)

    jac_op = EpetraInterface.CrsMatrix(interface.jacobian(x))
    mass_op = EpetraInterface.CrsMatrix(interface.mass_matrix())
    jada_interface = JadaHYMLSInterface.JadaHYMLSInterface(interface.map, interface, preconditioned_solve=True)

    alpha, beta = jdqz.jdqz(jac_op, mass_op, num_evs, tol=tol, subspace_dimensions=[20, 40],
                            interface=jada_interface)
    jdqz_eigs = numpy.array(sorted(alpha / beta, key=lambda x: abs(x)))

    assert_allclose(jdqz_eigs.real, arpack_eigs.real, rtol=0, atol=atol)
    assert_allclose(abs(jdqz_eigs.imag), abs(arpack_eigs.imag), rtol=0, atol=atol)

    if not interactive:
        return x

    fig, ax = plt.subplots()
    ax.scatter(jdqz_eigs.real, jdqz_eigs.imag, marker='+')
    plt.show()
Beispiel #2
0
    def solve(self, op, rhs, tol, maxit):
        solver_parameters = self.parameters.sublist('Solver')

        iterative_solver_parameters = solver_parameters.sublist(
            'Iterative Solver')
        iterative_solver_parameters.set('Convergence Tolerance',
                                        tol if maxit > 1 else 1e-3)
        # iterative_solver_parameters.set('Maximum Iterations', maxit)

        if rhs.shape[1] == 2:
            solver_parameters.set('Complex', True)
        else:
            solver_parameters.set('Complex', False)
        solver_parameters.set('Use Bordering', True)

        out = EpetraInterface.Vector(rhs)

        epetra_op = EpetraInterface.Operator(ShiftedOperator(op))
        if self.preconditioned_solve:
            solver = HYMLS.Solver(epetra_op, self.interface.preconditioner,
                                  self.parameters)
            solver.SetBorder(op.Z, op.Q)
            self.interface.preconditioner.Compute()
            solver.ApplyInverse(rhs, out)
            solver.UnsetBorder()
        else:
            raise Exception('Not implemented')

        return out
Beispiel #3
0
    def solve(self, op, rhs, tol, maxit):
        solver_parameters = self.parameters.sublist('Solver')

        iterative_solver_parameters = solver_parameters.sublist(
            'Iterative Solver')
        iterative_solver_parameters.set('Convergence Tolerance',
                                        tol if maxit > 1 else 1e-3)
        # iterative_solver_parameters.set('Maximum Iterations', maxit)

        if rhs.shape[1] == 2:
            solver_parameters.set('Complex', True)
        else:
            solver_parameters.set('Complex', False)

        out = EpetraInterface.Vector(rhs)

        epetra_op = EpetraInterface.Operator(op)
        if self.preconditioned_solve:
            epetra_precop = JadaHYMLSPrecOp(op, self.interface.preconditioner)
            solver = HYMLS.Solver(epetra_op, epetra_precop, self.parameters)
        else:
            solver = HYMLS.Solver(epetra_op, epetra_op, self.parameters)
        solver.ApplyInverse(rhs, out)

        return out
Beispiel #4
0
    def solve(self, op, rhs, tol, maxit):
        solver_parameters = self.parameters.sublist('Solver')

        iterative_solver_parameters = solver_parameters.sublist(
            'Iterative Solver')
        iterative_solver_parameters.set('Convergence Tolerance',
                                        tol if maxit > 1 else 1e-3)
        # iterative_solver_parameters.set('Maximum Iterations', maxit)

        solver_parameters.set('Complex', True)

        x = EpetraInterface.Vector(rhs.real.Map(), 2)
        y = EpetraInterface.Vector(rhs.real.Map(), 2)
        x[:, 0] = rhs.real
        x[:, 1] = rhs.imag

        epetra_op = ComplexEpetraInterface.Operator(op)
        if self.preconditioned_solve:
            epetra_precop = ComplexJadaHYMLSPrecOp(
                op, self.interface.preconditioner)
            solver = HYMLS.Solver(epetra_op, epetra_precop, self.parameters)
        else:
            solver = HYMLS.Solver(epetra_op, epetra_op, self.parameters)
        solver.ApplyInverse(x, y)

        out = ComplexEpetraInterface.ComplexVector(y[:, 0], y[:, 1])
        return out
Beispiel #5
0
    def prec(self, x, *args):
        y = EpetraInterface.Vector(x.real.Map(), 2)
        z = EpetraInterface.Vector(x.real.Map(), 2)
        y[:, 0] = x.real
        y[:, 1] = x.imag

        self.interface.preconditioner.ApplyInverse(y, z)

        out = ComplexEpetraInterface.ComplexVector(z[:, 0], z[:, 1])
        return out
Beispiel #6
0
    def Apply(self, x, y):
        assert x.NumVectors() == 2
        # Create a view here because this is an Epetra.MultiVector.
        x = EpetraInterface.Vector(Epetra.View, x, 0, x.NumVectors())
        y = EpetraInterface.Vector(Epetra.View, y, 0, y.NumVectors())

        x = ComplexVector(x[:, 0], x[:, 1])
        z = self.op.matvec(x)
        ret = y[:, 0].Update(1.0, z.real, 0.0)
        ret += y[:, 1].Update(1.0, z.imag, 0.0)
        return ret
Beispiel #7
0
    def __matmul__(self, x):
        if isinstance(x, numpy.ndarray):
            local_map = Epetra.LocalMap(x.shape[0], 0, self.Comm())
            x = ComplexVector(EpetraInterface.Vector(local_map, x.T.real), EpetraInterface.Vector(local_map, x.T.imag))

        tmp = ComplexVector(self.real.Map(), x.shape[1])

        tmp.real.Multiply('N', 'N', 1.0, self.real, x.real, 0.0)
        tmp.real.Multiply('N', 'N', -1.0, self.imag, x.imag, 1.0)

        tmp.imag.Multiply('N', 'N', 1.0, self.real, x.imag, 0.0)
        tmp.imag.Multiply('N', 'N', 1.0, self.imag, x.real, 1.0)

        return tmp
Beispiel #8
0
def test_Epetra_lowdim():
    try:
        from PyTrilinos import Epetra
        from jadapy import EpetraInterface
    except ImportError:
        pytest.skip("Trilinos not found")

    dtype = numpy.float64
    numpy.random.seed(1234)
    tol = numpy.finfo(dtype).eps * 1e3
    atol = tol * 10
    n = 20
    k = 2

    comm = Epetra.PyComm()
    map = Epetra.Map(n, 0, comm)
    a1, a2 = generate_Epetra_test_matrix(map, [n, n], dtype)

    interface = EpetraInterface.EpetraInterface(map)

    alpha = jdqr.jdqr(a2, k, Target.LargestMagnitude, tol=tol, subspace_dimensions=[10, 18], interface=interface)
    jdqr_eigs = numpy.array(sorted(alpha, key=lambda x: -abs(x)))

    eigs = scipy.linalg.eigvals(a1)
    eigs = numpy.array(sorted(eigs, key=lambda x: -abs(x)))
    eigs = eigs[:k]

    assert_allclose(jdqr_eigs.real, eigs.real, rtol=0, atol=atol)
    assert_allclose(abs(jdqr_eigs.imag), abs(eigs.imag), rtol=0, atol=atol)
Beispiel #9
0
    def ApplyInverse(self, x, y):
        self.prec.ApplyInverse(x, y)

        # Create a view here because this is an Epetra.MultiVector.
        z = EpetraInterface.Vector(Epetra.View, y, 0, y.NumVectors())
        z = self.op.proj(z)
        return y.Update(1.0, z, 0.0)
Beispiel #10
0
def test_Epetra():
    try:
        from PyTrilinos import Epetra
        from jadapy import EpetraInterface
    except ImportError:
        pytest.skip("Trilinos not found")

    dtype = numpy.float64
    numpy.random.seed(1234)
    tol = numpy.finfo(dtype).eps * 1e3
    atol = tol * 10
    n = 20
    k = 5

    comm = Epetra.PyComm()
    map = Epetra.Map(n, 0, comm)
    a1, a2 = generate_Epetra_test_matrix(map, [n, n], dtype)
    b1, b2 = generate_Epetra_test_matrix(map, [n, n], dtype)

    interface = EpetraInterface.EpetraInterface(map)

    alpha, beta = jdqz.jdqz(a2, b2, k, tol=tol, interface=interface)
    jdqz_eigs = numpy.array(sorted(alpha / beta, key=lambda x: abs(x)))

    eigs = scipy.linalg.eigvals(a1, b1)
    eigs = numpy.array(sorted(eigs, key=lambda x: abs(x)))
    eigs = eigs[:k]

    assert_allclose(jdqz_eigs.real, eigs.real, rtol=0, atol=atol)
    assert_allclose(abs(jdqz_eigs.imag), abs(eigs.imag), rtol=0, atol=atol)
Beispiel #11
0
    def ApplyInverse(self, x, y):
        self.prec.ApplyInverse(x, y)

        assert x.NumVectors() == 2
        # Create a view here because this is an Epetra.MultiVector.
        y = EpetraInterface.Vector(Epetra.View, y, 0, y.NumVectors())
        y = ComplexEpetraInterface.ComplexVector(y[:, 0], y[:, 1])

        z = self.op.proj(y)
        y *= 0.0
        y += z
        return 0
Beispiel #12
0
    def __init__(self, *args, **kwargs):
        self.real = None
        self.imag = None

        if len(args) > 1 and isinstance(args[0],
                                        EpetraInterface.Vector) and isinstance(
                                            args[1], EpetraInterface.Vector):
            self.real = args[0]
            self.imag = args[1]
        elif len(args) > 0 and isinstance(args[0], ComplexVector):
            self.real = EpetraInterface.Vector(args[0].real, *args[1:],
                                               **kwargs)
            self.imag = EpetraInterface.Vector(args[0].imag, *args[1:],
                                               **kwargs)
        elif len(args) > 1 and isinstance(args[1], ComplexVector):
            self.real = EpetraInterface.Vector(args[0], args[1].real,
                                               *args[1:], **kwargs)
            self.imag = EpetraInterface.Vector(args[0], args[1].imag,
                                               *args[1:], **kwargs)
        else:
            self.real = EpetraInterface.Vector(*args, **kwargs)
            self.imag = EpetraInterface.Vector(*args, **kwargs)

        self.shape = self.real.shape
        self.dtype = numpy.dtype(self.real.dtype.char.upper())
Beispiel #13
0
    def solve(self, op, rhs, tol, maxit):
        solver_parameters = self.parameters.sublist('Solver')

        iterative_solver_parameters = solver_parameters.sublist(
            'Iterative Solver')
        iterative_solver_parameters.set('Convergence Tolerance',
                                        tol if maxit > 1 else 1e-3)
        # iterative_solver_parameters.set('Maximum Iterations', maxit)

        solver_parameters.set('Complex', True)
        solver_parameters.set('Use Bordering', True)

        x = EpetraInterface.Vector(rhs.real.Map(), 2)
        y = EpetraInterface.Vector(rhs.real.Map(), 2)
        x[:, 0] = rhs.real
        x[:, 1] = rhs.imag

        m = op.Q.real.NumVectors()

        Q = EpetraInterface.Vector(rhs.real.Map(), m * 2)
        Q[:, 0:m] = op.Q.real
        Q[:, m:2 * m] = op.Q.imag

        Z = EpetraInterface.Vector(rhs.real.Map(), m * 2)
        Z[:, 0:m] = op.Z.real
        Z[:, m:2 * m] = op.Z.imag

        epetra_op = ComplexEpetraInterface.Operator(ShiftedOperator(op))
        if self.preconditioned_solve:
            solver = HYMLS.Solver(epetra_op, self.interface.preconditioner,
                                  self.parameters)
            solver.SetBorder(Z, Q)
            self.interface.preconditioner.Compute()
            solver.ApplyInverse(x, y)
            solver.UnsetBorder()
        else:
            raise Exception('Not implemented')

        out = ComplexEpetraInterface.ComplexVector(y[:, 0], y[:, 1])
        return out
Beispiel #14
0
def test_Epetra_eigenvectors():
    try:
        from PyTrilinos import Epetra
        from jadapy import EpetraInterface
    except ImportError:
        pytest.skip("Trilinos not found")

    dtype = numpy.float64
    numpy.random.seed(1234)
    tol = numpy.finfo(dtype).eps * 1e3
    atol = tol * 10
    n = 20
    k = 5

    comm = Epetra.PyComm()
    map = Epetra.Map(n, 0, comm)
    a1, a2 = generate_Epetra_test_matrix(map, [n, n], dtype)
    b1, b2 = generate_Epetra_test_matrix(map, [n, n], dtype)

    interface = EpetraInterface.EpetraInterface(map)

    alpha, beta, v = jdqz.jdqz(a2,
                               b2,
                               num=k,
                               tol=tol,
                               return_eigenvectors=True,
                               interface=interface)
    jdqz_eigs = numpy.array(sorted(alpha / beta, key=lambda x: abs(x)))
    jdqz_eigs = jdqz_eigs[:k]

    eigs = scipy.linalg.eigvals(a1, b1)
    eigs = numpy.array(sorted(eigs, key=lambda x: abs(x)))
    eigs = eigs[:k]

    assert_allclose(jdqz_eigs.real, eigs.real, rtol=0, atol=atol)
    assert_allclose(abs(jdqz_eigs.imag), abs(eigs.imag), rtol=0, atol=atol)

    i = 0
    while i < k:
        if alpha[i].imag:
            assert norm(a2 @ v[:, i] * beta[i].real -
                        b2 @ v[:, i] * alpha[i].real +
                        b2 @ v[:, i + 1] * alpha[i].imag) < atol
            assert norm(a2 @ v[:, i + 1] * beta[i].real -
                        b2 @ v[:, i] * alpha[i].imag -
                        b2 @ v[:, i + 1] * alpha[i].real) < atol
            i += 2
        else:
            assert norm(a2 @ v[:, i] * beta[i].real -
                        b2 @ v[:, i] * alpha[i].real) < atol
            i += 1
Beispiel #15
0
def generate_Epetra_test_matrix(map, shape, dtype):
    try:
        from PyTrilinos import Epetra
        from jadapy import EpetraInterface
    except ImportError:
        pytest.skip("Trilinos not found")

    a1 = generate_test_matrix(shape, dtype)
    a2 = EpetraInterface.CrsMatrix(Epetra.Copy, map, shape[1])

    for i in range(shape[0]):
        for j in range(shape[1]):
            a2[i, j] = a1[i, j]
    a2.FillComplete()

    return a1, a2
Beispiel #16
0
def test_orthonormalization_multiple_vectors_epetra(otype):
    try:
        from PyTrilinos import Epetra
        from jadapy import EpetraInterface
    except ImportError:
        pytest.skip("Trilinos not found")

    dtype = numpy.float64
    atol = numpy.finfo(dtype).eps * 100
    n = 20
    k = 5

    comm = Epetra.PyComm()
    map = Epetra.Map(n, 0, comm)
    x = EpetraInterface.Vector(map, k)
    x.Random()

    orthogonalization.orthonormalize(x, method=otype)
    for i in range(k):
        for j in range(k):
            if i == j:
                continue
            assert_allclose(x[:, i].dot(x[:, j]), 0, rtol=0, atol=atol)
        assert_allclose(norm(x[:, i]), 1, rtol=0, atol=atol)
Beispiel #17
0
 def prec(self, x, *args):
     out = EpetraInterface.Vector(x)
     self.interface.preconditioner.ApplyInverse(x, out)
     return out