Memosa-FVM  0.2
AMG Class Reference

#include <AMG.h>

Inheritance diagram for AMG:
Collaboration diagram for AMG:

Public Types

enum  CycleType { V_CYCLE, W_CYCLE, F_CYCLE }
 
enum  SmootherType { GAUSS_SEIDEL, JACOBI }
 

Public Member Functions

 AMG ()
 
virtual ~AMG ()
 
 DEFINE_TYPENAME ("AMG")
 
virtual MFRPtr solve (LinearSystem &ls)
 
virtual void smooth (LinearSystem &ls)
 
virtual void setMergeLevelSize (int ls_size)
 
void redirectPrintToFile (const string &fname)
 
void redirectPrintToScreen ()
 
int getTotalIterations () const
 
virtual void cleanup ()
 
- Public Member Functions inherited from LinearSolver
 LinearSolver ()
 

Public Attributes

int maxCoarseLevels
 
int nPreSweeps
 
int nPostSweeps
 
int coarseGroupSize
 
double weightRatioThreshold
 
CycleType cycleType
 
SmootherType smootherType
 
bool scaleCorrections
 
- Public Attributes inherited from LinearSolver
int nMaxIterations
 
int verbosity
 
double relativeTolerance
 
double absoluteTolerance
 

Private Member Functions

 AMG (const AMG &)
 
void createCoarseLevels ()
 
void doSweeps (const int nSweeps, const int level)
 
void cycle (CycleType cycleType, const int level)
 
void flipComm ()
 

Private Attributes

LinearSystem_finestLinearSystem
 
vector< shared_ptr
< LinearSystem > > 
_coarseLinearSystems
 
shared_ptr< LinearSystemMerger > _mergeLS
 
int _mergeLevelSize
 
int _mergeLevel
 
bool _isMerge
 
int _totalIterations
 
streambuf * m_psbuf
 
streambuf * m_backup
 
ofstream m_filestr
 
bool _isCOMMWORLD
 

Static Private Attributes

static int amg_indx = 0
 

Detailed Description

Solve a linear system using algebraic multigrid.

Definition at line 27 of file AMG.h.

Member Enumeration Documentation

Enumerator
V_CYCLE 
W_CYCLE 
F_CYCLE 

Definition at line 31 of file AMG.h.

32  {
33  V_CYCLE,
34  W_CYCLE,
35  F_CYCLE
36  };
Enumerator
GAUSS_SEIDEL 
JACOBI 

Definition at line 38 of file AMG.h.

39  {
41  JACOBI
42  };

Constructor & Destructor Documentation

AMG::AMG ( )

Definition at line 15 of file AMG.cpp.

References logCtor.

15  :
16  maxCoarseLevels(30),
17  nPreSweeps(0),
18  nPostSweeps(1),
19  coarseGroupSize(2),
23  scaleCorrections(true),
25  _mergeLevelSize(0),
26  _mergeLevel(-1),
27  _isMerge(false),
29 #ifdef FVM_PARALLEL
30  _commTarget(MPI::COMM_WORLD),
31 #endif
32  _isCOMMWORLD(true)
33 {
34  logCtor();
35 
36 }
int _mergeLevelSize
Definition: AMG.h:97
int nPostSweeps
Definition: AMG.h:76
int nPreSweeps
Definition: AMG.h:75
SmootherType smootherType
Definition: AMG.h:80
bool scaleCorrections
Definition: AMG.h:81
#define logCtor()
Definition: RLogInterface.h:26
int _mergeLevel
Definition: AMG.h:98
int maxCoarseLevels
Definition: AMG.h:74
bool _isMerge
Definition: AMG.h:99
int _totalIterations
Definition: AMG.h:100
bool _isCOMMWORLD
Definition: AMG.h:109
LinearSystem * _finestLinearSystem
Definition: AMG.h:86
CycleType cycleType
Definition: AMG.h:79
double weightRatioThreshold
Definition: AMG.h:78
int coarseGroupSize
Definition: AMG.h:77
AMG::~AMG ( )
virtual

Definition at line 38 of file AMG.cpp.

References logDtor.

