forked from sanshar/Dice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Davidson.cpp
686 lines (589 loc) · 20.4 KB
/
Davidson.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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*
Developed by Sandeep Sharma with contributions from James E. T. Smith and Adam A. Holmes, 2017
Copyright (c) 2017, Sandeep Sharma
This file is part of DICE.
This program is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see <http://www.gnu.org/licenses/>.
*/
#include "Davidson.h"
#include "Hmult.h"
#include <Eigen/Dense>
#include <iostream>
#include <iostream>
#include "boost/format.hpp"
#include "communicate.h"
#include "iowrapper.h"
#include "global.h"
using namespace Eigen;
using namespace std;
using namespace boost;
std::complex<double> sumComplex(const std::complex<double>& a, const std::complex<double>& b) { return a+b; };
//=============================================================================
void AllocateSHM(vector<MatrixXx>& x0, CItype* &bcol, CItype* &sigmacol){
//-----------------------------------------------------------------------------
/*!
Segment in shared memory
:Inputs:
vector<MatrixXx>& x0:
BM_description
CItype* &bcol:
BM_description
CItype* &sigmacol:
BM_description
*/
//-----------------------------------------------------------------------------
size_t totalMemory = 0, xrows=0;
int comm_rank=0, comm_size=1;
#ifndef SERIAL
MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
#endif
if (comm_rank == 0) {
totalMemory = x0[0].rows()*2*(sizeof(CItype));
xrows = x0[0].rows();
}
#ifndef SERIAL
MPI_Bcast(&totalMemory, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
MPI_Bcast(&xrows, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
#endif
DavidsonSegment.truncate(totalMemory);
regionDavidson = boost::interprocess::mapped_region{DavidsonSegment, boost::interprocess::read_write};
memset(regionDavidson.get_address(), 0., totalMemory);
#ifndef SERIAL
MPI_Barrier(MPI_COMM_WORLD);
#endif
bcol = static_cast<CItype*>(regionDavidson.get_address());
sigmacol = bcol + xrows;
boost::interprocess::shared_memory_object::remove(shciDetsCI.c_str());
boost::interprocess::shared_memory_object::remove(shciDavidson.c_str());
} // end AllocateSHM
//=============================================================================
void precondition(MatrixXx& r, MatrixXx& diag, double& e) {
//-----------------------------------------------------------------------------
/*!
Properly precondition the matrix "r"
:Inputs:
MatrixXx& r:
Input/Ouput matrix to be preconditionned (output)
MatrixXx& diag:
Diagonal vector
double& e:
Threshold and shift
*/
//-----------------------------------------------------------------------------
for (int i=0; i<r.rows(); i++) {
if (abs(e-diag(i,0)) > 1e-12)
r(i,0) = r(i,0)/(e-diag(i,0));
else
r(i,0) = r(i,0)/(e-diag(i,0)-1.e-12);
}
} // end precondition
//=============================================================================
vector<double> davidson(Hmult2& H, vector<MatrixXx>& x0, MatrixXx& diag, int maxCopies, double tol, int& numIter, bool print) {
//-----------------------------------------------------------------------------
/*!
BM_description
:Inputs:
Hmult2& H:
BM_description
vector<MatrixXx>& x0:
BM_description
MatrixXx& diag:
BM_description
int maxCopies:
BM_description
double tol:
BM_description
int& numIter:
BM_description
bool print:
BM_description
:Returns:
std::vector<double> eroots:
BM_description
*/
//-----------------------------------------------------------------------------
std::vector<double> eroots;
CItype* bcol, *sigmacol;
AllocateSHM(x0, bcol, sigmacol);
int nroots = x0.size();
MatrixXx b;
if (commrank == 0)
b=MatrixXx::Zero(x0[0].rows(), maxCopies);
int brows = x0[0].rows();
#ifndef SERIAL
MPI_Bcast(&brows, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
#endif
int niter;
//if some vector has zero norm then randomise it
if (commrank == 0) {
for (int i=0; i<nroots; i++) {
b.col(i) = 1.*x0[i];
if (x0[i].norm() < 1.e-10) {
b.col(i).setRandom();
b.col(i) = b.col(i)/b.col(i).norm();
}
}
//make vectors orthogonal to each other
for (int i=0; i<x0.size(); i++) {
for (int j=0; j<i; j++) {
CItype overlap = (b.col(j).adjoint()*b.col(i))(0,0);
b.col(i) -= overlap*b.col(j);
}
if (b.col(i).norm() <1e-8) {
b.col(i).setRandom();
}
for (int j=0; j<i; j++) {
CItype overlap = (b.col(j).adjoint()*b.col(i))(0,0);
b.col(i) -= overlap*b.col(j);
}
b.col(i) = b.col(i)/b.col(i).norm();
} // i
} // commrank=0
MatrixXx sigma;
if (commrank == 0) sigma = MatrixXx::Zero(x0[0].rows(), maxCopies);
int sigmaSize=0, bsize = x0.size();
MatrixXx r;
if (commrank == 0) {r=MatrixXx::Zero(x0[0].rows(),1);}
int convergedRoot = 0;
//int iter = 0;
numIter = 0;
double ei = 0.0;
while(true) {
//0->continue with the loop, 1 -> continue clause, 2 -> return
int continueOrReturn = 0;
#ifndef SERIAL
MPI_Bcast(&bsize, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&sigmaSize, 1, MPI_INT, 0, MPI_COMM_WORLD);
#endif
for (int i=sigmaSize; i<bsize; i++) {
if (commrank==0) {
for (int k=0; k<brows; k++) bcol[k] = b(k,i);
}
for (int k=0; k<brows; k++) sigmacol[k] = 0.0;
//by default the MatrixXx is column major,
//so all elements of bcol are contiguous
#ifndef SERIAL
MPI_Barrier(MPI_COMM_WORLD);
#ifndef Complex
MPI_Bcast(bcol, brows, MPI_DOUBLE, 0, MPI_COMM_WORLD);
#else
MPI_Bcast(bcol, 2*brows, MPI_DOUBLE, 0, MPI_COMM_WORLD);
#endif
MPI_Barrier(MPI_COMM_WORLD);
#endif
H(bcol, sigmacol);
sigmaSize++;
#ifndef SERIAL
MPI_Barrier(MPI_COMM_WORLD);
if (localrank == 0) {
#ifndef Complex
if (commrank == 0)
MPI_Reduce(MPI_IN_PLACE, sigmacol, brows, MPI_DOUBLE, MPI_SUM, 0, shmcomm);
else
MPI_Reduce(sigmacol, sigmacol, brows, MPI_DOUBLE, MPI_SUM, 0, shmcomm);
#else
if (commrank == 0)
MPI_Reduce(MPI_IN_PLACE, sigmacol, 2*brows, MPI_DOUBLE, MPI_SUM, 0, shmcomm);
else
MPI_Reduce(sigmacol, sigmacol, 2*brows, MPI_DOUBLE, MPI_SUM, 0, shmcomm);
#endif
} // localrank=0
MPI_Barrier(MPI_COMM_WORLD);
#endif
if (commrank==0) {
for (int k=0; k<brows; k++) sigma(k,i) = sigmacol[k];
}
#ifndef SERIAL
MPI_Barrier(MPI_COMM_WORLD);
#endif
} // i
if (commrank == 0) {
MatrixXx hsubspace(bsize, bsize);hsubspace.setZero(bsize, bsize);
for (int i=0; i<bsize; i++)
for (int j=i; j<bsize; j++) {
hsubspace(i,j) = b.col(i).dot(sigma.col(j));
#ifdef Complex
hsubspace(j,i) = conj(hsubspace(i,j));
#else
hsubspace(j,i) = hsubspace(i,j);
#endif
}
SelfAdjointEigenSolver<MatrixXx> eigensolver(hsubspace);
if (eigensolver.info() != Success) {
pout << "Eigenvalue solver unsuccessful."<<endl;
pout << hsubspace<<endl;
abort();
}
b.block(0,0,b.rows(), bsize) = b.block(0,0,b.rows(), bsize)*eigensolver.eigenvectors();
sigma.block(0,0,b.rows(), bsize) = sigma.block(0,0,b.rows(), bsize)*eigensolver.eigenvectors();
ei = eigensolver.eigenvalues()[convergedRoot];
for (int i=0; i<convergedRoot; i++) {
r = sigma.col(i) - eigensolver.eigenvalues()[i]*b.col(i);
double error = r.norm();
if (error > tol) {
convergedRoot = i;
if (print) pout << "going back to converged root "<<i<<endl;
continue;
}
}
r = sigma.col(convergedRoot) - ei*b.col(convergedRoot);
double error = r.norm();
//if (numIter == 0)
//if (print ) pout << str(boost::format("#niter:%3d root:%3d -> Energy : %18.10g \n") %(numIter) % (convergedRoot-1) % ei );
if (print) {
if (numIter == 0) printf("nIter Root Energy Error\n");
if (commrank == 0) printf ("%5i %4i %18.10g %18.10g %10.2f\n", numIter, convergedRoot, ei, error, (getTime()-startofCalc));
}
numIter++;
if (hsubspace.rows() == b.rows()) {
//all root are available
for (int i=0; i<x0.size(); i++) {
x0[i] = b.col(i);
eroots.push_back(eigensolver.eigenvalues()[i]);
if (print ) pout << str(boost::format("#niter:%3d root:%3d -> Energy : %18.10g \n") %(numIter) % (i) % eroots[i] );
}
continueOrReturn = 2;
goto label1;
//return eroots;
}
if (error < tol || numIter >800*x0.size()) {
if (numIter >2000*x0.size()) {
pout << str(boost::format("Davidson calculation did not converge for root %3d, #iter %5d\n") % (convergedRoot+1) % (numIter) );
exit(0);
continueOrReturn = 2;
//return eroots;
}
convergedRoot++;
if(print) pout << str(boost::format("#niter:%3d root:%3d -> Energy : %18.10g \n") %(numIter) % (convergedRoot-1) % ei );
if (convergedRoot == nroots) {
for (int i=0; i<convergedRoot; i++) {
x0[i] = b.col(i);
eroots.push_back(eigensolver.eigenvalues()[i]);
}
continueOrReturn = 2;
goto label1;
//return eroots;
}
} // cvg
} // commrank=0
label1:
#ifndef SERIAL
MPI_Bcast(&continueOrReturn, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&numIter , 1, MPI_INT, 0, MPI_COMM_WORLD);
#endif
if (continueOrReturn == 2) return eroots;
if (commrank == 0) {
precondition(r,diag,ei);
for (int i=0; i<bsize; i++)
r = r - (b.col(i).adjoint()*r)(0,0)*b.col(i)/(b.col(i).adjoint()*b.col(i));
if (bsize < maxCopies) {
b.col(bsize) = r/r.norm();
bsize++;
} else {
bsize = nroots+3;
sigmaSize = nroots+2;
b.col(bsize-1) = r/r.norm();
}
} // commrank=0
} // while
} // end davidson
//=============================================================================
vector<double> davidsonDirect(HmultDirect& Hdirect, vector<MatrixXx>& x0, MatrixXx& diag, int maxCopies, double tol, int& numIter, bool print) {
//-----------------------------------------------------------------------------
/*!
Davidson, implemented very similarly to as implementeded in Block
:Inputs:
HmultDirect& Hdirect:
BM_description
vector<MatrixXx>& x0:
BM_description
MatrixXx& diag:
BM_description
int maxCopies:
BM_description
double tol:
BM_description
int& numIter:
BM_description
bool print:
BM_description
:Returns:
type name:
BM_description
*/
//-----------------------------------------------------------------------------
std::vector<double> eroots;
CItype* bcol, *sigmacol;
AllocateSHM(x0, bcol, sigmacol);
int nroots = x0.size();
MatrixXx b;
if (commrank == 0)
b=MatrixXx::Zero(x0[0].rows(), maxCopies);
int brows = x0[0].rows();
#ifndef SERIAL
MPI_Bcast(&brows, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
#endif
int niter;
//if some vector has zero norm then randomise it
if (commrank == 0) {
for (int i=0; i<nroots; i++) {
b.col(i) = 1.*x0[i];
if (x0[i].norm() < 1.e-10) {
b.col(i).setRandom();
b.col(i) = b.col(i)/b.col(i).norm();
}
}
//make vectors orthogonal to each other
for (int i=0; i<x0.size(); i++) {
for (int j=0; j<i; j++) {
CItype overlap = (b.col(j).adjoint()*b.col(i))(0,0);
b.col(i) -= overlap*b.col(j);
}
if (b.col(i).norm() <1e-8) {
b.col(i).setRandom();
}
for (int j=0; j<i; j++) {
CItype overlap = (b.col(j).adjoint()*b.col(i))(0,0);
b.col(i) -= overlap*b.col(j);
}
b.col(i) = b.col(i)/b.col(i).norm();
} // i
} // commrank=0
MatrixXx sigma;
if (commrank == 0) sigma = MatrixXx::Zero(x0[0].rows(), maxCopies);
int sigmaSize=0, bsize = x0.size();
MatrixXx r;
if (commrank == 0) {r=MatrixXx::Zero(x0[0].rows(),1);}
int convergedRoot = 0;
//int iter = 0;
numIter = 0;
double ei = 0.0;
while(true) {
//0->continue with the loop, 1 -> continue clause, 2 -> return
int continueOrReturn = 0;
#ifndef SERIAL
MPI_Bcast(&bsize, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&sigmaSize, 1, MPI_INT, 0, MPI_COMM_WORLD);
#endif
for (int i=sigmaSize; i<bsize; i++) {
if (commrank==0) {
for (int k=0; k<brows; k++) bcol[k] = b(k,i);
}
for (int k=0; k<brows; k++) sigmacol[k] = 0.0;
//by default the MatrixXx is column major,
//so all elements of bcol are contiguous
#ifndef SERIAL
MPI_Barrier(MPI_COMM_WORLD);
#ifndef Complex
MPI_Bcast(bcol, brows, MPI_DOUBLE, 0, MPI_COMM_WORLD);
#else
MPI_Bcast(bcol, 2*brows, MPI_DOUBLE, 0, MPI_COMM_WORLD);
#endif
MPI_Barrier(MPI_COMM_WORLD);
#endif
Hdirect(bcol, sigmacol);
sigmaSize++;
#ifndef SERIAL
MPI_Barrier(MPI_COMM_WORLD);
if (localrank == 0) {
#ifndef Complex
if (commrank == 0)
MPI_Reduce(MPI_IN_PLACE, sigmacol, brows, MPI_DOUBLE, MPI_SUM, 0, shmcomm);
else
MPI_Reduce(sigmacol, sigmacol, brows, MPI_DOUBLE, MPI_SUM, 0, shmcomm);
#else
if (commrank == 0)
MPI_Reduce(MPI_IN_PLACE, sigmacol, 2*brows, MPI_DOUBLE, MPI_SUM, 0, shmcomm);
else
MPI_Reduce(sigmacol, sigmacol, 2*brows, MPI_DOUBLE, MPI_SUM, 0, shmcomm);
#endif
} // localrank=0
MPI_Barrier(MPI_COMM_WORLD);
#endif
if (commrank==0) {
for (int k=0; k<brows; k++) sigma(k,i) = sigmacol[k];
}
#ifndef SERIAL
MPI_Barrier(MPI_COMM_WORLD);
#endif
} // i
if (commrank == 0) {
MatrixXx hsubspace(bsize, bsize);hsubspace.setZero(bsize, bsize);
for (int i=0; i<bsize; i++)
for (int j=i; j<bsize; j++) {
hsubspace(i,j) = b.col(i).dot(sigma.col(j));
#ifdef Complex
hsubspace(j,i) = conj(hsubspace(i,j));
#else
hsubspace(j,i) = hsubspace(i,j);
#endif
}
SelfAdjointEigenSolver<MatrixXx> eigensolver(hsubspace);
if (eigensolver.info() != Success) {
pout << "Eigenvalue solver unsuccessful."<<endl;
pout << hsubspace<<endl;
abort();
}
b.block(0,0,b.rows(), bsize) = b.block(0,0,b.rows(), bsize)*eigensolver.eigenvectors();
sigma.block(0,0,b.rows(), bsize) = sigma.block(0,0,b.rows(), bsize)*eigensolver.eigenvectors();
ei = eigensolver.eigenvalues()[convergedRoot];
for (int i=0; i<convergedRoot; i++) {
r = sigma.col(i) - eigensolver.eigenvalues()[i]*b.col(i);
double error = r.norm();
if (error > tol) {
convergedRoot = i;
if (print) pout << "going back to converged root "<<i<<endl;
continue;
}
}
r = sigma.col(convergedRoot) - ei*b.col(convergedRoot);
double error = r.norm();
//if (numIter == 0)
//if (print ) pout << str(boost::format("#niter:%3d root:%3d -> Energy : %18.10g \n") %(numIter) % (convergedRoot-1) % ei );
if (print) {
if (numIter == 0) printf("nIter Root Energy Error\n");
if (commrank == 0) printf ("%5i %4i %18.10g %18.10g %10.2f\n", numIter, convergedRoot, ei, error, (getTime()-startofCalc));
}
numIter++;
if (hsubspace.rows() == b.rows()) {
//all root are available
for (int i=0; i<x0.size(); i++) {
x0[i] = b.col(i);
eroots.push_back(eigensolver.eigenvalues()[i]);
if (print ) pout << str(boost::format("#niter:%3d root:%3d -> Energy : %18.10g \n") %(numIter) % (i) % eroots[i] );
}
continueOrReturn = 2;
goto label1;
//return eroots;
}
if (error < tol || numIter >400*x0.size()) {
if (numIter >400*x0.size()) {
pout << str(boost::format("Davidson calculation did not converge for root %3d, #iter %5d\n") % (convergedRoot+1) % (numIter) );
exit(0);
continueOrReturn = 2;
//return eroots;
}
convergedRoot++;
if(print) pout << str(boost::format("#niter:%3d root:%3d -> Energy : %18.10g \n") %(numIter) % (convergedRoot-1) % ei );
if (convergedRoot == nroots) {
for (int i=0; i<convergedRoot; i++) {
x0[i] = b.col(i);
eroots.push_back(eigensolver.eigenvalues()[i]);
}
continueOrReturn = 2;
goto label1;
//return eroots;
}
} // cvg
} // commrank=0
label1:
#ifndef SERIAL
MPI_Bcast(&continueOrReturn, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&numIter , 1, MPI_INT, 0, MPI_COMM_WORLD);
#endif
if (continueOrReturn == 2) return eroots;
if (commrank == 0) {
precondition(r,diag,ei);
for (int i=0; i<bsize; i++)
r = r - (b.col(i).adjoint()*r)(0,0)*b.col(i)/(b.col(i).adjoint()*b.col(i));
if (bsize < maxCopies) {
b.col(bsize) = r/r.norm();
bsize++;
} else {
bsize = min(nroots+3, maxCopies);
sigmaSize = bsize-1;
b.col(bsize-1) = r/r.norm();
}
} // commrank=0
} // while
} // end davidsonDirect
//=============================================================================
double LinearSolver(Hmult2& H, double E0, MatrixXx& x0, MatrixXx& b, vector<CItype*>& proj, double tol, bool print) {
//-----------------------------------------------------------------------------
/*!
Solve (H0-E0)*x0 = b
where "proj" is used to keep the solution orthogonal
:Inputs:
Hmult2& H:
The matrix H0
double E0:
The energy E0
MatrixXx& x0:
The unknown vector x0 (output)
MatrixXx& b:
The right vector b
vector<CItype*>& proj:
Projector to keep the solution orthogonal
double tol:
Tolerance
bool print:
Triggers printing out of messages
*/
//-----------------------------------------------------------------------------
for (int i=0; i<proj.size(); i++) {
CItype dotProduct = 0.0, norm=0.0;
for (int j=0; j<b.rows(); j++) {
#ifdef Complex
dotProduct += conj(proj[i][j])*b(j,0);
norm += conj(proj[i][j])*proj[i][j];
#else
dotProduct += proj[i][j]*b(j,0);
norm += proj[i][j]*proj[i][j];
#endif
}
for (int j=0; j<b.rows(); j++)
b(j,0) = b(j,0) - dotProduct*proj[i][j]/norm;
}
x0.setZero(b.rows(),1);
MatrixXx r = 1.*b, p = 1.*b;
double rsold = r.squaredNorm();
if (fabs(r.norm()) < tol) return 0.0;
int iter = 0;
while (true) {
MatrixXx Ap = 0.*p;
H(&p(0,0), &Ap(0,0)); ///REPLACE THIS WITH SOMETHING
#ifndef SERIAL
MPI_Allreduce(MPI_IN_PLACE, &Ap(0,0), Ap.rows(), MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
#endif
Ap = Ap - E0*p; //H0-E0
CItype alpha = rsold/(p.adjoint()*Ap)(0,0);
x0 += alpha*p;
r -= alpha*Ap;
for (int i=0; i<proj.size(); i++) {
CItype dotProduct = 0.0, norm=0.0;
for (int j=0; j<b.rows(); j++) {
#ifdef Complex
dotProduct += conj(proj[i][j])*r(j,0);
norm += conj(proj[i][j])*proj[i][j];
#else
dotProduct += proj[i][j]*r(j,0);
norm += proj[i][j]*proj[i][j];
#endif
}
for (int j=0; j<r.rows(); j++)
r(j,0) = r(j,0) - dotProduct*proj[i][j]/norm;
}
//r = r - ((proj[i].adjoint()*r)(0,0))*proj[i]/((proj[i].adjoint()*proj[i])(0,0));
//r = r- ((proj.adjoint()*r)(0,0))*proj/((proj.adjoint()*proj)(0,0));
double rsnew = r.squaredNorm();
CItype ept = -(x0.adjoint()*r + x0.adjoint()*b)(0,0);
if (false)
pout <<"#"<< iter<<" "<<ept<<" "<<rsnew<<std::endl;
if (r.norm() < tol || iter > 100) {
p.setZero(p.rows(),1);
H(&x0(0,0), &p(0,0)); ///REPLACE THIS WITH SOMETHING
p -=b;
return abs(ept);
}
p = r +(rsnew/rsold)*p;
rsold = rsnew;
iter++;
} // while
} // end LinearSolver