ug4
algebra_conv_check.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2015: G-CSC, Goethe University Frankfurt
3  * Author: Arne Nägel
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  * algebraic version of composite_conv_check.h by M. Breit
35  */
36 
37 #ifndef __H__LIB_ALGEBRA__OPERATOR__ALGEBRA_CONVERGENCE_CHECK__
38 #define __H__LIB_ALGEBRA__OPERATOR__ALGEBRA_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"
55 
56 namespace ug{
57 
59 // Composite convergence check
61 
68 template <class TVector>
69 class AlgebraicConvCheck : public IConvergenceCheck<TVector>
70 {
71  public:
74  AlgebraicConvCheck(size_t ncmp);
75  AlgebraicConvCheck(size_t ncmp, int maxSteps, number minDefect, number relReduction);
76  AlgebraicConvCheck(size_t ncmp, int maxSteps, number minDefect, number relReduction, bool verbose);
78 
81  virtual std::string config_string() const {return std::string("AlgebraicConvCheck");}
82 
84  void start_defect(number initialDefect);
85  void start(const TVector& d);
86  void update_defect(number newDefect);
87  void update(const TVector& d);
88  bool iteration_ended();
89  bool post();
90 
92  int step() const {return m_currentStep;}
93  number defect() const {return defect_all();};
95  number rate() const {return defect_all()/last_defect_all();}
96  number avg_rate() const {return std::pow(defect_all()/initial_defect_all(), 1.0/m_currentStep);}
97 
99  int get_offset() const {return m_offset;};
100  void set_offset(int offset){m_offset = offset;};
101  void set_symbol(char symbol){m_symbol = symbol;};
102  void set_name(std::string name) {m_name = name;};
103  void set_info(std::string info) {m_info = info;};
104 
105 
107  void set_maximum_steps(int maxSteps) {m_maxSteps = maxSteps;}
108 
110  inline void set_component_check(const size_t cmp,
111  const number abs,
112  const number red)
113  {
114  m_vCmpInfo[cmp].minDefect = abs;
115  m_vCmpInfo[cmp].relReduction = red;
116  }
117 
119  void set_component_checks(const number abs, const number red)
120  {
121  for(size_t cmp = 0; cmp < m_vCmpInfo.size(); ++cmp)
122  set_component_check(cmp, abs, red);
123  }
124 
126  void set_verbose(bool level) {m_verbose = level;};
127 
129  void set_time_measurement(bool yesOrNo) {m_bTimeMeas = yesOrNo;};
130 
132  void print_line(std::string line);
133 
134 
136  void get_statistics(double *first, double *last, int &niter) const
137  {
138  for (size_t cmp = 0; cmp < m_vCmpInfo.size(); cmp++)
139  {
140  const CmpInfo& cmpInfo = m_vCmpInfo[cmp];
141  first[cmp] = cmpInfo.initDefect;
142  last[cmp] = cmpInfo.currDefect;
143  }
144  niter = step();
145  };
146  protected:
147  void print_offset();
148  bool is_valid_number(number value);
149  const std::string& fctName(size_t i) {return m_vCmpInfo[i].name;};
150 
152  number norm(const TVector& vec, size_t cmp);
153 
154  protected:
155 
156  struct CmpInfo{
157  CmpInfo(number minDef, number relRed) : minDefect(minDef), relReduction(relRed) {}
158  std::string name;
159 
163 
167  };
168 
170  std::vector<CmpInfo> m_vCmpInfo;
171 
172 
173 
174 
175 
177  number defect_all() const {
178  number defect = 0.0;
179  for(size_t fct = 0; fct < m_vCmpInfo.size(); ++fct)
180  defect += (m_vCmpInfo[fct].currDefect*m_vCmpInfo[fct].currDefect);
181  return sqrt(defect);
182  }
183 
186  number defect = 0.0;
187  for(size_t fct = 0; fct < m_vCmpInfo.size(); ++fct)
188  defect += (m_vCmpInfo[fct].lastDefect*m_vCmpInfo[fct].lastDefect);
189  return sqrt(defect);
190  }
191 
194  number defect = 0.0;
195  for(size_t fct = 0; fct < m_vCmpInfo.size(); ++fct)
196  defect += m_vCmpInfo[fct].initDefect*m_vCmpInfo[fct].initDefect;
197  return sqrt(defect);
198  }
199 
200  protected:
201 
202 
206 
207  bool m_verbose;
209 
210 
211  int m_offset;
212  char m_symbol;
213  std::string m_name;
214  std::string m_info;
215 
218 };
219 
220 } // end namespace ug
221 
222 #include "algebra_conv_check_impl.h"
223 
224 #endif /* __H__LIB_DISC__OPERATOR__COMPOSITE_CONVERGENCE_CHECK__ */
location name
Definition: checkpoint_util.lua:128
location verbose
Definition: checkpoint_util.lua:128
Definition: smart_pointer.h:108
Definition: algebra_conv_check.h:70
const std::string & fctName(size_t i)
Definition: algebra_conv_check.h:149
virtual SmartPtr< IConvergenceCheck< TVector > > clone()
clones this instance
Definition: algebra_conv_check_impl.h:87
Stopwatch m_stopwatch
a stopwatch
Definition: algebra_conv_check.h:217
number m_relReduction
Relative reduction required for component.
Definition: algebra_conv_check.h:205
void update(const TVector &d)
computes the defect and sets it a the next defect value
Definition: algebra_conv_check_impl.h:193
void get_statistics(double *first, double *last, int &niter) const
statistics
Definition: algebra_conv_check.h:136
int m_currentStep
current step
Definition: algebra_conv_check.h:208
void set_maximum_steps(int maxSteps)
sets maximum number of iteration steps
Definition: algebra_conv_check.h:107
bool is_valid_number(number value)
Definition: algebra_conv_check_impl.h:339
number norm(const TVector &vec, size_t cmp)
calculates the 2-norm of the entries of the vector vec specified by index
Definition: algebra_conv_check_impl.h:353
void update_defect(number newDefect)
sets the update for the current defect
Definition: algebra_conv_check_impl.h:183
std::string m_info
info for iteration (e.g. preconditioner type)
Definition: algebra_conv_check.h:214
bool iteration_ended()
Definition: algebra_conv_check_impl.h:227
number reduction() const
Definition: algebra_conv_check.h:94
void set_symbol(char symbol)
sets the symbol used for output
Definition: algebra_conv_check.h:101
void set_component_checks(const number abs, const number red)
sets check for all components
Definition: algebra_conv_check.h:119
number defect() const
returns the current defect
Definition: algebra_conv_check.h:93
void start_defect(number initialDefect)
defect control
Definition: algebra_conv_check_impl.h:99
virtual std::string config_string() const
returns information about configuration parameters
Definition: algebra_conv_check.h:81
AlgebraicConvCheck(size_t ncmp)
Definition: algebra_conv_check_impl.h:51
int m_offset
number of spaces inserted before output
Definition: algebra_conv_check.h:211
char m_symbol
symbol for output appearance
Definition: algebra_conv_check.h:212
void set_name(std::string name)
sets the name of the iteration
Definition: algebra_conv_check.h:102
number m_minDefect
Minimal required Defect of component.
Definition: algebra_conv_check.h:204
number avg_rate() const
Definition: algebra_conv_check.h:96
void set_time_measurement(bool yesOrNo)
enables time measurement
Definition: algebra_conv_check.h:129
bool m_bTimeMeas
enables time measurement
Definition: algebra_conv_check.h:216
number last_defect_all() const
returns last defect for all components
Definition: algebra_conv_check.h:185
void print_line(std::string line)
prints a line using prefixes
Definition: algebra_conv_check_impl.h:331
int step() const
information about current status
Definition: algebra_conv_check.h:92
void start(const TVector &d)
computes the start defect and set it
Definition: algebra_conv_check_impl.h:109
void set_offset(int offset)
sets the number of spaces printed before output information
Definition: algebra_conv_check.h:100
void print_offset()
Definition: algebra_conv_check_impl.h:321
std::string m_name
name of iteration
Definition: algebra_conv_check.h:213
int get_offset() const
output
Definition: algebra_conv_check.h:99
bool post()
Definition: algebra_conv_check_impl.h:252
void set_component_check(const size_t cmp, const number abs, const number red)
sets check for single component
Definition: algebra_conv_check.h:110
number initial_defect_all() const
returns initial defect for all components
Definition: algebra_conv_check.h:193
std::vector< CmpInfo > m_vCmpInfo
info on components
Definition: algebra_conv_check.h:170
int m_maxSteps
maximum number of steps to be performed
Definition: algebra_conv_check.h:203
void set_verbose(bool level)
sets if verbose
Definition: algebra_conv_check.h:126
bool m_verbose
verbose level
Definition: algebra_conv_check.h:207
number defect_all() const
returns defect for all components
Definition: algebra_conv_check.h:177
number rate() const
Definition: algebra_conv_check.h:95
void set_info(std::string info)
sets info string
Definition: algebra_conv_check.h:103
Definition: convergence_check.h:72
Stopwatch class for quickly taking times.
Definition: stopwatch.h:124
double number
Definition: types.h:124
the ug namespace
stopwatch class for quickly taking times
Definition: algebra_conv_check.h:156
number minDefect
Minimal required Defect of component.
Definition: algebra_conv_check.h:164
number weight
weight for this component
Definition: algebra_conv_check.h:166
number initDefect
Initial Defect of component.
Definition: algebra_conv_check.h:160
number lastDefect
Last Defect if component.
Definition: algebra_conv_check.h:162
number currDefect
Current Defect of component.
Definition: algebra_conv_check.h:161
number relReduction
Relative reduction required for component.
Definition: algebra_conv_check.h:165
CmpInfo(number minDef, number relRed)
Definition: algebra_conv_check.h:157
std::string name
Name of components.
Definition: algebra_conv_check.h:158