39 {
40  logDtor();
41 }
#define logDtor()
Definition: RLogInterface.h:33
AMG::AMG ( const AMG )
private

Member Function Documentation

void AMG::cleanup ( )
virtual

Implements LinearSolver.

Definition at line 213 of file AMG.cpp.

References _coarseLinearSystems, and _finestLinearSystem.

214 {
216  _coarseLinearSystems.clear();
217 }
vector< shared_ptr< LinearSystem > > _coarseLinearSystems
Definition: AMG.h:87
LinearSystem * _finestLinearSystem
Definition: AMG.h:86
void AMG::createCoarseLevels ( )
private

Definition at line 150 of file AMG.cpp.

References _coarseLinearSystems, _finestLinearSystem, _isMerge, _mergeLevel, _mergeLevelSize, _mergeLS, coarseGroupSize, LinearSystem::createCoarse(), flipComm(), MultiFieldMatrix::getLocalSize(), LinearSystem::getMatrix(), LinearSystem::isSymmetric, maxCoarseLevels, LinearSolver::verbosity, and weightRatioThreshold.

Referenced by smooth(), and solve().

151 {
152 
153  _coarseLinearSystems.clear();
154  for(int n=0; n<maxCoarseLevels; n++)
155  {
156  LinearSystem& fineLS = (n == 0) ?
158  shared_ptr<LinearSystem>
160 
161  coarseLS->isSymmetric = fineLS.isSymmetric;
162 
163  int isContinue = int( fineLS.getMatrix().getLocalSize() != coarseLS->getMatrix().getLocalSize() );
164 #ifdef FVM_PARALLEL
165  _commTarget.Allreduce(MPI::IN_PLACE, &isContinue, 1, MPI::INT, MPI::SUM);
166 #endif
167  if ( isContinue == 0 )
168  break;
169 
170 #ifdef FVM_PARALLEL
171  _coarseLinearSystems.push_back(coarseLS);
172 
173  int min_size = coarseLS->getMatrix().getMinSize( _commTarget );
174 
175 
176  if ( verbosity > 1 && MPI::COMM_WORLD.Get_rank() == 0 )
177  cout << " proc_id = " << MPI::COMM_WORLD.Get_rank() << " Created coarse level " << n << " of size "
178  << min_size << endl;
179 
180  if ( min_size <= 3 )
181  break;
182 
183  if ( coarseLS->getMatrix().getMergeSize( _commTarget ) < _mergeLevelSize && _mergeLevel == -1 && _isMerge ){
184  _mergeLevel = n+1;
185  set<int> group;
186  int size = MPI::COMM_WORLD.Get_size();
187  for ( int i = 0; i < size; i++ )
188  group.insert(i);
189 
190  _mergeLS = shared_ptr<LinearSystemMerger> ( new LinearSystemMerger( 0, group, *coarseLS ) );
191  _mergeLS->gatherMatrix();
192  _coarseLinearSystems.push_back( _mergeLS->getLS() );
193  n++;
194  flipComm(); //this change to COMM_WORLD to one-processedGROUP
195  }
196 
197 #endif
198 
199 #ifndef FVM_PARALLEL
200  if ( coarseLS->getMatrix().getSize() <= 3 )
201  break;
202  if ( verbosity > 1 )
203  cout << "Created coarse level " << n << " of size " << coarseLS->getMatrix().getSize() << endl;
204  _coarseLinearSystems.push_back(coarseLS);
205 #endif
206 
207  }
208 
209 
210 }
int _mergeLevelSize
Definition: AMG.h:97
vector< shared_ptr< LinearSystem > > _coarseLinearSystems
Definition: AMG.h:87
int getLocalSize() const
int _mergeLevel
Definition: AMG.h:98
int maxCoarseLevels
Definition: AMG.h:74
bool _isMerge
Definition: AMG.h:99
shared_ptr< LinearSystemMerger > _mergeLS
Definition: AMG.h:88
LinearSystem * _finestLinearSystem
Definition: AMG.h:86
shared_ptr< LinearSystem > createCoarse(const int groupSize, const double weightRatioThreshold)
void flipComm()
Definition: AMG.cpp:303
bool isSymmetric
Definition: LinearSystem.h:50
double weightRatioThreshold
Definition: AMG.h:78
MultiFieldMatrix & getMatrix()
Definition: LinearSystem.h:37
int coarseGroupSize
Definition: AMG.h:77
void AMG::cycle ( CycleType  cycleType,
const int  level 
)
private

