summaryrefslogtreecommitdiff
path: root/get_energy.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'get_energy.cpp')
-rw-r--r--get_energy.cpp101
1 files changed, 15 insertions, 86 deletions
diff --git a/get_energy.cpp b/get_energy.cpp
index 6164a6d..a388e0c 100644
--- a/get_energy.cpp
+++ b/get_energy.cpp
@@ -1,80 +1,8 @@
+#include "fourier.hpp"
#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;
@@ -133,24 +61,25 @@ int main(int argc, char* argv[]) {
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);
+ std::ifstream cfile(fourierFile("C", p, s, λ, τ₀, y, log2n, τₘₐₓ), 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();
+ cfile.read((char*)(C.data()), C.size() * sizeof(Real));
+ cfile.close();
- Real energy = 0;
+ std::ifstream rfile(fourierFile("R", p, s, λ, τ₀, y, log2n, τₘₐₓ), std::ios::binary);
+ rfile.read((char*)(R.data()), R.size() * sizeof(Real));
+ rfile.close();
- for (unsigned i = 0; i < n; i++) {
- energy += y * R[i] * df(λ, p, s, C[i]) * M_PI * Δτ;
- }
+ Real e = energy(C, R, p, s, λ, y, Δτ);
+
+ std::vector<Complex> Ct = fft.fourier(C);
+ std::vector<Complex> Rt = fft.fourier(R);
+
+ auto [RddfCt, dfCt] = RddfCtdfCt(fft, C, R, p, s, λ);
- std::vector<Complex> Ct = fft.fourier(C);
+ Real z = ((std::conj(Rt[0]) + pow(y, 2) * (RddfCt[0] * Ct[0] + dfCt[0] * std::conj(Rt[0]))) / Ct[0]).real();
- std::cout << y << " " << energy << " " << Ct[0].real() << std::endl;
+ std::cout << y << " " << e << " " << Ct[0].real() << " " << z << std::endl;
}
}