mantaflow  0.10
A framework for fluid simulation
conjugategrad.h
Go to the documentation of this file.
1 
2 /******************************************************************************
3  *
4  * MantaFlow fluid solver framework
5  * Copyright 2011 Tobias Pfaff, Nils Thuerey
6  *
7  * This program is free software, distributed under the terms of the
8  * GNU General Public License (GPL)
9  * http://www.gnu.org/licenses
10  *
11  * Conjugate gradient solver
12  *
13  ******************************************************************************/
14 
15 #ifndef _CONJUGATEGRADIENT_H
16 #define _CONJUGATEGRADIENT_H
17 
18 #include "vectorbase.h"
19 #include "grid.h"
20 #include "kernel.h"
21 #include "multigrid.h"
22 
23 namespace Manta {
24 
25 static const bool CG_DEBUG = false;
26 
29  public:
30  enum PreconditionType { PC_None=0, PC_ICP, PC_mICP, PC_MGP };
31 
32  GridCgInterface() : mUseL2Norm(true) {};
33  virtual ~GridCgInterface() {};
34 
35  // solving functions
36  virtual bool iterate() = 0;
37  virtual void solve(int maxIter) = 0;
38 
39  // precond
40  virtual void setICPreconditioner(PreconditionType method, Grid<Real> *A0, Grid<Real> *Ai, Grid<Real> *Aj, Grid<Real> *Ak) = 0;
41  virtual void setMGPreconditioner(PreconditionType method, GridMg* MG) = 0;
42 
43  // access
44  virtual Real getSigma() const = 0;
45  virtual Real getIterations() const = 0;
46  virtual Real getResNorm() const = 0;
47  virtual void setAccuracy(Real set) = 0;
48  virtual Real getAccuracy() const = 0;
49 
51  virtual void forceReinit() = 0;
52 
53  void setUseL2Norm(bool set) { mUseL2Norm = set; }
54 
55  protected:
56 
57  // use l2 norm of residualfor threshold? (otherwise uses max norm)
58  bool mUseL2Norm;
59 };
60 
61 
63 
66 template<class APPLYMAT>
67 class GridCg : public GridCgInterface {
68  public:
70  GridCg(Grid<Real>& dst, Grid<Real>& rhs, Grid<Real>& residual, Grid<Real>& search, FlagGrid& flags, Grid<Real>& tmp,
71  Grid<Real>* A0, Grid<Real>* pAi, Grid<Real>* pAj, Grid<Real>* pAk);
72  ~GridCg() {}
73 
74  void doInit();
75  bool iterate();
76  void solve(int maxIter);
78  void setICPreconditioner(PreconditionType method, Grid<Real> *A0, Grid<Real> *Ai, Grid<Real> *Aj, Grid<Real> *Ak);
79  void setMGPreconditioner(PreconditionType method, GridMg* MG);
80  void forceReinit() { mInited = false; }
81 
82  // Accessors
83  Real getSigma() const { return mSigma; }
84  Real getIterations() const { return mIterations; }
85 
86  Real getResNorm() const { return mResNorm; }
87 
88  void setAccuracy(Real set) { mAccuracy=set; }
89  Real getAccuracy() const { return mAccuracy; }
90 
91  protected:
92  bool mInited;
93  int mIterations;
94  // grids
95  Grid<Real>& mDst;
96  Grid<Real>& mRhs;
97  Grid<Real>& mResidual;
98  Grid<Real>& mSearch;
99  FlagGrid& mFlags;
100  Grid<Real>& mTmp;
101 
102  Grid<Real> *mpA0, *mpAi, *mpAj, *mpAk;
103 
104  PreconditionType mPcMethod;
106  Grid<Real> *mpPCA0, *mpPCAi, *mpPCAj, *mpPCAk;
107  GridMg* mMG;
108 
110  Real mSigma;
112  Real mAccuracy;
114  Real mResNorm;
115 }; // GridCg
116 
117 
120 void ApplyMatrix (FlagGrid& flags, Grid<Real>& dst, Grid<Real>& src, Grid<Real>& A0, Grid<Real>& Ai, Grid<Real>& Aj, Grid<Real>& Ak) {}
121 
122 
125 void ApplyMatrix2D (FlagGrid& flags, Grid<Real>& dst, Grid<Real>& src, Grid<Real>& A0, Grid<Real>& Ai, Grid<Real>& Aj, Grid<Real>& Ak) {}
126 
127 
130 void MakeLaplaceMatrix(FlagGrid& flags, Grid<Real>& A0, Grid<Real>& Ai, Grid<Real>& Aj, Grid<Real>& Ak, MACGrid* fractions = 0) {}
131 
132 
133 
134 
135 
136 } // namespace
137 
138 #endif
139 
140 
void MakeLaplaceMatrix(FlagGrid &flags, Grid< Real > &A0, Grid< Real > &Ai, Grid< Real > &Aj, Grid< Real > &Ak, MACGrid *fractions=0)
Definition: conjugategrad.h:130
Definition: commonkernels.h:22
Grid< Real > * mpPCA0
preconditioning grids
Definition: conjugategrad.h:106
Definition: grid.h:267
Definition: grid.h:229
void forceReinit()
force reinit upon next iterate() call, can be used for doing multiple solves
Definition: conjugategrad.h:80
Real mSigma
sigma / residual
Definition: conjugategrad.h:110
virtual void forceReinit()=0
force reinit upon next iterate() call, can be used for doing multiple solves
Real mResNorm
norm of the residual
Definition: conjugategrad.h:114
Multigrid solver.
Definition: multigrid.h:32
Basic CG interface.
Definition: conjugategrad.h:28
void ApplyMatrix2D(FlagGrid &flags, Grid< Real > &dst, Grid< Real > &src, Grid< Real > &A0, Grid< Real > &Ai, Grid< Real > &Aj, Grid< Real > &Ak)
Definition: conjugategrad.h:125
Real mAccuracy
accuracy of solver (max. residuum)
Definition: conjugategrad.h:112
void ApplyMatrix(FlagGrid &flags, Grid< Real > &dst, Grid< Real > &src, Grid< Real > &A0, Grid< Real > &Ai, Grid< Real > &Aj, Grid< Real > &Ak)
Definition: conjugategrad.h:120
Run single iteration of the cg solver.
Definition: conjugategrad.h:67