Definition at line 71 of file AMG.cpp.

References _coarseLinearSystems, _finestLinearSystem, _mergeLevel, _mergeLS, MultiFieldMatrix::computeResidual(), MultiFieldMatrix::correctSolution(), doSweeps(), MultiField::dotWith(), F_CYCLE, LinearSystem::getB(), LinearSystem::getCoarseIndex(), LinearSystem::getDelta(), LinearSystem::getMatrix(), LinearSystem::getResidual(), MultiFieldMatrix::injectResidual(), LinearSystem::isSymmetric, nPostSweeps, nPreSweeps, MultiFieldMatrix::quadProduct(), scaleCorrections, V_CYCLE, W_CYCLE, and MultiField::zero().

Referenced by smooth(), and solve().

72 {
73  doSweeps(nPreSweeps,level);
74 
75  if (level < (int)_coarseLinearSystems.size())
76  {
77  LinearSystem& fineLS = (level == 0) ?
79 
80 
81  LinearSystem& coarseLS = *_coarseLinearSystems[level];
82 
83 
84  MultiFieldMatrix& fineMatrix = fineLS.getMatrix();
85 
86  fineMatrix.computeResidual(fineLS.getDelta(),fineLS.getB(),fineLS.getResidual());
87  coarseLS.getB().zero();
88  coarseLS.getDelta().zero();
89 
90  fineMatrix.injectResidual(fineLS.getCoarseIndex(),
91  fineLS.getResidual(),
92  coarseLS.getB());
93 
94  int nextLevel = level+1;
95 #ifdef FVM_PARALLEL
96  if ( nextLevel == _mergeLevel )
97  {
98  _mergeLS->gatherB();
99  nextLevel++;
100  }
101 #endif
102  //if ( level == 4 )
103  // cycleType = W_CYCLE;
104 
105  cycle(cycleType,nextLevel);
106 
107  if (cycleType == W_CYCLE)
108  cycle(W_CYCLE,nextLevel);
109  else if (cycleType == F_CYCLE)
110  cycle(V_CYCLE,nextLevel);
111 
112 #ifdef FVM_PARALLEL
113  if ( level+1 == _mergeLevel ) {
114  _mergeLS->scatterDelta();
115  }
116 #endif
117 
118  MFRPtr scale;
119  if (coarseLS.isSymmetric && scaleCorrections)
120  {
121  const MultiField& x = coarseLS.getDelta();
122  const MultiField& b = coarseLS.getB();
123  const MultiFieldMatrix& A = coarseLS.getMatrix();
124  MFRPtr xb = x.dotWith(b);
125  xb->reduceSum();
126 
127  MFRPtr mxb = -(*xb);
128  MFRPtr xTAx = A.quadProduct(x);
129 
130  xTAx->reduceSum();
131 
132  scale = (*mxb)/(*xTAx);
133  scale->limit(1.0, 1.0);
134 
135  }
136 
137  fineMatrix.correctSolution(fineLS.getCoarseIndex(),
138  fineLS.getDelta(),
139  scale,
140  coarseLS.getDelta()) ;
141 
142 
143  }
144 
145 doSweeps(nPostSweeps,level);
146 
147 }
int nPostSweeps
Definition: AMG.h:76
int nPreSweeps
Definition: AMG.h:75
MultiField & getResidual()
Definition: LinearSystem.h:35
void correctSolution(const MultiField &coarseIndex, MultiField &fineSolutionField, MFRPtr scaleField, const MultiField &coarseSolutionField)
vector< shared_ptr< LinearSystem > > _coarseLinearSystems
Definition: AMG.h:87
void doSweeps(const int nSweeps, const int level)
Definition: AMG.cpp:44
bool scaleCorrections
Definition: AMG.h:81
MultiField & getCoarseIndex()
Definition: LinearSystem.h:39
virtual void zero()
Definition: MultiField.cpp:115
MultiField & getDelta()
Definition: LinearSystem.h:34
int _mergeLevel
Definition: AMG.h:98
shared_ptr< LinearSystemMerger > _mergeLS
Definition: AMG.h:88
LinearSystem * _finestLinearSystem
Definition: AMG.h:86
virtual void computeResidual(const IContainer &xB, const IContainer &bB, IContainer &rB) const
MultiField & getB()
Definition: LinearSystem.h:33
shared_ptr< MultiFieldReduction > MFRPtr
void cycle(CycleType cycleType, const int level)
Definition: AMG.cpp:71
CycleType cycleType
Definition: AMG.h:79
bool isSymmetric
Definition: LinearSystem.h:50
shared_ptr< MultiFieldReduction > dotWith(const MultiField &ofield) const
Definition: MultiField.cpp:231
void injectResidual(const MultiField &coarseIndex, const MultiField &fineResidualField, MultiField &coarseBField)
MultiFieldMatrix & getMatrix()
Definition: LinearSystem.h:37
MFRPtr quadProduct(const MultiField &x) const
AMG::DEFINE_TYPENAME ( "AMG"  )
void AMG::doSweeps ( const int  nSweeps,
const int  level 
)
private

