Memosa-FVM  0.2
CG.cpp
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #ifdef FVM_PARALLEL
6 #include <mpi.h>
7 #endif
8 
9 
10 #include "CG.h"
11 CG::CG() :
12  preconditioner(0)
13 {}
14 
16 {
17 }
18 
19 void
21 {
23 }
24 
25 MFRPtr
27 {
28  const MultiFieldMatrix& matrix = ls.getMatrix();
29 
30  // original system is in delta form
31  shared_ptr<MultiField> x(ls.getDeltaPtr());
32  shared_ptr<MultiField> bOrig(ls.getBPtr());
33 
34  matrix.computeResidual(ls.getDelta(),ls.getB(),ls.getResidual());
35 
36  MFRPtr rNorm0(ls.getResidual().getOneNorm());
37 
38  shared_ptr<MultiField> r(dynamic_pointer_cast<MultiField>(ls.getResidual().newCopy()));
39 
40  shared_ptr<MultiField> p;
41  shared_ptr<MultiField> z(dynamic_pointer_cast<MultiField>(x->newClone()));
42  shared_ptr<MultiField> q(dynamic_pointer_cast<MultiField>(x->newClone()));
43 
44  MFRPtr rho;
45  MFRPtr rhoPrev;
46 
47  MFRPtr alpha;
48  MFRPtr beta;
49 
50 #ifndef FVM_PARALLEL
51  if (verbosity >0)
52  cout << 0 << ": " << *rNorm0 << endl;
53 #endif
54 
55 #ifdef FVM_PARALLEL
56  if (verbosity >0 && MPI::COMM_WORLD.Get_rank() == 0)
57  cout << 0 << ": " << *rNorm0 << endl;
58 #endif
59 
60 
61  for(int i = 0; i<nMaxIterations; i++)
62  {
63  z->zero();
64  ls.replaceDelta(z);
65  ls.replaceB(r);
66 
68 
69 
70  rhoPrev = rho;
71  rho = r->dotWith(*z);
72 
73  rho->reduceSum();
74 
75  if (!p)
76  {
77  p = dynamic_pointer_cast<MultiField>(z->newCopy());
78  }
79  else
80  {
81  MFRPtr beta = (*rho) / (*rhoPrev);
82  *p *= *beta;
83  *p += *z;
84  }
85 
86  matrix.multiply(*q,*p);
87 
88  MFRPtr ptq = p->dotWith(*q);
89  ptq->reduceSum();
90 
91  alpha = (*rho)/(*ptq);
92 
93  x->msaxpy(*alpha,*p);
94  r->msaxpy(*alpha,*q);
95 
96  MFRPtr rNorm = r->getOneNorm();
97 
98 
99  MFRPtr normRatio(rNorm->normalize(*rNorm0));
100 
101 #ifndef FVM_PARALLEL
102  if (verbosity >0)
103  cout << i+1 << ": " << *rNorm << endl;
104 #endif
105 
106 #ifdef FVM_PARALLEL
107  if (verbosity >0 && MPI::COMM_WORLD.Get_rank() == 0)
108  cout << i+1 << ": " << *rNorm << endl;
109 #endif
110 
111  if (*rNorm < absoluteTolerance || *normRatio < relativeTolerance)
112  break;
113 
114 
115  }
116  ls.replaceDelta(x);
117  ls.replaceB(bOrig);
118 #ifdef FVM_PARALLEL
119  x->sync();
120 #endif
121 
122  matrix.computeResidual(ls.getDelta(),ls.getB(),ls.getResidual());
123  MFRPtr rNormn(ls.getResidual().getOneNorm());
124 
125 #ifndef FVM_PARALLEL
126  if (verbosity >0)
127  cout << "n" << ": " << *rNormn << endl;
128 #endif
129 
130 
131 #ifdef FVM_PARALLEL
132  if (verbosity >0 && MPI::COMM_WORLD.Get_rank() == 0)
133  cout << "n" << ": " << *rNormn << endl;
134 #endif
135 
136 
137 
138  return rNorm0;
139 }
140 
141 void
143 {
144  throw CException("cannot use CG as preconditioner");
145 }
virtual shared_ptr< IContainer > newCopy() const
Definition: MultiField.cpp:101
MultiField & getResidual()
Definition: LinearSystem.h:35
void replaceB(shared_ptr< MultiField > newB)
Definition: LinearSystem.h:45
MultiField & getDelta()
Definition: LinearSystem.h:34
virtual ~CG()
Definition: CG.cpp:15
virtual void cleanup()=0
int nMaxIterations
Definition: LinearSolver.h:31
shared_ptr< MultiField > getDeltaPtr()
Definition: LinearSystem.h:41
shared_ptr< MultiFieldReduction > getOneNorm() const
Definition: MultiField.cpp:216
double relativeTolerance
Definition: LinearSolver.h:33
virtual void computeResidual(const IContainer &xB, const IContainer &bB, IContainer &rB) const
virtual void smooth(LinearSystem &ls)
Definition: CG.cpp:142
virtual void cleanup()
Definition: CG.cpp:20
CG()
Definition: CG.cpp:11
double absoluteTolerance
Definition: LinearSolver.h:34
MultiField & getB()
Definition: LinearSystem.h:33
shared_ptr< MultiFieldReduction > MFRPtr
virtual MFRPtr solve(LinearSystem &ls)
Definition: CG.cpp:26
virtual void multiply(IContainer &yB, const IContainer &xB) const
virtual void smooth(LinearSystem &ls)=0
LinearSolver * preconditioner
Definition: CG.h:31
void replaceDelta(shared_ptr< MultiField > newDelta)
Definition: LinearSystem.h:44
shared_ptr< MultiField > getBPtr()
Definition: LinearSystem.h:42
MultiFieldMatrix & getMatrix()
Definition: LinearSystem.h:37