00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <PETScKrylovSolver.hpp>
00021 #include <ErrorHandler.hpp>
00022
00023 int monitor(KSP ksp, int it, double norm, void*)
00024 {
00025 std::cout << "iteration: " << it << " residual norm = " << norm << '\n';
00026 return 0;
00027 }
00028
00029 PETScKrylovSolver::
00030 PETScKrylovSolver(const Vector<real_t>& b,
00031 const PETScMatrix& A,
00032 Vector<real_t>& x)
00033 {
00034 Vec petscB;
00035 VecCreateSeqWithArray(PETSC_COMM_WORLD, b.size(), &b[0], &petscB);
00036
00037 Vec petscX;
00038 VecCreateSeqWithArray(PETSC_COMM_WORLD, x.size(), &x[0], &petscX);
00039
00040 KSP ksp;
00041 KSPCreate(PETSC_COMM_WORLD, &ksp);
00042 KSPSetTolerances(ksp, 1E-20, 1E-100,1E5, 500);
00043 KSPSetType(ksp, KSPGMRES);
00044
00045 PC pc;
00046 KSPGetPC(ksp, &pc);
00047 PCSetType(pc, PCNONE);
00048 KSPSetOperators(ksp, A.mat(), A.mat(), DIFFERENT_NONZERO_PATTERN);
00049
00050 KSPSetRhs(ksp, petscB);
00051 KSPSetSolution(ksp, petscX);
00052 KSPSetUp(ksp);
00053
00054 KSPSetMonitor(ksp, monitor, 0, 0);
00055
00056 KSPSolve(ksp);
00057
00058 KSPDestroy(ksp);
00059 VecDestroy(petscB);
00060 VecDestroy(petscX);
00061
00062 }