Exemple #1
0
 def test_chebyshev_iteration_1(self):
     '''  Tests the pcd_shell operators produce correct output. '''
     A = self.quad_mass_matrix
     n = self.quad_mass_matrix.shape[0]
     alpha = 1. / 4
     beta = 9. / 4
     x0 = np.zeros(n)
     b1 = np.ones(n)
     for i in range(0, n, 2):
         b1[i] = 0.
     A_petsc = LAT.dense_numpy_2_petsc4py(A)
     x0_petsc = p4pyPETSc.Vec().createWithArray(x0)
     b1_petsc = p4pyPETSc.Vec().createWithArray(b1)
     solver = LS.ChebyshevSemiIteration(A_petsc, alpha, beta, True)
     solver.apply(b1_petsc, x0_petsc, 20)
     expected = np.load(
         os.path.join(self._scriptdir, 'import_modules/sol_10.npy'))
     actual = x0_petsc
     assert np.allclose(expected, actual.getArray())
Exemple #2
0
 def test_chebyshev_iteration_2(self):
     '''  Tests the pcd_shell operators produce correct output. '''
     A = np.diag(1. / np.diag(self.quad_mass_matrix)).dot(
         self.quad_mass_matrix)
     n = self.quad_mass_matrix.shape[0]
     alpha = 1. / 4
     beta = 9. / 4
     x0 = np.zeros(n)
     b1 = np.zeros(n)
     for i in range(0, n):
         b1[i] = i
     A_petsc = LAT.dense_numpy_2_petsc4py(A)
     x0_petsc = p4pyPETSc.Vec().createWithArray(x0)
     b1_petsc = p4pyPETSc.Vec().createWithArray(b1)
     solver = LS.ChebyshevSemiIteration(A_petsc,
                                        alpha,
                                        beta,
                                        save_iterations=True)
     solver.apply(b1_petsc, x0_petsc, 20)
     expected = np.load(
         os.path.join(self._scriptdir, 'import_modules/sol_20_lst.npy'))
     for i, item in enumerate(expected):
         assert np.allclose(item, solver.iteration_results[i], 1e-12)
Exemple #3
0
def create_petsc_vecs(matrix_A):
    """
    Creates a right-hand-side and solution PETSc4Py vector for
    testing ksp solves.

    Parameters
    ----------
    matrix_A: :class:`p4pyPETSc.Mat`
        Global matrix object

    Returns
    -------
    vec_lst: tuple
        This is a list of :class:`pypyPETSc.Vec` where the first is
        a vector of ones (usually to act as a RHS-vector) while the
        second vector is a vector of zeros (usually to act as a
        storage vector for the solution).
    """
    b = p4pyPETSc.Vec().create()
    x = p4pyPETSc.Vec().create()
    b.createWithArray(np.ones(matrix_A.getSizes()[0][0]))
    x.createWithArray(np.zeros(matrix_A.getSizes()[0][0]))
    return (b, x)