| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2014 - 2025 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_time_stepping_templates_h |
| 16 |
#define dealii_time_stepping_templates_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <deal.II/base/exceptions.h> |
| 21 |
#include <deal.II/base/time_stepping.h> |
| 22 |
|
| 23 |
#include <functional> |
| 24 |
|
| 25 |
DEAL_II_NAMESPACE_OPEN |
| 26 |
|
| 27 |
namespace TimeStepping |
| 28 |
{ |
| 29 |
DeclExceptionMsg(ExcNoMethodSelected, |
| 30 |
"No method selected. You need to call initialize or pass a " |
| 31 |
"runge_kutta_method to the constructor."); |
| 32 |
|
| 33 |
// ---------------------------------------------------------------------- |
| 34 |
// RungeKutta |
| 35 |
// ---------------------------------------------------------------------- |
| 36 |
|
| 37 |
template <typename VectorType> |
| 38 |
double |
| 39 |
RungeKutta<VectorType>::evolve_one_time_step( |
| 40 |
std::vector<std::function<VectorType(const double, const VectorType &)>> &F, |
| 41 |
std::vector< |
| 42 |
std::function<VectorType(const double, const double, const VectorType &)>> |
| 43 |
&J_inverse, |
| 44 |
|
| 45 |
double t, |
| 46 |
double delta_t, |
| 47 |
VectorType &y) |
| 48 |
{ |
| 49 |
AssertThrow( |
| 50 |
F.empty(), |
| 51 |
ExcMessage( |
| 52 |
"RungeKutta methods cannot handle more that one function to integrate.")); |
| 53 |
AssertThrow( |
| 54 |
J_inverse.empty(), |
| 55 |
ExcMessage( |
| 56 |
"RungeKutta methods cannot handle more that one function to integrate.")); |
| 57 |
|
| 58 |
return evolve_one_time_step(F[0], J_inverse[0], t, delta_t, y); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
// ---------------------------------------------------------------------- |
| 64 |
// ExplicitRungeKutta |
| 65 |
// ---------------------------------------------------------------------- |
| 66 |
|
| 67 |
template <typename VectorType> |
| 68 |
ExplicitRungeKutta<VectorType>::ExplicitRungeKutta( |
| 69 |
const runge_kutta_method method) |
| 70 |
{ |
| 71 |
// virtual functions called in constructors and destructors never use the |
| 72 |
// override in a derived class |
| 73 |
// for clarity be explicit on which function is called |
| 74 |
ExplicitRungeKutta<VectorType>::initialize(method); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
template <typename VectorType> |
| 80 |
void |
| 81 |
ExplicitRungeKutta<VectorType>::initialize(const runge_kutta_method method) |
| 82 |
{ |
| 83 |
status.method = method; |
| 84 |
|
| 85 |
switch (method) |
| 86 |
{ |
| 87 |
case (FORWARD_EULER): |
| 88 |
{ |
| 89 |
this->n_stages = 1; |
| 90 |
this->b.push_back(1.0); |
| 91 |
this->c.push_back(0.0); |
| 92 |
|
| 93 |
break; |
| 94 |
} |
| 95 |
case (RK_THIRD_ORDER): |
| 96 |
{ |
| 97 |
this->n_stages = 3; |
| 98 |
this->b.reserve(this->n_stages); |
| 99 |
this->c.reserve(this->n_stages); |
| 100 |
this->b.push_back(1.0 / 6.0); |
| 101 |
this->b.push_back(2.0 / 3.0); |
| 102 |
this->b.push_back(1.0 / 6.0); |
| 103 |
this->c.push_back(0.0); |
| 104 |
this->c.push_back(0.5); |
| 105 |
this->c.push_back(1.0); |
| 106 |
std::vector<double> tmp; |
| 107 |
this->a.push_back(tmp); |
| 108 |
tmp.resize(1); |
| 109 |
tmp[0] = 0.5; |
| 110 |
this->a.push_back(tmp); |
| 111 |
tmp.resize(2); |
| 112 |
tmp[0] = -1.0; |
| 113 |
tmp[1] = 2.0; |
| 114 |
this->a.push_back(tmp); |
| 115 |
|
| 116 |
break; |
| 117 |
} |
| 118 |
case (SSP_THIRD_ORDER): |
| 119 |
{ |
| 120 |
this->n_stages = 3; |
| 121 |
this->b.reserve(this->n_stages); |
| 122 |
this->c.reserve(this->n_stages); |
| 123 |
this->b.push_back(1.0 / 6.0); |
| 124 |
this->b.push_back(1.0 / 6.0); |
| 125 |
this->b.push_back(2.0 / 3.0); |
| 126 |
this->c.push_back(0.0); |
| 127 |
this->c.push_back(1.0); |
| 128 |
this->c.push_back(0.5); |
| 129 |
std::vector<double> tmp; |
| 130 |
this->a.push_back(tmp); |
| 131 |
tmp.resize(1); |
| 132 |
tmp[0] = 1.0; |
| 133 |
this->a.push_back(tmp); |
| 134 |
tmp.resize(2); |
| 135 |
tmp[0] = 1.0 / 4.0; |
| 136 |
tmp[1] = 1.0 / 4.0; |
| 137 |
this->a.push_back(tmp); |
| 138 |
|
| 139 |
break; |
| 140 |
} |
| 141 |
case (RK_CLASSIC_FOURTH_ORDER): |
| 142 |
{ |
| 143 |
this->n_stages = 4; |
| 144 |
this->b.reserve(this->n_stages); |
| 145 |
this->c.reserve(this->n_stages); |
| 146 |
std::vector<double> tmp; |
| 147 |
this->a.push_back(tmp); |
| 148 |
tmp.resize(1); |
| 149 |
tmp[0] = 0.5; |
| 150 |
this->a.push_back(tmp); |
| 151 |
tmp.resize(2); |
| 152 |
tmp[0] = 0.0; |
| 153 |
tmp[1] = 0.5; |
| 154 |
this->a.push_back(tmp); |
| 155 |
tmp.resize(3); |
| 156 |
tmp[1] = 0.0; |
| 157 |
tmp[2] = 1.0; |
| 158 |
this->a.push_back(tmp); |
| 159 |
this->b.push_back(1.0 / 6.0); |
| 160 |
this->b.push_back(1.0 / 3.0); |
| 161 |
this->b.push_back(1.0 / 3.0); |
| 162 |
this->b.push_back(1.0 / 6.0); |
| 163 |
this->c.push_back(0.0); |
| 164 |
this->c.push_back(0.5); |
| 165 |
this->c.push_back(0.5); |
| 166 |
this->c.push_back(1.0); |
| 167 |
|
| 168 |
break; |
| 169 |
} |
| 170 |
case (RK_FIFTH_ORDER): |
| 171 |
{ |
| 172 |
/** |
| 173 |
* Rabiei, F. and Ismail, F., 2012. Fifth-order improved |
| 174 |
* Runge-Kutta method with reduced number of function evaluations. |
| 175 |
* Australian Journal of Basic and Applied Sciences, 6(3), |
| 176 |
* pp.97-105. |
| 177 |
* |
| 178 |
* Hossain, M.B., Hossain, M.J., Miah, M.M. and Alam, M.S., 2017. |
| 179 |
* A comparative study on fourth order and butcher’s fifth order |
| 180 |
* runge-kutta methods with third order initial value problem (IVP). |
| 181 |
* Appl. Comput. Math, 6(6), p.243. |
| 182 |
*/ |
| 183 |
this->n_stages = 6; |
| 184 |
this->b.reserve(this->n_stages); |
| 185 |
this->c.reserve(this->n_stages); |
| 186 |
std::vector<double> tmp; |
| 187 |
this->a.push_back(tmp); |
| 188 |
tmp.assign(5, 0.0); |
| 189 |
tmp[0] = 1.0 / 4.0; |
| 190 |
this->a.push_back(tmp); |
| 191 |
tmp.assign(5, 0.0); |
| 192 |
tmp[0] = 1.0 / 8.0; |
| 193 |
tmp[1] = 1.0 / 8.0; |
| 194 |
this->a.push_back(tmp); |
| 195 |
tmp.assign(5, 0.0); |
| 196 |
tmp[0] = 0.0; |
| 197 |
tmp[1] = -1.0 / 2.0; |
| 198 |
tmp[2] = 1.0; |
| 199 |
this->a.push_back(tmp); |
| 200 |
tmp.assign(5, 0.0); |
| 201 |
tmp[0] = 3.0 / 16.0; |
| 202 |
tmp[1] = 0.0; |
| 203 |
tmp[2] = 0.0; |
| 204 |
tmp[3] = 9.0 / 16.0; |
| 205 |
this->a.push_back(tmp); |
| 206 |
tmp.assign(5, 0.0); |
| 207 |
tmp[0] = -3.0 / 7.0; |
| 208 |
tmp[1] = 2.0 / 7.0; |
| 209 |
tmp[2] = 12.0 / 7.0; |
| 210 |
tmp[3] = -12.0 / 7.0; |
| 211 |
tmp[4] = 8.0 / 7.0; |
| 212 |
this->a.push_back(tmp); |
| 213 |
|
| 214 |
this->b.push_back(7.0 / 90.0); |
| 215 |
this->b.push_back(0.0); |
| 216 |
this->b.push_back(32.0 / 90.0); |
| 217 |
this->b.push_back(12.0 / 90.0); |
| 218 |
this->b.push_back(32.0 / 90.0); |
| 219 |
this->b.push_back(7.0 / 90.0); |
| 220 |
|
| 221 |
this->c.push_back(0.0); |
| 222 |
this->c.push_back(1.0 / 4.0); |
| 223 |
this->c.push_back(1.0 / 4.0); |
| 224 |
this->c.push_back(1.0 / 2.0); |
| 225 |
this->c.push_back(3.0 / 4.0); |
| 226 |
this->c.push_back(1.0); |
| 227 |
|
| 228 |
break; |
| 229 |
} |
| 230 |
case (RK_SIXTH_ORDER): |
| 231 |
{ |
| 232 |
/** |
| 233 |
* Butcher, J.C., 1964. On Runge-Kutta processes of high order. |
| 234 |
* Journal of the Australian Mathematical Society, 4(2), pp.179-194. |
| 235 |
*/ |
| 236 |
this->n_stages = 7; |
| 237 |
this->b.reserve(this->n_stages); |
| 238 |
this->c.reserve(this->n_stages); |
| 239 |
std::vector<double> tmp; |
| 240 |
this->a.push_back(tmp); |
| 241 |
tmp.assign(6, 0.0); |
| 242 |
tmp[0] = 1.0 / 3.0; |
| 243 |
this->a.push_back(tmp); |
| 244 |
tmp.assign(6, 0.0); |
| 245 |
tmp[0] = 0.0; |
| 246 |
tmp[1] = 2.0 / 3.0; |
| 247 |
this->a.push_back(tmp); |
| 248 |
tmp.assign(6, 0.0); |
| 249 |
tmp[0] = 1.0 / 12.0; |
| 250 |
tmp[1] = 1.0 / 3.0; |
| 251 |
tmp[2] = -1.0 / 12.0; |
| 252 |
this->a.push_back(tmp); |
| 253 |
tmp.assign(6, 0.0); |
| 254 |
tmp[0] = -1.0 / 16.0; |
| 255 |
tmp[1] = 9.0 / 8.0; |
| 256 |
tmp[2] = -3.0 / 16.0; |
| 257 |
tmp[3] = -3.0 / 8.0; |
| 258 |
this->a.push_back(tmp); |
| 259 |
tmp.assign(6, 0.0); |
| 260 |
tmp[0] = 0.0; |
| 261 |
tmp[1] = 9.0 / 8.0; |
| 262 |
tmp[2] = -3.0 / 8.0; |
| 263 |
tmp[3] = -3.0 / 4.0; |
| 264 |
tmp[4] = 1.0 / 2.0; |
| 265 |
this->a.push_back(tmp); |
| 266 |
tmp.assign(6, 0.0); |
| 267 |
tmp[0] = 9.0 / 44.0; |
| 268 |
tmp[1] = -9.0 / 11.0; |
| 269 |
tmp[2] = 63.0 / 44.0; |
| 270 |
tmp[3] = 18.0 / 11.0; |
| 271 |
tmp[4] = 0.0; |
| 272 |
tmp[5] = -16.0 / 11.0; |
| 273 |
this->a.push_back(tmp); |
| 274 |
|
| 275 |
this->b.push_back(11.0 / 120.0); |
| 276 |
this->b.push_back(0.0); |
| 277 |
this->b.push_back(27.0 / 40.0); |
| 278 |
this->b.push_back(27.0 / 40.0); |
| 279 |
this->b.push_back(-4.0 / 15.0); |
| 280 |
this->b.push_back(-4.0 / 15.0); |
| 281 |
this->b.push_back(11.0 / 120.0); |
| 282 |
|
| 283 |
this->c.push_back(0.0); |
| 284 |
this->c.push_back(1.0 / 3.0); |
| 285 |
this->c.push_back(2.0 / 3.0); |
| 286 |
this->c.push_back(1.0 / 3.0); |
| 287 |
this->c.push_back(1.0 / 2.0); |
| 288 |
this->c.push_back(1.0 / 2.0); |
| 289 |
this->c.push_back(1.0); |
| 290 |
|
| 291 |
break; |
| 292 |
} |
| 293 |
default: |
| 294 |
{ |
| 295 |
AssertThrow( |
| 296 |
false, ExcMessage("Unimplemented explicit Runge-Kutta method.")); |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
template <typename VectorType> |
| 304 |
double |
| 305 |
ExplicitRungeKutta<VectorType>::evolve_one_time_step( |
| 306 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 307 |
const std::function< |
| 308 |
VectorType(const double, const double, const VectorType &)> |
| 309 |
& /*id_minus_tau_J_inverse*/, |
| 310 |
double t, |
| 311 |
double delta_t, |
| 312 |
VectorType &y) |
| 313 |
{ |
| 314 |
return evolve_one_time_step(f, t, delta_t, y); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
template <typename VectorType> |
| 320 |
double |
| 321 |
ExplicitRungeKutta<VectorType>::evolve_one_time_step( |
| 322 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 323 |
double t, |
| 324 |
double delta_t, |
| 325 |
VectorType &y) |
| 326 |
{ |
| 327 |
Assert(status.method != runge_kutta_method::invalid, ExcNoMethodSelected()); |
| 328 |
|
| 329 |
std::vector<VectorType> f_stages(this->n_stages, y); |
| 330 |
// Compute the different stages needed. |
| 331 |
compute_stages(f, t, delta_t, y, f_stages); |
| 332 |
|
| 333 |
// Linear combinations of the stages. |
| 334 |
for (unsigned int i = 0; i < this->n_stages; ++i) |
| 335 |
y.sadd(1., delta_t * this->b[i], f_stages[i]); |
| 336 |
|
| 337 |
return (t + delta_t); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
template <typename VectorType> |
| 343 |
const typename ExplicitRungeKutta<VectorType>::Status & |
| 344 |
ExplicitRungeKutta<VectorType>::get_status() const |
| 345 |
{ |
| 346 |
return status; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
template <typename VectorType> |
| 352 |
void |
| 353 |
ExplicitRungeKutta<VectorType>::compute_stages( |
| 354 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 355 |
const double t, |
| 356 |
const double delta_t, |
| 357 |
const VectorType &y, |
| 358 |
std::vector<VectorType> &f_stages) const |
| 359 |
{ |
| 360 |
for (unsigned int i = 0; i < this->n_stages; ++i) |
| 361 |
{ |
| 362 |
VectorType Y(y); |
| 363 |
for (unsigned int j = 0; j < i; ++j) |
| 364 |
Y.sadd(1., delta_t * this->a[i][j], f_stages[j]); |
| 365 |
// Evaluate the function f at the point (t+c[i]*delta_t,Y). |
| 366 |
f_stages[i] = f(t + this->c[i] * delta_t, Y); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
// ---------------------------------------------------------------------- |
| 373 |
// LowStorageRungeKutta |
| 374 |
// ---------------------------------------------------------------------- |
| 375 |
|
| 376 |
template <typename VectorType> |
| 377 |
LowStorageRungeKutta<VectorType>::LowStorageRungeKutta( |
| 378 |
const runge_kutta_method method) |
| 379 |
{ |
| 380 |
// virtual functions called in constructors and destructors never use the |
| 381 |
// override in a derived class |
| 382 |
// for clarity be explicit on which function is called |
| 383 |
LowStorageRungeKutta<VectorType>::initialize(method); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
template <typename VectorType> |
| 389 |
void |
| 390 |
LowStorageRungeKutta<VectorType>::initialize(const runge_kutta_method method) |
| 391 |
{ |
| 392 |
status.method = method; |
| 393 |
|
| 394 |
switch (method) |
| 395 |
{ |
| 396 |
case (LOW_STORAGE_RK_STAGE3_ORDER3): |
| 397 |
{ |
| 398 |
this->n_stages = 3; |
| 399 |
this->b.reserve(this->n_stages); |
| 400 |
this->b.push_back(0.245170287303492); |
| 401 |
this->b.push_back(0.184896052186740); |
| 402 |
this->b.push_back(0.569933660509768); |
| 403 |
|
| 404 |
std::vector<double> tmp; |
| 405 |
tmp = {{0.755726351946097, 0.386954477304099}}; |
| 406 |
this->a.push_back(tmp); |
| 407 |
break; |
| 408 |
} |
| 409 |
case (LOW_STORAGE_RK_STAGE5_ORDER4): |
| 410 |
{ |
| 411 |
this->n_stages = 5; |
| 412 |
this->b = {{1153189308089. / 22510343858157., |
| 413 |
1772645290293. / 4653164025191., |
| 414 |
-1672844663538. / 4480602732383., |
| 415 |
2114624349019. / 3568978502595., |
| 416 |
5198255086312. / 14908931495163.}}; |
| 417 |
std::vector<double> ai; |
| 418 |
ai = {{970286171893. / 4311952581923., |
| 419 |
6584761158862. / 12103376702013., |
| 420 |
2251764453980. / 15575788980749., |
| 421 |
26877169314380. / 34165994151039.}}; |
| 422 |
this->a.push_back(ai); |
| 423 |
break; |
| 424 |
} |
| 425 |
case (LOW_STORAGE_RK_STAGE7_ORDER4): |
| 426 |
{ |
| 427 |
this->n_stages = 7; |
| 428 |
this->b = {{0.0941840925477795334, |
| 429 |
0.149683694803496998, |
| 430 |
0.285204742060440058, |
| 431 |
-0.122201846148053668, |
| 432 |
0.0605151571191401122, |
| 433 |
0.345986987898399296, |
| 434 |
0.186627171718797670}}; |
| 435 |
std::vector<double> ai; |
| 436 |
ai = {{0.241566650129646868 + this->b[0], |
| 437 |
0.0423866513027719953 + this->b[1], |
| 438 |
0.215602732678803776 + this->b[2], |
| 439 |
0.232328007537583987 + this->b[3], |
| 440 |
0.256223412574146438 + this->b[4], |
| 441 |
0.0978694102142697230 + this->b[5]}}; |
| 442 |
this->a.push_back(ai); |
| 443 |
break; |
| 444 |
} |
| 445 |
case (LOW_STORAGE_RK_STAGE9_ORDER5): |
| 446 |
{ |
| 447 |
this->n_stages = 9; |
| 448 |
this->b = {{2274579626619. / 23610510767302., |
| 449 |
693987741272. / 12394497460941., |
| 450 |
-347131529483. / 15096185902911., |
| 451 |
1144057200723. / 32081666971178., |
| 452 |
1562491064753. / 11797114684756., |
| 453 |
13113619727965. / 44346030145118., |
| 454 |
393957816125. / 7825732611452., |
| 455 |
720647959663. / 6565743875477., |
| 456 |
3559252274877. / 14424734981077.}}; |
| 457 |
std::vector<double> ai; |
| 458 |
ai = {{1107026461565. / 5417078080134., |
| 459 |
38141181049399. / 41724347789894., |
| 460 |
493273079041. / 11940823631197., |
| 461 |
1851571280403. / 6147804934346., |
| 462 |
11782306865191. / 62590030070788., |
| 463 |
9452544825720. / 13648368537481., |
| 464 |
4435885630781. / 26285702406235., |
| 465 |
2357909744247. / 11371140753790.}}; |
| 466 |
this->a.push_back(ai); |
| 467 |
break; |
| 468 |
} |
| 469 |
default: |
| 470 |
{ |
| 471 |
AssertThrow(false, |
| 472 |
ExcMessage( |
| 473 |
"Unimplemented low-storage Runge-Kutta method.")); |
| 474 |
} |
| 475 |
} |
| 476 |
// compute ci |
| 477 |
this->c.reserve(this->n_stages); |
| 478 |
this->c.push_back(0.); |
| 479 |
double sum_previous_bi = 0.; |
| 480 |
for (unsigned int stage = 1; stage < this->n_stages; ++stage) |
| 481 |
{ |
| 482 |
const double tmp = sum_previous_bi + this->a[0][stage - 1]; |
| 483 |
this->c.push_back(tmp); |
| 484 |
sum_previous_bi += this->b[stage - 1]; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
template <typename VectorType> |
| 491 |
double |
| 492 |
LowStorageRungeKutta<VectorType>::evolve_one_time_step( |
| 493 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 494 |
const std::function< |
| 495 |
VectorType(const double, const double, const VectorType &)> |
| 496 |
& /*id_minus_tau_J_inverse*/, |
| 497 |
double t, |
| 498 |
double delta_t, |
| 499 |
VectorType &y) |
| 500 |
{ |
| 501 |
// We need two auxiliary vectors, namely the vector ki |
| 502 |
// to hold the evaluation of the differential operator, and the vector ri |
| 503 |
// that holds the right-hand side for the differential operator application. |
| 504 |
VectorType vec_ri; |
| 505 |
VectorType vec_ki; |
| 506 |
return evolve_one_time_step(f, t, delta_t, y, vec_ri, vec_ki); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
template <typename VectorType> |
| 512 |
double |
| 513 |
LowStorageRungeKutta<VectorType>::evolve_one_time_step( |
| 514 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 515 |
double t, |
| 516 |
double delta_t, |
| 517 |
VectorType &solution, |
| 518 |
VectorType &vec_ri, |
| 519 |
VectorType &vec_ki) |
| 520 |
{ |
| 521 |
Assert(status.method != runge_kutta_method::invalid, ExcNoMethodSelected()); |
| 522 |
|
| 523 |
compute_one_stage(f, |
| 524 |
t, |
| 525 |
this->b[0] * delta_t, |
| 526 |
this->a[0][0] * delta_t, |
| 527 |
solution, |
| 528 |
vec_ki, |
| 529 |
solution, |
| 530 |
vec_ri); |
| 531 |
|
| 532 |
for (unsigned int stage = 1; stage < this->n_stages; ++stage) |
| 533 |
{ |
| 534 |
const double c_i = this->c[stage]; |
| 535 |
const double factor_ai = |
| 536 |
(stage == this->n_stages - 1 ? 0 : this->a[0][stage] * delta_t); |
| 537 |
compute_one_stage(f, |
| 538 |
t + c_i * delta_t, |
| 539 |
this->b[stage] * delta_t, |
| 540 |
factor_ai, |
| 541 |
vec_ri, |
| 542 |
vec_ki, |
| 543 |
solution, |
| 544 |
vec_ri); |
| 545 |
} |
| 546 |
return (t + delta_t); |
| 547 |
} |
| 548 |
|
| 549 |
template <typename VectorType> |
| 550 |
void |
| 551 |
LowStorageRungeKutta<VectorType>::get_coefficients( |
| 552 |
std::vector<double> &a, |
| 553 |
std::vector<double> &b, |
| 554 |
std::vector<double> &c) const |
| 555 |
{ |
| 556 |
a.resize(this->a[0].size()); |
| 557 |
a = this->a[0]; |
| 558 |
|
| 559 |
b.resize(this->b.size()); |
| 560 |
b = this->b; |
| 561 |
|
| 562 |
c.resize(this->c.size()); |
| 563 |
c = this->c; |
| 564 |
} |
| 565 |
|
| 566 |
template <typename VectorType> |
| 567 |
const typename LowStorageRungeKutta<VectorType>::Status & |
| 568 |
LowStorageRungeKutta<VectorType>::get_status() const |
| 569 |
{ |
| 570 |
return status; |
| 571 |
} |
| 572 |
|
| 573 |
template <typename VectorType> |
| 574 |
void |
| 575 |
LowStorageRungeKutta<VectorType>::compute_one_stage( |
| 576 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 577 |
const double t, |
| 578 |
const double factor_solution, |
| 579 |
const double factor_ai, |
| 580 |
const VectorType ¤t_ri, |
| 581 |
VectorType &vec_ki, |
| 582 |
VectorType &solution, |
| 583 |
VectorType &next_ri) const |
| 584 |
{ |
| 585 |
const double ai = factor_ai; |
| 586 |
const double bi = factor_solution; |
| 587 |
vec_ki = f(t, current_ri); |
| 588 |
|
| 589 |
if (ai == double()) |
| 590 |
{ |
| 591 |
solution.sadd(1., bi, vec_ki); |
| 592 |
} |
| 593 |
else |
| 594 |
{ |
| 595 |
next_ri = solution; |
| 596 |
next_ri.sadd(1., ai, vec_ki); |
| 597 |
solution.add(bi, vec_ki); |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
// ---------------------------------------------------------------------- |
| 603 |
// ImplicitRungeKutta |
| 604 |
// ---------------------------------------------------------------------- |
| 605 |
|
| 606 |
template <typename VectorType> |
| 607 |
ImplicitRungeKutta<VectorType>::ImplicitRungeKutta( |
| 608 |
const runge_kutta_method method, |
| 609 |
const unsigned int max_it, |
| 610 |
const double tolerance) |
| 611 |
: RungeKutta<VectorType>() |
| 612 |
, max_it(max_it) |
| 613 |
, tolerance(tolerance) |
| 614 |
{ |
| 615 |
// virtual functions called in constructors and destructors never use the |
| 616 |
// override in a derived class |
| 617 |
// for clarity be explicit on which function is called |
| 618 |
ImplicitRungeKutta<VectorType>::initialize(method); |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
template <typename VectorType> |
| 624 |
void |
| 625 |
ImplicitRungeKutta<VectorType>::initialize(const runge_kutta_method method) |
| 626 |
{ |
| 627 |
status.method = method; |
| 628 |
|
| 629 |
switch (method) |
| 630 |
{ |
| 631 |
case (BACKWARD_EULER): |
| 632 |
{ |
| 633 |
this->n_stages = 1; |
| 634 |
this->a.push_back(std::vector<double>(1, 1.0)); |
| 635 |
this->b.push_back(1.0); |
| 636 |
this->c.push_back(1.0); |
| 637 |
|
| 638 |
break; |
| 639 |
} |
| 640 |
case (IMPLICIT_MIDPOINT): |
| 641 |
{ |
| 642 |
this->a.push_back(std::vector<double>(1, 0.5)); |
| 643 |
this->b.push_back(1.0); |
| 644 |
this->c.push_back(0.5); |
| 645 |
this->n_stages = 1; |
| 646 |
|
| 647 |
break; |
| 648 |
} |
| 649 |
case (CRANK_NICOLSON): |
| 650 |
{ |
| 651 |
this->n_stages = 2; |
| 652 |
this->b.reserve(this->n_stages); |
| 653 |
this->c.reserve(this->n_stages); |
| 654 |
this->a.push_back(std::vector<double>(1, 0.0)); |
| 655 |
this->a.push_back(std::vector<double>(2, 0.5)); |
| 656 |
this->b.push_back(0.5); |
| 657 |
this->b.push_back(0.5); |
| 658 |
this->c.push_back(0.0); |
| 659 |
this->c.push_back(1.0); |
| 660 |
|
| 661 |
break; |
| 662 |
} |
| 663 |
case (SDIRK_TWO_STAGES): |
| 664 |
{ |
| 665 |
this->n_stages = 2; |
| 666 |
this->b.reserve(this->n_stages); |
| 667 |
this->c.reserve(this->n_stages); |
| 668 |
const double gamma = 1.0 - 1.0 / std::sqrt(2.0); |
| 669 |
this->b.push_back(1.0 - gamma); |
| 670 |
this->b.push_back(gamma); |
| 671 |
this->a.push_back(std::vector<double>(1, gamma)); |
| 672 |
this->a.push_back(this->b); |
| 673 |
this->c.push_back(gamma); |
| 674 |
this->c.push_back(1.0); |
| 675 |
|
| 676 |
break; |
| 677 |
} |
| 678 |
default: |
| 679 |
{ |
| 680 |
AssertThrow( |
| 681 |
false, ExcMessage("Unimplemented implicit Runge-Kutta method.")); |
| 682 |
} |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
template <typename VectorType> |
| 689 |
double |
| 690 |
ImplicitRungeKutta<VectorType>::evolve_one_time_step( |
| 691 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 692 |
const std::function<VectorType(const double, |
| 693 |
const double, |
| 694 |
const VectorType &)> &id_minus_tau_J_inverse, |
| 695 |
double t, |
| 696 |
double delta_t, |
| 697 |
VectorType &y) |
| 698 |
{ |
| 699 |
Assert(status.method != runge_kutta_method::invalid, ExcNoMethodSelected()); |
| 700 |
|
| 701 |
VectorType old_y(y); |
| 702 |
std::vector<VectorType> f_stages(this->n_stages, y); |
| 703 |
// Compute the different stages needed. |
| 704 |
compute_stages(f, id_minus_tau_J_inverse, t, delta_t, y, f_stages); |
| 705 |
|
| 706 |
y = old_y; |
| 707 |
for (unsigned int i = 0; i < this->n_stages; ++i) |
| 708 |
y.sadd(1., delta_t * this->b[i], f_stages[i]); |
| 709 |
|
| 710 |
return (t + delta_t); |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
template <typename VectorType> |
| 716 |
void |
| 717 |
ImplicitRungeKutta<VectorType>::set_newton_solver_parameters( |
| 718 |
unsigned int max_it_, |
| 719 |
double tolerance_) |
| 720 |
{ |
| 721 |
max_it = max_it_; |
| 722 |
tolerance = tolerance_; |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
template <typename VectorType> |
| 728 |
const typename ImplicitRungeKutta<VectorType>::Status & |
| 729 |
ImplicitRungeKutta<VectorType>::get_status() const |
| 730 |
{ |
| 731 |
return status; |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
template <typename VectorType> |
| 737 |
void |
| 738 |
ImplicitRungeKutta<VectorType>::compute_stages( |
| 739 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 740 |
const std::function<VectorType(const double, |
| 741 |
const double, |
| 742 |
const VectorType &)> &id_minus_tau_J_inverse, |
| 743 |
double t, |
| 744 |
double delta_t, |
| 745 |
VectorType &y, |
| 746 |
std::vector<VectorType> &f_stages) |
| 747 |
{ |
| 748 |
VectorType z(y); |
| 749 |
for (unsigned int i = 0; i < this->n_stages; ++i) |
| 750 |
{ |
| 751 |
VectorType old_y(z); |
| 752 |
for (unsigned int j = 0; j < i; ++j) |
| 753 |
old_y.sadd(1., delta_t * this->a[i][j], f_stages[j]); |
| 754 |
|
| 755 |
// Solve the nonlinear system using Newton's method |
| 756 |
const double new_t = t + this->c[i] * delta_t; |
| 757 |
const double new_delta_t = this->a[i][i] * delta_t; |
| 758 |
VectorType &f_stage = f_stages[i]; |
| 759 |
newton_solve( |
| 760 |
[this, &f, new_t, new_delta_t, &old_y, &f_stage]( |
| 761 |
const VectorType &y, VectorType &residual) { |
| 762 |
this->compute_residual( |
| 763 |
f, new_t, new_delta_t, old_y, y, f_stage, residual); |
| 764 |
}, |
| 765 |
[&id_minus_tau_J_inverse, new_t, new_delta_t](const VectorType &y) { |
| 766 |
return id_minus_tau_J_inverse(new_t, new_delta_t, y); |
| 767 |
}, |
| 768 |
y); |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
template <typename VectorType> |
| 775 |
void |
| 776 |
ImplicitRungeKutta<VectorType>::newton_solve( |
| 777 |
const std::function<void(const VectorType &, VectorType &)> &get_residual, |
| 778 |
const std::function<VectorType(const VectorType &)> &id_minus_tau_J_inverse, |
| 779 |
VectorType &y) |
| 780 |
{ |
| 781 |
VectorType residual(y); |
| 782 |
get_residual(y, residual); |
| 783 |
unsigned int i = 0; |
| 784 |
const double initial_residual_norm = residual.l2_norm(); |
| 785 |
double norm_residual = initial_residual_norm; |
| 786 |
while (i < max_it) |
| 787 |
{ |
| 788 |
y.sadd(1.0, -1.0, id_minus_tau_J_inverse(residual)); |
| 789 |
get_residual(y, residual); |
| 790 |
norm_residual = residual.l2_norm(); |
| 791 |
if (norm_residual < tolerance) |
| 792 |
break; |
| 793 |
++i; |
| 794 |
} |
| 795 |
status.n_iterations = i + 1; |
| 796 |
status.norm_residual = norm_residual; |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
template <typename VectorType> |
| 802 |
void |
| 803 |
ImplicitRungeKutta<VectorType>::compute_residual( |
| 804 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 805 |
double t, |
| 806 |
double delta_t, |
| 807 |
const VectorType &old_y, |
| 808 |
const VectorType &y, |
| 809 |
VectorType &tendency, |
| 810 |
VectorType &residual) const |
| 811 |
{ |
| 812 |
// The tendency is stored to save one evaluation of f. |
| 813 |
tendency = f(t, y); |
| 814 |
residual = tendency; |
| 815 |
residual.sadd(delta_t, 1.0, old_y); |
| 816 |
residual.sadd(-1.0, 1., y); |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
// ---------------------------------------------------------------------- |
| 822 |
// EmbeddedExplicitRungeKutta |
| 823 |
// ---------------------------------------------------------------------- |
| 824 |
|
| 825 |
template <typename VectorType> |
| 826 |
EmbeddedExplicitRungeKutta<VectorType>::EmbeddedExplicitRungeKutta( |
| 827 |
const runge_kutta_method method, |
| 828 |
const double coarsen_param, |
| 829 |
const double refine_param, |
| 830 |
const double min_delta, |
| 831 |
const double max_delta, |
| 832 |
const double refine_tol, |
| 833 |
const double coarsen_tol) |
| 834 |
: coarsen_param(coarsen_param) |
| 835 |
, refine_param(refine_param) |
| 836 |
, min_delta_t(min_delta) |
| 837 |
, max_delta_t(max_delta) |
| 838 |
, refine_tol(refine_tol) |
| 839 |
, coarsen_tol(coarsen_tol) |
| 840 |
, last_same_as_first(false) |
| 841 |
, last_stage(nullptr) |
| 842 |
, status{} |
| 843 |
{ |
| 844 |
// virtual functions called in constructors and destructors never use the |
| 845 |
// override in a derived class |
| 846 |
// for clarity be explicit on which function is called |
| 847 |
EmbeddedExplicitRungeKutta<VectorType>::initialize(method); |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
template <typename VectorType> |
| 853 |
void |
| 854 |
EmbeddedExplicitRungeKutta<VectorType>::initialize( |
| 855 |
const runge_kutta_method method) |
| 856 |
{ |
| 857 |
status.method = method; |
| 858 |
|
| 859 |
switch (method) |
| 860 |
{ |
| 861 |
case (HEUN_EULER): |
| 862 |
{ |
| 863 |
this->n_stages = 2; |
| 864 |
this->a.push_back(std::vector<double>()); |
| 865 |
this->a.push_back(std::vector<double>(1, 1.0)); |
| 866 |
this->c.push_back(0.0); |
| 867 |
this->c.push_back(1.0); |
| 868 |
b1.push_back(0.5); |
| 869 |
b1.push_back(0.5); |
| 870 |
b2.push_back(1.0); |
| 871 |
b2.push_back(0.0); |
| 872 |
|
| 873 |
break; |
| 874 |
} |
| 875 |
case (BOGACKI_SHAMPINE): |
| 876 |
{ |
| 877 |
last_same_as_first = true; |
| 878 |
this->n_stages = 4; |
| 879 |
this->c.reserve(this->n_stages); |
| 880 |
this->b1.reserve(this->n_stages); |
| 881 |
this->b2.reserve(this->n_stages); |
| 882 |
std::vector<double> tmp; |
| 883 |
this->a.push_back(tmp); |
| 884 |
tmp.resize(1); |
| 885 |
tmp[0] = 0.5; |
| 886 |
this->a.push_back(tmp); |
| 887 |
tmp.resize(2); |
| 888 |
tmp[0] = 0.0; |
| 889 |
tmp[1] = 0.75; |
| 890 |
this->a.push_back(tmp); |
| 891 |
tmp.resize(3); |
| 892 |
tmp[0] = 2.0 / 9.0; |
| 893 |
tmp[1] = 1.0 / 3.0; |
| 894 |
tmp[2] = 4.0 / 9.0; |
| 895 |
this->a.push_back(tmp); |
| 896 |
this->c.push_back(0.0); |
| 897 |
this->c.push_back(0.5); |
| 898 |
this->c.push_back(0.75); |
| 899 |
this->c.push_back(1.0); |
| 900 |
this->b1.push_back(2.0 / 9.0); |
| 901 |
this->b1.push_back(1.0 / 3.0); |
| 902 |
this->b1.push_back(4.0 / 9.0); |
| 903 |
this->b1.push_back(0.0); |
| 904 |
this->b2.push_back(7.0 / 24.0); |
| 905 |
this->b2.push_back(0.25); |
| 906 |
this->b2.push_back(1.0 / 3.0); |
| 907 |
this->b2.push_back(0.125); |
| 908 |
|
| 909 |
break; |
| 910 |
} |
| 911 |
case (DOPRI): |
| 912 |
{ |
| 913 |
last_same_as_first = true; |
| 914 |
this->n_stages = 7; |
| 915 |
this->c.reserve(this->n_stages); |
| 916 |
this->b1.reserve(this->n_stages); |
| 917 |
this->b2.reserve(this->n_stages); |
| 918 |
std::vector<double> tmp; |
| 919 |
this->a.push_back(tmp); |
| 920 |
tmp.resize(1); |
| 921 |
tmp[0] = 1. / 5.; |
| 922 |
this->a.push_back(tmp); |
| 923 |
tmp.resize(2); |
| 924 |
tmp[0] = 3. / 40.; |
| 925 |
tmp[1] = 9. / 40.; |
| 926 |
this->a.push_back(tmp); |
| 927 |
tmp.resize(3); |
| 928 |
tmp[0] = 44. / 45.; |
| 929 |
tmp[1] = -56. / 15.; |
| 930 |
tmp[2] = 32. / 9.; |
| 931 |
this->a.push_back(tmp); |
| 932 |
tmp.resize(4); |
| 933 |
tmp[0] = 19372. / 6561.; |
| 934 |
tmp[1] = -25360. / 2187.; |
| 935 |
tmp[2] = 64448. / 6561.; |
| 936 |
tmp[3] = -212. / 729.; |
| 937 |
this->a.push_back(tmp); |
| 938 |
tmp.resize(5); |
| 939 |
tmp[0] = 9017. / 3168.; |
| 940 |
tmp[1] = -355. / 33.; |
| 941 |
tmp[2] = 46732. / 5247.; |
| 942 |
tmp[3] = 49. / 176.; |
| 943 |
tmp[4] = -5103. / 18656; |
| 944 |
this->a.push_back(tmp); |
| 945 |
tmp.resize(6); |
| 946 |
tmp[0] = 35. / 384.; |
| 947 |
tmp[1] = 0.; |
| 948 |
tmp[2] = 500. / 1113.; |
| 949 |
tmp[3] = 125. / 192.; |
| 950 |
tmp[4] = -2187. / 6784.; |
| 951 |
tmp[5] = 11. / 84.; |
| 952 |
this->a.push_back(tmp); |
| 953 |
this->c.push_back(0.); |
| 954 |
this->c.push_back(1. / 5.); |
| 955 |
this->c.push_back(3. / 10.); |
| 956 |
this->c.push_back(4. / 5.); |
| 957 |
this->c.push_back(8. / 9.); |
| 958 |
this->c.push_back(1.); |
| 959 |
this->c.push_back(1.); |
| 960 |
this->b1.push_back(35. / 384.); |
| 961 |
this->b1.push_back(0.); |
| 962 |
this->b1.push_back(500. / 1113.); |
| 963 |
this->b1.push_back(125. / 192.); |
| 964 |
this->b1.push_back(-2187. / 6784.); |
| 965 |
this->b1.push_back(11. / 84.); |
| 966 |
this->b1.push_back(0.); |
| 967 |
this->b2.push_back(5179. / 57600.); |
| 968 |
this->b2.push_back(0.); |
| 969 |
this->b2.push_back(7571. / 16695.); |
| 970 |
this->b2.push_back(393. / 640.); |
| 971 |
this->b2.push_back(-92097. / 339200.); |
| 972 |
this->b2.push_back(187. / 2100.); |
| 973 |
this->b2.push_back(1. / 40.); |
| 974 |
|
| 975 |
break; |
| 976 |
} |
| 977 |
case (FEHLBERG): |
| 978 |
{ |
| 979 |
this->n_stages = 6; |
| 980 |
this->c.reserve(this->n_stages); |
| 981 |
this->b1.reserve(this->n_stages); |
| 982 |
this->b2.reserve(this->n_stages); |
| 983 |
std::vector<double> tmp; |
| 984 |
this->a.push_back(tmp); |
| 985 |
tmp.resize(1); |
| 986 |
tmp[0] = 0.25; |
| 987 |
this->a.push_back(tmp); |
| 988 |
tmp.resize(2); |
| 989 |
tmp[0] = 0.09375; |
| 990 |
tmp[1] = 0.28125; |
| 991 |
this->a.push_back(tmp); |
| 992 |
tmp.resize(3); |
| 993 |
tmp[0] = 1932.0 / 2197.0; |
| 994 |
tmp[1] = -7200.0 / 2197.0; |
| 995 |
tmp[2] = 7296.0 / 2197.0; |
| 996 |
this->a.push_back(tmp); |
| 997 |
tmp.resize(4); |
| 998 |
tmp[0] = 439.0 / 216.0; |
| 999 |
tmp[1] = -8.0; |
| 1000 |
tmp[2] = 3680.0 / 513.0; |
| 1001 |
tmp[3] = -845.0 / 4104.0; |
| 1002 |
this->a.push_back(tmp); |
| 1003 |
tmp.resize(5); |
| 1004 |
tmp[0] = -8.0 / 27.0; |
| 1005 |
tmp[1] = 2.0; |
| 1006 |
tmp[2] = -3544.0 / 2565.0; |
| 1007 |
tmp[3] = 1859.0 / 4104.0; |
| 1008 |
tmp[4] = -0.275; |
| 1009 |
this->a.push_back(tmp); |
| 1010 |
this->c.push_back(0.0); |
| 1011 |
this->c.push_back(0.25); |
| 1012 |
this->c.push_back(0.375); |
| 1013 |
this->c.push_back(12.0 / 13.0); |
| 1014 |
this->c.push_back(1.0); |
| 1015 |
this->c.push_back(0.5); |
| 1016 |
this->b1.push_back(16.0 / 135.0); |
| 1017 |
this->b1.push_back(0.0); |
| 1018 |
this->b1.push_back(6656.0 / 12825.0); |
| 1019 |
this->b1.push_back(28561.0 / 56430.0); |
| 1020 |
this->b1.push_back(-0.18); |
| 1021 |
this->b1.push_back(2.0 / 55.0); |
| 1022 |
this->b2.push_back(25.0 / 216.0); |
| 1023 |
this->b2.push_back(0.0); |
| 1024 |
this->b2.push_back(1408.0 / 2565.0); |
| 1025 |
this->b2.push_back(2197.0 / 4104.0); |
| 1026 |
this->b2.push_back(-0.2); |
| 1027 |
this->b2.push_back(0.0); |
| 1028 |
|
| 1029 |
break; |
| 1030 |
} |
| 1031 |
case (CASH_KARP): |
| 1032 |
{ |
| 1033 |
this->n_stages = 6; |
| 1034 |
this->c.reserve(this->n_stages); |
| 1035 |
this->b1.reserve(this->n_stages); |
| 1036 |
this->b2.reserve(this->n_stages); |
| 1037 |
std::vector<double> tmp; |
| 1038 |
this->a.push_back(tmp); |
| 1039 |
tmp.resize(1); |
| 1040 |
tmp[0] = 0.2; |
| 1041 |
this->a.push_back(tmp); |
| 1042 |
tmp.resize(2); |
| 1043 |
tmp[0] = 0.075; |
| 1044 |
tmp[1] = 0.225; |
| 1045 |
this->a.push_back(tmp); |
| 1046 |
tmp.resize(3); |
| 1047 |
tmp[0] = 0.3; |
| 1048 |
tmp[1] = -0.9; |
| 1049 |
tmp[2] = 1.2; |
| 1050 |
this->a.push_back(tmp); |
| 1051 |
tmp.resize(4); |
| 1052 |
tmp[0] = -11.0 / 54.0; |
| 1053 |
tmp[1] = 2.5; |
| 1054 |
tmp[2] = -70.0 / 27.0; |
| 1055 |
tmp[3] = 35.0 / 27.0; |
| 1056 |
this->a.push_back(tmp); |
| 1057 |
tmp.resize(5); |
| 1058 |
tmp[0] = 1631.0 / 55296.0; |
| 1059 |
tmp[1] = 175.0 / 512.0; |
| 1060 |
tmp[2] = 575.0 / 13824.0; |
| 1061 |
tmp[3] = 44275.0 / 110592.0; |
| 1062 |
tmp[4] = 253.0 / 4096.0; |
| 1063 |
this->a.push_back(tmp); |
| 1064 |
this->c.push_back(0.0); |
| 1065 |
this->c.push_back(0.2); |
| 1066 |
this->c.push_back(0.3); |
| 1067 |
this->c.push_back(0.6); |
| 1068 |
this->c.push_back(1.0); |
| 1069 |
this->c.push_back(0.875); |
| 1070 |
this->b1.push_back(37.0 / 378.0); |
| 1071 |
this->b1.push_back(0.0); |
| 1072 |
this->b1.push_back(250.0 / 621.0); |
| 1073 |
this->b1.push_back(125.0 / 594.0); |
| 1074 |
this->b1.push_back(0.0); |
| 1075 |
this->b1.push_back(512.0 / 1771.0); |
| 1076 |
this->b2.push_back(2825.0 / 27648.0); |
| 1077 |
this->b2.push_back(0.0); |
| 1078 |
this->b2.push_back(18575.0 / 48384.0); |
| 1079 |
this->b2.push_back(13525.0 / 55296.0); |
| 1080 |
this->b2.push_back(277.0 / 14336.0); |
| 1081 |
this->b2.push_back(0.25); |
| 1082 |
|
| 1083 |
break; |
| 1084 |
} |
| 1085 |
default: |
| 1086 |
{ |
| 1087 |
AssertThrow( |
| 1088 |
false, ExcMessage("Unimplemented Embedded Runge-Kutta method.")); |
| 1089 |
} |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
template <typename VectorType> |
| 1096 |
void |
| 1097 |
EmbeddedExplicitRungeKutta<VectorType>::free_memory() |
| 1098 |
{ |
| 1099 |
if (last_stage != nullptr) |
| 1100 |
delete last_stage; |
| 1101 |
|
| 1102 |
last_stage = nullptr; |
| 1103 |
} |
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
template <typename VectorType> |
| 1108 |
double |
| 1109 |
EmbeddedExplicitRungeKutta<VectorType>::evolve_one_time_step( |
| 1110 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 1111 |
const std::function< |
| 1112 |
VectorType(const double, const double, const VectorType &)> |
| 1113 |
& /*id_minus_tau_J_inverse*/, |
| 1114 |
double t, |
| 1115 |
double delta_t, |
| 1116 |
VectorType &y) |
| 1117 |
{ |
| 1118 |
return evolve_one_time_step(f, t, delta_t, y); |
| 1119 |
} |
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
template <typename VectorType> |
| 1124 |
double |
| 1125 |
EmbeddedExplicitRungeKutta<VectorType>::evolve_one_time_step( |
| 1126 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 1127 |
double t, |
| 1128 |
double delta_t, |
| 1129 |
VectorType &y) |
| 1130 |
{ |
| 1131 |
Assert(status.method != runge_kutta_method::invalid, ExcNoMethodSelected()); |
| 1132 |
|
| 1133 |
bool done = false; |
| 1134 |
unsigned int count = 0; |
| 1135 |
double error_norm = 0.; |
| 1136 |
VectorType old_y(y); |
| 1137 |
VectorType error(y); |
| 1138 |
std::vector<VectorType> f_stages(this->n_stages, y); |
| 1139 |
|
| 1140 |
while (!done) |
| 1141 |
{ |
| 1142 |
error = 0.; |
| 1143 |
y = old_y; |
| 1144 |
// Compute the different stages needed. |
| 1145 |
compute_stages(f, t, delta_t, y, f_stages); |
| 1146 |
|
| 1147 |
for (unsigned int i = 0; i < this->n_stages; ++i) |
| 1148 |
{ |
| 1149 |
y.sadd(1., delta_t * this->b1[i], f_stages[i]); |
| 1150 |
error.sadd(1., delta_t * (b2[i] - b1[i]), f_stages[i]); |
| 1151 |
} |
| 1152 |
|
| 1153 |
error_norm = error.l2_norm(); |
| 1154 |
// Check if the norm of error is less than the coarsening tolerance |
| 1155 |
if (error_norm < coarsen_tol) |
| 1156 |
{ |
| 1157 |
done = true; |
| 1158 |
// Increase the guessed time step |
| 1159 |
double new_delta_t = delta_t * coarsen_param; |
| 1160 |
// Check that the guessed time step is smaller than the maximum time |
| 1161 |
// step allowed. |
| 1162 |
if (new_delta_t > max_delta_t) |
| 1163 |
{ |
| 1164 |
status.exit_delta_t = MAX_DELTA_T; |
| 1165 |
status.delta_t_guess = max_delta_t; |
| 1166 |
} |
| 1167 |
else |
| 1168 |
{ |
| 1169 |
status.exit_delta_t = DELTA_T; |
| 1170 |
status.delta_t_guess = new_delta_t; |
| 1171 |
} |
| 1172 |
} |
| 1173 |
// Check if the norm of error is less than the refining tolerance |
| 1174 |
else if (error_norm < refine_tol) |
| 1175 |
{ |
| 1176 |
done = true; |
| 1177 |
status.exit_delta_t = DELTA_T; |
| 1178 |
status.delta_t_guess = delta_t; |
| 1179 |
} |
| 1180 |
else |
| 1181 |
{ |
| 1182 |
// If the time step is already the smallest acceptable, exit. |
| 1183 |
if (delta_t == min_delta_t) |
| 1184 |
{ |
| 1185 |
done = true; |
| 1186 |
status.exit_delta_t = MIN_DELTA_T; |
| 1187 |
status.delta_t_guess = delta_t; |
| 1188 |
} |
| 1189 |
// Reduce the time step. |
| 1190 |
else |
| 1191 |
{ |
| 1192 |
delta_t *= refine_param; |
| 1193 |
if (delta_t < min_delta_t) |
| 1194 |
delta_t = min_delta_t; |
| 1195 |
} |
| 1196 |
} |
| 1197 |
|
| 1198 |
++count; |
| 1199 |
} |
| 1200 |
|
| 1201 |
// Save the last stage if necessary |
| 1202 |
if (last_same_as_first == true) |
| 1203 |
{ |
| 1204 |
if (last_stage == nullptr) |
| 1205 |
last_stage = new VectorType(f_stages.back()); |
| 1206 |
else |
| 1207 |
*last_stage = f_stages.back(); |
| 1208 |
} |
| 1209 |
|
| 1210 |
status.n_iterations = count; |
| 1211 |
status.error_norm = error_norm; |
| 1212 |
|
| 1213 |
return (t + delta_t); |
| 1214 |
} |
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
template <typename VectorType> |
| 1219 |
void |
| 1220 |
EmbeddedExplicitRungeKutta<VectorType>::set_time_adaptation_parameters( |
| 1221 |
const double coarsen_param_, |
| 1222 |
const double refine_param_, |
| 1223 |
const double min_delta_, |
| 1224 |
const double max_delta_, |
| 1225 |
const double refine_tol_, |
| 1226 |
const double coarsen_tol_) |
| 1227 |
{ |
| 1228 |
coarsen_param = coarsen_param_; |
| 1229 |
refine_param = refine_param_; |
| 1230 |
min_delta_t = min_delta_; |
| 1231 |
max_delta_t = max_delta_; |
| 1232 |
refine_tol = refine_tol_; |
| 1233 |
coarsen_tol = coarsen_tol_; |
| 1234 |
} |
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
template <typename VectorType> |
| 1239 |
const typename EmbeddedExplicitRungeKutta<VectorType>::Status & |
| 1240 |
EmbeddedExplicitRungeKutta<VectorType>::get_status() const |
| 1241 |
{ |
| 1242 |
return status; |
| 1243 |
} |
| 1244 |
|
| 1245 |
|
| 1246 |
template <typename VectorType> |
| 1247 |
void |
| 1248 |
EmbeddedExplicitRungeKutta<VectorType>::compute_stages( |
| 1249 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 1250 |
const double t, |
| 1251 |
const double delta_t, |
| 1252 |
const VectorType &y, |
| 1253 |
std::vector<VectorType> &f_stages) |
| 1254 |
{ |
| 1255 |
VectorType Y(y); |
| 1256 |
unsigned int i = 0; |
| 1257 |
|
| 1258 |
// If the last stage is the same as the first, we can skip the evaluation |
| 1259 |
// of the first stage. |
| 1260 |
if (last_same_as_first == true) |
| 1261 |
{ |
| 1262 |
if (last_stage != nullptr) |
| 1263 |
{ |
| 1264 |
f_stages[0] = *last_stage; |
| 1265 |
i = 1; |
| 1266 |
} |
| 1267 |
} |
| 1268 |
|
| 1269 |
for (; i < this->n_stages; ++i) |
| 1270 |
{ |
| 1271 |
Y = y; |
| 1272 |
for (unsigned int j = 0; j < i; ++j) |
| 1273 |
Y.sadd(1.0, delta_t * this->a[i][j], f_stages[j]); |
| 1274 |
f_stages[i] = f(t + this->c[i] * delta_t, Y); |
| 1275 |
} |
| 1276 |
} |
| 1277 |
} // namespace TimeStepping |
| 1278 |
|
| 1279 |
DEAL_II_NAMESPACE_CLOSE |
| 1280 |
|
| 1281 |
#endif |