NeoPZ
pzstepsolver.cpp
Go to the documentation of this file.
1 
6 #include "pzstepsolver.h"
7 #include <stdlib.h>
8 using namespace std;
9 
10 #include "pzlog.h"
11 
12 #include "TPZPersistenceManager.h"
13 
14 #ifdef LOG4CXX
15 static LoggerPtr logger(Logger::getLogger("pz.converge"));
16 #endif
17 
18 template <class TVar>
20  fPrecond = 0;
21  ResetSolver();
22 }
23 
24 template <class TVar>
27  fSolver = copy.fSolver;
28  fDecompose = copy.fDecompose;
30  fTol = copy.fTol;
31  fOverRelax = copy.fOverRelax;
32  fPrecond = 0;
33  if(copy.fPrecond) fPrecond = copy.fPrecond->Clone();
35  fNumVectors = copy.fNumVectors;//Cedric: 24/04/2003 - 12:39
36 }
37 
38 template <class TVar>
40  if(fPrecond) delete fPrecond;
41 }
42 
47 template <class TVar>
49 {
51 }
52 
56 template <class TVar>
58 {
59  if (fSolver == this->EDirect) {
60  this->Matrix()->Decompose(fDecompose,fSingular);
61  }
62 }
63 
64 template <class TVar>
66  if(!this->Matrix()) {
67  cout << "TPZMatrixSolver::Solve called without a matrix pointer\n";
68  DebugStop();
69  }
70 
71  TPZAutoPointer<TPZMatrix<TVar> > mat = this->Matrix();
72  // update the matrix to which the preconditioner refers
73  if(fPrecond)
74  {
75 
76  fPrecond->UpdateFrom(this->Matrix());
77  }
78 
79  if(result.Rows() != mat->Rows() || result.Cols() != F.Cols()) {
80  result.Redim(mat->Rows(),F.Cols());
81  }
82 
83  if(this->fScratch.Rows() != result.Rows() || this->fScratch.Cols() != result.Cols()) {
84  this->fScratch.Redim(result.Rows(),result.Cols());
85  }
86 
87  REAL tol = fTol;
88  int64_t numiterations = fMaxIterations;
89  switch(fSolver) {
91  default:
92  cout << "TPZMatrixSolver::Solve called without initialized solver, Jacobi used\n";
93  SetJacobi(1,0.,0);
95  // cout << "fScratch dimension " << fScratch.Rows() << ' ' << fScratch.Cols() << endl;
96  mat->SolveJacobi(numiterations,F,result,residual,this->fScratch,tol,fFromCurrent);
97  fNumIterations = numiterations;
98  break;
100  mat->SolveSOR(numiterations,F,result,residual,this->fScratch,fOverRelax,tol,fFromCurrent);
101  fNumIterations = numiterations;
102  break;
104  mat->SolveSSOR(numiterations,F,result,residual,this->fScratch,fOverRelax,tol,fFromCurrent);
105  fNumIterations = numiterations;
106  break;
107  case TPZStepSolver::ECG:
108  mat->SolveCG(numiterations,*fPrecond,F,result,residual,tol,fFromCurrent);
109  cout << "Number of equations " << mat->Rows() << std::endl;
110  cout << "Number of CG iterations " << numiterations << " tol = " << tol << endl;
111  fNumIterations = numiterations;
112  fTol = tol;
113 #ifdef LOG4CXX
114  if(logger->isDebugEnabled())
115  {
116  std::stringstream sout;
117  sout << "Number of equations " << mat->Rows() << std::endl;
118  sout << "Number of CG iterations " << numiterations << " tol = " << tol;
119  LOGPZ_DEBUG(logger,sout.str().c_str());
120  }
121 #endif
122  break;
123  case TPZStepSolver::EGMRES: {
125  mat->SolveGMRES(numiterations,*fPrecond,H,fNumVectors,F,result,residual,tol,fFromCurrent);
126  fNumIterations = numiterations;
127  cout << "Number of GMRES iterations " << numiterations << " tol = " << tol;
128  if(numiterations == fMaxIterations || tol >= fTol)
129  {
130  std::cout << "GMRes tolerance was not achieved : numiter " << numiterations <<
131  " tol " << tol << endl;
132  }
133 #ifdef LOG4CXX
134  {
135  std::stringstream sout;
136  sout << "Number of GMRES iterations " << numiterations << " tol = " << tol;
137  if(logger->isDebugEnabled()) LOGPZ_DEBUG(logger,sout.str().c_str());
138  }
139 #endif
140  }
141  break;
143  mat->SolveBICGStab(numiterations, *fPrecond, F, result,residual,tol,fFromCurrent);
144  fNumIterations = numiterations;
145 
146  if(numiterations == fMaxIterations || tol >= fTol)
147  {
148  std::cout << "BiCGStab tolerance was not achieved : numiter " << numiterations <<
149  " tol " << tol << endl;
150  }
151 #ifdef LOG4CXX
152  {
153  std::stringstream sout;
154  sout << "Number of BiCGStab iterations " << numiterations << " tol = " << tol;
155  LOGPZ_DEBUG(logger,sout.str().c_str());
156  }
157 #endif
158  break;
160  result = F;
161  mat->SolveDirect(result,fDecompose,fSingular);
162  if(residual) residual->Redim(F.Rows(),F.Cols());
163  break;
165  mat->Multiply(F,result);
166  if(residual) mat->Residual(result,F,*residual);
167 
168  }
169 }
170 template<class TVar>
172  fSolver = this->ENoSolver;
174  fMaxIterations = 0;
175  fNumIterations = -1;
176  fTol = 0.;
177  fNumVectors = 0;
178  fOverRelax = 0.;
179  if(fPrecond) delete fPrecond;
180  fPrecond = 0;
181  fFromCurrent = 0;
182 }
183 template <class TVar>
185  ResetSolver();
186  fSolver = this->EDirect;
187  fDecompose = decomp;
188 }
189 template <class TVar>
190 void TPZStepSolver<TVar>::SetCG(const int64_t numiterations, const TPZMatrixSolver<TVar> &pre, const REAL tol, const int64_t FromCurrent){
191  ResetSolver();
192  fSolver = this->ECG;
193  fMaxIterations = numiterations;
194  fNumIterations = -1;
195  fTol = tol;
196  // fPrecond = &pre;
197  if(fPrecond) delete fPrecond;
198  fPrecond = pre.Clone();
199  fFromCurrent = FromCurrent;
200 }
201 template<class TVar>
202 void TPZStepSolver<TVar>::SetGMRES(const int64_t numiterations, const int numvectors, const TPZMatrixSolver<TVar> &pre, const REAL tol, const int64_t FromCurrent){
203  ResetSolver();
204  fSolver = this->EGMRES;
205  fNumVectors = numvectors;
206  fMaxIterations = numiterations;
207  fNumIterations = -1;
208  fTol = tol;
209  // fPrecond = &pre;
210  if(fPrecond) delete fPrecond;
211  fPrecond = pre.Clone();
212  fFromCurrent = FromCurrent;
213 }
214 template<class TVar>
215 void TPZStepSolver<TVar>::SetBiCGStab(const int64_t numiterations, const TPZMatrixSolver<TVar>&pre,const REAL tol,const int64_t FromCurrent){
216  ResetSolver();
217  fSolver = this->EBICGSTAB;
218  fMaxIterations = numiterations;
219  fNumIterations = -1;
220  fTol = tol;
221  // fPrecond = &pre;
222  if(fPrecond) delete fPrecond;
223  fPrecond = pre.Clone();
224  fFromCurrent = FromCurrent;
225 }
226 template<class TVar>
227 void TPZStepSolver<TVar>::SetJacobi(const int64_t numiterations, const REAL tol, const int64_t FromCurrent) {
228  ResetSolver();
229  fSolver = this->EJacobi;
230  fMaxIterations = numiterations;
231  fNumIterations = -1;
232  fTol = tol;
233  fFromCurrent = FromCurrent;
234 }
235 template <class TVar>void TPZStepSolver<TVar>::SetSSOR(const int64_t numiterations,const REAL overrelax,const REAL tol,const int64_t FromCurrent) {
236  ResetSolver();
237  fSolver = this->ESSOR;
238  fOverRelax = overrelax;
239  fMaxIterations = numiterations;
240  fNumIterations = -1;
241  fTol = tol;
242  fFromCurrent = FromCurrent;
243 }
244 template <class TVar>
245 void TPZStepSolver<TVar>::SetSOR(const int64_t numiterations,const REAL overrelax,const REAL tol,const int64_t FromCurrent){
246  ResetSolver();
247  fSolver = this->ESOR;
248  fMaxIterations = numiterations;
249  fNumIterations = -1;
250  fOverRelax = overrelax;
251  fTol = tol;
252  fFromCurrent = FromCurrent;
253 }
254 template<class TVar>
256  ResetSolver();
257  fSolver = this->EMultiply;
258 }
259 
260 
264 template <class TVar>
266 {
267  if (fSolver == this->EDirect) {
268  DebugStop();
269  }
270  if(fPrecond) delete fPrecond;
271  fPrecond = solve.Clone();
272 }
273 
274 template <class TVar>
275 void TPZStepSolver<TVar>::Write(TPZStream &buf, int withclassid) const {
276  TPZMatrixSolver<TVar>::Write(buf, withclassid);
278  int lfSolver = fSolver;
279  buf.Write(&lfSolver, 1);
280  int lfDT = fDecompose;
281  buf.Write(&lfDT, 1);
282  buf.Write(&fMaxIterations, 1);
283  buf.Write(&fNumVectors, 1);
284  buf.Write(&fTol, 1);
285  buf.Write(&fOverRelax, 1);
286  buf.Write(&fFromCurrent, 1);
287  int64_t size = fSingular.size();
288  buf.Write(&size, 1);
289  std::list<int64_t>::const_iterator it = fSingular.begin();
290  for (; it != fSingular.end(); it++) {
291  buf.Write(&*it, 1);
292  }
293 }
294 
295 template <class TVar>
296 void TPZStepSolver<TVar>::Read(TPZStream &buf, void *context)
297 {
298  TPZMatrixSolver<TVar>::Read(buf, context);
300 
301  int lfSolver = 0;
302  buf.Read(&lfSolver, 1);
303  fSolver = (typename TPZMatrixSolver<TVar>::MSolver)lfSolver;
304  int lfDT = 0;
305  buf.Read(&lfDT, 1);
306  fDecompose = (DecomposeType)lfDT;
307  buf.Read(&fMaxIterations, 1);
308  buf.Read(&fNumVectors, 1);
309  buf.Read(&fTol, 1);
310  buf.Read(&fOverRelax, 1);
311  buf.Read(&fFromCurrent, 1);
312  int64_t size = 0;
313  buf.Read(&size, 1);
314  fSingular.resize(size);
315  std::list<int64_t>::iterator it = fSingular.begin();
316  for(;it != fSingular.end(); it++)
317  {
318  buf.Read(&*it, 1);
319  }
320 }
321 
322 template class TPZStepSolver<float>;
323 template class TPZStepSolver<double>;
324 template class TPZStepSolver<long double>;
325 
326 template class TPZStepSolver<std::complex<float> >;
327 template class TPZStepSolver<std::complex<double> >;
329 
330 #ifndef BORLAND
334 
338 #endif
void SetSOR(const int64_t numiterations, const REAL overrelax, const REAL tol, const int64_t FromCurrent)
void ResetMatrix() override
Resets current object.
Definition: pzsolve.cpp:50
Contains definitions to LOGPZ_DEBUG, LOGPZ_INFO, LOGPZ_WARN, LOGPZ_ERROR and LOGPZ_FATAL, and the implementation of the inline InitializePZLOG(string) function using log4cxx library or not. It must to be called out of "#ifdef LOG4CXX" scope.
int ClassId() const override
Serialization methods.
Definition: pzstepsolver.h:138
void SetPreconditioner(TPZSolver< TVar > &solve)
Define the preconditioner as a solver object.
virtual ~TPZStepSolver()
Defines a abstract class of solvers which will be used by matrix classes. Solver. ...
Definition: pzmatrix.h:32
void Write(TPZStream &buf, int withclassid) const override
Writes this object to the TPZStream buffer. Include the classid if withclassid = true.
Definition: pzsolve.cpp:65
int64_t fFromCurrent
Definition: pzstepsolver.h:132
Defines step solvers class. Solver.
Definition: pzmganalysis.h:17
virtual void ResetMatrix() override
This method will reset the matrix associated with the solver.
Defines a class of matrix solvers. Solver.
Definition: pzanalysis.h:24
static TPZSavable * GetInstance(const int64_t &objId)
void SetSSOR(const int64_t numiterations, const REAL overrelax, const REAL tol, const int64_t FromCurrent)
void Write(TPZStream &buf, int withclassid) const override
Writes this object to the TPZStream buffer. Include the classid if withclassid = true.
DecomposeType fDecompose
Definition: pzstepsolver.h:119
void SetJacobi(const int64_t numiterations, const REAL tol, const int64_t FromCurrent)
void SetCG(const int64_t numiterations, const TPZMatrixSolver< TVar > &pre, const REAL tol, const int64_t FromCurrent)
static const double tol
Definition: pzgeoprism.cpp:23
virtual void Write(const bool val)
Definition: TPZStream.cpp:8
MSolver
Defines a series of solvers available in PZ.
Definition: pzsolve.h:88
virtual void Decompose() override
Decompose the system of equations if a direct solver is used.
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
#define LOGPZ_DEBUG(A, B)
Define log for debug info.
Definition: pzlog.h:87
TPZMatrixSolver< TVar >::MSolver fSolver
Definition: pzstepsolver.h:118
void Read(TPZStream &buf, void *context) override
read objects from the stream
Definition: pzsolve.cpp:106
int64_t Rows() const
Returns number of rows.
Definition: pzmatrix.h:803
void SetBiCGStab(const int64_t numiterations, const TPZMatrixSolver< TVar > &pre, const REAL tol, const int64_t FromCurrent)
TPZStepSolver(TPZAutoPointer< TPZMatrix< TVar > > refmat=0)
TPZFMatrix< TVar > fScratch
Manipulation matrix.
Definition: pzsolve.h:169
Full matrix class. Matrix.
Definition: pzfmatrix.h:32
int Redim(const int64_t newRows, const int64_t newCols) override
Redimension a matrix and ZERO your elements.
Definition: pzfmatrix.h:616
void Solve(const TPZFMatrix< TVar > &F, TPZFMatrix< TVar > &result, TPZFMatrix< TVar > *residual=0) override
Solves the system of linear equations.
virtual TPZSolver * Clone() const =0
Clones the current object returning a pointer of type TPZSolver.
int64_t fNumIterations
Number of iterations of last solve.
Definition: pzstepsolver.h:125
int64_t fMaxIterations
Maximum number of iterations.
Definition: pzstepsolver.h:122
TPZAutoPointer< TPZMatrix< TVar > > Matrix() const
Returns a pointer to TPZMatrix<>
Definition: pzsolve.h:138
TPZSolver< TVar > * fPrecond
Solver using preconditioner matrix.
Definition: pzstepsolver.h:131
Contains TPZStepSolver class which defines step solvers class.
int64_t Cols() const
Returns number of cols.
Definition: pzmatrix.h:809
Defines the interface for saving and reading data. Persistency.
Definition: TPZStream.h:50
void Read(TPZStream &buf, void *context) override
read objects from the stream
void SetDirect(const DecomposeType decomp)
std::list< int64_t > fSingular
Definition: pzstepsolver.h:134
void ResetSolver()
reset the data structure of the solver object
static void WritePointer(const TPZSavable *obj, TPZStream *stream)
Implements an interface to register a class id and a restore function. Persistence.
Definition: TPZSavable.h:150
DecomposeType
Defines decomposition type for any matrix classes.
Definition: pzmatrix.h:52
void SetGMRES(const int64_t numiterations, const int numvectors, const TPZMatrixSolver< TVar > &pre, const REAL tol, const int64_t FromCurrent)
virtual void Read(bool &val)
Definition: TPZStream.cpp:91
Root matrix class (abstract). Matrix.
Definition: pzmatrix.h:60
This class implements a reference counter mechanism to administer a dynamically allocated object...