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