forked from sanshar/Block
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sweep_mps.C
286 lines (222 loc) · 9.88 KB
/
sweep_mps.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
/*
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 "guess_wavefunction.h"
#include "sweepgenblock.h"
#include "sweep.h"
#include "global.h"
#include "solver.h"
#include "initblocks.h"
#include "MatrixBLAS.h"
#include <boost/format.hpp>
#ifndef SERIAL
#include <boost/mpi/communicator.hpp>
#include <boost/mpi.hpp>
#endif
#include "rotationmat.h"
#include "density.h"
#include "pario.h"
using namespace boost;
using namespace std;
void getComplementarySites(std::vector<int> &sites, std::vector<int> &complementarySites)
{
for (int i=0; i<dmrginp.last_site(); ++i)
if (find(sites.begin(), sites.end(), i) == sites.end())
complementarySites.push_back(i);
return;
}
//Canonicalize wavefunction, takes the wavefunction and does a sweep to update all the roation matrices so that we get a consistent wavefunction along the whole sweep
void SpinAdapted::Sweep::CanonicalizeWavefunction(SweepParams &sweepParams, const bool &forward, int currentstate)
{
sweepParams.set_sweep_parameters();
sweepParams.set_block_iter() = 0;
std::vector<int> sites;
int new_site, wave_site;
if (forward) {
pout << "\t\t\t Starting sweep "<< sweepParams.set_sweep_iter()<<" in forwards direction"<<endl;
new_site = 0;
}
else {
pout << "\t\t\t Starting sweep "<< sweepParams.set_sweep_iter()<<" in backwards direction" << endl;
new_site = dmrginp.spinAdapted() ? dmrginp.last_site()-1 : dmrginp.last_site()/2-1;
}
pout << "\t\t\t ============================================================================ " <<
endl;
if (dmrginp.spinAdapted())
sites.push_back(new_site);
else {
sites.push_back(2*new_site);
sites.push_back(2*new_site+1);
std::sort(sites.begin(), sites.end());
}
//only need statinfos
StateInfo stateInfo1; makeStateInfo(stateInfo1, new_site);
for (; sweepParams.get_block_iter() < sweepParams.get_n_iters(); ) {
pout << "\n\t\t\t Block Iteration :: " << sweepParams.get_block_iter() << endl;
pout << "\t\t\t ----------------------------" << endl;
if (forward) {
new_site++;
wave_site = new_site+1;
p1out << "\t\t\t Current direction is :: Forwards " << endl;
}
else {
new_site--;
wave_site = new_site-1;
p1out << "\t\t\t Current direction is :: Backwards " << endl;
}
std::vector<int> complementarySites, spindotsites(1, new_site), oldsites = sites, oldcomplement;
if (dmrginp.spinAdapted())
sites.push_back(new_site);
else {
sites.push_back(2*new_site);
sites.push_back(2*new_site+1);
std::sort(sites.begin(), sites.end());
}
getComplementarySites(sites, complementarySites);
getComplementarySites(oldsites, oldcomplement);
StateInfo siteState, newState1, bigstate, envstate;
makeStateInfo(siteState, new_site);
TensorProduct(stateInfo1, siteState, newState1, NO_PARTICLE_SPIN_NUMBER_CONSTRAINT);
newState1.CollectQuanta();
Wavefunction w; w.set_deltaQuantum() = dmrginp.effective_molecule_quantum_vec();
w.set_onedot(true);
if (!dmrginp.spinAdapted()) {
std::vector<int> spinSites(complementarySites.size()/2, 0);
for (int s=0; s<spinSites.size(); s++)
spinSites[s] = complementarySites[2*s]/2;
StateInfo::restore(!forward, spinSites, envstate, currentstate);
}
else
StateInfo::restore(!forward, complementarySites, envstate, currentstate);
TensorProduct(newState1, envstate, bigstate, PARTICLE_SPIN_NUMBER_CONSTRAINT);
if (sweepParams.get_block_iter() == 0)
GuessWave::transpose_previous_wavefunction(w, bigstate, complementarySites, spindotsites, currentstate, true, true);
else
GuessWave::transform_previous_wavefunction(w, bigstate, oldsites, oldcomplement, currentstate, true, true);
w.SaveWavefunctionInfo(bigstate, sites, currentstate);
//make the newstate
std::vector<Matrix> rotation1;
DensityMatrix tracedMatrix;
tracedMatrix.allocate(*bigstate.leftStateInfo);
operatorfunctions::MultiplyProduct(w, Transpose(const_cast<Wavefunction&> (w)), tracedMatrix, 1.0);
int largeNumber = 1000000;
if (!mpigetrank())
double error = makeRotateMatrix(tracedMatrix, rotation1, largeNumber, sweepParams.get_keep_qstates());
SaveRotationMatrix (sites, rotation1, currentstate);
StateInfo renormState1;
SpinAdapted::StateInfo::transform_state(rotation1, newState1, renormState1);
StateInfo::store(forward, sites, renormState1, currentstate);
stateInfo1 = renormState1;
++sweepParams.set_block_iter();
}
}
//Initialize stateinfo using the rotation matrices
void SpinAdapted::Sweep::InitializeStateInfo(SweepParams &sweepParams, const bool &forward, int currentstate)
{
sweepParams.set_sweep_parameters();
sweepParams.set_block_iter() = 0;
std::vector<int> sites;
int new_site, wave_site;
if (forward)
new_site = 0;
else
new_site = dmrginp.spinAdapted() ? dmrginp.last_site()-1 : dmrginp.last_site()/2-1;
if (dmrginp.spinAdapted())
sites.push_back(new_site);
else {
sites.push_back(2*new_site);
sites.push_back(2*new_site+1);
std::sort(sites.begin(), sites.end());
}
//only need statinfos
StateInfo stateInfo1; makeStateInfo(stateInfo1, new_site);
StateInfo::store(forward, sites, stateInfo1, currentstate);
for (; sweepParams.get_block_iter() < sweepParams.get_n_iters(); ) {
if (forward)
new_site++;
else
new_site--;
if (dmrginp.spinAdapted())
sites.push_back(new_site);
else {
sites.push_back(2*new_site);
sites.push_back(2*new_site+1);
std::sort(sites.begin(), sites.end());
}
StateInfo siteState, newState1;
makeStateInfo(siteState, new_site);
TensorProduct(stateInfo1, siteState, newState1, NO_PARTICLE_SPIN_NUMBER_CONSTRAINT);
newState1.CollectQuanta();
//make the newstate
std::vector<Matrix> rotation1;
LoadRotationMatrix (sites, rotation1, currentstate);
StateInfo renormState1;
SpinAdapted::StateInfo::transform_state(rotation1, newState1, renormState1);
StateInfo::store(forward, sites, renormState1, currentstate);
stateInfo1 = renormState1;
++sweepParams.set_block_iter();
}
}
//before you start optimizing each state you want to initalize all the overlap matrices
void Sweep::InitializeOverlapSpinBlocks(SweepParams &sweepParams, const bool &forward, int stateA, int stateB, int integralIndex)
{
SpinBlock system;
sweepParams.set_sweep_parameters();
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;
int restartSize = 0; bool restart = false, warmUp = false;
InitBlocks::InitStartingBlock (system,forward, stateA, stateB, sweepParams.get_forward_starting_size(), sweepParams.get_backward_starting_size(), restartSize, restart, warmUp, integralIndex);
sweepParams.set_block_iter() = 0;
p2out << "\t\t\t Starting block is :: " << endl << system << endl;
SpinBlock::store (forward, system.get_sites(), system, stateA, stateB); // if restart, just restoring an existing block --
sweepParams.savestate(forward, system.get_sites().size());
bool dot_with_sys = true;
vector<int> syssites = system.get_sites();
if (dmrginp.outputlevel() > 0)
mcheck("at the very start of sweep"); // just timer
for (; sweepParams.get_block_iter() < sweepParams.get_n_iters(); ) // get_n_iters() returns the number of blocking iterations needed in one sweep
{
pout << "\t\t\t Block Iteration :: " << sweepParams.get_block_iter() << endl;
pout << "\t\t\t ----------------------------" << endl;
if (forward) p1out << "\t\t\t Current direction is :: Forwards " << endl;
else p1out << "\t\t\t Current direction is :: Backwards " << endl;
SpinBlock systemDot, environmentDot;
int systemDotStart, systemDotEnd;
int systemDotSize = sweepParams.get_sys_add() - 1;
if (forward)
{
systemDotStart = dmrginp.spinAdapted() ? *system.get_sites().rbegin () + 1 : (*system.get_sites().rbegin ())/2 + 1 ;
systemDotEnd = systemDotStart + systemDotSize;
}
else
{
systemDotStart = dmrginp.spinAdapted() ? system.get_sites()[0] - 1 : (system.get_sites()[0])/2 - 1 ;
systemDotEnd = systemDotStart - systemDotSize;
}
systemDot = SpinBlock(systemDotStart, systemDotEnd, integralIndex, true);
SpinBlock newSystem; // new system after blocking and decimating
newSystem.initialise_op_array(OVERLAP, false);
newSystem.setstoragetype(DISTRIBUTED_STORAGE);
newSystem.BuildSumBlock (NO_PARTICLE_SPIN_NUMBER_CONSTRAINT, system, systemDot);
std::vector<Matrix> brarotateMatrix, ketrotateMatrix;
LoadRotationMatrix(newSystem.get_sites(), brarotateMatrix, stateA);
LoadRotationMatrix(newSystem.get_sites(), ketrotateMatrix, stateB);
newSystem.transform_operators(brarotateMatrix, ketrotateMatrix);
system = newSystem;
p2out << system<<endl;
SpinBlock::store (forward, system.get_sites(), system, stateA, stateB);
++sweepParams.set_block_iter();
sweepParams.savestate(forward, syssites.size());
if (dmrginp.outputlevel() > 0)
mcheck("at the end of sweep iteration");
}
pout << "\t\t\t ============================================================================ " << endl;
// update the static number of iterations
return ;
}