-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLinearSystemSolver.cpp
More file actions
354 lines (280 loc) · 10.6 KB
/
Copy pathLinearSystemSolver.cpp
File metadata and controls
354 lines (280 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/************************************************************************
* Copyright © 2020 The Multiphysics Modeling and Computation (M2C) Lab
* <kevin.wgy@gmail.com> <kevinw3@vt.edu>
************************************************************************/
#include<LinearSystemSolver.h>
#include<cassert>
#include<iostream>
//-----------------------------------------------------
LinearSystemSolver::LinearSystemSolver(MPI_Comm &comm_, DM &dm_, LinearSolverData &lin_input,
const char *equation_name_)
: LinearOperator(comm_, dm_), log_filename(lin_input.logfile),
Xtmp(comm_, &dm_), Rtmp(comm_, &dm_)
{
KSPCreate(comm, &ksp);
KSPSetInitialGuessNonzero(ksp, PETSC_TRUE); //!< initial guess is passed to KSPSolve
SetTolerancesInput(lin_input);
if(lin_input.ksp == LinearSolverData::PETSC_KSP_DEFAULT) {
/* nothing to do */
} else if(lin_input.ksp == LinearSolverData::FLEXIBLE_GMRES) {
KSPSetType(ksp, KSPFGMRES);
} else if(lin_input.ksp == LinearSolverData::STAB_BI_CG) {
KSPSetType(ksp, KSPBCGSL);
} else if(lin_input.ksp == LinearSolverData::IMPROVED_STAB_BI_CG) {
KSPSetType(ksp, KSPIBCGS);
} else {
print_error("*** Error: Detected unknown PETSc KSP type.\n");
exit_mpi();
}
if(lin_input.pc == LinearSolverData::PETSC_PC_DEFAULT) {
/* nothing to do*/
}
else {
PC pc;
KSPGetPC(ksp, &pc);
//PCFactorSetMatSolverType(pc, MATSOLVERMUMPS);
if(lin_input.pc == LinearSolverData::PC_NONE)
PCSetType(pc, PCNONE);
else if(lin_input.pc == LinearSolverData::JACOBI)
PCSetType(pc, PCJACOBI);
else if(lin_input.pc == LinearSolverData::BLOCK_JACOBI)
PCSetType(pc, PCBJACOBI);
else if(lin_input.pc == LinearSolverData::MG)
PCSetType(pc, PCMG);
else {
print_error("*** Error: Detected unknown PETSc KSP preconditioner type.\n");
exit_mpi();
}
}
if(strcmp(lin_input.options_file, "")) {
FILE *file = fopen(lin_input.options_file, "r");
if(file == NULL) {
print_error("*** Error: Cannot open PETSc options file %s.\n", lin_input.options_file);
exit_mpi();
}
fclose(file); // just make sure the file can be opened...
PetscOptionsInsert(NULL, NULL, NULL, lin_input.options_file);
}
KSPSetFromOptions(ksp); //overrides any options specified above
rnorm_history.resize(5000); //5000 entries should be more than enough
KSPSetResidualHistory(ksp, rnorm_history.data(), rnorm_history.size(), PETSC_TRUE); //reset for each Solve
// Set up log file
equation_name = equation_name_;
log_filename = lin_input.logfile;
if(!log_filename.empty()) {
FILE *file = fopen(log_filename.c_str(), "w");
if(file == NULL) {
print_error("*** Error: Unable to open file %s for printing the log of linear system solver.\n");
exit_mpi();
}
if(equation_name.empty())
print(file, "## Solution of linear systems - Log file.\n");
else
print(file, "## Solution of linear systems (%s) - Log file.\n", equation_name.c_str());
fclose(file);
MPI_Barrier(comm);
}
write_log_to_screen = lin_input.write_log_to_screen == LinearSolverData::YES;
}
//-----------------------------------------------------
LinearSystemSolver::~LinearSystemSolver()
{ }
//-----------------------------------------------------
void
LinearSystemSolver::Destroy()
{
Xtmp.Destroy();
Rtmp.Destroy();
KSPDestroy(&ksp);
LinearOperator::Destroy();
}
//-----------------------------------------------------
void
LinearSystemSolver::SetTolerancesInput(LinearSolverData &lin_input)
{
double relative_error = lin_input.rtol;
double absolute_error = lin_input.abstol;
double divergence_tol = lin_input.dtol;
int max_iterations = lin_input.maxits;
SetTolerances(relative_error, absolute_error, divergence_tol, max_iterations);
}
//-----------------------------------------------------
void
LinearSystemSolver::SetTolerances(double relative_error, double absolute_error, double divergence_tol,
int max_iterations)
{
KSPSetTolerances(ksp,
relative_error>0 ? relative_error : PETSC_DEFAULT,
absolute_error>0 ? absolute_error : PETSC_DEFAULT,
divergence_tol>0 ? divergence_tol : PETSC_DEFAULT,
max_iterations>0 ? max_iterations : PETSC_DEFAULT);
}
//-----------------------------------------------------
void
LinearSystemSolver::GetTolerances(double *rtol, double *abstol, double *dtol, int *maxits)
{
KSPGetTolerances(ksp, rtol, abstol, dtol, maxits);
}
//-----------------------------------------------------
//-----------------------------------------------------
void
LinearSystemSolver::ConvergedDefaultSetUMIRNorm()
{
KSPConvergedDefaultSetUMIRNorm(ksp);
}
//-----------------------------------------------------
void
LinearSystemSolver::GetSolverType(string *ksp_type, string *pc_type)
{
if(ksp_type) {
KSPType ksp_type_;
KSPGetType(ksp, &ksp_type_);
*ksp_type = (string)ksp_type_;
}
if(pc_type) {
PC pc;
KSPGetPC(ksp, &pc);
PCType pc_type_;
PCGetType(pc, &pc_type_);
*pc_type = (string)pc_type_;
}
}
//-----------------------------------------------------
void
LinearSystemSolver::SetLinearOperator(vector<RowEntries>& row_entries)
{
LinearOperator::SetLinearOperator(row_entries); //build A
KSPSetOperators(ksp, A, A);
}
//-----------------------------------------------------
void
LinearSystemSolver::ComputeResidual(SpaceVariable3D &b, SpaceVariable3D &x,
SpaceVariable3D &res)
{
ApplyLinearOperator(x, res); //res = Ax
res.AXPlusBY(-1.0, 1.0, b); //res = -1.0*res + b = b - Ax
}
//-----------------------------------------------------
void
LinearSystemSolver::UsePreviousPreconditioner(bool reuse_or_not)
{
KSPSetReusePreconditioner(ksp, reuse_or_not ? PETSC_TRUE : PETSC_FALSE);
}
//-----------------------------------------------------
bool
LinearSystemSolver::Solve(SpaceVariable3D &b, SpaceVariable3D &x,
LinearSolverConvergenceReason *reason, int *numIts, std::vector<double> *rnorm,
std::vector<int> *rnorm_its)
{
// --------------------------------------------------
// Sanity checks
int dof_ = b.NumDOF();
assert(dof_ == dof);
dof_ = x.NumDOF();
assert(dof_ == dof);
int i0_, j0_, k0_, imax_, jmax_, kmax_;
b.GetCornerIndices(&i0_, &j0_, &k0_, &imax_, &jmax_, &kmax_);
assert(i0_==i0 && j0_==j0 && k0_==k0 && imax_==imax && jmax_==jmax && kmax_==kmax);
x.GetCornerIndices(&i0_, &j0_, &k0_, &imax_, &jmax_, &kmax_);
assert(i0_==i0 && j0_==j0 && k0_==k0 && imax_==imax && jmax_==jmax && kmax_==kmax);
b.GetGhostedCornerIndices(&i0_, &j0_, &k0_, &imax_, &jmax_, &kmax_);
assert(i0_==ii0 && j0_==jj0 && k0_==kk0 && imax_==iimax && jmax_==jjmax && kmax_==kkmax);
x.GetGhostedCornerIndices(&i0_, &j0_, &k0_, &imax_, &jmax_, &kmax_);
assert(i0_==ii0 && j0_==jj0 && k0_==kk0 && imax_==iimax && jmax_==jjmax && kmax_==kkmax);
if(rnorm_its)
assert(rnorm); //if the user requested rnorm_its, they should also request rnorm
// ---------------------------------------------------
// Store initial guess (*may* be used later)
Xtmp.AXPlusBY(0.0, 1.0, x); //Xtmp = x
// ---------------------------------------------------
// Solve!
Vec &bb(b.GetRefToGlobalVec());
Vec &xx(x.GetRefToGlobalVec());
KSPSolve(ksp, bb, xx);
x.SyncLocalToGlobal(); //update the "localVec" of x to match xx
// ---------------------------------------------------
KSPConvergedReason ksp_code; //positive if convergence; negative if diverged
KSPGetConvergedReason(ksp, &ksp_code);
bool success = ksp_code>0;
if(reason) { //user requested convergence/divergence reason
if(ksp_code == KSP_CONVERGED_RTOL)
*reason = CONVERGED_REL_TOL;
else if(ksp_code == KSP_CONVERGED_ATOL)
*reason = CONVERGED_ABS_TOL;
else if((int)ksp_code>0)
*reason = CONVERGED_OTHER;
else if(ksp_code == KSP_DIVERGED_ITS)
*reason = DIVERGED_ITS;
else if(ksp_code == KSP_DIVERGED_DTOL)
*reason = DIVERGED_DTOL;
else
*reason = DIVERGED_OTHER;
}
int nIts = 0;
KSPGetIterationNumber(ksp, &nIts);
if(numIts) //user requested output of number of iterations
*numIts = nIts;
// log
if(rnorm || !log_filename.empty() || write_log_to_screen) {//need residual norm history
int nEntries(0);
KSPGetResidualHistory(ksp, NULL, &nEntries);
vector<int> indices;
if(nEntries==0) {
//some solvers in PETSc do not provide the residual time-history. calculate initial & final residuals
nEntries = 2;
indices.push_back(0);
ComputeResidual(b, Xtmp, Rtmp);
rnorm_history[0] = Rtmp.CalculateVectorTwoNorm();
indices.push_back(nIts-1);
ComputeResidual(b, x, Rtmp);
rnorm_history[1] = Rtmp.CalculateVectorTwoNorm();
} else {
for(int i=0; i<nEntries; i++)
indices.push_back(i);
}
if(rnorm) {
rnorm->resize(nEntries, 0);
for(int i=0; i<nEntries; i++) //copy data instead of passing rnorm_history (safer)
(*rnorm)[i] = rnorm_history[i];
if(rnorm_its) {
rnorm_its->resize(nEntries,0);
for(int i=0; i<nEntries; i++) //copy data instead of passing rnorm_history (safer)
(*rnorm_its)[i] = indices[i];
}
}
if(write_log_to_screen) {
if(success) {
if(equation_name.empty())
print(" o Linear solver converged (It. %d).\n", nIts);
else
print(" o Linear solver for %s converged (It. %d).\n", equation_name.c_str(), nIts);
} else {
if(equation_name.empty())
print_warning(" o Linear solver failed to converged (It. %d).\n", nIts);
else
print_warning(" o Linear solver for %s failed to converged (It. %d).\n",
equation_name.c_str(), nIts);
}
for(int i=0; i<nEntries; i++)
print(" > It. %d: residual = %e.\n", indices[i]+1, rnorm_history[i]);
}
if(!log_filename.empty()) {
FILE *file = fopen(log_filename.c_str(), "a");
if(!file) {
print_error("*** Error: Unable to open file %s for printing the log of linear system solver.\n");
exit_mpi();
}
if(success)
print(file, " o Linear solver converged (It. %d).\n", nIts);
else
print(file, " o Linear solver failed to converged (It. %d).\n", nIts);
for(int i=0; i<nEntries; i++)
print(file, " > It. %d: residual = %e.\n", indices[i]+1, rnorm_history[i]);
fclose(file);
MPI_Barrier(comm);
}
}
return success;
}
//-----------------------------------------------------
//-----------------------------------------------------