-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArpackSolver.hpp
136 lines (110 loc) · 2.88 KB
/
ArpackSolver.hpp
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
#pragma once
#include "arpackdef.h"
#include <vector>
#include <Eigen/Dense>
#include <random>
extern "C"
{
void dsaupd_(a_int *, char *, a_int *, char *, a_int *, double *,
double *, a_int *, double *, a_int *, a_int *, a_int *,
double *, double *, a_int *, a_int *);
void dseupd_(a_int* rvec, char* howmny, a_int* select, double* d, double* z,
a_int* ldz, double* sigma, char* bmat, a_int* n, char* which,
a_int* nev, double* tol, double* resid, a_int* ncv, double* v,
a_int* ldv, a_int* iparam, a_int* inptr, double* workd, double* workl,
a_int* lworkl, a_int* info);
}
enum class ErrorType
{
NormalExit, NotConverged, IncorrectParams, Others
};
template<typename MatrixVectorOp>
class ArpackSolver
{
private:
MatrixVectorOp& op_;
const a_int dim_;
std::vector<double> d_;
std::vector<double> z_;
public:
ArpackSolver(MatrixVectorOp& op, a_int dim)
: op_{op}, dim_{dim}
{
}
ErrorType solve(a_int nev)
{
a_int dim = dim_;
if(dim <= 0 || nev <= 0)
{
return ErrorType::IncorrectParams;
}
a_int ido = 0;
char bmat[] = "I";
a_int n = dim;
char which[] = "SA";
double tol = 1e-10;
std::vector<double> resid(dim);
a_int ncv = 3*nev;
std::vector<double> v(ncv*dim);
a_int ldv = dim;
a_int iparam[11] = {
1, //ishift
0, //levec (not used)
1000000000, //maxiter
1, //nb
0, //nconv
0, //iupd (not used)
1, //mode (1 is usual eigenvalue problem)
0, //np
0, 0, 0 //only for output
};
a_int ipntr[14];
std::vector<double> workd(3*dim, 0.);
int lworkl = 3*ncv*ncv + 6*ncv;
std::vector<double> workl(lworkl, 0);
a_int info = 0;
//first call
dsaupd_(&ido, bmat, &n, which, &nev, &tol, resid.data(), &ncv,
v.data(), &ldv, iparam, ipntr, workd.data(), workl.data(),
&lworkl, &info);
while(ido == -1 || ido == 1)
{
op_.perform_op(workd.data() + ipntr[0]-1, workd.data() + ipntr[1]-1);
dsaupd_(&ido, bmat, &n, which, &nev, &tol, resid.data(), &ncv,
v.data(), &ldv, iparam, ipntr, workd.data(), workl.data(),
&lworkl, &info);
}
if(info == 1 || iparam[4] != nev)
{
return ErrorType::NotConverged;
}
else if(info != 0)
{
return ErrorType::IncorrectParams;
}
a_int rvec = 1;
d_.resize(nev);
z_.resize((dim)*(nev));
a_int ldz = dim;
double sigma = 0;
a_int select[ncv];
for(int i = 0; i < ncv; i++)
{
select[i] = 1;
}
char howmny[] = "All";
dseupd_(&rvec, howmny, select, d_.data(), z_.data(),
&ldz, &sigma, bmat, &n, which,
&nev, &tol, resid.data(), &ncv, v.data(),
&ldv, iparam, ipntr, workd.data(), workl.data(), &lworkl, &info);
return ErrorType::NormalExit;
}
Eigen::Map<const Eigen::VectorXd> eigenvalues() const
{
return Eigen::Map<const Eigen::VectorXd>(d_.data(), d_.size());
}
Eigen::Map<const Eigen::MatrixXd> eigenvectors() const
{
return Eigen::Map<const Eigen::MatrixXd>(z_.data(), dim_, d_.size());
}
};