Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
integrator.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
19
20#include <functional>
21
22namespace shammath {
23
24 template<class T, class Lambda>
25 inline constexpr T integ_riemann_sum(T start, T end, T step, Lambda &&fct) {
26 T acc = {};
27
28 for (T x = start; x < end; x += step) {
29 acc += fct(x) * step;
30 }
31 return acc;
32 }
33
50 template<class T, class Lambda>
51 inline constexpr std::pair<std::vector<T>, std::vector<T>> euler_ode(
52 T start, T end, T step, Lambda &&ode, T x0, T u0) {
53 std::vector<T> U = {u0};
54 std::vector<T> X = {x0};
55
56 T u_prev = u0;
57 T u = u0;
58 for (T x = x0 + step; x < end; x += step) {
59 u = u_prev + ode(u_prev, x) * step;
60 X.push_back(x);
61 U.push_back(u);
62 u_prev = u;
63 };
64 u_prev = u0;
65 std::vector<T> X_backward, U_backward;
66 for (T x = x0 - step; x > start; x -= step) {
67 u = u_prev - ode(u_prev, x) * step;
68 X_backward.push_back(x);
69 U_backward.push_back(u);
70 u_prev = u;
71 }
72 X.insert(X.begin(), X_backward.rbegin(), X_backward.rend());
73 U.insert(U.begin(), U_backward.rbegin(), U_backward.rend());
74 return {X, U};
75 }
76
77} // namespace shammath
namespace for math utility
Definition AABB.hpp:26
constexpr std::pair< std::vector< T >, std::vector< T > > euler_ode(T start, T end, T step, Lambda &&ode, T x0, T u0)
Euler solving of ODE The ode has the form.