-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDVR_C.cpp
More file actions
412 lines (383 loc) · 13.3 KB
/
DVR_C.cpp
File metadata and controls
412 lines (383 loc) · 13.3 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include"DVR_C.h"
#include<iostream>
#include<fstream>
#include<Eigen/Dense>
#include<omp.h>
using namespace Eigen;
using namespace std;
/*
Construction function with initialization
*/
DVR_C::DVR_C(const int& NDim_, const VectorXi& NGrids_, const VectorXd& CoordStart_, const VectorXd& CoordEnd_, const VectorXd& mass_, MatrixXcd (* PotentialPointer_)(const VectorXd& Coord, const int& ND), const int& NStates_, const bool &saveMem_, const bool& readPESfromFile_)
: NDim(NDim_), NGrids(NGrids_), CoordStart(CoordStart_), saveMem(saveMem_),
CoordEnd(CoordEnd_), mass(mass_), PotentialPointer(PotentialPointer_), NStates(NStates_), readPESfromFile(readPESfromFile_)
{
dx.resize(NDim_);
for(int ii = 0; ii < NDim_; ii++)
{
dx(ii) = (CoordEnd_(ii) - CoordStart_(ii)) / (NGrids_(ii) - 1);
}
buildDVR();
}
DVR_C::~DVR_C()
{
}
/*
Function to return element of one-dimensional DVR kinetic energy matrix.
It is based on sinc functions.
*/
double DVR_C::oneDimK(const double& deltaX, const double& massX, const int& iii, const int& jjj)
{
double kinetics;
if(iii == jjj) kinetics = 1.0 / 2.0 / massX / pow(deltaX, 2) * (M_PI * M_PI / 3.0);
else kinetics = pow(-1, (iii - jjj)%2 ) / 2.0 / massX / pow(deltaX, 2) * 2.0 / pow(iii - jjj, 2);
return kinetics;
}
/*
Initialize and construct Hamiltonian matrix, which is based on (2.6) of the Ref.
Potential energy is obtained from PotentialPointer.
Kinetic energy is calculated based on sinc functions.
For formula details, see the Ref.
saveMem is a special bool. If it is true, only length will be calculated. None matrices
are initialized to save memory. And in kernel(), some special functions will be used in
Lanczos Algorithm.
*/
void DVR_C::buildDVR()
{
length = 1;
VectorXd Coord(NDim);
for(int ii = 0; ii < NDim; ii++)
{
length = length * NGrids(ii);
}
/* Calculate indices */
indice.resize(length, NDim);
#pragma omp parallel for
for(int ll = 0; ll < length; ll++)
{
int tmpll = ll;
for(int dd = 0; dd < NDim; dd++)
{
int tmp = 1;
for(int ii = NDim - 1; ii > dd; ii--)
{
tmp = tmp * NGrids(ii);
}
indice(ll, dd) = tmpll/tmp;
tmpll = tmpll - indice(ll, dd) * tmp;
}
}
if(saveMem)
{
cout << "saveMem mode is on." << endl;
/*
When saveMem mode is on. kineticT and potentialV will not be constructed explicitly.
Instead, they will be resized to a small matrix.
The potentialV stores the potential energies.
The kineticT stores the kinetic energies for different dimension using the delta iii - jjj.
*/
int size_nstates = NStates * (NStates + 1) / 2;
kineticT.resize(NDim, length * NStates);
potentialV.resize(size_nstates, length * NStates);
if(readPESfromFile)
{
if(NStates > 1)
{
cout << "When more than one states is included, reading PES from file is NOT supported." << endl;
exit(99);
}
ifstream ifs;
int tmp;
ifs.open("PESinput");
ifs >> tmp;
if(tmp != length)
{
cout << "data in PESinput does not match the Hamiltonian size";
ifs.close();
exit(99);
}
else
{
for(int ii = 0; ii < length; ii++)
{
ifs >> potentialV(0, ii);
}
}
ifs.close();
}
for(int ii = 0; ii < length; ii++)
{
for(int dd = 0; dd < NDim; dd++)
{
Coord(dd) = CoordStart(dd) + indice(ii, dd) * dx(dd);
kineticT(dd, ii) = oneDimK(dx(dd), mass(dd), 0, ii);
}
if(!readPESfromFile)
{
for(int mm = 0; mm < NStates; mm++)
for(int nn = 0; nn <= mm; nn++)
{
potentialV(mm * (mm+1) / 2 + nn, ii) = PotentialPointer(Coord, NDim)(mm,nn);
}
}
}
}
else
{
kineticT.resize(length * NStates, length * NStates);
potentialV.resize(length * NStates, length * NStates);
kineticT = MatrixXd::Zero(length * NStates, length * NStates);
potentialV = MatrixXcd::Zero(length * NStates, length * NStates);
/* Calculate potentialE and kineticE matrices, add them together */
if(readPESfromFile)
{
if(NStates > 1)
{
cout << "When more than one states is included, reading PES from file is NOT supported." << endl;
exit(99);
}
ifstream ifs;
int tmp;
ifs.open("PESinput");
ifs >> tmp;
if(tmp != length)
{
cout << "data in PESinput does not match the Hamiltonian size";
ifs.close();
exit(99);
}
else
{
for(int ii = 0; ii < length; ii++)
{
ifs >> potentialV(ii, ii);
}
}
ifs.close();
}
for(int ii = 0; ii < length; ii++)
{
if(!readPESfromFile)
{
for(int dd = 0; dd < NDim; dd++)
{
Coord(dd) = CoordStart(dd) + indice(ii, dd) * dx(dd);
}
MatrixXcd pesTMP = PotentialPointer(Coord, NDim);
/* This is correct only for diabatic Hamiltonian */
for(int ss1 = 0; ss1 < NStates; ss1++)
for(int ss2 = 0; ss2 < NStates; ss2++)
{
potentialV(ss1*length + ii, ss2*length + ii) = pesTMP(ss1, ss2);
}
// std::cout << Coord(0) << "\t" << potentialE(ii, ii) << std::endl;
}
for(int jj = 0; jj < length; jj++)
{
double tmp = 0.0;
for(int dd = 0; dd < NDim; dd++)
{
bool Ktmp = true;
for(int ddtmp = 0; ddtmp < NDim; ddtmp++)
{
/* These "if"s are the delta symbols in equ.(2.6) */
if(ddtmp != dd)
{
if(indice(ii,ddtmp) != indice(jj,ddtmp)) Ktmp = false;
}
}
if(Ktmp) tmp = tmp + oneDimK(dx(dd), mass(dd), indice(ii, dd), indice(jj, dd));
}
for(int ss = 0; ss < NStates; ss++)
{
kineticT(ss*length + ii, ss*length + jj) = tmp;
}
// Hamiltonian(ii, jj) += tmp;
}
}
}
cout << "DVR object has been initialized." << endl << endl;
}
/*
Function to calculate eigenvalues and eigenvectors of Hamiltonian
solverSize is how many lowest energies calculated.
If it is 0, solve all eigenvalues and eigenvectors using Eigen::SelfAdjointEigenSolver.
If it is k != 0, solve lowest k eigenvalues and eigenvectors.
In this case, when saveMem is used, perform a reorthogonalized Lanczos Algorithm.
Otherwise, use intel mkl interface to perform LAPACKE_dsyevr.
lanzcos is a bool to determine whether to use (reorthogonalized) Lanczos Algorithm
*/
void DVR_C::kernel(VectorXcd& energies, MatrixXcd& states)
{
if(saveMem)
{
cout << endl << "When saveMem is on, one can only use kernel() with solverSize." << endl;
}
SelfAdjointEigenSolver<MatrixXcd> KernelSolver(kineticT + potentialV);
eigenValues = KernelSolver.eigenvalues();
eigenStates = KernelSolver.eigenvectors();
energies = eigenValues;
states = eigenStates;
}
void DVR_C::kernel(VectorXcd& energies, MatrixXcd& states, const int& solverSize, const bool& lanczos)
{
if(solverSize <= 0)
{
cout << "Input a wrong solverSize. It must be positive integer from 1 to length." << endl;
exit(99);
}
else
{
cout << solverSize << " eigenvalues and eigenvectors will be evaluated." << endl;
if(!lanczos) cout << endl << "saveMem mode is on. Automatically switch to Lanczos Algorithm." << solverSize << endl;
cout << "The reorthogonalized Lanczos Algorithm is used." << endl << endl;
VectorXcd q(length * NStates), r(length * NStates), v(length * NStates), alpha(solverSize), beta(solverSize);
MatrixXcd T(solverSize, solverSize), Q(length * NStates, solverSize);
/*
Using Harmonic Oscillator states (Gaussian function times polynomial)
as the initial Lanczos vector q. In this way, we want to the subspace includes
the first several excited states.
In saveMem mode, the Hamiltonian and indice is not initialized.
It will use H_times_V and oneD2mD instead.
*/
q = VectorXcd::Random(length * NStates);
q = q/q.norm();
Q = 0.0 * Q;
T = 0.0 * T;
#pragma omp parallel for
for(int kk = 0; kk < length * NStates; kk++) Q(kk, 0) = q(kk);
H_times_V(q, r);
alpha(0) = q.adjoint() * r;
r = r -alpha(0) * q;
beta(0) = r.norm();
T(0,0) = alpha(0);
T(0,1) = beta(0);
T(1,0) = beta(0);
for(int jj = 1; jj < solverSize; jj++)
{
v = q;
q = r/beta[jj - 1];
#pragma omp parallel for
for(int kk = 0; kk < length * NStates; kk++) Q(kk, jj) = q(kk);
H_times_V(q, r);
r = r - beta(jj - 1) * v;
alpha(jj) = q.adjoint() * r;
r = r - alpha(jj) * q;
r = r - Q.block(0, 0, length * NStates, jj)*(Q.block(0, 0, length * NStates, jj).adjoint()*r);
beta(jj) = r.norm();
cout << "beta_" << jj + 1 << " = " << beta(jj) << endl;
T(jj, jj) = alpha(jj);
if(jj != solverSize - 1)
{
T(jj, jj + 1) = beta(jj);
T(jj + 1, jj) = beta(jj);
}
}
SelfAdjointEigenSolver<MatrixXcd> KernelSolver(T);
eigenValues = KernelSolver.eigenvalues();
eigenStates = Q * KernelSolver.eigenvectors();
energies = eigenValues;
states = eigenStates;
}
solved = true;
}
/*
Special evaluation functions used in memory saving purpose.
oneD2mD(lll) equivalently returns lll th row in indice.
H_times_V(V) returns product of Hamiltonian and V.
*/
inline VectorXi DVR_C::oneD2mD(const int& lll)const
{
VectorXi indiceMD(NDim);
int tmpll = lll;
for(int dd = 0; dd < NDim; dd++)
{
int tmp = 1;
for(int ii = NDim - 1; ii > dd; ii--)
{
tmp = tmp * NGrids(ii);
}
indiceMD(dd) = tmpll/tmp;
tmpll = tmpll - indiceMD(dd) * tmp;
}
return indiceMD;
}
inline int DVR_C::mD2oneD(const VectorXi& indicesMD)const
{
int tmp = 1, returnValue = 0;
for(int ii = NDim - 1; ii >= 0; ii--)
{
returnValue += tmp * indicesMD(ii);
tmp = tmp * NGrids(ii);
}
return returnValue;
}
void DVR_C::H_times_V(const VectorXcd& V, VectorXcd& result)const
{
if(V.rows() != length * NStates)
{
std::cout << "The Size of V is inconsistent with H in H_times_V." << std::endl;
exit(99);
}
else
{
if(!saveMem)
{
result = (kineticT + potentialV) * V;
}
else
{
result.resize(length * NStates);
result = VectorXd::Zero(length * NStates);
for(int ss = 0; ss < NStates; ss++)
{
#pragma omp parallel for
for(int ii = 0; ii < length; ii++)
{
VectorXi indicesMD = indice.block(ii,0,1,NDim).transpose();
for(int rr = 0; rr < NStates; rr++)
{
if(ss >= rr) result(ii + ss*length) += potentialV(ss * (ss+1) / 2 + rr, ii) * V(ii + rr*length);
else result(ii + ss*length) += conj(potentialV(rr * (rr+1) / 2 + ss, ii)) * V(ii + rr*length);
}
for(int dd = 0; dd < NDim; dd++)
{
complex<double> tmp = 0.0;
VectorXi indicesMDtmp = indicesMD;
for(int jj = 0; jj < NGrids(dd); jj++)
{
indicesMDtmp(dd) = jj;
tmp += kineticT(dd, abs(jj - indicesMD(dd))) * V(mD2oneD(indicesMDtmp) + ss*length);
}
result(ii + ss*length) += tmp;
}
}
}
}
}
}
/*
Public functions to get corresponding private matrices
*/
MatrixXcd DVR_C::getHamiltonian()
{
return kineticT + potentialV;
}
MatrixXcd DVR_C::getEigenStates()
{
if(solved) return eigenStates;
else
{
std::cout << "getEigenStates() must be called after kernel()" << std::endl;
exit(99);
}
}
VectorXcd DVR_C::getEigenValues()
{
if(solved) return eigenValues;
else
{
std::cout << "getEigenValues() must be called after kernel()" << std::endl;
exit(99);
}
}