diff options
author | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2025-04-18 23:02:43 -0300 |
---|---|---|
committer | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2025-04-18 23:02:43 -0300 |
commit | e4ab12ce914b2471355a99943b58c5b274d8754c (patch) | |
tree | ce730c80936dba6ed4ac82e210cd5b7faddbc258 | |
parent | 92bd43e33e79a7d682267d3f6054e8b1dd9d00db (diff) | |
download | code-e4ab12ce914b2471355a99943b58c5b274d8754c.tar.gz code-e4ab12ce914b2471355a99943b58c5b274d8754c.tar.bz2 code-e4ab12ce914b2471355a99943b58c5b274d8754c.zip |
Refactor
-rw-r--r-- | Makefile | 24 | ||||
-rw-r--r-- | fourier.cpp | 125 | ||||
-rw-r--r-- | fourier.hpp | 39 | ||||
-rw-r--r-- | integrator.cpp | 1 | ||||
-rw-r--r-- | log-fourier.cpp | 102 | ||||
-rw-r--r-- | log-fourier.hpp | 33 | ||||
-rw-r--r-- | log-fourier_integrator.cpp | 3 | ||||
-rw-r--r-- | p-spin.cpp | 25 | ||||
-rw-r--r-- | p-spin.hpp | 6 | ||||
-rw-r--r-- | types.hpp | 6 |
10 files changed, 193 insertions, 171 deletions
@@ -8,17 +8,23 @@ walk: walk.cpp correlations: correlations.cpp $(CC) correlations.cpp -o correlations -integrator: integrator.cpp fourier.o fourier.hpp - $(CC) integrator.cpp fourier.o -lfftw3 -lgsl -o integrator +integrator: integrator.cpp fourier.o p-spin.o fourier.hpp + $(CC) integrator.cpp fourier.o p-spin.o -lfftw3 -lgsl -o integrator -fourier.o: fourier.cpp fourier.hpp +fourier.o: fourier.cpp fourier.hpp types.hpp $(CC) fourier.cpp -c -o fourier.o -fourier_integrator: fourier_integrator.cpp fourier.hpp fourier.o - $(CC) fourier_integrator.cpp fourier.o -lfftw3 -lgsl -o fourier_integrator +p-spin.o: p-spin.cpp p-spin.hpp types.hpp + $(CC) p-spin.cpp -c -o p-spin.o -log-fourier_integrator: log-fourier_integrator.cpp fourier.hpp fourier.o - $(CC) log-fourier_integrator.cpp fourier.o -lfftw3 -lgsl -o log-fourier_integrator +log-fourier.o: log-fourier.cpp log-fourier.hpp types.hpp + $(CC) log-fourier.cpp -o log-fourier.o -get_energy: get_energy.cpp fourier.hpp fourier.o - $(CC) get_energy.cpp fourier.o -lfftw3 -lgsl -o get_energy +fourier_integrator: fourier_integrator.cpp fourier.hpp fourier.o p-spin.o + $(CC) fourier_integrator.cpp fourier.o p-spin.o -lfftw3 -lgsl -o fourier_integrator + +log-fourier_integrator: log-fourier_integrator.cpp log-fourier.hpp log-fourier.o p-spin.o + $(CC) log-fourier_integrator.cpp log-fourier.o p-spin.o -lfftw3 -lgsl -o log-fourier_integrator + +get_energy: get_energy.cpp fourier.hpp fourier.o p-spin.o + $(CC) get_energy.cpp fourier.o p-spin.o -lfftw3 -lgsl -o get_energy diff --git a/fourier.cpp b/fourier.cpp index 15cad52..95d0025 100644 --- a/fourier.cpp +++ b/fourier.cpp @@ -1,30 +1,7 @@ #include "fourier.hpp" +#include "p-spin.hpp" #include <fftw3.h> -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); -} - -Real f(Real λ, unsigned p, unsigned s, Real q) { - return (1 - λ) * fP(p, q) + λ * fP(s, q); -} - -Real df(Real λ, unsigned p, unsigned s, Real q) { - return (1 - λ) * dfP(p, q) + λ * dfP(s, q); -} - -Real ddf(Real λ, unsigned p, unsigned s, Real q) { - return (1 - λ) * ddfP(p, q) + λ * ddfP(s, q); -} - FourierTransform::FourierTransform(unsigned n, Real Δω, Real Δτ, unsigned flags) : n(n), Δω(Δω), Δτ(Δτ) { a = fftw_alloc_real(2 * n); â = reinterpret_cast<Complex*>(fftw_alloc_complex(n + 1)); @@ -100,106 +77,6 @@ void FourierTransform::writeToA(unsigned i, Real ai) { a[i] = ai; } -LogarithmicFourierTransform::LogarithmicFourierTransform(unsigned N, Real k, Real Δτ, unsigned pad) : N(N), pad(pad), k(k), Δτ(Δτ) { - τₛ = -0.5 * N; - ωₛ = -0.5 * N; - sₛ = -0.5 * pad * N; - a = reinterpret_cast<Complex*>(fftw_alloc_complex(pad*N)); - â = reinterpret_cast<Complex*>(fftw_alloc_complex(pad*N)); - fftw_import_wisdom_from_filename("fftw.wisdom"); - a_to_â = fftw_plan_dft_1d(pad*N, reinterpret_cast<fftw_complex*>(a), reinterpret_cast<fftw_complex*>(â), FFTW_BACKWARD, 0); - â_to_a = fftw_plan_dft_1d(pad*N, reinterpret_cast<fftw_complex*>(â), reinterpret_cast<fftw_complex*>(a), FFTW_BACKWARD, 0); - fftw_export_wisdom_to_filename("fftw.wisdom"); -} - -LogarithmicFourierTransform::~LogarithmicFourierTransform() { - fftw_destroy_plan(a_to_â); - fftw_destroy_plan(â_to_a); - fftw_free(a); - fftw_free(â); - fftw_cleanup(); -} - -Real LogarithmicFourierTransform::τ(unsigned n) const { - return Δτ * (n + τₛ); -} - -Real LogarithmicFourierTransform::ω(unsigned n) const { - return Δτ * (n + ωₛ); -} - -Real LogarithmicFourierTransform::s(unsigned n) const { - return (n + sₛ) * 2*M_PI / (pad * N * Δτ); -} - -Real LogarithmicFourierTransform::t(unsigned n) const { - return exp(τ(n)); -} - -Real LogarithmicFourierTransform::ν(unsigned n) const { - return exp(ω(n)); -} - -Complex gamma(Complex z) { - gsl_sf_result logΓ; - gsl_sf_result argΓ; - - gsl_sf_lngamma_complex_e(z.real(), z.imag(), &logΓ, &argΓ); - - return exp(logΓ.val + 1i * argΓ.val); -} - -std::vector<Complex> LogarithmicFourierTransform::fourier(const std::vector<Real>& c, bool symmetric) { - std::vector<Complex> ĉ(N); - std::vector<Real> σs = {1}; - if (symmetric){ - σs.push_back(-1); - } - for (Real σ : σs) { - for (unsigned n = 0; n < pad*N; n++) { - if (n < N) { - a[n] = c[n] * exp((1 - k) * τ(n)); - } else { - a[n] = 0; - } - } - fftw_execute(a_to_â); - for (unsigned n = 0; n < pad*N; n++) { - â[(pad*N / 2 + n) % (pad*N)] *= pow(1i * σ, 1i * s(n) - k) * gamma(k - 1i * s(n)); - } - fftw_execute(â_to_a); - for (unsigned n = 0; n < N; n++) { - ĉ[n] += exp(-k * ω(n)) * a[(pad - 1)*N+n] / (Real)(pad*N); - } - } - - return ĉ; -} - -std::vector<Real> LogarithmicFourierTransform::inverse(const std::vector<Complex>& ĉ) { - std::vector<Real> c(N); - std::vector<Real> σs = {1, -1}; - for (Real σ : σs) { - for (unsigned n = 0; n < pad * N; n++) { - if (n < N) { - a[n] = ĉ[n] * exp((1 - k) * ω(n)); - } else { - a[n] = 0; - } - } - fftw_execute(a_to_â); - for (unsigned n = 0; n < pad*N; n++) { - â[(pad*N / 2 + n) % (pad*N)] *= pow(-1i * σ, 1i * s(n) - k) * gamma(k - 1i * s(n)); - } - fftw_execute(â_to_a); - for (unsigned n = 0; n < N; n++) { - c[n] += exp(-k * τ(n)) * a[(pad - 1)*N+n].real() / (Real)(pad*N) / (2 * M_PI); - } - } - - return c; -} - std::string fourierFile(std::string prefix, unsigned p, unsigned s, Real λ, Real τ₀, Real y, unsigned log2n, Real τₘₐₓ) { return prefix + "_" + 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"; } diff --git a/fourier.hpp b/fourier.hpp index 1708667..6be0931 100644 --- a/fourier.hpp +++ b/fourier.hpp @@ -1,23 +1,15 @@ #pragma once +#include "types.hpp" + #include <cmath> #include <fftw3.h> -#include <complex> #include <vector> #include <tuple> -#include <gsl/gsl_sf_gamma.h> #ifndef FFTW_THREADS #define FFTW_THREADS 1 #endif -using Real = double; -using Complex = std::complex<Real>; -using namespace std::complex_literals; - -Real f(Real λ, unsigned p, unsigned s, Real q); -Real df(Real λ, unsigned p, unsigned s, Real q); -Real ddf(Real λ, unsigned p, unsigned s, Real q); - class FourierTransform { private: Real* a; @@ -37,33 +29,6 @@ public: std::vector<Real> convolve(const std::vector<Real>& Γh, const std::vector<Real>& R); }; -class LogarithmicFourierTransform { -private: - Complex* a; - Complex* â; - fftw_plan a_to_â; - fftw_plan â_to_a; - unsigned N; - unsigned pad; - Real k; - Real Δτ; - Real τₛ; - Real ωₛ; - Real sₛ; -public: - LogarithmicFourierTransform(unsigned N, Real k, Real Δτ, unsigned pad = 4); - ~LogarithmicFourierTransform(); - Real τ(unsigned n) const; - Real ω(unsigned n) const; - Real t(unsigned n) const; - Real ν(unsigned n) const; - Real s(unsigned n) const; - std::vector<Complex> fourier(const std::vector<Real>& c, bool symmetric); - std::vector<Real> inverse(const std::vector<Complex>& ĉ); -}; - -Complex gamma(Complex z); - std::string fourierFile(std::string prefix, unsigned p, unsigned s, Real λ, Real τ₀, Real y, unsigned log2n, Real τₘₐₓ); Real energy(const std::vector<Real>& C, const std::vector<Real>& R, unsigned p, unsigned s, Real λ, Real y, Real Δτ); std::tuple<std::vector<Complex>, std::vector<Complex>> RddfCtdfCt(FourierTransform& fft, const std::vector<Real>& C, const std::vector<Real>& R, unsigned p, unsigned s, Real λ); diff --git a/integrator.cpp b/integrator.cpp index 7e5c512..4f8246d 100644 --- a/integrator.cpp +++ b/integrator.cpp @@ -1,4 +1,5 @@ #include "fourier.hpp" +#include "p-spin.hpp" #include <fstream> #include <fftw3.h> #include <getopt.h> diff --git a/log-fourier.cpp b/log-fourier.cpp new file mode 100644 index 0000000..1e2e7d9 --- /dev/null +++ b/log-fourier.cpp @@ -0,0 +1,102 @@ +#include "log-fourier.hpp" + +LogarithmicFourierTransform::LogarithmicFourierTransform(unsigned N, Real k, Real Δτ, unsigned pad) : N(N), pad(pad), k(k), Δτ(Δτ) { + τₛ = -0.5 * N; + ωₛ = -0.5 * N; + sₛ = -0.5 * pad * N; + a = reinterpret_cast<Complex*>(fftw_alloc_complex(pad*N)); + â = reinterpret_cast<Complex*>(fftw_alloc_complex(pad*N)); + fftw_import_wisdom_from_filename("fftw.wisdom"); + a_to_â = fftw_plan_dft_1d(pad*N, reinterpret_cast<fftw_complex*>(a), reinterpret_cast<fftw_complex*>(â), FFTW_BACKWARD, 0); + â_to_a = fftw_plan_dft_1d(pad*N, reinterpret_cast<fftw_complex*>(â), reinterpret_cast<fftw_complex*>(a), FFTW_BACKWARD, 0); + fftw_export_wisdom_to_filename("fftw.wisdom"); +} + +LogarithmicFourierTransform::~LogarithmicFourierTransform() { + fftw_destroy_plan(a_to_â); + fftw_destroy_plan(â_to_a); + fftw_free(a); + fftw_free(â); + fftw_cleanup(); +} + +Real LogarithmicFourierTransform::τ(unsigned n) const { + return Δτ * (n + τₛ); +} + +Real LogarithmicFourierTransform::ω(unsigned n) const { + return Δτ * (n + ωₛ); +} + +Real LogarithmicFourierTransform::s(unsigned n) const { + return (n + sₛ) * 2*M_PI / (pad * N * Δτ); +} + +Real LogarithmicFourierTransform::t(unsigned n) const { + return exp(τ(n)); +} + +Real LogarithmicFourierTransform::ν(unsigned n) const { + return exp(ω(n)); +} + +Complex gamma(Complex z) { + gsl_sf_result logΓ; + gsl_sf_result argΓ; + + gsl_sf_lngamma_complex_e(z.real(), z.imag(), &logΓ, &argΓ); + + return exp(logΓ.val + 1i * argΓ.val); +} + +std::vector<Complex> LogarithmicFourierTransform::fourier(const std::vector<Real>& c, bool symmetric) { + std::vector<Complex> ĉ(N); + std::vector<Real> σs = {1}; + if (symmetric){ + σs.push_back(-1); + } + for (Real σ : σs) { + for (unsigned n = 0; n < pad*N; n++) { + if (n < N) { + a[n] = c[n] * exp((1 - k) * τ(n)); + } else { + a[n] = 0; + } + } + fftw_execute(a_to_â); + for (unsigned n = 0; n < pad*N; n++) { + â[(pad*N / 2 + n) % (pad*N)] *= pow(1i * σ, 1i * s(n) - k) * gamma(k - 1i * s(n)); + } + fftw_execute(â_to_a); + for (unsigned n = 0; n < N; n++) { + ĉ[n] += exp(-k * ω(n)) * a[(pad - 1)*N+n] / (Real)(pad*N); + } + } + + return ĉ; +} + +std::vector<Real> LogarithmicFourierTransform::inverse(const std::vector<Complex>& ĉ) { + std::vector<Real> c(N); + std::vector<Real> σs = {1, -1}; + for (Real σ : σs) { + for (unsigned n = 0; n < pad * N; n++) { + if (n < N) { + a[n] = ĉ[n] * exp((1 - k) * ω(n)); + } else { + a[n] = 0; + } + } + fftw_execute(a_to_â); + for (unsigned n = 0; n < pad*N; n++) { + â[(pad*N / 2 + n) % (pad*N)] *= pow(-1i * σ, 1i * s(n) - k) * gamma(k - 1i * s(n)); + } + fftw_execute(â_to_a); + for (unsigned n = 0; n < N; n++) { + c[n] += exp(-k * τ(n)) * a[(pad - 1)*N+n].real() / (Real)(pad*N) / (2 * M_PI); + } + } + + return c; +} + diff --git a/log-fourier.hpp b/log-fourier.hpp new file mode 100644 index 0000000..f57c6bc --- /dev/null +++ b/log-fourier.hpp @@ -0,0 +1,33 @@ +#pragma once +#include "types.hpp" + +#include <cmath> +#include <fftw3.h> +#include <vector> +#include <gsl/gsl_sf_gamma.h> + +class LogarithmicFourierTransform { +private: + Complex* a; + Complex* â; + fftw_plan a_to_â; + fftw_plan â_to_a; + unsigned N; + unsigned pad; + Real k; + Real Δτ; + Real τₛ; + Real ωₛ; + Real sₛ; +public: + LogarithmicFourierTransform(unsigned N, Real k, Real Δτ, unsigned pad = 4); + ~LogarithmicFourierTransform(); + Real τ(unsigned n) const; + Real ω(unsigned n) const; + Real t(unsigned n) const; + Real ν(unsigned n) const; + Real s(unsigned n) const; + std::vector<Complex> fourier(const std::vector<Real>& c, bool symmetric); + std::vector<Real> inverse(const std::vector<Complex>& ĉ); +}; + diff --git a/log-fourier_integrator.cpp b/log-fourier_integrator.cpp index eda4945..061cb08 100644 --- a/log-fourier_integrator.cpp +++ b/log-fourier_integrator.cpp @@ -1,4 +1,5 @@ -#include "fourier.hpp" +#include "log-fourier.hpp" +#include "p-spin.hpp" #include <getopt.h> #include <iostream> diff --git a/p-spin.cpp b/p-spin.cpp new file mode 100644 index 0000000..3691ed6 --- /dev/null +++ b/p-spin.cpp @@ -0,0 +1,25 @@ +#include "p-spin.hpp" + +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); +} + +Real f(Real λ, unsigned p, unsigned s, Real q) { + return (1 - λ) * fP(p, q) + λ * fP(s, q); +} + +Real df(Real λ, unsigned p, unsigned s, Real q) { + return (1 - λ) * dfP(p, q) + λ * dfP(s, q); +} + +Real ddf(Real λ, unsigned p, unsigned s, Real q) { + return (1 - λ) * ddfP(p, q) + λ * ddfP(s, q); +} diff --git a/p-spin.hpp b/p-spin.hpp new file mode 100644 index 0000000..c293d65 --- /dev/null +++ b/p-spin.hpp @@ -0,0 +1,6 @@ +#pragma once +#include "types.hpp" + +Real f(Real λ, unsigned p, unsigned s, Real q); +Real df(Real λ, unsigned p, unsigned s, Real q); +Real ddf(Real λ, unsigned p, unsigned s, Real q); diff --git a/types.hpp b/types.hpp new file mode 100644 index 0000000..05eceb4 --- /dev/null +++ b/types.hpp @@ -0,0 +1,6 @@ +#pragma once +#include <complex> + +using Real = double; +using Complex = std::complex<Real>; +using namespace std::complex_literals; |