Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
imex.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_DAE_IMEX_HPP_
25#define VULPES_TIMEINTEGRATION_DAE_IMEX_HPP_
26
27#include <limits>
28#include <vector>
29
32#include "vulpes/la/vector.hpp"
33
34namespace vulpes {
35namespace timeintegration {
36
42template <typename ButcherTableType>
44 static inline constexpr std::size_t n_stages = ButcherTableType::n_stages;
45 static constexpr bool FSAL =
46 (ButcherTableType::c.back() == 1.0) &&
47 (ButcherTableType::A_ex.back() == ButcherTableType::b_ex) &&
48 (ButcherTableType::A_im.back() == ButcherTableType::b_im);
49
50 public:
51 using real_t = PetscScalar;
52
54 std::function<void(const la::Vector&, real_t, la::Vector&)>;
55 using ForceFunc = std::function<void(real_t, la::Vector&)>;
56
58 real_t initial_time,
59 real_t final_time,
60 real_t step_size,
61 const la::Vector& initial_condition,
62 const la::SparseMatrix& mass_matrix,
63 const la::SparseMatrix& linear_op,
64 const NonlinearPartFunc& non_linear_op,
65 const ForceFunc& force_func)
66 : m_initial_time(initial_time),
67 m_final_time(final_time),
68 m_step_size(step_size),
70 m_solution(initial_condition),
71 m_mass_matrix(mass_matrix),
72 m_linear_op(linear_op),
73 m_non_linear_op(non_linear_op),
74 m_force_func(force_func),
75 m_force_eval(initial_condition.getLocAndGlobSize()),
76 m_stage_sol(initial_condition.getLocAndGlobSize()),
77 m_stage_rhs_ex(initial_condition.getLocAndGlobSize()),
78 m_stage_rhs_im(initial_condition.getLocAndGlobSize()),
79 m_tmp(initial_condition.getLocAndGlobSize()),
80 m_stage_operator(mass_matrix, MAT_COPY_VALUES) {
81 static_assert(ButcherTableType::has_explicit_part);
82 initialize();
83 }
84
86 real_t initial_time,
87 real_t final_time,
88 real_t step_size,
89 const la::Vector& initial_condition,
90 const la::SparseMatrix& mass_matrix,
91 const la::SparseMatrix& linear_op,
92 const ForceFunc& force_func)
93 : m_initial_time(initial_time),
94 m_final_time(final_time),
95 m_step_size(step_size),
97 m_solution(initial_condition),
98 m_mass_matrix(mass_matrix),
99 m_linear_op(linear_op),
100 m_non_linear_op(nullptr),
101 m_force_func(force_func),
102 m_force_eval(initial_condition.getLocAndGlobSize()),
103 m_stage_sol(initial_condition.getLocAndGlobSize()),
104 m_stage_rhs_ex(initial_condition.getLocAndGlobSize()),
105 m_stage_rhs_im(initial_condition.getLocAndGlobSize()),
106 m_tmp(initial_condition.getLocAndGlobSize()),
107 m_stage_operator(mass_matrix, MAT_COPY_VALUES) {
108 initialize();
109 }
110
111 void step() {
112 const std::size_t first_stage = prepareFSAL();
113 for (std::size_t i = first_stage; i < n_stages; ++i) {
114 const double stage_time = m_time + c[i] * m_step_size;
115 m_force_func(stage_time, m_force_eval);
116
117 // U_i = U_{n-1} + \sum a_{ij}_{ex} h K_j + \sum \hat{a}_{ij}_{im} h
118 // \hat{K}_j
120 if constexpr (ButcherTableType::has_explicit_part) {
121 for (std::size_t j = 0; j < i; ++j) {
123 }
124 }
125
126 // compute U* (in stage_sol; stage_sol now holds U_i)
127 // (M + h a_ii L) U* = h a_ii F + M U_i [implicit]
128 // M U* = M U_i [explicit] -> nothing to do
129 if constexpr (ButcherTableType::has_implicit_part) {
130 // U_i = U_{n-1} + \sum a_{ij} h K_j + \sum \hat{a}_{ij} h \hat{K}_j
131 for (std::size_t j = 0; j < i; ++j) {
133 }
134
135 if (A_im[i][i] != 0.0) {
136 // tmp = M U_i
138 // tmp += h a_ii F
139 m_tmp.add(m_step_size * A_im[i][i], m_force_eval);
140 // stage_operator = (M + h a_ii L)
141 m_stage_operator.copy(m_mass_matrix, SUBSET_NONZERO_PATTERN);
142 m_stage_operator.addScaledMatrix(
143 m_linear_op, m_step_size * A_im[i][i], SUBSET_NONZERO_PATTERN);
144 // Solve: (M + h a_ii L) U* = h a_ii F + M U_i
145 // TODO: this is actually a PETSc bug
146 // diff --git a/src/mat/impls/aij/seq/inode.c
147 // b/src/mat/impls/aij/seq/inode.c index 34eb9f1b7f7..0081e76c64b
148 // 100644
149 // --- a/src/mat/impls/aij/seq/inode.c
150 // +++ b/src/mat/impls/aij/seq/inode.c
151 // @@ -1385,7 +1385,7 @@ PetscErrorCode
152 // MatLUFactorNumeric_SeqAIJ_Inode(Mat B, Mat A, const MatFactorInfo
153 // nz = ai[r[i] + 1] - ai[r[i]];
154 // ajtmp = aj + ai[r[i]];
155 // v1 = aa + ai[r[i]];
156 // - v2 = aa + ai[r[i] + 1];
157 // + v2 = aa + ai[r[i + 1]];
158 // for (j = 0; j < nz; j++) {
159 // col = ics[ajtmp[j]];
160 // rtmp1[col] = v1[j];
161 la::SparseMatrix stage_op(m_stage_operator, MAT_COPY_VALUES);
162 m_solver.setOperators(stage_op);
163 m_solver.solve(m_tmp, m_stage_sol);
164 }
165 }
166
167 // compute the rhs
168 // rhs [explicit] = N(U_i)
169 if (m_non_linear_op) {
171 } else {
172 m_stage_rhs_ex.zero();
173 }
174 if constexpr (ButcherTableType::has_implicit_part) {
175 // rhs [implicit] = L U_i
177 // rhs [implicit] = F - L U_i
178 m_stage_rhs_im.addScaled(-1.0, m_force_eval);
179 // rhs [explicit] = -N(U_i)
180 m_stage_rhs_ex.scale(-1.0);
181 } else {
182 // rhs = N(U_i) + L U_i
184 // rhs = F - N(U_i) - L U_i
185 m_stage_rhs_ex.addScaled(-1.0, m_force_eval);
186 }
187
188 // Solve for new internal stages explicit/implicit
189 if constexpr (ButcherTableType::has_explicit_part) {
190 m_solver.setOperators(m_mass_matrix);
192 }
193 if constexpr (ButcherTableType::has_implicit_part) {
194 m_solver.setOperators(m_mass_matrix);
196 }
197 }
198
199 // Update the solution
200 for (std::size_t i = 0; i < n_stages; ++i) {
201 if constexpr (ButcherTableType::has_explicit_part) {
203 }
204 if constexpr (ButcherTableType::has_implicit_part) {
206 }
207 }
208
209 // Update the time
210 m_step++;
212 }
213 auto& getSolver() { return m_solver; }
214 const auto& getSolver() const { return m_solver; }
215 const la::Vector& getSolution() const { return m_solution; };
216 real_t getTime() const { return m_time; };
217
218 protected:
219 void initialize() {
220 if constexpr (ButcherTableType::has_explicit_part) {
221 for (std::size_t i = 0; i < n_stages; ++i) {
222 m_internal_stages_ex.emplace_back(m_solution.getLocAndGlobSize());
223 }
224 }
225 if constexpr (ButcherTableType::has_implicit_part) {
226 for (std::size_t i = 0; i < n_stages; ++i) {
227 m_internal_stages_im.emplace_back(m_solution.getLocAndGlobSize());
228 }
229 }
231 m_solver.setPreconditioner(la::LinearSolver::LU);
232
233 if constexpr (ButcherTableType::has_implicit_part) {
234 // prepare the non-zero sparsity pattern
235 m_stage_operator.addScaledMatrix(
236 m_linear_op, std::numeric_limits<double>::infinity(),
237 DIFFERENT_NONZERO_PATTERN);
238 }
239 }
240
241 std::size_t prepareFSAL() {
242 std::size_t first_stage = 0;
243 if constexpr (FSAL) {
244 if (m_step > 0) {
245 first_stage = 1;
246 if constexpr (ButcherTableType::has_explicit_part) {
248 }
249 if constexpr (ButcherTableType::has_implicit_part) {
251 }
252 }
253 }
254 return first_stage;
255 }
256 // state - time
261 std::size_t m_step = 0;
263
264 // operators
269
271
272 // temporary objects
273 std::vector<la::Vector> m_internal_stages_ex;
274 std::vector<la::Vector> m_internal_stages_im;
279
281 // (M + h a_ii L)
283
284 static constexpr auto& A_ex = ButcherTableType::A_ex;
285 static constexpr auto& b_ex = ButcherTableType::b_ex;
286 static constexpr auto& A_im = ButcherTableType::A_im;
287 static constexpr auto& b_im = ButcherTableType::b_im;
288 static constexpr auto& c = ButcherTableType::c;
289};
290
291} // namespace timeintegration
292} // namespace vulpes
293
294#endif // VULPES_TIMEINTEGRATION_DAE_IMEX_HPP_
A linear solver class wrapping PETSc KSP solvers.
Definition linear_solver.hpp:47
@ PrecondOnly
Definition linear_solver.hpp:57
@ LU
Definition linear_solver.hpp:71
Definition sparse_matrix.hpp:52
A wrapper around PETSc Vec for distributed vectors.
Definition vector.hpp:54
std::size_t prepareFSAL()
Definition imex.hpp:241
la::Vector m_stage_sol
Definition imex.hpp:276
const la::SparseMatrix & m_mass_matrix
Definition imex.hpp:265
const ForceFunc m_force_func
Definition imex.hpp:268
static constexpr auto & c
Definition imex.hpp:288
std::size_t m_step
Definition imex.hpp:261
static constexpr auto & A_im
Definition imex.hpp:286
PetscScalar real_t
Definition imex.hpp:51
static constexpr auto & A_ex
Definition imex.hpp:284
static constexpr std::size_t n_stages
Definition imex.hpp:44
la::Vector m_force_eval
Definition imex.hpp:275
la::Vector m_solution
Definition imex.hpp:262
std::vector< la::Vector > m_internal_stages_im
Definition imex.hpp:274
real_t m_initial_time
Definition imex.hpp:257
real_t m_step_size
Definition imex.hpp:259
la::Vector m_stage_rhs_im
Definition imex.hpp:278
std::function< void(real_t, la::Vector &)> ForceFunc
Definition imex.hpp:55
real_t getTime() const
Definition imex.hpp:216
static constexpr bool FSAL
Definition imex.hpp:45
la::Vector m_stage_rhs_ex
Definition imex.hpp:277
std::vector< la::Vector > m_internal_stages_ex
Definition imex.hpp:273
TimeIntegratorDIRKIMEX(real_t initial_time, real_t final_time, real_t step_size, const la::Vector &initial_condition, const la::SparseMatrix &mass_matrix, const la::SparseMatrix &linear_op, const ForceFunc &force_func)
Definition imex.hpp:85
real_t m_final_time
Definition imex.hpp:258
la::Vector m_tmp
Definition imex.hpp:280
const auto & getSolver() const
Definition imex.hpp:214
const la::SparseMatrix & m_linear_op
Definition imex.hpp:266
auto & getSolver()
Definition imex.hpp:213
la::LinearSolver m_solver
Definition imex.hpp:270
const la::Vector & getSolution() const
Definition imex.hpp:215
static constexpr auto & b_ex
Definition imex.hpp:285
la::SparseMatrix m_stage_operator
Definition imex.hpp:282
void initialize()
Definition imex.hpp:219
TimeIntegratorDIRKIMEX(real_t initial_time, real_t final_time, real_t step_size, const la::Vector &initial_condition, const la::SparseMatrix &mass_matrix, const la::SparseMatrix &linear_op, const NonlinearPartFunc &non_linear_op, const ForceFunc &force_func)
Definition imex.hpp:57
const NonlinearPartFunc m_non_linear_op
Definition imex.hpp:267
std::function< void(const la::Vector &, real_t, la::Vector &)> NonlinearPartFunc
Definition imex.hpp:53
static constexpr auto & b_im
Definition imex.hpp:287
Definition butcher_tables.hpp:32
Definition cell_manager.hpp:38