summaryrefslogtreecommitdiff
path: root/src/new_model.cpp
blob: 96789d179f2ffe60bc6c30bcffd985447b91956b (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
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

#include <getopt.h>
#include <iostream>

#include "../include/randutils/randutils.hpp"

#include <functional>
#include <vector>
#include <queue>

#include <wolff/types.h>
#include <wolff/graph.hpp>

using namespace wolff;

namespace ising {
#define WOLFF_SITE_DEPENDENCE
#include <wolff.hpp>
#include <wolff/models/ising.hpp>
#undef WOLFF_SITE_DEPEDENCE
}

namespace xy {
#undef WOLFF_H
#undef WOLFF_SYSTEM_H
#undef WOLFF_CLUSTER_H
#undef WOLFF_MEASUREMENTS_H

#define WOLFF_NO_FIELD
#define WOLFF_BOND_DEPENDENCE
#include <wolff.hpp>
#include <wolff/models/vector.hpp>
#include <wolff/models/orthogonal.hpp>
#undef WOLFF_NO_FIELD
#undef WOLFF_BOND_DEPENDENCE
}

double E;

using ising::wolff::ising_t;
using xy::wolff::vector_t;
using xy::wolff::orthogonal_t;

class i_measurement : public ising::wolff::measurement<ising_t, ising_t> {
  private:
    N_t nc;
    N_t ns;

    int M;
    int A;
    v_t C;

    double totalaA;
    double totalA2;
    double totalaM;
    double totalM2;
    double totalC;
    double totalC2;

  public:
    i_measurement(const ising::wolff::system<ising_t, ising_t>& S) {
      nc = 0;
      ns = 0;
      M = 0;
      A = S.nv;

      totalaA = 0;
      totalA2 = 0;
      totalaM = 0;
      totalM2 = 0;
      totalC = 0;
      totalC2 = 0;
    }

    void pre_cluster(N_t, N_t, const ising::wolff::system<ising_t, ising_t>&, v_t, const ising_t&) override {
      C = 0;
    }

    void plain_bond_visited(const ising::wolff::system<ising_t, ising_t>&, v_t, const ising_t&, v_t, double dE) override {
      E += dE;
    }

    void ghost_bond_visited(const ising::wolff::system<ising_t, ising_t>& S, v_t i, const ising_t& s_old, const ising_t& s_new, double dE) override {
      E += dE;
      M += s_new - s_old;

      if (i >= S.nv / 2) {
        A -= s_new - s_old;
      } else {
        A += s_new - s_old;
      }
    }

    void plain_site_transformed(const ising::wolff::system<ising_t, ising_t>& S, v_t i, const ising_t& si_new) override {
      C++;
    }

    void post_cluster(N_t, N_t, const ising::wolff::system<ising_t, ising_t>&) override {
      totalaA += abs(A);
      totalA2 += pow(A, 2);
      totalaM += abs(M);
      totalM2 += pow(M, 2);
      totalC += C;
      totalC2 += pow(C, 2);

      nc++;

      ns++;
    }

    double avgaA() {
      return totalaA / ns;
    }

    double avgA2() {
      return totalA2 / ns;
    }

    double avgaM() {
      return totalaM / ns;
    }

    double avgM2() {
      return totalM2 / ns;
    }

    double avgC() {
      return totalC / nc;
    }

    double avgC2() {
      return totalC2 / nc;
    }
};

class x_measurement : public xy::wolff::measurement<orthogonal_t<2, double>, vector_t<2, double>> {
  private:
    N_t ns;
    N_t nc;
    orthogonal_t<2, double> R;

    vector_t<2, double> M;
    v_t C;
    vector_t<2, double> CM;

    vector_t<2, double> totalaM;
    vector_t<2, double> totalM2;
    double totalM;
    double totalCM;
    double totalC;
    double totalC2;

  public:
    x_measurement(const xy::wolff::system<orthogonal_t<2, double>, vector_t<2, double>>& S) {
      ns = 0;
      nc = 0;
      M = S.nv * S.s[0];

      totalaM.fill(0);
      totalM2.fill(0);
      totalC = 0;
      totalC2 = 0;
      totalM = 0;
      totalCM = 0;
    }

    void pre_cluster(N_t, N_t, const xy::wolff::system<orthogonal_t<2, double>, vector_t<2, double>>&, v_t, const orthogonal_t<2, double>& r) override {
      R = r;
      C = 0;
      CM.fill(0);
    }

    void plain_bond_visited(const xy::wolff::system<orthogonal_t<2, double>, vector_t<2, double>>&, v_t, const vector_t<2, double>&, v_t, double dE) override {
      E += dE;
    }

    void plain_site_transformed(const xy::wolff::system<orthogonal_t<2, double>, vector_t<2, double>>& S, v_t i, const vector_t<2, double>& si_new) override {
      C++;
      CM += S.s[i];
      M += si_new - S.s[i];
    }

    void post_cluster(N_t, N_t, const xy::wolff::system<orthogonal_t<2, double>, vector_t<2, double>>&) override {
      totalaM[0] += fabs(M[0]);
      totalaM[1] += fabs(M[1]);
      totalM2[0] += pow(M[0], 2); 
      totalM2[1] += pow(M[1], 2); 
      totalM += (pow(M[0], 2) + pow(M[1], 2));
      totalCM += pow(CM[0] * R[0][0] + CM[1] * R[0][1], 2) / C;
      totalC += C;
      totalC2 += pow(C, 2);

      nc++;

      ns++;
    }

    vector_t<2, double> avgaM() {
      return totalaM / ns;
    }

    vector_t<2, double> avgM2() {
      return totalM2 / ns;
    }

    double avgM() {
      return totalM / ns;
    }

    double avgCM() {
      return totalCM / ns;
    }

    double avgC() {
      return totalC / nc;
    }

    double avgC2() {
      return totalC2 / nc;
    }
};

using namespace wolff;

int main (int argc, char *argv[]) {

  // set defaults
  N_t N = (N_t)1e4;
  const D_t D = 2;
  L_t L = 128;
  double T = 2.26918531421;
  double J = 1.0;
  double a = 1.5;

  int opt;

  // take command line arguments
  while ((opt = getopt(argc, argv, "N:L:T:J:a:")) != -1) {
    switch (opt) {
    case 'N': // number of steps
      N = (N_t)atof(optarg);
      break;
    case 'L': // linear size
      L = atoi(optarg);
      break;
    case 'T': // temperature
      T = atof(optarg);
      break;
    case 'J': // temperature
      J = atof(optarg);
      break;
    case 'a': // temperature
      a = atof(optarg);
      break;
    default:
      exit(EXIT_FAILURE);
    }
  }

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

  std::function <double(const ising_t&, const ising_t&)> Zi = [=] (const ising_t& s1, const ising_t s2) -> double {
    if (s1.x == s2.x) {
      return -J;
    } else {
      return J;
    }
  };

  std::vector<vector_t<2, double>> *xy_spins;

  std::function <double(v_t, const ising_t&)> Bi = [L, D, a, &xy_spins] (v_t v, const ising_t& s) -> double {
    v_t xyv1 = v % (v_t)pow(L, D);
    v_t xyv2;
    if (v / (v_t)pow(L, D) == 0) {
      xyv2 = L * (xyv1 / L) + ((xyv1 % L) + 1) % L;
    } else {
      xyv2 = L * (((xyv1 / L) + 1) % L) + (xyv1 % L);
    }

    double value = a * ((xy_spins->at(xyv1)) * (xy_spins->at(xyv2)));

    if (s.x) {
      return -value;
    } else {
      return value;
    }
  };

  graph Gi;
  
  Gi.nv = 2 * pow(L, 2);
  Gi.ne = 2 * Gi.nv;

  Gi.adjacency.resize(Gi.nv);
  Gi.coordinate.resize(Gi.nv);

  for (std::vector<v_t> adj_i : Gi.adjacency) {
    adj_i.reserve(4);
  }

  for (D_t i = 0; i < 2; i++) {
    v_t sb = i * pow(L, 2);

    for (v_t j = 0; j < pow(L, 2); j++) {
      v_t vc = sb + j;

      Gi.adjacency[vc].push_back(((i + 1) % 2) * pow(L, 2) + j);
      Gi.adjacency[vc].push_back(((i + 1) % 2) * pow(L, 2) + pow(L, 2 - 1) * (j / (v_t)pow(L, 2 - 1)) + (j + 1 - 2 * (i % 2)) % L);
      Gi.adjacency[vc].push_back(((i + 1) % 2) * pow(L, 2) + pow(L, 2 - 1) * ((L + (j/ (v_t)pow(L, 2 - 1)) - 1 + 2 * (i % 2)) % L) + (j - i) % L);
      Gi.adjacency[vc].push_back(((i + 1) % 2) * pow(L, 2) + pow(L, 2 - 1) * ((L + (j/ (v_t)pow(L, 2 - 1)) - 1 + 2 * (i % 2)) % L) + (j + 1 - i) % L);
    }
  }
  
  ising::wolff::system<ising_t, ising_t> si(Gi, T, Zi, Bi);

  std::function <double(v_t, const vector_t<2, double>&, v_t, const vector_t<2, double>&)> Zxy = [L, D, a, &si] (v_t v1, const vector_t<2, double>& s1, v_t v2, const vector_t<2, double>& s2) -> double {
    v_t iv;
    if (v1 / L == v2 / L) {
      v_t vs = v1 < v2 ? v1 : v2;
      v_t vl = v1 < v2 ? v2 : v1;

      if (vl == L - 1 && vs == 0) {
        iv = vl;
      } else {
        iv = vs;
      }
    } else {
      v_t vs = v1 < v2 ? v1 : v2;
      v_t vl = v1 < v2 ? v2 : v1;

      if (vl / L == L - 1 && vs / L == 0) {
        iv = vl;
      } else {
        iv = vs;
      }
    }

    if (si.s[iv].x) {
      return (1 + a) * (s1 * s2);
    } else {
      return (1 - a) * (s1 * s2);
    }
  };

  graph Gxy(2, L);

  xy::wolff::system<orthogonal_t<2, double>, vector_t<2, double>> sxy(Gxy, T, Zxy);

  xy_spins = &(sxy.s);

  for (v_t i = 0; i < pow(L, D); i++) {
    si.s[pow(L, D) + i].x = true;
  }

  E = - J * 4 * pow(L, D) - 2 * pow(L, D);

  i_measurement mi(si);
  x_measurement mxy(sxy);

  double total_E;
  double total_E2;

  for (N_t i = 0; i < N; i++) {
    si.run_wolff(1, ising::wolff::gen_ising, mi, rng);
    sxy.run_wolff(1, xy::wolff::generate_rotation_uniform<2>, mxy, rng);
    total_E += E;
    total_E2 += pow(E, 2);
  }

  std::cout << mi.avgC() / si.nv << " " << mi.avgA2() / pow(si.nv, 2) << "\n";
  std::cout << mxy.avgM() / pow(sxy.nv, 1) << " " << 2 * mxy.avgCM() / pow(sxy.nv, 0) << "\n";
}