Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
bdf.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2019-2021, 2023-2024 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#ifndef lethe_bdf_h
5#define lethe_bdf_h
6
7#include <core/parameters.h>
8
9#include <deal.II/lac/vector.h>
10
11#include <vector>
12
13using namespace dealii;
14
15/**
16 * @brief Calculate the coefficients required for backward differentiation
17 * formula (BDF) integration of @p order \f$n\f$.
18 *
19 * The coefficients are determined through a recursion algorithm.
20 * The algorithm is taken from: Hay, Alexander, et al. "hp-Adaptive time
21 * integration based on the BDF for viscous flows." Journal of Computational
22 * Physics 291 (2015): 151-176.
23 *
24 * @param[in] method Time stepping method.
25 *
26 * @param[in] time_steps Vector containing all the time steps. The time steps
27 * should be in decreasing order.
28 *
29 * For example, if the method is a BDF2 (\f$n=2\f$), it uses three values for
30 * the time: \f$t\f$, \f$t-\Delta t_1\f$ and \f$t-\Delta t_2\f$. Thus, the @p
31 * time_steps vector should contain \f$\Delta t_1\f$ and \f$\Delta t_2\f$.
32 *
33 * @return Vector containing BDF integration coefficient values.
34 *
35 * @note At the moment, the highest implemented scheme is @p bdf3 (@p order=3).
36 */
37Vector<double>
40 const std::vector<double> &time_steps);
41
42
43/**
44 * @brief Recursive function for calculating the \f$j^{th}\f$ order divided
45 * differences (\f$\delta^j\f$) that is used for calculating backward
46 * differentiation formula (BDF) coefficients.
47 *
48 * The algorithm is taken from: Hay, Alexander, et al. "hp-Adaptive time
49 * integration based on the BDF for viscous flows." Journal of Computational
50 * Physics 291 (2015): 151-176.
51 *
52 * @param[in] order Order of the BDF method.
53 *
54 * @param[in] n Integer required for the recursive function calculation.
55 *
56 * @param[in] j Order of the divided difference of the value of interest.
57 *
58 * @param[in] times Vector of times values corresponding to the various
59 * iteration in decreasing order.
60 *
61 * For example, if the method is a BDF2 (\f$n=2\f$), three values of the time
62 * must be in the array: \f$t\f$, \f$t-\Delta t_1\f$ and \f$t-\Delta t_2\f$.
63 *
64 * @return Vector of \f$n\f$ divided differences (\f$\delta^j\f$) used in the
65 * calculation of BDF coefficients.
66 */
67Vector<double>
68delta(unsigned int order,
69 unsigned int n,
70 unsigned int j,
71 const Vector<double> &times);
72
73/**
74 * @brief Get the maximum number of previous time steps supposed by the BDF
75 * schemes implemented in Lethe.
76 *
77 * @return Value of the maximum number of previous time steps used in BDF
78 * schemes implemented in Lethe.
79 *
80 * @note At the moment, this is hardcoded to 3, but eventually this could be
81 * made larger or smaller depending on the methods used.
82 */
83inline unsigned int
88
89/**
90 * @brief Get the number of previous time steps for a specific BDF scheme.
91 *
92 * @param[in] method Time stepping method of the simulation.
93 *
94 * @return Number of previous time steps used in a given BDF scheme.
95 */
96inline unsigned int
110
111/**
112 * @brief Extrapolate vector of solution to time_vector[0] using previous
113 * solution times and previous solutions.
114 *
115 * @tparam DataType Type of the variable being extrapolated (e.g. double,
116 * Tensor<1,dim>)
117 *
118 * @param[in] time_vector Vector of times. The solution will be extrapolated to
119 * time 0. It should be of size @p number_of_previous_solutions+1.
120 *
121 * @param[in] solution_vector Vector of solution. The solution will be
122 * extrapolated to index 0. It should be at least of size @p
123 * number_of_previous_solutions.
124 *
125 * @param[in] number_of_previous_solutions Number of previous solutions used
126 * to extrapolate.
127 *
128 * @param[out] extrapolated_solution Vector of extrapolated solution.
129 */
130
131template <typename DataType>
132void
133bdf_extrapolate(const std::vector<double> &time_vector,
134 const std::vector<std::vector<DataType>> &solution_vector,
135 const unsigned int number_of_previous_solutions,
136 std::vector<DataType> &extrapolated_solution)
137{
138 // If only one previous solution is used, we don't make a Lagrange
139 // polynomial but use the previous value
141 {
142 for (unsigned int q = 0; q < extrapolated_solution.size(); ++q)
143 extrapolated_solution[q] = solution_vector[0][q];
144 return;
145 }
146
147 // Otherwise, we extrapolate with a Lagrange polynomial
148 for (unsigned int q = 0; q < extrapolated_solution.size(); ++q)
149 {
150 // Set extrapolated solution to zero
151 extrapolated_solution[q] = 0;
152
153 // For all the previous solutions which we will use to extrapolate
154 for (unsigned int p = 0; p < number_of_previous_solutions; ++p)
155 {
156 // Factor is the weight of the previous solution fixed by the Lagrange
157 // polynomial
158 double factor = 1;
159 for (unsigned int k = 0; k < number_of_previous_solutions; ++k)
160 {
161 if (p != k)
162 {
163 // The time vector also contains the time of the solution to
164 // be extrapolated to, hence the +1
165 factor *= (time_vector[0] - time_vector[k + 1]) /
166 (time_vector[p + 1] - time_vector[k + 1]);
167 }
168 }
169 // Calculate the contribution of this time step to the extrapolated
170 // solution
171 extrapolated_solution[q] += solution_vector[p][q] * factor;
172 }
173 }
174}
175
176
177#endif
unsigned int maximum_number_of_previous_solutions()
Get the maximum number of previous time steps supposed by the BDF schemes implemented in Lethe.
Definition bdf.h:84
void bdf_extrapolate(const std::vector< double > &time_vector, const std::vector< std::vector< DataType > > &solution_vector, const unsigned int number_of_previous_solutions, std::vector< DataType > &extrapolated_solution)
Extrapolate vector of solution to time_vector[0] using previous solution times and previous solutions...
Definition bdf.h:133
Vector< double > calculate_bdf_coefficients(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
unsigned int number_of_previous_solutions(const Parameters::SimulationControl::TimeSteppingMethod method)
Get the number of previous time steps for a specific BDF scheme.
Definition bdf.h:97
Vector< double > delta(unsigned int order, unsigned int n, 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