Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
bdf.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2019-2022, 2024 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#include <core/bdf.h>
5
6#include <deal.II/lac/vector.h>
7
8
9Vector<double>
10bdf_coefficients(const unsigned int p, const std::vector<double> &time_steps)
11{
12 // There should be at least p time steps
13 assert(time_steps.size() >= p);
14
15 // Create a timetable for the bdf formula
16 Vector<double> times(p + 1);
17 for (unsigned int i = 0; i < p + 1; ++i)
18 {
19 times[i] = 0.;
20 for (unsigned int j = 0; j < i; ++j)
21 times[i] -= time_steps[j];
22 }
23
24 // The alphas are the bdf coefficients
25 Vector<double> alpha(p + 1);
26 alpha = 0;
27
28 for (unsigned int j = 1; j < p + 1; ++j)
29 {
30 double factor = 1.;
31 for (unsigned int i = 1; i < j; ++i)
32 factor *= times[0] - times[i];
33
34 Vector<double> term(p + 1);
35 term.equ(factor, delta(p, 0, j, times));
36 alpha += term;
37 }
38 return alpha;
39}
40
41Vector<double>
44 const std::vector<double> &time_steps)
45{
46 switch (method)
47 {
49 return bdf_coefficients(1, time_steps);
51 return bdf_coefficients(1, time_steps);
53 return bdf_coefficients(2, time_steps);
55 return bdf_coefficients(3, time_steps);
56 default:
57 throw(std::runtime_error(
58 "BDF coefficients were requested without a BDF method"));
59 }
60}
61
62Vector<double>
63delta(const unsigned int order,
64 const unsigned int n,
65 const unsigned int j,
66 const Vector<double> &times)
67{
68 if (j == 0)
69 {
70 Vector<double> arr(order + 1);
71 arr = 0.;
72 arr[n] = 1;
73 return arr;
74 }
75
76 // else
77 Vector<double> delta_1 = delta(order, n, j - 1, times);
78 Vector<double> delta_2 = delta(order, n + 1, j - 1, times);
79 Vector<double> delta_sol(order + 1);
80 for (unsigned int i_del = 0; i_del < order + 1; ++i_del)
81 delta_sol[i_del] =
82 (delta_1[i_del] - delta_2[i_del]) / (times[n] - times[n + j]);
83 return delta_sol;
84}
Vector< double > delta(const unsigned int order, const unsigned int n, const unsigned int j, const Vector< double > &times)
Recursive function for calculating the order divided differences ( ) that is used for calculating ba...
Definition bdf.cc:63
Vector< double > calculate_bdf_coefficients(const Parameters::SimulationControl::TimeSteppingMethod method, const std::vector< double > &time_steps)
Calculate the coefficients required for backward differentiation formula (BDF) integration of order .
Definition bdf.cc:42
Vector< double > bdf_coefficients(const unsigned int p, const std::vector< double > &time_steps)
Definition bdf.cc:10