#include #include #include #include using Real = double; unsigned p = 2; Real f(Real q) { return 0.5 * pow(q, p); } Real df(Real q) { return 0.5 * p * pow(q, p - 1); } Real ddf(Real q) { return 0.5 * p * (p - 1) * pow(q, p - 2); } Real integrate(const std::vector& C, signed τ = std::numeric_limits::max()) { Real I = 0; if (τ > C.size() - 1) { τ = C.size() - 1; } #pragma omp parallel for reduction(+:I) for (unsigned σ = 0; σ < τ; σ++) { unsigned τ_σ = τ - σ; Real Cτ_σ = (C[τ_σ] + C[τ_σ - 1]) / 2; Real dCσ = C[σ + 1] - C[σ]; I += df(Cτ_σ) * dCσ; } return I; } Real integratePast(const std::vector& C, signed τ) { Real I = 0; #pragma omp parallel for reduction(+:I) for (signed σ = -C.size() + τ + 3; σ < τ - 2; σ++) { signed τ_σ = τ - σ; Real Cτ_σ = (C[abs(τ_σ)] + C[abs(τ_σ) - 1]) / 2; Real Cσ = (C[abs(σ) + 1] + C[abs(σ)]) / 2; Real dddC; if (τ_σ != 0) { dddC = (τ_σ / abs(τ_σ)) * (C[abs(τ_σ)+2] - 2 * C[abs(τ_σ)+1] + 2 * C[abs(τ_σ)-1] - C[abs(τ_σ)-2]) / 2; } else { dddC = 0; } I += dddC * ddf(Cτ_σ) * Cσ; } #pragma omp parallel for reduction(+:I) for (signed σ = -C.size() + τ + 3; σ < -1; σ++) { signed τ_σ = τ - σ; Real Cτ_σ = (C[abs(τ_σ)] + C[abs(τ_σ) - 1]) / 2; Real dddC; if (σ != 0) { dddC = -(σ / abs(σ)) * (C[abs(σ)+2] - 2 * C[abs(σ)+1] + 2 * C[abs(σ)-1] - C[abs(σ)-2]) / 2; } else { dddC = 0; } I += dddC * df(Cτ_σ); } return I; } Real integrateDelay(const std::vector& C, unsigned τ, Real Δτ, Real τ₀) { Real I = 0; #pragma omp parallel for reduction(+:I) for (signed σ = 2; σ < C.size() - τ - 2; σ++) { unsigned τ_σ = τ + σ; Real dC = -(C[σ+1] - C[σ-1]) / 2; Real dddC = -(C[σ+2] - 2 * C[σ+1] + 2 * C[σ-1] - C[σ-2]) / 2; I += (dC - pow(τ₀ / Δτ, 2) * dddC) * exp(-(τ_σ * Δτ / τ₀)); } return I / τ₀; } Real energy(const std::vector& C, Real Δτ, Real τ₀) { Real I = 0; for (unsigned σ = 0; σ < C.size() - 1; σ++) { Real Cσ = (C[σ] + C[σ + 1]) / 2; Real dC = (C[σ + 1] - C[σ]) / Δτ; Real dddC = 0; if (σ > 1 && σ < C.size() - 2 && C.size() > 3) { dddC = (C[σ+1] - 3 * C[σ] + 3 * C[σ-1] - C[σ-2]) / pow(Δτ, 3); } I += Δτ * df(Cσ) * (dC - pow(τ₀, 2) * dddC); } return I; } int main(int argc, char* argv[]) { Real Δτ = 1e-3; Real τₘₐₓ = 1e3; Real τ₀ = 0; Real y = 0.5; unsigned iterations = 10; int opt; while ((opt = getopt(argc, argv, "d:T:t:y:I:")) != -1) { switch (opt) { case 'd': Δτ = atof(optarg); break; case 'T': τₘₐₓ = atof(optarg); break; case 't': τ₀ = atof(optarg); break; case 'y': y = atof(optarg); break; case 'I': iterations = (unsigned)atof(optarg); break; default: exit(1); } } Real z = 0.5; Real Γ₀ = 1; Real τ = 0; std::vector C; C.reserve(τₘₐₓ / Δτ + 1); C.push_back(1); // while (std::cout << τ << " " << C.back() << std::endl, τ < τₘₐₓ) { while (τ < τₘₐₓ) { τ += Δτ; Real dC = -(z - 2 * pow(y, 2)) * C.back() - 2 / Γ₀ * pow(y, 2) * integrate(C); C.push_back(C.back() + Δτ * dC); } for (unsigned it = 0; it < iterations; it++) { τ = 0; std::vector C2; C2.reserve(τₘₐₓ / Δτ + 1); C2.push_back(1); while (τ < τₘₐₓ) { τ += Δτ; Real dC = -(z - 2 * pow(y, 2)) * C2.back() + integrateDelay(C, C2.size() - 1, Δτ, τ₀) - 2 / Γ₀ * pow(y, 2) * (integrate(C2) - pow(τ₀ / Δτ, 2) * integratePast(C, C2.size()-1)); C2.push_back(C2.back() + Δτ * dC); } Real error = 0; for (unsigned i = 0; i < std::min(C.size(), C2.size()); i++) { error += pow(C[i] - C2[i], 2); } std::cerr << "Iteration " << it << ": " << sqrt(error / C.size()) << " " << z << std::endl; C = C2; } /* Real zNew = (2.0 * ((C[2] - 2 * C[1] + C[0]) / pow(Δτ, 2) - pow(τ₀, 2) * (C[4] - 4 * C[3] + 6 * C[2] - 4 * C[1] + C[0]) / pow(Δτ, 4))); Real zNew = (2.0 * ((C[2] - 2 * C[1] + C[0]) / pow(Δτ, 2) - pow(τ₀, 2) * (C[4] - 4 * C[3] + 6 * C[2] - 4 * C[1] + C[0]) / pow(Δτ, 4))); // Real zNew = (2.0 * ((83 * C[6] - 245 * C[5] + 101 * C[4] + 254 * C[3] - 31 * C[2] - 377 * C[1] + 215 * C[0]) / (132 * pow(Δτ, 2)) - pow(τ₀, 2) * (3 * C[6] - 7 * C[5] + C[4] + 6 * C[3] + C[2] - 7 * C[1] + 3 * C[0]) / (11 * pow(Δτ, 4)))); z = z / zNew; τ = 0; C.clear(); C.reserve(τₘₐₓ / Δτ + 1); C.push_back(1); // while (std::cout << τ << " " << C.back() << std::endl, τ < τₘₐₓ) { while (τ < τₘₐₓ) { τ += Δτ; Real dC = -z * C.back() - 2 / Γ₀ * pow(y, 2) * integrate(C); C.push_back(C.back() + Δτ * dC); } */ τ = 0; for (Real Ci : C) { std::cout << τ << " " << Ci << std::endl; τ += Δτ; } std::cerr << - 2 * y / Γ₀ * energy(C, Δτ, τ₀) << std::endl; return 0; }