forked from sanshar/Block
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sweep.C
460 lines (391 loc) · 18 KB
/
sweep.C
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
/*
Developed by Sandeep Sharma and Garnet K.-L. Chan, 2012
Copyright (c) 2012, Garnet K.-L. Chan
This program is integrated in Molpro with the permission of
Sandeep Sharma and Garnet K.-L. Chan
*/
#include "sweep.h"
#include "global.h"
#include "solver.h"
#include "initblocks.h"
#include "MatrixBLAS.h"
#ifndef SERIAL
#include <boost/mpi/communicator.hpp>
#include <boost/mpi.hpp>
#endif
#include "rotationmat.h"
#include "density.h"
#ifdef MOLPRO
#include "global/CxOutputStream.h"
#define pout if (dmrginp.outputlevel() != 0) xout
#endif
using namespace boost;
using namespace std;
void SpinAdapted::Sweep::BlockAndDecimate (SweepParams &sweepParams, SpinBlock& system, SpinBlock& newSystem, const bool &useSlater, const bool& dot_with_sys)
{
if (dmrginp.outputlevel() > 0) {
mcheck("at the start of block and decimate");
pout << "\t\t\t dot with system "<<dot_with_sys<<endl;
}
pout <<endl<< "\t\t\t Performing Blocking"<<endl;
// figure out if we are going forward or backwards
dmrginp.guessgenT -> start();
bool forward = (system.get_sites() [0] == 0);
SpinBlock systemDot;
SpinBlock envDot;
int systemDotStart, systemDotEnd;
int systemDotSize = sweepParams.get_sys_add() - 1;
if (forward)
{
systemDotStart = *system.get_sites().rbegin () + 1;
systemDotEnd = systemDotStart + systemDotSize;
}
else
{
systemDotStart = system.get_sites() [0] - 1;
systemDotEnd = systemDotStart - systemDotSize;
}
vector<int> spindotsites(2);
spindotsites[0] = systemDotStart;
spindotsites[1] = systemDotEnd;
systemDot = SpinBlock(systemDotStart, systemDotEnd);
SpinBlock environment, environmentDot, newEnvironment;
int environmentDotStart, environmentDotEnd, environmentStart, environmentEnd;
int environmentDotSize = sweepParams.get_env_add() -1;
if (environmentDotSize <0) environmentDotSize = 0 ;
if (forward)
{
environmentDotStart = systemDotEnd + 1;
environmentDotEnd = environmentDotStart + environmentDotSize;
}
else
{
environmentDotStart = systemDotEnd - 1;
environmentDotEnd = environmentDotStart - environmentDotSize;
}
vector<int> envdotsites(2);
envdotsites[0] = environmentDotStart;
envdotsites[1] = environmentDotEnd;
if (!sweepParams.get_onedot())
environmentDot = SpinBlock(environmentDotStart, environmentDotEnd);
const int nexact = forward ? sweepParams.get_forward_starting_size() : sweepParams.get_backward_starting_size();
//before halfway put the sysdot with system otherwise with environment
if (!sweepParams.get_onedot()) {
dmrginp.datatransfer -> start();
system.addAdditionalCompOps();
dmrginp.datatransfer -> stop();
InitBlocks::InitNewSystemBlock(system, systemDot, newSystem, sweepParams.get_sys_add(), dmrginp.direct(),
DISTRIBUTED_STORAGE, dot_with_sys, true);
if (dmrginp.outputlevel() > 0)
mcheck("");
InitBlocks::InitNewEnvironmentBlock(environment, environmentDot, newEnvironment, system, systemDot,
sweepParams.get_sys_add(), sweepParams.get_env_add(), forward, dmrginp.direct(),
sweepParams.get_onedot(), nexact, useSlater, !dot_with_sys, true, dot_with_sys);
if (dmrginp.outputlevel() > 0)
mcheck("");
}
else {
dmrginp.datatransfer -> start();
system.addAdditionalCompOps();
dmrginp.datatransfer -> stop();
if (dot_with_sys) {
InitBlocks::InitNewSystemBlock(system, systemDot, newSystem, sweepParams.get_sys_add(), dmrginp.direct(), DISTRIBUTED_STORAGE, dot_with_sys, true);
}
InitBlocks::InitNewEnvironmentBlock(environment, systemDot, newEnvironment, system, systemDot,
sweepParams.get_sys_add(), sweepParams.get_env_add(), forward, dmrginp.direct(),
sweepParams.get_onedot(), nexact, useSlater, !dot_with_sys, true, dot_with_sys);
}
SpinBlock big;
if (dot_with_sys) {
newSystem.set_loopblock(true);
system.set_loopblock(false);
newEnvironment.set_loopblock(false);
if (!sweepParams.get_onedot())
environment.set_loopblock(false);
InitBlocks::InitBigBlock(newSystem, newEnvironment, big);
}
else{
if (sweepParams.get_onedot()) {
system.set_loopblock(false);
newEnvironment.set_loopblock(true);
environment.set_loopblock(true);
InitBlocks::InitBigBlock(system, newEnvironment, big);
}
else {
newSystem.set_loopblock(false);
system.set_loopblock(false);
newEnvironment.set_loopblock(true);
environment.set_loopblock(false);
InitBlocks::InitBigBlock(newSystem, newEnvironment, big);
}
}
//analyse_operator_distribution(big);
dmrginp.guessgenT -> stop();
dmrginp.multiplierT -> start();
std::vector<Matrix> rotatematrix;
if (dmrginp.outputlevel() > 0)
mcheck("");
if (dmrginp.outputlevel() == 0) {
if (!dot_with_sys && sweepParams.get_onedot()) {
pout << "\t\t\t System Block"<<system;
pout << "\t\t\t Environment Block"<<newEnvironment<<endl;
}
else {
pout << "\t\t\t System Block"<<newSystem;
pout << "\t\t\t Environment Block"<<newEnvironment<<endl;
}
pout << "\t\t\t Solving wavefunction "<<endl;
}
newSystem.RenormaliseFrom (sweepParams.set_lowest_energy(), sweepParams.set_lowest_energy_spins(), sweepParams.set_lowest_error(),
rotatematrix, sweepParams.get_keep_states(),
sweepParams.get_keep_qstates(), sweepParams.get_davidson_tol(), big, sweepParams.get_guesstype(), sweepParams.get_noise(),
sweepParams.get_additional_noise(), sweepParams.get_onedot(), system, systemDot, environmentDot, environment,
dot_with_sys, useSlater, sweepParams.get_sweep_iter());
std::vector<StateInfo> storeStates(3);
storeStates[0] = newSystem.get_stateInfo();
if(dot_with_sys)
storeStates[1] = newEnvironment.get_stateInfo();
else
storeStates[1] = environment.get_stateInfo();
if (dmrginp.outputlevel() > 0)
mcheck("");
environment.clear();
newEnvironment.clear();
pout <<"\t\t\t Performing Renormalization "<<endl;
pout << "\t\t\t Total discarded weight "<<sweepParams.set_lowest_error()<<endl<<endl;
dmrginp.multiplierT -> stop();
dmrginp.operrotT -> start();
newSystem.transform_operators(rotatematrix);
storeStates[2] = newSystem.get_stateInfo();
dmrginp.operrotT -> stop();
if (dmrginp.outputlevel() > 0)
mcheck("after rotation and transformation of block");
if (dmrginp.outputlevel() > 0){
pout << dmrginp.guessgenT<<" "<<dmrginp.multiplierT<<" "<<dmrginp.operrotT<< " "<<globaltimer.totalwalltime()<<" timer "<<endl;
pout << dmrginp.makeopsT<<" makeops "<<endl;
pout << dmrginp.datatransfer<<" datatransfer "<<endl;
pout <<"oneindexopmult twoindexopmult Hc couplingcoeff"<<endl;
pout << dmrginp.oneelecT<<" "<<dmrginp.twoelecT<<" "<<dmrginp.hmultiply<<" "<<dmrginp.couplingcoeff<<" hmult"<<endl;
pout << dmrginp.buildsumblock<<" "<<dmrginp.buildblockops<<" build block"<<endl;
pout << "addnoise S_0_opxop S_1_opxop S_2_opxop"<<endl;
pout << dmrginp.addnoise<<" "<<dmrginp.s0time<<" "<<dmrginp.s1time<<" "<<dmrginp.s2time<<endl;
}
}
double SpinAdapted::Sweep::do_one(SweepParams &sweepParams, const bool &warmUp, const bool &forward, const bool &restart, const int &restartSize)
{
SpinBlock system;
const int nroots = dmrginp.nroots(sweepParams.get_sweep_iter());
std::vector<double> finalEnergy(nroots,1.0e10);
std::vector<double> finalEnergy_spins(nroots,0.);
double finalError = 0.;
if (restart) {
finalEnergy = sweepParams.get_lowest_energy();
finalEnergy_spins = sweepParams.get_lowest_energy();
finalError = sweepParams.get_lowest_error();
}
sweepParams.set_sweep_parameters();
// a new renormalisation sweep routine
pout << endl;
if (forward)
pout << "\t\t\t Starting sweep "<< sweepParams.set_sweep_iter()<<" in forwards direction"<<endl;
else
pout << "\t\t\t Starting sweep "<< sweepParams.set_sweep_iter()<<" in backwards direction" << endl;
pout << "\t\t\t ============================================================================ " << endl;
InitBlocks::InitStartingBlock (system,forward, sweepParams.get_forward_starting_size(), sweepParams.get_backward_starting_size(), restartSize, restart, warmUp);
if(!restart)
sweepParams.set_block_iter() = 0;
if (dmrginp.outputlevel() > 0)
pout << "\t\t\t Starting block is :: " << endl << system << endl;
SpinBlock::store (forward, system.get_sites(), system); // if restart, just restoring an existing block --
sweepParams.savestate(forward, system.get_sites().size());
bool dot_with_sys = true;
vector<int> syssites;
{
syssites = system.get_sites();
}
if (restart)
{
if (forward && system.get_complementary_sites()[0] >= dmrginp.last_site()/2)
dot_with_sys = false;
if (!forward && system.get_sites()[0]-1 < dmrginp.last_site()/2)
dot_with_sys = false;
}
if (dmrginp.outputlevel() > 0)
mcheck("at the very start of sweep");
for (; sweepParams.get_block_iter() < sweepParams.get_n_iters(); )
{
pout << "\t\t\t Block Iteration :: " << sweepParams.get_block_iter() << endl;
pout << "\t\t\t ----------------------------" << endl;
if (dmrginp.outputlevel() > 0) {
if (forward)
pout << "\t\t\t Current direction is :: Forwards " << endl;
else
pout << "\t\t\t Current direction is :: Backwards " << endl;
}
if (dmrginp.no_transform() || (sweepParams.get_sweep_iter()-sweepParams.get_restart_iter() == 0 && sweepParams.get_block_iter() == 0))
sweepParams.set_guesstype() = BASIC;
else if (!warmUp && sweepParams.get_block_iter() != 0)
sweepParams.set_guesstype() = TRANSFORM;
else if (!warmUp && sweepParams.get_block_iter() == 0 &&
((dmrginp.algorithm_method() == TWODOT_TO_ONEDOT && dmrginp.twodot_to_onedot_iter() != sweepParams.get_sweep_iter()) ||
dmrginp.algorithm_method() != TWODOT_TO_ONEDOT))
sweepParams.set_guesstype() = TRANSPOSE;
else
sweepParams.set_guesstype() = BASIC;
if (dmrginp.outputlevel() > 0)
pout << "\t\t\t Blocking and Decimating " << endl;
SpinBlock newSystem;
//Need to substitute by:
if (warmUp && (sym=="dinfh"||sym=="trans"))
//if (warmUp)// && (sym=="dinfh"||sym=="trans"))
Startup(sweepParams, system, newSystem);
else {
if (sweepParams.set_sweep_iter() == 1 && sweepParams.get_block_iter() == 0)
sweepParams.set_guesstype() = BASIC;
BlockAndDecimate (sweepParams, system, newSystem, warmUp, dot_with_sys);
}
//Need to substitute by?
if (!warmUp || !(sym == "dinfh"||sym=="trans") ){
//if (!warmUp){// || !(sym == "dinfh"||sym=="trans") ){
for(int j=0;j<nroots;++j)
{
#ifndef MOLPRO
pout << "\t\t\t Total block energy for State [ " << j <<
" ] with " << sweepParams.get_keep_states()<<" States :: " << sweepParams.get_lowest_energy()[j]+dmrginp.get_coreenergy() <<endl;
#else
//We might want to relax the output restrictions here, so it prints out with outputlevel=0
if (dmrginp.outputlevel() < 0) {
pout << "\t\t\t Total block energy for State [ " << j <<
" ] with " << sweepParams.get_keep_states()<<" States :: " << fixed << setprecision(10) << sweepParams.get_lowest_energy()[j]+dmrginp.get_coreenergy() <<endl;
}
#endif
}
finalEnergy_spins = ((sweepParams.get_lowest_energy()[0] < finalEnergy[0]) ? sweepParams.get_lowest_energy_spins() : finalEnergy_spins);
finalEnergy = ((sweepParams.get_lowest_energy()[0] < finalEnergy[0]) ? sweepParams.get_lowest_energy() : finalEnergy);
finalError = max(sweepParams.get_lowest_error(),finalError);
pout << endl;
}
system = newSystem;
if (dmrginp.outputlevel() > 0){
pout << system<<endl;
system.printOperatorSummary();
}
//system size is going to be less than environment size
if (forward && system.get_complementary_sites()[0] >= dmrginp.last_site()/2)
dot_with_sys = false;
if (!forward && system.get_sites()[0]-1 < dmrginp.last_site()/2)
dot_with_sys = false;
SpinBlock::store (forward, system.get_sites(), system);
syssites = system.get_sites();
if (dmrginp.outputlevel() > 0)
pout << "\t\t\t saving state " << syssites.size() << endl;
++sweepParams.set_block_iter();
#ifndef SERIAL
mpi::communicator world;
world.barrier();
#endif
sweepParams.savestate(forward, syssites.size());
if (dmrginp.outputlevel() > 0)
mcheck("at the end of sweep iteration");
}
for(int j=0;j<nroots;++j)
pout << "\t\t\t Finished Sweep with " << sweepParams.get_keep_states() << " states and sweep energy for State [ " << j
<< " ] with Spin [ " << dmrginp.molecule_quantum().get_s() << " ] :: " << finalEnergy[j]+dmrginp.get_coreenergy() << endl;
pout << "\t\t\t Largest Error for Sweep with " << sweepParams.get_keep_states() << " states is " << finalError << endl;
for(int j=0;j<nroots;++j){
if (mpigetrank() == 0) {
#ifndef MOLPRO
printf("\t\t\t M = %6i Largest Discarded Weight = %8.3e Sweep Energy = %20.10f \n",sweepParams.get_keep_states(), finalError, finalEnergy[j]+dmrginp.get_coreenergy());
#else
//printf("\t\t\t M = %6i Largest Discarded Weight = %8.3e Sweep Energy = %20.10f \n",sweepParams.get_keep_states(), finalError, finalEnergy[j]+dmrginp.get_coreenergy());
xout << "\t\t\t M = " << setw(6) << sweepParams.get_keep_states() ;
xout << "\t Largest Discarded Weight = " << scientific << setprecision(8) << finalError ;
xout << "\t Sweep Energy = " << fixed << setprecision(10) << finalEnergy[j]+dmrginp.get_coreenergy() << endl;
#endif
}
}
pout << "\t\t\t ============================================================================ " << endl;
// update the static number of iterations
++sweepParams.set_sweep_iter();
return finalEnergy[0];
}
void SpinAdapted::Sweep::Startup (SweepParams &sweepParams, SpinBlock& system, SpinBlock& newSystem)
{
mcheck("at the start of block and decimate");
dmrginp.guessgenT -> start();
bool forward = (system.get_sites() [0] == 0);
SpinBlock systemDot;
int systemDotStart, systemDotEnd;
int systemDotSize = sweepParams.get_sys_add() - 1;
if (forward)
{
systemDotStart = *system.get_sites().rbegin () + 1;
systemDotEnd = systemDotStart + systemDotSize;
}
else
{
systemDotStart = system.get_sites() [0] - 1;
systemDotEnd = systemDotStart - systemDotSize;
}
vector<int> spindotsites(2);
spindotsites[0] = systemDotStart;
spindotsites[1] = systemDotEnd;
systemDot = SpinBlock(systemDotStart, systemDotEnd);
const int nexact = forward ? sweepParams.get_forward_starting_size() : sweepParams.get_backward_starting_size();
dmrginp.datatransfer -> start();
system.addAdditionalCompOps();
dmrginp.datatransfer -> stop();
InitBlocks::InitNewSystemBlock(system, systemDot, newSystem, sweepParams.get_sys_add(), dmrginp.direct(),
DISTRIBUTED_STORAGE, true, true);
int nquanta = newSystem.get_stateInfo().quanta.size();
std::vector<DiagonalMatrix > energies(nquanta);
std::vector<Matrix> rotateMatrix(nquanta);
DensityMatrix transformmatrix;
transformmatrix.allocate(newSystem.get_stateInfo());
SpinQuantum q(0,0,IrrepSpace(0));
if (mpigetrank() == 0) {
double minval = 1e12;
for (int i=0; i<nquanta; i++) {
diagonalise(newSystem.get_op_rep(HAM,q)->operator_element(i,i), energies[i], transformmatrix(i,i));
for (int j=0; j<energies[i].Nrows(); j++)
if (minval > energies[i](j+1))
minval = energies[i](j+1);
}
for (int i=0; i<nquanta; i++) {
for (int j=0; j<energies[i].Nrows(); j++)
energies[i](j+1) = 1.0/(energies[i](j+1)-minval+1);
}
vector<pair<int, int> > inorderwts;
vector<vector<int> > wtsbyquanta;
sort_weights(energies, inorderwts, wtsbyquanta);
// make transformation matrix by various algorithms
int keptstates = sweepParams.get_keep_states()/2, keptqstates = sweepParams.get_keep_states()-keptstates;
int totalstatesbydm = min(static_cast<int>(inorderwts.size()), keptstates);
int totalstatesbyquanta = min(static_cast<int>(inorderwts.size()), keptstates + keptqstates) - totalstatesbydm;
if (totalstatesbyquanta < 0) totalstatesbyquanta = 0;
pout << "\t\t\t total states using dm and quanta " << totalstatesbydm << " " << totalstatesbyquanta << endl;
double error = assign_matrix_by_dm(rotateMatrix, energies, transformmatrix, inorderwts, wtsbyquanta, totalstatesbydm, totalstatesbyquanta, newSystem.size(), 2*totalstatesbydm);
pout << "\t\t\t Total discarded weight "<<error<<endl;
}
#ifndef SERIAL
mpi::communicator world;
broadcast(world, rotateMatrix, 0);
#endif
dmrginp.operrotT -> start();
newSystem.transform_operators(rotateMatrix);
for (int i=0; i<dmrginp.nroots(); i++)
SaveRotationMatrix (newSystem.get_sites(), rotateMatrix, i);
dmrginp.operrotT -> stop();
mcheck("after rotation and transformation of block");
pout << dmrginp.guessgenT<<" "<<dmrginp.multiplierT<<" "<<dmrginp.operrotT<< " "<<globaltimer.totalwalltime()<<" timer "<<endl;
pout << dmrginp.makeopsT<<" makeops "<<endl;
pout << dmrginp.datatransfer<<" datatransfer "<<endl;
//cout << dmrginp.justmultiply<<" just multiply "<<endl;
//cout << dmrginp.otherrotation<<" "<<dmrginp.spinrotation<<" "<<dmrginp.operrotT<<" rotations time "<<endl;
pout <<"oneindexopmult twoindexopmult Hc couplingcoeff"<<endl;
pout << dmrginp.oneelecT<<" "<<dmrginp.twoelecT<<" "<<dmrginp.hmultiply<<" "<<dmrginp.couplingcoeff<<" hmult"<<endl;
pout << dmrginp.buildsumblock<<" "<<dmrginp.buildblockops<<" build block"<<endl;
pout << "addnoise S_0_opxop S_1_opxop S_2_opxop"<<endl;
pout << dmrginp.addnoise<<" "<<dmrginp.s0time<<" "<<dmrginp.s1time<<" "<<dmrginp.s2time<<endl;
//mcheck("After renorm transform");
}