Debian Codegraph @

include/deal.II/multigrid/multigrid.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 1999 - 2025 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_multigrid_h
16 #define dealii_multigrid_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/enable_observer_pointer.h>
22 #include <deal.II/base/mg_level_object.h>
23 #include <deal.II/base/observer_pointer.h>
24
25 #include <deal.II/distributed/tria.h>
26
27 #include <deal.II/dofs/dof_handler.h>
28
29 #include <deal.II/lac/sparse_matrix.h>
30 #include <deal.II/lac/vector.h>
31
32 #include <deal.II/multigrid/mg_base.h>
33
34 #include <boost/signals2.hpp>
35
36 #include <vector>
37
38
39 DEAL_II_NAMESPACE_OPEN
40
41 #ifdef signals
42 # error \
43 "The name 'signals' is already defined. You are most likely using the QT library \
44 and using the 'signals' keyword. You can either #include the Qt headers (or any conflicting headers) \
45 *after* the deal.II headers or you can define the 'QT_NO_KEYWORDS' macro and use the 'Q_SIGNALS' macro."
46 #endif
47
48 /**
49 * @addtogroup mg
50 * @{
51 */
52
53 namespace mg
54 {
55 /**
56 * A structure containing boost::signal objects for optional processing in
57 * multigrid solvers.
58 *
59 * Each of these signals is called twice, once before and once after
60 * the action is performed. The two function calls differ in the
61 * boolean argument @p before, which is true the first time and
62 * false the second.
63 */
64 struct Signals
65 {
66 /**
67 * This signal is triggered before (@p before is true) and after (@p before
68 * is false) the call to MGTransfer::copy_to_mg which transfers the vector
69 * given to it to a multi-level vector.
70 */
71 boost::signals2::signal<void(const bool before)> transfer_to_mg;
72
73 /**
74 * This signal is triggered before (@p before is true) and after (@p before
75 * is false) the call to MGTransfer::copy_from_mg which transfers the
76 * multi-level vector given to it to a normal vector.
77 */
78 boost::signals2::signal<void(const bool before)> transfer_to_global;
79
80 /**
81 * This signal is triggered before (@p before is true) and after (@p before
82 * is
83 * false) the call to the coarse solver on @p level.
84 *
85 * The coarse solve will be done with ``defect[level]`` and returned in
86 * ``solution[level]``, which can be inspected by the user using this
87 * signal.
88 */
89 boost::signals2::signal<void(const bool before, const unsigned int level)>
90 coarse_solve;
91
92 /**
93 * This signal is triggered before (@p before is true) and after (@p before
94 * is false) the call to MGTransfer::restrict_and_add() which restricts a
95 * vector from @p level to the next coarser one (@p level - 1).
96 *
97 * The vector ``defect[level-1]`` will be updated between these two
98 * triggers and can be inspected by the user using this signal.
99 */
100 boost::signals2::signal<void(const bool before, const unsigned int level)>
101 restriction;
102
103 /**
104 * This signal is triggered before (@p before is true) and after (@p before
105 * is false) the call to MGTransfer::prolongate() which prolongs a vector to
106 * @p level from the next coarser one (@p level - 1).
107 */
108 boost::signals2::signal<void(const bool before, const unsigned int level)>
109 prolongation;
110
111 /**
112 * This signal is triggered before (@p before is true) and after (@p before
113 * is false) the call to a pre-smoothing step via MGPreSmoother::apply() on
114 * @p level.
115 *
116 * The smoother result will be stored in ``solution[level]`` and can be
117 * inspected by the user using this signal.
118 */
119 boost::signals2::signal<void(const bool before, const unsigned int level)>
120 pre_smoother_step;
121
122 /**
123 * This signal is triggered before (@p before is true) and after (@p before
124 * is false) the call to a post-smoothing step via MGPostSmoother::apply()
125 * on @p level.
126 */
127 boost::signals2::signal<void(const bool before, const unsigned int level)>
128 post_smoother_step;
129
130 /**
131 * This signal is triggered before (@p before is true) and after (@p before
132 * is false) the computation of the residual vector on @p level, including
133 * the result of edge_out and edge_down.
134 */
135 boost::signals2::signal<void(const bool before, const unsigned int level)>
136 residual_step;
137
138 /**
139 * This signal is triggered before (@p before is true) and after (@p before
140 * is false) the execution of edge_in and edge_up.
141 */
142 boost::signals2::signal<void(const bool before, const unsigned int level)>
143 edge_prolongation;
144 };
145 } // namespace mg
146
147 /**
148 * Implementation of the multigrid method. The implementation supports both
149 * continuous and discontinuous elements and follows the procedure described in
150 * the
151 * @ref mg_paper "multigrid paper by Janssen and Kanschat".
152 *
153 * The function which starts a multigrid cycle on the finest level is cycle().
154 * Depending on the cycle type chosen with the constructor (see enum Cycle),
155 * this function triggers one of the cycles level_v_step() or level_step(),
156 * where the latter one can do different types of cycles.
157 *
158 * Using this class, it is expected that the right hand side has been
159 * converted from a vector living on the locally finest level to a multilevel
160 * vector. This is a nontrivial operation, usually initiated automatically by
161 * the class PreconditionMG and performed by the classes derived from
162 * MGTransferBase.
163 */
164 template <typename VectorType>
165 class Multigrid : public EnableObserverPointer
166 {
167 public:
168 /**
169 * List of implemented cycle types.
170 */
171 enum Cycle
172 {
173 /// The V-cycle
174 v_cycle,
175 /// The W-cycle
176 w_cycle,
177 /// The F-cycle
178 f_cycle
179 };
180
181 using vector_type = VectorType;
182 using const_vector_type = const VectorType;
183
184 /**
185 * Constructor. <tt>transfer</tt> is an object performing prolongation and
186 * restriction. For levels in [minlevel, maxlevel] matrix has to contain
187 * valid matrices. By default the maxlevel is set to the maximal valid level.
188 *
189 * This function already initializes the vectors which will be used later in
190 * the course of the computations. You should therefore create objects of
191 * this type as late as possible.
192 */
193 Multigrid(const MGMatrixBase<VectorType> &matrix,
194 const MGCoarseGridBase<VectorType> &coarse,
195 const MGTransferBase<VectorType> &transfer,
196 const MGSmootherBase<VectorType> &pre_smooth,
197 const MGSmootherBase<VectorType> &post_smooth,
198 const unsigned int minlevel = 0,
199 const unsigned int maxlevel = numbers::invalid_unsigned_int,
200 Cycle cycle = v_cycle);
201
202 /**
203 * Reinit this class according to #minlevel and #maxlevel.
204 */
205 void
206 reinit(const unsigned int minlevel, const unsigned int maxlevel);
207
208 /**
209 * Execute one multigrid cycle. The type of cycle is selected by the
210 * constructor argument cycle. See the enum Cycle for available types.
211 */
212 void
213 cycle();
214
215 /**
216 * Execute one step of the V-cycle algorithm. This function assumes, that
217 * the multilevel vector #defect is filled with the residual of an outer
218 * defect correction scheme. This is usually taken care of by
219 * PreconditionMG). After vcycle(), the result is in the multilevel vector
220 * #solution. See <tt>copy_*_mg</tt> in the MGTools namespace if you want to
221 * use these vectors yourself.
222 *
223 * The actual work for this function is done in level_v_step().
224 */
225 void
226 vcycle();
227
228 /**
229 * Set additional matrices to correct residual computation at refinement
230 * edges. Since we only smoothen in the interior of the refined part of the
231 * mesh, the coupling across the refinement edge is missing. This coupling
232 * is provided by these two matrices.
233 *
234 * @note While <tt>edge_out.vmult</tt> is used, for the second argument, we
235 * use <tt>edge_in.Tvmult</tt>. Thus, <tt>edge_in</tt> should be assembled
236 * in transposed form. This saves a second sparsity pattern for
237 * <tt>edge_in</tt>. In particular, for symmetric operators, both arguments
238 * can refer to the same matrix, saving assembling of one of them.
239 */
240 void
241 set_edge_matrices(const MGMatrixBase<VectorType> &edge_out,
242 const MGMatrixBase<VectorType> &edge_in);
243
244 /**
245 * Similar to the function above: however, only @p edge_in is set. This
246 * is useful if the matrix attached to this class ignores the edge
247 * constraints during vmult(), which is only used during the computation
248 * of the residual.
249 */
250 void
251 set_edge_in_matrix(const MGMatrixBase<VectorType> &edge_in);
252
253 /**
254 * Set additional matrices to correct residual computation at refinement
255 * edges. These matrices originate from discontinuous Galerkin methods (see
256 * FE_DGQ etc.), where they correspond to the edge fluxes at the refinement
257 * edge between two levels.
258 *
259 * @note While <tt>edge_down.vmult</tt> is used, for the second argument, we
260 * use <tt>edge_up.Tvmult</tt>. Thus, <tt>edge_up</tt> should be assembled
261 * in transposed form. This saves a second sparsity pattern for
262 * <tt>edge_up</tt>. In particular, for symmetric operators, both arguments
263 * can refer to the same matrix, saving assembling of one of them.
264 */
265 void
266 set_edge_flux_matrices(const MGMatrixBase<VectorType> &edge_down,
267 const MGMatrixBase<VectorType> &edge_up);
268
269 /**
270 * Return the finest level for multigrid.
271 */
272 unsigned int
273 get_maxlevel() const;
274
275 /**
276 * Return the coarsest level for multigrid.
277 */
278 unsigned int
279 get_minlevel() const;
280
281 /**
282 * Set the highest level for which the multilevel method is performed. By
283 * default, this is the finest level of the Triangulation. Accepted are
284 * values not smaller than the current #minlevel.
285 */
286 void
287 set_maxlevel(const unsigned int);
288
289 /**
290 * Set the coarsest level for which the multilevel method is performed. By
291 * default, this is zero. Accepted are non-negative values not larger than
292 * the current #maxlevel.
293 *
294 * If <tt>relative</tt> is <tt>true</tt>, then this function determines the
295 * number of levels used, that is, it sets #minlevel to
296 * #maxlevel-<tt>level</tt>.
297 *
298 * @note The mesh on the coarsest level must cover the whole domain. There
299 * may not be hanging nodes on #minlevel.
300 *
301 * @note If #minlevel is set to a nonzero value, do not forget to adjust
302 * your coarse grid solver!
303 */
304 void
305 set_minlevel(const unsigned int level, bool relative = false);
306
307 /**
308 * Chance #cycle_type used in cycle().
309 */
310 void set_cycle(Cycle);
311
312 /**
313 * Connect a function to mg::Signals::pre_smoother_step.
314 */
315 boost::signals2::connection
316 connect_pre_smoother_step(
317 const std::function<void(const bool, const unsigned int)> &slot);
318
319 /**
320 * Connect a function to mg::Signals::residual_step.
321 */
322 boost::signals2::connection
323 connect_residual_step(
324 const std::function<void(const bool, const unsigned int)> &slot);
325
326 /**
327 * Connect a function to mg::Signals::restriction.
328 */
329 boost::signals2::connection
330 connect_restriction(
331 const std::function<void(const bool, const unsigned int)> &slot);
332
333 /**
334 * Connect a function to mg::Signals::coarse_solve.
335 */
336 boost::signals2::connection
337 connect_coarse_solve(
338 const std::function<void(const bool, const unsigned int)> &slot);
339
340 /**
341 * Connect a function to mg::Signals::prolongation.
342 */
343 boost::signals2::connection
344 connect_prolongation(
345 const std::function<void(const bool, const unsigned int)> &slot);
346
347 /**
348 * Connect a function to mg::Signals::edge_prolongation.
349 */
350 boost::signals2::connection
351 connect_edge_prolongation(
352 const std::function<void(const bool, const unsigned int)> &slot);
353
354 /**
355 * Connect a function to mg::Signals::post_smoother_step.
356 */
357 boost::signals2::connection
358 connect_post_smoother_step(
359 const std::function<void(const bool, const unsigned int)> &slot);
360
361 private:
362 /**
363 * Signals for the various actions that the Multigrid algorithm uses.
364 */
365 mg::Signals signals;
366
367 /**
368 * The V-cycle multigrid method. <tt>level</tt> is the level the function
369 * starts on. It will usually be called for the highest level from outside,
370 * but will then call itself recursively for <tt>level-1</tt>, unless we are
371 * on #minlevel where the coarse grid solver solves the problem exactly.
372 */
373 void
374 level_v_step(const unsigned int level);
375
376 /**
377 * The actual W-cycle or F-cycle multigrid method. <tt>level</tt> is the
378 * level the function starts on. It will usually be called for the highest
379 * level from outside, but will then call itself recursively for
380 * <tt>level-1</tt>, unless we are on #minlevel where the coarse grid solver
381 * solves the problem exactly.
382 */
383 void
384 level_step(const unsigned int level, Cycle cycle);
385
386 /**
387 * Cycle type performed by the method cycle().
388 */
389 Cycle cycle_type;
390
391 /**
392 * Level for coarse grid solution.
393 */
394 unsigned int minlevel;
395
396 /**
397 * Highest level of cells.
398 */
399 unsigned int maxlevel;
400
401 public:
402 /**
403 * Input vector for the cycle. Contains the defect of the outer method
404 * projected to the multilevel vectors.
405 */
406 MGLevelObject<VectorType> defect;
407
408 /**
409 * The solution update after the multigrid step.
410 */
411 MGLevelObject<VectorType> solution;
412
413 private:
414 /**
415 * Auxiliary vector.
416 */
417 MGLevelObject<VectorType> t;
418
419 /**
420 * Auxiliary vector for W- and F-cycles. Left uninitialized in V-cycle.
421 */
422 MGLevelObject<VectorType> defect2;
423
424
425 /**
426 * The matrix for each level.
427 */
428 ObserverPointer<const MGMatrixBase<VectorType>, Multigrid<VectorType>> matrix;
429
430 /**
431 * The matrix for each level.
432 */
433 ObserverPointer<const MGCoarseGridBase<VectorType>, Multigrid<VectorType>>
434 coarse;
435
436 /**
437 * Object for grid transfer.
438 */
439 ObserverPointer<const MGTransferBase<VectorType>, Multigrid<VectorType>>
440 transfer;
441
442 /**
443 * The pre-smoothing object.
444 */
445 ObserverPointer<const MGSmootherBase<VectorType>, Multigrid<VectorType>>
446 pre_smooth;
447
448 /**
449 * The post-smoothing object.
450 */
451 ObserverPointer<const MGSmootherBase<VectorType>, Multigrid<VectorType>>
452 post_smooth;
453
454 /**
455 * Edge matrix from the interior of the refined part to the refinement edge.
456 *
457 * @note Only <tt>vmult</tt> is used for these matrices.
458 */
459 ObserverPointer<const MGMatrixBase<VectorType>> edge_out;
460
461 /**
462 * Transpose edge matrix from the refinement edge to the interior of the
463 * refined part.
464 *
465 * @note Only <tt>Tvmult</tt> is used for these matrices.
466 */
467 ObserverPointer<const MGMatrixBase<VectorType>> edge_in;
468
469 /**
470 * Edge matrix from fine to coarse.
471 *
472 * @note Only <tt>vmult</tt> is used for these matrices.
473 */
474 ObserverPointer<const MGMatrixBase<VectorType>, Multigrid<VectorType>>
475 edge_down;
476
477 /**
478 * Transpose edge matrix from coarse to fine.
479 *
480 * @note Only <tt>Tvmult</tt> is used for these matrices.
481 */
482 ObserverPointer<const MGMatrixBase<VectorType>, Multigrid<VectorType>>
483 edge_up;
484
485 template <int dim, typename OtherVectorType, typename TransferType>
486 friend class PreconditionMG;
487 };
488
489
490 /**
491 * Multi-level preconditioner. Here, we collect all information needed for
492 * multi-level preconditioning and provide the standard interface for LAC
493 * iterative methods.
494 *
495 * Furthermore, it needs functions <tt>void copy_to_mg(const VectorType&)</tt>
496 * to store @p src in the right hand side of the multi-level method and
497 * <tt>void copy_from_mg(VectorType&)</tt> to store the result of the v-cycle
498 * in @p dst.
499 *
500 * If VectorType is in fact a block vector and the `TransferType` object
501 * supports use of a separate DoFHandler for each block, this class also allows
502 * to be initialized with a separate DoFHandler for each block.
503 */
504 template <int dim, typename VectorType, typename TransferType>
505 class PreconditionMG : public EnableObserverPointer
506 {
507 public:
508 /**
509 * Constructor. Arguments are the multigrid object, pre-smoother,
510 * post-smoother and coarse grid solver.
511 */
512 PreconditionMG(const DoFHandler<dim> &dof_handler,
513 Multigrid<VectorType> &mg,
514 const TransferType &transfer);
515
516 /**
517 * Same as above in case every component of a block vector
518 * uses its own DoFHandler.
519 */
520 PreconditionMG(const std::vector<const DoFHandler<dim> *> &dof_handler,
521 Multigrid<VectorType> &mg,
522 const TransferType &transfer);
523
524 /**
525 * Dummy function needed by other classes.
526 */
527 bool
528 empty() const;
529
530 /**
531 * Preconditioning operator. Calls the @p vcycle function of the @p MG
532 * object passed to the constructor.
533 *
534 * This is the operator used by LAC iterative solvers.
535 */
536 template <typename OtherVectorType>
537 void
538 vmult(OtherVectorType &dst, const OtherVectorType &src) const;
539
540 /**
541 * Preconditioning operator. Calls the @p vcycle function of the @p MG
542 * object passed to the constructor.
543 */
544 template <typename OtherVectorType>
545 void
546 vmult_add(OtherVectorType &dst, const OtherVectorType &src) const;
547
548 /**
549 * Transposed preconditioning operator.
550 *
551 * Not implemented, but the definition may be needed.
552 */
553 template <typename OtherVectorType>
554 void
555 Tvmult(OtherVectorType &dst, const OtherVectorType &src) const;
556
557 /**
558 * Transposed preconditioning operator.
559 *
560 * Not implemented, but the definition may be needed.
561 */
562 template <typename OtherVectorType>
563 void
564 Tvmult_add(OtherVectorType &dst, const OtherVectorType &src) const;
565
566 /**
567 * Return the partitioning of the range space of this preconditioner, i.e.,
568 * the partitioning of the vectors that are result from matrix-vector
569 * products. By default, the respective information for the first DoFHandler
570 * object are returned.
571 */
572 IndexSet
573 locally_owned_range_indices(const unsigned int block = 0) const;
574
575 /**
576 * Return the partitioning of the domain space of this preconditioner, i.e.,
577 * the partitioning of the vectors this matrix has to be multiplied with.
578 * By default, the respective information for the first DoFHandler object
579 * are returned.
580 */
581 IndexSet
582 locally_owned_domain_indices(const unsigned int block = 0) const;
583
584 /**
585 * Return the underlying MPI communicator.
586 */
587 MPI_Comm
588 get_mpi_communicator() const;
589
590 /**
591 * Connect a function to mg::Signals::transfer_to_mg.
592 */
593 boost::signals2::connection
594 connect_transfer_to_mg(const std::function<void(bool)> &slot);
595
596 /**
597 * Connect a function to mg::Signals::transfer_to_global.
598 */
599 boost::signals2::connection
600 connect_transfer_to_global(const std::function<void(bool)> &slot);
601
602 /**
603 * Return the Multigrid object passed to the constructor.
604 */
605 Multigrid<VectorType> &
606 get_multigrid();
607
608 /**
609 * Return the Multigrid object passed to the constructor.
610 */
611 const Multigrid<VectorType> &
612 get_multigrid() const;
613
614 private:
615 /**
616 * Associated @p DoFHandler.
617 */
618 std::vector<ObserverPointer<const DoFHandler<dim>,
619 PreconditionMG<dim, VectorType, TransferType>>>
620 dof_handler_vector;
621
622 /**
623 * Storage for the pointers to the DoFHandler objects
624 * without ObserverPointer wrapper.
625 */
626 std::vector<const DoFHandler<dim> *> dof_handler_vector_raw;
627
628 /**
629 * The multigrid object.
630 */
631 ObserverPointer<Multigrid<VectorType>,
632 PreconditionMG<dim, VectorType, TransferType>>
633 multigrid;
634
635 /**
636 * Object for grid transfer.
637 */
638 ObserverPointer<const TransferType,
639 PreconditionMG<dim, VectorType, TransferType>>
640 transfer;
641
642 /**
643 * Flag to indicate if the object is initialized with a single DoFHandler
644 * or with one for each block.
645 */
646 const bool uses_dof_handler_vector;
647
648 /**
649 * Signals used by this object
650 */
651 mg::Signals signals;
652 };
653
654 /** @} */
655
656 #ifndef DOXYGEN
657 /* --------------------------- inline functions --------------------- */
658
659
660 template <typename VectorType>
661 Multigrid<VectorType>::Multigrid(const MGMatrixBase<VectorType> &matrix,
662 const MGCoarseGridBase<VectorType> &coarse,
663 const MGTransferBase<VectorType> &transfer,
664 const MGSmootherBase<VectorType> &pre_smooth,
665 const MGSmootherBase<VectorType> &post_smooth,
666 const unsigned int min_level,
667 const unsigned int max_level,
668 Cycle cycle)
669 : cycle_type(cycle)
670 , matrix(&matrix, typeid(*this).name())
671 , coarse(&coarse, typeid(*this).name())
672 , transfer(&transfer, typeid(*this).name())
673 , pre_smooth(&pre_smooth, typeid(*this).name())
674 , post_smooth(&post_smooth, typeid(*this).name())
675 , edge_out(nullptr, typeid(*this).name())
676 , edge_in(nullptr, typeid(*this).name())
677 , edge_down(nullptr, typeid(*this).name())
678 , edge_up(nullptr, typeid(*this).name())
679 {
680 if (max_level == numbers::invalid_unsigned_int)
681 maxlevel = matrix.get_maxlevel();
682 else
683 maxlevel = max_level;
684 reinit(min_level, maxlevel);
685 }
686
687
688
689 template <typename VectorType>
690 inline unsigned int
691 Multigrid<VectorType>::get_maxlevel() const
692 {
693 return maxlevel;
694 }
695
696
697
698 template <typename VectorType>
699 inline unsigned int
700 Multigrid<VectorType>::get_minlevel() const
701 {
702 return minlevel;
703 }
704
705
706 /* --------------------------- inline functions --------------------- */
707
708
709 namespace internal
710 {
711 namespace PreconditionMGImplementation
712 {
713 template <int dim,
714 typename VectorType,
715 typename TransferType,
716 typename OtherVectorType>
717 std::enable_if_t<TransferType::supports_dof_handler_vector>
718 vmult(const std::vector<const DoFHandler<dim> *> &dof_handler_vector,
719 Multigrid<VectorType> &multigrid,
720 const TransferType &transfer,
721 OtherVectorType &dst,
722 const OtherVectorType &src,
723 const bool uses_dof_handler_vector,
724 const typename dealii::mg::Signals &signals,
725 int)
726 {
727 signals.transfer_to_mg(true);
728 if (uses_dof_handler_vector)
729 transfer.copy_to_mg(dof_handler_vector, multigrid.defect, src);
730 else
731 transfer.copy_to_mg(*dof_handler_vector[0], multigrid.defect, src);
732 signals.transfer_to_mg(false);
733
734 multigrid.cycle();
735
736 signals.transfer_to_global(true);
737 if (uses_dof_handler_vector)
738 transfer.copy_from_mg(dof_handler_vector, dst, multigrid.solution);
739 else
740 transfer.copy_from_mg(*dof_handler_vector[0], dst, multigrid.solution);
741 signals.transfer_to_global(false);
742 }
743
744 template <int dim,
745 typename VectorType,
746 typename TransferType,
747 typename OtherVectorType>
748 void
749 vmult(const std::vector<const DoFHandler<dim> *> &dof_handler_vector,
750 Multigrid<VectorType> &multigrid,
751 const TransferType &transfer,
752 OtherVectorType &dst,
753 const OtherVectorType &src,
754 const bool uses_dof_handler_vector,
755 const typename dealii::mg::Signals &signals,
756 ...)
757 {
758 (void)uses_dof_handler_vector;
759 Assert(!uses_dof_handler_vector, ExcInternalError());
760
761 signals.transfer_to_mg(true);
762 transfer.copy_to_mg(*dof_handler_vector[0], multigrid.defect, src);
763 signals.transfer_to_mg(false);
764
765 multigrid.cycle();
766
767 signals.transfer_to_global(true);
768 transfer.copy_from_mg(*dof_handler_vector[0], dst, multigrid.solution);
769 signals.transfer_to_global(false);
770 }
771
772 template <int dim,
773 typename VectorType,
774 typename TransferType,
775 typename OtherVectorType>
776 std::enable_if_t<TransferType::supports_dof_handler_vector>
777 vmult_add(const std::vector<const DoFHandler<dim> *> &dof_handler_vector,
778 Multigrid<VectorType> &multigrid,
779 const TransferType &transfer,
780 OtherVectorType &dst,
781 const OtherVectorType &src,
782 const bool uses_dof_handler_vector,
783 const typename dealii::mg::Signals &signals,
784 int)
785 {
786 signals.transfer_to_mg(true);
787 if (uses_dof_handler_vector)
788 transfer.copy_to_mg(dof_handler_vector, multigrid.defect, src);
789 else
790 transfer.copy_to_mg(*dof_handler_vector[0], multigrid.defect, src);
791 signals.transfer_to_mg(false);
792
793 multigrid.cycle();
794
795 signals.transfer_to_global(true);
796 if (uses_dof_handler_vector)
797 transfer.copy_from_mg_add(dof_handler_vector, dst, multigrid.solution);
798 else
799 transfer.copy_from_mg_add(*dof_handler_vector[0],
800 dst,
801 multigrid.solution);
802 signals.transfer_to_global(false);
803 }
804
805 template <int dim,
806 typename VectorType,
807 typename TransferType,
808 typename OtherVectorType>
809 void
810 vmult_add(const std::vector<const DoFHandler<dim> *> &dof_handler_vector,
811 Multigrid<VectorType> &multigrid,
812 const TransferType &transfer,
813 OtherVectorType &dst,
814 const OtherVectorType &src,
815 const bool uses_dof_handler_vector,
816 const typename dealii::mg::Signals &signals,
817 ...)
818 {
819 (void)uses_dof_handler_vector;
820 Assert(!uses_dof_handler_vector, ExcInternalError());
821
822 signals.transfer_to_mg(true);
823 transfer.copy_to_mg(*dof_handler_vector[0], multigrid.defect, src);
824 signals.transfer_to_mg(false);
825
826 multigrid.cycle();
827
828 signals.transfer_to_global(true);
829 transfer.copy_from_mg_add(*dof_handler_vector[0],
830 dst,
831 multigrid.solution);
832 signals.transfer_to_global(false);
833 }
834 } // namespace PreconditionMGImplementation
835 } // namespace internal
836
837 template <int dim, typename VectorType, typename TransferType>
838 PreconditionMG<dim, VectorType, TransferType>::PreconditionMG(
839 const DoFHandler<dim> &dof_handler,
840 Multigrid<VectorType> &mg,
841 const TransferType &transfer)
842 : dof_handler_vector(1, &dof_handler)
843 , dof_handler_vector_raw(1, &dof_handler)
844 , multigrid(&mg)
845 , transfer(&transfer)
846 , uses_dof_handler_vector(false)
847 {}
848
849 template <int dim, typename VectorType, typename TransferType>
850 PreconditionMG<dim, VectorType, TransferType>::PreconditionMG(
851 const std::vector<const DoFHandler<dim> *> &dof_handler,
852 Multigrid<VectorType> &mg,
853 const TransferType &transfer)
854 : dof_handler_vector(dof_handler.size())
855 , dof_handler_vector_raw(dof_handler.size())
856 , multigrid(&mg)
857 , transfer(&transfer)
858 , uses_dof_handler_vector(true)
859 {
860 for (unsigned int i = 0; i < dof_handler.size(); ++i)
861 {
862 dof_handler_vector[i] = dof_handler[i];
863 dof_handler_vector_raw[i] = dof_handler[i];
864 }
865 }
866
867 template <int dim, typename VectorType, typename TransferType>
868 inline bool
869 PreconditionMG<dim, VectorType, TransferType>::empty() const
870 {
871 return false;
872 }
873
874 template <int dim, typename VectorType, typename TransferType>
875 template <typename OtherVectorType>
876 void
877 PreconditionMG<dim, VectorType, TransferType>::vmult(
878 OtherVectorType &dst,
879 const OtherVectorType &src) const
880 {
881 internal::PreconditionMGImplementation::vmult(dof_handler_vector_raw,
882 *multigrid,
883 *transfer,
884 dst,
885 src,
886 uses_dof_handler_vector,
887 this->signals,
888 0);
889 }
890
891
892 template <int dim, typename VectorType, typename TransferType>
893 IndexSet
894 PreconditionMG<dim, VectorType, TransferType>::locally_owned_range_indices(
895 const unsigned int block) const
896 {
897 AssertIndexRange(block, dof_handler_vector.size());
898 return dof_handler_vector[block]->locally_owned_dofs();
899 }
900
901
902 template <int dim, typename VectorType, typename TransferType>
903 IndexSet
904 PreconditionMG<dim, VectorType, TransferType>::locally_owned_domain_indices(
905 const unsigned int block) const
906 {
907 AssertIndexRange(block, dof_handler_vector.size());
908 return dof_handler_vector[block]->locally_owned_dofs();
909 }
910
911
912
913 template <int dim, typename VectorType, typename TransferType>
914 MPI_Comm
915 PreconditionMG<dim, VectorType, TransferType>::get_mpi_communicator() const
916 {
917 // currently parallel GMG works with parallel triangulations only,
918 // so it should be a safe bet to use it to query MPI communicator:
919 const Triangulation<dim> &tria = dof_handler_vector[0]->get_triangulation();
920 const parallel::TriangulationBase<dim> *ptria =
921 dynamic_cast<const parallel::TriangulationBase<dim> *>(&tria);
922 Assert(ptria != nullptr, ExcInternalError());
923 return ptria->get_mpi_communicator();
924 }
925
926
927
928 template <int dim, typename VectorType, typename TransferType>
929 boost::signals2::connection
930 PreconditionMG<dim, VectorType, TransferType>::connect_transfer_to_mg(
931 const std::function<void(bool)> &slot)
932 {
933 return this->signals.transfer_to_mg.connect(slot);
934 }
935
936
937
938 template <int dim, typename VectorType, typename TransferType>
939 boost::signals2::connection
940 PreconditionMG<dim, VectorType, TransferType>::connect_transfer_to_global(
941 const std::function<void(bool)> &slot)
942 {
943 return this->signals.transfer_to_global.connect(slot);
944 }
945
946
947
948 template <int dim, typename VectorType, typename TransferType>
949 template <typename OtherVectorType>
950 void
951 PreconditionMG<dim, VectorType, TransferType>::vmult_add(
952 OtherVectorType &dst,
953 const OtherVectorType &src) const
954 {
955 internal::PreconditionMGImplementation::vmult_add(dof_handler_vector_raw,
956 *multigrid,
957 *transfer,
958 dst,
959 src,
960 uses_dof_handler_vector,
961 this->signals,
962 0);
963 }
964
965
966 template <int dim, typename VectorType, typename TransferType>
967 template <typename OtherVectorType>
968 void
969 PreconditionMG<dim, VectorType, TransferType>::Tvmult(
970 OtherVectorType &,
971 const OtherVectorType &) const
972 {
973 DEAL_II_NOT_IMPLEMENTED();
974 }
975
976
977 template <int dim, typename VectorType, typename TransferType>
978 template <typename OtherVectorType>
979 void
980 PreconditionMG<dim, VectorType, TransferType>::Tvmult_add(
981 OtherVectorType &,
982 const OtherVectorType &) const
983 {
984 DEAL_II_NOT_IMPLEMENTED();
985 }
986
987
988 template <int dim, typename VectorType, typename TransferType>
989 Multigrid<VectorType> &
990 PreconditionMG<dim, VectorType, TransferType>::get_multigrid()
991 {
992 return *this->multigrid;
993 }
994
995
996 template <int dim, typename VectorType, typename TransferType>
997 const Multigrid<VectorType> &
998 PreconditionMG<dim, VectorType, TransferType>::get_multigrid() const
999 {
1000 return *this->multigrid;
1001 }
1002
1003 #endif // DOXYGEN
1004
1005 DEAL_II_NAMESPACE_CLOSE
1006
1007 #endif