-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
286 lines (277 loc) · 13.3 KB
/
main.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
#include <time.h>
#include <fstream>
#include "FreeFunctions.h"
#include "ESolver.h"
#include "GlobalPrecisionParameters.h"
using namespace Eigen;
int main()
{
clock_t start = clock();
std::ifstream filein("Input/Input");
if(!filein)
{
std::cerr << "Couldn't open input file." << std::endl;
exit(EXIT_FAILURE);
};
// read in parameters that are constant across all trials:
int nTrials;
bool calcObservables;
filein >> nTrials >> calcObservables;
bool calcOneSiteExpValues;
int indexOfOneSiteOp;
obsMatrixD_t oneSiteOp;
bool calcTwoSiteExpValues;
int indexOfFirstTwoSiteOp,
indexOfSecondTwoSiteOp;
obsMatrixD_t firstTwoSiteOp,
secondTwoSiteOp;
#include "ObservableOps.h"
if(calcObservables)
{
filein >> calcOneSiteExpValues;
if(calcOneSiteExpValues)
{
filein >> indexOfOneSiteOp;
oneSiteOp = obsList[indexOfOneSiteOp];
};
filein >> calcTwoSiteExpValues;
if(calcTwoSiteExpValues)
{
filein >> indexOfFirstTwoSiteOp >> indexOfSecondTwoSiteOp;
firstTwoSiteOp = obsList[indexOfFirstTwoSiteOp];
secondTwoSiteOp = obsList[indexOfSecondTwoSiteOp];
};
};
std::ofstream infoout("Output/Info");
if(infoout)
{
infoout << "Number of trials: " << nTrials
<< "\nCalculate observables? "
<< (calcObservables ? "Yes" : "No") << std::endl;
if(calcObservables)
{
infoout << "Calculate one-site observables? ";
if(calcOneSiteExpValues)
infoout << "Yes\nIndex of one-site observable: "
<< indexOfOneSiteOp << std::endl;
else
infoout << "No" << std::endl;
infoout << "Calculate two-site observables? ";
if(calcTwoSiteExpValues)
infoout << "Yes\nIndices of two-site observables: "
<< indexOfFirstTwoSiteOp << " "
<< indexOfSecondTwoSiteOp << std::endl;
else
infoout << "No" << std::endl;
infoout << "Observables threshold: " << observableThreshold
<< std::endl;
};
}
else
{
std::cerr << "Couldn't open output files." << std::endl;
exit(EXIT_FAILURE);
};
infoout.close();
stepData data; // this struct will contain most of the important parameters
data.compBlock = data.beforeCompBlock = NULL;
for(int trial = 1; trial <= nTrials; trial++)
{
clock_t startTrial = clock();
std::cout << "Trial " << trial << ":" << std::endl;
std::ofstream fileout("Output/Trial_" + std::to_string(trial));
fileout << "Trial " << trial << ":\n" << std::endl;
// read in parameters that vary over trials:
int lSys; // system length
filein >> lSys;
std::vector<double> couplingConstants(nCouplingConstants);
for(int i = 0; i < nCouplingConstants; i++)
filein >> couplingConstants[i];
int targetQNum,
rangeOfObservables, // number of sites at which to measure observables
nSweeps; // number of sweeps to be performed
filein >> targetQNum >> rangeOfObservables >> data.mMax >> nSweeps;
if(rangeOfObservables == -1)
rangeOfObservables = lSys;
std::vector<double> groundStateErrorTolerances(nSweeps + 1);
for(int sweep = 0; sweep <= nSweeps; sweep++)
filein >> groundStateErrorTolerances[sweep];
fileout << "System length: " << lSys << "\nCoupling constants:";
for(double couplingConstant : couplingConstants)
fileout << " " << couplingConstant;
fileout << "\nTargeted quantum number: " << targetQNum
<< "\nMaximum bond dimension: " << data.mMax
<< "\nNumber of sweeps: " << nSweeps << "\nLanczos tolerances:";
for(double groundStateErrorTolerance : groundStateErrorTolerances)
fileout << " " << groundStateErrorTolerance;
fileout << std::endl << std::endl;
data.ham.setParams(couplingConstants, targetQNum, lSys);
int skips = 0,
runningKeptStates = d * d;
for(; runningKeptStates <= data.mMax; skips++)
runningKeptStates *= d; // find how many edge sites can be skipped
bool oddSize = lSys % 2;
int lSFinal, // final length of the system block
lEFinal; // final length of the environment block
if(oddSize)
{
lSFinal = (lSys - 1)/2;
lEFinal = (lSys - 3)/2;
}
else
lSFinal = lEFinal = lSys / 2 - 1;
bool completeED = false;
if(skips + 1 >= lSFinal)
{
if(skips + 1 == lSFinal && runningKeptStates == data.mMax * d)
{
std::cout << "Note: the maximum bond dimension is large enough "
<< "to perform exact diagonalization." << std::endl;
completeED = true;
}
else
{
std::cout << "Error: the maximum bond dimension is larger than "
<< "required for exact diagonalization." << std::endl;
continue;
};
};
std::vector<TheBlock> leftBlocks(lSys - 2 - skips),
rightBlocks(lSys - 2 - skips);
// initialize system - the last block is only used for odd-size ED
TheBlock* rightBlocksStart = rightBlocks.data();
leftBlocks.front() = rightBlocks.front() = TheBlock(data.ham);
// initialize the one-site block
std::cout << "Performing iDMRG..." << std::endl;
// note: this iDMRG code assumes parity symmetry of the Hamiltonian
data.exactDiag = true;
data.compBlock = rightBlocksStart;
data.infiniteStage = true;
data.lancTolerance = groundStateErrorTolerances.front()
* groundStateErrorTolerances.front() / 2;
rmMatrixX_t psiGround; // seed for Lanczos algorithm
double cumulativeTruncationError = 0.;
for(int site = 0; site < skips; site++, data.compBlock++) // initial ED
rightBlocks[site + 1] = leftBlocks[site + 1]
= leftBlocks[site]
.nextBlock(data, psiGround,
cumulativeTruncationError);
data.exactDiag = completeED;
for(int site = skips, end = lEFinal - 1; site < end; site++,
data.compBlock++)
// iDMRG
{
psiGround = randomSeed(leftBlocks[site], rightBlocks[site]);
rightBlocks[site + 1] = leftBlocks[site + 1]
= leftBlocks[site]
.nextBlock(data, psiGround,
cumulativeTruncationError);
rightBlocks[site].primeToRhoBasis = leftBlocks[site].primeToRhoBasis;
// copy primeToRhoBasis to reflected block
};
if(oddSize) // last half-step of iDMRG for an odd-sized system
{
data.compBlock = rightBlocksStart + (lSFinal - 2);
psiGround = randomSeed(leftBlocks[lSFinal - 2],
rightBlocks[lSFinal - 2]);
leftBlocks[lSFinal - 1] = leftBlocks[lSFinal - 2]
.nextBlock(data, psiGround,
cumulativeTruncationError);
};
std::cout << "iDMRG average truncation error: "
<< cumulativeTruncationError / (lSys - lSys / 2 - 1)
<< std::endl; // handles both even and odd system sizes
if(completeED || nSweeps == 0)
psiGround = randomSeed(leftBlocks[lSFinal - 1],
rightBlocks[lEFinal - 1]);
else
{
std::cout << "Performing fDMRG..." << std::endl;
data.infiniteStage = false;
int endSweep = lSys - 4 - skips; // last site of sweep
psiGround = randomSeed(leftBlocks[lSFinal - 1],
rightBlocks[lEFinal - 1]);
for(int sweep = 1; sweep <= nSweeps; sweep++)
// perform the fDMRG sweeps
{
data.compBlock = rightBlocksStart + (lEFinal - 1);
data.lancTolerance = groundStateErrorTolerances[sweep]
* groundStateErrorTolerances[sweep] / 2;
data.beforeCompBlock = data.compBlock - 1;
cumulativeTruncationError = 0.;
for(int site = lSFinal - 1; site < endSweep;
site++, data.compBlock--, data.beforeCompBlock--)
leftBlocks[site + 1] = leftBlocks[site]
.nextBlock(data, psiGround,
cumulativeTruncationError);
reflectPredictedPsi(psiGround, leftBlocks[endSweep],
rightBlocks[skips]);
// reflect the system to reverse sweep direction
data.compBlock = &leftBlocks[endSweep];
data.beforeCompBlock = data.compBlock - 1;
for(int site = skips; site < endSweep;
site++, data.compBlock--, data.beforeCompBlock--)
rightBlocks[site + 1] = rightBlocks[site]
.nextBlock(data, psiGround,
cumulativeTruncationError);
reflectPredictedPsi(psiGround, rightBlocks[endSweep],
leftBlocks[skips]);
data.compBlock = rightBlocksStart + endSweep;
data.beforeCompBlock = data.compBlock - 1;
for(int site = skips, end = lSFinal - 1; site < end;
site++, data.compBlock--, data.beforeCompBlock--)
leftBlocks[site + 1] = leftBlocks[site]
.nextBlock(data, psiGround,
cumulativeTruncationError);
std::cout << "Sweep " << sweep
<< " complete. Average truncation error: "
<< cumulativeTruncationError / (2 * lSys - 4)
<< std::endl;
};
};
data.compBlock = rightBlocksStart + (lEFinal - 1);
data.infiniteStage = false;
FinalSuperblock hSuperFinal
= leftBlocks[lSFinal - 1].createHSuperFinal(data, psiGround, skips);
// calculate ground-state energy
fileout << "Ground state energy density = "
<< hSuperFinal.gsEnergy / lSys << std::endl << std::endl;
if(calcObservables)
{
std::cout << "Calculating observables..." << std::endl;
VectorXd oneSiteVals;
MatrixXd twoSiteVals;
if(calcOneSiteExpValues) // calculate one-site expectation values
oneSiteVals = oneSiteExpValues(oneSiteOp, rangeOfObservables,
lSys, hSuperFinal, leftBlocks,
rightBlocks, fileout);
if(calcTwoSiteExpValues) // calculate two-site expectation values
twoSiteVals = twoSiteExpValues(firstTwoSiteOp, secondTwoSiteOp,
rangeOfObservables, lSys,
hSuperFinal, leftBlocks,
rightBlocks, fileout);
if(calcOneSiteExpValues && calcTwoSiteExpValues)
{
MatrixXd connectedCorrFunc
= twoSiteVals - oneSiteVals * oneSiteVals.transpose();
for(int i = 0, end = rangeOfObservables * rangeOfObservables;
i < end; i++)
if(std::abs(connectedCorrFunc(i)) < observableThreshold)
connectedCorrFunc(i) = 0.;
fileout << "Connected correlation function:\n"
<< connectedCorrFunc << std::endl << std::endl;
};
};
std::cout << std::endl;
clock_t stopTrial = clock();
fileout << "Elapsed time: "
<< float(stopTrial - startTrial)/CLOCKS_PER_SEC << " s"
<< std::endl;
fileout.close();
};
filein.close();
clock_t stop = clock();
std::cout << "Done. Elapsed time: " << float(stop - start)/CLOCKS_PER_SEC
<< " s" << std::endl;
return 0;
}