ThunderEgg  1.0.0
PatchSolver.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) 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_ITERATIVE_PATCHSOLVER_H
22 #define THUNDEREGG_ITERATIVE_PATCHSOLVER_H
23 
29 #include <ThunderEgg/Domain.h>
30 #include <ThunderEgg/GMG/Level.h>
34 #include <ThunderEgg/PatchSolver.h>
35 #include <ThunderEgg/Vector.h>
36 #include <bitset>
37 #include <map>
38 
39 namespace ThunderEgg::Iterative {
45 template<int D>
47 {
48 private:
53  class SinglePatchOp : public Operator<D>
54  {
55  private:
59  std::shared_ptr<const PatchOperator<D>> op;
63  const PatchInfo<D>& pinfo;
64 
65  public:
72  SinglePatchOp(const PatchInfo<D>& pinfo, std::shared_ptr<const PatchOperator<D>> op)
73  : op(op)
74  , pinfo(pinfo)
75  {}
76  void apply(const Vector<D>& x, Vector<D>& b) const override
77  {
79  PatchView<double, D> b_view = b.getPatchView(0);
80  op->applySinglePatchWithInternalBoundaryConditions(pinfo, x_view, b_view);
81  }
87  SinglePatchOp* clone() const override { return new SinglePatchOp(*this); }
88  };
89 
93  std::shared_ptr<const Solver<D>> solver;
94 
98  std::shared_ptr<const PatchOperator<D>> op;
99 
103  bool continue_on_breakdown;
104 
105 public:
121  const PatchOperator<D>& op,
122  bool continue_on_breakdown = false)
124  , solver(solver.clone())
125  , op(op.clone())
126  , continue_on_breakdown(continue_on_breakdown)
127  {}
133  PatchSolver<D>* clone() const override { return new PatchSolver<D>(*this); }
134  void solveSinglePatch(const PatchInfo<D>& pinfo,
135  const PatchView<const double, D>& f_view,
136  const PatchView<double, D>& u_view) const override
137  {
138  SinglePatchOp single_op(pinfo, op);
139 
140  std::array<int, D + 1> f_lengths;
141  for (int i = 0; i < D + 1; i++) {
142  f_lengths[i] = f_view.getEnd()[i] + 1;
143  }
144  const Vector<D> f_single(Communicator(MPI_COMM_SELF),
145  { const_cast<double*>(&f_view[f_view.getGhostStart()]) },
146  f_view.getStrides(),
147  f_lengths,
148  this->getDomain().getNumGhostCells());
149 
150  std::array<int, D + 1> u_lengths;
151  for (int i = 0; i < D + 1; i++) {
152  u_lengths[i] = u_view.getEnd()[i] + 1;
153  }
154  Vector<D> u_single(Communicator(MPI_COMM_SELF),
155  { &u_view[u_view.getGhostStart()] },
156  u_view.getStrides(),
157  u_lengths,
158  this->getDomain().getNumGhostCells());
159 
160  Vector<D> f_copy = f_single.getZeroClone();
161  f_copy.copy(f_single);
162  op->modifyRHSForInternalBoundaryConditions(pinfo, u_view, f_copy.getPatchView(0));
163 
164  int iterations = 0;
165  try {
166  iterations = solver->solve(single_op, u_single, f_copy);
167  } catch (const BreakdownError& err) {
168  if (!continue_on_breakdown) {
169  throw err;
170  }
171  }
172  if (this->getDomain().hasTimer()) {
173  this->getDomain().getTimer()->addIntInfo("Iterations", iterations);
174  }
175  }
176 };
177 } // namespace ThunderEgg::Iterative
178 extern template class ThunderEgg::Iterative::PatchSolver<2>;
179 extern template class ThunderEgg::Iterative::PatchSolver<3>;
180 #endif
ThunderEgg::PatchSolver::getDomain
const Domain< D > & getDomain() const
Get the Domain object.
Definition: PatchSolver.h:84
ThunderEgg::PatchSolver::apply
virtual void apply(const Vector< D > &f, Vector< D > &u) const override
Solve all the patches in the domain, assuming zero boundary conditions for the patches.
Definition: PatchSolver.h:107
Vector.h
Vector class.
ThunderEgg::View< T, D+1 >::getStrides
const std::array< int, D > & getStrides() const
Get the strides of the patch in each direction.
Definition: View.h:203
ThunderEgg::PatchSolver
Solves the problem on the patches using a specified interface value.
Definition: PatchSolver.h:42
ThunderEgg::Iterative::BreakdownError
Breakdown exception for iterative methods.
Definition: BreakdownError.h:35
ThunderEgg::View< T, D+1 >::getGhostStart
const std::array< int, D > & getGhostStart() const
Get the coordinate of the first ghost cell element.
Definition: View.h:215
ThunderEgg::Iterative::PatchSolver::solveSinglePatch
void solveSinglePatch(const PatchInfo< D > &pinfo, const PatchView< const double, D > &f_view, const PatchView< double, D > &u_view) const override
Perform a single solve over a patch.
Definition: PatchSolver.h:134
ThunderEgg::Iterative::PatchSolver::PatchSolver
PatchSolver(const Iterative::Solver< D > &solver, const PatchOperator< D > &op, bool continue_on_breakdown=false)
Construct a new IterativePatchSolver object.
Definition: PatchSolver.h:120
PatchOperator.h
PatchOperator class.
Solver.h
Solver class.
Level.h
Level class.
ThunderEgg::Iterative
Iterative Solvers.
Definition: BiCGStab.h:34
ThunderEgg::Iterative::PatchSolver
Solves the patches using an iterative Solver on each patch.
Definition: PatchSolver.h:46
ThunderEgg::PatchView
View for accessing data of a patch. It supports variable striding.
Definition: PatchView.h:37
ThunderEgg
The ThunderEgg namespace.
Definition: BiLinearGhostFiller.h:31
ThunderEgg::Vector::copy
void copy(const Vector< D > &b)
copy the values of the other vector
Definition: Vector.h:443
ThunderEgg::View< T, D+1 >::getEnd
const std::array< int, D > & getEnd() const
Get the coordinate of the last element.
Definition: View.h:211
ThunderEgg::PatchInfo
Contains metadata for a patch.
Definition: PatchInfo.h:51
ThunderEgg::PatchSolver::getGhostFiller
const GhostFiller< D > & getGhostFiller() const
Get the GhostFiller object.
Definition: PatchSolver.h:90
Domain.h
Domain class.
ThunderEgg::PatchOperator
This is an Operator where derived classes only have to implement the two virtual functions that opera...
Definition: PatchOperator.h:40
ThunderEgg::Operator
Base class for operators.
Definition: Operator.h:37
ThunderEgg::Vector::getPatchView
PatchView< double, D > getPatchView(int patch_local_index)
Get the View objects for the specified patch index of View object will correspond to component index.
Definition: Vector.h:358
ThunderEgg::Communicator
wrapper arount MPI_Comm, provides proper copy operators. Classes that have a communicator are meant t...
Definition: Communicator.h:36
ThunderEgg::Iterative::Solver
Abstract interface for Iterative solvers.
Definition: Solver.h:39
ThunderEgg::Vector
Vector class for use in thunderegg.
Definition: Vector.h:42
ThunderEgg::Vector::getZeroClone
Vector< D > getZeroClone() const
Get a vector of the same length initialized to zero.
Definition: Vector.h:601
PatchSolver.h
PatchSolver class.
BreakdownError.h
BreakdownError struct.
ThunderEgg::Iterative::PatchSolver::clone
PatchSolver< D > * clone() const override
Clone this patch solver.
Definition: PatchSolver.h:133