summaryrefslogtreecommitdiff
path: root/get_energy.cpp
blob: 6164a6d4a6419d49415d97cd8b2b272a3a68f79c (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
#include <getopt.h>
#include <vector>
#include <cmath>
#include <iostream>
#include <fftw3.h>
#include <complex>
#include <fstream>

using Real = double;
using Complex = std::complex<Real>;
using namespace std::complex_literals;

inline Real fP(unsigned p, Real q) {
  return 0.5 * pow(q, p);
}

inline Real dfP(unsigned p, Real q) {
  return 0.5 * p * pow(q, p - 1);
}

inline Real ddfP(unsigned p, Real q) {
  return 0.5 * p * (p - 1) * pow(q, p - 2);
}

inline Real f(Real λ, unsigned p, unsigned s, Real q) {
  return (1 - λ) * fP(p, q) + λ * fP(s, q);
}

inline Real df(Real λ, unsigned p, unsigned s, Real q) {
  return (1 - λ) * dfP(p, q) + λ * dfP(s, q);
}

inline Real ddf(Real λ, unsigned p, unsigned s, Real q) {
  return (1 - λ) * ddfP(p, q) + λ * ddfP(s, q);
}

class FourierTransform {
private:
  std::vector<Real> a;
  std::vector<Complex> â;
  fftw_plan plan_r2c;
  fftw_plan plan_c2r;
  Real Δω;
  Real Δτ;
public:
  FourierTransform(unsigned n, Real Δω, Real Δτ, unsigned flags = 0) : a(2 * n), â(n + 1), Δω(Δω), Δτ(Δτ) {
    plan_r2c = fftw_plan_dft_r2c_1d(2 * n, a.data(), reinterpret_cast<fftw_complex*>(â.data()), flags);
    plan_c2r = fftw_plan_dft_c2r_1d(2 * n, reinterpret_cast<fftw_complex*>(â.data()), a.data(), flags);
  }

  ~FourierTransform() {
    fftw_destroy_plan(plan_r2c);
    fftw_destroy_plan(plan_c2r);
    fftw_cleanup();
  }

  std::vector<Complex> fourier(const std::vector<Real>& c) {
    a = c;
    fftw_execute(plan_r2c);
    std::vector<Complex> ĉ(â.size());
    for (unsigned i = 0; i < â.size(); i++) {
      ĉ[i] = â[i] * (Δτ * M_PI);
    }
    return ĉ;
  }

  std::vector<Real> inverse(const std::vector<Complex>& ĉ) {
    â = ĉ;
    fftw_execute(plan_c2r);
    std::vector<Real> c(a.size());
    for (unsigned i = 0; i < a.size(); i++) {
      c[i] = a[i] * (Δω / (2 * M_PI));
    }
    return c;
  }
};

int main(int argc, char* argv[]) {
  unsigned p = 2;
  unsigned s = 2;
  Real λ = 0.5;
  Real τ₀ = 0;
  Real y₀ = 0;
  Real yₘₐₓ = 0.5;
  Real Δy = 0.05;

  unsigned log2n = 8;
  Real τₘₐₓ = 20;

  int opt;

  while ((opt = getopt(argc, argv, "p:s:2:T:t:0:y:d:")) != -1) {
    switch (opt) {
    case 'p':
      p = atoi(optarg);
      break;
    case 's':
      s = atoi(optarg);
      break;
    case '2':
      log2n = atoi(optarg);
      break;
    case 'T':
      τₘₐₓ = atof(optarg);
      break;
    case 't':
      τ₀ = atof(optarg);
      break;
    case '0':
      y₀ = atof(optarg);
      break;
    case 'y':
      yₘₐₓ = atof(optarg);
      break;
    case 'd':
      Δy = atof(optarg);
      break;
    default:
      exit(1);
    }
  }

  unsigned n = pow(2, log2n);

  Real Δτ = τₘₐₓ / M_PI / n;
  Real Δω = M_PI / τₘₐₓ;

  Real y = y₀;

  FourierTransform fft(n, Δω, Δτ, FFTW_ESTIMATE);

  while (y += Δy, y <= yₘₐₓ) {
    std::vector<Real> C(2 * n);
    std::vector<Real> R(2 * n);

    std::string file_end = std::to_string(p) + "_" + std::to_string(s) + "_" + std::to_string(λ) + "_" + std::to_string(τ₀) + "_" + std::to_string(y) + "_" + std::to_string(log2n) + "_" + std::to_string(τₘₐₓ) + ".dat";
    std::ifstream cfile("C_"+file_end, std::ios::binary);
    if (cfile.is_open()) {
    cfile.read((char*)(C.data()), C.size() * sizeof(Real));
    cfile.close();
    std::ifstream rfile("R_"+file_end, std::ios::binary);
    rfile.read((char*)(R.data()), R.size() * sizeof(Real));
    rfile.close();

    Real energy = 0;

    for (unsigned i = 0; i < n; i++) {
      energy += y * R[i] * df(λ, p, s, C[i]) * M_PI * Δτ;
    }

    std::vector<Complex> Ct = fft.fourier(C);

    std::cout << y << " " << energy << " " << Ct[0].real() << std::endl;
    }
  }

  return 0;
}