-
Notifications
You must be signed in to change notification settings - Fork 3
/
maindeteval.cpp
480 lines (417 loc) · 18 KB
/
maindeteval.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. See the enclosed file LICENSE for a copy or if
* that was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright 2017 Max H. Gerlach
*
* */
#if defined (MAX_DEBUG) && ! defined(DUMA_NO_DUMA)
#include "dumapp.h"
#endif
/*
* deteval.cpp
*
* Created on: Apr 29, 2013
* Author: gerlach
*/
// Evaluate time series generated by detqmc.
// Call in directory containing timeseries files.
#include <iostream>
#include <algorithm>
#include <iterator>
#include <functional>
#include <memory>
#include <map>
#include <cmath>
#include <vector>
#include <string>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wshadow"
#include "boost/program_options.hpp"
#include "boost/filesystem.hpp"
#pragma GCC diagnostic pop
#include "git-revision.h"
#include "tools.h" //glob
#include "dataseriesloader.h"
#include "datamapwriter.h"
#include "metadata.h"
#include "exceptions.h"
#include "statistics.h"
namespace {
// what to do
uint32_t discard = 0;
uint32_t read_maximally = 0;
uint32_t subsample_interval = 1;
uint32_t jkBlocks = 1;
bool notau = false;
bool noexp = false;
bool reweight = false;
num original_r = 0;
num reweight_to_this_r = 0;
std::vector<std::string> noncollect_observables; // do not process these observables
//Store averages / nonlinear estimates, jackknife errors,
//integrated autocorrelation times here
//key: observable name
typedef std::map<std::string, double> ObsValMap;
ObsValMap estimates, errors, tauints;
//jackknife-block wise estimates
typedef std::map<std::string, std::vector<double>> ObsVecMap;
ObsVecMap jkBlockEstimates;
uint32_t evalSamples = 0;
uint32_t guessedLength = 0;
//metadata necessary for the computation of the susceptibility
// spatial system size, and number of imaginary time slices
uint32_t L;
uint32_t N;
uint32_t m;
double dtau;
// for reweighting the other timeseries: need to store a function
// of the timeseries of associatedEnergy
std::shared_ptr<std::vector<num>> reweightingFactors;
};
MetadataMap readAndCleanMetadata(const std::string& filename) {
//take simulation metadata from file info.dat, remove some unnecessary parts
MetadataMap meta = readOnlyMetadata(filename);
std::string keys[] = {"buildDate", "buildHost", "buildTime",
"cppflags", "cxxflags", "gitBranch", "gitRevisionHash",
"sweepsDone", "sweepsDoneThermalization", "totalWallTimeSecs"};
for ( std::string key : keys) {
if (meta.count(key)) {
meta.erase(key);
}
}
return meta;
}
void prepareReweightingFactors() {
// we need the associatedEnergy time series to be able to do reweighting
DoubleSeriesLoader associatedEnergyReader;
associatedEnergyReader.readFromFile("associatedEnergy.series", subsample_interval,
discard, read_maximally, guessedLength);
std::shared_ptr<std::vector<num>> associatedEnergyData = associatedEnergyReader.getData();
reweightingFactors.reset(new std::vector<num>());
reweightingFactors->reserve(associatedEnergyData->size());
for (num e : *associatedEnergyData) {
// the data read in has been normalized by system size -- correct this effect:
e *= (dtau * m * N);
reweightingFactors->push_back( std::exp(-(reweight_to_this_r - original_r) * e) );
}
}
void processTimeseries(const std::string& filename) {
std::cout << "Processing " << filename << ", ";
DoubleSeriesLoader reader;
reader.readFromFile(filename, subsample_interval, discard, read_maximally, guessedLength);
int reader_cols = reader.getColumns();
if (reader_cols == 0) {
// time series is empty
std::cout << "Time series " + filename + " is empty, skip" << std::endl;
return;
}
else if (reader_cols != 1) {
throw_GeneralError("File " + filename + " does not have exactly 1 column, but " + numToString(reader_cols));
}
std::shared_ptr<std::vector<double>> data = reader.getData();
std::string obsName;
reader.getMeta("observable", obsName);
std::cout << "observable: " << obsName << "...";
if (std::any_of(std::begin(noncollect_observables),
std::end(noncollect_observables),
[&](const std::string& nc_obs) { return nc_obs == obsName; })) {
std::cout << " skip" << std::endl;
return;
}
if (reweight) {
std::cout << " [reweighting from r=" << original_r
<< " to r=" << reweight_to_this_r << "] ...";
}
std::cout << std::flush;
if (not noexp) {
using std::pow;
auto average_func_maybe_reweight = [&]( const std::function<double(double)>& func ) -> double {
if (reweight) {
return average<double>( func, *data, *reweightingFactors );
} else {
return average<double>( func, *data );
}
};
auto average_maybe_reweight = [&]( ) -> double {
if (reweight) {
return average<double>( *data, *reweightingFactors );
} else {
return average<double>( *data );
}
};
auto jackknifeBlockEstimates_func_maybe_reweight = [&]( const std::function<double(double)>& func ) -> std::vector<double> {
if (reweight) {
return jackknifeBlockEstimates<double>( func, *data, *reweightingFactors, jkBlocks );
} else {
return jackknifeBlockEstimates<double>( func, *data, jkBlocks );
}
};
auto jackknifeBlockEstimates_maybe_reweight = [&]( ) -> std::vector<double> {
if (reweight) {
return jackknifeBlockEstimates<double>( *data, *reweightingFactors, jkBlocks );
} else {
return jackknifeBlockEstimates<double>( *data, jkBlocks );
}
};
estimates[obsName] = average_maybe_reweight();
jkBlockEstimates[obsName] = jackknifeBlockEstimates_maybe_reweight();
// compute Binder cumulant and susceptibility (<.^2> - <.>^2);
// part susceptibility: <.^2>
if (obsName == "normMeanPhi") {
estimates["normMeanPhiSquared"] = average_func_maybe_reweight(
[](double v) { return pow(v, 2); } );
jkBlockEstimates["normMeanPhiSquared"] = jackknifeBlockEstimates_func_maybe_reweight(
[](double v) { return pow(v, 2); } );
estimates["normMeanPhiFourth"] = average_func_maybe_reweight(
[](double v) { return pow(v, 4); } );
jkBlockEstimates["normMeanPhiFourth"] = jackknifeBlockEstimates_func_maybe_reweight(
[](double v) { return pow(v, 4); } );
estimates["phiBinder"] = 1.0 - (3.0*estimates["normMeanPhiFourth"]) /
(5.0*pow(estimates["normMeanPhiSquared"], 2));
jkBlockEstimates["phiBinder"] = std::vector<double>(jkBlocks, 0);
for (uint32_t jb = 0; jb < jkBlocks; ++jb) {
jkBlockEstimates["phiBinder"][jb] =
1.0 - (3.0*jkBlockEstimates["normMeanPhiFourth"][jb]) /
(5.0*pow(jkBlockEstimates["normMeanPhiSquared"][jb], 2));
}
estimates["phiBinderRatio"] = estimates["normMeanPhiFourth"] /
pow(estimates["normMeanPhiSquared"], 2);
jkBlockEstimates["phiBinderRatio"] = std::vector<double>(jkBlocks, 0);
for (uint32_t jb = 0; jb < jkBlocks; ++jb) {
jkBlockEstimates["phiBinderRatio"][jb] =
jkBlockEstimates["normMeanPhiFourth"][jb] /
pow(jkBlockEstimates["normMeanPhiSquared"][jb], 2);
}
estimates["phiSusceptibilityPart"] = (dtau * m * N) *
estimates["normMeanPhiSquared"];
jkBlockEstimates["phiSusceptibilityPart"] = std::vector<double>(jkBlocks, 0);
for (uint32_t jb = 0; jb < jkBlocks; ++jb) {
jkBlockEstimates["phiSusceptibilityPart"][jb] = (dtau * m * N) *
jkBlockEstimates["normMeanPhiSquared"][jb];
}
estimates["phiSusceptibility"] = (dtau * m * N) * (
estimates["normMeanPhiSquared"] -
pow(estimates["normMeanPhi"], 2)
);
jkBlockEstimates["phiSusceptibility"] = std::vector<double>(jkBlocks, 0);
for (uint32_t jb = 0; jb < jkBlocks; ++jb) {
jkBlockEstimates["phiSusceptibility"][jb] = (dtau * m * N) * (
jkBlockEstimates["normMeanPhiSquared"][jb] -
pow(jkBlockEstimates["normMeanPhi"][jb], 2)
);
}
}
// experimental: compute a Binder parameter for the energy
if (obsName == "associatedEnergy") {
auto eSquared = average_func_maybe_reweight(
[](double v) { return pow(v, 2); } );
auto jbe_eSquared = jackknifeBlockEstimates_func_maybe_reweight(
[](double v) { return pow(v, 2); } );
auto eForth = average_func_maybe_reweight(
[](double v) { return pow(v, 4); } );
auto jbe_eForth = jackknifeBlockEstimates_func_maybe_reweight(
[](double v) { return pow(v, 4); } );
estimates["energyBinder"] = 1.0 - (3.0*eForth) /
(5.0*pow(eSquared, 2));
jkBlockEstimates["energyBinder"] = std::vector<double>(jkBlocks, 0);
for (uint32_t jb = 0; jb < jkBlocks; ++jb) {
jkBlockEstimates["energyBinder"][jb] =
1.0 - (3.0*jbe_eForth[jb]) /
(5.0*pow(jbe_eSquared[jb], 2));
}
}
// also compute bosonic spin stiffness, if the data is present
// rhoS = (beta / L**2) * ( <Gc> + <Gs>**2 + - <Gs**2> )
// ==> need to compute <Gs**2>
if (obsName == "phiRhoS_Gs") {
estimates["phiRhoS_Gs_squared"] = average_func_maybe_reweight(
[](double v) { return pow(v, 2); } );
jkBlockEstimates["phiRhoS_Gs_squared"] = jackknifeBlockEstimates_func_maybe_reweight(
[](double v) { return pow(v, 2); } );
}
}
// std::copy(std::begin(jkBlockEstimates[obsName]), std::end(jkBlockEstimates[obsName]), std::ostream_iterator<double>(std::cout, " "));
// std::cout << std::endl;
// std::cout << average(jkBlockEstimates[obsName]);
if (not notau) {
tauints[obsName] = tauint(*data);
}
evalSamples = static_cast<uint32_t>(data->size());
std::cout << std::endl;
}
void evaluateCombinedQuantities() {
using std::pow;
// also compute bosonic spin stiffness, if the data is present
// rhoS = (1. / (L**2 beta)) * ( <Gc> + <Gs>**2 - <Gs**2> )
if (estimates.count("phiRhoS_Gs") and estimates.count("phiRhoS_Gc")) {
assert(estimates.count("phiRhoS_Gs_squared"));
estimates["phiRhoS"] = (1.0 / ((dtau * m) * N)) *
( pow(estimates["phiRhoS_Gs"], 2)
+ estimates["phiRhoS_Gc"]
- estimates["phiRhoS_Gs_squared"]);
jkBlockEstimates["phiRhoS"] = std::vector<double>(jkBlocks, 0);
for (uint32_t jb = 0; jb < jkBlocks; ++jb) {
jkBlockEstimates["phiRhoS"][jb] = (1.0 / ((dtau * m) * N)) *
( pow(jkBlockEstimates["phiRhoS_Gs"][jb], 2)
+ jkBlockEstimates["phiRhoS_Gc"][jb]
- jkBlockEstimates["phiRhoS_Gs_squared"][jb]);
}
}
}
void jackknifeEvaluation() {
for (auto const& nameBlockPair : jkBlockEstimates) {
const std::string obsName = nameBlockPair.first;
const std::vector<double> blockEstimates = nameBlockPair.second;
errors[obsName] = jackknife(blockEstimates, estimates[obsName]);
}
}
std::string get_results_filename() {
std::string filename_insert = (reweight ? "-reweighted-r" + numToString(reweight_to_this_r) : "");
return "eval-results" + filename_insert + ".values";
}
void removeOldResultsFile() {
std::string fname = get_results_filename();
if(boost::filesystem::exists(fname)) {
boost::filesystem::remove(fname);
}
}
void writeoutResults(MetadataMap meta) {
if (estimates.empty()) {
// nothing to write out, create no file
return;
}
StringDoubleMapWriter resultsWriter;
if (reweight) {
meta["r"] = numToString(reweight_to_this_r);
meta["original-r"] = numToString(original_r);
}
resultsWriter.addMetadataMap(meta);
resultsWriter.addMeta("eval-jackknife-blocks", jkBlocks);
resultsWriter.addMeta("eval-discard", discard);
resultsWriter.addMeta("eval-read", read_maximally);
resultsWriter.addMeta("eval-subsample", subsample_interval);
resultsWriter.addMeta("eval-samples", evalSamples);
if (reweight) {
resultsWriter.addMeta("eval-reweighted-to-r", reweight_to_this_r);
resultsWriter.addMeta("eval-original-r", original_r);
resultsWriter.addHeaderText("Time series were reweighted");
}
if (jkBlocks > 1) {
resultsWriter.addHeaderText("Averages and jackknife error bars computed from time series");
resultsWriter.setData(std::make_shared<ObsValMap>(estimates));
resultsWriter.setErrors(std::make_shared<ObsValMap>(errors));
} else {
resultsWriter.addHeaderText("Averages computed from time series");
resultsWriter.setData(std::make_shared<ObsValMap>(estimates));
}
resultsWriter.writeToFile(get_results_filename());
}
std::string get_tauint_filename() {
return "eval-tauint.values";
}
void removeOldTauintFile() {
std::string fname = get_tauint_filename();
if(boost::filesystem::exists(fname)) {
boost::filesystem::remove(fname);
}
}
void writeoutTauints(MetadataMap meta) {
if (tauints.empty()) {
// nothing to write out, create no file
return;
}
StringDoubleMapWriter tauintWriter;
tauintWriter.addMetadataMap(meta);
tauintWriter.addMeta("eval-discard", discard);
tauintWriter.addMeta("eval-read", read_maximally);
tauintWriter.addMeta("eval-subsample", subsample_interval);
tauintWriter.addMeta("eval-samples", evalSamples);
tauintWriter.addHeaderText("Tauint estimates computed from time series");
tauintWriter.setData(std::make_shared<ObsValMap>(tauints));
tauintWriter.writeToFile(get_tauint_filename());
}
int main(int argc, char **argv) {
//parse command line options
namespace po = boost::program_options;
po::options_description evalOptions("Time series evaluation options");
evalOptions.add_options()
("help", "print help on allowed options and exit")
("version,v", "print version information (git hash, build date) and exit")
("discard,d", po::value<uint32_t>(&discard)->default_value(0),
"number of initial time series entries to discard (additional thermalization)")
("read,r", po::value<uint32_t>(&read_maximally)->default_value(0),
"maximum number of time series entries to read (after discarded initial samples, before subsampling). Default value of 0: read all entries")
("subsample,s", po::value<uint32_t>(&subsample_interval)->default_value(1),
"take only every s'th sample into account")
("jkblocks,j", po::value<uint32_t>(&jkBlocks)->default_value(1),
"number of jackknife blocks to use")
("notau", po::bool_switch(¬au)->default_value(false),
"switch off estimation of integrated autocorrelation times")
("noexp", po::bool_switch(&noexp)->default_value(false),
"switch off estimation of expectation values and errorbars")
("reweight", po::value<double>(&reweight_to_this_r), "reweight timeseries to a new value of parameter r (SDW-model) "
"[will not affect tauint]")
("noncollect,n", po::value<std::vector<std::string>>(&noncollect_observables)->multitoken(),
"do not process these observables")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, evalOptions), vm);
po::notify(vm);
bool earlyExit = false;
if (vm.count("help")) {
std::cout << "Evaluate time series generated by detqmc. Call in directory containing timeseries files.\n"
<< "Will write results to files eval-results.values and eval-tauint.values\n\n"
<< evalOptions << std::endl;
earlyExit = true;
}
if (vm.count("version")) {
std::cout << "Build info:\n"
<< metadataToString(collectVersionInfo())
<< std::endl;
earlyExit = true;
}
if (vm.count("reweight")) {
reweight = true;
// reweight_to_this_r has been set to the argument
}
if (earlyExit) {
return 0;
}
MetadataMap meta = readAndCleanMetadata("info.dat");
guessedLength = static_cast<uint32_t>(fromString<double>(meta.at("sweeps")) /
fromString<double>(meta.at("measureInterval")));
L = fromString<uint32_t>(meta.at("L"));
N = L*L;
m = fromString<uint32_t>(meta.at("m"));
dtau = fromString<double>(meta.at("dtau"));
original_r = fromString<double>(meta.at("r"));
if (reweight) {
prepareReweightingFactors();
}
//process time series files
std::vector<std::string> filenames = glob("*.series");
for (std::string fn : filenames) {
processTimeseries(fn);
}
//maybe compute bosonic spin stiffness
if (not noexp) {
evaluateCombinedQuantities();
}
//calculate error bars from jackknife block estimates
if (not noexp and jkBlocks > 1) {
jackknifeEvaluation();
}
if (not noexp) {
removeOldResultsFile();
writeoutResults(meta);
}
if (not notau) {
removeOldTauintFile();
writeoutTauints(meta);
}
std::cout << "Done!" << std::endl;
return 0;
}