ThunderEgg  1.0.0
MPIRestrictor.h
Go to the documentation of this file.
1 /***************************************************************************
2  * ThunderEgg, a library for solvers on adaptively refined block-structured
3  * Cartesian grids.
4  *
5  * Copyright (c) 2020-2021 Scott Aiton
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  ***************************************************************************/
20 
21 #ifndef THUNDEREGG_GMG_MPIRESTRICTOR_H
22 #define THUNDEREGG_GMG_MPIRESTRICTOR_H
23 
28 #include <ThunderEgg/Domain.h>
30 #include <ThunderEgg/GMG/Level.h>
32 namespace ThunderEgg::GMG {
37 template<int D>
38 class MPIRestrictor : public Restrictor<D>
39 {
40 private:
44  mutable InterLevelComm<D> ilc;
45 
46 public:
52  MPIRestrictor(const Domain<D>& coarser_domain, const Domain<D>& finer_domain)
53  : ilc(coarser_domain, finer_domain)
54  {}
55  Vector<D> restrict(const Vector<D>& fine) const override
56  {
57  if constexpr (ENABLE_DEBUG) {
58  if (fine.getNumLocalPatches() != ilc.getFinerDomain().getNumLocalPatches()) {
59  throw RuntimeError("fine vector is incorrect length. Expected Length of " +
60  std::to_string(ilc.getFinerDomain().getNumLocalPatches()) +
61  " but vector was length " + std::to_string(fine.getNumLocalPatches()));
62  }
63  }
64  Vector<D> coarse(ilc.getCoarserDomain(), fine.getNumComponents());
65  Vector<D> coarse_ghost = ilc.getNewGhostVector(fine.getNumComponents());
66 
67  // fill in ghost values
68  restrictPatches(ilc.getPatchesWithGhostParent(), fine, coarse_ghost);
69 
70  // clear values in coarse vector
71  coarse.setWithGhost(0);
72 
73  // start scatter for ghost values
74  ilc.sendGhostPatchesStart(coarse, coarse_ghost);
75 
76  // fill in local values
77  restrictPatches(ilc.getPatchesWithLocalParent(), fine, coarse);
78 
79  // finish scatter for ghost values
80  ilc.sendGhostPatchesFinish(coarse, coarse_ghost);
81 
82  return coarse;
83  }
96  virtual void restrictPatches(
97  const std::vector<std::pair<int, std::reference_wrapper<const PatchInfo<D>>>>& patches,
98  const Vector<D>& finer_vector,
99  Vector<D>& coarser_vector) const = 0;
100 };
101 } // namespace ThunderEgg::GMG
102 // explicit instantiation
103 extern template class ThunderEgg::GMG::MPIRestrictor<2>;
104 extern template class ThunderEgg::GMG::MPIRestrictor<3>;
105 #endif
ThunderEgg::GMG::InterLevelComm
Facilitates communication between a finer domain and a coarser domain.
Definition: InterLevelComm.h:52
ThunderEgg::GMG::InterLevelComm::getPatchesWithGhostParent
const std::vector< std::pair< int, std::reference_wrapper< const PatchInfo< D > > > > & getPatchesWithGhostParent() const
Get the vector of finer patches that have a ghost parent.
Definition: InterLevelComm.h:276
ThunderEgg::GMG::Restrictor
Abstract class for restriction operators.
Definition: Restrictor.h:36
ThunderEgg::Domain
Uses a collection of PatchInfo objects to represent the domain of the problem.
Definition: Domain.h:50
ThunderEgg::GMG::MPIRestrictor::restrict
Vector< D > restrict(const Vector< D > &fine) const override
Definition: MPIRestrictor.h:55
Level.h
Level class.
ThunderEgg::GMG::InterLevelComm::sendGhostPatchesFinish
void sendGhostPatchesFinish(Vector< D > &vector, const Vector< D > &ghost_vector)
Finish the communication for sending ghost values.
Definition: InterLevelComm.h:372
ThunderEgg::GMG::MPIRestrictor
Base class that makes the necessary mpi calls, derived classes only have to implement restrictPatches...
Definition: MPIRestrictor.h:38
ThunderEgg::GMG::MPIRestrictor::restrictPatches
virtual void restrictPatches(const std::vector< std::pair< int, std::reference_wrapper< const PatchInfo< D >>>> &patches, const Vector< D > &finer_vector, Vector< D > &coarser_vector) const =0
Restrict values into coarse vector.
ThunderEgg::GMG::InterLevelComm::getFinerDomain
const Domain< D > & getFinerDomain() const
Get the finer Domain.
Definition: InterLevelComm.h:580
ThunderEgg::PatchInfo
Contains metadata for a patch.
Definition: PatchInfo.h:51
ThunderEgg::Vector::setWithGhost
void setWithGhost(double alpha)
set all values in the vector (including ghost cells)
Definition: Vector.h:404
InterLevelComm.h
InterLevelComm class.
Domain.h
Domain class.
ThunderEgg::GMG::InterLevelComm::getPatchesWithLocalParent
const std::vector< std::pair< int, std::reference_wrapper< const PatchInfo< D > > > > & getPatchesWithLocalParent() const
Get the vector of finer patches that have a local parent.
Definition: InterLevelComm.h:260
ThunderEgg::GMG::InterLevelComm::getCoarserDomain
const Domain< D > & getCoarserDomain() const
Get the coarser Domain.
Definition: InterLevelComm.h:574
ThunderEgg::GMG::InterLevelComm::sendGhostPatchesStart
void sendGhostPatchesStart(Vector< D > &vector, const Vector< D > &ghost_vector)
Start the communication for sending ghost values.
Definition: InterLevelComm.h:293
ThunderEgg::Vector
Vector class for use in thunderegg.
Definition: Vector.h:42
ThunderEgg::GMG
Geometric-Multigrid classes.
Definition: Cycle.h:33
ThunderEgg::Vector::getNumLocalPatches
int getNumLocalPatches() const
Get the number of local patches.
Definition: Vector.h:316
Restrictor.h
Restrictor class.
ThunderEgg::GMG::MPIRestrictor::MPIRestrictor
MPIRestrictor(const Domain< D > &coarser_domain, const Domain< D > &finer_domain)
Create new LinearRestrictor object.
Definition: MPIRestrictor.h:52
ThunderEgg::GMG::InterLevelComm::getNewGhostVector
Vector< D > getNewGhostVector(int num_components) const
Allocate a new vector for ghost patch values.
Definition: InterLevelComm.h:243
ThunderEgg::RuntimeError
ThunderEgg runtime exception.
Definition: RuntimeError.h:36