diff options
Diffstat (limited to 'fourier_integrator.cpp')
-rw-r--r-- | fourier_integrator.cpp | 121 |
1 files changed, 20 insertions, 101 deletions
diff --git a/fourier_integrator.cpp b/fourier_integrator.cpp index 22866f2..86c3b24 100644 --- a/fourier_integrator.cpp +++ b/fourier_integrator.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 Δτ) : a(2 * n), â(n + 1), Δω(Δω), Δτ(Δτ) { - plan_r2c = fftw_plan_dft_r2c_1d(2 * n, a.data(), reinterpret_cast<fftw_complex*>(â.data()), 0); - plan_c2r = fftw_plan_dft_c2r_1d(2 * n, reinterpret_cast<fftw_complex*>(â.data()), a.data(), 0); - } - - ~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; @@ -147,6 +75,10 @@ int main(int argc, char* argv[]) { std::vector<Real> R(2 * n); FourierTransform fft(n, Δω, Δτ); + std::vector<Complex> Ct; + std::vector<Complex> Rt; + + Real y = y₀; if (!loadData) { // start from the exact solution for τ₀ = 0 @@ -158,37 +90,30 @@ int main(int argc, char* argv[]) { } R[i] = exp(-z * τ); } + Ct = fft.fourier(C); + Rt = fft.fourier(R); } else { - 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); cfile.read((char*)(C.data()), C.size() * sizeof(Real)); cfile.close(); - std::ifstream rfile("R_"+file_end, std::ios::binary); + std::ifstream rfile(fourierFile("R", p, s, λ, τ₀, y, log2n, τₘₐₓ), std::ios::binary); rfile.read((char*)(R.data()), R.size() * sizeof(Real)); rfile.close(); - } - std::vector<Complex> Ct = fft.fourier(C); - std::vector<Complex> Rt = fft.fourier(R); + Ct = fft.fourier(C); + Rt = fft.fourier(R); - Real y = y₀; + auto [RddfCt, dfCt] = RddfCtdfCt(fft, C, R, p, s, λ); + + z = ((Γ₀ * std::conj(Rt[0]) + pow(y, 2) * (RddfCt[0] * Ct[0] + dfCt[0] * std::conj(Rt[0]))) / Ct[0]).real(); + } while (y += Δy, y <= yₘₐₓ) { Real ΔC = 1;; unsigned it = 0; while (sqrt(ΔC / C.size()) > ε) { it++; - std::vector<Real> RddfC(C.size()); - for (unsigned i = 0; i < C.size(); i++) { - RddfC[i] = R[i] * ddf(λ, p, s, C[i]); - } - std::vector<Complex> RddfCt = fft.fourier(RddfC); - - std::vector<Real> dfC(C.size()); - for (unsigned i = 0; i < C.size(); i++) { - dfC[i] = df(λ, p, s, C[i]); - } - std::vector<Complex> dfCt = fft.fourier(dfC); + auto [RddfCt, dfCt] = RddfCtdfCt(fft, C, R, p, s, λ); for (unsigned i = 0; i < Rt.size(); i++) { Real ω = i * Δω; @@ -230,21 +155,15 @@ int main(int argc, char* argv[]) { } } - Real energy = 0; - - for (unsigned i = 0; i < n; i++) { - energy += y * R[i] * df(λ, p, s, C[i]) * M_PI * Δτ; - } - - std::cerr << "y " << y << " " << energy << std::endl; + Real e = energy(C, R, p, s, λ, y, Δτ); - 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::cerr << "y " << y << " " << e << " " << z << std::endl; - std::ofstream outfile("C_" + file_end, std::ios::out | std::ios::binary); + std::ofstream outfile(fourierFile("C", p, s, λ, τ₀, y, log2n, τₘₐₓ), std::ios::out | std::ios::binary); outfile.write((const char*)(C.data()), C.size() * sizeof(Real)); outfile.close(); - std::ofstream outfileR("R_" + file_end, std::ios::out | std::ios::binary); + std::ofstream outfileR(fourierFile("R", p, s, λ, τ₀, y, log2n, τₘₐₓ), std::ios::out | std::ios::binary); outfileR.write((const char*)(R.data()), R.size() * sizeof(Real)); outfileR.close(); } |