Reference documentation for deal.II version GIT d9d8a449a2 2022-08-17 08:45:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
multigrid.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2022 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_multigrid_h
17 #define dealii_multigrid_h
18 
19 
20 #include <deal.II/base/config.h>
21 
25 
27 
29 
31 #include <deal.II/lac/vector.h>
32 
34 
35 #include <vector>
36 
38 
39 #ifdef signals
40 # error \
41  "The name 'signals' is already defined. You are most likely using the QT library \
42 and using the 'signals' keyword. You can either #include the Qt headers (or any conflicting headers) \
43 *after* the deal.II headers or you can define the 'QT_NO_KEYWORDS' macro and use the 'Q_SIGNALS' macro."
44 #endif
45 
51 namespace mg
52 {
62  struct Signals
63  {
69  boost::signals2::signal<void(const bool before)> transfer_to_mg;
70 
76  boost::signals2::signal<void(const bool before)> transfer_to_global;
77 
87  boost::signals2::signal<void(const bool before, const unsigned int level)>
89 
98  boost::signals2::signal<void(const bool before, const unsigned int level)>
100 
106  boost::signals2::signal<void(const bool before, const unsigned int level)>
108 
117  boost::signals2::signal<void(const bool before, const unsigned int level)>
119 
125  boost::signals2::signal<void(const bool before, const unsigned int level)>
127 
133  boost::signals2::signal<void(const bool before, const unsigned int level)>
135 
140  boost::signals2::signal<void(const bool before, const unsigned int level)>
142  };
143 } // namespace mg
144 
162 template <typename VectorType>
163 class Multigrid : public Subscriptor
164 {
165 public:
169  enum Cycle
170  {
176  f_cycle
177  };
178 
179  using vector_type = VectorType;
180  using const_vector_type = const VectorType;
181 
196  const unsigned int minlevel = 0,
197  const unsigned int maxlevel = numbers::invalid_unsigned_int,
198  Cycle cycle = v_cycle);
199 
203  void
204  reinit(const unsigned int minlevel, const unsigned int maxlevel);
205 
210  void
211  cycle();
212 
223  void
225 
238  void
241 
248  void
250 
263  void
266 
270  unsigned int
271  get_maxlevel() const;
272 
276  unsigned int
277  get_minlevel() const;
278 
284  void
285  set_maxlevel(const unsigned int);
286 
302  void
303  set_minlevel(const unsigned int level, bool relative = false);
304 
309 
313  boost::signals2::connection
315  const std::function<void(const bool, const unsigned int)> &slot);
316 
320  boost::signals2::connection
322  const std::function<void(const bool, const unsigned int)> &slot);
323 
327  boost::signals2::connection
329  const std::function<void(const bool, const unsigned int)> &slot);
330 
334  boost::signals2::connection
336  const std::function<void(const bool, const unsigned int)> &slot);
337 
341  boost::signals2::connection
343  const std::function<void(const bool, const unsigned int)> &slot);
344 
348  boost::signals2::connection
350  const std::function<void(const bool, const unsigned int)> &slot);
351 
355  boost::signals2::connection
357  const std::function<void(const bool, const unsigned int)> &slot);
358 
359 private:
364 
371  void
372  level_v_step(const unsigned int level);
373 
381  void
382  level_step(const unsigned int level, Cycle cycle);
383 
388 
392  unsigned int minlevel;
393 
397  unsigned int maxlevel;
398 
399 public:
405 
410 
411 private:
416 
421 
422 
427 
433 
439 
445 
451 
458 
466 
473 
480 
481  template <int dim, class OtherVectorType, class TRANSFER>
482  friend class PreconditionMG;
483 };
484 
485 
500 template <int dim, typename VectorType, class TRANSFER>
502 {
503 public:
508  PreconditionMG(const DoFHandler<dim> &dof_handler,
510  const TRANSFER & transfer);
511 
516  PreconditionMG(const std::vector<const DoFHandler<dim> *> &dof_handler,
518  const TRANSFER & transfer);
519 
523  bool
524  empty() const;
525 
532  template <class OtherVectorType>
533  void
534  vmult(OtherVectorType &dst, const OtherVectorType &src) const;
535 
540  template <class OtherVectorType>
541  void
542  vmult_add(OtherVectorType &dst, const OtherVectorType &src) const;
543 
549  template <class OtherVectorType>
550  void
551  Tvmult(OtherVectorType &dst, const OtherVectorType &src) const;
552 
558  template <class OtherVectorType>
559  void
560  Tvmult_add(OtherVectorType &dst, const OtherVectorType &src) const;
561 
568  IndexSet
569  locally_owned_range_indices(const unsigned int block = 0) const;
570 
577  IndexSet
578  locally_owned_domain_indices(const unsigned int block = 0) const;
579 
583  MPI_Comm
585 
589  boost::signals2::connection
590  connect_transfer_to_mg(const std::function<void(bool)> &slot);
591 
595  boost::signals2::connection
596  connect_transfer_to_global(const std::function<void(bool)> &slot);
597 
603 
607  const Multigrid<VectorType> &
608  get_multigrid() const;
609 
610 private:
614  std::vector<SmartPointer<const DoFHandler<dim>,
617 
622  std::vector<const DoFHandler<dim> *> dof_handler_vector_raw;
623 
629 
635 
641 
646 };
647 
650 #ifndef DOXYGEN
651 /* --------------------------- inline functions --------------------- */
652 
653 
654 template <typename VectorType>
656  const MGCoarseGridBase<VectorType> &coarse,
657  const MGTransferBase<VectorType> & transfer,
658  const MGSmootherBase<VectorType> & pre_smooth,
659  const MGSmootherBase<VectorType> &post_smooth,
660  const unsigned int min_level,
661  const unsigned int max_level,
662  Cycle cycle)
663  : cycle_type(cycle)
664  , matrix(&matrix, typeid(*this).name())
665  , coarse(&coarse, typeid(*this).name())
666  , transfer(&transfer, typeid(*this).name())
667  , pre_smooth(&pre_smooth, typeid(*this).name())
668  , post_smooth(&post_smooth, typeid(*this).name())
669  , edge_out(nullptr, typeid(*this).name())
670  , edge_in(nullptr, typeid(*this).name())
671  , edge_down(nullptr, typeid(*this).name())
672  , edge_up(nullptr, typeid(*this).name())
673 {
674  if (max_level == numbers::invalid_unsigned_int)
675  maxlevel = matrix.get_maxlevel();
676  else
677  maxlevel = max_level;
678  reinit(min_level, maxlevel);
679 }
680 
681 
682 
683 template <typename VectorType>
684 inline unsigned int
686 {
687  return maxlevel;
688 }
689 
690 
691 
692 template <typename VectorType>
693 inline unsigned int
695 {
696  return minlevel;
697 }
698 
699 
700 /* --------------------------- inline functions --------------------- */
701 
702 
703 namespace internal
704 {
705  namespace PreconditionMGImplementation
706  {
707  template <int dim,
708  typename VectorType,
709  class TRANSFER,
710  typename OtherVectorType>
711  std::enable_if_t<TRANSFER::supports_dof_handler_vector>
712  vmult(
713  const std::vector<const ::DoFHandler<dim> *> &dof_handler_vector,
714  ::Multigrid<VectorType> & multigrid,
715  const TRANSFER & transfer,
716  OtherVectorType & dst,
717  const OtherVectorType & src,
718  const bool uses_dof_handler_vector,
719  const typename ::mg::Signals &signals,
720  int)
721  {
722  signals.transfer_to_mg(true);
723  if (uses_dof_handler_vector)
724  transfer.copy_to_mg(dof_handler_vector, multigrid.defect, src);
725  else
726  transfer.copy_to_mg(*dof_handler_vector[0], multigrid.defect, src);
727  signals.transfer_to_mg(false);
728 
729  multigrid.cycle();
730 
731  signals.transfer_to_global(true);
732  if (uses_dof_handler_vector)
733  transfer.copy_from_mg(dof_handler_vector, dst, multigrid.solution);
734  else
735  transfer.copy_from_mg(*dof_handler_vector[0], dst, multigrid.solution);
736  signals.transfer_to_global(false);
737  }
738 
739  template <int dim,
740  typename VectorType,
741  class TRANSFER,
742  typename OtherVectorType>
743  void
744  vmult(
745  const std::vector<const ::DoFHandler<dim> *> &dof_handler_vector,
746  ::Multigrid<VectorType> & multigrid,
747  const TRANSFER & transfer,
748  OtherVectorType & dst,
749  const OtherVectorType & src,
750  const bool uses_dof_handler_vector,
751  const typename ::mg::Signals &signals,
752  ...)
753  {
754  (void)uses_dof_handler_vector;
755  Assert(!uses_dof_handler_vector, ExcInternalError());
756 
757  signals.transfer_to_mg(true);
758  transfer.copy_to_mg(*dof_handler_vector[0], multigrid.defect, src);
759  signals.transfer_to_mg(false);
760 
761  multigrid.cycle();
762 
763  signals.transfer_to_global(true);
764  transfer.copy_from_mg(*dof_handler_vector[0], dst, multigrid.solution);
765  signals.transfer_to_global(false);
766  }
767 
768  template <int dim,
769  typename VectorType,
770  class TRANSFER,
771  typename OtherVectorType>
772  std::enable_if_t<TRANSFER::supports_dof_handler_vector>
773  vmult_add(
774  const std::vector<const ::DoFHandler<dim> *> &dof_handler_vector,
775  ::Multigrid<VectorType> & multigrid,
776  const TRANSFER & transfer,
777  OtherVectorType & dst,
778  const OtherVectorType & src,
779  const bool uses_dof_handler_vector,
780  const typename ::mg::Signals &signals,
781  int)
782  {
783  signals.transfer_to_mg(true);
784  if (uses_dof_handler_vector)
785  transfer.copy_to_mg(dof_handler_vector, multigrid.defect, src);
786  else
787  transfer.copy_to_mg(*dof_handler_vector[0], multigrid.defect, src);
788  signals.transfer_to_mg(false);
789 
790  multigrid.cycle();
791 
792  signals.transfer_to_global(true);
793  if (uses_dof_handler_vector)
794  transfer.copy_from_mg_add(dof_handler_vector, dst, multigrid.solution);
795  else
796  transfer.copy_from_mg_add(*dof_handler_vector[0],
797  dst,
798  multigrid.solution);
799  signals.transfer_to_global(false);
800  }
801 
802  template <int dim,
803  typename VectorType,
804  class TRANSFER,
805  typename OtherVectorType>
806  void
807  vmult_add(
808  const std::vector<const ::DoFHandler<dim> *> &dof_handler_vector,
809  ::Multigrid<VectorType> & multigrid,
810  const TRANSFER & transfer,
811  OtherVectorType & dst,
812  const OtherVectorType & src,
813  const bool uses_dof_handler_vector,
814  const typename ::mg::Signals &signals,
815  ...)
816  {
817  (void)uses_dof_handler_vector;
818  Assert(!uses_dof_handler_vector, ExcInternalError());
819 
820  signals.transfer_to_mg(true);
821  transfer.copy_to_mg(*dof_handler_vector[0], multigrid.defect, src);
822  signals.transfer_to_mg(false);
823 
824  multigrid.cycle();
825 
826  signals.transfer_to_global(true);
827  transfer.copy_from_mg_add(*dof_handler_vector[0],
828  dst,
829  multigrid.solution);
830  signals.transfer_to_global(false);
831  }
832  } // namespace PreconditionMGImplementation
833 } // namespace internal
834 
835 template <int dim, typename VectorType, class TRANSFER>
837  const DoFHandler<dim> &dof_handler,
839  const TRANSFER & transfer)
840  : dof_handler_vector(1, &dof_handler)
841  , dof_handler_vector_raw(1, &dof_handler)
842  , multigrid(&mg)
843  , transfer(&transfer)
844  , uses_dof_handler_vector(false)
845 {}
846 
847 template <int dim, typename VectorType, class TRANSFER>
849  const std::vector<const DoFHandler<dim> *> &dof_handler,
851  const TRANSFER & transfer)
852  : dof_handler_vector(dof_handler.size())
853  , dof_handler_vector_raw(dof_handler.size())
854  , multigrid(&mg)
855  , transfer(&transfer)
856  , uses_dof_handler_vector(true)
857 {
858  for (unsigned int i = 0; i < dof_handler.size(); ++i)
859  {
860  dof_handler_vector[i] = dof_handler[i];
861  dof_handler_vector_raw[i] = dof_handler[i];
862  }
863 }
864 
865 template <int dim, typename VectorType, class TRANSFER>
866 inline bool
868 {
869  return false;
870 }
871 
872 template <int dim, typename VectorType, class TRANSFER>
873 template <class OtherVectorType>
874 void
876  OtherVectorType & dst,
877  const OtherVectorType &src) const
878 {
879  internal::PreconditionMGImplementation::vmult(dof_handler_vector_raw,
880  *multigrid,
881  *transfer,
882  dst,
883  src,
884  uses_dof_handler_vector,
885  this->signals,
886  0);
887 }
888 
889 
890 template <int dim, typename VectorType, class TRANSFER>
891 IndexSet
893  const unsigned int block) const
894 {
895  AssertIndexRange(block, dof_handler_vector.size());
896  return dof_handler_vector[block]->locally_owned_dofs();
897 }
898 
899 
900 template <int dim, typename VectorType, class TRANSFER>
901 IndexSet
903  const unsigned int block) const
904 {
905  AssertIndexRange(block, dof_handler_vector.size());
906  return dof_handler_vector[block]->locally_owned_dofs();
907 }
908 
909 
910 
911 template <int dim, typename VectorType, class TRANSFER>
912 MPI_Comm
914 {
915  // currently parallel GMG works with parallel triangulations only,
916  // so it should be a safe bet to use it to query MPI communicator:
917  const Triangulation<dim> &tria = dof_handler_vector[0]->get_triangulation();
918  const parallel::TriangulationBase<dim> *ptria =
919  dynamic_cast<const parallel::TriangulationBase<dim> *>(&tria);
920  Assert(ptria != nullptr, ExcInternalError());
921  return ptria->get_communicator();
922 }
923 
924 
925 
926 template <int dim, typename VectorType, class TRANSFER>
927 boost::signals2::connection
929  const std::function<void(bool)> &slot)
930 {
931  return this->signals.transfer_to_mg.connect(slot);
932 }
933 
934 
935 
936 template <int dim, typename VectorType, class TRANSFER>
937 boost::signals2::connection
939  const std::function<void(bool)> &slot)
940 {
941  return this->signals.transfer_to_global.connect(slot);
942 }
943 
944 
945 
946 template <int dim, typename VectorType, class TRANSFER>
947 template <class OtherVectorType>
948 void
950  OtherVectorType & dst,
951  const OtherVectorType &src) const
952 {
953  internal::PreconditionMGImplementation::vmult_add(dof_handler_vector_raw,
954  *multigrid,
955  *transfer,
956  dst,
957  src,
958  uses_dof_handler_vector,
959  this->signals,
960  0);
961 }
962 
963 
964 template <int dim, typename VectorType, class TRANSFER>
965 template <class OtherVectorType>
966 void
968  const OtherVectorType &) const
969 {
970  Assert(false, ExcNotImplemented());
971 }
972 
973 
974 template <int dim, typename VectorType, class TRANSFER>
975 template <class OtherVectorType>
976 void
978  OtherVectorType &,
979  const OtherVectorType &) const
980 {
981  Assert(false, ExcNotImplemented());
982 }
983 
984 
985 template <int dim, typename VectorType, class TRANSFER>
988 {
989  return *this->multigrid;
990 }
991 
992 
993 template <int dim, typename VectorType, class TRANSFER>
994 const Multigrid<VectorType> &
996 {
997  return *this->multigrid;
998 }
999 
1000 #endif // DOXYGEN
1001 
1003 
1004 #endif
@ v_cycle
The V-cycle.
Definition: multigrid.h:172
@ w_cycle
The W-cycle.
Definition: multigrid.h:174
@ f_cycle
The F-cycle.
Definition: multigrid.h:176
MGLevelObject< VectorType > defect2
Definition: multigrid.h:420
void cycle()
void set_edge_flux_matrices(const MGMatrixBase< VectorType > &edge_down, const MGMatrixBase< VectorType > &edge_up)
SmartPointer< const MGTransferBase< VectorType >, Multigrid< VectorType > > transfer
Definition: multigrid.h:438
unsigned int minlevel
Definition: multigrid.h:392
SmartPointer< const MGMatrixBase< VectorType >, Multigrid< VectorType > > edge_up
Definition: multigrid.h:479
mg::Signals signals
Definition: multigrid.h:363
SmartPointer< const MGMatrixBase< VectorType > > edge_in
Definition: multigrid.h:465
SmartPointer< const MGMatrixBase< VectorType > > edge_out
Definition: multigrid.h:457
MGLevelObject< VectorType > solution
Definition: multigrid.h:409
boost::signals2::connection connect_edge_prolongation(const std::function< void(const bool, const unsigned int)> &slot)
unsigned int maxlevel
Definition: multigrid.h:397
VectorType vector_type
Definition: multigrid.h:179
SmartPointer< const MGMatrixBase< VectorType >, Multigrid< VectorType > > matrix
Definition: multigrid.h:426
void set_maxlevel(const unsigned int)
unsigned int get_minlevel() const
void set_edge_matrices(const MGMatrixBase< VectorType > &edge_out, const MGMatrixBase< VectorType > &edge_in)
boost::signals2::connection connect_residual_step(const std::function< void(const bool, const unsigned int)> &slot)
void level_step(const unsigned int level, Cycle cycle)
void set_cycle(Cycle)
unsigned int get_maxlevel() const
const VectorType const_vector_type
Definition: multigrid.h:180
SmartPointer< const MGSmootherBase< VectorType >, Multigrid< VectorType > > pre_smooth
Definition: multigrid.h:444
SmartPointer< const MGSmootherBase< VectorType >, Multigrid< VectorType > > post_smooth
Definition: multigrid.h:450
boost::signals2::connection connect_post_smoother_step(const std::function< void(const bool, const unsigned int)> &slot)
boost::signals2::connection connect_coarse_solve(const std::function< void(const bool, const unsigned int)> &slot)
SmartPointer< const MGCoarseGridBase< VectorType >, Multigrid< VectorType > > coarse
Definition: multigrid.h:432
Cycle cycle_type
Definition: multigrid.h:387
boost::signals2::connection connect_prolongation(const std::function< void(const bool, const unsigned int)> &slot)
SmartPointer< const MGMatrixBase< VectorType >, Multigrid< VectorType > > edge_down
Definition: multigrid.h:472
void set_edge_in_matrix(const MGMatrixBase< VectorType > &edge_in)
MGLevelObject< VectorType > t
Definition: multigrid.h:415
void set_minlevel(const unsigned int level, bool relative=false)
void level_v_step(const unsigned int level)
Multigrid(const MGMatrixBase< VectorType > &matrix, const MGCoarseGridBase< VectorType > &coarse, const MGTransferBase< VectorType > &transfer, const MGSmootherBase< VectorType > &pre_smooth, const MGSmootherBase< VectorType > &post_smooth, const unsigned int minlevel=0, const unsigned int maxlevel=numbers::invalid_unsigned_int, Cycle cycle=v_cycle)
MGLevelObject< VectorType > defect
Definition: multigrid.h:404
void vcycle()
boost::signals2::connection connect_restriction(const std::function< void(const bool, const unsigned int)> &slot)
void reinit(const unsigned int minlevel, const unsigned int maxlevel)
boost::signals2::connection connect_pre_smoother_step(const std::function< void(const bool, const unsigned int)> &slot)
SmartPointer< Multigrid< VectorType >, PreconditionMG< dim, VectorType, TRANSFER > > multigrid
Definition: multigrid.h:628
void vmult_add(OtherVectorType &dst, const OtherVectorType &src) const
void vmult(OtherVectorType &dst, const OtherVectorType &src) const
IndexSet locally_owned_range_indices(const unsigned int block=0) const
SmartPointer< const TRANSFER, PreconditionMG< dim, VectorType, TRANSFER > > transfer
Definition: multigrid.h:634
PreconditionMG(const std::vector< const DoFHandler< dim > * > &dof_handler, Multigrid< VectorType > &mg, const TRANSFER &transfer)
std::vector< SmartPointer< const DoFHandler< dim >, PreconditionMG< dim, VectorType, TRANSFER > > > dof_handler_vector
Definition: multigrid.h:616
bool empty() const
void Tvmult_add(OtherVectorType &dst, const OtherVectorType &src) const
void Tvmult(OtherVectorType &dst, const OtherVectorType &src) const
boost::signals2::connection connect_transfer_to_mg(const std::function< void(bool)> &slot)
const Multigrid< VectorType > & get_multigrid() const
PreconditionMG(const DoFHandler< dim > &dof_handler, Multigrid< VectorType > &mg, const TRANSFER &transfer)
mg::Signals signals
Definition: multigrid.h:645
IndexSet locally_owned_domain_indices(const unsigned int block=0) const
boost::signals2::connection connect_transfer_to_global(const std::function< void(bool)> &slot)
const bool uses_dof_handler_vector
Definition: multigrid.h:640
MPI_Comm get_mpi_communicator() const
Multigrid< VectorType > & get_multigrid()
std::vector< const DoFHandler< dim > * > dof_handler_vector_raw
Definition: multigrid.h:622
Triangulation< dim, spacedim > & get_triangulation()
virtual MPI_Comm get_communicator() const override
Definition: tria_base.cc:144
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:442
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:443
unsigned int level
Definition: grid_out.cc:4608
static ::ExceptionBase & ExcInternalError()
#define Assert(cond, exc)
Definition: exceptions.h:1473
static ::ExceptionBase & ExcNotImplemented()
#define AssertIndexRange(index, range)
Definition: exceptions.h:1732
@ matrix
Contents is actually a matrix.
void reinit(MatrixBlock< MatrixType > &v, const BlockSparsityPattern &p)
Definition: matrix_block.h:618
Definition: mg.h:82
static const unsigned int invalid_unsigned_int
Definition: types.h:201
boost::signals2::signal< void(const bool before, const unsigned int level)> prolongation
Definition: multigrid.h:107
boost::signals2::signal< void(const bool before, const unsigned int level)> pre_smoother_step
Definition: multigrid.h:118
boost::signals2::signal< void(const bool before, const unsigned int level)> coarse_solve
Definition: multigrid.h:88
boost::signals2::signal< void(const bool before, const unsigned int level)> edge_prolongation
Definition: multigrid.h:141
boost::signals2::signal< void(const bool before, const unsigned int level)> post_smoother_step
Definition: multigrid.h:126
boost::signals2::signal< void(const bool before)> transfer_to_global
Definition: multigrid.h:76
boost::signals2::signal< void(const bool before, const unsigned int level)> residual_step
Definition: multigrid.h:134
boost::signals2::signal< void(const bool before, const unsigned int level)> restriction
Definition: multigrid.h:99
boost::signals2::signal< void(const bool before)> transfer_to_mg
Definition: multigrid.h:69
const ::Triangulation< dim, spacedim > & tria