Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
fluid_dynamics_block.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2019-2025 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#ifndef lethe_fluid_dynamics_block_h
5#define lethe_fluid_dynamics_block_h
6
7#include <core/vector.h>
8
10
11#include <deal.II/lac/precondition_block.h>
12#include <deal.II/lac/solver_bicgstab.h>
13#include <deal.II/lac/solver_cg.h>
14#include <deal.II/lac/solver_gmres.h>
15#include <deal.II/lac/sparse_direct.h>
16#include <deal.II/lac/trilinos_block_sparse_matrix.h>
17#include <deal.II/lac/trilinos_parallel_block_vector.h>
18#include <deal.II/lac/trilinos_precondition.h>
19#include <deal.II/lac/trilinos_solver.h>
20
21using namespace dealii;
22
23template <class BSPreconditioner>
24class BlockSchurPreconditioner : public EnableObserverPointer
25{
26public:
29 const TrilinosWrappers::BlockSparseMatrix &S,
30 const TrilinosWrappers::SparseMatrix &P,
31 const BSPreconditioner *p_amat_preconditioner,
32 const BSPreconditioner *p_pmass_preconditioner,
33 Parameters::LinearSolver solver_parameters);
34
35 void
37
38private:
39 const double gamma;
40 const double kinematic_viscosity;
42 const TrilinosWrappers::BlockSparseMatrix &stokes_matrix;
43 const TrilinosWrappers::SparseMatrix &pressure_mass_matrix;
44 const BSPreconditioner *amat_preconditioner;
45 const BSPreconditioner *pmass_preconditioner;
46};
47
48/**
49 * A solver class for the Navier-Stokes equation using Grad-Div
50 * stabilization
51 *
52 * @tparam dim An integer that denotes the dimension of the space in which
53 * the flow is solved
54 *
55 * @ingroup solvers
56 * @author Bruno Blais, 2019
57 */
58
59template <int dim>
61 : public NavierStokesBase<dim, GlobalBlockVectorType, std::vector<IndexSet>>
62{
63public:
66
67 void
68 solve();
69
70private:
71 /**
72 * @brief Assembles the matrix associated with the solver
73 */
74 void
75 assemble_system_matrix() override;
76
77 /**
78 * @brief Assemble the rhs associated with the solver
79 */
80 void
81 assemble_system_rhs() override;
82
83 /**
84 * @brief Set up preconditioner. Not used for this solver but
85 * needed for other physics solvers.
86 */
87 void
88 setup_preconditioner() override;
89
90 /**
91 * @brief Update the average velocity field solution in the multiphysics interface
92 */
93 virtual void
95
96 /**
97 * @brief Assemble the local matrix for a given cell.
98 *
99 * This function is used by the WorkStream class to assemble
100 * the system matrix. It is a thread safe function.
101 *
102 * @param cell The cell for which the local matrix is assembled.
103 *
104 * @param scratch_data The scratch data which is used to store
105 * the calculated finite element information at the gauss point.
106 * See the documentation for NavierStokesScratchData for more
107 * information
108 *
109 * @param copy_data The copy data which is used to store
110 * the results of the assembly over a cell
111 */
112 void
114 const typename DoFHandler<dim>::active_cell_iterator &cell,
115 NavierStokesScratchData<dim> &scratch_data,
117
118 /**
119 * @brief Assemble the local rhs for a given cell
120 *
121 * @param cell The cell for which the local matrix is assembled.
122 *
123 * @param scratch_data The scratch data which is used to store
124 * the calculated finite element information at the gauss point.
125 * See the documentation for NavierStokesScratchData for more
126 * information
127 *
128 * @param copy_data The copy data which is used to store
129 * the results of the assembly over a cell
130 */
131 void
133 const typename DoFHandler<dim>::active_cell_iterator &cell,
134 NavierStokesScratchData<dim> &scratch_data,
136
137 /**
138 * @brief sets up the vector of assembler functions
139 */
140 void
142
143
144 /**
145 * @brief Copy local cell information to global matrix
146 */
147
148 void
150 const StabilizedMethodsTensorCopyData<dim> &copy_data);
151
152 /**
153 * @brief Copy local cell rhs information to global rhs
154 */
155
156 void
158 const StabilizedMethodsTensorCopyData<dim> &copy_data);
159
160
161 void
163
164 virtual void
165 setup_dofs_fd() override;
166
167 /**
168 * @brief Update the sum_over_previous_stages variable to use it in the solver.
169 *
170 * @param stage An unsigned integer which gives the index of the current stage
171 * @param method SDIRK method for now (BDF methods are single stage)
172 */
173 void
175 unsigned int stage,
177
178 /**
179 * @brief Calculate \f$ k_i \f$ from \f$ u*_i : k_i = \frac{u*_i - u_n}{time_step*a_ii} - \sum{j=1}^{i-1} a_ij * k_j \f$
180 * Update \f$ \sum_{i=1}^{N_{stages}} b_i * k_i \f$
181 *
182 * @param stage An unsigned integer which gives the index of the current stage
183 * @param method SDIRK methods for now (BDF methods are single stage)
184 */
185 virtual void
187 unsigned int stage,
189 double time_step) override;
190
191 /**
192 * @brief \f$ u_{n+1} = u_n + time_step * \sum_{i=1}^{N_{stages}} b_i * k_i \f$
193 *
194 * @param time_step The current time step to build the solution at the next time step
195 */
196 virtual void
197 update_multi_stage_solution(double time_step) override;
198
199 /**
200 * @brief Sets the initial condition for the solver
201 *
202 * If the simulation is restarted from a checkpoint, the initial solution
203 * setting is bypassed and the checkpoint is instead read.
204 *
205 * @param initial_condition_type The type of initial condition to be set
206 *
207 * @param restart A boolean that indicates if the simulation is being restarted.
208 * if set to true, the initial conditions are never set, but are instead
209 * overridden by the read_checkpoint functionality.
210 *
211 **/
212 virtual void
215 bool restart = false) override;
216
217 void
218 set_solution_vector(double value);
219
220 /**
221 * Solver for the L2 Projection linear system
222 */
223
224 void
225 solve_L2_system(double relative_residual, double minimum_residual);
226
227 /**
228 * Solver for the NS linear system of equations
229 */
230
231 void
232 solve_linear_system() override;
233
234 /**
235 * GMRES solver with ILU preconditioning or AMG preconditioning
236 */
237 void
238 solve_system_GMRES(const double relative_residual,
239 const double minimum_residual);
240
241 /**
242 * Set-up AMG preconditioner
243 */
244 void
245 setup_AMG();
246
247 /**
248 * Set-up ILU preconditioner
249 */
250 void
251 setup_ILU();
252
253
254
255 /**
256 * Members
257 */
258 TrilinosWrappers::BlockSparsityPattern sparsity_pattern;
259 TrilinosWrappers::BlockSparseMatrix system_matrix;
260 TrilinosWrappers::SparseMatrix pressure_mass_matrix;
261
262 std::vector<types::global_dof_index> dofs_per_block;
263
264 std::shared_ptr<TrilinosWrappers::PreconditionILU>
266 std::shared_ptr<TrilinosWrappers::PreconditionAMG>
268 std::shared_ptr<TrilinosWrappers::PreconditionILU>
270 std::shared_ptr<TrilinosWrappers::PreconditionAMG>
272
273 std::shared_ptr<BlockSchurPreconditioner<TrilinosWrappers::PreconditionILU>>
275
276 std::shared_ptr<BlockSchurPreconditioner<TrilinosWrappers::PreconditionAMG>>
278
279 const double gamma = 1;
280};
281
282
283
284// We can notice that the initialization of the inverse of the matrix at the
285// top left corner is completed in the constructor. If so, every application
286// of the preconditioner then no longer requires the computation of the
287// matrix factors.
288template <typename BSPreconditioner>
290 double gamma,
291 double kinematic_viscosity,
292 const TrilinosWrappers::BlockSparseMatrix &S,
293 const TrilinosWrappers::SparseMatrix &P,
294 const BSPreconditioner *p_amat_preconditioner,
295 const BSPreconditioner *p_pmass_preconditioner,
296 Parameters::LinearSolver p_solver_parameters)
297 : gamma(gamma)
299 , linear_solver_parameters(p_solver_parameters)
300 , stokes_matrix(S)
302 , amat_preconditioner(p_amat_preconditioner)
303 , pmass_preconditioner(p_pmass_preconditioner)
304{}
305
306template <class BSPreconditioner>
307void
310 const GlobalBlockVectorType &src) const
311{
312 // TimerOutput computing_timer(std::cout,
313 // TimerOutput::summary,
314 // TimerOutput::wall_times);
315
316 GlobalVectorType utmp(src.block(0));
317 {
318 // computing_timer.enter_section("Pressure");
319 SolverControl solver_control(
320 linear_solver_parameters.max_iterations,
321 std::max(
322 1e-3 * src.block(1).l2_norm(),
323 // linear_solver_parameters.relative_residual*src.block(0).l2_norm(),
324 linear_solver_parameters.minimum_residual));
325 SolverCG<GlobalVectorType> cg(solver_control);
326
327 dst.block(1) = 0.0;
328 cg.solve(pressure_mass_matrix,
329 dst.block(1),
330 src.block(1),
332 dst.block(1) *= -(kinematic_viscosity + gamma);
333 // computing_timer.exit_section("Pressure");
334 }
335
336 {
337 // computing_timer.enter_section("Operations");
338 stokes_matrix.block(0, 1).vmult(utmp, dst.block(1));
339 utmp *= -1.0;
340 utmp += src.block(0);
341 // computing_timer.exit_section("Operations");
342 }
343 {
344 // computing_timer.enter_section("A Matrix");
345 SolverControl solver_control(
346 linear_solver_parameters.max_iterations,
347 std::max(1e-1 * src.block(0).l2_norm(),
348 linear_solver_parameters.minimum_residual));
349
350
351
352 // TrilinosWrappers::SolverBicgstab solver(solver_control);
353#ifndef LETHE_USE_LDV
354 TrilinosWrappers::SolverGMRES solver(solver_control);
355#else
356 SolverGMRES<GlobalVectorType> solver(solver_control);
357#endif
358
359 // A_inverse.solve(stokes_matrix.block(0, 0),dst.block(0), utmp,
360 // mp_preconditioner);
361 solver.solve(stokes_matrix.block(0, 0),
362 dst.block(0),
363 utmp,
365 // computing_timer.exit_section("A Matrix");
366 }
367}
368
369
370
371template <class PreconditionerMp>
372class BlockDiagPreconditioner : public EnableObserverPointer
373{
374public:
375 BlockDiagPreconditioner(const TrilinosWrappers::BlockSparseMatrix &S,
376 const PreconditionerMp &Mppreconditioner,
377 SolverControl &A_parameters);
378
379 void
380 vmult(GlobalBlockVectorType &dst, const GlobalBlockVectorType &src) const;
381
382private:
383 const TrilinosWrappers::BlockSparseMatrix &stokes_matrix;
384 TrilinosWrappers::PreconditionILU amat_preconditioner;
385 TrilinosWrappers::PreconditionILU pmat_preconditioner;
386 const PreconditionerMp &mp_preconditioner;
387 SolverFGMRES<GlobalVectorType> A_inverse;
388};
389
390template <class PreconditionerMp>
392 const TrilinosWrappers::BlockSparseMatrix &S,
393 const PreconditionerMp &Mppreconditioner,
394 SolverControl &A_parameters)
395 : stokes_matrix(S)
396 , mp_preconditioner(Mppreconditioner)
397 , A_inverse(A_parameters)
398{
399 TrilinosWrappers::PreconditionILU::AdditionalData preconditionerOptions(0,
400 1e-12,
401 1,
402 0);
403 amat_preconditioner.initialize(stokes_matrix.block(0, 0),
404 preconditionerOptions);
405
406 TrilinosWrappers::PreconditionILU::AdditionalData pmat_preconditionerOptions(
407 0, 1e-12, 1, 0);
408 TrilinosWrappers::PreconditionILU pmat_preconditioner;
409 pmat_preconditioner.initialize(stokes_matrix.block(1, 1),
410 pmat_preconditionerOptions);
411}
412
413template <class PreconditionerMp>
414void
417 const GlobalBlockVectorType &src) const
418{
419 {
420 SolverControl solver_control(100000, 1e-12);
421 TrilinosWrappers::SolverCG cg(solver_control);
422
423 cg.solve(stokes_matrix.block(1, 1),
424 dst.block(1),
425 src.block(1),
427 }
428 {
429 SolverControl solver_control(10000, 1e-12);
430
431 TrilinosWrappers::SolverGMRES solver(solver_control);
432 solver.solve(stokes_matrix.block(0, 0),
433 dst.block(0),
434 src.block(0),
436 }
437}
438
439
440
441#endif
void vmult(GlobalBlockVectorType &dst, const GlobalBlockVectorType &src) const
const PreconditionerMp & mp_preconditioner
TrilinosWrappers::PreconditionILU pmat_preconditioner
BlockDiagPreconditioner(const TrilinosWrappers::BlockSparseMatrix &S, const PreconditionerMp &Mppreconditioner, SolverControl &A_parameters)
SolverFGMRES< GlobalVectorType > A_inverse
const TrilinosWrappers::BlockSparseMatrix & stokes_matrix
TrilinosWrappers::PreconditionILU amat_preconditioner
const TrilinosWrappers::BlockSparseMatrix & stokes_matrix
const Parameters::LinearSolver linear_solver_parameters
const BSPreconditioner * amat_preconditioner
void vmult(GlobalBlockVectorType &dst, const GlobalBlockVectorType &src) const
const TrilinosWrappers::SparseMatrix & pressure_mass_matrix
BlockSchurPreconditioner(double gamma, double kinematic_viscosity, const TrilinosWrappers::BlockSparseMatrix &S, const TrilinosWrappers::SparseMatrix &P, const BSPreconditioner *p_amat_preconditioner, const BSPreconditioner *p_pmass_preconditioner, Parameters::LinearSolver solver_parameters)
const BSPreconditioner * pmass_preconditioner
virtual void multi_stage_postresolution(unsigned int stage, Parameters::SimulationControl::TimeSteppingMethod method, double time_step) override
Calculate from Update .
void solve_linear_system() override
void set_solution_vector(double value)
std::vector< types::global_dof_index > dofs_per_block
void multi_stage_preresolution(unsigned int stage, Parameters::SimulationControl::TimeSteppingMethod method) override
Update the sum_over_previous_stages variable to use it in the solver.
std::shared_ptr< TrilinosWrappers::PreconditionAMG > velocity_amg_preconditioner
virtual void update_multiphysics_time_average_solution() override
Update the average velocity field solution in the multiphysics interface.
std::shared_ptr< BlockSchurPreconditioner< TrilinosWrappers::PreconditionAMG > > system_amg_preconditioner
TrilinosWrappers::SparseMatrix pressure_mass_matrix
void assemble_local_system_matrix(const typename DoFHandler< dim >::active_cell_iterator &cell, NavierStokesScratchData< dim > &scratch_data, StabilizedMethodsTensorCopyData< dim > &copy_data)
Assemble the local matrix for a given cell.
std::shared_ptr< BlockSchurPreconditioner< TrilinosWrappers::PreconditionILU > > system_ilu_preconditioner
void solve_L2_system(double relative_residual, double minimum_residual)
void copy_local_matrix_to_global_matrix(const StabilizedMethodsTensorCopyData< dim > &copy_data)
Copy local cell information to global matrix.
void assemble_system_rhs() override
Assemble the rhs associated with the solver.
virtual void update_multi_stage_solution(double time_step) override
void copy_local_rhs_to_global_rhs(const StabilizedMethodsTensorCopyData< dim > &copy_data)
Copy local cell rhs information to global rhs.
std::shared_ptr< TrilinosWrappers::PreconditionAMG > pressure_amg_preconditioner
TrilinosWrappers::BlockSparsityPattern sparsity_pattern
void setup_assemblers()
sets up the vector of assembler functions
virtual void set_initial_condition_fd(Parameters::FluidDynamicsInitialConditionType initial_condition_type, bool restart=false) override
Sets the initial condition for the solver.
std::shared_ptr< TrilinosWrappers::PreconditionILU > velocity_ilu_preconditioner
void solve_system_GMRES(const double relative_residual, const double minimum_residual)
FluidDynamicsBlock(SimulationParameters< dim > &nsparam)
void setup_preconditioner() override
Set up preconditioner. Not used for this solver but needed for other physics solvers.
TrilinosWrappers::BlockSparseMatrix system_matrix
void assemble_system_matrix() override
Assembles the matrix associated with the solver.
virtual void setup_dofs_fd() override
Initialize the dofs for fluid dynamics.
void assemble_local_system_rhs(const typename DoFHandler< dim >::active_cell_iterator &cell, NavierStokesScratchData< dim > &scratch_data, StabilizedMethodsTensorCopyData< dim > &copy_data)
Assemble the local rhs for a given cell.
std::shared_ptr< TrilinosWrappers::PreconditionILU > pressure_ilu_preconditioner
NavierStokesBase(SimulationParameters< dim > &nsparam, const FluidDynamicsFormulation fluid_dynamics_formulation=FluidDynamicsFormulation::standard)
Class that stores the information required by the assembly procedure for a Navier-Stokes equation....
Class responsible for storing the information calculated using the assembly of stabilized Tensor<1,...
Definition copy_data.h:166
LinearSolver - Parameters that control the solution of the linear system of equations that arise from...
dealii::TrilinosWrappers::MPI::Vector GlobalVectorType
Definition vector.h:17
dealii::TrilinosWrappers::MPI::BlockVector GlobalBlockVectorType
Definition vector.h:18