Definition at line 44 of file AMG.cpp.

References _coarseLinearSystems, _finestLinearSystem, MultiFieldMatrix::forwardGS(), GAUSS_SEIDEL, LinearSystem::getB(), LinearSystem::getDelta(), LinearSystem::getMatrix(), LinearSystem::getResidual(), MultiFieldMatrix::Jacobi(), MultiFieldMatrix::reverseGS(), and smootherType.

Referenced by cycle().

45 {
46 
47  LinearSystem& ls = (level == 0) ?
49 
50  const MultiFieldMatrix& m = ls.getMatrix();
51  MultiField& delta = ls.getDelta();
52  const MultiField& b = ls.getB();
53  MultiField& r = ls.getResidual();
54 
55  for(int i=0; i<nSweeps; i++)
56  {
58  {
59  m.forwardGS(delta,b,r);
60  m.reverseGS(delta,b,r);
61  }
62  else
63  {
64  m.Jacobi(delta,b,r);
65  m.Jacobi(delta,b,r);
66  }
67  }
68 }
MultiField & getResidual()
Definition: LinearSystem.h:35
virtual void forwardGS(IContainer &xB, const IContainer &bB, IContainer &temp) const
vector< shared_ptr< LinearSystem > > _coarseLinearSystems
Definition: AMG.h:87
SmootherType smootherType
Definition: AMG.h:80
MultiField & getDelta()
Definition: LinearSystem.h:34
virtual void reverseGS(IContainer &xB, const IContainer &bB, IContainer &temp) const
LinearSystem * _finestLinearSystem
Definition: AMG.h:86
virtual void Jacobi(IContainer &xB, const IContainer &bB, IContainer &tempB) const
MultiField & getB()
Definition: LinearSystem.h:33
MultiFieldMatrix & getMatrix()
Definition: LinearSystem.h:37
void AMG::flipComm ( )
private

Definition at line 303 of file AMG.cpp.

References _isCOMMWORLD.

Referenced by createCoarseLevels().

304 {
305 
306 #ifdef FVM_PARALLEL
307  if ( _isCOMMWORLD ){
308  int color = ( MPI::COMM_WORLD.Get_rank() == 0 );
309  int key = MPI::COMM_WORLD.Get_rank();
310  _commTarget = MPI::COMM_WORLD.Split( color, key );
312  } else {
313  _commTarget = MPI::COMM_WORLD;
315  }
316 #endif
317 
318 
319 
320 }
bool _isCOMMWORLD
Definition: AMG.h:109
int AMG::getTotalIterations ( ) const
inline

Definition at line 69 of file AMG.h.

69 { return _totalIterations;}
int _totalIterations
Definition: AMG.h:100
void AMG::redirectPrintToFile ( const string &  fname)

Definition at line 325 of file AMG.cpp.

References m_backup, m_filestr, and m_psbuf.

