Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
fluid_dynamics_matrix_free.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2024-2026 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#include <core/grids.h>
6#include <core/manifolds.h>
7#include <core/multiphysics.h>
10#include <core/utilities.h>
11
13
14#include <deal.II/base/exceptions.h>
15
16#include <deal.II/dofs/dof_renumbering.h>
17#include <deal.II/dofs/dof_tools.h>
18
19#include <deal.II/grid/grid_tools.h>
20
21#include <deal.II/lac/precondition.h>
22#include <deal.II/lac/solver_bicgstab.h>
23#include <deal.II/lac/solver_control.h>
24#include <deal.II/lac/solver_gmres.h>
25#include <deal.II/lac/sparse_matrix_tools.h>
26#include <deal.II/lac/vector.h>
27
28#include <deal.II/multigrid/mg_coarse.h>
29#include <deal.II/multigrid/mg_constrained_dofs.h>
30#include <deal.II/multigrid/mg_matrix.h>
31#include <deal.II/multigrid/mg_smoother.h>
32#include <deal.II/multigrid/mg_tools.h>
33#include <deal.II/multigrid/mg_transfer_global_coarsening.h>
34#include <deal.II/multigrid/mg_transfer_matrix_free.h>
35#if DEAL_II_VERSION_GTE(9, 8, 0)
36# include <deal.II/multigrid/mg_transfer_matrix_free.templates.h>
37#else
38# include <deal.II/multigrid/mg_transfer_global_coarsening.templates.h>
39#endif
41
42#include <deal.II/lac/trilinos_solver.h>
43
44#include <deal.II/multigrid/multigrid.h>
45
46#include <deal.II/numerics/vector_tools.h>
47
48#include <cmath>
49#include <exception>
50
51/**
52 * @brief A base class of preconditioners used by the smoother.
53 */
54template <typename VectorType>
55class PreconditionBase : public EnableObserverPointer
56{
57public:
58 /**
59 * @brief Constructor.
60 */
62 : comm(MPI_COMM_WORLD)
63 , pcout(std::cout, Utilities::MPI::this_mpi_process(comm) == 0)
64 , timer(pcout, TimerOutput::never, TimerOutput::wall_times)
65 {}
66
67 /**
68 * @brief Apply preconditioner.
69 *
70 * @param[in,out] dst Destination vector holding the result.
71 * @param[in] src Input source vector.
72 */
73 virtual void
74 vmult(VectorType &dst, const VectorType &src) const = 0;
75
76 /**
77 * @brief Print timers.
78 */
79 void
81 {
82 timer.print_wall_time_statistics(comm);
83 }
84
85 /**
86 * @brief Reset timers.
87 */
88 void
90 {
91 timer.reset();
92 }
93
94protected:
95 /// Communicator used for timer output.
96 const MPI_Comm comm;
97
98 /// Stream used for timer output.
99 ConditionalOStream pcout;
100
101 /// Timer.
102 mutable TimerOutput timer;
103};
104
105/**
106 * @brief A wrapper class around a preconditioner to be
107 * able to handle vector types that are not supported
108 * by the preconditioner.
109 */
110template <typename VectorType, typename VectorTypePrecondition>
111class PreconditionAdapter : public PreconditionBase<VectorType>
112{
113public:
114 /**
115 * Constructor.
116 *
117 * @param[in] preconditioner Preconditioner.
118 */
119 template <typename PreconditionType>
120 PreconditionAdapter(const std::shared_ptr<PreconditionType> &preconditioner)
121 : fu_preconditioner([preconditioner](VectorTypePrecondition &dst,
122 const VectorTypePrecondition &src) {
123 preconditioner->vmult(dst, src);
124 })
125 {}
126
127 /**
128 * @brief Apply preconditioner.
129 *
130 * @param[in,out] dst Destination vector holding the result.
131 * @param[in] src Input source vector.
132 */
133 void
134 vmult(VectorType &dst, const VectorType &src) const override
135 {
136 if constexpr (std::is_same_v<VectorType, VectorTypePrecondition>)
137 {
138 // vector is supported: nothing to do
139 fu_preconditioner(dst, src);
140 }
141 else
142 {
143 // vector is not supported: copy vectors
144 src_ = src;
145 dst_ = dst;
147 dst = dst_;
148 }
149 }
150
151 /**
152 * @brief Apply preconditioner.
153 *
154 * @param[in,out] dst Destination vector holding the result.
155 * @param[in] src Input source vector.
156 */
157 template <typename U = VectorTypePrecondition>
158 requires(!std::is_same_v<VectorType, U>)
159 void
160 vmult(U &dst, const U &src) const
161 {
162 fu_preconditioner(dst, src);
163 }
164
165private:
166 /// Preconditioner.
167 const std::function<void(VectorTypePrecondition &,
168 const VectorTypePrecondition &)>
170
171 /// Source vector supported by the preconditioner.
172 mutable VectorTypePrecondition src_;
173
174 /// Destination vector supported by the preconditioner.
175 mutable VectorTypePrecondition dst_;
176};
177
178/**
179 * @brief A wrapper class around DiagonalMatrix class from deal.II.
180 */
181template <typename VectorType>
182class MyDiagonalMatrix : public PreconditionBase<VectorType>
183{
184public:
185 /**
186 * @brief Constructor.
187
188 * @param[in] diagonal Vector containing the diagonal of the matrix.
189 */
191 : diagonal_matrix(diagonal)
192 {}
193
194 /**
195 * @brief Apply preconditioner.
196 *
197 * @param[in,out] dst Destination vector holding the result.
198 * @param[in] src Input source vector.
199 */
200 void
201 vmult(VectorType &dst, const VectorType &src) const override
202 {
203 diagonal_matrix.vmult(dst, src);
204 }
205
206private:
207 /// DiagonalMatrix class from deal.II.
208 DiagonalMatrix<VectorType> diagonal_matrix;
209};
210
211/**
212 * @brief An Additive Schwarz preconditioner.
213 */
214template <typename VectorType>
215class PreconditionASM : public PreconditionBase<VectorType>
216{
217private:
218 /// Weighting type.
219 enum WeightingType : std::uint8_t
220 {
225 };
226
227public:
228 /// Value type.
229 using Number = typename VectorType::value_type;
230
231 /**
232 * @brief Constructor.
233 */
237
238 /**
239 * @brief Initialize inverses of blocks.
240 */
241 template <int dim,
242 typename GlobalSparseMatrixType,
243 typename GlobalSparsityPattern,
244 typename Number>
245 void
247 const DoFHandler<dim> &dof_handler,
248 const GlobalSparseMatrixType &global_sparse_matrix,
249 const GlobalSparsityPattern &global_sparsity_pattern,
250 const AffineConstraints<Number> &constraints,
251 const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner,
252 const unsigned int mg_level)
253 {
254 this->timer.enter_subsection("asm::indices");
255
256 std::vector<std::vector<types::global_dof_index>> patches;
257
258 const auto add_indices = [&](const auto &local_dof_indices) {
259 std::vector<types::global_dof_index> local_dof_indices_temp;
260
261 for (const auto i : local_dof_indices)
262 if (!constraints.is_constrained(i))
263 local_dof_indices_temp.emplace_back(i);
264
265 if (!local_dof_indices_temp.empty())
266 patches.push_back(local_dof_indices_temp);
267 };
268
269 if (mg_level == numbers::invalid_unsigned_int)
270 for (const auto &cell : dof_handler.active_cell_iterators())
271 {
272 if (cell->is_locally_owned() == false)
273 continue;
274
275 std::vector<types::global_dof_index> local_dof_indices(
276 cell->get_fe().n_dofs_per_cell());
277 cell->get_dof_indices(local_dof_indices);
278
279 add_indices(local_dof_indices);
280 }
281 else
282 for (const auto &cell : dof_handler.mg_cell_iterators_on_level(mg_level))
283 {
284 if (cell->is_locally_owned_on_level() == false)
285 continue;
286
287 std::vector<types::global_dof_index> local_dof_indices(
288 cell->get_fe().n_dofs_per_cell());
289 cell->get_mg_dof_indices(local_dof_indices);
290
291 add_indices(local_dof_indices);
292 }
293
294 const IndexSet locally_owned_dofs =
295 (mg_level == numbers::invalid_unsigned_int) ?
296 dof_handler.locally_owned_dofs() :
297 dof_handler.locally_owned_mg_dofs(mg_level);
298
299 IndexSet ghost_dofs(locally_owned_dofs.size());
300 for (const auto &indices : patches)
301 ghost_dofs.add_indices(indices.begin(), indices.end());
302
303 std::shared_ptr<const Utilities::MPI::Partitioner> partition =
304 std::make_shared<Utilities::MPI::Partitioner>(locally_owned_dofs,
305 ghost_dofs,
306 MPI_COMM_WORLD);
307
308 if (dealii::internal::is_partitioner_contained(partition, partitioner))
309 {
310 partition = partitioner;
311 }
312 else
313 {
314 src_internal.reinit(partition);
315 dst_internal.reinit(partition);
316 }
317
318 // convert indices to local ones
319 this->patches.resize(patches.size());
320 for (unsigned int c = 0; c < patches.size(); ++c)
321 {
322 this->patches[c].resize(0);
323 this->patches[c].reserve(patches[c].size());
324
325 for (const auto &i : patches[c])
326 this->patches[c].emplace_back(partition->global_to_local(i));
327 }
328
329 this->timer.leave_subsection("asm::indices");
330 this->timer.enter_subsection("asm::restrict");
331
332 std::vector<FullMatrix<Number>> blocks;
333
334 // The deprecation warning is triggered internally by deal.II: the call
335 // below instantiates SparseMatrixTools code that still relies on the
336 // deprecated ConsensusAlgorithms::Process class. Suppress it locally until
337 // it is resolved upstream.
338#pragma GCC diagnostic push
339#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
340 SparseMatrixTools::restrict_to_full_matrices(global_sparse_matrix,
341 global_sparsity_pattern,
342 patches,
343 blocks);
344#pragma GCC diagnostic pop
345
346 this->timer.leave_subsection("asm::restrict");
347 this->timer.enter_subsection("asm::invert");
348
349 this->blocks.resize(blocks.size());
350
351 for (unsigned int b = 0; b < blocks.size(); ++b)
352 {
353 this->blocks[b] =
354 LAPACKFullMatrix<Number>(blocks[b].m(), blocks[b].n());
355 this->blocks[b] = blocks[b];
356 this->blocks[b].compute_lu_factorization();
357 }
358
359 this->timer.leave_subsection("asm::invert");
360
362 {
363 this->timer.enter_subsection("asm::weight");
364 Vector<Number> vector_weights;
365 weights.reinit(partition);
366
367 for (const auto &patch : patches)
368 {
369 const unsigned int dofs_per_cell = patch.size();
370 vector_weights.reinit(dofs_per_cell);
371
372 for (unsigned int i = 0; i < dofs_per_cell; ++i)
373 vector_weights[i] = 1.0;
374
375 for (unsigned int i = 0; i < dofs_per_cell; ++i)
376 weights[patch[i]] += vector_weights[i];
377 }
378
379 weights.compress(VectorOperation::add);
380 for (auto &i : weights)
381 i = (weighting_type == WeightingType::symm) ? std::sqrt(1.0 / i) :
382 (1.0 / i);
383 weights.update_ghost_values();
384
385 this->timer.leave_subsection("asm::weight");
386 }
387 }
388
389 /**
390 * @brief Apply preconditioner.
391 *
392 * @param[in,out] dst Destination vector holding the result.
393 * @param[in] src Input source vector.
394 */
395 void
396 vmult(VectorType &dst, const VectorType &src) const override
397 {
398 this->timer.enter_subsection("asm::vmult");
399
400 const auto &src_ptr = (src_internal.size() != 0) ? src_internal : src;
401 auto &dst_ptr = (dst_internal.size() != 0) ? dst_internal : dst;
402
403 dst_ptr = 0.0;
404 if (src_internal.size() != 0)
405 src_internal.copy_locally_owned_data_from(src);
406 src_ptr.update_ghost_values();
407
408 Vector<Number> vector_src, vector_dst, vector_weights;
409
410 for (unsigned int c = 0; c < patches.size(); ++c)
411 {
412 const unsigned int dofs_per_cell = patches[c].size();
413
414 vector_src.reinit(dofs_per_cell);
415 vector_dst.reinit(dofs_per_cell);
417 vector_weights.reinit(dofs_per_cell);
418
419 for (unsigned int i = 0; i < dofs_per_cell; ++i)
420 vector_src[i] = src_ptr.local_element(patches[c][i]);
421
423 for (unsigned int i = 0; i < dofs_per_cell; ++i)
424 vector_weights[i] = weights.local_element(patches[c][i]);
425
428 for (unsigned int i = 0; i < dofs_per_cell; ++i)
429 vector_src[i] *= vector_weights[i];
430
431 blocks[c].solve(vector_src);
432 vector_dst = vector_src;
433
436 for (unsigned int i = 0; i < dofs_per_cell; ++i)
437 vector_dst[i] *= vector_weights[i];
438
439 for (unsigned int i = 0; i < dofs_per_cell; ++i)
440 dst_ptr.local_element(patches[c][i]) += vector_dst[i];
441 }
442
443 src_ptr.zero_out_ghost_values();
444 dst_ptr.compress(VectorOperation::add);
445
446 if (dst_internal.size() != 0)
447 dst.copy_locally_owned_data_from(dst_internal);
448
449 this->timer.leave_subsection("asm::vmult");
450 }
451
452private:
453 /// DoF indices of patches.
454 std::vector<std::vector<unsigned int>> patches;
455
456 /// Inverse of patch matrices.
457 std::vector<LAPACKFullMatrix<Number>> blocks;
458
459 /// Weights.
461
462 /// Weighting type.
464
465 /// Internal destination vector with correct ghosting.
467
468 /// Internal source vector with correct ghosting.
470};
471
472
473
474namespace dealii
475{
476 /**
477 * Coarse grid solver using a preconditioner only. This is a little wrapper,
478 * transforming a preconditioner into a coarse grid solver.
479 */
480 template <class VectorType, class PreconditionerType>
481 class MGCoarseGridApplyPreconditioner : public MGCoarseGridBase<VectorType>
482 {
483 public:
484 /**
485 * Default constructor.
486 */
488
489 /**
490 * Constructor. Store a pointer to the preconditioner for later use.
491 */
492 MGCoarseGridApplyPreconditioner(const PreconditionerType &precondition);
493
494 /**
495 * Clear the pointer.
496 */
497 void
498 clear();
499
500 /**
501 * Initialize new data.
502 */
503 void
504 initialize(const PreconditionerType &precondition);
505
506 /**
507 * Implementation of the abstract function.
508 */
509 virtual void
510 operator()(const unsigned int level,
511 VectorType &dst,
512 const VectorType &src) const override;
513
514 private:
515 /**
516 * Reference to the preconditioner.
517 */
518 ObserverPointer<
519 const PreconditionerType,
522 };
523
524
525
526 template <class VectorType, class PreconditionerType>
531
532
533
534 template <class VectorType, class PreconditionerType>
539
540
541
542 template <class VectorType, class PreconditionerType>
543 void
545 const PreconditionerType &preconditioner_)
546 {
547 preconditioner = &preconditioner_;
548 }
549
550
551
552 template <class VectorType, class PreconditionerType>
553 void
558
559
560
561 template <class VectorType, class PreconditionerType>
562 void
564 const unsigned int /*level*/,
565 VectorType &dst,
566 const VectorType &src) const
567 {
568 preconditioner->vmult(dst, src);
569 }
570} // namespace dealii
571
572/**
573 * A class that wraps MGTransferMatrixFree and MGTransferMF to allow
574 * hp-multigrid in the local-smoothing case.
575 */
576template <int dim, typename Number>
578 : public MGTransferBase<LinearAlgebra::distributed::Vector<Number>>
579{
580public:
581 using VectorType = LinearAlgebra::distributed::Vector<Number>;
582
587
588 void
589 build(const MGLevelObject<MGTwoLevelTransfer<dim, VectorType>> &transfers,
590 const DoFHandler<dim> &dof_handler,
591 const MGConstrainedDoFs &mg_constrained_dofs,
592 const std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
593 &external_partitioners_in)
594 {
595 if (transfers.min_level() != transfers.max_level())
596 {
597 this->n_gc_levels = transfers.max_level() - transfers.min_level();
598
599 std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
600 external_partitioners;
601
602 for (unsigned int l = transfers.min_level(); l <= transfers.max_level();
603 ++l)
604 external_partitioners.emplace_back(external_partitioners_in[l]);
605
606 gc.initialize_two_level_transfers(transfers);
607 gc.build(external_partitioners);
608 }
609
610 ls.initialize_constraints(mg_constrained_dofs);
611
612 std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
613 external_partitioners(min_h_level + external_partitioners_in.size() -
615
616 for (unsigned int i = n_gc_levels; i < external_partitioners_in.size(); ++i)
617 external_partitioners[min_h_level + i - n_gc_levels] =
618 external_partitioners_in[i];
619
620 ls.build(dof_handler, external_partitioners);
621 }
622
623 template <class InVector, int spacedim>
624 void
625 copy_to_mg(const DoFHandler<dim, spacedim> &dof_handler,
626 MGLevelObject<VectorType> &dst,
627 const InVector &src) const
628 {
629 if ((n_gc_levels > 0) && (dst.max_level() <= n_gc_levels))
630 {
631 gc.copy_to_mg(dof_handler, dst, src);
632 return;
633 }
634
635 MGLevelObject<VectorType> dst_gc(0, n_gc_levels);
636 MGLevelObject<VectorType> dst_ls(min_h_level,
637 dst.max_level() + min_h_level -
639
640 for (unsigned int l = dst.min_level(); l <= dst.max_level(); ++l)
641 {
642 if (l <= n_gc_levels)
643 dst_gc[l] = dst[l];
644
645 if (l >= n_gc_levels)
646 dst_ls[min_h_level + (l - n_gc_levels)] = dst[l];
647 }
648
649 ls.copy_to_mg(dof_handler, dst_ls, src);
650
651 if (n_gc_levels > 0)
652 gc.copy_to_mg(dof_handler, dst_gc, dst_ls[dst_ls.min_level()]);
653
654 for (unsigned int l = dst.min_level(); l <= dst.max_level(); ++l)
655 if (l < n_gc_levels)
656 dst[l] = dst_gc[l];
657 else
658 dst[l] = dst_ls[min_h_level + (l - n_gc_levels)];
659 }
660
661 template <class OutVector, int spacedim>
662 void
663 copy_from_mg(const DoFHandler<dim, spacedim> &dof_handler,
664 OutVector &dst,
665 const MGLevelObject<VectorType> &src) const
666 {
667 if ((n_gc_levels > 0) && (src.max_level() <= n_gc_levels))
668 {
669 gc.copy_from_mg(dof_handler, dst, src);
670 return;
671 }
672
673 MGLevelObject<VectorType> src_ls(min_h_level,
674 src.max_level() + min_h_level -
676 for (unsigned int l = n_gc_levels; l <= src.max_level(); ++l)
677 src_ls[min_h_level + (l - n_gc_levels)] = src[l];
678
679 ls.copy_from_mg(dof_handler, dst, src_ls);
680 }
681
682 template <typename InVectorType>
683 void
684 interpolate_to_mg(const DoFHandler<dim> &dof_handler,
685 MGLevelObject<VectorType> &dst,
686 const InVectorType &src) const
687 {
688 if ((n_gc_levels > 0) && (dst.max_level() <= n_gc_levels))
689 {
690 gc.interpolate_to_mg(dof_handler, dst, src);
691 return;
692 }
693
694 MGLevelObject<VectorType> dst_gc(0, n_gc_levels);
695 MGLevelObject<VectorType> dst_ls(min_h_level,
696 dst.max_level() + min_h_level -
698
699 for (unsigned int l = dst.min_level(); l <= dst.max_level(); ++l)
700 {
701 if (l <= n_gc_levels)
702 dst_gc[l] = dst[l];
703
704 if (l >= n_gc_levels)
705 dst_ls[min_h_level + (l - n_gc_levels)] = dst[l];
706 }
707
708 ls.interpolate_to_mg(dof_handler, dst_ls, src);
709
710 if (n_gc_levels > 0)
711 gc.interpolate_to_mg(dof_handler, dst_gc, dst_ls[dst_ls.min_level()]);
712
713 for (unsigned int l = dst.min_level(); l <= dst.max_level(); ++l)
714 if (l < n_gc_levels)
715 dst[l] = dst_gc[l];
716 else
717 dst[l] = dst_ls[min_h_level + (l - n_gc_levels)];
718 }
719
720 void
721 prolongate(const unsigned int to_level,
722 VectorType &dst,
723 const VectorType &src) const override
724 {
725 if (to_level <= n_gc_levels)
726 gc.prolongate(to_level, dst, src);
727 else
728 ls.prolongate(to_level + min_h_level - n_gc_levels, dst, src);
729 }
730
731 void
732 restrict_and_add(const unsigned int from_level,
733 VectorType &dst,
734 const VectorType &src) const override
735 {
736 if (from_level <= n_gc_levels)
737 gc.restrict_and_add(from_level, dst, src);
738 else
739 ls.restrict_and_add(from_level + min_h_level - n_gc_levels, dst, src);
740 }
741
742private:
743 const unsigned int min_h_level;
744 unsigned int n_gc_levels;
745
746 MGTransferMatrixFree<dim, Number> ls;
747#if DEAL_II_VERSION_GTE(9, 8, 0)
748 MGTransferMatrixFree<dim, Number> gc;
749#else
750 MGTransferGlobalCoarsening<dim, VectorType> gc;
751#endif
752};
753
754template <int dim>
757 const DoFHandler<dim> &dof_handler,
758 const DoFHandler<dim> &dof_handler_fe_q_iso_q1)
759 : pcout(std::cout, Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0)
762 , mg_setup_timer(this->pcout, TimerOutput::never, TimerOutput::wall_times)
763 , mg_vmult_timer(this->pcout, TimerOutput::never, TimerOutput::wall_times)
764{
765 (void)dof_handler_fe_q_iso_q1; // TODO: remove
766
767 // If mortar is enabled, we need to change one default parameter of the
768 // function that computes normal vectors. In this case, we cannot use the
769 // manifold to compute them, since it leads to issues at the mortar interface.
770 // Hence we use the mapping instead.
771 this->use_manifold_for_normal = true;
772 if (this->simulation_parameters.mortar_parameters.enable)
773 this->use_manifold_for_normal = false;
774
775 // Verify whether the minimum coarsening degree prescribed is lower than the
776 // problem's polynomial degree
777 AssertThrow(
778 this->dof_handler.get_fe().degree >=
779 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
780 .mg_p_min_coarsening_degree,
781 ExcMessage(
782 "The prescribed minimum polynomial degree for the p-multigrid needs to be lower than the velocity/pressure interpolation order."));
783}
784
785template <int dim>
786void
788 const std::shared_ptr<Mapping<dim>> &mapping,
789 const std::shared_ptr<Quadrature<dim>> &cell_quadrature,
790 const std::shared_ptr<Function<dim>> &forcing_function,
791 const std::shared_ptr<SimulationControl> &simulation_control,
792 const std::shared_ptr<PhysicalPropertiesManager> &physical_properties_manager,
793 const std::shared_ptr<FESystem<dim>> &fe)
794{
795 AssertThrow(*cell_quadrature ==
796 QGauss<dim>(this->dof_handler.get_fe().degree + 1),
797 ExcNotImplemented());
798
799 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
801 {
802 const auto &triangulation = dof_handler.get_triangulation();
803
804 // Define maximum and minimum level according to triangulation
805 const unsigned int n_h_levels = triangulation.n_global_levels();
806 unsigned int min_h_level = 0;
807 const unsigned int max_h_level = n_h_levels - 1;
808
809 // If multigrid number of levels or minimum number of cells in level are
810 // specified, change the min level and print levels information
811
812 int mg_min_level =
814 .mg_min_level;
815
816 int mg_int_level =
818 .mg_int_level;
819
820 int mg_level_min_cells =
822 .mg_level_min_cells;
823
824
825 std::vector<unsigned int> n_cells_on_levels(
826 triangulation.n_global_levels(), 0);
827
828 for (unsigned int l = 0; l < triangulation.n_levels(); ++l)
829 for (const auto &cell : triangulation.cell_iterators_on_level(l))
830 if (cell->is_locally_owned_on_level())
831 n_cells_on_levels[l]++;
832
833 Utilities::MPI::sum(n_cells_on_levels,
834 this->dof_handler.get_mpi_communicator(),
835 n_cells_on_levels);
836
837 if (mg_min_level != -1)
838 {
839 AssertThrow(
840 mg_min_level <= static_cast<int>(MGTools::max_level_for_coarse_mesh(
841 triangulation)),
842 ExcMessage(std::string(
843 "The maximum level allowed for the coarse mesh (mg min level) is: " +
844 std::to_string(
845 MGTools::max_level_for_coarse_mesh(triangulation)) +
846 ".")));
847
848 min_h_level = mg_min_level;
849 }
850 else if (mg_level_min_cells != -1)
851 {
852 AssertThrow(
853 mg_level_min_cells <=
854 static_cast<int>(
855 n_cells_on_levels[MGTools::max_level_for_coarse_mesh(
856 triangulation)]),
857 ExcMessage(
858 "The mg level min cells specified are larger than the cells of the finest mg level."));
859
860 for (unsigned int level = min_h_level; level <= max_h_level; ++level)
861 {
862 const int n_cells_on_level =
863 static_cast<int>(n_cells_on_levels[level]);
864 if (n_cells_on_level >= mg_level_min_cells)
865 {
866 min_h_level = level;
867 break;
868 }
869 }
870 }
871
872 // p-multigrid
873 const auto mg_coarsening_type =
875 .mg_coarsening_type;
876
877 auto polynomial_coarsening_sequence =
878 MGTransferGlobalCoarseningTools::create_polynomial_coarsening_sequence(
879 this->dof_handler.get_fe().degree,
880 this->simulation_parameters.linear_solver
882 .mg_p_coarsening_type);
883
884 // Remove terms of polynomial coarsening sequence that are below the
885 // minimum coarsening degree
886 for (unsigned int p = 1; p < this->simulation_parameters.linear_solver
888 .mg_p_min_coarsening_degree;
889 p++)
890 std::erase(polynomial_coarsening_sequence, p);
891
892 std::vector<std::pair<unsigned int, unsigned int>> levels;
893
894 if (mg_coarsening_type ==
896 {
897 // p
898 for (const auto i : polynomial_coarsening_sequence)
899 levels.emplace_back(min_h_level, i);
900
901 // h
902 for (unsigned int i = min_h_level + 1; i <= max_h_level; ++i)
903 levels.emplace_back(i, polynomial_coarsening_sequence.back());
904 }
905 else if (mg_coarsening_type ==
907 {
908 AssertThrow(false, ExcNotImplemented());
909 }
910 else if (mg_coarsening_type ==
912 {
913 AssertThrow(false, ExcNotImplemented());
914 }
915 else if (mg_coarsening_type ==
917 {
918 // h
919 for (unsigned int i = min_h_level; i <= max_h_level; ++i)
920 levels.emplace_back(i, polynomial_coarsening_sequence.back());
921 }
922 else
923 {
924 AssertThrow(false, ExcNotImplemented());
925 }
926
927 this->minlevel = 0;
928 this->maxlevel = levels.size() - 1;
929
930 this->intlevel = (mg_int_level == -1) ? this->minlevel : mg_int_level;
931
932 std::map<unsigned int, unsigned int> p_map;
933 for (const auto &[_, p] : levels)
934 p_map.insert({p, p_map.size()});
935
936 dof_handlers.resize(0, p_map.size() - 1);
937
938 for (const auto [p, l] : p_map)
939 {
940 this->dof_handlers[l].reinit(triangulation);
941
942 // To use elements with linear interpolation for coarse-grid we need
943 // to create the min level dof handler with the appropriate element
944 // type
945 if (this->simulation_parameters.linear_solver
947 .mg_use_fe_q_iso_q1 &&
948 l == 0)
949 {
950 AssertThrow(
951 mg_coarsening_type ==
953 ExcNotImplemented());
954
955 const auto points = QGaussLobatto<1>(p + 1).get_points();
956
957 this->dof_handler_fe_q_iso_q1.reinit(triangulation);
958 this->dof_handler_fe_q_iso_q1.distribute_dofs(
959 FESystem<dim>(FE_Q_iso_Q1<dim>(points), dim + 1));
960 this->dof_handler_fe_q_iso_q1.distribute_mg_dofs();
961 DoFRenumbering::Cuthill_McKee(this->dof_handler_fe_q_iso_q1);
962 }
963
964 this->dof_handlers[l].distribute_dofs(
965 FESystem<dim>(FE_Q<dim>(p), dim + 1));
966 this->dof_handlers[l].distribute_mg_dofs();
967 DoFRenumbering::Cuthill_McKee(this->dof_handlers[l]);
968 }
969
970 if (this->simulation_parameters.linear_solver
972 .mg_verbosity != Parameters::Verbosity::quiet)
973 {
974 this->pcout << std::endl;
975 this->pcout << " -Levels of MG preconditioner:" << std::endl;
976 for (unsigned int level = this->minlevel; level <= this->maxlevel;
977 ++level)
978 this->pcout << " Level " << level << ": "
979 << this->dof_handlers[p_map[levels[level].second]]
980 .n_dofs(levels[level].first)
981 << " DoFs, " << n_cells_on_levels[levels[level].first]
982 << " cells" << std::endl;
983 this->pcout << std::endl;
984 }
985
986 if (this->simulation_parameters.linear_solver
989 {
990 this->pcout << " -MG vertical communication efficiency: "
991 << MGTools::vertical_communication_efficiency(
992 triangulation)
993 << std::endl;
994
995 this->pcout << " -MG workload imbalance: "
996 << MGTools::workload_imbalance(triangulation)
997 << std::endl;
998 }
999
1000 std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
1001 partitioners(this->maxlevel - this->minlevel + 1);
1002
1003 // Local object for constraints of the different levels
1004 MGLevelObject<AffineConstraints<MGNumber>> level_constraints;
1005
1006 // Resize all multilevel objects according to level
1007 this->mg_operators.resize(this->minlevel, this->maxlevel);
1008 level_constraints.resize(this->minlevel, this->maxlevel);
1009 this->ls_mg_interface_in.resize(this->minlevel, this->maxlevel);
1010 this->ls_mg_operators.resize(this->minlevel, this->maxlevel);
1011
1012 // Fill the level constraints
1013 this->mg_setup_timer.enter_subsection("Set boundary conditions");
1014
1015 this->mg_constrained_dofs.clear();
1016 this->mg_constrained_dofs.resize(dof_handlers.min_level(),
1017 dof_handlers.max_level());
1018
1019 for (unsigned int l = mg_constrained_dofs.min_level();
1020 l <= mg_constrained_dofs.max_level();
1021 ++l)
1022 this->mg_constrained_dofs[l].initialize(this->dof_handlers[l]);
1023
1024 FEValuesExtractors::Vector velocities(0);
1025 FEValuesExtractors::Scalar pressure(dim);
1026
1027 for (auto const &[id, type] :
1028 this->simulation_parameters.boundary_conditions.type)
1029 {
1031 {
1032 std::set<types::boundary_id> no_normal_flux_boundaries;
1033 no_normal_flux_boundaries.insert(id);
1034 for (unsigned int level = this->minlevel; level <= this->maxlevel;
1035 ++level)
1036 {
1037 const unsigned int p_level = p_map[levels[level].second];
1038
1039 AffineConstraints<double> temp_constraints;
1040
1041 const IndexSet locally_owned_level_dofs =
1042 this->dof_handlers[p_level].locally_owned_mg_dofs(
1043 levels[level].first);
1044 const IndexSet locally_relevant_level_dofs =
1045 DoFTools::extract_locally_relevant_level_dofs(
1046 this->dof_handlers[p_level], levels[level].first);
1047 temp_constraints.reinit(locally_owned_level_dofs,
1048 locally_relevant_level_dofs);
1049
1050 VectorTools::compute_no_normal_flux_constraints_on_level(
1051 this->dof_handlers[p_level],
1052 0,
1053 no_normal_flux_boundaries,
1054 temp_constraints,
1055 *mapping,
1056 this->mg_constrained_dofs[p_level]
1057 .get_refinement_edge_indices(levels[level].first),
1058 levels[level].first);
1059 temp_constraints.close();
1060 this->mg_constrained_dofs[p_level].add_user_constraints(
1061 levels[level].first, temp_constraints);
1062 }
1063 }
1064 else if (type ==
1066 {
1067 /*already taken into account when mg_constrained_dofs is
1068 * initialized*/
1069 }
1071 periodic_neighbor)
1072 {
1073 /*already taken into account when mg_constrained_dofs is
1074 * initialized*/
1075 }
1076 else if (type ==
1078 {
1079 /*The pressure boundary condition is implemented in the
1080 * operators*/
1081 }
1082 else if (type ==
1084 {
1085 /*The function weak boundary condition is implemented in the
1086 * operators*/
1087 }
1089 neumann_traction)
1090 {
1091 /*The Neumann traction boundary condition is implemented in the
1092 * operators*/
1093 }
1094 else if (type ==
1096 {
1097 AssertThrow(
1098 false,
1099 ExcMessage(
1100 "Partial slip boundary conditions are not supported by the matrix free application."));
1101 }
1102 else if (type ==
1104 {
1105 /*The directional do-nothing boundary condition is implemented in
1106 * the operators*/
1107 }
1108 else if (type ==
1110 type ==
1112 {
1113 std::set<types::boundary_id> dirichlet_boundary_id = {id};
1114
1115 for (const auto [_, l] : p_map)
1116 this->mg_constrained_dofs[l].make_zero_boundary_constraints(
1117 this->dof_handlers[l],
1118 dirichlet_boundary_id,
1119 fe->component_mask(velocities));
1120 }
1121 else
1122 {
1123 AssertThrow(
1124 false,
1125 ExcMessage(
1126 "The boundary condition specified is not supported."));
1127 }
1128 }
1129
1130 this->mg_setup_timer.leave_subsection("Set boundary conditions");
1131
1132 // Create mg operators for each level and additional operators needed only
1133 // for local smoothing
1134 for (unsigned int level = this->minlevel; level <= this->maxlevel;
1135 ++level)
1136 {
1137 const unsigned int p_level = p_map[levels[level].second];
1138
1139 level_constraints[level].clear();
1140
1141 const auto &level_dof_handler = this->dof_handlers[p_level];
1142
1143 const IndexSet owned_dofs =
1144 level_dof_handler.locally_owned_mg_dofs(levels[level].first);
1145 const IndexSet relevant_dofs =
1146 DoFTools::extract_locally_relevant_level_dofs(level_dof_handler,
1147 levels[level].first);
1148
1149 level_constraints[level].reinit(owned_dofs, relevant_dofs);
1150
1151 if (this->simulation_parameters.boundary_conditions
1152 .fix_pressure_constant &&
1153 level == this->minlevel)
1154 {
1155 types::global_dof_index min_index = numbers::invalid_unsigned_int;
1156
1157 std::vector<types::global_dof_index> dof_indices;
1158
1159 // Loop over the cells to identify the min index
1160 for (const auto &cell :
1161 level_dof_handler.mg_cell_iterators_on_level(this->minlevel))
1162 {
1163 if (cell->is_locally_owned_on_level())
1164 {
1165 const auto &fe = cell->get_fe();
1166
1167 dof_indices.resize(fe.n_dofs_per_cell());
1168 cell->get_mg_dof_indices(dof_indices);
1169
1170 for (unsigned int i = 0; i < dof_indices.size(); ++i)
1171 if (fe.system_to_component_index(i).first == dim)
1172 min_index = std::min(min_index, dof_indices[i]);
1173 }
1174 }
1175
1176 // Necessary to find the min across all cores.
1177 min_index =
1178 Utilities::MPI::min(min_index,
1179 level_dof_handler.get_mpi_communicator());
1180
1181 AffineConstraints<double> temp_constraints;
1182 temp_constraints.reinit(owned_dofs, relevant_dofs);
1183 if (relevant_dofs.is_element(min_index))
1184 temp_constraints.add_line(min_index);
1185
1186 this->mg_constrained_dofs[p_level].add_user_constraints(
1187 levels[level].first, temp_constraints);
1188 }
1189
1190 // If there are solid regions, establish them as part of the
1191 // multigrid by registering them as user constraints on
1192 // mg_constrained_dofs (same pattern as the pressure-pin and slip
1193 // BC handling above). This is required for LSMG: the smoother and
1194 // transfer read Dirichlet info from mg_constrained_dofs, not from
1195 // level_constraints. The subsequent merge_constraints call (with
1196 // add_user_constraints = true) will then propagate them into
1197 // level_constraints[level] for the operator.
1198 if (simulation_parameters.physical_properties_manager
1199 .get_number_of_solids() > 0)
1200 {
1201 AffineConstraints<double> solid_constraints;
1202 solid_constraints.reinit(owned_dofs, relevant_dofs);
1203 establish_solid_domain_lsmg(level_dof_handler,
1204 levels[level].first,
1205 owned_dofs,
1206 false,
1207 solid_constraints);
1208 solid_constraints.close();
1209 this->mg_constrained_dofs[p_level].add_user_constraints(
1210 levels[level].first, solid_constraints);
1211 }
1212
1213 this->mg_constrained_dofs[p_level].merge_constraints(
1214 level_constraints[level],
1215 levels[level].first,
1216 true,
1217 false,
1218 true,
1219 true);
1220
1221 this->mg_setup_timer.enter_subsection("Set up operators");
1222
1223 // Provide appropriate quadrature depending on the type of elements of
1224 // the level
1225 Quadrature<dim> quadrature_mg =
1226 QGauss<dim>(level_dof_handler.get_fe().degree + 1);
1227 if (this->simulation_parameters.linear_solver
1229 .mg_use_fe_q_iso_q1 &&
1230 level == this->minlevel)
1231 {
1232 AssertThrow(
1233 mg_coarsening_type ==
1235 ExcNotImplemented());
1236
1237 const auto points =
1238 QGaussLobatto<1>(level_dof_handler.get_fe().degree + 1)
1239 .get_points();
1240
1241 quadrature_mg = QIterated<dim>(QGauss<1>(2), points);
1242 }
1243
1244 this->create_level_operator(level);
1245
1246 this->mg_operators[level]->reinit(
1247 *mapping,
1248 (this->simulation_parameters.linear_solver
1250 .mg_use_fe_q_iso_q1 &&
1251 level == this->minlevel) ?
1252 this->dof_handler_fe_q_iso_q1 :
1253 level_dof_handler,
1254 level_constraints[level],
1255 quadrature_mg,
1256 forcing_function,
1257 physical_properties_manager,
1258 this->simulation_parameters.stabilization.stabilization,
1259 levels[level].first,
1260 simulation_control,
1261 this->simulation_parameters.boundary_conditions,
1262 this->simulation_parameters.linear_solver
1264 .mg_enable_hessians_jacobian,
1265 true,
1266 this->simulation_parameters.mortar_parameters.enable);
1267
1268 this->ls_mg_operators[level].initialize(*(this->mg_operators)[level]);
1269 this->ls_mg_interface_in[level].initialize(
1270 *(this->mg_operators)[level]);
1271
1272 partitioners[level] =
1273 this->mg_operators[level]->get_vector_partitioner();
1274
1275 this->mg_setup_timer.leave_subsection("Set up operators");
1276 }
1277
1278 // Create transfer operators
1279 this->mg_setup_timer.enter_subsection("Create transfer operator");
1280
1281 this->mg_transfer_ls = std::make_shared<LSTransferType>(min_h_level);
1282
1283 if (p_map.size() > 1)
1284 {
1285 this->transfers.resize(0, p_map.size() - 1);
1286
1287 for (unsigned int level = transfers.min_level();
1288 level < transfers.max_level();
1289 ++level)
1290 this->transfers[level + 1].reinit(this->dof_handlers[level + 1],
1291 this->dof_handlers[level],
1292 level_constraints[level + 1],
1293 level_constraints[level],
1294 min_h_level,
1295 min_h_level);
1296 }
1297
1298 this->mg_transfer_ls->build(
1299 this->transfers,
1300 this->dof_handlers[this->dof_handlers.max_level()],
1301 this->mg_constrained_dofs[this->mg_constrained_dofs.max_level()],
1302 partitioners);
1303
1304 this->mg_setup_timer.leave_subsection("Create transfer operator");
1305 }
1306 else if (this->simulation_parameters.linear_solver
1308 .preconditioner ==
1310 {
1311 // Create triangulations
1312 this->mg_setup_timer.enter_subsection("Create level triangulations");
1314 MGTransferGlobalCoarseningTools::create_geometric_coarsening_sequence(
1315 this->dof_handler.get_triangulation());
1316 this->mg_setup_timer.leave_subsection("Create level triangulations");
1317
1318 // Modify the triangulations if multigrid number of levels or minimum
1319 // number of cells in level are specified
1320 std::vector<std::shared_ptr<const Triangulation<dim>>> temp;
1321
1322 int mg_min_level =
1323 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1324 .mg_min_level;
1325
1326 int mg_int_level =
1327 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1328 .mg_int_level;
1329
1330 AssertThrow(
1331 (mg_min_level + 1) <=
1332 static_cast<int>(this->coarse_grid_triangulations.size()),
1333 ExcMessage(
1334 "The mg min level specified is higher than the finest mg level."));
1335
1336 int mg_level_min_cells =
1337 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1338 .mg_level_min_cells;
1339
1340 AssertThrow(
1341 mg_level_min_cells <=
1342 static_cast<int>(this
1344 [this->coarse_grid_triangulations.size() - 1]
1345 ->n_global_active_cells()),
1346 ExcMessage(
1347 "The mg level min cells specified are larger than the cells of the finest mg level."));
1348
1349 // find first relevant coarse-grid triangulation
1350 auto ptr = std::find_if(
1351 this->coarse_grid_triangulations.begin(),
1352 this->coarse_grid_triangulations.end() - 1,
1353 [&mg_min_level, &mg_level_min_cells](const auto &tria) {
1354 if (mg_min_level != -1) // minimum number of levels
1355 {
1356 if ((mg_min_level + 1) <=
1357 static_cast<int>(tria->n_global_levels()))
1358 return true;
1359 }
1360 else if (mg_level_min_cells != -1) // minimum number of cells
1361 {
1362 if (static_cast<int>(tria->n_global_active_cells()) >=
1363 mg_level_min_cells)
1364 return true;
1365 }
1366 else
1367 {
1368 return true;
1369 }
1370 return false;
1371 });
1372
1373 // consider all triangulations from that one
1374 while (ptr != this->coarse_grid_triangulations.end())
1375 temp.push_back(*(ptr++));
1376
1377 this->coarse_grid_triangulations = temp;
1378
1379 // p-multigrid
1380 const auto mg_coarsening_type =
1381 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1382 .mg_coarsening_type;
1383
1384 auto polynomial_coarsening_sequence =
1385 MGTransferGlobalCoarseningTools::create_polynomial_coarsening_sequence(
1386 this->dof_handler.get_fe().degree,
1387 this->simulation_parameters.linear_solver
1389 .mg_p_coarsening_type);
1390
1391 // Remove terms of polynomial coarsening sequence that are below the
1392 // minimum coarsening degree
1393 for (unsigned int p = 1; p < this->simulation_parameters.linear_solver
1395 .mg_p_min_coarsening_degree;
1396 p++)
1397 std::erase(polynomial_coarsening_sequence, p);
1398
1399 std::vector<std::pair<unsigned int, unsigned int>> levels;
1400
1401 if (mg_coarsening_type ==
1403 {
1404 // p
1405 for (const auto i : polynomial_coarsening_sequence)
1406 levels.emplace_back(0, i);
1407
1408 // h
1409 for (unsigned int i = 1; i < this->coarse_grid_triangulations.size();
1410 ++i)
1411 levels.emplace_back(i, polynomial_coarsening_sequence.back());
1412 }
1413 else if (mg_coarsening_type ==
1415 {
1416 // h
1417 for (unsigned int i = 0;
1418 i < this->coarse_grid_triangulations.size() - 1;
1419 ++i)
1420 levels.emplace_back(i, polynomial_coarsening_sequence.front());
1421
1422 // p
1423 for (const auto i : polynomial_coarsening_sequence)
1424 levels.emplace_back(this->coarse_grid_triangulations.size() - 1, i);
1425 }
1426 else if (mg_coarsening_type ==
1428 {
1429 // p
1430 for (const auto i : polynomial_coarsening_sequence)
1431 levels.emplace_back(this->coarse_grid_triangulations.size() - 1, i);
1432 }
1433 else if (mg_coarsening_type ==
1435 {
1436 // h
1437 for (unsigned int i = 0; i < this->coarse_grid_triangulations.size();
1438 ++i)
1439 levels.emplace_back(i, polynomial_coarsening_sequence.back());
1440 }
1441 else
1442 {
1443 AssertThrow(false, ExcNotImplemented());
1444 }
1445
1446 // Define maximum and minimum level according to triangulations
1447 this->minlevel = 0;
1448 this->maxlevel = levels.size() - 1;
1449
1450 this->intlevel = (mg_int_level == -1) ? this->minlevel : mg_int_level;
1451
1452 // Local object for constraints of the different levels
1453 MGLevelObject<AffineConstraints<MGNumber>> constraints;
1454
1455 // Resize all multilevel objects according to level
1456 this->mg_operators.resize(this->minlevel, this->maxlevel);
1457 constraints.resize(this->minlevel, this->maxlevel);
1458 this->transfers.resize(this->minlevel, this->maxlevel);
1459
1460 // Distribute DoFs for each level
1461 this->mg_setup_timer.enter_subsection(
1462 "Create DoFHandlers and distribute DoFs");
1463 this->dof_handlers.resize(this->minlevel, this->maxlevel);
1464
1465 for (unsigned int l = this->minlevel; l <= this->maxlevel; ++l)
1466 {
1467 this->dof_handlers[l].reinit(
1468 *this->coarse_grid_triangulations[levels[l].first]);
1469
1470 // To use elements with linear interpolation for coarse-grid we need
1471 // to create the min level dof handler with the appropriate element
1472 // type
1473 if (this->simulation_parameters.linear_solver
1475 .mg_use_fe_q_iso_q1 &&
1476 l == this->minlevel)
1477 {
1478 AssertThrow(
1479 mg_coarsening_type ==
1481 ExcNotImplemented());
1482
1483 const auto points =
1484 QGaussLobatto<1>(levels[l].second + 1).get_points();
1485
1486 this->dof_handlers[l].distribute_dofs(
1487 FESystem<dim>(FE_Q_iso_Q1<dim>(points), dim + 1));
1488 DoFRenumbering::Cuthill_McKee(this->dof_handlers[l]);
1489 }
1490 else
1491 {
1492 this->dof_handlers[l].distribute_dofs(
1493 FESystem<dim>(FE_Q<dim>(levels[l].second), dim + 1));
1494 DoFRenumbering::Cuthill_McKee(this->dof_handlers[l]);
1495 }
1496 }
1497
1498 this->mg_setup_timer.leave_subsection(
1499 "Create DoFHandlers and distribute DoFs");
1500
1501 if (this->simulation_parameters.linear_solver
1503 .mg_verbosity != Parameters::Verbosity::quiet)
1504 {
1505 this->pcout << std::endl;
1506 this->pcout << " -Levels of MG preconditioner:" << std::endl;
1507 for (unsigned int level = this->minlevel; level <= this->maxlevel;
1508 ++level)
1509 this->pcout << " Level " << level << ": "
1510 << this->dof_handlers[level].n_dofs() << " DoFs, "
1511 << this->coarse_grid_triangulations[levels[level].first]
1512 ->n_global_active_cells()
1513 << " cells" << std::endl;
1514 this->pcout << std::endl;
1515 }
1516
1517 if (this->simulation_parameters.linear_solver
1519 .mg_verbosity == Parameters::Verbosity::extra_verbose)
1520 {
1521 this->pcout << " -MG vertical communication efficiency: "
1522 << MGTools::vertical_communication_efficiency(
1524 << std::endl;
1525
1526 this->pcout << " -MG workload imbalance: "
1527 << MGTools::workload_imbalance(
1529 << std::endl;
1530 }
1531
1532 // Create mappings for each level
1533 if (this->simulation_parameters.mortar_parameters.enable)
1534 this->mappings.resize(this->minlevel, this->maxlevel);
1535
1536 // Apply constraints and create mg operators for each level
1537 for (unsigned int level = this->minlevel; level <= this->maxlevel;
1538 ++level)
1539 {
1540 this->mg_setup_timer.enter_subsection("Create level mappings");
1541 std::shared_ptr<Mapping<dim>> level_mapping;
1542
1543 // If mortar is enabled, one MappingQCache<dim> object is created for
1544 // each level to access correct mapping information. Otherwise, the
1545 // MappingQ<dim> object passed to the current function can be used in
1546 // all levels
1547 if (this->simulation_parameters.mortar_parameters.enable)
1548 {
1549 this->mappings[level] = std::make_shared<MappingQCache<dim>>(
1550 this->dof_handlers[level].get_fe().degree);
1551
1552 // Rotate mapping only in the case of a circular mortar interface
1553 if (this->simulation_parameters.mortar_parameters
1554 .interface_type ==
1557 this->dof_handlers[level],
1558 *this->mappings[level],
1559 *mapping,
1560 this->simulation_parameters.mortar_parameters
1561 .rotor_rotation_angle->value(Point<dim>()),
1562 this->simulation_parameters.mortar_parameters
1563 .center_of_rotation,
1564 this->simulation_parameters.mortar_parameters
1565 .rotation_axis_direction);
1566 else if (this->simulation_parameters.mortar_parameters
1567 .interface_type ==
1569 this->mappings[level]->initialize(
1570 *mapping, this->dof_handlers[level].get_triangulation());
1571 else
1572 AssertThrow(false,
1573 ExcMessage("Invalid mortar interface type."));
1574
1575 level_mapping = this->mappings[level];
1576 }
1577 else
1578 level_mapping = mapping;
1579
1580 this->mg_setup_timer.leave_subsection("Create level mappings");
1581
1582 const auto &level_dof_handler = this->dof_handlers[level];
1583 auto &level_constraint = constraints[level];
1584
1585 this->mg_setup_timer.enter_subsection("Set boundary conditions");
1586
1587 level_constraint.clear();
1588
1589 const IndexSet locally_relevant_dofs =
1590 DoFTools::extract_locally_relevant_dofs(level_dof_handler);
1591
1592 level_constraint.reinit(level_dof_handler.locally_owned_dofs(),
1593 locally_relevant_dofs);
1594
1595 DoFTools::make_hanging_node_constraints(level_dof_handler,
1596 level_constraint);
1597
1598 FEValuesExtractors::Vector velocities(0);
1599 FEValuesExtractors::Scalar pressure(dim);
1600
1601 for (auto const &[id, type] :
1602 this->simulation_parameters.boundary_conditions.type)
1603 {
1605 {
1606 std::set<types::boundary_id> no_normal_flux_boundaries;
1607 no_normal_flux_boundaries.insert(id);
1608
1609 VectorTools::compute_no_normal_flux_constraints(
1610 level_dof_handler,
1611 0,
1612 no_normal_flux_boundaries,
1613 level_constraint,
1614 *level_mapping,
1616 }
1617 else if (type ==
1619 {
1620 const auto &periodic_boundary =
1621 this->simulation_parameters.boundary_conditions
1622 .periodic_boundaries.at(id);
1623 DoFTools::make_periodicity_constraints(
1624 level_dof_handler,
1625 id,
1626 periodic_boundary.neighbor_id,
1627 periodic_boundary.direction,
1628 level_constraint);
1629 }
1631 periodic_neighbor)
1632 {
1633 /*already taken into account in the previous else if*/
1634 }
1635 else if (type ==
1637 {
1638 /*The pressure boundary condition is implemented in
1639 * the operators*/
1640 }
1642 function_weak)
1643 {
1644 /*The function weak boundary condition is implemented in
1645 * the operators*/
1646 }
1648 partial_slip)
1649 {
1650 AssertThrow(
1651 false,
1652 ExcMessage(
1653 "Partial slip boundary conditions are not supported by the matrix free application."));
1654 }
1655 else if (type ==
1657 {
1658 /*The directional do-nothing boundary condition is implemented
1659 * in the operators*/
1660 }
1662 neumann_traction)
1663 {
1664 /*The Neumann traction boundary condition is implemented in
1665 * the operators*/
1666 }
1667 else if (
1670 {
1671 VectorTools::interpolate_boundary_values(
1672 *level_mapping,
1673 level_dof_handler,
1674 id,
1675 dealii::Functions::ZeroFunction<dim, MGNumber>(dim + 1),
1676 level_constraint,
1677 fe->component_mask(velocities));
1678 }
1679 else if (type ==
1681 {
1682 /*Default boundary condition*/
1683 }
1684 else
1685 {
1686 AssertThrow(
1687 false,
1688 ExcMessage(
1689 "The boundary condition specified is not supported."));
1690 }
1691 }
1692
1693 if (this->simulation_parameters.boundary_conditions
1694 .fix_pressure_constant &&
1695 level == this->minlevel)
1696 {
1697 types::global_dof_index min_index = numbers::invalid_unsigned_int;
1698
1699 std::vector<types::global_dof_index> dof_indices;
1700
1701 // Loop over the cells to identify the min index
1702 for (const auto &cell : level_dof_handler.active_cell_iterators())
1703 {
1704 if (cell->is_locally_owned())
1705 {
1706 const auto &fe = cell->get_fe();
1707
1708 dof_indices.resize(fe.n_dofs_per_cell());
1709 cell->get_dof_indices(dof_indices);
1710
1711 for (unsigned int i = 0; i < dof_indices.size(); ++i)
1712 if (fe.system_to_component_index(i).first == dim)
1713 min_index = std::min(min_index, dof_indices[i]);
1714 }
1715 }
1716
1717 // Necessary to find the min across all cores.
1718 min_index =
1719 Utilities::MPI::min(min_index,
1720 this->dof_handler.get_mpi_communicator());
1721
1722 if (locally_relevant_dofs.is_element(min_index))
1723 level_constraint.add_line(min_index);
1724 }
1725
1726 // If there are solid regions. Also establish them as part of the
1727 // multigrid
1728 if (simulation_parameters.physical_properties_manager
1729 .get_number_of_solids() > 0)
1730 establish_solid_domain(level_dof_handler,
1731 level_dof_handler.locally_owned_dofs(),
1732 false,
1733 level_constraint);
1734
1735 // constraints
1736 level_constraint.close();
1737
1738 this->mg_setup_timer.leave_subsection("Set boundary conditions");
1739
1740 this->mg_setup_timer.enter_subsection("Set up operators");
1741
1742 // Provide appropriate quadrature depending on the type of elements of
1743 // the level
1744 Quadrature<dim> quadrature_mg = QGauss<dim>(levels[level].second + 1);
1745 if (this->simulation_parameters.linear_solver
1747 .mg_use_fe_q_iso_q1 &&
1748 level == this->minlevel)
1749 {
1750 AssertThrow(
1751 mg_coarsening_type ==
1753 ExcNotImplemented());
1754
1755 const auto points =
1756 QGaussLobatto<1>(level_dof_handler.get_fe().degree + 1)
1757 .get_points();
1758
1759 quadrature_mg = QIterated<dim>(QGauss<1>(2), points);
1760 }
1761
1762 this->create_level_operator(level);
1763
1764 // On the coarsest level, if the level is discretized with Q1
1765 // elements, skip the mortar coupling and assemble the operator as if
1766 // mortar was not enabled. The mortar contribution is therefore only
1767 // neglected on this particular level.
1768 const bool coarsest_level_uses_q1_interpolation =
1769 (level == this->minlevel) &&
1770 (levels[level].second == 1 ||
1771 this->simulation_parameters.linear_solver
1773 .mg_use_fe_q_iso_q1);
1774
1775 const bool enable_mortar_on_level =
1776 this->simulation_parameters.mortar_parameters.enable &&
1777 !coarsest_level_uses_q1_interpolation;
1778
1779 this->mg_operators[level]->reinit(
1780 *level_mapping,
1781 level_dof_handler,
1782 level_constraint,
1783 quadrature_mg,
1784 forcing_function,
1785 physical_properties_manager,
1786 this->simulation_parameters.stabilization.stabilization,
1787 numbers::invalid_unsigned_int,
1788 simulation_control,
1789 this->simulation_parameters.boundary_conditions,
1790 this->simulation_parameters.linear_solver
1792 .mg_enable_hessians_jacobian,
1793 true,
1794 enable_mortar_on_level);
1795
1796 this->mg_setup_timer.leave_subsection("Set up operators");
1797
1798 // If enabled, create mortar operators
1799 if (enable_mortar_on_level)
1800 {
1801 this->mg_setup_timer.enter_subsection("Set up mortar operators");
1802
1803 // Manager
1804 if (this->simulation_parameters.mortar_parameters
1805 .interface_type ==
1807 this->mg_operators[level]->mortar_manager_mf =
1808 std::make_shared<MortarManagerCircle<dim>>(
1809 quadrature_mg,
1810 *level_mapping,
1811 level_dof_handler,
1812 this->simulation_parameters.mortar_parameters);
1813 else if (this->simulation_parameters.mortar_parameters
1814 .interface_type ==
1816 this->mg_operators[level]->mortar_manager_mf =
1817 std::make_shared<MortarManagerLinear<dim>>(
1818 quadrature_mg,
1819 *level_mapping,
1820 level_dof_handler,
1821 this->simulation_parameters.mortar_parameters);
1822 else
1823 AssertThrow(false,
1824 ExcMessage("Invalid mortar interface type."));
1825
1826 // Coupling evaluator
1827 this->mg_operators[level]->mortar_coupling_evaluator_mf =
1828 std::make_shared<
1830 *level_mapping,
1831 level_dof_handler,
1832 physical_properties_manager->get_rheology()
1833 ->get_kinematic_viscosity());
1834
1835 // Coupling operator
1836 this->mg_operators[level]->mortar_coupling_operator_mf =
1837 std::make_shared<CouplingOperator<dim, double>>(
1838 *level_mapping,
1839 level_dof_handler,
1840 level_constraint,
1841 this->mg_operators[level]->mortar_coupling_evaluator_mf,
1842 this->mg_operators[level]->mortar_manager_mf,
1843 this->simulation_parameters.mortar_parameters
1844 .rotor_boundary_id,
1845 this->simulation_parameters.mortar_parameters
1846 .stator_boundary_id,
1847 this->simulation_parameters.mortar_parameters.sip_factor);
1848
1849 this->mg_setup_timer.leave_subsection("Set up mortar operators");
1850 }
1851 }
1852
1853 // Create transfer operators
1854 this->mg_setup_timer.enter_subsection("Create transfer operator");
1855
1856 for (unsigned int level = this->minlevel; level < this->maxlevel; ++level)
1857 this->transfers[level + 1].reinit(this->dof_handlers[level + 1],
1858 this->dof_handlers[level],
1859 constraints[level + 1],
1860 constraints[level]);
1861
1862 this->mg_transfer_gc = std::make_shared<GCTransferType>(
1863 this->transfers, [&](const auto l, auto &vec) {
1864 this->mg_operators[l]->initialize_dof_vector(vec);
1865 });
1866
1867 this->mg_setup_timer.leave_subsection("Create transfer operator");
1868 }
1869 else
1870 AssertThrow(false, ExcNotImplemented());
1871
1872 mg_smoother_preconditioners.resize(this->minlevel, this->maxlevel);
1873}
1874
1875template <int dim>
1876void
1878{
1879 // Create smoother, fill parameters for each level and intialize it
1880 this->mg_setup_timer.enter_subsection("Set up and initialize smoother");
1881
1882 const auto &linear_solver_parameters =
1883 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics);
1884
1885 const auto smoother_preconditioner_type =
1886 linear_solver_parameters.mg_smoother_preconditioner_type;
1887
1888 const bool use_chebyshev =
1889 smoother_preconditioner_type ==
1891
1892 // Build the per-level inner preconditioner. The inverse diagonal is used both
1893 // for the InverseDiagonal relaxation smoother and as the inner preconditioner
1894 // of the Chebyshev smoother.
1895 for (unsigned int level = this->minlevel; level <= this->maxlevel; ++level)
1896 {
1897 if (smoother_preconditioner_type ==
1899 inverse_diagonal ||
1900 use_chebyshev)
1901 {
1902 MGVectorType diagonal_vector;
1903 this->mg_operators[level]->compute_inverse_diagonal(diagonal_vector);
1905 std::make_shared<MyDiagonalMatrix<MGVectorType>>(diagonal_vector);
1906 }
1907 else if (smoother_preconditioner_type ==
1909 additive_schwarz_method)
1910 {
1911 if (mg_smoother_preconditioners[level] == nullptr)
1913 std::make_shared<PreconditionASM<MGVectorType>>();
1914
1915 dynamic_cast<PreconditionASM<MGVectorType> *>(
1916 mg_smoother_preconditioners[level].get())
1917 ->initialize(this->mg_operators[level]
1918 ->get_system_matrix_free()
1919 .get_dof_handler(),
1920 this->mg_operators[level]->get_system_matrix(),
1921 this->mg_operators[level]->get_sparsity_pattern(),
1922 this->mg_operators[level]
1923 ->get_system_matrix_free()
1924 .get_affine_constraints(),
1925 this->mg_operators[level]->get_vector_partitioner(),
1926 this->mg_operators[level]
1927 ->get_system_matrix_free()
1928 .get_mg_level());
1929 }
1930 }
1931
1932 if (use_chebyshev)
1933 {
1934 // Chebyshev-accelerated inverse-diagonal smoother. A single smoothing
1935 // step of a degree-d polynomial is applied (the polynomial degree is the
1936 // main knob). The maximum eigenvalue is estimated internally, so no
1937 // manual power iteration is required.
1938 auto smoother =
1939 std::make_shared<MGSmootherPrecondition<OperatorType,
1941 MGVectorType>>();
1942
1943 MGLevelObject<typename ChebyshevSmootherType::AdditionalData>
1944 smoother_data(this->minlevel, this->maxlevel);
1945
1946 for (unsigned int level = this->minlevel; level <= this->maxlevel;
1947 ++level)
1948 {
1949 smoother_data[level].preconditioner =
1951 smoother_data[level].degree =
1952 linear_solver_parameters.mg_smoother_chebyshev_degree;
1953 smoother_data[level].smoothing_range =
1954 linear_solver_parameters.mg_smoother_chebyshev_smoothing_range;
1955 smoother_data[level].eig_cg_n_iterations =
1956 linear_solver_parameters.mg_smoother_chebyshev_eig_cg_n_iterations;
1957 smoother_data[level].constraints.copy_from(
1958 this->mg_operators[level]
1959 ->get_system_matrix_free()
1960 .get_affine_constraints());
1961 }
1962
1963 smoother->initialize(this->mg_operators, smoother_data);
1964
1965 if (linear_solver_parameters.eig_estimation_verbose !=
1967 {
1968 // Print eigenvalue estimation for all levels
1969 for (unsigned int level = this->minlevel; level <= this->maxlevel;
1970 ++level)
1971 {
1972 MGVectorType vec;
1973 this->mg_operators[level]->initialize_dof_vector(vec);
1974 const auto evs =
1975 smoother->smoothers[level].estimate_eigenvalues(vec);
1976
1977 this->pcout << std::endl;
1978 this->pcout << " -Eigenvalue estimation level "
1979 << level - this->minlevel << ":" << std::endl;
1980 this->pcout << " Minimum eigenvalue: "
1981 << evs.min_eigenvalue_estimate << std::endl;
1982 this->pcout << " Maximum eigenvalue: "
1983 << evs.max_eigenvalue_estimate << std::endl;
1984 this->pcout << std::endl;
1985 }
1986 }
1987
1988 this->mg_smoother = smoother;
1989 }
1990 else
1991 {
1992 auto smoother = std::make_shared<
1993 MGSmootherPrecondition<OperatorType, SmootherType, MGVectorType>>();
1994
1995 MGLevelObject<typename SmootherType::AdditionalData> smoother_data(
1996 this->minlevel, this->maxlevel);
1997
1998 for (unsigned int level = this->minlevel; level <= this->maxlevel;
1999 ++level)
2000 {
2001 smoother_data[level].preconditioner =
2003
2004 smoother_data[level].n_iterations =
2005 linear_solver_parameters.mg_smoother_iterations;
2006
2007 if (linear_solver_parameters.mg_smoother_eig_estimation)
2008 {
2009 // Set relaxation to zero so that eigenvalues are estimated
2010 // internally
2011 smoother_data[level].relaxation = 0.0;
2012 smoother_data[level].smoothing_range =
2013 linear_solver_parameters.eig_estimation_smoothing_range;
2014 smoother_data[level].eig_cg_n_iterations =
2015 linear_solver_parameters.eig_estimation_cg_n_iterations;
2016 smoother_data[level].eigenvalue_algorithm = SmootherType::
2017 AdditionalData::EigenvalueAlgorithm::power_iteration;
2018 smoother_data[level].constraints.copy_from(
2019 this->mg_operators[level]
2020 ->get_system_matrix_free()
2021 .get_affine_constraints());
2022 }
2023 else
2024 smoother_data[level].relaxation =
2025 linear_solver_parameters.mg_smoother_relaxation;
2026 }
2027
2028 smoother->initialize(this->mg_operators, smoother_data);
2029
2030 if (linear_solver_parameters.mg_smoother_eig_estimation &&
2031 linear_solver_parameters.eig_estimation_verbose !=
2033 {
2034 // Print eigenvalue estimation for all levels
2035 for (unsigned int level = this->minlevel; level <= this->maxlevel;
2036 ++level)
2037 {
2038 MGVectorType vec;
2039 this->mg_operators[level]->initialize_dof_vector(vec);
2040 const auto evs =
2041 smoother->smoothers[level].estimate_eigenvalues(vec);
2042
2043 this->pcout << std::endl;
2044 this->pcout << " -Eigenvalue estimation level "
2045 << level - this->minlevel << ":" << std::endl;
2046 this->pcout << " Relaxation parameter: "
2047 << smoother->smoothers[level].get_relaxation()
2048 << std::endl;
2049 this->pcout << " Minimum eigenvalue: "
2050 << evs.min_eigenvalue_estimate << std::endl;
2051 this->pcout << " Maximum eigenvalue: "
2052 << evs.max_eigenvalue_estimate << std::endl;
2053 this->pcout << std::endl;
2054 }
2055 }
2056
2057 this->mg_smoother = smoother;
2058 }
2059
2060 this->mg_setup_timer.leave_subsection("Set up and initialize smoother");
2061
2062 // Create coarse-grid GMRES solver and AMG preconditioner
2063 this->mg_setup_timer.enter_subsection("Create coarse-grid solver");
2064
2065 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2066 .mg_coarse_grid_solver ==
2068 {
2069 const int max_iterations =
2070 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2071 .mg_gmres_max_iterations;
2072 const double tolerance =
2073 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2074 .mg_gmres_tolerance;
2075 const double reduce =
2076 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2077 .mg_gmres_reduce;
2078 this->coarse_grid_solver_control = std::make_shared<ReductionControl>(
2079 max_iterations, tolerance, reduce, false, false);
2080 SolverGMRES<TrilinosVectorType>::AdditionalData solver_parameters;
2081 solver_parameters.max_n_tmp_vectors =
2082 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2083 .mg_gmres_max_krylov_vectors;
2084
2085 this->coarse_grid_solver =
2086 std::make_shared<SolverGMRES<TrilinosVectorType>>(
2087 *this->coarse_grid_solver_control, solver_parameters);
2088
2089 if (this->simulation_parameters.linear_solver
2091 .mg_gmres_preconditioner ==
2093 {
2094 setup_AMG();
2095
2096 this->mg_coarse = std::make_shared<MGCoarseGridIterativeSolver<
2098 SolverGMRES<TrilinosVectorType>,
2099 TrilinosWrappers::SparseMatrix,
2101 *this->coarse_grid_solver,
2102 this->mg_operators[this->minlevel]->get_system_matrix(),
2104 }
2105 else if (this->simulation_parameters.linear_solver
2107 .mg_gmres_preconditioner ==
2109 {
2110 setup_ILU();
2111
2112 this->mg_coarse = std::make_shared<MGCoarseGridIterativeSolver<
2114 SolverGMRES<TrilinosVectorType>,
2115 TrilinosWrappers::SparseMatrix,
2117 *this->coarse_grid_solver,
2118 this->mg_operators[this->minlevel]->get_system_matrix(),
2120 }
2121 }
2122 else if (this->simulation_parameters.linear_solver
2124 .mg_coarse_grid_solver ==
2126 {
2127 setup_AMG();
2128
2129 this->mg_coarse = std::make_shared<CoarseGridSolverApply>(
2131 }
2132 else if (this->simulation_parameters.linear_solver
2134 .mg_coarse_grid_solver ==
2136 {
2137 setup_ILU();
2138
2139 this->mg_coarse = std::make_shared<CoarseGridSolverApply>(
2141 }
2142 else if (this->simulation_parameters.linear_solver
2144 .mg_coarse_grid_solver ==
2146 {
2147 TrilinosWrappers::SolverDirect::AdditionalData data;
2148 this->direct_solver_control =
2149 std::make_shared<SolverControl>(100, 1.e-10);
2150
2151 auto precondition_direct =
2152 std::make_shared<TrilinosWrappers::SolverDirect>(
2153 *this->direct_solver_control, data);
2154
2155 precondition_direct->initialize(
2156 this->mg_operators[this->minlevel]->get_system_matrix());
2157
2159 std::make_shared<PreconditionAdapter<MGVectorType, TrilinosVectorType>>(
2160 precondition_direct);
2161
2162 this->mg_coarse = std::make_shared<CoarseGridSolverApply>(
2164 }
2165 else
2166 AssertThrow(
2167 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2168 .mg_coarse_grid_solver ==
2170 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2171 .mg_coarse_grid_solver ==
2173 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2174 .mg_coarse_grid_solver ==
2176 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2177 .mg_coarse_grid_solver ==
2179 ExcMessage(
2180 "This coarse-grid solver is not supported. Supported options are <gmres|amg|ilu|direct>."));
2181
2182 this->mg_setup_timer.leave_subsection("Create coarse-grid solver");
2183
2184 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2186 {
2187 // Create interface matrices needed for local smoothing in case of
2188 // local refinement
2190 std::make_shared<mg::Matrix<MGVectorType>>(this->ls_mg_interface_in);
2191
2192
2193 if (this->minlevel != this->intlevel)
2194 {
2195 // Create main MG object
2196 this->mg_intermediate =
2197 std::make_shared<Multigrid<MGVectorType>>(*this->mg_matrix,
2198 *this->mg_coarse,
2199 *this->mg_transfer_ls,
2200 *this->mg_smoother,
2201 *this->mg_smoother,
2202 this->minlevel,
2203 this->intlevel);
2204
2205 if (this->dof_handler.get_triangulation().has_hanging_nodes())
2206 this->mg_intermediate->set_edge_in_matrix(
2207 *this->mg_interface_matrix_in);
2208
2209 // Create MG preconditioner
2211 std::make_shared<PreconditionMG<dim, MGVectorType, LSTransferType>>(
2212 this->dof_handler, *this->mg_intermediate, *this->mg_transfer_ls);
2213
2214 const int max_iterations = this->simulation_parameters.linear_solver
2216 .mg_gmres_max_iterations;
2217 const double tolerance = this->simulation_parameters.linear_solver
2219 .mg_gmres_tolerance;
2220 const double reduce = this->simulation_parameters.linear_solver
2222 .mg_gmres_reduce;
2223
2225 std::make_shared<ReductionControl>(
2226 max_iterations, tolerance, reduce, false, false);
2227
2229 std::make_shared<SolverGMRES<MGVectorType>>(
2231
2233 std::make_shared<MGCoarseGridIterativeSolver<
2235 SolverGMRES<MGVectorType>,
2237 PreconditionMG<dim, MGVectorType, LSTransferType>>>(
2239 *this->mg_operators[this->intlevel],
2241 }
2242
2243 // Create main MG object
2244 this->mg = std::make_shared<Multigrid<MGVectorType>>(
2245 *this->mg_matrix,
2246 (this->minlevel != this->intlevel) ? (*this->mg_coarse_intermediate) :
2247 (*this->mg_coarse),
2248 *this->mg_transfer_ls,
2249 *this->mg_smoother,
2250 *this->mg_smoother,
2251 this->intlevel,
2252 this->maxlevel);
2253
2254 if (this->dof_handler.get_triangulation().has_hanging_nodes())
2255 this->mg->set_edge_in_matrix(*this->mg_interface_matrix_in);
2256
2257 // Create MG preconditioner
2259 std::make_shared<PreconditionMG<dim, MGVectorType, LSTransferType>>(
2260 this->dof_handler, *this->mg, *this->mg_transfer_ls);
2261 }
2262 else if (this->simulation_parameters.linear_solver
2264 .preconditioner ==
2266 {
2267 if (this->minlevel != this->intlevel)
2268 {
2269 // Create main MG object
2270 this->mg_intermediate =
2271 std::make_shared<Multigrid<MGVectorType>>(*this->mg_matrix,
2272 *this->mg_coarse,
2273 *this->mg_transfer_gc,
2274 *this->mg_smoother,
2275 *this->mg_smoother,
2276 this->minlevel,
2277 this->intlevel);
2278
2279 // Create MG preconditioner
2281 std::make_shared<PreconditionMG<dim, MGVectorType, GCTransferType>>(
2282 this->dof_handler, *this->mg_intermediate, *this->mg_transfer_gc);
2283
2284 const int max_iterations = this->simulation_parameters.linear_solver
2286 .mg_gmres_max_iterations;
2287 const double tolerance = this->simulation_parameters.linear_solver
2289 .mg_gmres_tolerance;
2290 const double reduce = this->simulation_parameters.linear_solver
2292 .mg_gmres_reduce;
2293
2295 std::make_shared<ReductionControl>(
2296 max_iterations, tolerance, reduce, false, false);
2297
2299 std::make_shared<SolverGMRES<MGVectorType>>(
2301
2303 std::make_shared<MGCoarseGridIterativeSolver<
2305 SolverGMRES<MGVectorType>,
2307 PreconditionMG<dim, MGVectorType, GCTransferType>>>(
2309 *this->mg_operators[this->intlevel],
2310 *this->gc_multigrid_preconditioner_intermediate);
2311 }
2312
2313 // Create main MG object
2314 this->mg = std::make_shared<Multigrid<MGVectorType>>(
2315 *this->mg_matrix,
2316 (this->minlevel != this->intlevel) ? (*this->mg_coarse_intermediate) :
2317 (*this->mg_coarse),
2318 *this->mg_transfer_gc,
2319 *this->mg_smoother,
2320 *this->mg_smoother,
2321 this->intlevel,
2322 this->maxlevel,
2323 Multigrid<MGVectorType>::Cycle::v_cycle);
2324
2325 // Create MG preconditioner
2327 std::make_shared<PreconditionMG<dim, MGVectorType, GCTransferType>>(
2328 this->dof_handler, *this->mg, *this->mg_transfer_gc);
2329 }
2330
2331 // Print detailed timings of multigrid vmult
2332 const auto create_mg_timer_function = [&](const std::string &label) {
2333 return [label, this](const bool flag, const unsigned int level) {
2334 const std::string label_full =
2335 (label == "") ?
2336 ("gmg::vmult::level_" + std::to_string(level)) :
2337 ("gmg::vmult::level_" + std::to_string(level) + "::" + label);
2338
2339 if (flag)
2340 this->mg_vmult_timer.enter_subsection(label_full);
2341 else
2342 this->mg_vmult_timer.leave_subsection(label_full);
2343 };
2344 };
2345
2346 this->mg->connect_pre_smoother_step(
2347 create_mg_timer_function("0_pre_smoother_step"));
2348 this->mg->connect_residual_step(create_mg_timer_function("1_residual_step"));
2349 this->mg->connect_restriction(create_mg_timer_function("2_restriction"));
2350 this->mg->connect_coarse_solve(create_mg_timer_function(""));
2351 this->mg->connect_prolongation(create_mg_timer_function("3_prolongation"));
2352 this->mg->connect_edge_prolongation(
2353 create_mg_timer_function("4_edge_prolongation"));
2354 this->mg->connect_post_smoother_step(
2355 create_mg_timer_function("5_post_smoother_step"));
2356
2357 if (mg_intermediate)
2358 {
2359 this->mg_intermediate->connect_pre_smoother_step(
2360 create_mg_timer_function("0_pre_smoother_step"));
2361 this->mg_intermediate->connect_residual_step(
2362 create_mg_timer_function("1_residual_step"));
2363 this->mg_intermediate->connect_restriction(
2364 create_mg_timer_function("2_restriction"));
2365 this->mg_intermediate->connect_coarse_solve(create_mg_timer_function(""));
2366 this->mg_intermediate->connect_prolongation(
2367 create_mg_timer_function("3_prolongation"));
2368 this->mg_intermediate->connect_edge_prolongation(
2369 create_mg_timer_function("4_edge_prolongation"));
2370 this->mg_intermediate->connect_post_smoother_step(
2371 create_mg_timer_function("5_post_smoother_step"));
2372 }
2373
2374 const auto create_mg_precon_timer_function = [&](const std::string &label) {
2375 return [label, this](const bool flag) {
2376 const std::string label_full = "gmg::vmult::" + label;
2377
2378 if (flag)
2379 this->mg_vmult_timer.enter_subsection(label_full);
2380 else
2381 this->mg_vmult_timer.leave_subsection(label_full);
2382 };
2383 };
2384
2385 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2387 {
2388 this->ls_multigrid_preconditioner->connect_transfer_to_mg(
2389 create_mg_precon_timer_function("transfer_to_mg"));
2390 this->ls_multigrid_preconditioner->connect_transfer_to_global(
2391 create_mg_precon_timer_function("transfer_to_global"));
2392 }
2393 else if (this->simulation_parameters.linear_solver
2395 .preconditioner ==
2397 {
2398 this->gc_multigrid_preconditioner->connect_transfer_to_mg(
2399 create_mg_precon_timer_function("transfer_to_mg"));
2400 this->gc_multigrid_preconditioner->connect_transfer_to_global(
2401 create_mg_precon_timer_function("transfer_to_global"));
2402 }
2403}
2404
2405template <int dim>
2406void
2408 const VectorType &src) const
2409{
2411 this->ls_multigrid_preconditioner->vmult(dst, src);
2412 else if (this->gc_multigrid_preconditioner)
2413 this->gc_multigrid_preconditioner->vmult(dst, src);
2414 else
2415 AssertThrow(false, ExcNotImplemented());
2416
2417 // Save number of coarse grid iterations needed in one vmult
2419 this->coarse_grid_iterations.emplace_back(
2420 this->coarse_grid_solver_control->last_step());
2422 this->coarse_grid_iterations.emplace_back(
2423 this->coarse_grid_solver_control_intermediate->last_step());
2424}
2425
2426template <int dim>
2427void
2429{
2430 if (this->coarse_grid_solver_control ||
2432 {
2433 if (this->coarse_grid_iterations.empty())
2434 this->pcout << " -Coarse grid solver took: 0 iterations" << std::endl;
2435 else
2436 {
2437 unsigned int total = this->coarse_grid_iterations[0];
2438 this->pcout << " -Coarse grid solver took: "
2439 << this->coarse_grid_iterations[0];
2440 for (unsigned int i = 1; i < this->coarse_grid_iterations.size(); i++)
2441 {
2442 this->pcout << " + " << this->coarse_grid_iterations[i];
2443 total += this->coarse_grid_iterations[i];
2444 }
2445 this->pcout << " = " << total << " iterations" << std::endl;
2446
2447 this->coarse_grid_iterations.clear();
2448 }
2449 }
2450}
2451
2452template <int dim>
2453const MGLevelObject<std::shared_ptr<NavierStokesOperatorBase<
2454 dim,
2460
2461template <int dim>
2462const MGLevelObject<std::shared_ptr<PreconditionBase<
2468
2469template <int dim>
2470void
2472{
2473 TrilinosWrappers::PreconditionAMG::AdditionalData amg_data;
2474
2475 // Extract matrix of the minlevel to avoid building it twice
2476 const TrilinosWrappers::SparseMatrix &min_level_matrix =
2477 this->mg_operators[this->minlevel]->get_system_matrix();
2478
2479 if (!this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2480 .mg_amg_use_default_parameters)
2481 {
2482 amg_data.elliptic = false;
2483 if (this->dof_handler.get_fe().degree > 1)
2484 amg_data.higher_order_elements = true;
2485 amg_data.n_cycles =
2486 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2487 .amg_n_cycles;
2488 amg_data.w_cycle =
2489 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2490 .amg_w_cycles;
2491 amg_data.aggregation_threshold =
2492 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2493 .amg_aggregation_threshold;
2494 amg_data.smoother_sweeps =
2495 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2496 .amg_smoother_sweeps;
2497 amg_data.smoother_overlap =
2498 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2499 .amg_smoother_overlap;
2500 amg_data.output_details = false;
2501 amg_data.smoother_type = "ILU";
2502 amg_data.coarse_type = "ILU";
2503
2504 std::vector<std::vector<bool>> constant_modes;
2505 ComponentMask components(dim + 1, true);
2506 if (this->simulation_parameters.linear_solver
2508 .preconditioner ==
2510
2511 {
2512 // Constant modes for velocity and pressure
2513 constant_modes = DoFTools::extract_level_constant_modes(
2514 this->mg_operators[this->minlevel]
2515 ->get_system_matrix_free()
2516 .get_mg_level(),
2517 this->mg_operators[this->minlevel]
2518 ->get_system_matrix_free()
2519 .get_dof_handler(),
2520 components);
2521 }
2522 else if (this->simulation_parameters.linear_solver
2524 .preconditioner ==
2526 {
2527 // Constant modes for velocity and pressure
2528 constant_modes =
2529 DoFTools::extract_constant_modes(this->dof_handlers[this->minlevel],
2530 components);
2531 }
2532
2533 amg_data.constant_modes = constant_modes;
2534
2535 Teuchos::ParameterList parameter_ml;
2536 std::unique_ptr<Epetra_MultiVector> distributed_constant_modes;
2537 amg_data.set_parameters(parameter_ml,
2538 distributed_constant_modes,
2539 min_level_matrix);
2540
2541 const double ilu_fill =
2542 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2543 .amg_precond_ilu_fill;
2544 const double ilu_atol =
2545 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2546 .amg_precond_ilu_atol;
2547 const double ilu_rtol =
2548 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2549 .amg_precond_ilu_rtol;
2550 parameter_ml.set("smoother: ifpack level-of-fill", ilu_fill);
2551 parameter_ml.set("smoother: ifpack absolute threshold", ilu_atol);
2552 parameter_ml.set("smoother: ifpack relative threshold", ilu_rtol);
2553
2554 parameter_ml.set("coarse: ifpack level-of-fill", ilu_fill);
2555 parameter_ml.set("coarse: ifpack absolute threshold", ilu_atol);
2556 parameter_ml.set("coarse: ifpack relative threshold", ilu_rtol);
2557
2558 auto precondition_amg =
2559 std::make_shared<TrilinosWrappers::PreconditionAMG>();
2560
2561 precondition_amg->initialize(min_level_matrix, parameter_ml);
2562
2564 std::make_shared<PreconditionAdapter<MGVectorType, TrilinosVectorType>>(
2565 precondition_amg);
2566 }
2567 else
2568 {
2569 auto precondition_amg =
2570 std::make_shared<TrilinosWrappers::PreconditionAMG>();
2571
2573 std::make_shared<PreconditionAdapter<MGVectorType, TrilinosVectorType>>(
2574 precondition_amg);
2575 }
2576}
2577
2578template <int dim>
2579void
2581{
2582 int current_preconditioner_fill_level =
2583 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2584 .ilu_precond_fill;
2585 const double ilu_atol =
2586 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2587 .ilu_precond_atol;
2588 const double ilu_rtol =
2589 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2590 .ilu_precond_rtol;
2591 TrilinosWrappers::PreconditionILU::AdditionalData preconditionerOptions(
2592 current_preconditioner_fill_level, ilu_atol, ilu_rtol, 0);
2593
2594 auto precondition_ilu = std::make_shared<TrilinosWrappers::PreconditionILU>();
2595
2596 precondition_ilu->initialize(
2597 this->mg_operators[this->minlevel]->get_system_matrix(),
2598 preconditionerOptions);
2599
2601 std::make_shared<PreconditionAdapter<MGVectorType, TrilinosVectorType>>(
2602 precondition_ilu);
2603}
2604
2605template <int dim>
2606void
2608 const unsigned int level)
2609{
2610 if (this->simulation_parameters.physical_properties_manager
2611 .is_non_newtonian())
2612 this->mg_operators[level] = std::make_shared<
2614 else
2615 this->mg_operators[level] =
2616 std::make_shared<NavierStokesStabilizedOperator<dim, MGNumber>>();
2617}
2618
2619template <int dim>
2628
2629template <int dim>
2630void
2632 const std::shared_ptr<SimulationControl> &simulation_control,
2633 FlowControl<dim> &flow_control,
2634 const VectorType &present_solution,
2635 const VectorType &time_derivative_previous_solutions)
2636{
2637 const bool transient =
2638 time_stepping_is_bdf(simulation_control->get_assembly_method()) ||
2639 time_stepping_is_sdirk(simulation_control->get_assembly_method());
2640 // Local objects for the different levels
2641 MGLevelObject<MGVectorType> mg_solution(this->minlevel, this->maxlevel);
2642 MGLevelObject<MGVectorType> mg_time_derivative_previous_solutions(
2643 this->minlevel, this->maxlevel);
2644
2645 for (unsigned int level = this->minlevel; level <= this->maxlevel; ++level)
2646 {
2647 this->mg_operators[level]->initialize_dof_vector(mg_solution[level]);
2648 if (transient)
2649 this->mg_operators[level]->initialize_dof_vector(
2650 mg_time_derivative_previous_solutions[level]);
2651 }
2652
2653 this->mg_setup_timer.enter_subsection("Execute relevant transfers");
2654
2655 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2657 {
2658 // Create transfer operator and transfer solution to mg levels
2659
2660 this->mg_transfer_ls->interpolate_to_mg(this->dof_handler,
2661 mg_solution,
2662 present_solution);
2663
2664 if (transient)
2665 this->mg_transfer_ls->interpolate_to_mg(
2666 this->dof_handler,
2667 mg_time_derivative_previous_solutions,
2668 time_derivative_previous_solutions);
2669
2670 this->mg_matrix =
2671 std::make_shared<mg::Matrix<MGVectorType>>(this->ls_mg_operators);
2672 }
2673 else if (this->simulation_parameters.linear_solver
2675 .preconditioner ==
2677 {
2678 this->mg_transfer_gc->interpolate_to_mg(this->dof_handler,
2679 mg_solution,
2680 present_solution);
2681
2682 if (transient)
2683 this->mg_transfer_gc->interpolate_to_mg(
2684 this->dof_handler,
2685 mg_time_derivative_previous_solutions,
2686 time_derivative_previous_solutions);
2687
2688 this->mg_matrix =
2689 std::make_shared<mg::Matrix<MGVectorType>>(this->mg_operators);
2690 }
2691
2692 this->mg_setup_timer.leave_subsection("Execute relevant transfers");
2693
2694 // Evaluate non linear terms for all mg operators
2695 for (unsigned int level = this->minlevel; level <= this->maxlevel; ++level)
2696 {
2697 mg_solution[level].update_ghost_values();
2698
2699 if (this->simulation_parameters.mortar_parameters.enable)
2700 this->mg_operators[level]->evaluate_velocity_ale(
2701 *this->mg_operators[level]
2702 ->get_system_matrix_free()
2703 .get_mapping_info()
2704 .mapping,
2705 this->simulation_parameters.mortar_parameters.center_of_rotation,
2706 this->simulation_parameters.mortar_parameters.rotation_axis_direction,
2707 this->simulation_parameters.mortar_parameters.rotor_angular_velocity);
2708
2709 this->mg_operators[level]->evaluate_non_linear_term_and_calculate_tau(
2710 mg_solution[level]);
2711
2712 if (transient)
2713 {
2714 mg_time_derivative_previous_solutions[level].update_ghost_values();
2715 this->mg_operators[level]
2716 ->evaluate_time_derivative_previous_solutions(
2717 mg_time_derivative_previous_solutions[level]);
2718
2719 if (this->simulation_parameters.flow_control.enable_flow_control)
2720 this->mg_operators[level]->update_beta_force(
2721 flow_control.get_beta());
2722 }
2723 }
2724
2726}
2727
2728template <int dim>
2729void
2731 const DoFHandler<dim> &temperature_dof_handler,
2732 const VectorType &temperature_present_solution)
2733{
2734 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2736 {
2737 AssertThrow(false, ExcNotImplemented());
2738 }
2739 else if (this->simulation_parameters.linear_solver
2741 .preconditioner ==
2743 {
2744 const unsigned int min_level = this->minlevel;
2745 const unsigned int max_level = this->maxlevel;
2746
2747 this->temperature_dof_handlers.resize(min_level, max_level);
2748
2749 for (unsigned int l = min_level; l <= max_level; l++)
2750 {
2751 this->temperature_dof_handlers[l].reinit(
2752 this->dof_handlers[l].get_triangulation());
2753 this->temperature_dof_handlers[l].distribute_dofs(
2754 temperature_dof_handler.get_fe());
2755 DoFRenumbering::Cuthill_McKee(this->temperature_dof_handlers[l]);
2756 }
2757
2758 this->transfers_temperature.resize(min_level, max_level);
2759
2760 for (unsigned int l = min_level; l < max_level; l++)
2761 {
2762 this->transfers_temperature[l + 1].reinit(
2763 this->temperature_dof_handlers[l + 1],
2764 this->temperature_dof_handlers[l],
2765 {},
2766 {});
2767 }
2768
2770 std::make_shared<GCTransferType>(this->transfers_temperature);
2771
2772 this->mg_transfer_gc_temperature->build(
2773 temperature_dof_handler, [&](const auto l, auto &vec) {
2774 vec.reinit(this->temperature_dof_handlers[l].locally_owned_dofs(),
2775 DoFTools::extract_locally_active_dofs(
2776 this->temperature_dof_handlers[l]),
2777 this->temperature_dof_handlers[l].get_mpi_communicator());
2778 });
2779
2780 MGLevelObject<MGVectorType> mg_temperature_solution(this->minlevel,
2781 this->maxlevel);
2782
2783 this->mg_transfer_gc_temperature->interpolate_to_mg(
2784 temperature_dof_handler,
2785 mg_temperature_solution,
2786 temperature_present_solution);
2787
2788 for (unsigned int l = min_level; l <= max_level; l++)
2789 {
2790 mg_temperature_solution[l].update_ghost_values();
2791
2792 this->mg_operators[l]->compute_thermal_buoyancy_term(
2793 mg_temperature_solution[l], this->temperature_dof_handlers[l]);
2794 }
2795 }
2796}
2797
2798template <int dim>
2801 const FluidDynamicsFormulation fluid_dynamics_formulation)
2802 : NavierStokesBase<dim, VectorType, IndexSet>(nsparam,
2803 fluid_dynamics_formulation)
2804{
2805 AssertThrow(
2808 dealii::ExcMessage(
2809 "Matrix free Navier-Stokes does not support different orders for the velocity and the pressure!"));
2810
2811 // The matrix-free operators do not scale the pressure trial functions, so the
2812 // pressure scaling factor is never applied to the Jacobian. The pressure
2813 // degrees of freedom of the Newton correction are nevertheless rescaled at
2814 // the end of the linear solve, which would silently produce a wrong Newton
2815 // correction. The parameter is therefore rejected rather than ignored.
2816 AssertThrow(
2817 std::abs(nsparam.stabilization.pressure_scaling_factor - 1.) < 1e-8,
2818 dealii::ExcMessage(
2819 "Matrix free Navier-Stokes does not support the <pressure scaling factor> parameter of the <stabilization> subsection. Set it to 1, or use the matrix-based solver (lethe-fluid) if the pressure has to be rescaled."));
2820
2821 this->fe = std::make_shared<FESystem<dim>>(
2822 FE_Q<dim>(nsparam.fem_parameters.velocity_degree), dim + 1);
2823
2824 // Initialize solution shared_ptr
2826 std::make_shared<TrilinosWrappers::MPI::Vector>();
2827
2828 physical_properties_manager = std::make_shared<PhysicalPropertiesManager>(
2829 this->simulation_parameters.physical_properties_manager);
2830
2831 if (this->physical_properties_manager->is_non_newtonian())
2832 {
2833 system_operator = std::make_shared<
2835 AssertThrow(
2836 this->simulation_parameters.stabilization.stabilization ==
2838 dealii::ExcMessage(
2839 "Matrix free Non-Newtonian Navier-Stokes only supports SUPG/PSPG stabilization."));
2840 }
2841 else
2843 std::make_shared<NavierStokesStabilizedOperator<dim, double>>();
2844
2845 if (!this->simulation_parameters.source_term.enable)
2846 {
2847 this->forcing_function.reset();
2848 }
2849}
2850
2851template <int dim>
2856
2857template <int dim>
2858void
2860 unsigned int stage,
2862{
2863 SDIRKTable table = sdirk_table(method);
2864 SDIRKStageData stage_data(table, stage + 1);
2865 const double a_ii = stage_data.a_ij[stage];
2866
2867 this->sdirk_vectors.sum_over_previous_stages = 0;
2868
2869 if (stage > 0)
2870 {
2871 // At the first stage, the sum_over_previous_stages is equal to 0
2872 // For the next stages, we have to compute this sum
2873 for (unsigned int p = 0; p < stage; ++p)
2874 {
2875 // sum((a_ij/a_ii) * k_j)
2876 this->sdirk_vectors.sum_over_previous_stages.add(
2877 stage_data.a_ij[p] / a_ii,
2878 this->sdirk_vectors.previous_k_j_solutions[p]);
2879 }
2880 }
2881
2882 this->computing_timer.enter_subsection(
2883 "Calculate time derivative previous solutions");
2884
2886 this->time_derivative_previous_solutions.update_ghost_values();
2887 this->system_operator->evaluate_time_derivative_previous_solutions(
2889
2890 this->computing_timer.leave_subsection(
2891 "Calculate time derivative previous solutions");
2892
2893 if (this->simulation_parameters.flow_control.enable_flow_control)
2894 this->system_operator->update_beta_force(this->flow_control.get_beta());
2895}
2896
2897template <int dim>
2898void
2900 const unsigned int stage,
2902 const double time_step)
2903{
2904 const SDIRKTable table = sdirk_table(method);
2905 const SDIRKStageData stage_data(table, stage + 1);
2906 const double a_ii = stage_data.a_ij[stage];
2907
2908 // Once we have solved the nonlinear system for the velocity, we
2909 // want to store the value of the coefficient k_i for the final
2910 // sum b_i*k_i with k_i = (u*_{i} - u_{n})/(time_step*a_ii) -
2911 // sum_over_previous_stages
2913 this->local_evaluation_point.add(-1.0, (*this->previous_solutions)[0]);
2914 this->local_evaluation_point.add((1.0 / (time_step * a_ii)) - 1,
2916 this->local_evaluation_point.add(
2917 -1.0, this->sdirk_vectors.sum_over_previous_stages);
2918
2919 // We store the value of the present_k_i_solution in the
2920 // previous_k_j_solutions vector.
2921 this->sdirk_vectors.previous_k_j_solutions[stage] =
2923
2924 // We update the sum of b_i*k_i
2925 // The name 'temp' is just used to match the syntax of Matrix Based resolution
2926 const double b_i = stage_data.b_i;
2927 this->sdirk_vectors.local_sum_bi_ki.add(b_i, this->local_evaluation_point);
2928}
2929
2930template <int dim>
2931void
2933{
2934 // At each time iteration, we update the value of present_solution
2935 this->local_evaluation_point = (*this->previous_solutions)[0];
2936 this->local_evaluation_point.add(time_step,
2937 this->sdirk_vectors.local_sum_bi_ki);
2939}
2940
2941template <int dim>
2942void
2944{
2947
2948 // The forcing term is precomputed by the operator, it must be recomputed for
2949 // a time-dependent forcing function to be accounted for.
2950 if (this->forcing_function)
2951 this->system_operator->compute_forcing_term();
2952}
2953
2954template <int dim>
2955void
2957{
2958 this->computing_timer.enter_subsection("Read mesh and manifolds");
2959
2960 if (this->simulation_parameters.mortar_parameters.enable)
2961 {
2962 // Mortar load balancing
2964
2966 *this->triangulation,
2967 this->simulation_parameters.mesh,
2968 this->simulation_parameters.manifolds_parameters,
2969 this->simulation_parameters.restart_parameters.restart,
2970 this->simulation_parameters.boundary_conditions.periodic_boundaries,
2971 this->simulation_parameters.mortar_parameters);
2972 // Create and initialize mapping cache
2973 this->mapping_cache =
2974 std::make_shared<MappingQCache<dim>>(this->velocity_fem_degree);
2975 this->rotate_rotor_mapping(true);
2976 }
2977 else
2979 *this->triangulation,
2980 this->simulation_parameters.mesh,
2981 this->simulation_parameters.manifolds_parameters,
2982 this->simulation_parameters.restart_parameters.restart,
2983 this->simulation_parameters.boundary_conditions.periodic_boundaries);
2984
2985 this->computing_timer.leave_subsection("Read mesh and manifolds");
2986
2987 this->setup_dofs();
2988 this->box_refine_mesh(this->simulation_parameters.restart_parameters.restart);
2990 this->simulation_parameters.initial_condition->type,
2991 this->simulation_parameters.restart_parameters.restart);
2992
2993 // Only needed if other physics apart from fluid dynamics are enabled.
2994 if (this->multiphysics->get_active_physics().size() > 1)
2996
2997 while (this->simulation_control->integrate())
2998 {
2999 // We allow the physics to update their forcing and boundary conditions
3000 // according to their own parameters
3002 this->multiphysics->update_boundary_conditions();
3003
3004 this->simulation_control->print_progression(this->pcout);
3005 this->dynamic_flow_control();
3007
3008 if (!this->simulation_control->is_at_start())
3009 {
3011 }
3012
3013 if (time_stepping_is_bdf(this->simulation_control->get_assembly_method()))
3014 {
3015 this->computing_timer.enter_subsection(
3016 "Calculate time derivative previous solutions");
3017
3019 this->time_derivative_previous_solutions.update_ghost_values();
3020 this->system_operator->evaluate_time_derivative_previous_solutions(
3022
3023 this->computing_timer.leave_subsection(
3024 "Calculate time derivative previous solutions");
3025
3026 if (this->simulation_parameters.flow_control.enable_flow_control)
3027 this->system_operator->update_beta_force(
3028 this->flow_control.get_beta());
3029 }
3030
3031 if (this->multiphysics->get_active_physics().size() > 1)
3032 {
3034
3035 if (this->simulation_parameters.multiphysics.thermal_buoyancy_force)
3036 this->system_operator->compute_thermal_buoyancy_term(
3038 this->multiphysics->get_dof_handler(PhysicsID::heat_transfer));
3039 }
3040
3041 this->iterate();
3042 this->postprocess(false);
3043 this->finish_time_step();
3044
3045 if (this->simulation_parameters.timer.type ==
3048 }
3049
3052
3053 this->finish_simulation();
3054}
3055
3056template <int dim>
3057void
3059{
3060 TimerOutput::Scope t(this->computing_timer, "Setup DoFs");
3061
3062 // Clear the preconditioners
3063 ilu_preconditioner.reset();
3064 gmg_preconditioner.reset();
3065
3066 // Clear matrix free operator
3067 this->system_operator->clear();
3068
3069 // Fill the dof handler and initialize vectors
3070 this->dof_handler->distribute_dofs(*this->fe);
3071 DoFRenumbering::Cuthill_McKee(*this->dof_handler);
3072
3073 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3075 {
3076 this->dof_handler->distribute_mg_dofs();
3077
3078 // To use elements with linear interpolation for coarse-grid we need to
3079 // have another dof handler with the appropriate element type
3080 if (this->simulation_parameters.linear_solver
3082 .mg_use_fe_q_iso_q1)
3083 {
3084 this->dof_handler_fe_q_iso_q1.reinit(*this->triangulation);
3085
3086 const auto points =
3087 QGaussLobatto<1>(this->dof_handler->get_fe().degree + 1)
3088 .get_points();
3089
3090 this->dof_handler_fe_q_iso_q1.distribute_dofs(
3091 FESystem<dim>(FE_Q_iso_Q1<dim>(points), dim + 1));
3092 this->dof_handler_fe_q_iso_q1.distribute_mg_dofs();
3093 DoFRenumbering::Cuthill_McKee(this->dof_handler_fe_q_iso_q1);
3094 }
3095 }
3096
3097 this->locally_owned_dofs = this->dof_handler->locally_owned_dofs();
3098 this->locally_relevant_dofs =
3099 DoFTools::extract_locally_relevant_dofs(*this->dof_handler);
3100
3101 // If enabled, rotate rotor mapping
3102 this->rotate_rotor_mapping(false);
3103
3104 // Non-zero constraints
3106
3107 // Check whether the boundary conditions specified in the parameter file are
3108 // available for this solver
3109
3110 for (auto const &[id, type] :
3111 this->simulation_parameters.boundary_conditions.type)
3112 {
3114 {
3115 AssertThrow(
3116 false,
3117 ExcMessage(
3118 "The following boundary conditions are not supported by the lethe-fluid-matrix-free application: partial slip."));
3119 }
3120 }
3121
3122 // Zero constraints
3124
3125 // If enabled, create mortar operators
3127
3128 // Initialize matrix-free object
3129 unsigned int mg_level = numbers::invalid_unsigned_int;
3130 this->system_operator->reinit(
3131 *this->get_mapping(),
3132 *this->dof_handler,
3133 this->zero_constraints,
3134 *this->cell_quadrature,
3135 this->forcing_function,
3137 this->simulation_parameters.stabilization.stabilization,
3138 mg_level,
3139 this->simulation_control,
3140 this->simulation_parameters.boundary_conditions,
3141 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3142 .enable_hessians_jacobian,
3143 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3144 .enable_hessians_residual,
3145 this->simulation_parameters.mortar_parameters.enable);
3146
3147
3148 // Initialize vectors using operator
3149 this->system_operator->initialize_dof_vector(*this->present_solution);
3150 this->system_operator->initialize_dof_vector(this->evaluation_point);
3151 this->system_operator->initialize_dof_vector(
3152 this->sdirk_vectors.sum_over_previous_stages);
3153 this->system_operator->initialize_dof_vector(
3154 this->sdirk_vectors.local_sum_bi_ki);
3155 this->system_operator->initialize_dof_vector(this->newton_update);
3156 this->system_operator->initialize_dof_vector(this->system_rhs);
3157 this->system_operator->initialize_dof_vector(this->local_evaluation_point);
3158 this->system_operator->initialize_dof_vector(
3160
3161 // Initialize vectors of previous solutions
3162 for (auto &solution : *this->previous_solutions)
3163 {
3164 this->system_operator->initialize_dof_vector(solution);
3165 }
3166
3167 // Initialize vectors of previous solutions
3168 for (auto &solution : this->sdirk_vectors.previous_k_j_solutions)
3169 {
3170 this->system_operator->initialize_dof_vector(solution);
3171 }
3172
3173 if (this->simulation_parameters.post_processing.calculate_average_velocities)
3174 {
3175 this->average_velocities->initialize_vectors(this->locally_owned_dofs,
3177 this->mpi_communicator);
3178
3179 // Intialize Trilinos vectors used to pass average velocities to
3180 // multiphysics interface
3183 this->mpi_communicator);
3184 }
3185
3186 double global_volume =
3187 GridTools::volume(*this->triangulation, *this->get_mapping());
3188
3189 this->pcout << " Number of active cells: "
3190 << this->triangulation->n_global_active_cells() << std::endl
3191 << " Number of degrees of freedom: "
3192 << this->dof_handler->n_dofs() << std::endl;
3193 this->pcout << " Volume of triangulation: " << global_volume
3194 << std::endl;
3195
3196 // Initialize Trilinos vectors used to pass solution to multiphysics
3197 // interface
3200 this->mpi_communicator);
3201
3202 // Pre-allocate memory for the previous solutions using the information
3203 // of the BDF schemes
3205 std::make_shared<std::vector<TrilinosWrappers::MPI::Vector>>(
3206 this->simulation_control->get_number_of_previous_solution_in_assembly());
3207
3208 for (auto &solution : *this->multiphysics_previous_solutions)
3209 solution.reinit(this->locally_owned_dofs,
3211 this->mpi_communicator);
3212
3213 // Provide relevant solution to multiphysics interface only if other physics
3214 // apart from fluid dynamics are enabled.
3215 if (this->multiphysics->get_active_physics().size() > 1)
3217}
3218
3219template <int dim>
3220void
3222{
3223 if (!this->simulation_parameters.mortar_parameters.enable)
3224 return;
3225
3226 TimerOutput::Scope t(this->computing_timer, "Update mortar configuration");
3227
3228 // We need to update the mortar operator/evaluator, as well as the sparsity
3229 // pattern, at every iteration. Since this is already done within
3230 // setup_dofs(), which is called in refine_mesh(), here we make sure that,
3231 // when there is no mesh refinement, the mortar information is still updated
3232 if (this->simulation_control->is_at_start() ||
3233 !this->simulation_control->is_refinement_step(
3234 this->simulation_parameters.mesh_adaptation) ||
3235 this->simulation_parameters.mesh_adaptation.type ==
3237 {
3238 // Clear the preconditioner before the matrix they are associated with is
3239 // cleared
3240 gmg_preconditioner.reset();
3241 ilu_preconditioner.reset();
3242
3243 // Rotate mapping
3244 this->rotate_rotor_mapping(false);
3245
3246 // Non Zero constraints
3248
3249 // Zero constraints
3251
3252 // Create mortar manager, operator, and evaluator
3254 }
3255}
3256
3257template <int dim>
3258void
3260{
3261 if (!this->simulation_parameters.mortar_parameters.enable)
3262 return;
3263
3264 TimerOutput::Scope t(this->computing_timer, "Reinit mortar operators");
3265
3266 // Create mortar manager
3267 if (this->simulation_parameters.mortar_parameters.interface_type ==
3269 this->system_operator->mortar_manager_mf =
3270 std::make_shared<MortarManagerCircle<dim>>(
3271 *this->cell_quadrature,
3272 *this->get_mapping(),
3273 *this->dof_handler,
3274 this->simulation_parameters.mortar_parameters);
3275 else if (this->simulation_parameters.mortar_parameters.interface_type ==
3277 this->system_operator->mortar_manager_mf =
3278 std::make_shared<MortarManagerLinear<dim>>(
3279 *this->cell_quadrature,
3280 *this->get_mapping(),
3281 *this->dof_handler,
3282 this->simulation_parameters.mortar_parameters);
3283 else
3284 AssertThrow(false, ExcMessage("Invalid mortar interface type."));
3285
3286 // Create mortar coupling evaluator
3287 this->system_operator->mortar_coupling_evaluator_mf =
3288 std::make_shared<NavierStokesCouplingEvaluationSIPG<dim, double>>(
3289 *this->get_mapping(),
3290 *this->dof_handler,
3291 this->physical_properties_manager->get_rheology()
3292 ->get_kinematic_viscosity());
3293
3294 this->system_operator->mortar_coupling_operator_mf =
3295 std::make_shared<CouplingOperator<dim, double>>(
3296 *this->get_mapping(),
3297 *this->dof_handler,
3298 this->zero_constraints,
3299 this->system_operator->mortar_coupling_evaluator_mf,
3300 this->system_operator->mortar_manager_mf,
3301 this->simulation_parameters.mortar_parameters.rotor_boundary_id,
3302 this->simulation_parameters.mortar_parameters.stator_boundary_id,
3303 this->simulation_parameters.mortar_parameters.sip_factor);
3304}
3305
3306template <int dim>
3307void
3309 Parameters::FluidDynamicsInitialConditionType initial_condition_type,
3310 bool restart)
3311{
3312 if (restart)
3313 {
3314 this->pcout << "************************" << std::endl;
3315 this->pcout << "---> Simulation Restart " << std::endl;
3316 this->pcout << "************************" << std::endl;
3317 this->read_checkpoint();
3318 }
3319 else if (initial_condition_type ==
3321 {
3322 this->set_nodal_values();
3323 this->present_solution->update_ghost_values();
3324 this->finish_time_step();
3325 }
3326 else if (initial_condition_type ==
3328 {
3329 // Set the nodal values to have an initial condition that is adequate
3330 this->set_nodal_values();
3331 this->present_solution->update_ghost_values();
3332
3333 // Get viscosity model
3334 std::shared_ptr<RheologicalModel> original_viscosity_model =
3335 this->physical_properties_manager->get_rheology();
3336
3337 // Temporarily set the rheology to be newtonian with predefined
3338 // viscosity
3339 std::shared_ptr<Newtonian> temporary_rheology =
3340 std::make_shared<Newtonian>(
3341 this->simulation_parameters.initial_condition->kinematic_viscosity);
3342
3343 this->physical_properties_manager->set_rheology(temporary_rheology);
3344
3345 // Temporarily solve a steady problem. The assembly method is restored
3346 // afterwards, otherwise the time-stepping scheme of the simulation would
3347 // remain steady for the rest of the simulation.
3348 const auto original_assembly_method =
3349 this->simulation_control->get_assembly_method();
3350
3351 // Solve the problem with the temporary viscosity
3352 this->simulation_control->set_assembly_method(
3355 LinearAlgebra::distributed::Vector<double>>::solve_governing_system();
3356 this->finish_time_step();
3357
3358 // Reset the assembly method to the one of the simulation
3359 this->simulation_control->set_assembly_method(original_assembly_method);
3360
3361 // Reset original rheology for the system operator
3362 this->physical_properties_manager->set_rheology(original_viscosity_model);
3363 }
3364 else if (initial_condition_type ==
3366 {
3367 this->pcout << "*********************************" << std::endl;
3368 this->pcout << " Initial condition using ramp " << std::endl;
3369 this->pcout << "*********************************" << std::endl;
3370
3371 Timer timer(this->mpi_communicator);
3372
3373 // Set the nodal values to have an initial condition that is adequate
3374 this->set_nodal_values();
3375 this->present_solution->update_ghost_values();
3376
3377 // The ramp solves a sequence of steady problems. The assembly method is
3378 // stored here and restored at the end of the ramp, otherwise the
3379 // time-stepping scheme of the simulation would remain steady for the rest
3380 // of the simulation.
3381 const auto original_assembly_method =
3382 this->simulation_control->get_assembly_method();
3383
3384 // Create a pointer to the current viscosity model
3385 std::shared_ptr<RheologicalModel> viscosity_model =
3386 this->physical_properties_manager->get_rheology();
3387
3388 // Gather the kinematic viscosity for the simulation
3389 const double original_viscosity =
3390 viscosity_model->get_kinematic_viscosity();
3391
3392 // Gather kinematic viscosity ramp parameters
3393 const int n_iter_viscosity =
3394 this->simulation_parameters.initial_condition->ramp.ramp_viscosity
3395 .n_iter;
3396 double kinematic_viscosity =
3397 n_iter_viscosity > 0 ?
3398 this->simulation_parameters.initial_condition->ramp.ramp_viscosity
3399 .kinematic_viscosity_init :
3400 original_viscosity;
3401 const double alpha_viscosity =
3402 this->simulation_parameters.initial_condition->ramp.ramp_viscosity
3403 .alpha;
3404
3405 // Ramp on kinematic viscosity
3406 for (int i = 0; i < n_iter_viscosity; ++i)
3407 {
3408 this->pcout << "********* Solution for kinematic viscosity = " +
3409 std::to_string(kinematic_viscosity) + " *********"
3410 << std::endl;
3411
3412 // Set the temporary viscosity to the one given by the ramp
3413 viscosity_model->set_kinematic_viscosity(kinematic_viscosity);
3414
3415 this->simulation_control->set_assembly_method(
3417
3418 // Solve the problem with the temporary viscosity
3420 solve_governing_system();
3421 this->finish_time_step();
3422
3423 // Update the viscosity to the next one in the ramp parameters
3424 kinematic_viscosity +=
3425 alpha_viscosity * (original_viscosity - kinematic_viscosity);
3426 }
3427
3428
3429 // Reset kinematic viscosity to original value for the system operator
3430 viscosity_model->set_kinematic_viscosity(original_viscosity);
3431
3432 // Reset the assembly method to the one of the simulation
3433 this->simulation_control->set_assembly_method(original_assembly_method);
3434
3435 timer.stop();
3436
3437 if (this->simulation_parameters.timer.type !=
3439 {
3440 this->pcout << "*********************************" << std::endl;
3441 this->pcout << " Time spent in ramp: " << timer.wall_time() << "s"
3442 << std::endl;
3443 this->pcout << "*********************************" << std::endl;
3444 }
3445 }
3446 else
3447 {
3448 throw std::runtime_error(
3449 "Type of initial condition is not supported by MF Navier-Stokes");
3450 }
3451}
3452
3453template <int dim>
3454void
3456{
3457 // Required for compilation but not used for matrix free solvers.
3458 TimerOutput::Scope t(this->computing_timer, "Assemble matrix");
3459}
3460
3461template <int dim>
3462void
3464{
3465 TimerOutput::Scope t(this->computing_timer, "Assemble RHS");
3466
3467 // Update the precomputed values needed for the evaluation of the residual.
3468 // This is needed, otherwise the line-search mechanism used in the Newton
3469 // method might fail even though the Newton step should have been accepted
3470 // due to a wrong evaluation of the residual and, consequently, a wrong
3471 // evaluation of the step length.
3472 this->evaluation_point.update_ghost_values();
3473
3474 if (this->simulation_parameters.mortar_parameters.enable)
3475 this->system_operator->evaluate_velocity_ale(
3476 *this->get_mapping(),
3477 this->simulation_parameters.mortar_parameters.center_of_rotation,
3478 this->simulation_parameters.mortar_parameters.rotation_axis_direction,
3479 this->simulation_parameters.mortar_parameters.rotor_angular_velocity);
3480
3481 this->system_operator->evaluate_non_linear_term_and_calculate_tau(
3482 this->evaluation_point);
3483
3484 /*
3485 (σ . n , v) = (traction_fn, v) on Γ_N. computes traction vector for the
3486 prescribed Neumann traction σ = 2με(u)− pI
3487 */
3488
3489 this->system_operator->evaluate_prescribed_neumann_traction();
3490
3491 this->system_operator->evaluate_residual(this->system_rhs,
3492 this->evaluation_point);
3493
3494 // If mortar is enabled, add RHS entries
3495 if (this->simulation_parameters.mortar_parameters.enable)
3496 {
3497 this->computing_timer.enter_subsection("Assemble RHS (mortar)");
3498 this->system_operator->mortar_coupling_operator_mf->vmult_add(
3499 this->system_rhs, this->evaluation_point);
3500 this->system_rhs.compress(VectorOperation::add);
3501 this->computing_timer.leave_subsection("Assemble RHS (mortar)");
3502 }
3503
3504 this->system_rhs *= -1.0;
3505
3506 // Provide residual to simulation control for stopping criterion when using
3507 // steady bdf
3508 if (this->simulation_control->is_first_assembly())
3509 this->simulation_control->provide_residual(this->system_rhs.l2_norm());
3510}
3511
3512template <int dim>
3513void
3515{
3516 TimerOutput::Scope t(this->computing_timer,
3517 "Update multiphysics average solution");
3518
3519 if (this->simulation_parameters.post_processing.calculate_average_velocities)
3520 {
3521 TrilinosWrappers::MPI::Vector temp_average_velocities(
3524 temp_average_velocities,
3525 *this->average_velocities->get_average_velocities());
3526 this->multiphysics_average_velocities = temp_average_velocities;
3527
3528#ifndef LETHE_USE_LDV
3529 this->multiphysics->set_time_average_solution(
3531 std::make_shared<TrilinosWrappers::MPI::Vector>(
3533#else
3534 this->multiphysics->set_time_average_solution(
3536 this->average_velocities->get_average_velocities());
3537#endif
3538 }
3539}
3540
3541template <int dim>
3542void
3544{
3546
3547 // Time stepping information
3548 const auto method = this->simulation_control->get_assembly_method();
3549
3550 if (time_stepping_is_sdirk(this->simulation_control->get_assembly_method()))
3551 {
3552 // For SDIRK, we only have one previous solution
3553 const SDIRKTable table = sdirk_table(method);
3554 const SDIRKStageData stage_data(table, 1);
3555 const double a_ii = stage_data.a_ij[0];
3556 const auto time_steps_vector =
3557 this->simulation_control->get_time_steps_vector();
3558 const double dt = time_steps_vector[0];
3560 -1 / (dt * a_ii), (*this->previous_solutions)[0]);
3562 -1, this->sdirk_vectors.sum_over_previous_stages);
3563 }
3564
3565 else
3566 {
3567 // Vector for the BDF coefficients
3568 const Vector<double> &bdf_coefs =
3569 this->simulation_control->get_bdf_coefficients();
3570
3571 for (unsigned int p = 0; p < number_of_previous_solutions(method); ++p)
3572 {
3574 bdf_coefs[p + 1], (*this->previous_solutions)[p]);
3575 }
3576 }
3577}
3578
3579template <int dim>
3580void
3582{
3583 gmg_preconditioner = std::make_shared<MFNavierStokesPreconditionGMG<dim>>(
3585 *this->dof_handler,
3587
3588 gmg_preconditioner->reinit(this->mapping,
3589 this->cell_quadrature,
3590 this->forcing_function,
3591 this->simulation_control,
3593 this->fe);
3594}
3595
3596template <int dim>
3597void
3599{
3600 // Initialize everything related to heat transfer within the MG algorithm
3601 if (this->simulation_parameters.multiphysics.thermal_buoyancy_force)
3603 ->initialize_auxiliary_physics(this->multiphysics->get_dof_handler(
3605 this->temperature_present_solution);
3606
3609 this->flow_control,
3610 *this->present_solution,
3612}
3613
3614template <int dim>
3615void
3617{
3618 TimerOutput::Scope t(this->computing_timer, "Setup GMG");
3619
3620 if (!gmg_preconditioner)
3621 this->create_GMG();
3622
3623 this->initialize_GMG();
3624}
3625
3626template <int dim>
3627void
3629{
3630 TimerOutput::Scope t(this->computing_timer, "Setup ILU");
3631
3632 int current_preconditioner_fill_level =
3633 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3634 .ilu_precond_fill;
3635 const double ilu_atol =
3636 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3637 .ilu_precond_atol;
3638 const double ilu_rtol =
3639 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3640 .ilu_precond_rtol;
3641 TrilinosWrappers::PreconditionILU::AdditionalData preconditionerOptions(
3642 current_preconditioner_fill_level, ilu_atol, ilu_rtol, 0);
3643
3644 ilu_preconditioner = std::make_shared<TrilinosWrappers::PreconditionILU>();
3645
3646 ilu_preconditioner->initialize(system_operator->get_system_matrix(),
3647 preconditionerOptions);
3648}
3649
3650
3651template <int dim>
3652void
3654{
3655 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3656 .mg_verbosity == Parameters::Verbosity::extra_verbose)
3657 {
3658 this->pcout << std::defaultfloat;
3659
3660 announce_string(this->pcout, "Multigrid setup times");
3661 this->gmg_preconditioner->mg_setup_timer.print_wall_time_statistics(
3662 MPI_COMM_WORLD);
3663
3664 announce_string(this->pcout, "Multigrid vmult times");
3665 this->gmg_preconditioner->mg_vmult_timer.print_wall_time_statistics(
3666 MPI_COMM_WORLD);
3667
3668 announce_string(this->pcout, "System operator times");
3669 this->system_operator->timer.print_wall_time_statistics(MPI_COMM_WORLD);
3670
3671 auto mg_operators = this->gmg_preconditioner->get_mg_operators();
3672 for (unsigned int level = mg_operators.min_level();
3673 level <= mg_operators.max_level();
3674 level++)
3675 {
3676 announce_string(this->pcout,
3677 "Operator level " + std::to_string(level) + " times");
3678 mg_operators[level]->timer.print_wall_time_statistics(MPI_COMM_WORLD);
3679
3680 // Reset timer if output is set to every iteration
3681 mg_operators[level]->timer.reset();
3682 }
3683
3684 auto mg_smoother_preconditioners =
3685 this->gmg_preconditioner->get_mg_smoother_preconditioners();
3686 for (unsigned int level = mg_operators.min_level();
3687 level <= mg_operators.max_level();
3688 level++)
3689 {
3690 announce_string(this->pcout,
3691 "Preconditioner level " + std::to_string(level) +
3692 " times");
3693 mg_smoother_preconditioners[level]->timer_print();
3694
3695 // Reset timer if output is set to every iteration
3696 mg_smoother_preconditioners[level]->timer_reset();
3697 }
3698
3699 // Reset timers if output is set to every iteration
3700 this->gmg_preconditioner->mg_setup_timer.reset();
3701 this->gmg_preconditioner->mg_vmult_timer.reset();
3702
3703 this->pcout << std::scientific;
3704 }
3705}
3706
3707template <int dim>
3708void
3710{
3711 TimerOutput::Scope t(this->computing_timer,
3712 "Update solutions for multiphysics");
3713
3714 // Provide the fluid dynamics dof_handler to the multiphysics interface
3715 this->multiphysics->set_dof_handler(PhysicsID::fluid_dynamics,
3716 this->dof_handler);
3717 // Provide the fluid dynamics mapping to the multiphysics interface
3718 this->multiphysics->set_mapping(PhysicsID::fluid_dynamics, this->mapping);
3719
3720 // Convert the present solution to multiphysics vector type and provide it
3721 // to the multiphysics interface
3722 TrilinosWrappers::MPI::Vector temp_solution(this->locally_owned_dofs,
3723 this->mpi_communicator);
3724
3725 this->present_solution->update_ghost_values();
3727 *multiphysics_present_solution = temp_solution;
3728
3729#ifndef LETHE_USE_LDV
3730 this->multiphysics->set_solution(PhysicsID::fluid_dynamics,
3732#else
3733 this->multiphysics->set_solution(PhysicsID::fluid_dynamics,
3734 this->present_solution);
3735#endif
3736
3737 // Convert the previous solutions to multiphysics vector type and provide
3738 // them to the multiphysics interface
3739 const unsigned int number_of_previous_solutions =
3740 this->simulation_control->get_number_of_previous_solution_in_assembly();
3741
3742 std::vector<TrilinosWrappers::MPI::Vector> temp_previous_solutions;
3743
3744 temp_previous_solutions.resize(number_of_previous_solutions);
3745 for (auto &solution : temp_previous_solutions)
3746 solution.reinit(this->locally_owned_dofs, this->mpi_communicator);
3747
3748 for (unsigned int i = 0; i < number_of_previous_solutions; i++)
3749 {
3750 (*this->previous_solutions)[i].update_ghost_values();
3751 convert_vector_dealii_to_trilinos(temp_previous_solutions[i],
3752 (*this->previous_solutions)[i]);
3753
3754 (*this->multiphysics_previous_solutions)[i] = temp_previous_solutions[i];
3755 }
3756
3757#ifndef LETHE_USE_LDV
3758 this->multiphysics->set_previous_solutions(
3760#else
3761 this->multiphysics->set_previous_solutions(PhysicsID::fluid_dynamics,
3762 this->previous_solutions);
3763#endif
3764}
3765
3766template <int dim>
3767void
3769{
3770 TimerOutput::Scope t(this->computing_timer,
3771 "Update solutions for fluid dynamics");
3772
3773 if (this->simulation_parameters.multiphysics.heat_transfer)
3774 {
3775 // Get present solution and dof handler of the heat transfer
3776 const auto &heat_solution =
3777 this->multiphysics->get_solution(PhysicsID::heat_transfer);
3778
3779#ifndef LETHE_USE_LDV
3780 const auto &heat_dof_handler =
3781 this->multiphysics->get_dof_handler(PhysicsID::heat_transfer);
3782
3783 // Copy solution to temporary vector
3784 TrilinosWrappers::MPI::Vector temp_heat_solution;
3785 temp_heat_solution.reinit(heat_dof_handler.locally_owned_dofs(),
3786 this->mpi_communicator);
3787 temp_heat_solution = heat_solution;
3788
3789 // Initialize deal.II vector to store solution
3790 this->temperature_present_solution.reinit(
3791 heat_dof_handler.locally_owned_dofs(),
3792 DoFTools::extract_locally_active_dofs(heat_dof_handler),
3793 this->mpi_communicator);
3794
3795 // Perform copy between two vector types
3797 temp_heat_solution);
3798
3799#else
3800 this->temperature_present_solution = heat_solution;
3801#endif
3802 // Update ghost values for deal.II vector
3803 this->temperature_present_solution.update_ghost_values();
3804 }
3805}
3806template <int dim>
3807void
3809{
3810 this->present_solution->update_ghost_values();
3811
3812 if (this->simulation_parameters.mortar_parameters.enable)
3813 this->system_operator->evaluate_velocity_ale(
3814 *this->get_mapping(),
3815 this->simulation_parameters.mortar_parameters.center_of_rotation,
3816 this->simulation_parameters.mortar_parameters.rotation_axis_direction,
3817 this->simulation_parameters.mortar_parameters.rotor_angular_velocity);
3818
3819 this->computing_timer.enter_subsection("Evaluate non linear term and tau");
3820
3821 this->system_operator->evaluate_non_linear_term_and_calculate_tau(
3822 *this->present_solution);
3823
3824 this->computing_timer.leave_subsection("Evaluate non linear term and tau");
3825
3826 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3828 setup_ILU();
3829 else if ((this->simulation_parameters.linear_solver
3831 .preconditioner ==
3833 (this->simulation_parameters.linear_solver
3835 .preconditioner ==
3837 setup_GMG();
3838 else
3839 AssertThrow(
3840 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3841 .preconditioner ==
3843 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3844 .preconditioner ==
3846 ExcMessage(
3847 "This linear solver does not support this preconditioner. Only <ilu|lsmg|gcmg> preconditioners are supported."));
3848}
3849
3850template <int dim>
3851void
3853{
3854 const double absolute_residual =
3855 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3856 .minimum_residual;
3857 const double relative_residual =
3858 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3859 .relative_residual;
3860
3861 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3863 solve_system_GMRES(absolute_residual, relative_residual);
3864 else if (this->simulation_parameters.linear_solver
3867 solve_system_BiCGStab(absolute_residual, relative_residual);
3868 else if (this->simulation_parameters.linear_solver
3871 solve_system_direct(absolute_residual, relative_residual);
3872 else
3873 AssertThrow(false, ExcMessage("This solver is not allowed"));
3875}
3876
3877template <int dim>
3878void
3883
3884template <int dim>
3885void
3887 const double relative_residual)
3888{
3889 const AffineConstraints<double> &zero_constraints = this->zero_constraints;
3890 const double rescale_metric = this->get_residual_rescale_metric();
3891 const double current_residual = this->system_rhs.l2_norm() / rescale_metric;
3892 const double linear_solver_tolerance =
3893 std::max(relative_residual * current_residual, absolute_residual);
3894 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3895 .verbosity != Parameters::Verbosity::quiet)
3896 {
3897 this->pcout << " -Tolerance of iterative solver is : "
3898 << linear_solver_tolerance << std::endl;
3899 }
3900 const double non_rescaled_linear_solver_tolerance =
3901 linear_solver_tolerance * rescale_metric;
3902 GlobalVectorType completely_distributed_solution(this->locally_owned_dofs,
3903 this->mpi_communicator);
3904
3905 SolverControl solver_control(this->simulation_parameters.linear_solver
3907 .max_iterations,
3908 non_rescaled_linear_solver_tolerance,
3909 true,
3910 true);
3911
3912 SolverGMRES<VectorType>::AdditionalData solver_parameters;
3913
3914 solver_parameters.max_n_tmp_vectors =
3915 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3916 .max_krylov_vectors;
3917 solver_parameters.right_preconditioning = true;
3918
3919 SolverGMRES<VectorType> solver(solver_control, solver_parameters);
3920
3921 AssertThrow(
3922 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3923 .preconditioner ==
3925 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3926 .preconditioner ==
3928 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
3930 ExcMessage(
3931 "This linear solver does not support this preconditioner. Only <ilu|lsmg|gcmg> preconditioners are supported."));
3932
3933 this->newton_update = 0.0;
3934
3935 // The timer section is held by a scope guard so that it is closed on every
3936 // path out of the solve, including the one taken when the exception raised by
3937 // a failed linear solve is rethrown below.
3938 {
3939 TimerOutput::Scope t(this->computing_timer, "Solve linear system");
3940
3941 // The matrix-free solver does not retry the linear solve with a higher
3942 // preconditioner fill level when it fails. If the linear solver fails and
3943 // the user requested to force its continuation through the <force linear
3944 // solver continuation> parameter, we ignore the failure and continue the
3945 // Newton step with the current Newton update. Otherwise, the exception is
3946 // rethrown.
3947 try
3948 {
3949 if ((this->simulation_parameters.linear_solver
3951 .preconditioner ==
3953 (this->simulation_parameters.linear_solver
3955 .preconditioner ==
3957 {
3958 solver.solve(*(this->system_operator),
3959 this->newton_update,
3960 this->system_rhs,
3961 *(this->gmg_preconditioner));
3962
3963 if (this->simulation_parameters.linear_solver
3965 .mg_verbosity != Parameters::Verbosity::quiet)
3966 this->gmg_preconditioner->print_relevant_info();
3967 }
3968 else if (this->simulation_parameters.linear_solver
3970 .preconditioner ==
3972 solver.solve(*(this->system_operator),
3973 this->newton_update,
3974 this->system_rhs,
3975 *(this->ilu_preconditioner));
3976 }
3977 catch (const std::exception &e)
3978 {
3979 if (!this->simulation_parameters.linear_solver
3981 .force_linear_solver_continuation)
3982 {
3983 this->pcout << " GMRES solver failed." << std::endl;
3984 if (const auto *nc =
3985 dynamic_cast<const SolverControl::NoConvergence *>(&e))
3986 this->pcout << " It stopped at iteration " << nc->last_step
3987 << " with a residual of "
3988 << nc->last_residual / rescale_metric
3989 << " (tolerance " << linear_solver_tolerance << ")."
3990 << std::endl;
3991
3992 throw;
3993 }
3994
3995 this->pcout
3996 << " GMRES solver failed! Continuing the Newton step as requested by the <force linear solver continuation> parameter."
3997 << std::endl;
3998 }
3999 }
4000
4001 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
4002 .verbosity != Parameters::Verbosity::quiet)
4003 {
4004 this->pcout << " -Iterative solver took : " << solver_control.last_step()
4005 << " steps to reach a residual norm of "
4006 << solver_control.last_value() / rescale_metric << std::endl;
4007 }
4008
4009 this->computing_timer.enter_subsection(
4010 "Distribute constraints after linear solver");
4011
4012 zero_constraints.distribute(this->newton_update);
4013
4014 this->computing_timer.leave_subsection(
4015 "Distribute constraints after linear solver");
4016}
4017
4018template <int dim>
4019void
4021 const double absolute_residual,
4022 const double relative_residual)
4023{
4024 const AffineConstraints<double> &zero_constraints = this->zero_constraints;
4025 const double rescale_metric = this->get_residual_rescale_metric();
4026 const double current_residual = this->system_rhs.l2_norm() / rescale_metric;
4027 const double linear_solver_tolerance =
4028 std::max(relative_residual * current_residual, absolute_residual);
4029 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
4030 .verbosity != Parameters::Verbosity::quiet)
4031 {
4032 this->pcout << " -Tolerance of iterative solver is : "
4033 << linear_solver_tolerance << std::endl;
4034 }
4035 const double non_rescaled_linear_solver_tolerance =
4036 linear_solver_tolerance * rescale_metric;
4037 GlobalVectorType completely_distributed_solution(this->locally_owned_dofs,
4038 this->mpi_communicator);
4039
4040 SolverControl solver_control(this->simulation_parameters.linear_solver
4042 .max_iterations,
4043 non_rescaled_linear_solver_tolerance,
4044 true,
4045 true);
4046
4047 SolverBicgstab<VectorType> solver(solver_control);
4048
4049 AssertThrow(
4050 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
4051 .preconditioner ==
4053 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
4054 .preconditioner ==
4056 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
4058 ExcMessage(
4059 "This linear solver does not support this preconditioner. Only <ilu|lsmg|gcmg> preconditioners are supported."));
4060
4061 this->newton_update = 0.0;
4062
4063 // The timer section is held by a scope guard so that it is closed on every
4064 // path out of the solve, including the one taken when the exception raised by
4065 // a failed linear solve is rethrown below.
4066 {
4067 TimerOutput::Scope t(this->computing_timer, "Solve linear system");
4068
4069 // The matrix-free solver does not retry the linear solve with a higher
4070 // preconditioner fill level when it fails. If the linear solver fails and
4071 // the user requested to force its continuation through the <force linear
4072 // solver continuation> parameter, we ignore the failure and continue the
4073 // Newton step with the current Newton update. Otherwise, the exception is
4074 // rethrown.
4075 try
4076 {
4077 if ((this->simulation_parameters.linear_solver
4079 .preconditioner ==
4081 (this->simulation_parameters.linear_solver
4083 .preconditioner ==
4085 {
4086 solver.solve(*(this->system_operator),
4087 this->newton_update,
4088 this->system_rhs,
4089 *(this->gmg_preconditioner));
4090
4091 if (this->simulation_parameters.linear_solver
4093 .mg_verbosity != Parameters::Verbosity::quiet)
4094 this->gmg_preconditioner->print_relevant_info();
4095 }
4096 else if (this->simulation_parameters.linear_solver
4098 .preconditioner ==
4100 solver.solve(*(this->system_operator),
4101 this->newton_update,
4102 this->system_rhs,
4103 *(this->ilu_preconditioner));
4104 }
4105 catch (const std::exception &e)
4106 {
4107 if (!this->simulation_parameters.linear_solver
4109 .force_linear_solver_continuation)
4110 {
4111 this->pcout << " BiCGStab solver failed." << std::endl;
4112 if (const auto *nc =
4113 dynamic_cast<const SolverControl::NoConvergence *>(&e))
4114 {
4115 this->pcout << " It stopped at iteration " << nc->last_step
4116 << " with a residual of "
4117 << nc->last_residual / rescale_metric
4118 << " (tolerance " << linear_solver_tolerance << ")."
4119 << std::endl;
4120
4121 if (!std::isfinite(nc->last_residual))
4122 this->pcout
4123 << " The residual is not finite, which indicates a breakdown of the BiCGStab iteration or of the preconditioner."
4124 << std::endl;
4125 else
4126 this->pcout
4127 << " The iteration budget was exhausted. BiCGStab restarts silently when it breaks down, so this may be either slow convergence or a breakdown."
4128 << std::endl;
4129 }
4130
4131 throw;
4132 }
4133
4134 this->pcout
4135 << " BiCGStab solver failed! Continuing the Newton step as requested by the <force linear solver continuation> parameter."
4136 << std::endl;
4137 }
4138 }
4139
4140 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
4141 .verbosity != Parameters::Verbosity::quiet)
4142 {
4143 this->pcout << " -Iterative solver took : " << solver_control.last_step()
4144 << " steps to reach a residual norm of "
4145 << solver_control.last_value() / rescale_metric << std::endl;
4146 }
4147
4148 this->computing_timer.enter_subsection(
4149 "Distribute constraints after linear solver");
4150
4151 zero_constraints.distribute(this->newton_update);
4152
4153 this->computing_timer.leave_subsection(
4154 "Distribute constraints after linear solver");
4155}
4156
4157template <int dim>
4158void
4160 const double absolute_residual,
4161 const double relative_residual)
4162{
4163 const AffineConstraints<double> &zero_constraints = this->zero_constraints;
4164 const double rescale_metric = this->get_residual_rescale_metric();
4165 const double current_residual = this->system_rhs.l2_norm() / rescale_metric;
4166 const double linear_solver_tolerance =
4167 std::max(relative_residual * current_residual, absolute_residual);
4168 const double non_rescaled_linear_solver_tolerance =
4169 linear_solver_tolerance * rescale_metric;
4170
4171 SolverControl solver_control(this->simulation_parameters.linear_solver
4173 .max_iterations,
4174 non_rescaled_linear_solver_tolerance,
4175 true,
4176 true);
4177
4178 TrilinosWrappers::SolverDirect solver(solver_control);
4179
4180 this->newton_update = 0.0;
4181
4182 this->computing_timer.enter_subsection("Solve linear system");
4183
4184 solver.initialize(this->system_operator->get_system_matrix());
4185 solver.solve(this->newton_update, this->system_rhs);
4186
4187 this->computing_timer.leave_subsection("Solve linear system");
4188
4189 this->computing_timer.enter_subsection(
4190 "Distribute constraints after linear solver");
4191
4192 zero_constraints.distribute(this->newton_update);
4193
4194 this->computing_timer.leave_subsection(
4195 "Distribute constraints after linear solver");
4196}
4197
4198template class FluidDynamicsMatrixFree<2>;
4199template class FluidDynamicsMatrixFree<3>;
4200
4203
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
Dynamic flow controller that drives the average velocity of the flow towards a target value.
Tensor< 1, dim > get_beta() const
Get the beta force of the current time step.
A solver for the incompressible Navier-Stokes equations implemented in a matrix-free fashion.
virtual void initialize_GMG()
Intialize the geometric multigrid preconditioner.
virtual void solve()
Solve the problem defined by simulation parameters by iterating through time or through the mesh refi...
void reinit_mortar_operators_mf()
Initialize mortar coupling manager, operator, and evaluator for the matrix-free solver.
TrilinosWrappers::MPI::Vector multiphysics_average_velocities
Trilinos vector storing the average velocities that are provided to other physics.
void solve_system_GMRES(const double absolute_residual, const double relative_residual)
GMRES solver with preconditioning.
void setup_ILU()
Setup the implicit LU preconditioner and call the solve function of the linear solver....
void print_mg_setup_times()
Prints the setup times for the geometric multigrid preconditioner.
std::shared_ptr< PhysicalPropertiesManager > physical_properties_manager
Pointer to the physical properties object.
std::shared_ptr< std::vector< TrilinosWrappers::MPI::Vector > > multiphysics_previous_solutions
Vector storing trilinos vectors containing the previous solutions that are provided to other physics.
virtual void update_solutions_for_fluid_dynamics() override
Provide present multiphysics solutions from the multiphysics interface to the fluid dynamics.
DoFHandler< dim > dof_handler_fe_q_iso_q1
Describes the layout of DoFs for FE_Q_iso_Q1 elements.
void update_forcing_and_boundary_conditions() override
Set the time of the forcing function and of the time-dependent boundary conditions to the current tim...
virtual void set_initial_condition_fd(Parameters::FluidDynamicsInitialConditionType initial_condition_type, bool restart=false) override
VectorType temperature_present_solution
Vector storing the present temperature solution from the heat transfer solver.
VectorType time_derivative_previous_solutions
Vector to store the time derivatives of the previous solutions at the end of each time step for time-...
void setup_preconditioner() override
Set up appropriate preconditioner.
virtual void update_solutions_for_multiphysics() override
Provide present and previous flow solutions to the multiphysics interface. These solutions can be acc...
virtual void update_multiphysics_time_average_solution() override
Update the average velocity field solution in the multiphyscics interface.
virtual void update_mortar_configuration() override
Update mortar configuration.
std::shared_ptr< TrilinosWrappers::MPI::Vector > multiphysics_present_solution
Trilinos vector storing the present solution that is provided to other physics.
virtual void assemble_system_rhs() override
Assemble the system right hand side associated with the solver.
std::shared_ptr< NavierStokesOperatorBase< dim, double > > system_operator
Matrix-free operator in used for all the matrix-vector multiplications calls (vmult).
void solve_system_direct(const double absolute_residual, const double relative_residual)
Direct solver using TrilinosWrappers::SolverDirect.
std::shared_ptr< MFNavierStokesPreconditionGMGBase< dim > > gmg_preconditioner
Geometric multigrid preconditioner.
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.
void solve_system_BiCGStab(const double absolute_residual, const double relative_residual)
BiCGStab solver with preconditioning.
void calculate_time_derivative_previous_solutions()
Calculate and store time derivatives of previous solutions according to time-stepping scheme to use t...
virtual void create_GMG()
Create the geometric multigrid preconditioner.
FluidDynamicsMatrixFree(SimulationParameters< dim > &nsparam, const FluidDynamicsFormulation fluid_dynamics_formulation=FluidDynamicsFormulation::standard)
Constructor that sets the finite element degree and system operator according to simulation parameter...
void setup_GMG()
Setup the geometric multigrid preconditioner.
std::shared_ptr< TrilinosWrappers::PreconditionILU > ilu_preconditioner
Implicit LU preconditioner.
virtual void update_multi_stage_solution(double time_step) override
virtual void setup_dofs_fd() override
Setup the degrees of freedom, system constraints, system operator and solution vectors.
virtual void assemble_system_matrix() override
Assemble the matrix associated with the solver. Only required for compilation and it is not used for ...
void solve_linear_system() override
Solve the linear system of equations using the method specified in the simulation parameters.
virtual void multi_stage_postresolution(unsigned int stage, Parameters::SimulationControl::TimeSteppingMethod method, double time_step) override
Calculate from Update .
LinearAlgebra::distributed::Vector< double > VectorType
void assemble_L2_projection()
Assemble a L2 projection matrix for the velocity and the pressure, which can be used to set the initi...
A geometric multigrid preconditioner compatible with the matrix-free solver.
MGLevelObject< std::shared_ptr< PreconditionBase< MGVectorType > > > mg_smoother_preconditioners
Preconditioners associated to smoothers.
std::shared_ptr< SolverControl > coarse_grid_solver_control_intermediate
Solver control for the coarse grid solver (intermediate level).
MGLevelObject< std::shared_ptr< OperatorType > > mg_operators
Level operators for the geometric multigrid.
MGLevelObject< DoFHandler< dim > > temperature_dof_handlers
std::shared_ptr< PreconditionMG< dim, MGVectorType, GCTransferType > > gc_multigrid_preconditioner
Global coarsening multigrid preconditioner object.
PreconditionChebyshev< OperatorType, MGVectorType, SmootherPreconditionerType > ChebyshevSmootherType
SimulationParameters< dim > simulation_parameters
Simulation parameters.
void setup_AMG()
Set up AMG object needed for coarse-grid solver or preconditioning.
TimerOutput mg_vmult_timer
Internal timer for vmult timings.
unsigned int maxlevel
Max level of the multigrid hierarchy.
std::shared_ptr< PreconditionAdapter< MGVectorType, TrilinosVectorType > > coarse_grid_precondition
Coarse grid solver (algebraic multigrid, ILU, direct solver).
void setup_ILU()
Set up ILU object needed for coarse-grid solver or preconditioning.
std::shared_ptr< Multigrid< MGVectorType > > mg
Multigrid method.
std::shared_ptr< mg::Matrix< MGVectorType > > mg_interface_matrix_in
Interface edge matrix needed only for local smoothing.
std::shared_ptr< SolverControl > direct_solver_control
Solver control for the direct solver.
TimerOutput mg_setup_timer
Timer for specific geometric multigrid components.
LinearAlgebra::distributed::Vector< Number > VectorType
void initialize()
Initialize smoother, coarse grid solver and multigrid object needed for the geometric multigrid preco...
std::shared_ptr< MGCoarseGridBase< MGVectorType > > mg_coarse_intermediate
Multigrid wrapper for the coarse grid solver (intermediate level).
std::shared_ptr< MGCoarseGridBase< MGVectorType > > mg_coarse
Multigrid wrapper for the coarse grid solver.
std::shared_ptr< SolverGMRES< MGVectorType > > coarse_grid_solver_intermediate
GMRES as coarse grid solver (intermediate level).
std::shared_ptr< SolverControl > coarse_grid_solver_control
Solver control for the coarse grid solver.
const DoFHandler< dim > & dof_handler
Describes the layout of DoFs and the type of FE.
std::shared_ptr< LSTransferType > mg_transfer_ls
Transfer operator for local smoothing.
std::shared_ptr< mg::Matrix< MGVectorType > > mg_matrix
Multigrid level object storing all operators.
LinearAlgebra::distributed::Vector< MGNumber > MGVectorType
MFNavierStokesPreconditionGMGBase(const SimulationParameters< dim > &simulation_parameters, const DoFHandler< dim > &dof_handler, const DoFHandler< dim > &dof_handler_fe_q_iso_q1)
Construct a new precondition GMG object. Sets constraints, operators and transfer objects.
std::shared_ptr< PreconditionMG< dim, MGVectorType, GCTransferType > > gc_multigrid_preconditioner_intermediate
Global coarsening multigrid preconditioner object (intermediate level).
std::shared_ptr< GCTransferType > mg_transfer_gc_temperature
Transfer operator for global coarsening for the temperature.
unsigned int minlevel
Min level of the multigrid hierarchy.
void vmult(VectorType &dst, const VectorType &src) const
Calls the v cycle function of the multigrid object.
const MGLevelObject< std::shared_ptr< OperatorType > > & get_mg_operators() const
Getter function for all level operators.
void reinit(const std::shared_ptr< Mapping< dim > > &mapping, const std::shared_ptr< Quadrature< dim > > &cell_quadrature, const std::shared_ptr< Function< dim > > &forcing_function, const std::shared_ptr< SimulationControl > &simulation_control, const std::shared_ptr< PhysicalPropertiesManager > &physical_properties_manager, const std::shared_ptr< FESystem< dim > > &fe)
Initializes geometric multigrid preconditioner and pre-calculate terms that are constant.
std::vector< std::shared_ptr< const Triangulation< dim > > > coarse_grid_triangulations
Triangulations for the global coarsening case.
MGLevelObject< MGTwoLevelTransfer< dim, MGVectorType > > transfers_temperature
DoFHandler< dim > dof_handler_fe_q_iso_q1
Describes the layout of DoFs for FE_Q_iso_Q1 elements.
void print_relevant_info() const
Prints relevant multigrid information.
NavierStokesOperatorBase< dim, MGNumber > OperatorType
std::vector< unsigned int > coarse_grid_iterations
Vector holding number of coarse grid iterations.
MGLevelObject< MatrixFreeOperators::MGInterfaceOperator< OperatorType > > ls_mg_operators
const MGLevelObject< std::shared_ptr< PreconditionBase< MGVectorType > > > & get_mg_smoother_preconditioners() const
Getter function for all level smoother preconditioners.
MGLevelObject< MGTwoLevelTransfer< dim, MGVectorType > > transfers
Transfers for each of the levels of the global coarsening algorithm.
MGLevelObject< MatrixFreeOperators::MGInterfaceOperator< OperatorType > > ls_mg_interface_in
std::shared_ptr< GCTransferType > mg_transfer_gc
Transfer operator for global coarsening.
std::shared_ptr< Multigrid< MGVectorType > > mg_intermediate
Multigrid method (intermediate level).
MGLevelObject< MGConstrainedDoFs > mg_constrained_dofs
ConditionalOStream pcout
Conditional Ostream.
std::shared_ptr< MGSmootherBase< MGVectorType > > mg_smoother
std::shared_ptr< PreconditionMG< dim, MGVectorType, LSTransferType > > ls_multigrid_preconditioner
Local smoothing multigrid preconditioner object.
MGLevelObject< std::shared_ptr< MappingQCache< dim > > > mappings
MappingQCache for each of the levels of the global coarsening algorithm.
std::shared_ptr< PreconditionMG< dim, MGVectorType, LSTransferType > > ls_multigrid_preconditioner_intermediate
Local smoothing multigrid preconditioner object (intermediate level).
unsigned int intlevel
Intermediate level of the multigrid hierarchy.
std::shared_ptr< SolverGMRES< TrilinosVectorType > > coarse_grid_solver
GMRES as coarse grid solver.
virtual void create_level_operator(const unsigned int level)=0
Creates the operator for a given level.
MGLevelObject< DoFHandler< dim > > dof_handlers
DoF handlers for each of the levels of the global coarsening algorithm.
A geometric multigrid preconditioner implementation for incompressible flow.
void create_level_operator(const unsigned int level) override
Creates the operator for a given level.
MFNavierStokesPreconditionGMG(const SimulationParameters< dim > &simulation_parameters, const DoFHandler< dim > &dof_handler, const DoFHandler< dim > &dof_handler_fe_q_iso_q1)
typename MFNavierStokesPreconditionGMGBase< dim >::VectorType VectorType
void initialize(const std::shared_ptr< SimulationControl > &simulation_control, FlowControl< dim > &flow_control, const VectorType &present_solution, const VectorType &time_derivative_previous_solutions)
void initialize_auxiliary_physics(const DoFHandler< dim > &temperature_dof_handler, const VectorType &temperature_present_solution)
Transfer the current temperature solution to the different multigrid levels and compute the thermal b...
void restrict_and_add(const unsigned int from_level, VectorType &dst, const VectorType &src) const override
MGTransferMatrixFreeWrapper(const unsigned int min_h_level)
LinearAlgebra::distributed::Vector< MGNumber > VectorType
void build(const MGLevelObject< MGTwoLevelTransfer< dim, VectorType > > &transfers, const DoFHandler< dim > &dof_handler, const MGConstrainedDoFs &mg_constrained_dofs, const std::vector< std::shared_ptr< const Utilities::MPI::Partitioner > > &external_partitioners_in)
void interpolate_to_mg(const DoFHandler< dim > &dof_handler, MGLevelObject< VectorType > &dst, const InVectorType &src) const
void prolongate(const unsigned int to_level, VectorType &dst, const VectorType &src) const override
void copy_from_mg(const DoFHandler< dim, spacedim > &dof_handler, OutVector &dst, const MGLevelObject< VectorType > &src) const
MGTransferGlobalCoarsening< dim, VectorType > gc
void copy_to_mg(const DoFHandler< dim, spacedim > &dof_handler, MGLevelObject< VectorType > &dst, const InVector &src) const
DiagonalMatrix< VectorType > diagonal_matrix
DiagonalMatrix class from deal.II.
void vmult(VectorType &dst, const VectorType &src) const override
Apply preconditioner.
MyDiagonalMatrix(const VectorType &diagonal)
Constructor.
virtual void set_initial_condition(const Parameters::FluidDynamicsInitialConditionType initial_condition_type, const bool restart=false)
std::shared_ptr< LinearAlgebra::distributed::Vector< double > > present_solution
virtual void refine_mesh()
Allow the refinement of the mesh according to one of the 2 methods proposed.
std::shared_ptr< std::vector< LinearAlgebra::distributed::Vector< double > > > previous_solutions
NavierStokesBase(SimulationParameters< dim > &nsparam, const FluidDynamicsFormulation fluid_dynamics_formulation=FluidDynamicsFormulation::standard)
virtual void update_forcing_and_boundary_conditions()
Set the time of the forcing function and of the time-dependent boundary conditions to the current tim...
std::shared_ptr< AverageVelocities< dim, LinearAlgebra::distributed::Vector< double >, IndexSet > > average_velocities
std::shared_ptr< parallel::DistributedTriangulationBase< dim > > triangulation
Implements the matrix-free operator to solve steady/transient non-Newtonian Navier-Stokes equations u...
A class that serves as base for all the matrix-free Navier-Stokes operators.
PhysicsSolver(const Parameters::NonLinearSolver non_linear_solver_parameters)
An Additive Schwarz preconditioner.
VectorType dst_internal
Internal destination vector with correct ghosting.
void initialize(const DoFHandler< dim > &dof_handler, const GlobalSparseMatrixType &global_sparse_matrix, const GlobalSparsityPattern &global_sparsity_pattern, const AffineConstraints< Number > &constraints, const std::shared_ptr< const Utilities::MPI::Partitioner > &partitioner, const unsigned int mg_level)
Initialize inverses of blocks.
std::vector< std::vector< unsigned int > > patches
DoF indices of patches.
VectorType src_internal
Internal source vector with correct ghosting.
typename VectorType::value_type Number
Value type.
const WeightingType weighting_type
Weighting type.
void vmult(VectorType &dst, const VectorType &src) const override
Apply preconditioner.
std::vector< LAPACKFullMatrix< Number > > blocks
Inverse of patch matrices.
A wrapper class around a preconditioner to be able to handle vector types that are not supported by t...
const std::function< void(VectorTypePrecondition &, const VectorTypePrecondition &)> fu_preconditioner
Preconditioner.
void vmult(U &dst, const U &src) const
Apply preconditioner.
void vmult(VectorType &dst, const VectorType &src) const override
Apply preconditioner.
VectorTypePrecondition dst_
Destination vector supported by the preconditioner.
PreconditionAdapter(const std::shared_ptr< PreconditionType > &preconditioner)
VectorTypePrecondition src_
Source vector supported by the preconditioner.
A base class of preconditioners used by the smoother.
virtual void vmult(VectorType &dst, const VectorType &src) const =0
Apply preconditioner.
void timer_reset() const
Reset timers.
void timer_print() const
Print timers.
std::vector< double > a_ij
Parameters::Mortar< dim > mortar_parameters
Parameters::Stabilization stabilization
ObserverPointer< const PreconditionerType, MGCoarseGridApplyPreconditioner< VectorType, PreconditionerType > > preconditioner
void initialize(const PreconditionerType &precondition)
virtual void operator()(const unsigned int level, VectorType &dst, const VectorType &src) const override
void announce_string(const ConditionalOStream &pcout, const std::string &expression, const char delimiter='-')
Prints a string and then adds a line above and below made with dashes containing as many dashes as th...
Definition utilities.h:500
void read_mesh_and_manifolds_for_stator_and_rotor(parallel::DistributedTriangulationBase< dim, spacedim > &triangulation, const Parameters::Mesh< dim, spacedim > &mesh_parameters, const Parameters::Manifolds &manifolds_parameters, bool restart, const Parameters::PeriodicBoundaries &periodic_boundaries, const Parameters::Mortar< dim > &mortar_parameters)
Completely sets up a mesh and its manifolds for rotor-stator domains.
Definition grids.cc:501
void read_mesh_and_manifolds(parallel::DistributedTriangulationBase< dim, spacedim > &triangulation, const Parameters::Mesh< dim, spacedim > &mesh_parameters, const Parameters::Manifolds &manifolds_parameters, bool restart, const Parameters::PeriodicBoundaries &periodic_boundaries)
Completely sets up a mesh and its manifolds.
Definition grids.cc:447
Identifiers for multiphysics modules and their solution fields.
@ pressure
Pressure scalar field from fluid dynamics.
FluidDynamicsFormulation
Formulation of the fluid dynamics equations solved by a Navier-Stokes solver.
@ fluid_dynamics
Fluid Dynamics (Velocity,Pressure).
@ heat_transfer
Heat transfer (Temperature).
void rotate_mapping(const DoFHandler< dim > &dof_handler, MappingQCache< dim > &mapping_cache, const Mapping< dim > &mapping, const double rotation_angle, const Point< dim > &center_of_rotation=Point< dim >(), const unsigned int rotation_axis_direction=2)
Rotate the mapping according to the rotation angle in a rotor-stator configuration.
void read_checkpoint(TimerOutput &computing_timer, const DEMSolverParameters< dim > &parameters, std::shared_ptr< SimulationControl > &simulation_control, PVDHandler &particles_pvdhandler, PVDHandler &grid_pvdhandler, parallel::distributed::Triangulation< dim > &triangulation, Particles::ParticleHandler< dim > &particle_handler, std::shared_ptr< Insertion< dim, PropertiesIndex > > &insertion_object, std::vector< std::shared_ptr< SerialSolid< dim - 1, dim > > > &solid_surfaces, CheckpointControl &checkpoint_controller)
Read a DEM simulation checkpoint, allowing the simulation to restart from where it stopped.
SDIRKTable sdirk_table(const Parameters::SimulationControl::TimeSteppingMethod method)
Construct the Butcher tableau (A, b, c) for a given SDIRK method.
void establish_solid_domain(const dealii::DoFHandler< dim > &dof_handler, const DofsType &locally_owned_dofs, const bool non_zero_constraints, dealii::AffineConstraints< double > &constraints)
Turn regions of the mesh where the material_id>0 into a solid block by injecting velocity and pressur...
void establish_solid_domain_lsmg(const dealii::DoFHandler< dim > &dof_handler, const unsigned int level, const dealii::IndexSet &locally_owned_mg_dofs, const bool non_zero_constraints, dealii::AffineConstraints< double > &constraints)
Multigrid-level variant of establish_solid_domain for the local smoothing multigrid (LSMG) preconditi...
unsigned int velocity_degree
unsigned int pressure_degree
MultigridSmootherPreconditionerType
Type of preconditioner for the MG smoother.
@ lsmg
Local-Smoothing Geometric Multigrid.
@ ilu
Incomplete LU Factorization.
@ gcmg
Global-Coarsening Multigrid.
Structure representing a Butcher tableau for a SDIRK method.
GlobalVectorType VectorType
bool time_stepping_is_bdf(const Parameters::SimulationControl::TimeSteppingMethod method)
Determines if the time integration method is within the bdf family.
bool time_stepping_is_sdirk(const Parameters::SimulationControl::TimeSteppingMethod method)
Determines if the time integration method is within the sdirk family.
dealii::TrilinosWrappers::MPI::Vector GlobalVectorType
Definition vector.h:17
void convert_vector_trilinos_to_dealii(dealii::LinearAlgebra::distributed::Vector< number > &out, const dealii::TrilinosWrappers::MPI::Vector &in)
Helper function that allows to convert Trilinos vectors to deal.II vectors.
Definition vector.h:58
void convert_vector_dealii_to_trilinos(dealii::TrilinosWrappers::MPI::Vector &out, const dealii::LinearAlgebra::distributed::Vector< number > &in)
Helper function that allows to convert deal.II vectors to Trilinos vectors.
Definition vector.h:39