summaryrefslogtreecommitdiff
path: root/ising.cpp
blob: ca2e2e746959999e14fa1e3ed3e9aed0ab3d1ba4 (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
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

#include "space_wolff.hpp"

std::function<double(spin<signed, 2, signed>)> B_sin(unsigned L, unsigned n, double H) {
  return [n, H, L] (spin<signed, 2, signed> s) -> double {
    return H * cos(2 * M_PI * n * s.x[0] / ((double)L));
  };
}

int main(int argc, char* argv[]) {
  const unsigned D = 2;

  unsigned L = 32;
  unsigned N = 1000;
  unsigned mod = 0;
  unsigned multi = 1e4;
  double T = 2.0 / log(1.0 + sqrt(2.0));
  double H = 1.0;
  double ε = 0.1;

  int opt;

  while ((opt = getopt(argc, argv, "N:L:T:H:e:m:M")) != -1) {
    switch (opt) {
      case 'N': 
        N = (unsigned)atof(optarg);
        break;
      case 'L':
        L = atoi(optarg);
        break;
      case 'T':
        T = atof(optarg);
        break;
      case 'H':
        H = atof(optarg);
        break;
      case 'e':
        ε = atof(optarg);
        break;
      case 'm':
        mod = atoi(optarg);
        break;
      case 'M':
        multi = atoi(optarg);
        break;
      default:
        exit(1);
    }
  }

  std::function<double(spin<signed, D, signed>, spin<signed, D, signed>)> Z =
    [] (spin<signed, D, signed> s1, spin<signed, D, signed> s2) -> double {
      bool one_one = false;
      bool many_ones = false;
      bool any_two = false;
      
      for (unsigned i = 0; i < D; i++) {
        unsigned diff = abs(s1.x(i) - s2.x(i));
        if (diff == 1 && !one_one) {
          one_one = true;
        } else if (diff == 1 && one_one) {
          many_ones = true;
          break;
        } else if (diff > 1) {
          any_two = true;
          break;
        }
      }

      if (!one_one && !any_two) {
        return -std::numeric_limits<double>::infinity();
      } else if (one_one && !many_ones && !any_two) {
        return s1.s * s2.s;
      } else {
        return 0;
      }
    };

  std::function<double(spin<signed, D, signed>)> B_face =
    [L, H] (spin<signed, D, signed> s) -> double {
      return H * s.s * smiley[s.x(1) * 16 / L][s.x(0) * 16 / L];
    };

  std::function<double(spin<signed, D, signed>)> B;

  if (mod > 0) {
    B = B_sin(L, mod, H);
  } else {
    B = B_face;
  }

  std::function<std::set<unsigned>(model<signed, D, signed>&, unsigned, spin<signed, D, signed>)> neighbors =
    [] (model<signed, D, signed>& m, unsigned i0, spin<signed, D, signed> s1) -> std::set<unsigned> {
      std::set<unsigned> nn;
      if (i0 < m.s.size()) {
        std::set<unsigned> nn0 = m.dict.neighbors(m.s[i0].x, 1);
        std::set<unsigned> nn1 = m.dict.neighbors(s1.x, 1);
        nn.insert(nn0.begin(), nn0.end());
        nn.insert(nn1.begin(), nn1.end());
        nn.insert(m.s.size());
      } else {
        for (unsigned i = 0; i < m.s.size(); i++) {
          nn.insert(i);
        }
      }
      return nn;
    };

  model<signed, D, signed> ising(L, Z, B, neighbors);

  randutils::auto_seed_128 seeds;
  std::mt19937 rng{seeds};

  std::uniform_int_distribution<unsigned> coin(0, 1);

  unsigned n = 0;
  unsigned up = 0;
  unsigned down = 0;
  for (unsigned i = 0; i < L; i++) {
    for (unsigned j = 0; j < L; j++) {
      if ((coin(rng) && up < pow(L, 2) / 2) || down >= pow(L, 2) / 2) {
        ising.s.push_back({{i, j}, 1});
        up++;
      } else {
        ising.s.push_back({{i, j}, -1});
        down++;
      }
      ising.dict.record<signed>({i, j}, n);
      n++;
    }
  }
  /*
  for (unsigned i = 0; i < L; i++) {
    for (unsigned j = 0; j < L; j++) {
      if (i < L / 2) {
        ising.s.push_back({{i, j}, 1});
      } else {
        ising.s.push_back({{i, j}, -1});
      }
      ising.dict.record<signed>({i, j}, n);
      n++;
    }
  }
  */

  ising.update_energy();

  while (true) {
    ising.wolff(T, N, rng);
    std::array<double, 2> τ = ising.Eq.τ();
    std::cout << ising.Eq.num_added() << " " << τ[0] << " " << τ[1] << " " << τ[1] / τ[0] << "\n";
    if (τ[1] / τ[0] < ε && τ[0] * multi < ising.Eq.num_added()) {
      break;
    }
  }

  std::vector<signed> output(pow(L, D));

  for (spin<signed, D, signed> s : ising.s) {
    spin<signed, D, signed> rs = ising.s0.inverse().act(s);
    output[L * rs.x(1) + rs.x(0)] = s.s;
  }

  std::ofstream outfile;
  outfile.open("out.dat", std::ios::app);

  std::array<double, 2> act = ising.Eq.τ();
  std::vector<double> ρ = ising.Eq.ρ();

  outfile << L << " " << T << " " << mod << " " << H << " " << ising.Eq.num_added() << " " << ising.Cq.avg() << " " << ising.Cq.serr() << " " << act[0] << " " << act[1];
  for (double ρi : ρ) {
    outfile << " " << ρi;
  }
  outfile << "\n";

  return 0;
}