summaryrefslogtreecommitdiff
path: root/hmm_correlation.cpp
blob: 522367fd389e1b06288dec57869fa54e4497bba1 (plain)
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

#include "hadamard_mcmc.hpp"
#include "quantity.hpp"
#include "matrices.hpp"

#include <chrono>
#include <fstream>
#include <iostream>

std::string getFilename(unsigned n, double β, double θ₀, unsigned m, unsigned lag, unsigned skip) {
  return "correlation_" + std::to_string(n) + "_" + std::to_string(β) + "_" +
    std::to_string(θ₀) + "_" + std::to_string(m) + "_" + std::to_string(lag) + "_" + std::to_string(skip)
    + ".dat";
}

class MeasureCorrelation : public Measurement {
public:
  Quantity<Orthogonal> Q;

  MeasureCorrelation(unsigned lag, unsigned skip) : Q(lag, skip) {}

  void after_sweep(double, double E, const Orthogonal& M) override {
    Q.add(M);
  }
};

int main(int argc, char* argv[]) {
  // model parameters
  unsigned n = 2; // matrix size over four

  // simulation settings
  double β = 1; // temperature
  unsigned m = 1e4; // number of relaxation sweeps
  unsigned N = 1e4; // number of measurement sweeps
  double θ₀ = 0.05; // standard deviation of step size

  // measurement settings
  unsigned l = 1e3; // lag in autocorrelation function
  unsigned s = 1; // skip in autocorrelation function

  bool loadDataFromFile = false;

  int opt;

  while ((opt = getopt(argc, argv, "n:b:m:N:l:t:i:Ls:")) != -1) {
    switch (opt) {
    case 'n':
      n = atoi(optarg);
      break;
    case 'b':
      β = atof(optarg);
      break;
    case 'm':
      m = (unsigned)atof(optarg);
      break;
    case 'N':
      N = (unsigned)atof(optarg);
      break;
    case 'l':
      l = (unsigned)atof(optarg);
      break;
    case 's':
      s = (unsigned)atof(optarg);
      break;
    case 't':
      θ₀ = atof(optarg);
      break;
    case 'L':
      loadDataFromFile = true;
      break;
    default:
      exit(1);
    }
  }

  MeasureCorrelation Q(l, s);
  MCMC simulation(n, β, Q, θ₀);

  if (loadDataFromFile) {
    Q.Q.read(getFilename(n, β, θ₀, m, l, s));
    std::cout << "Imported data from file, number of steps " << Q.Q.num_added() << "." << std::endl;
  }

  simulation.run(m, true);
  std::cout << "Finished initial relaxation at " << β << "." << std::endl;
  simulation.run(N);

  std::cout << "Finished simulation of " << β << ", writing output file." << std::endl;
  std::string filename = getFilename(n, β, θ₀, m, l, s);
  Q.Q.write(filename);

  return 0;
}