-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLinearOperator.cpp
More file actions
230 lines (178 loc) · 5.71 KB
/
Copy pathLinearOperator.cpp
File metadata and controls
230 lines (178 loc) · 5.71 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
/************************************************************************
* Copyright © 2020 The Multiphysics Modeling and Computation (M2C) Lab
* <kevin.wgy@gmail.com> <kevinw3@vt.edu>
************************************************************************/
#include<LinearOperator.h>
#include<petscksp.h> //for computing singular values & condition number
#include<cassert>
#include<cfloat>
//-----------------------------------------------------
LinearOperator::LinearOperator(MPI_Comm &comm_, DM &dm_)
: comm(comm_)
{
DMClone(dm_, &dm);
DMSetMatType(dm, MATAIJ);
DMSetMatrixPreallocateOnly(dm, PETSC_TRUE);
DMCreateMatrix(dm, &A);
// -------------------------------------------------------
// Get info about the domain decomposition
DMDAGetInfo(dm, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &dof, NULL, NULL, NULL, NULL, NULL);
int nx(0), ny(0), nz(0);
DMDAGetCorners(dm, &i0, &j0, &k0, &nx, &ny, &nz);
imax = i0 + nx;
jmax = j0 + ny;
kmax = k0 + nz;
int ghost_nx(0), ghost_ny(0), ghost_nz(0);
DMDAGetGhostCorners(dm, &ii0, &jj0, &kk0, &ghost_nx, &ghost_ny, &ghost_nz);
iimax = ii0 + ghost_nx;
jjmax = jj0 + ghost_ny;
kkmax = kk0 + ghost_nz;
// -------------------------------------------------------
}
//-----------------------------------------------------
LinearOperator::~LinearOperator()
{ }
//-----------------------------------------------------
void
LinearOperator::Destroy()
{
MatDestroy(&A);
DMDestroy(&dm);
}
//-----------------------------------------------------
void
LinearOperator::SetLinearOperator(vector<RowEntries>& row_entries)
{
//MatZeroEntries(A); //!< zeros all entries but retains the previous nonzero structure (KW: NOT WORKING?)
MatDestroy(&A);
DMCreateMatrix(dm, &A);
for(auto&& entries : row_entries)
MatSetValuesStencil(A, 1, &entries.row, entries.cols.size(), entries.cols.data(),
entries.vals.data(), ADD_VALUES);
MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
}
//-----------------------------------------------------
void
LinearOperator::ApplyLinearOperator(SpaceVariable3D &x, SpaceVariable3D &y)
{
Vec &xx(x.GetRefToGlobalVec());
Vec &yy(y.GetRefToGlobalVec());
assert(&xx != &yy); //cannot be the same vector
MatMult(A, xx, yy);
y.SyncLocalToGlobal(); //update the localVec of y
}
//-----------------------------------------------------
void
LinearOperator::ApplyLinearOperatorAndAdd(SpaceVariable3D &x, SpaceVariable3D &b,
SpaceVariable3D &y)
{
Vec &xx(x.GetRefToGlobalVec());
Vec &bb(b.GetRefToGlobalVec());
Vec &yy(y.GetRefToGlobalVec());
assert(&xx != &yy); //cannot be the same vector
MatMultAdd(A, xx, bb, yy);
y.SyncLocalToGlobal(); //update the localVec of y
}
//-----------------------------------------------------
void
LinearOperator::CalculateExtremeSingularValues(double &lambda_max, double &lambda_min)
{
KSP ksp_tmp; //create a temporary Krylov-subspace based solver
KSPCreate(comm, &ksp_tmp);
KSPSetComputeSingularValues(ksp_tmp, PETSC_TRUE);
PC pc;
KSPGetPC(ksp_tmp, &pc);
PCSetType(pc, PCNONE);
KSPGMRESSetRestart(ksp_tmp, 1000000); //disable restart
KSPSetOperators(ksp_tmp, A, A);
KSPSetTolerances(ksp_tmp, 1.0e-8, 1.0e-8, PETSC_DEFAULT, 3000);
KSPSetUp(ksp_tmp);
//singular values are estimated while solving a linear system...
Vec x,b;
DMCreateGlobalVector(dm, &x);
DMCreateGlobalVector(dm, &b);
VecSet(x, 1.0);
VecSet(b, 2.0);
KSPSolve(ksp_tmp, b, x);
KSPComputeExtremeSingularValues(ksp_tmp, &lambda_max, &lambda_min); //min can be quite inaccurate
//cleanup
VecDestroy(&x);
VecDestroy(&b);
KSPDestroy(&ksp_tmp);
}
//-----------------------------------------------------
double
LinearOperator::EstimateConditionNumber()
{
double lambda_max(1.0), lambda_min(1.0);
CalculateExtremeSingularValues(lambda_max, lambda_min);
if(lambda_min == 0.0)
return DBL_MAX;
return lambda_max/lambda_min;
}
//-----------------------------------------------------
double
LinearOperator::CalculateMatrixOneNorm()
{
double norm(0.0);
MatNorm(A, NORM_1, &norm);
return norm;
}
//-----------------------------------------------------
double
LinearOperator::CalculateMatrixTwoNorm()
{
double lambda_max(0.0), lambda_min(0.0);
CalculateExtremeSingularValues(lambda_max, lambda_min);
return lambda_max; //matrix 2-norm is just max singular value
}
//-----------------------------------------------------
double
LinearOperator::CalculateMatrixInfNorm()
{
double norm(0.0);
MatNorm(A, NORM_INFINITY, &norm);
return norm;
}
//-----------------------------------------------------
double
LinearOperator::CalculateMatrixFrobeniusNorm()
{
double norm(0.0);
MatNorm(A, NORM_FROBENIUS, &norm);
return norm;
}
//-----------------------------------------------------
bool
LinearOperator::IsSymmetric(double tol)
{
assert(tol>=0.0);
PetscBool flg(PETSC_FALSE);
MatIsSymmetric(A, tol, &flg);
return flg==PETSC_TRUE;
}
//-----------------------------------------------------
void
LinearOperator::SetOutputVariableName(const char *name)
{
PetscObjectSetName((PetscObject)A, name);
}
//-----------------------------------------------------
void
LinearOperator::WriteToMatlabFile(const char *filename, const char *varname)
{
if(varname)
SetOutputVariableName(varname);
PetscViewer viewer;
int code = PetscViewerASCIIOpen(comm, filename, &viewer);
if(code) {
print_error("*** Error: Cannot open file '%s' for output. (code: %d)\n", filename, code);
exit_mpi();
}
PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
MatView(A, viewer);
PetscViewerDestroy(&viewer);
MPI_Barrier(comm);
}
//-----------------------------------------------------