summaryrefslogtreecommitdiff
path: root/langevin.cpp
blob: 3bc12ed61dc492d47affde75179522787c4fc51f (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
#include <complex>
#include <cstdlib>
#include <functional>
#include <getopt.h>
#include <random>

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

#include "complex_normal.hpp"
#include "p-spin.hpp"
#include "stereographic.hpp"

using Rng = randutils::random_generator<pcg32>;

template <class Distribution, class Generator>
Vector randomVector(unsigned N, Distribution d, Generator& r) {
  Vector z(N);

  for (unsigned i = 0; i < N; i++) {
    z(i) = d(r);
  }

  return z;
}

Vector findSaddle(const Tensor& J, const Vector& z0, double γ0, double δ, double ε, Rng& r) {
  Vector z = z0;

  double W;
  Vector dW;
  std::tie(W, dW) = WdW(J, z);

  while (W > δ) {
    double γ = pow(r.variate<double, std::normal_distribution>(0, γ0), 2);

    Vector zNew = z - γ * dW.conjugate();
    zNew *= sqrt(z.size()) / sqrt((Scalar)(zNew.transpose() * zNew));

    double WNew;
    Vector dWNew;
    std::tie(WNew, dWNew) = WdW(J, zNew);

    if (WNew < W) {
      z = zNew;
      W = WNew;
      dW = dWNew;
      std::cout << W << std::endl;
    } 
  }

  Vector ζ = euclideanToStereographic(z);
  Vector dH;
  Matrix ddH;
  std::tie(std::ignore, dH, ddH) = stereographicHamGradHess(J, ζ);

  while (W > ε) {
    double γ = pow(r.variate<double, std::normal_distribution>(0, γ0), 2);

    Vector ζNew = ζ - ddH.ldlt().solve(dH);

    double WNew;
    Vector dWNew;
    std::tie(WNew, dWNew) = WdW(J, stereographicToEuclidean(ζNew));

    if (WNew < W) {
      ζ = ζNew;
      W = WNew;
      dW = dWNew;
      std::tie(std::ignore, dH, ddH) = stereographicHamGradHess(J, ζ);
      std::cout << WNew << std::endl;
    } 
  }

  return stereographicToEuclidean(ζ);
}

Vector langevin(const Tensor& J, const Vector& z0, double T, double γ0,
                std::function<bool(double, unsigned)> quit, Rng& r) {
  Vector z = z0;

  double W;
  Vector dW;
  std::tie(W, dW) = WdW(J, z);

  unsigned steps = 0;
  complex_normal_distribution<> d(0, sqrt(T), 0);

  while (!quit(W, steps)) {
    double γ = pow(r.variate<double, std::normal_distribution>(0, γ0), 2);
    Vector η = randomVector(z.size(), d, r.engine());

    z -= γ * (dW.conjugate() / pow((double)z.size(), 2) + η);
    z *= sqrt(z.size()) / sqrt((Scalar)(z.transpose() * z));

    std::tie(W, dW) = WdW(J, z);
  }

  return z;
}

int main(int argc, char* argv[]) {
  // model parameters
  unsigned N = 10; // number of spins
  double T = 1;    // temperature
  double= 1;   // real part of distribution parameter
  double= 0;   // imaginary part of distribution parameter

  // simulation parameters
  double ε = 1e-4;
  double δ = 1e-2;   // threshold for determining saddle
  double γ = 1e-2;   // step size
  unsigned t = 1000; // number of Langevin steps

  int opt;

  while ((opt = getopt(argc, argv, "N:T:e:r:i:g:t:E:")) != -1) {
    switch (opt) {
    case 'N':
      N = (unsigned)atof(optarg);
      break;
    case 't':
      t = (unsigned)atof(optarg);
      break;
    case 'T':
      T = atof(optarg);
      break;
    case 'e':
      δ = atof(optarg);
      break;
    case 'E':
      ε = atof(optarg);
      break;
    case 'g':
      γ = atof(optarg);
    case 'r':= atof(optarg);
      break;
    case 'i':= atof(optarg);
      break;
    default:
      exit(1);
    }
  }

  Scalar κ(,);
  double σ = sqrt(factorial(p) / (2.0 * pow(N, p - 1)));

  Rng r;

  Tensor J = generateCouplings<Scalar, PSPIN_P>(N, complex_normal_distribution<>(0, σ, κ), r.engine());
  Vector z = randomVector(N, complex_normal_distribution<>(0, 1, 0), r.engine());
  z *= sqrt(N) / sqrt((Scalar)(z.transpose() * z)); // Normalize.

  std::function<bool(double, unsigned)> f = [δ](double W, unsigned) {
    std::cout << W << std::endl;
    return W < δ;
  };

  //Vector zm = langevin(J, z, T, γ, f, r);
  Vector zm = findSaddle(J, z, γ, δ, ε, r);

  Scalar H;
  Vector dH;

  std::tie(H, dH, std::ignore) = hamGradHess(J, zm);

  Vector constraint = dH - ((double)p * H / (double)N) * zm;

  std::cout << constraint << std::endl;
  std::cout << H / (double)N << std::endl;
}