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

#include <assert.h>
#include <set>
#include <vector>
#include <queue>

#include <eigen3/Eigen/Dense>

#include "pcg-cpp/include/pcg_random.hpp"
#include "randutils/randutils.hpp"

using Rng = randutils::random_generator<pcg32>;

template <int D> using Vector = Eigen::Matrix<double, D, 1>;
template <int D> using Matrix = Eigen::Matrix<double, D, D>;

template <int D> class Position : public Vector<D> {
private:
  void recenter() {
    for (double& vi : *this) {
      if (vi >= 0) {
        vi = fmod(vi, 1);
      } else {
        vi = fmod(vi + ceil(-vi), 1);
      }
    }
  }

public:
  using Vector<D>::Vector;

  Position<D>& operator+=(const Vector<D>& u) {
    Vector<D>::operator+=(u);
    recenter();
    return *this;
  }

  friend Position<D> operator+(Position<D> v, const Vector<D>& u) {
    v += u;
    return v;
  }

  Position<D>& operator-=(const Vector<D>& u) {
    Vector<D>::operator-=(u);
    recenter();
    return *this;
  }

  friend Vector<D> operator-(Position<D> v, const Position<D>& u) {
    v -= (Vector<D>)u;

    for (double& vi : v) {
      if (vi > 0.5)
        vi = 1 - vi;
    }

    return (Vector<D>)v;
  }
};

template <typename T, int D> concept Particle = requires(T v, const T& w) {
  { v.x } -> std::same_as<Position<D>>;
  { v.m } -> std::same_as<bool>;
  { v.U(w) } -> std::same_as<double>;
};

template <int D, Particle<D> T> class NeighborLists {
private:
  unsigned n;
  std::vector<std::set<T*>> lists;

  unsigned mod(signed a, unsigned b) const {
    if (a >= 0) {
      return a % b;
    } else {
      return (a + b * ((b - a) / b)) % b;
    }
  }

  int iPow(int x, unsigned p) const {
    if (p == 0)
      return 1;
    if (p == 1)
      return x;

    int tmp = iPow(x, p / 2);
    if (p % 2 == 0) {
      return tmp * tmp;
    } else {
      return x * tmp * tmp;
    }
  }

  unsigned index(Position<D> x) const {
    unsigned ind = 0;
    for (unsigned i = 0; i < D; i++) {
      ind += pow(n, i) * std::floor(n * x(i));
    }
    assert(ind < lists.size());
    return ind;
  }

  std::pair<typename std::set<T*>::iterator, bool> insert(T& p, unsigned ind) {
    return lists[ind].insert(&p);
  }

  size_t erase(T& p, unsigned ind) {
    return lists[ind].erase(&p);
  }

  void neighborsOfHelper(signed i0, unsigned d, std::set<T*>& ns) const {
    if (d == 0) {
      const std::set<T*>& a = lists[i0];
      ns.insert(a.begin(), a.end());
    } else {
      for (signed j : {-1, 0, 1}) {
        signed i1 = iPow(n, d) * (i0 / iPow(n, d)) + mod(i0 + j * iPow(n, d-1), iPow(n, d));
        neighborsOfHelper(i1, d - 1, ns);
      }
    }
  }

public:
  NeighborLists(unsigned m) : n(m), lists(pow(n, D)) {}
  NeighborLists(double l) : NeighborLists((unsigned)floor(1 / l)) {}

  std::pair<typename std::set<T*>::iterator, bool> insert(T& p, const Position<D>& x) {
    return insert(p, index(x));
  }

  std::pair<typename std::set<T*>::iterator, bool> insert(T& p) {
    return insert(p, p.x);
  }

  size_t erase(T& p, const Position<D>& x) {
    return erase(p, index(x));
  }

  size_t erase(T& p) {
    return erase(p, p.x);
  }

  void move(T& p, const Position<D>& xNew) {
    unsigned iOld = index(p.x);
    unsigned iNew = index(xNew);
    if (iNew != iOld) {
      erase(p, iOld);
      insert(p, iNew);
    }
  }

  std::set<T*> neighborsOf(const Position<D>& x) const {
    std::set<T*> neighbors;
    neighborsOfHelper(index(x), D, neighbors);
    return neighbors;
  }
};

template <int D> class Sphere {
public:
  Position<D> x;
  bool m;
  double r;

  Sphere(const Position<D>& x, double r) : x(x), m(false), r(r) {};

  double regularizedDistanceSquared(const Sphere<D>& s) const {
    return (x - s.x).squaredNorm() / pow(r + s.r, 2);
  } 

  bool intersectsBoundary() const {
    for (double xi : x) {
      if (xi < r || 1 - xi < r) {
        return true;
      }
    }
    return false;
  }
};

