summaryrefslogtreecommitdiff
path: root/langevin.cpp
blob: adb521289dc157a6cba3a85d73caf64b42aa0324 (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
#include <getopt.h>
#include <limits>
#include <stereographic.hpp>
#include <unordered_map>
#include <list>

#include "Eigen/src/Eigenvalues/ComplexEigenSolver.h"
#include "complex_normal.hpp"
#include "p-spin.hpp"
#include "dynamics.hpp"
#include "stokes.hpp"

#include "pcg-cpp/include/pcg_random.hpp"
#include "randutils/randutils.hpp"
#include "unsupported/Eigen/CXX11/src/Tensor/TensorFFT.h"

#define PSPIN_P 3
const int p = PSPIN_P; // polynomial degree of Hamiltonian
using Complex = std::complex<Real>;
using ComplexVector = Vector<Complex>;
using ComplexMatrix = Matrix<Complex>;
using ComplexTensor = Tensor<Complex, p>;

using Rng = randutils::random_generator<pcg32>;

std::list<std::array<ComplexVector, 2>> saddlesCloserThan(const std::unordered_map<uint64_t, ComplexVector>& saddles, Real δ) {
  std::list<std::array<ComplexVector, 2>> pairs;

  for (auto it1 = saddles.begin(); it1 != saddles.end(); it1++) {
    for (auto it2 = std::next(it1); it2 != saddles.end(); it2++) {
      if ((it1->second - it2->second).norm() < δ) {
        pairs.push_back({it1->second, it2->second});
      }
    }
  }

  return pairs;
}

template <class Generator>
std::tuple<ComplexTensor, ComplexVector, ComplexVector> matchImaginaryEnergies(const ComplexTensor& J0, const ComplexVector& z10, const ComplexVector& z20, Real ε, Real Δ, Generator& r) {
  Real σ = sqrt(factorial(p) / (Real(2) * pow(z10.size(), p - 1)));
  complex_normal_distribution<Real> dJ(0, σ, 0);

  ComplexTensor J = J0;
  ComplexVector z1, z2;
  Real ΔH = abs(imag(getHamiltonian(J, z10) - getHamiltonian(J, z20))) / z10.size();
  Real γ = ΔH;

  std::function<void(ComplexTensor&, std::array<unsigned, p>)> perturbJ =
    [&γ, &dJ, &r] (ComplexTensor& JJ, std::array<unsigned, p> ind) {
      Complex Ji = getJ<Complex, p>(JJ, ind);
      setJ<Complex, p>(JJ, ind, Ji + γ * dJ(r.engine()));
    };

  while (ΔH > ε) {
    ComplexTensor Jp = J;
    iterateOver<Complex, p>(Jp, perturbJ);

    try {
      z1 = findSaddle(Jp, z10, Δ);
      z2 = findSaddle(Jp, z20, Δ);

      Real Δz = (z1 - z2).norm();

      if (Δz > 1e-2) {
        Real ΔHNew = abs(imag(getHamiltonian(Jp, z1) - getHamiltonian(Jp, z2))) / z1.size();

        if (ΔHNew < ΔH) {
          J = Jp;
          ΔH = ΔHNew;
          γ = ΔH;

          std::cerr << "Match imaginary energies: Found couplings with ΔH = " << ΔH << std::endl;
        }
      }
    } catch (std::exception& e) {}
  }

  return {J, z1, z2};
}

int main(int argc, char* argv[]) {
  // model parameters
  unsigned N = 10; // number of spins
  Real T = 1;    // temperature
  Real Rκ = 0;   // real part of distribution parameter
  Real Iκ = 0;   // imaginary part of distribution parameter
  // simulation parameters
  Real ε = 1e-15;
  Real εJ = 1e-5;
  Real δ = 1;   // threshold for determining saddle
  Real Δ = 1e-3;
  Real γ = 1e-1;   // step size
  unsigned t = 1000; // number of Langevin steps
  unsigned M = 100;
  unsigned n = 100;

  int opt;

  while ((opt = getopt(argc, argv, "N:M: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;
    case 'n':
      n = (unsigned)atof(optarg);
      break;
    case 'M':
      M = (unsigned)atof(optarg);
      break;
    default:
      exit(1);
    }
  }

  Complex κ(,);
  Real σ = sqrt(factorial(p) / ((Real)2 * pow(N, p - 1)));

  Rng r;

  complex_normal_distribution<Real> d(0, 1, 0);

  ComplexTensor J = generateCouplings<Complex, p>(N, complex_normal_distribution<Real>(0, σ, κ), r.engine());

  std::function<Real(const ComplexTensor&, const ComplexVector&)> energyNormGrad = []
    (const ComplexTensor& J, const ComplexVector& z) {
      Real W;
      std::tie(W, std::ignore) = WdW(J, z);
      return W;
    };

  ComplexVector zSaddle = randomSaddle(J, d, r, ε);
  std::cerr << "Found saddle." << std::endl;

  ComplexVector zSaddleNext;
  bool foundSaddle = false;
  while (!foundSaddle) {
    ComplexVector z0 = normalize(zSaddle + δ * randomVector<Complex>(N, d, r.engine()));
    try {
      zSaddleNext = findSaddle(J, z0, ε);
      Real saddleDistance = (zSaddleNext - zSaddle).norm();
      if (saddleDistance / N > 1e-2) {
        foundSaddle = true;
      }
    } catch (std::exception& e) {}
  }

  auto [H1, dH1, ddH1] = hamGradHess(J, zSaddle);
  auto [H2, dH2, ddH2] = hamGradHess(J, zSaddleNext);

  Eigen::ComplexEigenSolver<ComplexMatrix> ces;
  ces.compute(ddH1);

  Real φ = atan2( H2.imag() - H1.imag(), H1.real() - H2.real());
  std::cerr << (zSaddle - zSaddleNext).norm() / (Real)N << " " << φ << " " << H1 * exp(Complex(0, φ)) << " " << H2 * exp(Complex(0, φ)) << std::endl;
  Real smallestNorm = std::numeric_limits<Real>::infinity();
  for (Complex z : ces.eigenvalues()) {
    if (norm(z) < smallestNorm) {
      smallestNorm = norm(z);
    }
  }
  std::cerr << smallestNorm << std::endl;

  J = exp(Complex(0, φ)) * J;

  /*
  if (stokesLineTest<Real>(J, zSaddle, zSaddleNext, 10, 4)) {
    std::cerr << "Found a Stokes line" << std::endl;
//    stokesLineTestNew<Real>(J, zSaddle, zSaddleNext, 10, 3);
  } else {
    std::cerr << "Didn't find a Stokes line" << std::endl;
  }
  */

  /*
  J(0,0,0) = Complex(2, 3);
  J(1,1,1) = Complex(-2, 0.3);
  J(0,1,1) = Complex(4, 0.2);
  J(1,0,1) = Complex(4, 0.2);
  J(1,1,0) = Complex(4, 0.2);
  J(1,0,0) = Complex(0.7, 0.4);
  J(0,1,0) = Complex(0.7, 0.4);
  J(0,0,1) = Complex(0.7, 0.4);

  ComplexVector z0(2);;
  z0 << Complex(0.8, 0.3), Complex(0.7, 0.2);
  ComplexVector z1(2);
  z1 << Complex(-0.5, 0.2), Complex(1.0, 1.0);

  Cord test(J, z0, z1, 2);

  test.gs[0](0) = Complex(0.2, 0.2);
  test.gs[0](1) = Complex(0.4, 0.4);
  test.gs[1](0) = Complex(0.1, 0.2);
  test.gs[1](1) = Complex(0.3, 0.4);

  auto [dgs, ddgs] = test.gsGradHess(J, 0.7);

  std::cout << dgs << std::endl;
  std::cout << ddgs << std::endl;
  */

  Cord test(J, zSaddle, zSaddleNext, 5);
  test.relaxNewton(J, 20, 1, 1e4);

  std::cout << test.z0.transpose() << std::endl;
  std::cout << test.z1.transpose() << std::endl;

  for (Vector<Complex>& g : test.gs) {
    std::cout << g.transpose() << std::endl;
  }

  return 0;
}