-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve_gep.cpp
144 lines (118 loc) · 9.08 KB
/
solve_gep.cpp
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
#include <slepceps.h>
#include <array>
#include <string>
void early_exit(int status, const char msg[]) {
PetscPrintf(PETSC_COMM_WORLD, msg);
SlepcFinalize();
exit(status);
}
std::array<std::string, 4> file_names(std::string prefix) {
return {
"./tmp/" + prefix + "_a.dat",
"./tmp/" + prefix + "_b.dat",
"./tmp/" + prefix + "_evec.dat",
"./tmp/" + prefix + "_eval.dat",
};
}
int main(int argc, char* argv[]) {
PetscErrorCode ierr;
Mat A, B;
Vec xr, xi;
PetscViewer viewer;
EPS eps;
ST st;
KSP ksp;
PC pc;
PetscInt nconv;
PetscBool flg;
char file_prefix[PETSC_MAX_PATH_LEN];
double eigenvalue_r, eigenvalue_i;
double target_eval = 1.0;
// ------------------------------------------------------------------------------------------------------------------
// Basic Setup and Arguments
// ------------------------------------------------------------------------------------------------------------------
int num_threads;
ierr = SlepcInitialize(&argc, &argv, (char *) 0, NULL);
if (ierr) early_exit(1, "Failed to initialize Slepc!");
MPI_Comm_size(PETSC_COMM_WORLD, &num_threads);
if (num_threads != 1) early_exit(2, "GEP Solver is only setup to use 1 MPI Thread!");
PetscOptionsGetScalar(NULL, NULL, "-te", &target_eval, &flg);
if (!flg) early_exit(2, "Must indicate Target Eigenvalue with the -te option!");
PetscOptionsGetString(NULL, NULL, "-fp", file_prefix, PETSC_MAX_PATH_LEN-1, &flg);
if (!flg) early_exit(2, "Must indicate file prefix -fp option!");
std::array<std::string, 4> filenames = file_names(std::string(file_prefix));
// ------------------------------------------------------------------------------------------------------------------
// Load Matrices From Files
// ------------------------------------------------------------------------------------------------------------------
ierr = MatCreate(PETSC_COMM_WORLD, &A); if (ierr) early_exit(3, "Unable to Create A Matrix");
ierr = MatSetType(A, MATSEQAIJ); if (ierr) early_exit(3, "Unable to Create A Matrix");
ierr = MatCreate(PETSC_COMM_WORLD, &B); if (ierr) early_exit(3, "Unable to Create B Matrix");
ierr = MatSetType(B, MATSEQAIJ); if (ierr) early_exit(3, "Unable to Create B Matrix");
ierr = PetscViewerCreate(PETSC_COMM_WORLD, &viewer); if (ierr) early_exit(3, "Unable to Create A Matrix");
ierr = PetscViewerSetType(viewer, "binary"); if (ierr) early_exit(3, "Unable to Create A Matrix");
ierr = PetscViewerFileSetMode(viewer, FILE_MODE_READ); if (ierr) early_exit(3, "Unable to Create A Matrix");
ierr = PetscViewerFileSetName(viewer, &filenames[0][0]); if (ierr) early_exit(3, "Unable to Create A Matrix");
ierr = MatLoad(A, viewer); if (ierr) early_exit(3, "Unable to Create A Matrix");
ierr = PetscViewerDestroy(&viewer); if (ierr) early_exit(3, "Unable to Create A Matrix");
ierr = PetscViewerCreate(PETSC_COMM_WORLD, &viewer); if (ierr) early_exit(3, "Unable to Create B Matrix");
ierr = PetscViewerSetType(viewer, "binary"); if (ierr) early_exit(3, "Unable to Create B Matrix");
ierr = PetscViewerFileSetMode(viewer, FILE_MODE_READ); if (ierr) early_exit(3, "Unable to Create B Matrix");
ierr = PetscViewerFileSetName(viewer, &filenames[1][0]); if (ierr) early_exit(3, "Unable to Create B Matrix");
ierr = MatLoad(B, viewer); if (ierr) early_exit(3, "Unable to Create B Matrix");
ierr = PetscViewerDestroy(&viewer); if (ierr) early_exit(3, "Unable to Create B Matrix");
ierr = MatCreateVecs(A, NULL, &xr); if (ierr) early_exit(3, "Unable to Create Eigenvector");
ierr = MatCreateVecs(A, NULL, &xi); if (ierr) early_exit(3, "Unable to Create Eigenvector");
// ------------------------------------------------------------------------------------------------------------------
// Setup Eigenvalue Problem
// ------------------------------------------------------------------------------------------------------------------
ierr = EPSCreate(PETSC_COMM_WORLD, &eps); if (ierr) early_exit(4, "Unable to Setup Eigenproblem");
ierr = EPSSetOperators(eps, A, B); if (ierr) early_exit(4, "Unable to Setup Eigenproblem");
ierr = EPSSetProblemType(eps, EPS_GHEP); if (ierr) early_exit(4, "Unable to Setup Eigenproblem");
ierr = EPSSetFromOptions(eps); if (ierr) early_exit(4, "Unable to Setup Eigenproblem");
ierr = EPSSetTolerances(eps, 1.0e-15, 100); if (ierr) early_exit(4, "Unable to Setup Eigenproblem");
ierr = EPSSetType(eps, "krylovschur"); if (ierr) early_exit(4, "Unable to Setup Eigenproblem");
ierr = EPSSetWhichEigenpairs(eps, EPS_TARGET_MAGNITUDE); if (ierr) early_exit(4, "Unable to Setup Eigenproblem");
ierr = EPSSetTarget(eps, target_eval); if (ierr) early_exit(5, "Unable to Setup Target");
ierr = EPSGetST(eps,&st); if (ierr) early_exit(5, "Unable to Setup Target");
ierr = STSetType(st,"sinvert"); if (ierr) early_exit(5, "Unable to Setup Target");
ierr = STSetShift(st, target_eval); if (ierr) early_exit(5, "Unable to Setup Target");
ierr = STGetKSP(st, &ksp); if (ierr) early_exit(6, "Unable to setup KSP");
ierr = KSPSetType(ksp, KSPPREONLY); if (ierr) early_exit(6, "Unable to setup KSP");
ierr = KSPGetPC(ksp, &pc); if (ierr) early_exit(6, "Unable to setup KSP");
ierr = PCSetType(pc, PCCHOLESKY); if (ierr) early_exit(6, "Unable to setup KSP");
ierr = EPSSetTrueResidual(eps, PETSC_TRUE); if (ierr) early_exit(6, "Unable to set convergence condition");
ierr = EPSSetConvergenceTest(eps, EPS_CONV_NORM); if (ierr) early_exit(6, "Unable to set convergence condition");
ierr = EPSSolve(eps); if (ierr) early_exit(7, "Unable to Solve Eigenproblem");
EPSGetConverged(eps, &nconv);
if (nconv > 0) {
ierr = EPSGetEigenpair(eps, 0, &eigenvalue_r, &eigenvalue_i, xr, xi); if (ierr) early_exit(7, "Unable to get Solution");
// PetscPrintf(PETSC_COMM_WORLD, "eigenvalue: %f", eigenvalue_r);
} else {
early_exit(7, "GEP Solver Failed to converge on target Eigenvalue!");
}
EPSDestroy(&eps);
// ------------------------------------------------------------------------------------------------------------------
// Return Solution
// ------------------------------------------------------------------------------------------------------------------
ierr = PetscViewerCreate(PETSC_COMM_WORLD, &viewer); if (ierr) early_exit(8, "Unable to write Eigenvector");
ierr = PetscViewerSetType(viewer, "binary"); if (ierr) early_exit(8, "Unable to write Eigenvector");
ierr = PetscViewerFileSetMode(viewer, FILE_MODE_WRITE); if (ierr) early_exit(8, "Unable to write Eigenvector");
ierr = PetscViewerFileSetName(viewer, &filenames[2][0]); if (ierr) early_exit(8, "Unable to write Eigenvector");
ierr = VecView(xr, viewer); if (ierr) early_exit(8, "Unable to write Eigenvector");
ierr = PetscViewerDestroy(&viewer); if (ierr) early_exit(8, "Unable to write Eigenvector");
ierr = PetscViewerCreate(PETSC_COMM_WORLD, &viewer); if (ierr) early_exit(8, "Unable to write Eigenvalue");
ierr = PetscViewerSetType(viewer, "binary"); if (ierr) early_exit(8, "Unable to write Eigenvalue");
ierr = PetscViewerFileSetMode(viewer, FILE_MODE_WRITE); if (ierr) early_exit(8, "Unable to write Eigenvalue");
ierr = PetscViewerFileSetName(viewer, &filenames[3][0]); if (ierr) early_exit(8, "Unable to write Eigenvalue");
ierr = PetscScalarView(1, &eigenvalue_r, viewer); if (ierr) early_exit(8, "Unable to write Eigenvalue");
ierr = PetscViewerDestroy(&viewer); if (ierr) early_exit(8, "Unable to write Eigenvalue");
// ------------------------------------------------------------------------------------------------------------------
// Cleanup
// ------------------------------------------------------------------------------------------------------------------
MatDestroy(&A);
MatDestroy(&B);
VecDestroy(&xr);
VecDestroy(&xi);
SlepcFinalize();
return 0;
}