ug4
convergence_check.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3  * Author: Andreas Vogel
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 /*
34  * Comment: Based on an idea by Arne Naegel
35  */
36 
37 #ifndef __H__LIB_ALGEBRA__OPERATOR__CONVERGENCE_CHECK__
38 #define __H__LIB_ALGEBRA__OPERATOR__CONVERGENCE_CHECK__
39 
40 #include <ostream>
41 #include <sstream>
42 #include <string>
43 #include <limits>
44 #include <algorithm>
45 #include <iomanip>
46 #include <vector>
47 #include <list>
48 #include <math.h>
49 
50 #include "common/common.h"
51 #include "common/stopwatch.h"
54 
55 namespace ug{
56 
59 // Convergence check
62 
70 template <typename TVector>
72 {
73  public:
75  virtual void start_defect(number defect) = 0;
76 
78  virtual void start(const TVector& d) = 0;
79 
81  virtual void update_defect(number defect) = 0;
82 
84  virtual void update(const TVector& d) = 0;
85 
94  virtual bool iteration_ended() = 0;
95 
104  virtual bool post() = 0;
105 
107  // informations about current status
109 
111  virtual number defect() const = 0;
112 
114  virtual int step() const = 0;
115 
116  // returns the current relative reduction
117  virtual number reduction() const = 0;
118 
119  // returns the current convergence rate
120  virtual number rate() const = 0;
121 
122  // returns the averaged convergence rate
123  virtual number avg_rate() const = 0;
124 
126  // output style
128 
130  virtual void set_offset(int offset) = 0;
131 
133  virtual int get_offset() const = 0;
134 
136  virtual void set_symbol(char symbol) = 0;
137 
139  virtual void set_name(std::string name) = 0;
140 
142  virtual void set_info(std::string name) = 0;
143 
145  virtual void print_line(std::string line) = 0;
146 
149 
151  virtual ~IConvergenceCheck() {};
152 
154 
161  virtual std::string config_string() const = 0;
162 };
163 
164 
167 // Standard convergence check
170 
177 template <typename TVector>
178 class StdConvCheck : public IConvergenceCheck<TVector>
179 {
180  public:
181  StdConvCheck();
182 
183  StdConvCheck(int maxSteps, number minDefect, number relReduction);
184  StdConvCheck(int maxSteps, number minDefect, number relReduction, bool verbose);
185  StdConvCheck(int maxSteps, number minDefect, number relReduction, bool verbose,bool suppressUnsuccessful);
186 
187  void set_verbose(bool level) {m_verbose = level;}
188  void set_maximum_steps(int maxSteps) {m_maxSteps = maxSteps;}
189  void set_minimum_defect(number minDefect) {m_minDefect = minDefect;}
190  void set_reduction(number relReduction) {m_relReduction = relReduction;}
191  void set_supress_unsuccessful(bool bsupress){ m_supress_unsuccessful = bsupress; }
192 
193  void start_defect(number initialDefect);
194 
195  void start(const TVector& d);
196 
197  void update_defect(number newDefect);
198 
199  void update(const TVector& d);
200 
201  bool iteration_ended();
202 
203  bool post();
204 
205  virtual std::string config_string() const
206  {
207  std::stringstream ss;
208  ss << "StdConvCheck( max steps = " << m_maxSteps << ", min defect = " << m_minDefect << ", relative reduction = " << m_relReduction << ")";
209  return ss.str();
210  }
211 
213  number defect() const {return m_currentDefect;};
214  number previous_defect() const { return m_lastDefect; }
215  int step() const {return m_currentStep;}
217  number avg_rate() const {return std::pow((number)m_ratesProduct,(number)1.0/step());}
218 
219  int get_offset() const {return m_offset;}
220  void set_offset(int offset){m_offset = offset;}
221  void set_symbol(char symbol){m_symbol = symbol;}
222  void set_name(std::string name) {m_name = name;}
223  void set_info(std::string info) {m_info = info;}
224  const std::vector<number> get_defects() const { return _defects;}
225  number get_defect(size_t i) const { return _defects[i];}
226  void print_line(std::string line);
227 
229  {
231  // use std assignment (implicit member-wise is fine here)
232  *newInst = *this;
233  return newInst;
234  }
235 
236  protected:
237  void print_offset();
238 
239  bool is_valid_number(number value);
240 
241  protected:
242  // start defect
244 
245  // current defect
247 
248  // defect of the previous step
250 
251  // current step
253 
254  // sum of the convergence rates over all steps
256 
257  protected:
258  // maximum number of steps to be performed
260 
261  // absolute reduction to be reached for convergence
263 
264  // relative reduction to be reached for convergence
266 
267  // verbose level
268  bool m_verbose;
269 
270  // number of spaces inserted before output
271  int m_offset;
272 
273  // symbol for output appearance
274  char m_symbol;
275 
276  // name of iteration
277  std::string m_name;
278 
279  // info for iteration (e.g. preconditioner type)
280  std::string m_info;
281 
282  // return true in post method if max nr of iterations is reached
284 
285  private:
286  std::vector<number> _defects;
287 };
288 
289 } // end namespace ug
290 
291 #include "convergence_check_impl.h"
292 
293 #endif /* __H__LIB_ALGEBRA__OPERATOR__CONVERGENCE_CHECK__ */
location name
Definition: checkpoint_util.lua:128
location verbose
Definition: checkpoint_util.lua:128
Definition: smart_pointer.h:108
Definition: convergence_check.h:72
virtual void start(const TVector &d)=0
computes the start defect and set it
virtual std::string config_string() const =0
returns information about configuration parameters
virtual void update(const TVector &d)=0
computes the defect and sets it a the next defect value
virtual ~IConvergenceCheck()
virtual destructor
Definition: convergence_check.h:151
virtual number defect() const =0
returns the current defect
virtual int step() const =0
returns the current number of steps
virtual void set_name(std::string name)=0
sets the name of the iteration
virtual void update_defect(number defect)=0
sets the update for the current defect
virtual int get_offset() const =0
get the current offset
virtual void set_symbol(char symbol)=0
sets the symbol used for output
virtual void print_line(std::string line)=0
prints a line
virtual void set_offset(int offset)=0
sets the number of spaces printed before output information
virtual number avg_rate() const =0
virtual number reduction() const =0
virtual bool iteration_ended()=0
virtual SmartPtr< IConvergenceCheck< TVector > > clone()=0
clone the object
virtual void set_info(std::string name)=0
sets info string
virtual bool post()=0
virtual number rate() const =0
virtual void start_defect(number defect)=0
sets the given start defect
Definition: convergence_check.h:179
number get_defect(size_t i) const
Definition: convergence_check.h:225
std::string m_info
Definition: convergence_check.h:280
number m_minDefect
Definition: convergence_check.h:262
number previous_defect() const
Definition: convergence_check.h:214
void set_maximum_steps(int maxSteps)
Definition: convergence_check.h:188
int step() const
returns the current number of steps
Definition: convergence_check.h:215
number m_currentDefect
Definition: convergence_check.h:246
void set_supress_unsuccessful(bool bsupress)
Definition: convergence_check.h:191
int get_offset() const
get the current offset
Definition: convergence_check.h:219
char m_symbol
Definition: convergence_check.h:274
void set_reduction(number relReduction)
Definition: convergence_check.h:190
bool m_verbose
Definition: convergence_check.h:268
number m_lastDefect
Definition: convergence_check.h:249
int m_maxSteps
Definition: convergence_check.h:259
bool is_valid_number(number value)
Definition: convergence_check_impl.h:247
void set_verbose(bool level)
Definition: convergence_check.h:187
bool m_supress_unsuccessful
Definition: convergence_check.h:283
void update_defect(number newDefect)
sets the update for the current defect
Definition: convergence_check_impl.h:140
void set_symbol(char symbol)
sets the symbol used for output
Definition: convergence_check.h:221
virtual std::string config_string() const
returns information about configuration parameters
Definition: convergence_check.h:205
const std::vector< number > get_defects() const
Definition: convergence_check.h:224
std::string m_name
Definition: convergence_check.h:277
void update(const TVector &d)
computes the defect and sets it a the next defect value
Definition: convergence_check_impl.h:156
void set_name(std::string name)
sets the name of the iteration
Definition: convergence_check.h:222
number avg_rate() const
Definition: convergence_check.h:217
number m_relReduction
Definition: convergence_check.h:265
number rate() const
Definition: convergence_check.h:216
virtual SmartPtr< IConvergenceCheck< TVector > > clone()
clone the object
Definition: convergence_check.h:228
void print_offset()
Definition: convergence_check_impl.h:229
StdConvCheck()
Definition: convergence_check_impl.h:50
void set_offset(int offset)
sets the number of spaces printed before output information
Definition: convergence_check.h:220
int m_offset
Definition: convergence_check.h:271
std::vector< number > _defects
Definition: convergence_check.h:286
void set_info(std::string info)
sets info string
Definition: convergence_check.h:223
number m_ratesProduct
Definition: convergence_check.h:255
void start(const TVector &d)
computes the start defect and set it
Definition: convergence_check_impl.h:134
number defect() const
returns the current defect
Definition: convergence_check.h:213
int m_currentStep
Definition: convergence_check.h:252
bool iteration_ended()
Definition: convergence_check_impl.h:162
bool post()
Definition: convergence_check_impl.h:172
void print_line(std::string line)
prints a line
Definition: convergence_check_impl.h:239
void start_defect(number initialDefect)
sets the given start defect
Definition: convergence_check_impl.h:85
void set_minimum_defect(number minDefect)
Definition: convergence_check.h:189
number reduction() const
Definition: convergence_check.h:212
number m_initialDefect
Definition: convergence_check.h:243
double number
Definition: types.h:124
the ug namespace
stopwatch class for quickly taking times