diff options
author | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2025-03-29 07:34:24 -0300 |
---|---|---|
committer | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2025-03-29 07:34:24 -0300 |
commit | 5bf9d56ab096878498eee9a8b91a093a494910f0 (patch) | |
tree | b446ae6692ee3c5224a7cfd7bd04757ceca9d42b | |
parent | 82a6f374485989431754b63500a9db2a5f9cda3f (diff) | |
download | code-5bf9d56ab096878498eee9a8b91a093a494910f0.tar.gz code-5bf9d56ab096878498eee9a8b91a093a494910f0.tar.bz2 code-5bf9d56ab096878498eee9a8b91a093a494910f0.zip |
Started writing code for integrating mean-field equations
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | integrator.cpp | 85 |
2 files changed, 88 insertions, 1 deletions
@@ -1,4 +1,4 @@ -all: walk correlations +all: walk correlations integrator CC := clang++ -std=c++17 -Wno-mathematical-notation-identifier-extension -O3 -march=native -mtune=native @@ -8,3 +8,5 @@ walk: walk.cpp correlations: correlations.cpp $(CC) correlations.cpp -o correlations +integrator: integrator.cpp + $(CC) integrator.cpp -o integrator diff --git a/integrator.cpp b/integrator.cpp new file mode 100644 index 0000000..bb22a36 --- /dev/null +++ b/integrator.cpp @@ -0,0 +1,85 @@ +#include <getopt.h> +#include <vector> +#include <array> +#include <cmath> +#include <iostream> + +using Real = double; + +class Point : public std::array<Real, 2> { +public: + Real τ() const { + return front(); + } + Real C() const { + return back(); + } +}; + +Real f(Real q) { + return 0.5 * pow(q, 2); +} + +Real df(Real q) { + return q; +} + +Real ddf(Real q) { + return 1; +} + +Real integrate(const std::vector<Point>& Cₜ) { + Real I = 0; + for (unsigned i = 0; i < Cₜ.size() - 1; i++) { + Real Δτ = Cₜ[i + 1].τ() - Cₜ[i].τ(); + Real C = (Cₜ[i + 1].C() + Cₜ[i].C()) / 2; + Real dC = (Cₜ[i + 1].C() - Cₜ[i].C()) / Δτ; + I += Δτ * df(C) * dC; + } + return I; +} + +int main(int argc, char* argv[]) { + Real Δτ = 1e-3; + Real τₘₐₓ = 1e3; + Real τ₀ = 0; + Real y = 0.5; + + int opt; + + while ((opt = getopt(argc, argv, "d:T:t:y:")) != -1) { + switch (opt) { + case 'd': + Δτ = atof(optarg); + break; + case 'T': + τₘₐₓ = atof(optarg); + break; + case 't': + τ₀ = atof(optarg); + break; + case 'y': + y = atof(optarg); + break; + default: + exit(1); + } + } + + std::vector<Point> Cₜ; + Cₜ.reserve(τₘₐₓ / Δτ); + + Cₜ.push_back({0, 1}); + Cₜ.push_back({Δτ, 1 - Δτ}); + + while (Cₜ.back().τ() < τₘₐₓ) { + Real dC = -Cₜ.back().C() - 2 * pow(y, 2) * integrate(Cₜ); + Cₜ.push_back({Cₜ.back().τ() + Δτ, Cₜ.back().C() + Δτ * dC}); + } + + for (const Point& p : Cₜ) { + std::cout << p.τ() << " " << p.C() << std::endl; + } + + return 0; +} |