325  {
326  m_filestr.open ( fname.c_str() );
327  m_backup = cout.rdbuf(); // back up cout's streambuf
328  m_psbuf = m_filestr.rdbuf(); // get file's streambuf
329  cout.rdbuf(m_psbuf);
330 }
streambuf * m_backup
Definition: AMG.h:107
streambuf * m_psbuf
Definition: AMG.h:106
ofstream m_filestr
Definition: AMG.h:108
void AMG::redirectPrintToScreen ( )

Definition at line 334 of file AMG.cpp.

References m_backup, and m_filestr.

334  {
335  cout.rdbuf(m_backup); // restore cout's original streambuf
336  m_filestr.close();
337 }
streambuf * m_backup
Definition: AMG.h:107
ofstream m_filestr
Definition: AMG.h:108
virtual void AMG::setMergeLevelSize ( int  ls_size)
inlinevirtual

Definition at line 53 of file AMG.h.

53  {
54 #ifdef FVM_PARALLEL
55  //_mergeLevelSize = ceil( double(ls_size) / double(MPI::COMM_WORLD.Get_size()) );
56  _mergeLevelSize = ls_size;
57  _isMerge = true;
58 #endif
59 
60 #ifndef FVM_PARALLEL
61  _mergeLevelSize = ls_size;
62  cout << " you can not set mergeLevelSize in serial version !!!!!!!!!!! " << endl;
63  abort();
64 #endif
65 }
int _mergeLevelSize
Definition: AMG.h:97
bool _isMerge
Definition: AMG.h:99
void AMG::smooth ( LinearSystem ls)
virtual

Implements LinearSolver.

Definition at line 286 of file AMG.cpp.

References _finestLinearSystem, createCoarseLevels(), cycle(), and cycleType.

287 {
288  if (_finestLinearSystem != &ls)
289  {
290  _finestLinearSystem = &ls;
292  }
293  _finestLinearSystem = &ls;
294 
295  {
296  cycle(cycleType,0);
297  }
298 }
void createCoarseLevels()
Definition: AMG.cpp:150
LinearSystem * _finestLinearSystem
Definition: AMG.h:86
void cycle(CycleType cycleType, const int level)
Definition: AMG.cpp:71
CycleType cycleType
Definition: AMG.h:79
MFRPtr AMG::solve ( LinearSystem ls)
virtual

Implements LinearSolver.

Definition at line 220 of file AMG.cpp.

References _finestLinearSystem, _totalIterations, LinearSolver::absoluteTolerance, MultiFieldMatrix::computeResidual(), createCoarseLevels(), cycle(), cycleType, LinearSystem::getB(), LinearSystem::getDelta(), LinearSystem::getMatrix(), MultiField::getOneNorm(), LinearSystem::getResidual(), LinearSolver::nMaxIterations, LinearSolver::relativeTolerance, MultiField::sync(), and LinearSolver::verbosity.

Referenced by main().

