Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
butcher_tables.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Matteo Caldana <matteo.caldana@polimi.it>
22// Author: Pedro Peixoto <ppeixoto@usp.br>
23
24#ifndef VULPES_TIMEINTEGRATION_BUTCHER_TABLES_HPP_
25#define VULPES_TIMEINTEGRATION_BUTCHER_TABLES_HPP_
26
27#include <array>
28#include <cmath>
29#include <numeric>
30
31namespace vulpes {
32namespace timeintegration {
33
34namespace internal {
35template <typename T>
36constexpr bool checkButcherTable(const T& t) {
37 const double b_sum = std::accumulate(t.b.begin(), t.b.end(), 0.0);
38 const bool b_sum_1 = (std::abs(b_sum - 1.) < 1e-14);
39 if (!b_sum_1) {
40 return false;
41 }
42 for (std::size_t i = 0; i < T::n_stages; ++i) {
43 const auto Arow = std::accumulate(t.A[i].begin(), t.A[i].end(), 0.0);
44 const bool A_row_sum_c = (std::abs(Arow - t.c[i]) < 1e-14);
45 if (!A_row_sum_c) {
46 return false;
47 }
48 }
49 return true;
50}
51
52template <typename T, std::size_t N>
53constexpr std::array<T, N + 1> padStartArray(const std::array<T, N>& input) {
54 std::array<T, N + 1> result{};
55 for (std::size_t i = 0; i < N; ++i) {
56 result[i + 1] = input[i];
57 }
58 return result;
59}
60
61template <typename T, std::size_t N>
62constexpr std::array<T, N + 1> padEndArray(const std::array<T, N>& input) {
63 std::array<T, N + 1> result{};
64 for (std::size_t i = 0; i < N; ++i) {
65 result[i] = input[i];
66 }
67 return result;
68}
69
70template <typename T, std::size_t N>
71constexpr std::array<std::array<T, N + 1>, N + 1> padTopLeftMat(
72 const std::array<std::array<T, N>, N>& input) {
73 std::array<std::array<T, N + 1>, N + 1> result{};
74 for (std::size_t i = 0; i < N; ++i) {
75 for (std::size_t j = 0; j < N; ++j) {
76 result[i + 1][j + 1] = input[i][j];
77 }
78 }
79 return result;
80}
81
82template <typename T, std::size_t N>
83constexpr std::array<std::array<T, N + 1>, N + 1> padTopRightMat(
84 const std::array<std::array<T, N>, N>& input) {
85 std::array<std::array<T, N + 1>, N + 1> result{};
86 for (std::size_t i = 0; i < N; ++i) {
87 for (std::size_t j = 0; j < N; ++j) {
88 result[i + 1][j] = input[i][j];
89 }
90 }
91 return result;
92}
93
94template <typename ButcherTableType>
95constexpr bool checkFSAL() {
96 return (ButcherTableType::c.back() == 1.0) &&
97 (ButcherTableType::A.back() == ButcherTableType::b);
98}
99
100} // namespace internal
101
102template <std::size_t S>
104 using ArrayType = std::array<double, S>;
105 using MatrixType = std::array<std::array<double, S>, S>;
106 static inline constexpr std::size_t n_stages = S;
107 static inline constexpr MatrixType A = {};
108 static inline constexpr ArrayType b = {};
109 static inline constexpr ArrayType c = {};
110};
111
112template <std::size_t S>
114 static inline constexpr std::size_t n_stages = S;
117 static inline constexpr bool has_implicit_part = false;
118 static inline constexpr bool has_explicit_part = false;
119 static inline constexpr MatrixType A_ex = {};
120 static inline constexpr ArrayType b_ex = {};
121 static inline constexpr MatrixType A_im = {};
122 static inline constexpr ArrayType b_im = {};
123 static inline constexpr ArrayType c = {};
124};
125
127 static inline constexpr MatrixType A = {{0}};
128 static inline constexpr ArrayType b = {1};
129 static inline constexpr ArrayType c = {0};
130};
131
133 static inline constexpr MatrixType A = {{1}};
134 static inline constexpr ArrayType b = {1};
135 static inline constexpr ArrayType c = {1};
136};
137
139 static inline constexpr MatrixType A = {{{0, 0}, {0.5, 0.}}};
140 static inline constexpr ArrayType b = {0, 1.};
141 static inline constexpr ArrayType c = {0, 0.5};
142};
143
144struct Heun : ButcherTable<2> {
145 static inline constexpr MatrixType A = {{{0, 0}, {1., 0.}}};
146 static inline constexpr ArrayType b = {0.5, 0.5};
147 static inline constexpr ArrayType c = {0, 1.0};
148};
149
151 static inline constexpr MatrixType A = {{{0, 0}, {2. / 3., 0.}}};
152 static inline constexpr ArrayType b = {0.25, 0.75};
153 static inline constexpr ArrayType c = {0, 2. / 3};
154};
155
157 static inline constexpr MatrixType A = {{{0, 0}, {.5, .5}}};
158 static inline constexpr ArrayType b = {.5, .5};
159 static inline constexpr ArrayType c = {0, 1.};
160};
161
163 static inline constexpr MatrixType A = {
164 {{0, 0, 0}, {0.5, 0, 0}, {0, 0.75, 0}}};
165 static inline constexpr ArrayType b = {2. / 9., 1. / 3., 4. / 9.};
166 static inline constexpr ArrayType c = {0, .5, .75};
167};
168
169struct SSPRK3 : ButcherTable<3> {
170 static inline constexpr MatrixType A = {
171 {{0, 0, 0}, {1., 0, 0}, {0.25, 0.25, 0}}};
172 static inline constexpr ArrayType b = {1. / 6., 1. / 6., 2. / 3.};
173 static inline constexpr ArrayType c = {0, 1., .5};
174};
175
176struct RK4 : ButcherTable<4> {
177 static inline constexpr MatrixType A = {
178 {{0, 0, 0, 0}, {0.5, 0, 0, 0}, {0, 0.5, 0, 0}, {0, 0, 1., 0}}};
179 static inline constexpr ArrayType b = {1. / 6., 1. / 3., 1. / 3., 1. / 6.};
180 static inline constexpr ArrayType c = {0, 0.5, 0.5, 1.0};
181};
182
184 static inline constexpr MatrixType A = {
185 {{0, 0, 0}, {.25, .25, 0}, {0.0, 1.0, 0.0}}};
186 static inline constexpr ArrayType b = {1. / 6., 4. / 6., 1. / 6.};
187 static inline constexpr ArrayType c = {0, 0.5, 1.0};
188};
189
190// Diagonally Implicit Runge-Kutta Methods for Ordinary Differential Equations.
191// A Review. Christopher A. Kennedy, Mark H. Carpenter
192// https://ntrs.nasa.gov/api/citations/20160005923/downloads/20160005923.pdf
194 static inline constexpr MatrixType A = {
195 {{0, 0, 0}, {.25, .25, 0}, {1. / 6., 4. / 6., 1. / 6.}}};
196 static inline constexpr ArrayType b = {1. / 6., 4. / 6., 1. / 6.};
197 static inline constexpr ArrayType c = {0, 0.5, 1.0};
198};
199
201 static inline constexpr MatrixType A = {
202 {{0, 0, 0, 0},
203 {1. / 6., 1. / 6., 0, 0},
204 {1. / 12., 0.5, 1. / 12., 0},
205 {1. / 8., 3. / 8., 3. / 8., 1. / 8.}}};
206 static inline constexpr ArrayType b = {1. / 8., 3. / 8., 3. / 8., 1. / 8.};
207 static inline constexpr ArrayType c = {0, 1. / 3., 2. / 3., 1.0};
208};
209
211 static inline constexpr MatrixType A = {
212 {{1, 0, 0}, {-1. / 12., 5. / 12., 0}, {0, 0.75, 0.25}}};
213 static inline constexpr ArrayType b = {0.0, 0.75, 0.25};
214 static inline constexpr ArrayType c = {1., 1. / 3., 1.0};
215};
216
217// Analysis and implementation of TR-BDF2
218// M.E. Hosea a, L.F. Shampine
219struct TRBDF2 : ButcherTable<3> {
220 static inline constexpr double gamma = 0.58578643762690495124;
221 static inline constexpr double d = 0.29289321881345247562;
222 static inline constexpr double w = 0.3535533905932737622;
223 static inline constexpr MatrixType A = {{{0, 0, 0}, {d, d, 0}, {w, w, d}}};
224 static inline constexpr ArrayType b = {w, w, d};
225 static inline constexpr ArrayType c = {0.0, gamma, 1.0};
226};
227
229 static inline constexpr MatrixType A = {
230 {{0.5, 0, 0, 0},
231 {1. / 6., 1. / 2., 0, 0},
232 {-0.5, 0.5, 0.5, 0.0},
233 {1.5, -1.5, 0.5, 0.5}}};
234 static inline constexpr ArrayType b = {1.5, -1.5, 0.5, 0.5};
235 static inline constexpr ArrayType c = {0.5, 2. / 3., 0.5, 1.};
236};
237
238static_assert(internal::checkButcherTable(ForwardEuler{}));
239static_assert(internal::checkButcherTable(BackwardEuler{}));
240static_assert(internal::checkButcherTable(MidPoint{}));
241static_assert(internal::checkButcherTable(Heun{}));
245static_assert(internal::checkButcherTable(SSPRK3{}));
246static_assert(internal::checkButcherTable(RK4{}));
250static_assert(internal::checkButcherTable(Miller3{}));
251static_assert(internal::checkButcherTable(TRBDF2{}));
253
254template <typename PlainButcher>
255struct ExplicitButcherIMEX : ButcherTableIMEX<PlainButcher::n_stages> {
256 static inline constexpr std::size_t n_stages = PlainButcher::n_stages;
259 static inline constexpr bool has_explicit_part = true;
260 static inline constexpr MatrixType A_ex = PlainButcher::A;
261 static inline constexpr ArrayType b_ex = PlainButcher::b;
262 static inline constexpr ArrayType c = PlainButcher::c;
263};
264
265template <typename PlainButcher>
266struct ImplicitButcherIMEX : ButcherTableIMEX<PlainButcher::n_stages + 1> {
267 static inline constexpr std::size_t n_stages = PlainButcher::n_stages + 1;
270 static inline constexpr bool has_implicit_part = true;
271 static inline constexpr MatrixType A_im =
272 internal::padTopLeftMat(PlainButcher::A);
273 static inline constexpr ArrayType b_im =
274 internal::padStartArray(PlainButcher::b);
275 static inline constexpr ArrayType c =
276 internal::padStartArray(PlainButcher::c);
277};
278
279struct ForwardEulerIMEX : ExplicitButcherIMEX<ForwardEuler> {};
281struct SSPRK3IMEX : ExplicitButcherIMEX<SSPRK3> {};
283
284struct BackwardEulerIMEX : ImplicitButcherIMEX<BackwardEuler> {};
285struct CrankNicolsonIMEX : ImplicitButcherIMEX<CrankNicolson> {};
286struct LobattoIIICIMEX : ImplicitButcherIMEX<LobattoIIIC> {};
287struct CeschinoKunzmann3IMEX : ImplicitButcherIMEX<CeschinoKunzmann3> {};
288struct CeschinoKunzmann4IMEX : ImplicitButcherIMEX<CeschinoKunzmann4> {};
289struct Miller3IMEX : ImplicitButcherIMEX<Miller3> {};
290struct LstableDIRKIMEX : ImplicitButcherIMEX<LstableDIRK> {};
291struct TRBDF2IMEX : ImplicitButcherIMEX<TRBDF2> {};
292
294 static inline constexpr std::size_t n_stages = 2;
297 static inline constexpr bool has_implicit_part = true;
298 static inline constexpr bool has_explicit_part = true;
299 static inline constexpr MatrixType A_ex =
301 static inline constexpr ArrayType b_ex =
303 static inline constexpr MatrixType A_im =
305 static inline constexpr ArrayType b_im =
307 static inline constexpr ArrayType c =
309};
310
312 static inline constexpr std::size_t n_stages = 2;
315 static inline constexpr bool has_implicit_part = true;
316 static inline constexpr bool has_explicit_part = true;
317 static inline constexpr MatrixType A_ex = {{{0, 0}, {0.5, 0.}}};
318 static inline constexpr ArrayType b_ex = {0, 1.};
319 static inline constexpr MatrixType A_im = {{{0, 0}, {0., 0.5}}};
320 static inline constexpr ArrayType b_im = {0, 1.};
321 static inline constexpr ArrayType c = {0.0, 0.5};
322};
323
325 static inline constexpr std::size_t n_stages = 5;
328 static inline constexpr bool has_implicit_part = true;
329 static inline constexpr bool has_explicit_part = true;
330 static inline constexpr MatrixType A_ex = {
331 {{0, 0, 0, 0, 0},
332 {0.5, 0, 0, 0, 0},
333 {11. / 18, 1. / 18., 0, 0, 0},
334 {5. / 6., -5. / 6., 0.5, 0, 0},
335 {0.25, 7. / 4., 3. / 4., -7. / 4., 0}}};
336 static inline constexpr ArrayType b_ex = {
337 0.25, 7. / 4., 3. / 4., -7. / 4., 0};
338 static inline constexpr MatrixType A_im =
340 {{0.5, 0, 0, 0},
341 {1. / 6., 0.5, 0, 0},
342 {-0.5, 0.5, 0.5, 0},
343 {1.5, -1.5, 0.5, 0.5}}});
344 static inline constexpr ArrayType b_im = {0.0, 1.5, -1.5, 0.5, 0.5};
345 static inline constexpr ArrayType c = {0, 0.5, 2. / 3., 0.5, 1.};
346};
347
348} // namespace timeintegration
349} // namespace vulpes
350
351#endif // VULPES_TIMEINTEGRATION_BUTCHER_TABLES_HPP_
Definition butcher_tables.hpp:34
constexpr std::array< T, N+1 > padStartArray(const std::array< T, N > &input)
Definition butcher_tables.hpp:53
constexpr std::array< T, N+1 > padEndArray(const std::array< T, N > &input)
Definition butcher_tables.hpp:62
constexpr std::array< std::array< T, N+1 >, N+1 > padTopRightMat(const std::array< std::array< T, N >, N > &input)
Definition butcher_tables.hpp:83
constexpr bool checkButcherTable(const T &t)
Definition butcher_tables.hpp:36
constexpr std::array< std::array< T, N+1 >, N+1 > padTopLeftMat(const std::array< std::array< T, N >, N > &input)
Definition butcher_tables.hpp:71
constexpr bool checkFSAL()
Definition butcher_tables.hpp:95
Definition butcher_tables.hpp:32
Definition cell_manager.hpp:38
static constexpr ArrayType c
Definition butcher_tables.hpp:345
static constexpr MatrixType A_im
Definition butcher_tables.hpp:338
static constexpr MatrixType A_ex
Definition butcher_tables.hpp:330
ButcherTable< n_stages >::ArrayType ArrayType
Definition butcher_tables.hpp:326
static constexpr bool has_explicit_part
Definition butcher_tables.hpp:329
static constexpr std::size_t n_stages
Definition butcher_tables.hpp:325
static constexpr bool has_implicit_part
Definition butcher_tables.hpp:328
static constexpr ArrayType b_ex
Definition butcher_tables.hpp:336
static constexpr ArrayType b_im
Definition butcher_tables.hpp:344
ButcherTable< n_stages >::MatrixType MatrixType
Definition butcher_tables.hpp:327
Definition butcher_tables.hpp:284
Definition butcher_tables.hpp:132
static constexpr ArrayType c
Definition butcher_tables.hpp:135
static constexpr ArrayType b
Definition butcher_tables.hpp:134
static constexpr MatrixType A
Definition butcher_tables.hpp:133
Definition butcher_tables.hpp:113
static constexpr ArrayType b_im
Definition butcher_tables.hpp:122
static constexpr ArrayType c
Definition butcher_tables.hpp:123
static constexpr std::size_t n_stages
Definition butcher_tables.hpp:114
ButcherTable< S >::ArrayType ArrayType
Definition butcher_tables.hpp:115
ButcherTable< S >::MatrixType MatrixType
Definition butcher_tables.hpp:116
static constexpr MatrixType A_im
Definition butcher_tables.hpp:121
static constexpr ArrayType b_ex
Definition butcher_tables.hpp:120
static constexpr MatrixType A_ex
Definition butcher_tables.hpp:119
static constexpr bool has_explicit_part
Definition butcher_tables.hpp:118
static constexpr bool has_implicit_part
Definition butcher_tables.hpp:117
Definition butcher_tables.hpp:103
static constexpr MatrixType A
Definition butcher_tables.hpp:107
static constexpr std::size_t n_stages
Definition butcher_tables.hpp:106
static constexpr ArrayType b
Definition butcher_tables.hpp:108
static constexpr ArrayType c
Definition butcher_tables.hpp:109
std::array< double, S > ArrayType
Definition butcher_tables.hpp:104
std::array< std::array< double, S >, S > MatrixType
Definition butcher_tables.hpp:105
Definition butcher_tables.hpp:287
Definition butcher_tables.hpp:193
static constexpr MatrixType A
Definition butcher_tables.hpp:194
static constexpr ArrayType b
Definition butcher_tables.hpp:196
static constexpr ArrayType c
Definition butcher_tables.hpp:197
Definition butcher_tables.hpp:288
Definition butcher_tables.hpp:200
static constexpr ArrayType c
Definition butcher_tables.hpp:207
static constexpr ArrayType b
Definition butcher_tables.hpp:206
static constexpr MatrixType A
Definition butcher_tables.hpp:201
Definition butcher_tables.hpp:285
Definition butcher_tables.hpp:156
static constexpr ArrayType b
Definition butcher_tables.hpp:158
static constexpr MatrixType A
Definition butcher_tables.hpp:157
static constexpr ArrayType c
Definition butcher_tables.hpp:159
Definition butcher_tables.hpp:255
static constexpr MatrixType A_ex
Definition butcher_tables.hpp:260
ButcherTable< n_stages >::ArrayType ArrayType
Definition butcher_tables.hpp:257
ButcherTable< n_stages >::MatrixType MatrixType
Definition butcher_tables.hpp:258
static constexpr bool has_explicit_part
Definition butcher_tables.hpp:259
static constexpr ArrayType c
Definition butcher_tables.hpp:262
static constexpr ArrayType b_ex
Definition butcher_tables.hpp:261
static constexpr std::size_t n_stages
Definition butcher_tables.hpp:256
Definition butcher_tables.hpp:293
static constexpr std::size_t n_stages
Definition butcher_tables.hpp:294
ButcherTable< n_stages >::ArrayType ArrayType
Definition butcher_tables.hpp:295
static constexpr ArrayType b_im
Definition butcher_tables.hpp:305
static constexpr ArrayType b_ex
Definition butcher_tables.hpp:301
static constexpr bool has_explicit_part
Definition butcher_tables.hpp:298
static constexpr MatrixType A_ex
Definition butcher_tables.hpp:299
ButcherTable< n_stages >::MatrixType MatrixType
Definition butcher_tables.hpp:296
static constexpr ArrayType c
Definition butcher_tables.hpp:307
static constexpr MatrixType A_im
Definition butcher_tables.hpp:303
static constexpr bool has_implicit_part
Definition butcher_tables.hpp:297
Definition butcher_tables.hpp:279
Definition butcher_tables.hpp:126
static constexpr ArrayType c
Definition butcher_tables.hpp:129
static constexpr ArrayType b
Definition butcher_tables.hpp:128
static constexpr MatrixType A
Definition butcher_tables.hpp:127
Definition butcher_tables.hpp:280
Definition butcher_tables.hpp:144
static constexpr ArrayType b
Definition butcher_tables.hpp:146
static constexpr ArrayType c
Definition butcher_tables.hpp:147
static constexpr MatrixType A
Definition butcher_tables.hpp:145
Definition butcher_tables.hpp:266
static constexpr MatrixType A_im
Definition butcher_tables.hpp:271
static constexpr bool has_implicit_part
Definition butcher_tables.hpp:270
static constexpr ArrayType b_im
Definition butcher_tables.hpp:273
ButcherTable< n_stages >::MatrixType MatrixType
Definition butcher_tables.hpp:269
ButcherTable< n_stages >::ArrayType ArrayType
Definition butcher_tables.hpp:268
static constexpr ArrayType c
Definition butcher_tables.hpp:275
static constexpr std::size_t n_stages
Definition butcher_tables.hpp:267
static constexpr MatrixType A_ex
Definition butcher_tables.hpp:317
static constexpr ArrayType b_im
Definition butcher_tables.hpp:320
ButcherTable< n_stages >::ArrayType ArrayType
Definition butcher_tables.hpp:313
static constexpr bool has_explicit_part
Definition butcher_tables.hpp:316
static constexpr MatrixType A_im
Definition butcher_tables.hpp:319
static constexpr bool has_implicit_part
Definition butcher_tables.hpp:315
ButcherTable< n_stages >::MatrixType MatrixType
Definition butcher_tables.hpp:314
static constexpr ArrayType c
Definition butcher_tables.hpp:321
static constexpr ArrayType b_ex
Definition butcher_tables.hpp:318
static constexpr std::size_t n_stages
Definition butcher_tables.hpp:312
Definition butcher_tables.hpp:286
Definition butcher_tables.hpp:183
static constexpr ArrayType b
Definition butcher_tables.hpp:186
static constexpr ArrayType c
Definition butcher_tables.hpp:187
static constexpr MatrixType A
Definition butcher_tables.hpp:184
Definition butcher_tables.hpp:290
Definition butcher_tables.hpp:228
static constexpr ArrayType b
Definition butcher_tables.hpp:234
static constexpr MatrixType A
Definition butcher_tables.hpp:229
static constexpr ArrayType c
Definition butcher_tables.hpp:235
Definition butcher_tables.hpp:138
static constexpr MatrixType A
Definition butcher_tables.hpp:139
static constexpr ArrayType b
Definition butcher_tables.hpp:140
static constexpr ArrayType c
Definition butcher_tables.hpp:141
Definition butcher_tables.hpp:289
Definition butcher_tables.hpp:210
static constexpr MatrixType A
Definition butcher_tables.hpp:211
static constexpr ArrayType c
Definition butcher_tables.hpp:214
static constexpr ArrayType b
Definition butcher_tables.hpp:213
Definition butcher_tables.hpp:282
Definition butcher_tables.hpp:176
static constexpr ArrayType b
Definition butcher_tables.hpp:179
static constexpr MatrixType A
Definition butcher_tables.hpp:177
static constexpr ArrayType c
Definition butcher_tables.hpp:180
Definition butcher_tables.hpp:150
static constexpr MatrixType A
Definition butcher_tables.hpp:151
static constexpr ArrayType b
Definition butcher_tables.hpp:152
static constexpr ArrayType c
Definition butcher_tables.hpp:153
Definition butcher_tables.hpp:162
static constexpr MatrixType A
Definition butcher_tables.hpp:163
static constexpr ArrayType b
Definition butcher_tables.hpp:165
static constexpr ArrayType c
Definition butcher_tables.hpp:166
Definition butcher_tables.hpp:281
Definition butcher_tables.hpp:169
static constexpr ArrayType c
Definition butcher_tables.hpp:173
static constexpr MatrixType A
Definition butcher_tables.hpp:170
static constexpr ArrayType b
Definition butcher_tables.hpp:172
Definition butcher_tables.hpp:291
Definition butcher_tables.hpp:219
static constexpr ArrayType b
Definition butcher_tables.hpp:224
static constexpr double gamma
Definition butcher_tables.hpp:220
static constexpr double w
Definition butcher_tables.hpp:222
static constexpr double d
Definition butcher_tables.hpp:221
static constexpr MatrixType A
Definition butcher_tables.hpp:223
static constexpr ArrayType c
Definition butcher_tables.hpp:225