ug4
convergence_check_impl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-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 #ifndef __H__LIB_ALGEBRA__OPERATOR__CONVERGENCE_CHECK_IMPL__
34 #define __H__LIB_ALGEBRA__OPERATOR__CONVERGENCE_CHECK_IMPL__
35 
36 #include "convergence_check.h"
38 
39 namespace ug{
40 
41 
44 // Standard convergence check //
47 
48 template <typename TVector>
51  : m_initialDefect(0.0), m_currentDefect(0.0), m_lastDefect(0.0), m_currentStep(0),
52  m_ratesProduct(1), m_maxSteps(200), m_minDefect(10e-8), m_relReduction(10e-10),
53  m_verbose(true), m_offset(0), m_symbol('%'), m_name("Iteration"), m_info(""),
54  m_supress_unsuccessful(false)
55  {};
56 
57 template <typename TVector>
59 StdConvCheck(int maxSteps, number minDefect, number relReduction)
60  : m_initialDefect(0.0), m_currentDefect(0.0), m_lastDefect(0.0), m_currentStep(0),
61  m_ratesProduct(1), m_maxSteps(maxSteps), m_minDefect(minDefect), m_relReduction(relReduction),
62  m_verbose(true), m_offset(0), m_symbol('%'), m_name("Iteration"), m_info(""),
63  m_supress_unsuccessful(false)
64  {};
65 
66 template <typename TVector>
68 StdConvCheck(int maxSteps, number minDefect, number relReduction, bool verbose)
69  : m_initialDefect(0.0), m_currentDefect(0.0), m_lastDefect(0.0), m_currentStep(0),
70  m_ratesProduct(1), m_maxSteps(maxSteps), m_minDefect(minDefect), m_relReduction(relReduction),
71  m_verbose(verbose), m_offset(0), m_symbol('%'), m_name("Iteration"), m_info(""),
72  m_supress_unsuccessful(false)
73  {};
74 
75 template <typename TVector>
77 StdConvCheck(int maxSteps, number minDefect, number relReduction, bool verbose,bool supressUnsuccessful)
78  : m_initialDefect(0.0), m_currentDefect(0.0), m_lastDefect(0.0), m_currentStep(0),
79  m_ratesProduct(1), m_maxSteps(maxSteps), m_minDefect(minDefect), m_relReduction(relReduction),
80  m_verbose(verbose), m_offset(0), m_symbol('%'), m_name("Iteration"), m_info(""),
81  m_supress_unsuccessful(supressUnsuccessful)
82  {};
83 
84 template <typename TVector>
86 {
87  _defects.clear();
88  m_initialDefect = initialDefect;
89  m_currentDefect = m_initialDefect;
90  m_currentStep = 0;
91  m_ratesProduct = 1;
92 
93  if(m_verbose)
94  {
95  UG_LOG("\n");
96 
97  // number of symbols to print before name and info
98  int num_sym = 8;
99  int num_line_length = 50;
100 
101  int max_length = std::max(m_name.length(), m_info.length());
102  int space_left = std::max(num_line_length - max_length - num_sym, 0);
103 
104  // print name line
105  print_offset();
106  UG_LOG(repeat(m_symbol, num_sym));
107  int pre_space = (int)(max_length -(int)m_name.length()) / 2;
108  UG_LOG(repeat(' ', pre_space));
109  UG_LOG(" "<< m_name << " ");
110  UG_LOG(repeat(' ', max_length - pre_space -m_name.length()));
111  UG_LOG(repeat(m_symbol, space_left));
112  UG_LOG("\n");
113  // print info line
114  print_offset();
115  if(m_info.length() > 0)
116  {
117  UG_LOG(repeat(m_symbol, num_sym));
118  UG_LOG(" "<< m_info << " ");
119  UG_LOG(repeat(' ', max_length-m_info.length()));
120  UG_LOG(repeat(m_symbol, space_left))
121  UG_LOG("\n");
122  } else {
123  UG_LOG("\n");
124  }
125 
126  // start iteration output
127  print_offset(); UG_LOG(" Iter Defect Rate \n");
128  print_offset(); UG_LOG(std::setw(4) << step() << ": "
129  << std::scientific << defect() << " -------\n");
130  }
131 }
132 
133 template <typename TVector>
134 void StdConvCheck<TVector>::start(const TVector& d)
135 {
136  start_defect(d.norm());
137 }
138 
139 template <typename TVector>
141 {
142  m_lastDefect = m_currentDefect;
143  m_currentDefect = newDefect;
144  m_currentStep++;
145  m_ratesProduct *= newDefect/m_lastDefect;
146 
147  if(m_verbose)
148  {
149  print_offset(); UG_LOG(std::setw(4) << step() << ": " << std::scientific << defect() <<
150  " " << defect()/m_lastDefect << "\n");
151  _defects.push_back(defect());
152  }
153 }
154 
155 template <typename TVector>
156 void StdConvCheck<TVector>::update(const TVector& d)
157 {
158  update_defect(d.norm());
159 }
160 
161 template <typename TVector>
163 {
164  if(!is_valid_number(m_currentDefect)) return true;
165  if(step() >= m_maxSteps) return true;
166  if(defect() < m_minDefect) return true;
167  if(reduction() < m_relReduction) return true;
168  return false;
169 }
170 
171 template <typename TVector>
173 {
174  bool success = false;
175 
176  if(defect() < m_minDefect)
177  {
178  if(m_verbose)
179  {
180  print_offset(); UG_LOG("Absolute defect norm " << m_minDefect << " reached after " << step() << " steps.\n");
181  }
182  success = true;
183  };
184 
185  if(reduction() < m_relReduction)
186  {
187  if(m_verbose)
188  {
189  print_offset(); UG_LOG("Relative reduction " << m_relReduction << " reached after " << step() << " steps.\n");
190  }
191  success = true;
192  };
193 
194  if (m_verbose && is_valid_number(m_currentDefect))
195  {
196  print_offset(); UG_LOG("Average reduction over " << step() << " steps: " << pow(reduction(), 1.0/step()) << "\n");
197  }
198 
199  if(!success)
200  {
201  if (!is_valid_number(m_currentDefect))
202  if(m_verbose)
203  {
204  print_offset(); UG_LOG("Current defect " << m_currentDefect << " is not a valid number.\n");
205  }
206 
207  if(step() >= m_maxSteps){
208  if(m_verbose)
209  {
210  print_offset(); UG_LOG("Maximum numbers of "<< m_maxSteps << " iterations reached without convergence.\n");
211  }
212  if (m_supress_unsuccessful) return true;
213  }
214  }
215 
216  if(m_verbose)
217  {
218  print_offset();
219  UG_LOG(repeat(m_symbol, 5));
220  if(success) {UG_LOG(" Iteration converged ");}
221  else {UG_LOG(" Iteration not successful ");}
222  UG_LOG(repeat(m_symbol, 5));
223  UG_LOG("\n\n");
224  }
225  return success;
226 }
227 
228 template <typename TVector>
230 {
231  // step 1: whitespace
232  UG_LOG(repeat(' ', m_offset));
233 
234  // step 2: print style character
235  UG_LOG(m_symbol << " ");
236 }
237 
238 template <typename TVector>
239 void StdConvCheck<TVector>::print_line(std::string line)
240 {
241  print_offset();
242  UG_LOG(line << "\n");
243 }
244 
245 
246 template <typename TVector>
248 {
249  // (value >= std::numeric_limits<number>::min() ) == true if value > -infty
250  // (value <= std::numeric_limits<number>::max() ) == true if value < infty
251  // (value == value ) == true if value != NaN
252 
253  if (value == 0.0) return true;
254  else return (value >= std::numeric_limits<number>::min()
255  && value <= std::numeric_limits<number>::max()
256  && value == value && value >= 0.0);
257 }
258 
259 } // end namespace ug
260 
261 #endif /* __H__LIB_ALGEBRA__OPERATOR__CONVERGENCE_CHECK_IMPL__ */
location verbose
Definition: checkpoint_util.lua:128
bool is_valid_number(number value)
Definition: convergence_check_impl.h:247
void update_defect(number newDefect)
sets the update for the current defect
Definition: convergence_check_impl.h:140
void update(const TVector &d)
computes the defect and sets it a the next defect value
Definition: convergence_check_impl.h:156
void print_offset()
Definition: convergence_check_impl.h:229
StdConvCheck()
Definition: convergence_check_impl.h:50
void start(const TVector &d)
computes the start defect and set it
Definition: convergence_check_impl.h:134
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
string repeat(char c, int nr)
Builds a string with specified repetitions of given character.
Definition: string_util.cpp:346
#define UG_LOG(msg)
Definition: log.h:367
double number
Definition: types.h:124
the ug namespace