221 {
222  if (_finestLinearSystem != &ls)
223  {
224  _finestLinearSystem = &ls;
226  }
227  _finestLinearSystem = &ls;
228 
229 
230  const MultiFieldMatrix& finestMatrix = _finestLinearSystem->getMatrix();
234 
236 
237 #ifdef FVM_PARALLEL
238  if (verbosity >0 && MPI::COMM_WORLD.Get_rank() == 0 )
239  cout << "0: " << *rNorm0 << endl;
240 #endif
241 
242 #ifndef FVM_PARALLEL
243  if ( verbosity > 0 )
244  cout << "0: " << *rNorm0 << endl;
245 #endif
246 
247  if (*rNorm0 < absoluteTolerance )
248  return rNorm0;
249 
250  for(int i=1; i<nMaxIterations; i++)
251  {
253  cycle(cycleType,0);
258  MFRPtr normRatio(rNorm->normalize(*rNorm0));
259 
260 #ifndef FVM_PARALLEL
261  if (verbosity >0 )
262  cout << i << ": " << *rNorm << endl;
263 
264 
265 #endif
266 
267 
268 #ifdef FVM_PARALLEL
269  if (*rNorm < absoluteTolerance || *normRatio < relativeTolerance || i == nMaxIterations-1)
270  if (verbosity >0 && MPI::COMM_WORLD.Get_rank() == 0 )
271  cout <<i << ": " << *rNorm << endl;
272 #endif
273 
274  if (*rNorm < absoluteTolerance || *normRatio < relativeTolerance)
275  break;
276 
277  }
278 
280 
281  return rNorm0;
282 }
MultiField & getResidual()
Definition: LinearSystem.h:35
void createCoarseLevels()
Definition: AMG.cpp:150
MultiField & getDelta()
Definition: LinearSystem.h:34
int _totalIterations
Definition: AMG.h:100
int nMaxIterations
Definition: LinearSolver.h:31
shared_ptr< MultiFieldReduction > getOneNorm() const
Definition: MultiField.cpp:216
double relativeTolerance
Definition: LinearSolver.h:33
LinearSystem * _finestLinearSystem
Definition: AMG.h:86
virtual void computeResidual(const IContainer &xB, const IContainer &bB, IContainer &rB) const
double absoluteTolerance
Definition: LinearSolver.h:34
MultiField & getB()
Definition: LinearSystem.h:33
shared_ptr< MultiFieldReduction > MFRPtr
void cycle(CycleType cycleType, const int level)
Definition: AMG.cpp:71
CycleType cycleType
Definition: AMG.h:79
void sync()
Definition: MultiField.cpp:489
MultiFieldMatrix & getMatrix()
Definition: LinearSystem.h:37

Member Data Documentation

vector<shared_ptr<LinearSystem> > AMG::_coarseLinearSystems
private

Definition at line 87 of file AMG.h.

Referenced by cleanup(), createCoarseLevels(), cycle(), and doSweeps().

LinearSystem* AMG::_finestLinearSystem
private

Definition at line 86 of file AMG.h.

Referenced by cleanup(), createCoarseLevels(), cycle(), doSweeps(), smooth(), and solve().

bool AMG::_isCOMMWORLD
private

Definition at line 109 of file AMG.h.

Referenced by flipComm().

bool AMG::_isMerge
private

Definition at line 99 of file AMG.h.

Referenced by createCoarseLevels().

int AMG::_mergeLevel
private

Definition at line 98 of file AMG.h.

Referenced by createCoarseLevels(), and cycle().

int AMG::_mergeLevelSize
private

Definition at line 97 of file AMG.h.

Referenced by createCoarseLevels().

shared_ptr<LinearSystemMerger> AMG::_mergeLS
private

Definition at line 88 of file AMG.h.

Referenced by createCoarseLevels(), and cycle().

int AMG::_totalIterations
private

Definition at line 100 of file AMG.h.

Referenced by solve().

int AMG::amg_indx = 0
staticprivate

Definition at line 95 of file AMG.h.

int AMG::coarseGroupSize

Definition at line 77 of file AMG.h.

Referenced by createCoarseLevels().

CycleType AMG::cycleType

Definition at line 79 of file AMG.h.

Referenced by smooth(), and solve().

streambuf* AMG::m_backup
private

Definition at line 107 of file AMG.h.

Referenced by redirectPrintToFile(), and redirectPrintToScreen().

ofstream AMG::m_filestr
private

Definition at line 108 of file AMG.h.

Referenced by redirectPrintToFile(), and redirectPrintToScreen().

streambuf* AMG::m_psbuf
private

Definition at line 106 of file AMG.h.

Referenced by redirectPrintToFile().

int AMG::maxCoarseLevels

Definition at line 74 of file AMG.h.

Referenced by createCoarseLevels().

int AMG::nPostSweeps

Definition at line 76 of file AMG.h.

Referenced by cycle().

int AMG::nPreSweeps

Definition at line 75 of file AMG.h.

Referenced by cycle().

bool AMG::scaleCorrections

Definition at line 81 of file AMG.h.

Referenced by cycle().

SmootherType AMG::smootherType

Definition at line 80 of file AMG.h.

Referenced by doSweeps().

double AMG::weightRatioThreshold

Definition at line 78 of file AMG.h.

Referenced by createCoarseLevels().


The documentation for this class was generated from the following files: