summaryrefslogtreecommitdiff
path: root/transformation.hpp
blob: fc1833e35c01928e37a0b4e1d49e6972848d66c3 (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
#pragma once

#include <set>

#include "random.hpp"
#include "spin.hpp"

template <class U, int D, class R, class S> class Model;

template <class U, int D, class R, class S> class Transformation {
public:
  virtual std::set<Spin<U, D, S>*> current() const { return {NULL}; }
  virtual std::set<Spin<U, D, S>*> toConsider() const { return {}; }
  virtual double ΔE(const Spin<U, D, S>*) const { return 0; }
  virtual Transformation* createNew(double T, const std::set<Spin<U, D, S>*>&, Spin<U, D, S>*, Rng& rng) const { return new Transformation(); }
  virtual void apply(){};
};

template <class U, int D, class R, class S>
using Gen = std::function<Transformation<U, D, R, S>*(Model<U, D, R, S>&, Rng&)>;

template <class U, int D, class R, class S> class SpinFlip : public Transformation<U, D, R, S> {
private:
  Model<U, D, R, S>& M;
  Spin<U, D, S>* sOld;
  const R r;
  Spin<U, D, S> sNew;

public:
  SpinFlip(Model<U, D, R, S>& M, const R& r, Spin<U, D, S>* s) : M(M), r(r) {
    sOld = s;
    sNew = r.act(*s);
  }

  std::set<Spin<U, D, S>*> current() const override { return {sOld}; }

  std::set<Spin<U, D, S>*> toConsider() const override {
    std::set<Spin<U, D, S>*> neighbors;
    std::set<Spin<U, D, S>*> current_neighbors = M.dict.neighbors(sOld->x);
    std::set<Spin<U, D, S>*> new_neighbors = M.dict.neighbors(sNew.x);

    neighbors.insert(current_neighbors.begin(), current_neighbors.end());
    neighbors.insert(new_neighbors.begin(), new_neighbors.end());
    neighbors.insert(NULL);

    return neighbors;
  }

  double ΔE(const Spin<U, D, S>* s) const override {
    if (s == NULL) {
      Spin<U, D, S> s0s_old = M.s0.inverse().act(*sOld);
      Spin<U, D, S> s0s_new = M.s0.inverse().act(sNew);
      return M.B(s0s_new) - M.B(s0s_old);
    } else {
      return M.Z(*sOld, *s) - M.Z(sNew, *s);
    }
  }

  Transformation<U, D, R, S>* createNew(double, const std::set<Spin<U, D, S>*>&, Spin<U, D, S>* s, Rng&) const override;

  void apply() override {
    M.dict.remove(sOld);
    *sOld = sNew;
    M.dict.insert(sOld);
  }
};

template <class U, int D, class R, class S> class PairFlip : public Transformation<U, D, R, S> {
private:
  Model<U, D, R, S>& M;
  Spin<U, D, S>* s1Old;
  Spin<U, D, S>* s2Old;
  const R r;
  Spin<U, D, S> s1New;
  Spin<U, D, S> s2New;

public:
  PairFlip(Model<U, D, R, S>& M, const R& r, Spin<U, D, S>* s1, Spin<U, D, S>* s2) : M(M), r(r) {
    s1Old = s1;
    s2Old = s2;
    s1New = r.act(*s1);
    s2New = r.act(*s2);
  }

  std::set<Spin<U, D, S>*> current() const override { return {s1Old, s2Old}; }

  std::set<Spin<U, D, S>*> toConsider() const override {
    std::set<Spin<U, D, S>*> neighbors;
    std::set<Spin<U, D, S>*> current_neighbors_1 = M.dict.neighbors(s1Old->x);
    std::set<Spin<U, D, S>*> current_neighbors_2 = M.dict.neighbors(s2Old->x);
    std::set<Spin<U, D, S>*> new_neighbors_1 = M.dict.neighbors(s1New.x);
    std::set<Spin<U, D, S>*> new_neighbors_2 = M.dict.neighbors(s2New.x);

    neighbors.insert(current_neighbors_1.begin(), current_neighbors_1.end());
    neighbors.insert(current_neighbors_2.begin(), current_neighbors_2.end());
    neighbors.insert(new_neighbors_1.begin(), new_neighbors_1.end());
    neighbors.insert(new_neighbors_2.begin(), new_neighbors_2.end());
    neighbors.insert(NULL);

    return neighbors;
  }

  double ΔE(const Spin<U, D, S>* s) const override {
    if (s == NULL) {
      Spin<U, D, S> s0s1_old = M.s0.inverse().act(*s1Old);
      Spin<U, D, S> s0s1_new = M.s0.inverse().act(s1New);
      Spin<U, D, S> s0s2_old = M.s0.inverse().act(*s2Old);
      Spin<U, D, S> s0s2_new = M.s0.inverse().act(s2New);
      return M.B(s0s1_new) + M.B(s0s2_new) - M.B(s0s1_old) - M.B(s0s2_old);
    } else {
      return M.Z(*s1Old, *s) + M.Z(*s2Old, *s) - M.Z(s1New, *s) - M.Z(s2New, *s);
    }
  }

  Transformation<U, D, R, S>* createNew(double T, const std::set<Spin<U, D, S>*>& v, Spin<double, D, S>* s, Rng&) const;
  Transformation<U, D, R, S>* createNew(double T, const std::set<Spin<U, D, S>*>& v, Spin<signed, D, S>* s, Rng&) const;

  void apply() override {
    M.dict.remove(s1Old);
    M.dict.remove(s2Old);
    *s1Old = s1New;
    *s2Old = s2New;
    M.dict.insert(s1Old);
    M.dict.insert(s2Old);
  }
};

template <class U, int D, class R, class S> class FieldFlip : public Transformation<U, D, R, S> {
private:
  Model<U, D, R, S>& M;
  const R r;
  R s0New;

public:
  FieldFlip(Model<U, D, R, S>& M, const R& r) : M(M), r(r), s0New(r.act(M.s0)) {}

  std::set<Spin<U, D, S>*> toConsider() const override {
    std::set<Spin<U, D, S>*> neighbors;

    for (Spin<U, D, S>* s : M.s) {
      neighbors.insert(s);
    }

    return neighbors;
  }

  double ΔE(const Spin<U, D, S>* s) const override {
    Spin<U, D, S> s0s_old = M.s0.inverse().act(*s);
    Spin<U, D, S> s0s_new = s0New.inverse().act(*s);
    return M.B(s0s_new) - M.B(s0s_old);
  }

  Transformation<double, D, R, S>* createNew(double T, const std::set<Spin<U, D, S>*>& v, Spin<double, D, S>* s, Rng&) const {
    return new SpinFlip<U, D, R, S>(M, r, s);
  }

  Transformation<signed, D, R, S>* createNew(double, const std::set<Spin<U, D, S>*>&, Spin<signed, D, S>* s, Rng&) const {
    Vector<signed, 2> v = r.act(*s).x;
    std::set<Spin<U, D, S>*> on_site = M.dict.at(v);
    if (on_site.empty()) {
      return new SpinFlip<U, D, R, S>(M, r, s);
    } else {
      return new PairFlip<U, D, R, S>(M, r, s, *on_site.begin());
    }
  }

  void apply() override { M.s0 = s0New; }
};

template <class U, int D, class R, class S>
Transformation<U, D, R, S>* SpinFlip<U, D, R, S>::createNew(double T, const std::set<Spin<U, D, S>*>& v, Spin<U, D, S>* s, Rng& rng) const {
  if (s == NULL) {
    return new FieldFlip<U, D, R, S>(M, r);
  } else {
    SpinFlip<U, D, R, S>* t = new SpinFlip<U, D, R, S>(M, r, s);
    Spin<U, D, S>* sMax = NULL;
    double ΔEMax = 0.0;
    for (Spin<U, D, S>* ss : t->toConsider()) {
      if (ss != NULL && ss != s && !v.contains(ss)) {
        double ΔE = t->ΔE(ss);
        if (ΔE > ΔEMax) {
          sMax = ss;
          ΔEMax = ΔE;
        }
      }
    }
    if (sMax == NULL || 1.0 - exp(-ΔEMax / T) < rng.uniform<double>(0, 1)) {
      return t;
    } else {
      delete t;
      return new PairFlip<U, D, R, S>(M, r, s, sMax);
    }
  }
}

template <class U, int D, class R, class S>
Transformation<U, D, R, S>* PairFlip<U, D, R, S>::createNew(double T, const std::set<Spin<U, D, S>*>& v, Spin<double, D, S>* s, Rng& rng) const {
  if (s == NULL) {
    return new FieldFlip<U, D, R, S>(M, r);
  } else {
    SpinFlip<U, D, R, S>* t = new SpinFlip<U, D, R, S>(M, r, s);
    Spin<U, D, S>* sMax = NULL;
    double ΔEMax = 0.0;
    for (Spin<U, D, S>* ss : t->toConsider()) {
      if (ss != NULL && ss != s && !v.contains(ss)) {
        double ΔE = t->ΔE(ss);
        if (ΔE > ΔEMax) {
          sMax = ss;
          ΔEMax = ΔE;
        }
      }
    }
    if (sMax == NULL || 1.0 - exp(-ΔEMax / T) < rng.uniform<double>(0, 1)) {
      return t;
    } else {
      delete t;
      return new PairFlip<U, D, R, S>(M, r, s, sMax);
    }
  }
}

template <class U, int D, class R, class S>
Transformation<U, D, R, S>* PairFlip<U, D, R, S>::createNew(double, const std::set<Spin<U, D, S>*>&, Spin<signed, D, S>* s, Rng&) const {
  if (s == NULL) {
    return new FieldFlip<U, D, R, S>(M, r);
  } else {
    Vector<signed, 2> v = r.act(*s).x;
    std::set<Spin<U, D, S>*> on_site = M.dict.at(v);
    if (on_site.empty()) {
      return new SpinFlip<U, D, R, S>(M, r, s);
    } else {
      return new PairFlip<U, D, R, S>(M, r, s, *on_site.begin());
    }
  }
}