Iterative.cxx
1 // Copyright (C) 2003-2009 Marc DuruflĂ©
2 //
3 // This file is part of the linear-algebra library Seldon,
4 // http://seldon.sourceforge.net/.
5 //
6 // Seldon is free software; you can redistribute it and/or modify it under the
7 // terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 2.1 of the License, or (at your option)
9 // any later version.
10 //
11 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14 // more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with Seldon. If not, see http://www.gnu.org/licenses/.
18 
19 
20 #ifndef SELDON_FILE_ITERATIVE_CXX
21 
22 #include <vector>
23 
24 // headers of class Iteration and Preconditioner_Base
25 #include "Iterative.hxx"
26 
27 // and all iterative solvers
28 #include "Cg.cxx"
29 #include "Cgne.cxx"
30 #include "Lsqr.cxx"
31 #include "Cgs.cxx"
32 #include "BiCg.cxx"
33 #include "BiCgStab.cxx"
34 #include "BiCgStabl.cxx"
35 #include "BiCgcr.cxx"
36 #include "Gcr.cxx"
37 #include "CoCg.cxx"
38 #include "Gmres.cxx"
39 #include "MinRes.cxx"
40 #include "Qmr.cxx"
41 #include "QmrSym.cxx"
42 #include "QCgs.cxx"
43 #include "TfQmr.cxx"
44 #include "Symmlq.cxx"
45 
46 #include "IterativeInline.cxx"
47 
48 namespace Seldon
49 {
50 
52  template<class Titer>
54  {
55  tolerance = 1e-6;
56  max_iter = 100;
57  nb_iter = 0;
58  error_code = 0;
59  fail_convergence = false;
60  print_level = 1;
61  init_guess_null = true;
62  type_solver = 0; parameter_restart = 10;
63  type_preconditioning = 0;
64  }
65 
66 
68  template<class Titer>
69  Iteration<Titer>::Iteration(int max_iteration, const Titer& tol)
70  {
71  max_iter = max_iteration;
72  tolerance = tol;
73  nb_iter = 0;
74  error_code = 0;
75  fail_convergence = false;
76  print_level = 1;
77  init_guess_null = true;
78  type_solver = 0; parameter_restart = 10;
79  type_preconditioning = 0;
80  }
81 
82 
84  template<class Titer>
86  {
87  tolerance = outer.tolerance; facteur_reste = outer.facteur_reste;
88  max_iter = outer.max_iter;
89  nb_iter = 0;
90  fail_convergence = outer.fail_convergence;
91  print_level = outer.print_level; error_code = outer.error_code;
92  init_guess_null = true;
93  type_solver = outer.type_solver;
94  parameter_restart = outer.parameter_restart;
95  type_preconditioning = outer.type_preconditioning;
96  file_name_history = outer.file_name_history;
97  }
98 
99 
101  template<class Titer>
103  {
104  return type_solver;
105  }
106 
107 
109  template<class Titer>
111  {
112  return parameter_restart;
113  }
114 
115 
117  template<class Titer>
119  {
120  return facteur_reste;
121  }
122 
123 
125  template<class Titer>
127  {
128  return tolerance;
129  }
130 
131 
133  template<class Titer>
135  {
136  return nb_iter;
137  }
138 
139 
141  template<class Titer>
142  void Iteration<Titer>::SetSolver(int type_resolution,
143  int param_restart, int type_prec)
144  {
145  type_solver = type_resolution;
146  parameter_restart = param_restart;
147  type_preconditioning = type_prec;
148  }
149 
150 
152  template<class Titer>
154  {
155  parameter_restart = m;
156  }
157 
158 
160  template<class Titer>
161  void Iteration<Titer>::SetTolerance(Titer stopping_criterion)
162  {
163  tolerance = stopping_criterion;
164  }
165 
166 
168  template<class Titer>
170  {
171  max_iter = max_iteration;
172  }
173 
174 
176  template<class Titer>
178  {
179  nb_iter = nb;
180  }
181 
182 
184  template<class Titer>
186  {
187  print_level = 1;
188  }
189 
190 
192  template<class Titer>
194  {
195  print_level = 6;
196  }
197 
198 
200  template<class Titer>
201  void Iteration<Titer>::SaveFullHistory(const string& file_name)
202  {
203  file_name_history = file_name;
204  std::remove(file_name.data());
205  }
206 
207 
209  template<class Titer>
211  {
212  print_level = 0;
213  }
214 
215 
217  template<class Titer> template<class Vector1>
218  int Iteration<Titer>::Init(const Vector1& r)
219  {
220  Titer norme_rhs = Norm2(r);
221  // test of a null right hand side
222  if (norme_rhs == Titer(0))
223  return -1;
224 
225  // coefficient used later to compute relative residual
226  facteur_reste = Titer(1)/norme_rhs;
227 
228  // initialization of iterations
229  nb_iter = 0;
230  return 0; // successful initialization
231  }
232 
233 
235  template<class Titer>
237  {
238  if (nb_iter == 0)
239  return true;
240 
241  return false;
242  }
243 
244 
246  template<class Titer>
248  {
249  return init_guess_null;
250  }
251 
252 
254  template<class Titer>
256  {
257  init_guess_null = type;
258  }
259 
260 
262  template<class Titer> template<class Vector1>
263  bool Iteration<Titer>::
264  Finished(const Vector1& r) const
265  {
266  // absolute residual
267  Titer reste = Norm2(r);
268  // computation of relative residual
269  reste = facteur_reste*reste;
270 
271  // displaying residual if required
272  if ((print_level >= 1)&&(nb_iter%100 == 0))
273  cout<<"Residu at iteration number "<<
274  GetNumberIteration()<<" "<<reste<<endl;
275  else if (print_level >= 6)
276  cout<<"Residu at iteration number "<<
277  GetNumberIteration()<<" "<<reste<<endl;
278 
279  if (file_name_history.size() > 0)
280  {
281  ofstream file_out(file_name_history.data(), ios::app);
282  file_out.setf(ios::scientific);
283  file_out << GetNumberIteration() << " " << reste << '\n';
284  file_out.close();
285  }
286 
287  // end of iterative solver when residual is small enough
288  // or when the number of iterations is too high
289  if ((reste < tolerance)||(nb_iter >= max_iter))
290  return true;
291 
292  return false;
293  }
294 
295 
297  template<class Titer>
298  bool Iteration<Titer>::Finished(const Titer& r) const
299  {
300  // relative residual
301  Titer reste = facteur_reste*r;
302 
303  // displaying residual if required
304  if ((print_level >= 1)&&(nb_iter%100 == 0))
305  cout<<"Residu at iteration number "<<
306  GetNumberIteration()<<" "<<reste<<endl;
307  else if (print_level >= 6)
308  cout<<"Residu at iteration number "<<
309  GetNumberIteration()<<" "<<reste<<endl;
310 
311  if (file_name_history.size() > 0)
312  {
313  ofstream file_out(file_name_history.data(), ios::app);
314  file_out.setf(ios::scientific);
315  file_out << GetNumberIteration() << " " << reste << '\n';
316  file_out.close();
317  }
318 
319  // end of iterative solver when residual is small enough
320  // or when the number of iterations is too high
321  if ((reste < tolerance)||(nb_iter >= max_iter))
322  return true;
323 
324  return false;
325  }
326 
327 
329  template<class Titer>
330  void Iteration<Titer>::Fail(int i, const string& s)
331  {
332  fail_convergence = true;
333  error_code = i;
334  // displays information if required
335  if ((print_level >= 1)&&(nb_iter%100==0))
336  cout<<"Error during resolution : "<<s<<endl;
337  }
338 
339 
341  template<class Titer>
343  {
344  ++nb_iter;
345  return *this;
346  }
347 
348 
350  template<class Titer>
352  {
353  // clearing file where the history has been written
354  file_name_history.clear();
355 
356  if (nb_iter >= max_iter)
357  return -2;
358 
359  if (fail_convergence)
360  return error_code;
361 
362  return 0;
363  }
364 
365 } // end namespace
366 
367 #define SELDON_FILE_ITERATIVE_CXX
368 #endif
369 
Seldon::Iteration::parameter_restart
int parameter_restart
restart parameter (for Gmres and Gcr)
Definition: Iterative.hxx:77
Seldon::Iteration::First
bool First() const
Returns true if it is the first iteration.
Definition: Iterative.cxx:236
Seldon::Iteration::max_iter
int max_iter
maximum number of iterations
Definition: Iterative.hxx:64
Seldon::Iteration::GetNumberIteration
int GetNumberIteration() const
Returns the number of iterations.
Definition: Iterative.cxx:134
Seldon::Iteration::GetRestart
int GetRestart() const
Returns the restart parameter.
Definition: Iterative.cxx:110
Seldon::Iteration::GetTolerance
Titer GetTolerance() const
Returns stopping criterion.
Definition: Iterative.cxx:126
Seldon::Iteration::fail_convergence
bool fail_convergence
true if the iterative solver has converged print level
Definition: Iterative.hxx:67
Seldon::Norm2
ClassComplexType< T >::Treal Norm2(const VectorExpression< T, E > &X)
returns 2-norm of an expression with vectors
Definition: Functions_BaseInline.cxx:153
Seldon::Iteration::type_solver
int type_solver
iterative solver used
Definition: Iterative.hxx:76
Seldon::Iteration::GetTypeSolver
int GetTypeSolver() const
Returns the type of solver.
Definition: Iterative.cxx:102
Seldon::Iteration::SaveFullHistory
void SaveFullHistory(const string &file)
History of residuals is printed in a file.
Definition: Iterative.cxx:201
Seldon::Iteration::SetMaxNumberIteration
void SetMaxNumberIteration(int max_iteration)
Changes the maximum number of iterations.
Definition: Iterative.cxx:169
Seldon::Iteration::Init
int Init(const Vector1 &r)
Initialization with the right hand side.
Definition: Iterative.cxx:218
Seldon::Iteration::operator++
Iteration & operator++(void)
Increment the number of iterations.
Definition: Iterative.cxx:342
Seldon::Iteration::ShowMessages
void ShowMessages()
Sets to a normal display (residual each 100 iterations)
Definition: Iterative.cxx:185
Seldon::Iteration::ErrorCode
int ErrorCode()
Returns the error code (if an error occured)
Definition: Iterative.cxx:351
Seldon::Iteration::Iteration
Iteration()
Default constructor.
Definition: Iterative.cxx:53
Seldon::Iteration::ShowFullHistory
void ShowFullHistory()
Sets to a complete display (residual each iteration)
Definition: Iterative.cxx:193
Seldon::Iteration::GetFactor
Titer GetFactor() const
Returns used coefficient to compute relative residual.
Definition: Iterative.cxx:118
Seldon::Iteration::tolerance
Titer tolerance
stopping criterion
Definition: Iterative.hxx:62
Seldon::Iteration::Fail
void Fail(int i, const string &s)
Informs of a failure in the iterative solver.
Definition: Iterative.cxx:330
Seldon::Iteration::error_code
int error_code
error code returned by iterative solver
Definition: Iterative.hxx:66
Seldon::Iteration::SetTolerance
void SetTolerance(Titer stopping_criterion)
Changes the stopping criterion.
Definition: Iterative.cxx:161
Seldon::Iteration::facteur_reste
Titer facteur_reste
inverse of norm of first residual
Definition: Iterative.hxx:63
Seldon::Iteration::SetNumberIteration
void SetNumberIteration(int nb)
Changes the number of iterations.
Definition: Iterative.cxx:177
Seldon::Iteration::type_preconditioning
int type_preconditioning
preconditioner used
Definition: Iterative.hxx:78
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::Iteration
Class containing parameters for an iterative resolution.
Definition: Iterative.hxx:59
Seldon::Iteration::IsInitGuess_Null
bool IsInitGuess_Null() const
Returns true if the initial guess is null.
Definition: Iterative.cxx:247
Seldon::Iteration::print_level
int print_level
Definition: Iterative.hxx:74
Seldon::Iteration::SetSolver
void SetSolver(int type_resolution, int param_restart, int type_prec)
Changes the type of solver and preconditioning.
Definition: Iterative.cxx:142
Seldon::Iteration::SetRestart
void SetRestart(int m)
Changes the restart parameter.
Definition: Iterative.cxx:153
Seldon::Iteration::HideMessages
void HideMessages()
Doesn't display any information.
Definition: Iterative.cxx:210
Seldon::Iteration::Finished
bool Finished(const Vector1 &r) const
Returns true if the iterative solver has reached its end.
Definition: Iterative.cxx:264
Seldon::Iteration::SetInitGuess
void SetInitGuess(bool type)
informs if the initial guess is null
Definition: Iterative.cxx:255