template <int D> class HardSphere : public Sphere<D> {
public:
  using Sphere<D>::Sphere;

  double U(const HardSphere<D>& s) const {
    if (Sphere<D>::regularizedDistanceSquared(s) < 1) {
      return 1e6;
    } else {
      return 0;
    }
  }
};

template <int D, int n> class SoftSphere : public Sphere<D> {
private:
  const double c0 = -pow(2, 2*n-3) * pow(5, -n) * (8 + 6*n + pow(n, 2));
  const double c2 =  pow(2, 2+2*n) * pow(5, -n-2) * n * (4+n);
  const double c4 = -pow(2, 5+2*n) * pow(5, -n-4) * n * (2+n);
public:
  using Sphere<D>::Sphere;

  double U(const SoftSphere<D, n>& s) const {
    double r2 = Sphere<D>::regularizedDistanceSquared(s);
    if (r2 < pow(1.25, 2)) {
      return pow(1 / r2, n / 2) + c0 + c2 * r2 + c4 * pow(r2, 2);
    } else {
      return 0;
    }
  }
};

template <int D> class Euclidean {
public:
  Vector<D> t;
  Matrix<D> r;

  Euclidean() {
    t.setZero();
    r.setIdentity();
  }

  Euclidean(const Vector<D>& t0, const Matrix<D>& r0) : t(t0), r(r0) {}

  Euclidean inverse() const { return Euclidean(-r.transpose() * t, r.transpose()); }

  Vector<D> act(const Vector<D>& x) const { return r * x; }
  Position<D> act(const Position<D>& x) const { return (Position<D>)(r * x) + t; }
};

template <int D, Particle<D> T> class Model {
private:
  NeighborLists<D, T> nl;

public:
  std::vector<T> particles;

  Model(unsigned N, double l) : nl(l) {
    particles.reserve(N);
  };

  void insert(const T& p) { 
    particles.push_back(p);
    nl.insert(particles.back());
  }

  void move(T& p, const Position<D>& x) {
    nl.move(p, x);
    p.x = x;
  }

  std::set<T*> neighborsOf(const Position<D>& x) const {
    return nl.neighborsOf(x);
  }

  double energyChange(const T& p, const T& pNew) const {
    double ΔE = 0;
    for (const T* neighbor : neighborsOf(p.x)) {
      if (neighbor != &p) {
        ΔE -= neighbor->U(p);
      }
    }
    for (const T* neighbor : neighborsOf(pNew.x)) {
      if (neighbor != &p) {
        ΔE += neighbor->U(pNew);
      }
    }

    return ΔE;
  }

  double energyChangeSwap(const T& p1, const T& p2) const {
    double ΔE = 0;
    T p1New = p1;
    T p2New = p2;
    p1New.r = p2.r;
    p2New.r = p1.r;
    for (const T* neighbor : neighborsOf(p1.x)) {
      if (neighbor != &p1 && neighbor != &p2) {
        ΔE -= neighbor->U(p1);
        ΔE += neighbor->U(p1New);
      }
    }
    for (const T* neighbor : neighborsOf(p2.x)) {
      if (neighbor != &p1 && neighbor != &p2) {
        ΔE -= neighbor->U(p2);
        ΔE += neighbor->U(p2New);
      }
    }

    return ΔE;
  }

  bool metropolis(double β, T& p, const Vector<D>& Δx, Rng& r) {
    T pNew = p;
    pNew.x += Δx;

    double ΔE = energyChange(p, pNew);

    if (exp(-β * ΔE) > r.uniform(0.0, 1.0)) {
      move(p, pNew.x);
      return true;
    } else {
      return false;
    }
  }

  bool swap(double β, T& p1, T& p2, Rng& r) {
    double ΔE = energyChangeSwap(p1, p2);
    if (exp(-β * ΔE) > r.uniform(0.0, 1.0)) {
      std::swap(p1.r, p2.r);
      return true;
    } else {
      return false;
    }
  }

  unsigned clusterFlip(double β, const Euclidean<D>& R, T& p0, Rng& r) {
    unsigned n = 0;
    std::queue<T*> q;
    q.push(&p0);

    while (!q.empty()) {
      T* p = q.front();
      q.pop();

      if (!p->m) {
        p->m = true;
        T pNew = *p;
        pNew.x = R.act(p->x);

        for (T* neighbor : neighborsOf(pNew.x)) {
          if (!neighbor->m) {
            double ΔE = neighbor->U(pNew) - neighbor->U(*p);
            if (1 - exp(-β * ΔE) > r.uniform(0.0, 1.0)) {
              q.push(neighbor);
            }
          }
        }

        move(*p, pNew.x);
        n++;
      }
    }

    for (T& p : particles) {
      p.m = false;
    }

    return n;
  }
};