OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
MGPoissonSolver.h
Go to the documentation of this file.
1 // This class contains methods for solving Poisson's equation for the
3 // space charge portion of the calculation.
5 
6 #ifdef HAVE_SAAMG_SOLVER
7 
8 #ifndef MG_POISSON_SOLVER_H_
9 #define MG_POISSON_SOLVER_H_
10 
12 #include "PoissonSolver.h"
13 #include "IrregularDomain.h"
15 #include "ml_include.h"
16 
17 #ifdef HAVE_MPI
18 #include "mpi.h"
19 #include "Epetra_MpiComm.h"
20 #else
21 #include "Epetra_SerialComm.h"
22 #endif
23 
24 #if defined(HAVE_ML_EPETRA) && defined(HAVE_ML_TEUCHOS) && defined(HAVE_ML_AZTECOO)
25 
26 #include "Epetra_Map.h"
27 #include "Epetra_Vector.h"
28 #include "Epetra_CrsMatrix.h"
29 #include "Epetra_MpiComm.h"
30 
31 #pragma GCC diagnostic push
32 #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
33 #include "Teuchos_ParameterList.hpp"
34 // #include "BelosLinearProblem.hpp"
35 // #include "BelosRCGSolMgr.hpp"
36 
37 #include "Algorithms/PartBunch.h"
38 
39 #include "BelosTypes.hpp"
40 
41 namespace Belos {
42  template <class ScalarType, class MV, class OP>
43  class LinearProblem;
44 
45  template <class ScalarType, class MV, class OP>
46  class OperatorTraits;
47 
48  template<class ScalarType, class MV>
49  class MultiVecTraits;
50 
51  template<class ScalarType, class MV, class OP>
52  class SolverManager;
53 
54  class EpetraPrecOp;
55 
56  template <class ScalarType, class MV, class OP>
57  class StatusTestGenResNorm;
58 }
59 
60 namespace ML_Epetra {
61  class MultiLevelPreconditioner;
62 
63  int SetDefaults(std::string ProblemType, Teuchos::ParameterList & List,
64  int * options, double * params, const bool OverWrite);
65 }
66 
67 #pragma GCC diagnostic pop
68 
69 // using Teuchos::RCP;
70 // using Teuchos::rcp;
71 // using namespace ML_Epetra;
72 // using namespace Isorropia;
74 
78 typedef Cell Center_t;
81 
82 enum {
83  NO,
84  STD_PREC,
85  REUSE_PREC,
86  REUSE_HIERARCHY
87 };
88 
96 class BoundaryGeometry;
97 
98 class MGPoissonSolver : public PoissonSolver {
99 
100 public:
101  MGPoissonSolver(PartBunch *beam,Mesh_t *mesh, FieldLayout_t *fl, std::vector<BoundaryGeometry *> geometries, std::string itsolver, std::string interpl, double tol, int maxiters, std::string precmode);
102  ~MGPoissonSolver();
103 
108  void computePotential(Field_t &rho, Vector_t hr);
109  void computePotential(Field_t &rho, Vector_t hr, double zshift);
110 
112  void setGeometry(std::vector<BoundaryGeometry *> geometries);
113 
115  void recomputeMap() { hasParallelDecompositionChanged_m = true; }
116 
117  double getXRangeMin(unsigned short level) { return bp->getXRangeMin(); }
118  double getXRangeMax(unsigned short level) { return bp->getXRangeMax(); }
119  double getYRangeMin(unsigned short level) { return bp->getYRangeMin(); }
120  double getYRangeMax(unsigned short level) { return bp->getYRangeMax(); }
121  double getZRangeMin(unsigned short level) { return bp->getZRangeMin(); }
122  double getZRangeMax(unsigned short level) { return bp->getZRangeMax(); }
123  void test(PartBunchBase<double, 3> *bunch) { }
125  void printLoadBalanceStats();
126 
127  void extrapolateLHS();
128 
129  Inform &print(Inform &os) const;
130 
131 private:
132 
133  //TODO: we need to update this and maybe change attached
134  //solver!
136  BoundaryGeometry *currentGeometry;
137 
139  std::vector<BoundaryGeometry *> geometries_m;
140 
142  bool hasGeometryChanged_m;
144  bool hasParallelDecompositionChanged_m;
145  int repartFreq_m;
147  bool useRCB_m;
149  bool verbose_m;
150 
152  double tol_m;
154  int maxiters_m;
156  int itsolver_m;
158  int precmode_m;
160  int numIter_m;
162  int tolerableIterationsCount_m;
164  bool forcePreconditionerRecomputation_m;
166  int numBlocks_m;
168  int recycleBlocks_m;
169 
171  IrregularDomain *bp;
172 
174  Teuchos::RCP<Epetra_Vector> RHS;
176  Teuchos::RCP<Epetra_Vector> LHS;
178  Teuchos::RCP<Epetra_CrsMatrix> A;
179 
181  ML_Epetra::MultiLevelPreconditioner *MLPrec;
183  Epetra_Map *Map;
185  Epetra_MpiComm Comm;
186 
188  uint nLHS_m;
189  Teuchos::RCP<Epetra_MultiVector> P;
190  std::deque< Epetra_Vector > OldLHS;
191 
193  typedef double ST;
194  typedef Epetra_Operator OP;
195  typedef Epetra_MultiVector MV;
196  typedef Belos::OperatorTraits<ST, MV, OP> OPT;
197  typedef Belos::MultiVecTraits<ST, MV> MVT;
198 
199  //Belos::LinearProblem<double, MV, OP> problem;
200  typedef Belos::LinearProblem<ST, MV, OP> problem;
201  Teuchos::RCP< problem > problem_ptr;
202 
203  typedef Belos::SolverManager<ST, MV, OP> solver;
204  Teuchos::RCP< solver > solver_ptr;
205 
206  Teuchos::RCP< Belos::EpetraPrecOp > prec_m;
207  Teuchos::RCP< Belos::StatusTestGenResNorm< ST, MV, OP > > convStatusTest;
208 
210  Teuchos::ParameterList MLList_m;
212  Teuchos::ParameterList belosList;
213 
215  PartBunch *itsBunch_m;
216 
217  // mesh and layout objects for rho_m
218  Mesh_t *mesh_m;
219  FieldLayout_t *layout_m;
220 
221  // domains for the various fields
222  NDIndex<3> domain_m;
223 
225  Vector_t hr_m;
227  Vektor<int, 3> nr_m;
229  Vektor<int, 3> orig_nr_m;
230 
231  // timers
232  IpplTimings::TimerRef FunctionTimer1_m;
233  IpplTimings::TimerRef FunctionTimer2_m;
234  IpplTimings::TimerRef FunctionTimer3_m;
235  IpplTimings::TimerRef FunctionTimer4_m;
236  IpplTimings::TimerRef FunctionTimer5_m;
237  IpplTimings::TimerRef FunctionTimer6_m;
238  IpplTimings::TimerRef FunctionTimer7_m;
239  IpplTimings::TimerRef FunctionTimer8_m;
240 
241  void deletePtr();
242 
244  void computeMap(NDIndex<3> localId);
245 
248  void redistributeWithRCB(NDIndex<3> localId);
249 
252  void IPPLToMap3D(NDIndex<3> localId);
253 
260  void ComputeStencil(Vector_t hr, Teuchos::RCP<Epetra_Vector> RHS);
261 
262 
263 
264 protected:
265 
267  inline void SetupBelosList() {
268  belosList.set("Maximum Iterations", maxiters_m);
269  belosList.set("Convergence Tolerance", tol_m);
270 
271  if(numBlocks_m != 0 && recycleBlocks_m != 0){//only set if solver==RCGSolMgr
272  belosList.set("Num Blocks", numBlocks_m); // Maximum number of blocks in Krylov space
273  belosList.set("Num Recycled Blocks", recycleBlocks_m); // Number of vectors in recycle space
274  }
275  if(verbose_m) {
276  belosList.set("Verbosity", Belos::Errors + Belos::Warnings + Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails);
277  belosList.set("Output Frequency", 1);
278  } else
279  belosList.set("Verbosity", Belos::Errors + Belos::Warnings);
280  }
281 
283  inline void SetupMLList() {
284  ML_Epetra::SetDefaults("SA", MLList_m, 0, 0, true);
285 
286  MLList_m.set("max levels", 8);
287  MLList_m.set("increasing or decreasing", "increasing");
288 
289  // we use a V-cycle
290  MLList_m.set("prec type", "MGV");
291 
292  // uncoupled aggregation is used (every processor aggregates
293  // only local data)
294  MLList_m.set("aggregation: type", "Uncoupled");
295 
296  // smoother related parameters
297  MLList_m.set("smoother: type","Chebyshev");
298  MLList_m.set("smoother: sweeps", 3);
299  MLList_m.set("smoother: pre or post", "both");
300 
301  // on the coarsest level we solve with Tim Davis' implementation of
302  // Gilbert-Peierl's left-looking sparse partial pivoting algorithm,
303  // with Eisenstat & Liu's symmetric pruning. Gilbert's version appears
304  // as \c [L,U,P]=lu(A) in MATLAB. It doesn't exploit dense matrix
305  // kernels, but it is the only sparse LU factorization algorithm known to be
306  // asymptotically optimal, in the sense that it takes time proportional to the
307  // number of floating-point operations.
308  MLList_m.set("coarse: type", "Amesos-KLU");
309 
310  //XXX: or use Chebyshev coarse level solver
311  // SEE PAPER FOR EVALUATION KLU vs. Chebyshev
312  //MLList.set("coarse: sweeps", 10);
313  //MLList.set("coarse: type", "Chebyshev");
314 
315  // Controls the amount of printed information.
316  // Ranges from 0 to 10 (0 is no output, and
317  // 10 is incredibly detailed output). Default: 0
318  if(verbose_m)
319  MLList_m.set("ML output", 10);
320 
321  // heuristic for max coarse size depending on number of processors
322  int coarsest_size = std::max(Comm.NumProc() * 10, 1024);
323  MLList_m.set("coarse: max size", coarsest_size);
324 
325  }
326 
327 };
328 
329 
330 
331 inline Inform &operator<<(Inform &os, const MGPoissonSolver &fs) {
332  return fs.print(os);
333 }
334 
335 #else
336 
337 #include <stdlib.h>
338 #include <stdio.h>
339 #ifdef HAVE_MPI
340 #include "mpi.h"
341 #endif
342 
343 int main(int argc, char *argv[]) {
344 #ifdef HAVE_MPI
345  MPI_Init(&argc, &argv);
346 #endif
347 
348  puts("Please configure ML with:");
349  puts("--enable-epetra");
350  puts("--enable-teuchos");
351  puts("--enable-aztecoo");
352 
353 #ifdef HAVE_MPI
354  MPI_Finalize();
355 #endif
356 
357  return(EXIT_SUCCESS);
358 }
359 
360 #endif /* #if defined(HAVE_ML_EPETRA) && defined(HAVE_ML_TEUCHOS) && defined(HAVE_ML_AZTECOO) */
361 
362 #endif /* #ifndef MG_POISSON_SOLVER_H_ */
363 
364 #endif /* #ifdef HAVE_SAAMG_SOLVER */
std::ostream & operator<<(std::ostream &os, const Attribute &attr)
Definition: Attribute.cpp:167
Field< double, 3, Mesh_t, Center_t > Field_t
Definition: PBunchDefs.h:49
virtual double getXRangeMin(unsigned short level=0)=0
virtual double getXRangeMax(unsigned short level=0)=0
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:123
CenteredFieldLayout< 3, Mesh_t, Center_t > FieldLayout_t
Definition: PBunchDefs.h:48
virtual double getZRangeMax(unsigned short level=0)=0
FRONT * fs
Definition: hypervolume.cpp:59
The LIST command.
Definition: List.h:32
Definition: Centering.h:31
Vektor< double, 3 > Vector_t
Definition: Vektor.h:6
virtual void computePotential(Field_t &rho, Vector_t hr)=0
virtual double getYRangeMin(unsigned short level=0)=0
virtual double getYRangeMax(unsigned short level=0)=0
FVps< double, 6 > Map
Definition: ThickMapper.cpp:67
int main(int argc, char *argv[])
Definition: Main.cpp:107
Particle Bunch.
Definition: PartBunch.h:30
virtual double getZRangeMin(unsigned short level=0)=0
Timing::TimerRef TimerRef
Definition: IpplTimings.h:176
ParticleSpatialLayout< double, 3, Mesh_t > Layout_t
Definition: PBunchDefs.h:43
Cell Center_t
Definition: PBunchDefs.h:46
Definition: Inform.h:41
UniformCartesian< 3, double > Mesh_t
Definition: PBunchDefs.h:41
virtual void test(PartBunchBase< double, 3 > *bunch)=0