def test_superlu_2_petsc(small_superlu): """ Tests the function superlu_2_petsc4py """ superlu_mat = small_superlu petsc4py_mat = LinearAlgebraTools.superlu_2_petsc4py(superlu_mat) dense_mat = LinearAlgebraTools.petsc4py_sparse_2_dense(petsc4py_mat) comparison_mat = numpy.loadtxt(os.path.join(os.path.dirname(__file__), 'sparse_mat_1.txt')) assert numpy.allclose(dense_mat,comparison_mat)
def test_superlu_2_petsc(small_superlu): """ Tests the function superlu_2_petsc4py """ superlu_mat = small_superlu petsc4py_mat = LinearAlgebraTools.superlu_2_petsc4py(superlu_mat) dense_mat = LinearAlgebraTools.petsc4py_sparse_2_dense(petsc4py_mat) comparison_mat = numpy.loadtxt( os.path.join(os.path.dirname(__file__), 'sparse_mat_1.txt')) assert numpy.allclose(dense_mat, comparison_mat)
def test_matrix_splitting_1(self): vals_F = [3.2, 1.1, 5.4, 6.3, 1., -5.1, 1.2] col_idx_F = [0, 1, 2, 0, 2, 0, 1] row_idx_F = [0, 3, 5, 7] num_v_unkwn = len(row_idx_F) - 1 petsc_matF = LAT.csr_2_petsc(size = (num_v_unkwn,num_v_unkwn), csr = (row_idx_F,col_idx_F,vals_F)) A = LAT.split_PETSc_Mat(petsc_matF) A[0].axpy(1.0,A[1]) assert np.allclose(A[0].getValuesCSR()[2], petsc_matF.getValuesCSR()[2])
def test_matrix_splitting_1(self): vals_F = [3.2, 1.1, 5.4, 6.3, 1., -5.1, 1.2] col_idx_F = [0, 1, 2, 0, 2, 0, 1] row_idx_F = [0, 3, 5, 7] num_v_unkwn = len(row_idx_F) - 1 petsc_matF = LAT.csr_2_petsc(size=(num_v_unkwn, num_v_unkwn), csr=(row_idx_F, col_idx_F, vals_F)) A = LAT.split_PETSc_Mat(petsc_matF) A[0].axpy(1.0, A[1]) assert np.allclose(A[0].getValuesCSR()[2], petsc_matF.getValuesCSR()[2])
def load_matrix_step_noslip(): """ Loads a medium sized backwards facing step matrix for studying different AMG preconditioners. """ A = LAT.petsc_load_matrix('dump_test_2_step2d_1.0par_j_0') return A
def test_superlu_2_dense(small_superlu): """ Tests the superlu wrapper to dense matrix function. """ superlu_mat = small_superlu dense_mat = LinearAlgebraTools.superlu_sparse_2_dense(superlu_mat) comparison_mat = numpy.loadtxt(os.path.join(os.path.dirname(__file__), 'sparse_mat_1.txt')) assert numpy.allclose(dense_mat,comparison_mat)
def test_superlu_2_dense(small_superlu): """ Tests the superlu wrapper to dense matrix function. """ superlu_mat = small_superlu dense_mat = LinearAlgebraTools.superlu_sparse_2_dense(superlu_mat) comparison_mat = numpy.loadtxt( os.path.join(os.path.dirname(__file__), 'sparse_mat_1.txt')) assert numpy.allclose(dense_mat, comparison_mat)
def create_simple_petsc_matrix(request): vals_F = [3.2, 1.1, 6.3, 1., -5.1] col_idx_F = [0, 1, 0, 1, 2] row_idx_F = [0, 2, 4, 5] num_v_unkwn = 3 petsc_matF = LAT.csr_2_petsc(size=(num_v_unkwn, num_v_unkwn), csr=(row_idx_F, col_idx_F, vals_F)) yield petsc_matF
def create_simple_petsc_matrix(request): vals_F = [3.2, 1.1, 6.3, 1., -5.1] col_idx_F = [0, 1, 0, 1, 2] row_idx_F = [0, 2, 4, 5] num_v_unkwn = 3 petsc_matF = LAT.csr_2_petsc(size = (num_v_unkwn,num_v_unkwn), csr = (row_idx_F,col_idx_F,vals_F)) yield petsc_matF
def setup_LSC_shell(petsc_options, fixture_data): petsc_options.setValue('innerLSCsolver_BTinvBt_ksp_type', 'preonly') petsc_options.setValue('innerLSCsolver_T_ksp_type', 'preonly') petsc_options.setValue('innerLSCsolver_BTinvBt_pc_type', 'lu') petsc_options.setValue('innerLSCsolver_T_pc_type', 'lu') return LAT.LSCInv_shell(fixture_data.petsc_matD, fixture_data.petsc_matB, fixture_data.petsc_matBt, fixture_data.petsc_matF)
def test_tppcd_shell(self): ''' Test for the lsc operator shell ''' Qp_visc = LAT.petsc_load_matrix( os.path.join(self._scriptdir, 'import_modules/Qp_visc')) Qp_dens = LAT.petsc_load_matrix( os.path.join(self._scriptdir, 'import_modules/Qp_dens')) Ap_rho = LAT.petsc_load_matrix( os.path.join(self._scriptdir, 'import_modules/Ap_rho')) Np_rho = LAT.petsc_load_matrix( os.path.join(self._scriptdir, 'import_modules/Np_rho')) alpha = True delta_t = 0.001 x_vec = LAT.petsc_load_vector( os.path.join(self._scriptdir, 'import_modules/input_vec_tppcd')) self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_ksp_type', 'preonly') self.petsc_options.setValue( 'innerTPPCDsolver_Ap_rho_ksp_constant_null_space', '') self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_pc_type', 'hypre') self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_pc_hypre_type', 'boomeramg') TPPCD_shell = LAT.TwoPhase_PCDInv_shell(Qp_visc, Qp_dens, Ap_rho, Np_rho, alpha, delta_t, 5) y_vec = x_vec.copy() y_vec.zeroEntries() A = None TPPCD_shell.apply(A, x_vec, y_vec) true_solu = LAT.petsc_load_vector( os.path.join(self._scriptdir, 'import_modules/tp_pcd_y_output')) assert np.allclose(y_vec.getArray(), true_solu.getArray())
def test_petsc_load_vec(tmpdir): """test petsc_load_matrix """ from petsc4py import PETSc as p4pyPETSc from proteus import LinearAlgebraTools as LAT vals_A = np.array([5.5,7.1,1.0]) A = p4pyPETSc.Vec() A.createWithArray(vals_A) A_tmp = tmpdir.join('A.petsc_vec') LAT._petsc_view(A,A_tmp.strpath) A_test = LAT.petsc_load_vector(A_tmp.strpath) vec_values = A_test.getArray() assert np.allclose(vec_values, vals_A) A_test = LAT.petsc_load_vector('dne.txt') assert A_test is None
def test_petsc_load_vec(tmpdir): """test petsc_load_matrix """ from petsc4py import PETSc as p4pyPETSc from proteus import LinearAlgebraTools as LAT vals_A = np.array([5.5, 7.1, 1.0]) A = p4pyPETSc.Vec() A.createWithArray(vals_A) A_tmp = tmpdir.join('A.petsc_vec') LAT._petsc_view(A, A_tmp.strpath) A_test = LAT.petsc_load_vector(A_tmp.strpath) vec_values = A_test.getArray() assert np.allclose(vec_values, vals_A) A_test = LAT.petsc_load_vector('dne.txt') assert A_test is None
def load_saddle_point_matrix_1(request): """ Loads a small example of a backwards facing step matrix for testing purposes. (Note: this matrix does not have advection) """ A = LAT.petsc_load_matrix(os.path.join (os.path.dirname(__file__), 'import_modules/saddle_point_mat_1')) yield A
def load_small_step_matrix(request): """ Loads a small example of a backwards facing step matrix for testing purposes. (Note: this matrix does not have advection) """ A = LAT.petsc_load_matrix( os.path.join(os.path.dirname(__file__), 'import_modules/saddle_point_small.bin')) yield A
def load_medium_step_matrix(request): """ Loads a medium sized backwards facing step matrix for studying different AMG preconditioners. (Note: this matrix does not have advection) """ A = LAT.petsc_load_matrix(os.path.join (os.path.dirname(__file__), 'import_modules/saddle_point_matrix')) yield A
def load_nse_step_matrix(request): """ Loads a Navier-Stokes matrix for the backwards step problem from the MPRANS module. This matrix is constructed using no-slip boundary conditions, and weakly enforced Dirichlet conditions. """ A = LAT.petsc_load_matrix( os.path.join(os.path.dirname(__file__), 'import_modules/NSE_step_no_slip.bin')) yield A
def load_medium_step_matrix(request): """ Loads a medium sized backwards facing step matrix for studying different AMG preconditioners. (Note: this matrix does not have advection) """ A = LAT.petsc_load_matrix( os.path.join(os.path.dirname(__file__), 'import_modules/saddle_point_matrix.bin')) yield A
def load_nse_step_matrix(request): """ Loads a Navier-Stokes matrix for the backwards step problem from the MPRANS module. This matrix is constructed using no-slip boundary conditions, and weakly enforced Dirichlet conditions. """ A = LAT.petsc_load_matrix(os.path.join (os.path.dirname(__file__), 'import_modules/NSE_step_no_slip.bin')) yield A
def test_superlu_2_petsc(): """ Tests the function superlu_2_petsc4py """ vals = numpy.array([ 10., -2., 3., 9., 3., 7., 8., 7., 3., 8., 7., 5., 8., 9., 9., 13., 4., 2., -1. ]) col_idx = numpy.array( [0, 4, 0, 1, 5, 1, 2, 3, 0, 2, 3, 4, 1, 3, 4, 5, 1, 4, 5], dtype='int32') row_idx = numpy.array([0, 2, 5, 8, 12, 16, 19], dtype='int32') size_n = len(row_idx) - 1 superlu_mat = LinearAlgebraTools.SparseMat(size_n, size_n, len(vals), vals, col_idx, row_idx) petsc4py_mat = LinearAlgebraTools.superlu_2_petsc4py(superlu_mat) dense_mat = LinearAlgebraTools.petsc4py_sparse_2_dense(petsc4py_mat) comparison_mat = numpy.loadtxt( os.path.join(os.path.dirname(__file__), 'sparse_mat_1.txt')) assert numpy.allclose(dense_mat, comparison_mat)
def test_lsc_shell(self): ''' Test for the lsc operator shell ''' vals_A = [5.5, 7.1, 1.0] col_idx_A = [0, 1, 2] row_idx_A = [0, 1, 2, 3] vals_B = [1.1, 6.3, 7.3, 3.6, 6.3] col_idx_B = [0, 2, 0, 1, 2] row_idx_B = [0, 2, 5] vals_Bt = [1.1, 7.3, 3.6, 6.3, 6.3] col_idx_Bt = [0, 1, 1, 0, 1] row_idx_Bt = [0, 2, 3, 5] vals_F = [3.2, 1.1, 6.3, 1., -5.1] col_idx_F = [0, 1, 0, 2, 0] row_idx_F = [0, 2, 4, 5] num_B_rows = len(row_idx_B) - 1 num_B_cols = len(row_idx_A) - 1 petsc_matA = LAT.csr_2_petsc(size=(num_B_cols, num_B_cols), csr=(row_idx_A, col_idx_A, vals_A)) petsc_matB = LAT.csr_2_petsc(size=(num_B_rows, num_B_cols), csr=(row_idx_B, col_idx_B, vals_B)) petsc_matBt = LAT.csr_2_petsc(size=(num_B_cols, num_B_rows), csr=(row_idx_Bt, col_idx_Bt, vals_Bt)) petsc_matF = LAT.csr_2_petsc(size=(num_B_cols, num_B_cols), csr=(row_idx_F, col_idx_F, vals_F)) self.petsc_options.setValue('innerLSCsolver_BTinvBt_ksp_type', 'preonly') self.petsc_options.setValue('innerLSCsolver_T_ksp_type', 'preonly') self.petsc_options.setValue('innerLSCsolver_BTinvBt_pc_type', 'lu') self.petsc_options.setValue('innerLSCsolver_T_pc_type', 'lu') LSC_shell = LAT.LSCInv_shell(petsc_matA, petsc_matB, petsc_matBt, petsc_matF) x_vec = np.ones(num_B_rows) y_vec = np.zeros(num_B_rows) x_PETSc_vec = p4pyPETSc.Vec().createWithArray(x_vec) y_PETSc_vec = p4pyPETSc.Vec().createWithArray(y_vec) A = None LSC_shell.apply(A, x_PETSc_vec, y_PETSc_vec) true_solu = np.mat('[-0.01096996,0.00983216]') assert np.allclose(y_vec, true_solu)
def test_petsc_load_matrix(tmpdir): """test petsc_load_matrix """ from petsc4py import PETSc as p4pyPETSc from proteus import LinearAlgebraTools as LAT vals_A = [5.5, 7.1, 1.0] col_idx_A = [0, 1, 2] row_idx_A = [0, 1, 2, 3] A = LAT.csr_2_petsc(size=(3, 3), csr=(row_idx_A, col_idx_A, vals_A)) A_tmp = tmpdir.join('A.petsc_mat') LAT._petsc_view(A, A_tmp.strpath) A_test = LAT.petsc_load_matrix(A_tmp.strpath) csr_values = A_test.getValuesCSR() assert np.allclose(csr_values[0], row_idx_A) assert np.allclose(csr_values[1], col_idx_A) assert np.allclose(csr_values[2], vals_A) A_test = LAT.petsc_load_matrix('dne.txt') assert A_test is None
def test_tppcd_shell_with_chebyshev_iteration(self): ''' Test for the lsc operator shell ''' Qp_visc = LAT.petsc_load_matrix(os.path.join(self._scriptdir,'import_modules/Qp_visc.bin')) Qp_dens = LAT.petsc_load_matrix(os.path.join(self._scriptdir,'import_modules/Qp_dens.bin')) Ap_rho = LAT.petsc_load_matrix(os.path.join(self._scriptdir, 'import_modules/Ap_rho.bin')) Np_rho = LAT.petsc_load_matrix(os.path.join(self._scriptdir, 'import_modules/Np_rho.bin')) alpha = True delta_t = 0.001 x_vec = LAT.petsc_load_vector(os.path.join(self._scriptdir,'import_modules/input_vec_tppcd.bin')) self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_ksp_type','preonly') self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_ksp_constant_null_space', '') self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_pc_type','hypre') self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_pc_hypre_type','boomeramg') TPPCD_shell = LAT.TwoPhase_PCDInv_shell(Qp_visc, Qp_dens, Ap_rho, Np_rho, alpha, delta_t, 5, laplace_null_space=True) y_vec = x_vec.copy() y_vec.zeroEntries() A = None TPPCD_shell.apply(A,x_vec,y_vec) true_solu = LAT.petsc_load_vector(os.path.join(self._scriptdir, 'import_modules/tp_pcd_y_output.bin')) assert np.allclose(y_vec.getArray(),true_solu.getArray())
def test_petsc_load_matrix(tmpdir): """test petsc_load_matrix """ from petsc4py import PETSc as p4pyPETSc from proteus import LinearAlgebraTools as LAT vals_A = [5.5,7.1,1.0] col_idx_A = [0 , 1 , 2 ] row_idx_A = [0, 1, 2, 3] A = LAT.csr_2_petsc(size = (3,3), csr = (row_idx_A,col_idx_A,vals_A)) A_tmp = tmpdir.join('A.petsc_mat') LAT._petsc_view(A,A_tmp.strpath) A_test = LAT.petsc_load_matrix(A_tmp.strpath) csr_values = A_test.getValuesCSR() assert np.allclose(csr_values[0], row_idx_A) assert np.allclose(csr_values[1], col_idx_A) assert np.allclose(csr_values[2], vals_A) A_test = LAT.petsc_load_matrix('dne.txt') assert A_test is None
def small_superlu(): vals = numpy.array([ 10., -2., 3., 9., 3., 7., 8., 7., 3., 8., 7., 5., 8., 9., 9., 13., 4., 2., -1. ]) col_idx = numpy.array( [0, 4, 0, 1, 5, 1, 2, 3, 0, 2, 3, 4, 1, 3, 4, 5, 1, 4, 5], dtype='int32') row_idx = numpy.array([0, 2, 5, 8, 12, 16, 19], dtype='int32') size_n = len(row_idx) - 1 superlu_mat = LinearAlgebraTools.SparseMat(size_n, size_n, len(vals), vals, col_idx, row_idx) yield superlu_mat
def test_superlu_2_petsc(): """ Tests the function superlu_2_petsc4py """ vals = numpy.array([10.,-2.,3.,9.,3.,7.,8.,7.,3., 8.,7.,5.,8.,9.,9.,13.,4.,2.,-1.]) col_idx = numpy.array([0, 4 ,0 ,1 ,5 ,1 ,2 ,3 ,0 ,2 , 3 ,4 ,1 ,3 ,4 ,5 ,1 ,4 , 5 ], dtype='int32') row_idx = numpy.array([0, 2, 5, 8, 12, 16, 19], dtype='int32') size_n = len(row_idx)-1 superlu_mat = LinearAlgebraTools.SparseMat(size_n, size_n, len(vals), vals, col_idx, row_idx) petsc4py_mat = LinearAlgebraTools.superlu_2_petsc4py(superlu_mat) dense_mat = LinearAlgebraTools.petsc4py_sparse_2_dense(petsc4py_mat) comparison_mat = numpy.loadtxt(os.path.join(os.path.dirname(__file__), 'sparse_mat_1.txt')) assert numpy.allclose(dense_mat,comparison_mat)
def test_1(self): """ Initial test to check whether this is working """ self.laplace_object.modelList[0].levelModelList[ 0].calculateCoefficients() rowptr, colind, nzval = self.laplace_object.modelList[ 0].levelModelList[0].jacobian.getCSRrepresentation() self.laplace_object.modelList[0].levelModelList[0].scale_dt = False self.Asys_rowptr = rowptr.copy() self.Asys_colptr = colind.copy() self.Asys_nzval = nzval.copy() nn = len(self.Asys_rowptr) - 1 self.Asys = LinearAlgebraTools.SparseMatrix(nn, nn, self.Asys_nzval.shape[0], self.Asys_nzval, self.Asys_colptr, self.Asys_rowptr) self.petsc4py_A = self.laplace_object.modelList[0].levelModelList[ 0].getSpatialJacobian(self.Asys) laplace_mat = LinearAlgebraTools.superlu_sparse_2_dense( self.petsc4py_A) expected_output = os.path.dirname(os.path.abspath( __file__)) + '/comparison_files/laplace_reference_c0p1_3D.txt' comparison_mat = numpy.loadtxt(expected_output) assert numpy.allclose(laplace_mat, comparison_mat)
def test_tppcd_shell_with_dirichlet_pressure(self): ''' Test for the lsc operator shell ''' Qp_visc = LAT.petsc_load_matrix( os.path.join(self._scriptdir, 'import_modules/Qp_visc.bin')) Qp_dens = LAT.petsc_load_matrix( os.path.join(self._scriptdir, 'import_modules/Qp_dens.bin')) Ap_rho = LAT.petsc_load_matrix( os.path.join(self._scriptdir, 'import_modules/Ap_rho.bin')) Np_rho = LAT.petsc_load_matrix( os.path.join(self._scriptdir, 'import_modules/Np_rho.bin')) alpha = True delta_t = 0.001 x_vec = LAT.petsc_load_vector( os.path.join(self._scriptdir, 'import_modules/input_vec_tppcd.bin')) self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_ksp_type', 'preonly') self.petsc_options.setValue( 'innerTPPCDsolver_Ap_rho_ksp_constant_null_space', '') self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_pc_type', 'hypre') self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_pc_hypre_type', 'boomeramg') dirichlet_nodes = [3, 12, 15, 21, 33] TPPCD_shell = LAT.TwoPhase_PCDInv_shell( Qp_visc, Qp_dens, Ap_rho, Np_rho, alpha=alpha, delta_t=delta_t, num_chebyshev_its=5, strong_dirichlet_DOF=dirichlet_nodes) # Test the index set is set correctly unknown_indices = np.arange(TPPCD_shell.getSize()) known_indices_mask = np.ones(TPPCD_shell.getSize(), dtype=bool) known_indices_mask[dirichlet_nodes] = False unknown_is = unknown_indices[known_indices_mask] assert np.array_equal(unknown_is, TPPCD_shell.unknown_dof_is.getIndices()) y_vec = x_vec.copy() y_vec.zeroEntries() A = None TPPCD_shell.apply(A, x_vec, y_vec) assert np.array_equal(y_vec[dirichlet_nodes], [0., 0., 0., 0., 0.]) true_solu = LAT.petsc_load_vector( os.path.join(self._scriptdir, 'import_modules/tppcd_y_dirichlet_dof.bin')) assert np.allclose(y_vec.getArray(), true_solu.getArray())
def test_SpInv_shell(self, create_simple_saddle_point_problem): """Test :math:`S_{p}` shell has correct behavior. """ fixture_data = create_simple_saddle_point_problem self.petsc_options.setValue('innerSpsolver_ksp_type', 'preonly') self.petsc_options.setValue('innerSpsolver_pc_type', 'hypre') self.petsc_options.setValue('innerSpsolver_pc_hypre_type', 'boomeramg') SpInv_shell = LAT.SpInv_shell(fixture_data.petsc_matF, fixture_data.petsc_matC, fixture_data.petsc_matBt, fixture_data.petsc_matB, constNullSpace=False) SpInv_shell.apply(None, fixture_data.x_vec, fixture_data.y_vec) true_solu = np.mat('[1.0655362, -0.30354183]') assert np.allclose(fixture_data.y_vec.getArray(), true_solu)
def test_lsc_shell(self, create_simple_saddle_point_problem): ''' Test for the lsc operator shell ''' fixture_data = create_simple_saddle_point_problem self.petsc_options.setValue('innerLSCsolver_BTinvBt_ksp_type', 'preonly') self.petsc_options.setValue('innerLSCsolver_T_ksp_type', 'preonly') self.petsc_options.setValue('innerLSCsolver_BTinvBt_pc_type', 'lu') self.petsc_options.setValue('innerLSCsolver_T_pc_type', 'lu') LSC_shell = LAT.LSCInv_shell(fixture_data.petsc_matD, fixture_data.petsc_matB, fixture_data.petsc_matBt, fixture_data.petsc_matF) LSC_shell.apply(None, fixture_data.x_vec, fixture_data.y_vec) true_solu = np.mat('[-0.01096996,0.00983216]') assert np.allclose(fixture_data.y_vec.getArray(), true_solu)
def test_1(self): """ Initial test to check whether this is working """ self.laplace_object.modelList[0].levelModelList[0].calculateCoefficients() rowptr, colind, nzval = self.laplace_object.modelList[0].levelModelList[0].jacobian.getCSRrepresentation() self.laplace_object.modelList[0].levelModelList[0].scale_dt = False self.Asys_rowptr = rowptr.copy() self.Asys_colptr = colind.copy() self.Asys_nzval = nzval.copy() nn = len(self.Asys_rowptr)-1 self.Asys = LinearAlgebraTools.SparseMatrix(nn,nn, self.Asys_nzval.shape[0], self.Asys_nzval, self.Asys_colptr, self.Asys_rowptr) self.petsc4py_A = self.laplace_object.modelList[0].levelModelList[0].getSpatialJacobian(self.Asys) laplace_mat = LinearAlgebraTools.superlu_sparse_2_dense(self.petsc4py_A) expected_output = os.path.dirname(os.path.abspath(__file__)) + '/comparison_files/laplace_reference_c0p1_2D.txt' comparison_mat = numpy.loadtxt(expected_output) assert numpy.allclose(laplace_mat,comparison_mat)
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())
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)
def test_create_petsc_ksp_obj(create_simple_petsc_matrix): def getSize(): return 3 petsc_options = p4pyPETSc.Options() petsc_options.setValue('test_F_ksp_type', 'preonly') petsc_options.setValue('test_F_pc_type', 'lu') petsc_matF = create_simple_petsc_matrix InvOpShell = LAT.InvOperatorShell() InvOpShell.const_null_space = False InvOpShell.options = petsc_options InvOpShell.strong_dirichlet_DOF = [1] InvOpShell.getSize = getSize A = InvOpShell.create_petsc_ksp_obj('test_F_', petsc_matF) assert A.getClassName() == 'KSP' assert A.getOperators()[0].equal(petsc_matF) InvOpShell._set_dirichlet_idx_set() assert np.array_equal(InvOpShell.unknown_dof_is.getIndices(), np.array([0, 2]))
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())
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)
def test_tppcd_shell_with_chebyshev_iteration(self): ''' Test for the lsc operator shell ''' Qp_visc = LAT.petsc_load_matrix( os.path.join(self._scriptdir, 'import_modules/Qp_visc.bin')) Qp_dens = LAT.petsc_load_matrix( os.path.join(self._scriptdir, 'import_modules/Qp_dens.bin')) Ap_rho = LAT.petsc_load_matrix( os.path.join(self._scriptdir, 'import_modules/Ap_rho.bin')) Np_rho = LAT.petsc_load_matrix( os.path.join(self._scriptdir, 'import_modules/Np_rho.bin')) alpha = True delta_t = 0.001 x_vec = LAT.petsc_load_vector( os.path.join(self._scriptdir, 'import_modules/input_vec_tppcd.bin')) self.petsc_options.clear() self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_ksp_type', 'preonly') self.petsc_options.setValue( 'innerTPPCDsolver_Ap_rho_ksp_constant_null_space', '') self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_pc_type', 'hypre') self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_pc_hypre_type', 'boomeramg') TPPCD_shell = LAT.TwoPhase_PCDInv_shell(Qp_visc, Qp_dens, Ap_rho, Np_rho, alpha, delta_t, 5, laplace_null_space=True) y_vec = x_vec.copy() y_vec.zeroEntries() A = None TPPCD_shell.apply(A, x_vec, y_vec) #np.savetxt(os.path.join(self._scriptdir,'comparison_files/tp_pcd_y_output_cheb.csv'),y_vec.getArray(),delimiter=',') true_solu = np.loadtxt(os.path.join( self._scriptdir, 'comparison_files/tp_pcd_y_output_cheb.csv'), delimiter=',') np.testing.assert_allclose(y_vec.getArray(), true_solu, rtol=1e-8, atol=1e-8)
def test_tppcd_shell_with_dirichlet_pressure(self): ''' Test for the lsc operator shell ''' Qp_visc = LAT.petsc_load_matrix(os.path.join(self._scriptdir,'import_modules/Qp_visc.bin')) Qp_dens = LAT.petsc_load_matrix(os.path.join(self._scriptdir,'import_modules/Qp_dens.bin')) Ap_rho = LAT.petsc_load_matrix(os.path.join(self._scriptdir, 'import_modules/Ap_rho.bin')) Np_rho = LAT.petsc_load_matrix(os.path.join(self._scriptdir, 'import_modules/Np_rho.bin')) alpha = True delta_t = 0.001 x_vec = LAT.petsc_load_vector(os.path.join(self._scriptdir,'import_modules/input_vec_tppcd.bin')) self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_ksp_type','preonly') self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_ksp_constant_null_space', '') self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_pc_type','hypre') self.petsc_options.setValue('innerTPPCDsolver_Ap_rho_pc_hypre_type','boomeramg') dirichlet_nodes = [3, 12, 15, 21, 33] TPPCD_shell = LAT.TwoPhase_PCDInv_shell(Qp_visc, Qp_dens, Ap_rho, Np_rho, alpha = alpha, delta_t = delta_t, num_chebyshev_its = 5, strong_dirichlet_DOF = dirichlet_nodes) # Test the index set is set correctly unknown_indices = np.arange(TPPCD_shell.getSize()) known_indices_mask = np.ones(TPPCD_shell.getSize(),dtype=bool) known_indices_mask[dirichlet_nodes] = False unknown_is = unknown_indices[known_indices_mask] assert np.array_equal(unknown_is, TPPCD_shell.unknown_dof_is.getIndices()) y_vec = x_vec.copy() y_vec.zeroEntries() A = None TPPCD_shell.apply(A,x_vec,y_vec) assert np.array_equal(y_vec[dirichlet_nodes], [0.,0.,0.,0.,0.]) true_solu = LAT.petsc_load_vector(os.path.join(self._scriptdir, 'import_modules/tppcd_y_dirichlet_dof.bin')) assert np.allclose(y_vec.getArray(),true_solu.getArray())
def load_rans2p_step_newton_5(request): A = LAT.petsc_load_matrix(os.path.join (os.path.dirname(__file__), 'import_modules/rans2p_step_newton_5.bin')) yield A
def test_petsc4py_has_pressure_null_space_1(small_petsc4py): superlu_mat = small_petsc4py has_null = LinearAlgebraTools.petsc4py_mat_has_pressure_null_space(superlu_mat) assert has_null == False
def test_petsc4py_get_rank_2(med_petsc_with_const_pressure): matr = med_petsc_with_const_pressure rank = LinearAlgebraTools.petsc4py_get_rank(matr) assert rank == 44
def test_superlu_get_rank_1(small_superlu): superlu_mat = small_superlu rank = LinearAlgebraTools.superlu_get_rank(superlu_mat) assert rank == 6
def load_nse_cavity_matrix(request): """Loads a Navier-Stokes matrix drawn from the MPRANS module. """ A = LAT.petsc_load_matrix( os.path.join(os.path.dirname(__file__), 'import_modules/NSE_cavity_matrix.bin')) yield A
def setup_method(self,method): self._scriptdir = os.path.dirname(__file__) self.saddle_point_matrix=LAT.petsc_load_matrix(os.path.join(self._scriptdir, 'import_modules/saddle_point_small'))
def create_simple_saddle_point_problem(request): """Builds simple matrices and vectors for saddle point shell tests. Returns ------- output_lst : lst This function returns an output list with three sublists. See the notes below for a description of these lists. Notes ----- The output of this function returns xxx sublists. The first contains a list of matrices. The second returns a list of vectors. The last returns a list of sizes. """ class Output_Storage(object): """Storage class for matrix objects. """ def __init__(self, petsc_matF, petsc_matD, petsc_matB, petsc_matBt, petsc_matC, x_vec, y_vec, num_p_unkwn, num_v_unkwn): self.petsc_matF = petsc_matF self.petsc_matD = petsc_matD self.petsc_matB = petsc_matB self.petsc_matBt = petsc_matBt self.petsc_matC = petsc_matC self.x_vec = x_vec self.y_vec = y_vec self.num_p_unkwn = num_p_unkwn self.num_v_unkwn = num_v_unkwn vals_F = [3.2, 1.1, 6.3, 1., -5.1] col_idx_F = [0, 1, 0, 2, 0] row_idx_F = [0, 2, 4, 5] vals_D = [5.5,7.1,1.0] col_idx_D = [0 , 1 , 2 ] row_idx_D = [0, 1, 2, 3] vals_B = [1.1, 6.3, 7.3, 3.6, 6.3] col_idx_B = [0 , 2 , 0 , 1 , 2 ] row_idx_B = [0, 2, 5] vals_Bt = [1.1, 7.3, 3.6, 6.3, 6.3] col_idx_Bt = [0, 1, 1, 0, 1] row_idx_Bt = [0, 2, 3, 5] vals_C = [1.2, 2.1, 3.3] col_idx_C = [0, 1, 1] row_idx_C = [0, 2, 3] num_p_unkwn = len(row_idx_B) - 1 num_v_unkwn = len(row_idx_F) - 1 petsc_matF = LAT.csr_2_petsc(size = (num_v_unkwn,num_v_unkwn), csr = (row_idx_F,col_idx_F,vals_F)) petsc_matD = LAT.csr_2_petsc(size = (num_v_unkwn,num_v_unkwn), csr = (row_idx_D,col_idx_D,vals_D)) petsc_matB = LAT.csr_2_petsc(size = (num_p_unkwn,num_v_unkwn), csr = (row_idx_B,col_idx_B,vals_B)) petsc_matBt = LAT.csr_2_petsc(size = (num_v_unkwn,num_p_unkwn), csr = (row_idx_Bt,col_idx_Bt,vals_Bt)) petsc_matC = LAT.csr_2_petsc(size = (num_p_unkwn,num_p_unkwn), csr = (row_idx_C,col_idx_C,vals_C)) x_vec = np.ones(num_p_unkwn) y_vec = np.zeros(num_p_unkwn) x_PETSc_vec = p4pyPETSc.Vec().createWithArray(x_vec) y_PETSc_vec = p4pyPETSc.Vec().createWithArray(y_vec) output_data = Output_Storage(petsc_matF, petsc_matD, petsc_matB, petsc_matBt, petsc_matC, x_PETSc_vec, y_PETSc_vec, num_p_unkwn, num_v_unkwn) yield output_data
def med_petsc_with_const_pressure(): petsc_mat = LinearAlgebraTools.petsc_load_matrix(os.path.join(os.path.dirname(__file__), 'jac.bin')) yield petsc_mat
def load_rans2p_step_newton_5(request): A = LAT.petsc_load_matrix( os.path.join(os.path.dirname(__file__), 'import_modules/rans2p_step_newton_5.bin')) yield A
def load_nse_cavity_matrix(request): """Loads a Navier-Stokes matrix drawn from the MPRANS module. """ A = LAT.petsc_load_matrix('dump_stokes_drivenCavityStokesTrial_1.0par_j_1') yield A
def test_petsc4py_has_pressure_null_space_2(med_petsc_with_const_pressure): superlu_mat = med_petsc_with_const_pressure has_null = LinearAlgebraTools.petsc4py_mat_has_pressure_null_space(superlu_mat) assert has_null == True
def load_matrix(mat_file_name): """Load a matrix """ A = LAT.petsc_load_matrix(os.path.join (os.path.dirname(__file__), 'import_modules/'+mat_file_name)) return A
def setup_method(self, method): self._scriptdir = os.path.dirname(__file__) self.saddle_point_matrix = LAT.petsc_load_matrix( os.path.join(self._scriptdir, 'import_modules/saddle_point_small'))
def create_simple_saddle_point_problem(request): """Builds simple matrices and vectors for saddle point shell tests. Returns ------- output_lst : lst This function returns an output list with three sublists. See the notes below for a description of these lists. Notes ----- The output of this function returns xxx sublists. The first contains a list of matrices. The second returns a list of vectors. The last returns a list of sizes. """ class Output_Storage(object): """Storage class for matrix objects. """ def __init__(self, petsc_matF, petsc_matD, petsc_matB, petsc_matBt, petsc_matC, x_vec, y_vec, num_p_unkwn, num_v_unkwn): self.petsc_matF = petsc_matF self.petsc_matD = petsc_matD self.petsc_matB = petsc_matB self.petsc_matBt = petsc_matBt self.petsc_matC = petsc_matC self.x_vec = x_vec self.y_vec = y_vec self.num_p_unkwn = num_p_unkwn self.num_v_unkwn = num_v_unkwn vals_F = [3.2, 1.1, 6.3, 1., -5.1] col_idx_F = [0, 1, 0, 2, 0] row_idx_F = [0, 2, 4, 5] vals_D = [5.5, 7.1, 1.0] col_idx_D = [0, 1, 2] row_idx_D = [0, 1, 2, 3] vals_B = [1.1, 6.3, 7.3, 3.6, 6.3] col_idx_B = [0, 2, 0, 1, 2] row_idx_B = [0, 2, 5] vals_Bt = [1.1, 7.3, 3.6, 6.3, 6.3] col_idx_Bt = [0, 1, 1, 0, 1] row_idx_Bt = [0, 2, 3, 5] vals_C = [1.2, 2.1, 3.3] col_idx_C = [0, 1, 1] row_idx_C = [0, 2, 3] num_p_unkwn = len(row_idx_B) - 1 num_v_unkwn = len(row_idx_F) - 1 petsc_matF = LAT.csr_2_petsc(size=(num_v_unkwn, num_v_unkwn), csr=(row_idx_F, col_idx_F, vals_F)) petsc_matD = LAT.csr_2_petsc(size=(num_v_unkwn, num_v_unkwn), csr=(row_idx_D, col_idx_D, vals_D)) petsc_matB = LAT.csr_2_petsc(size=(num_p_unkwn, num_v_unkwn), csr=(row_idx_B, col_idx_B, vals_B)) petsc_matBt = LAT.csr_2_petsc(size=(num_v_unkwn, num_p_unkwn), csr=(row_idx_Bt, col_idx_Bt, vals_Bt)) petsc_matC = LAT.csr_2_petsc(size=(num_p_unkwn, num_p_unkwn), csr=(row_idx_C, col_idx_C, vals_C)) x_vec = np.ones(num_p_unkwn) y_vec = np.zeros(num_p_unkwn) x_PETSc_vec = p4pyPETSc.Vec().createWithArray(x_vec) y_PETSc_vec = p4pyPETSc.Vec().createWithArray(y_vec) output_data = Output_Storage(petsc_matF, petsc_matD, petsc_matB, petsc_matBt, petsc_matC, x_PETSc_vec, y_PETSc_vec, num_p_unkwn, num_v_unkwn) yield output_data
def test_dense_numpy_2_petsc4py(self): A_petsc = LAT.dense_numpy_2_petsc4py(self.quad_mass_matrix) A_new = LAT.petsc4py_sparse_2_dense(A_petsc) assert np.linalg.norm(self.quad_mass_matrix - A_new) == 0
def test_superlu_has_pressure_null_space(small_superlu): superlu_mat = small_superlu has_null = LinearAlgebraTools.superlu_has_pressure_null_space(superlu_mat) assert has_null == False
def load_nse_cavity_matrix(request): """Loads a Navier-Stokes matrix drawn from the MPRANS module. """ A = LAT.petsc_load_matrix(os.path.join (os.path.dirname(__file__), 'import_modules/NSE_cavity_matrix.bin')) yield A
def load_matrix(mat_file_name): """Load a matrix """ A = LAT.petsc_load_matrix( os.path.join(os.path.dirname(__file__), 'import_modules/' + mat_file_name)) return A
def test_petsc4py_get_rank_1(small_petsc4py): matr = small_petsc4py rank = LinearAlgebraTools.petsc4py_get_rank(small_petsc4py) assert rank == 6