25 PetscFunctionBeginUser;
30 ierr = PetscFinalized(&finalized); CHKERRV(ierr);
31 if (finalized)
return;
33 ierr = KSPDestroy(&
ksp); CHKERRV(ierr);
41 ierr = KSPDestroy(&
ksp); CHKERRQ(ierr);
44 PetscFunctionReturn(0);
50 PetscFunctionBeginUser;
58 ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,
nullptr,
config.c_str(),
59 PETSC_TRUE); CHKERRQ(ierr);
62 ierr = KSPCreate(PETSC_COMM_WORLD, &
ksp); CHKERRQ(ierr);
63 ierr = KSPSetOptionsPrefix(
ksp, (
name +
"_").c_str()); CHKERRQ(ierr);
64 ierr = KSPSetType(
ksp, KSPCG); CHKERRQ(ierr);
65 ierr = KSPSetReusePreconditioner(
ksp, PETSC_TRUE); CHKERRQ(ierr);
66 ierr = KSPSetFromOptions(
ksp); CHKERRQ(ierr);
68 PetscFunctionReturn(0);
74 PetscFunctionBeginUser;
78 ierr = KSPReset(
ksp); CHKERRQ(ierr);
79 ierr = KSPSetOperators(
ksp, A, A); CHKERRQ(ierr);
81 PetscFunctionReturn(0);
87 PetscFunctionBeginUser;
90 KSPConvergedReason reason;
92 ierr = KSPSolve(
ksp, b,
x); CHKERRQ(ierr);
94 ierr = KSPGetConvergedReason(
ksp, &reason); CHKERRQ(ierr);
98 ierr = KSPReasonView(
ksp, PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr);
100 SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_CONV_FAILED,
101 "PetIBM exited due to PETSc KSP solver %s diverged with "
103 name.c_str(), reason);
106 PetscFunctionReturn(0);
112 PetscFunctionBeginUser;
116 ierr = KSPGetIterationNumber(
ksp, &iters); CHKERRQ(ierr);
118 PetscFunctionReturn(0);
124 PetscFunctionBeginUser;
128 ierr = KSPGetResidualNorm(
ksp, &res); CHKERRQ(ierr);
130 PetscFunctionReturn(0);
The abstract (base) class for different iterative solvers.
std::string config
Path of the solver configuration file to read.
std::string name
Name of the linear solver.
virtual PetscErrorCode destroy()
Manually destroy the data in the current instance.
std::string type
Type of the linear solver.
KSP ksp
the underlying KSP solver
virtual PetscErrorCode solve(Vec &x, Vec &b)
Solve the linear system.
virtual PetscErrorCode init()
Private initialization function.
virtual PetscErrorCode setMatrix(const Mat &A)
Set the coefficient matrix of the linear system.
virtual PetscErrorCode destroy()
Manually destroy the data in the current instance.
virtual ~LinSolverKSP()
Default destructor.
virtual PetscErrorCode getResidual(PetscReal &res)
Get the final residual of the solver.
LinSolverKSP(const std::string &solverName, const std::string &file)
Constructor.
virtual PetscErrorCode getIters(PetscInt &iters)
Get the number of iterations of the solver.
A toolbox for building flow solvers.