46template <
typename T =
double,
typename Y =
double>
49 virtual Y
a(T t)
const = 0;
50 virtual Y b(T t)
const = 0;
51 virtual Y c(T t)
const = 0;
52 virtual Y d(T t)
const = 0;
54 virtual Y
Sf(T)
const {
return Y(0); };
55 virtual Y Sg(T)
const {
return Y(0); };
56 virtual ~DerivativeMatrix() =
default;
66struct is_complex<std::complex<T>> : std::true_type {};
94template <
typename T,
typename U, std::
size_t N, std::
size_t M>
96 const std::array<U, M> &b) {
97 static_assert(std::is_convertible_v<U, T>,
98 "In inner_product, type of second array (U) must be "
99 "convertable to that of dirst (T)");
100 constexpr std::size_t Size = std::min(N, M);
102 if constexpr (Size == 0) {
104 }
else if constexpr (!std::is_same_v<T, U> && is_complex_v<T>) {
108 for (std::size_t i = 0; i < Size; ++i) {
109 const T y = a[i] *
static_cast<typename T::value_type
>(b[i]) - c;
110 const T tmp = sum + y;
117 for (std::size_t i = 0; i < Size; ++i) {
118 const T y = a[i] *
static_cast<T
>(b[i]) - c;
119 const T tmp = sum + y;
146template <std::
size_t K>
149 std::array<long, K> bk;
161static constexpr auto ADAMS_data = std::tuple{
163 AdamsB<1>{2, {1}, 1},
164 AdamsB<2>{12, {-1, 8}, 5},
165 AdamsB<3>{24, {1, -5, 19}, 9},
166 AdamsB<4>{720, {-19, 106, -264, 646}, 251},
167 AdamsB<5>{1440, {27, -173, 482, -798, 1427}, 475},
168 AdamsB<6>{60480, {-863, 6312, -20211, 37504, -46461, 65112}, 19087},
170 120960, {1375, -11351, 41499, -88547, 123133, -121797, 139849}, 36799},
173 {-33953, 312874, -1291214, 3146338, -5033120, 5595358, -4604594, 4467094},
176 {57281, -583435, 2687864, -7394032, 13510082, -17283646, 16002320,
179 AdamsB<10>{479001600,
180 {-3250433, 36284876, -184776195, 567450984, -1170597042,
181 1710774528, -1823311566, 1446205080, -890175549, 656185652},
183 AdamsB<11>{958003200,
184 {5675265, -68928781, 384709327, -1305971115, 3007739418,
185 -4963166514, 6043521486, -5519460582, 3828828885, -2092490673,
188 AdamsB<12>{2615348736000,
189 {-13695779093, 179842822566, -1092096992268, 4063327863170,
190 -10344711794985, 19058185652796, -26204344465152, 27345870698436,
191 -21847538039895, 13465774256510, -6616420957428, 3917551216986},
200static constexpr std::size_t K_max =
201 std::tuple_size_v<
decltype(helper::ADAMS_data)> - 1;
220template <std::
size_t K,
typename = std::enable_if_t<(K > 0)>,
221 typename = std::enable_if_t<(K <= K_max)>>
226 static constexpr std::array<double, K> make_ak() {
227 const auto &am = std::get<K>(helper::ADAMS_data);
230 "Kth Entry in ADAMS_data must correspond to K-order AM method");
231 std::array<double, K> tak{};
232 for (std::size_t i = 0; i < K; ++i) {
233 tak.at(i) = double(am.bk.at(i)) / double(am.denom);
239 static constexpr double make_aK() {
240 const auto &am = std::get<K>(helper::ADAMS_data);
241 return (
double(am.bK) /
double(am.denom));
246 static constexpr std::array<double, K>
ak{make_ak()};
248 static constexpr double aK{make_aK()};
414template <std::
size_t K,
typename T =
double,
typename Y =
double>
416 static_assert(K > 0,
"Order (K) for Adams method must be K>0");
417 static_assert(K <= K_max,
418 "Order (K) requested for Adams method too "
419 "large. Adams coefficients are implemented up to K_max-1 only");
421 is_complex_v<Y> || std::is_floating_point_v<Y>,
422 "Template parameter Y (function values and dt) must be floating point "
424 static_assert(std::is_floating_point_v<T> || std::is_integral_v<T> ||
426 "Template parameter T (derivative matrix argument) must be "
427 "floating point, complex, or integral");
446 std::array<Y, K>
f{}, g{};
449 std::array<Y, K>
df{}, dg{};
452 std::array<T, K>
t{};
467 assert(
dt != Y{0.0} &&
"Cannot have zero step-size in ODESolver2D");
468 assert(D !=
nullptr &&
"Cannot have null Derivative Matrix in ODESolver2D");
472 constexpr std::size_t
K_steps()
const {
return K; }
481 Y
dt()
const {
return m_dt; }
484 Y
dfdt(Y ft, Y gt, T tt)
const {
485 return m_D->
a(tt) * ft + m_D->b(tt) * gt + S_scale * m_D->
Sf(tt);
488 Y
dgdt(Y ft, Y gt, T tt)
const {
489 return m_D->c(tt) * ft + m_D->d(tt) * gt + S_scale * m_D->Sg(tt);
514 am.aK * S_scale * m_D->
Sf(t_next));
515 const auto sg = g.back() + m_dt * (
inner_product(dg, am.ak) +
516 am.aK * S_scale * m_D->Sg(t_next));
518 const auto a = m_D->
a(t_next);
519 const auto b = m_D->b(t_next);
520 const auto c = m_D->c(t_next);
521 const auto d = m_D->d(t_next);
523 const auto a0 = m_dt *
static_cast<Y
>(am.aK);
524 const auto a02 = a0 * a0;
526 Y{1.0} / (Y{1.0} - (a02 * (b * c - a * d) + a0 * (a + d)));
527 const auto fi = (sf - a0 * (d * sf - b * sg)) * det_inv;
528 const auto gi = (sg - a0 * (-c * sf + a * sg)) * det_inv;
532 std::rotate(
f.begin(),
f.begin() + 1,
f.end());
533 std::rotate(g.begin(), g.begin() + 1, g.end());
534 std::rotate(
df.begin(),
df.begin() + 1,
df.end());
535 std::rotate(dg.begin(), dg.begin() + 1, dg.end());
536 std::rotate(
t.begin(),
t.begin() + 1,
t.end());
542 df.back() =
dfdt(fi, gi, t_next);
543 dg.back() =
dgdt(fi, gi, t_next);
569 df.at(0) =
dfdt(f0, g0, t0);
570 dg.at(0) =
dgdt(f0, g0, t0);
571 first_k_i<1>(next_t(t0));
579 if constexpr (std::is_integral_v<T> && is_complex_v<Y>) {
581 }
else if constexpr (std::is_integral_v<T>) {
589 template <std::
size_t ik>
590 void first_k_i(T t_next) {
591 if constexpr (ik >= K) {
595 constexpr AM_Coefs<ik> ai{};
598 ai.aK * S_scale * m_D->
Sf(t_next));
599 const auto sg = g.at(ik - 1) + m_dt * (
inner_product(dg, ai.ak) +
600 ai.aK * S_scale * m_D->Sg(t_next));
601 const auto a0 = m_dt *
static_cast<Y
>(ai.aK);
602 const auto a02 = a0 * a0;
603 const auto a = m_D->
a(t_next);
604 const auto b = m_D->b(t_next);
605 const auto c = m_D->c(t_next);
606 const auto d = m_D->d(t_next);
608 Y{1.0} / (Y{1.0} - (a02 * (b *
c - a * d) + a0 * (a + d)));
609 const auto fi = (sf - a0 * (d * sf - b * sg)) * det_inv;
610 const auto gi = (sg - a0 * (-
c * sf + a * sg)) * det_inv;
615 df.at(ik) =
dfdt(fi, gi, t_next);
616 dg.at(ik) =
dgdt(fi, gi, t_next);
618 first_k_i<ik + 1>(next_t(t_next));
Solves a 2x2 system of ODEs using a K-step Adams-Moulton method.
Definition AdamsMoulton.hpp:415
std::array< T, K > t
Array to store the previous K values of t: f.at(i) = f(t.at(i))
Definition AdamsMoulton.hpp:452
Y last_f() const
Returns most recent f value. Can also access f array directly.
Definition AdamsMoulton.hpp:475
constexpr std::size_t K_steps() const
Returns the AM order (number of steps), K.
Definition AdamsMoulton.hpp:472
void drive(T t_next)
Drives the ODE system one step to t_next, given the K previous values.
Definition AdamsMoulton.hpp:508
void solve_initial_K(T t0, Y f0, Y g0)
Sets the first K values of F (and dF) given a single initial condition.
Definition AdamsMoulton.hpp:565
Y last_g() const
Returns most recent g value. Can also access g array directly.
Definition AdamsMoulton.hpp:477
Y dt() const
Returns the step size.
Definition AdamsMoulton.hpp:481
Y dgdt(Y ft, Y gt, T tt) const
Returns derivative, dg/dt(t), given f(t),g(t),t.
Definition AdamsMoulton.hpp:488
Y dfdt(Y ft, Y gt, T tt) const
Returns derivative, df/dt(t), given f(t),g(t),t.
Definition AdamsMoulton.hpp:484
void drive()
Overload of drive(T t) for 'default' case, where next t is defined as last_t + dt (for arithmetic/com...
Definition AdamsMoulton.hpp:549
ODESolver2D(Y dt, const DerivativeMatrix< T, Y > *D)
Constructs the ODE solver with a given step size and derivative matrix.
Definition AdamsMoulton.hpp:466
T last_t() const
Returns most recent t value; last_f() := f(last_t())
Definition AdamsMoulton.hpp:479
std::array< Y, K > df
Arrays to store the previous K values of derivatives, df and dg.
Definition AdamsMoulton.hpp:449
std::array< Y, K > f
Arrays storing the previous K values of f and g.
Definition AdamsMoulton.hpp:446
Contains classes and functions which use general N-step Adams Moulton method to solve systems of 2x2 ...
Definition AdamsMoulton.hpp:11
constexpr bool is_complex_v
Type trait: true if T is std::complex<U> for some U, false otherwise.
Definition AdamsMoulton.hpp:80
constexpr T inner_product(const std::array< T, N > &a, const std::array< U, M > &b)
Inner product of two std::arrays: sum_i a_i * b_i.
Definition AdamsMoulton.hpp:95
constexpr double c
speed of light in a.u. (=1/alpha)
Definition PhysConst_constants.hpp:63
Holds the K+1 Adams-Moulton coefficients for the K-step AM method.
Definition AdamsMoulton.hpp:222
static constexpr std::array< double, K > ak
First K coefficients: ak for k={0,1,...,K-1}.
Definition AdamsMoulton.hpp:246
static constexpr double aK
Final aK coefficients: ak for k=K.
Definition AdamsMoulton.hpp:248
Pure-virtual struct defining the derivative matrix for a 2x2 ODE system.
Definition AdamsMoulton.hpp:47
virtual Y a(T t) const =0
a,b,c,d are derivative matrix functions; all must be user implemented
virtual Y Sf(T) const
Sf and Sg are optional inhomogenous terms.
Definition AdamsMoulton.hpp:54
Type trait: true iff T is std::complex<U> for some U.
Definition Matrix.hpp:19