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
|
#include "hadamard_pt.hpp"
#include "matrices.hpp"
#include <chrono>
#include <fstream>
#include <iostream>
class MeasureEnergy : public Measurement {
public:
unsigned N;
double totalE;
double totalE2;
unsigned n;
std::vector<unsigned> ρ_dist;
MeasureEnergy(unsigned n_bins = 1e4) : ρ_dist(n_bins + 1, 0) {
n = n_bins;
N = 0;
totalE = 0;
totalE2 = 0;
}
void after_sweep(double, double E, const Orthogonal& M) override {
N++;
totalE += E;
totalE2 += pow(E, 2);
double max = sqrt(M.size());
for (unsigned i = 0; i < M.size(); i++) {
for (unsigned j = 0; j < M.size(); j++) {
ρ_dist[n * (M(i, j) + max) / (2 * max)]++;
}
}
}
double energy() const { return totalE / N; }
double specific_heat() const { return totalE2 / N - pow(totalE / N, 2); }
};
class MeasureTransitionRates : public ParallelMeasurement {
public:
std::vector<unsigned> nAccepted;
std::vector<unsigned> total_steps;
MeasureTransitionRates(unsigned n) : nAccepted(n - 1, 0), total_steps(n - 1, 0) {}
void after_step(bool accepted, unsigned i, double, double, const MCMC&, const MCMC&) override {
total_steps[i]++;
if (accepted)
nAccepted[i]++;
}
};
int main(int argc, char* argv[]) {
unsigned n_tuning = 1e2;
double β₀ = 0.1;
double β₁ = 10;
unsigned N = 16;
unsigned k = 2;
double ε = 0.01;
double ε2 = 0.01;
unsigned M = 10;
unsigned m = 1e4;
int opt;
while ((opt = getopt(argc, argv, "k:b:c:n:t:N:M:e:m:f:")) != -1) {
switch (opt) {
case 'k':
k = atoi(optarg);
if (k == 0 || k > 8) {
std::cout << "The size k must be an integer from 1 to 8!" << std::endl;
exit(1);
}
break;
case 'b':
β₀ = atof(optarg);
break;
case 'c':
β₁ = atof(optarg);
break;
case 'e':
ε = atof(optarg);
break;
case 'f':
ε2 = atof(optarg);
break;
case 'n':
m = (unsigned)atof(optarg);
break;
case 't':
n_tuning = (unsigned)atof(optarg);
break;
case 'N':
N = (unsigned)atof(optarg);
break;
case 'M':
M = (unsigned)atof(optarg);
break;
default:
exit(1);
}
}
unsigned n = 4 * k;
std::vector<Measurement*> As(N);
for (Measurement*& A : As) {
A = new MeasureEnergy();
}
MeasureTransitionRates B(N);
PT p(β₀, β₁, N, n, B, As);
for (MCMC& sim : p.Ms) {
sim.M = hadamards[k - 1];
sim.E = sim.M.energy();
}
std::vector<double> f;
std::cout << "Beginning simulation of " << n << ".\n";
bool still_tuning = true;
while (still_tuning) {
std::cout << "Beginning " << n_tuning << " tuning tempering updates of " << M
<< " sweeps each.\n";
f = p.tune(n_tuning, M, ε, ε2);
std::cout << "Finished tuning, beginning " << m << " measurement tempering updates of " << M
<< " sweeps each.\n";
std::cout << "βs: ";
for (const MCMC& M : p.Ms) {
std::cout << M.β << " ";
}
std::cout << std::endl;
std::cout << "Accept tuning as finished? (y/N): ";
char answer = getchar();
if (answer == 'y') {
still_tuning = false;
}
ε /= 2;
ε2 /= 2;
}
std::cout << "Running " << m << " PT swaps of " << M << " sweeps each.";
p.run(m, M);
std::cout << "Finished " << n << ".\n";
auto tag = std::chrono::high_resolution_clock::now();
std::string filename = "hmm_" + std::to_string(n) + "_" + std::to_string(β₀) + "_" +
std::to_string(β₁) + "_" + std::to_string(N) + "_" +
std::to_string(tag.time_since_epoch().count()) + ".dat";
std::ofstream file(filename);
for (const MCMC& M : p.Ms) {
file << M.β << " ";
}
file << std::endl;
for (double ff : f) {
file << ff << " ";
}
file << std::endl;
for (unsigned i = 0; i < B.nAccepted.size(); i++) {
file << std::fixed << B.nAccepted[i] / (double)B.total_steps[i] << " ";
}
file << std::endl;
for (unsigned i = 0; i < As.size(); i++) {
file << std::fixed << ((MeasureEnergy*)As[i])->energy() << " ";
}
file << std::endl;
for (unsigned i = 0; i < As.size(); i++) {
file << std::fixed << ((MeasureEnergy*)As[i])->specific_heat() << " ";
}
file << std::endl;
for (unsigned i = 0; i < As.size(); i++) {
for (unsigned j = 0; j < ((MeasureEnergy*)As[i])->ρ_dist.size(); j++) {
file << std::fixed << ((MeasureEnergy*)As[i])->ρ_dist[j] << " ";
}
file << std::endl;
}
file.close();
return 0;
}
|