PetIBM 0.5.4
Toolbox and applications of the immersed-boundary method for distributed-memory architectures
linsolverksp.cpp
Go to the documentation of this file.
1
8// PetIBM
10
11namespace petibm
12{
13namespace linsolver
14{
15// implement LinSolverKSP::LinSolverKSP
16LinSolverKSP::LinSolverKSP(const std::string &_name, const std::string &_config)
17 : LinSolverBase(_name, _config)
18{
19 init();
20} // LinSolverKSP
21
22// implement LinSolverKSP::~LinSolverKSP
24{
25 PetscFunctionBeginUser;
26
27 PetscErrorCode ierr;
28 PetscBool finalized;
29
30 ierr = PetscFinalized(&finalized); CHKERRV(ierr);
31 if (finalized) return;
32
33 ierr = KSPDestroy(&ksp); CHKERRV(ierr);
34} // ~LinSolverKSP
35
36// implement LinSolverKSP::destroy
37PetscErrorCode LinSolverKSP::destroy()
38{
39 PetscErrorCode ierr;
40
41 ierr = KSPDestroy(&ksp); CHKERRQ(ierr);
42 ierr = LinSolverBase::destroy(); CHKERRQ(ierr);
43
44 PetscFunctionReturn(0);
45} // destroy
46
47// implement LinSolverKSP::init
48PetscErrorCode LinSolverKSP::init()
49{
50 PetscFunctionBeginUser;
51
52 PetscErrorCode ierr;
53
54 type = "PETSc KSP";
55
56 if (config != "None")
57 {
58 ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD, nullptr, config.c_str(),
59 PETSC_TRUE); CHKERRQ(ierr);
60 }
61
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);
67
68 PetscFunctionReturn(0);
69} // init
70
71// implement LinSolverKSP::setMatrix
72PetscErrorCode LinSolverKSP::setMatrix(const Mat &A)
73{
74 PetscFunctionBeginUser;
75
76 PetscErrorCode ierr;
77
78 ierr = KSPReset(ksp); CHKERRQ(ierr);
79 ierr = KSPSetOperators(ksp, A, A); CHKERRQ(ierr);
80
81 PetscFunctionReturn(0);
82} // setMatrix
83
84// implement LinSolverKSP::solve
85PetscErrorCode LinSolverKSP::solve(Vec &x, Vec &b)
86{
87 PetscFunctionBeginUser;
88
89 PetscErrorCode ierr;
90 KSPConvergedReason reason;
91
92 ierr = KSPSolve(ksp, b, x); CHKERRQ(ierr);
93
94 ierr = KSPGetConvergedReason(ksp, &reason); CHKERRQ(ierr);
95
96 if (reason < 0)
97 {
98 ierr = KSPReasonView(ksp, PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr);
99
100 SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_CONV_FAILED,
101 "PetIBM exited due to PETSc KSP solver %s diverged with "
102 "reason %d.",
103 name.c_str(), reason);
104 }
105
106 PetscFunctionReturn(0);
107} // solve
108
109// implement LinSolverKSP::getIters
110PetscErrorCode LinSolverKSP::getIters(PetscInt &iters)
111{
112 PetscFunctionBeginUser;
113
114 PetscErrorCode ierr;
115
116 ierr = KSPGetIterationNumber(ksp, &iters); CHKERRQ(ierr);
117
118 PetscFunctionReturn(0);
119} // getIters
120
121// implement LinSolverKSP::getResidual
122PetscErrorCode LinSolverKSP::getResidual(PetscReal &res)
123{
124 PetscFunctionBeginUser;
125
126 PetscErrorCode ierr;
127
128 ierr = KSPGetResidualNorm(ksp, &res); CHKERRQ(ierr);
129
130 PetscFunctionReturn(0);
131} // getResidual
132
133} // end of namespace linsolver
134} // end of namespace petibm
The abstract (base) class for different iterative solvers.
Definition: linsolver.h:60
std::string config
Path of the solver configuration file to read.
Definition: linsolver.h:132
std::string name
Name of the linear solver.
Definition: linsolver.h:129
virtual PetscErrorCode destroy()
Manually destroy the data in the current instance.
Definition: linsolver.cpp:21
std::string type
Type of the linear solver.
Definition: linsolver.h:135
KSP ksp
the underlying KSP solver
Definition: linsolverksp.h:58
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.
Def. of LinSolverKSP.
A toolbox for building flow solvers.
Definition: bodypack.h:52