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

#include "Eigen/Dense"
#include "Eigen/src/Eigenvalues/ComplexEigenSolver.h"
#include "Eigen/src/Eigenvalues/SelfAdjointEigenSolver.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 RealVector = Vector<Real>;
using RealMatrix = Matrix<Real>;
using RealTensor = Tensor<Real, p>;
using ComplexVector = Vector<Complex>;
using ComplexMatrix = Matrix<Complex>;
using ComplexTensor = Tensor<Complex, p>;

using Rng = randutils::random_generator<pcg32>;

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;

  pSpinModel<Real, p> pSpin;

  std::get<0>(pSpin.Js) = generateCouplings<Real, p>(N, std::normal_distribution<Real>(0, σ), r.engine());

  std::normal_distribution<Real> Red(0, 1);

  RealVector zMin = randomMinimum(pSpin, Red, r, ε);
  Real Hr;
  RealVector dHr;
  RealMatrix ddHr;
  std::tie(Hr, dHr, ddHr, std::ignore) = pSpin.hamGradHess(zMin);
  Eigen::EigenSolver<Matrix<Real>> eigenS(ddHr - (dHr * zMin.transpose() + zMin.dot(dHr) * Matrix<Real>::Identity(zMin.size(), zMin.size()) + (ddHr * zMin) * zMin.transpose()) / (Real)zMin.size() + 2.0 * zMin * zMin.transpose());
  std::cout << eigenS.eigenvalues().transpose() << std::endl;
  for (unsigned i = 0; i < N; i++) {
    RealVector zNew = normalize(zMin + 0.01 * eigenS.eigenvectors().col(i).real());
    std::cout << pSpin.getHamiltonian(zNew) - Hr << " " << real(eigenS.eigenvectors().col(i).dot(zMin)) << std::endl;
  }
  std::cout << std::endl;
  getchar();

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

  pSpinModel<Complex, p> complexPSpin = pSpin.cast<Complex>();;
  ComplexVector zSaddle = zMin.cast<Complex>();

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

  Complex H1 = complexPSpin.getHamiltonian(zSaddle);
  Complex H2 = complexPSpin.getHamiltonian(zSaddleNext);

  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;

  std::get<0>(complexPSpin.Js) = exp(Complex(0, φ)) * std::get<0>(complexPSpin.Js);

  Cord test(complexPSpin, zSaddle, zSaddleNext, 3);
  test.relaxNewton(10, 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;
}