Line data Source code
1 : /*
2 : * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Markus Breit
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_DISC__OPERATOR__COMPOSITE_CONVERGENCE_CHECK__
34 : #define __H__LIB_DISC__OPERATOR__COMPOSITE_CONVERGENCE_CHECK__
35 :
36 : #include <ostream>
37 : #include <sstream>
38 : #include <string>
39 : #include <limits>
40 : #include <algorithm>
41 : #include <iomanip>
42 : #include <vector>
43 : #include <list>
44 : #include <math.h>
45 :
46 : #include "common/common.h"
47 : #include "common/stopwatch.h"
48 : #include "lib_algebra/operator/convergence_check.h"
49 : #include "lib_disc/dof_manager/dof_distribution.h"
50 : #include "lib_disc/function_spaces/approximation_space.h"
51 : #include "lib_disc/common/function_group.h"
52 :
53 : namespace ug{
54 :
55 : ////////////////////////////////////////////////////////////////////////////////
56 : // Composite convergence check
57 : ////////////////////////////////////////////////////////////////////////////////
58 :
59 : /** CompositeConvCheck
60 : *
61 : * This is an implementation of the convergence check interface,
62 : * that makes it possible to define required defect reductions on
63 : * the individual functions constituting the overall solution.
64 : */
65 : template <class TVector, class TDomain>
66 : class CompositeConvCheck : public IConvergenceCheck<TVector>
67 : {
68 : public:
69 : /// constructors
70 : /// \{
71 : CompositeConvCheck(SmartPtr<ApproximationSpace<TDomain> > approx);
72 : CompositeConvCheck(SmartPtr<ApproximationSpace<TDomain> > spApproxSpace,
73 : int maxSteps, number minDefect, number relReduction);
74 : /// \}
75 :
76 : /// destructor
77 0 : virtual ~CompositeConvCheck() {};
78 :
79 : /// set level of grid, where defect vectors come from
80 : void set_level(int level);
81 :
82 : /// sets maximum number of iteration steps
83 0 : void set_maximum_steps(int maxSteps) {m_maxSteps = maxSteps;}
84 :
85 : /// sets default values for non-explicitly specified cmps
86 0 : void set_rest_check(number minDefect, number relReduction){
87 0 : m_bCheckRest = true;
88 0 : m_restMinDefect = minDefect; m_restRelReduction = relReduction;
89 0 : update_rest_check();
90 0 : }
91 :
92 : /// disables rest check
93 0 : void disable_rest_check(){m_bCheckRest = false; update_rest_check();}
94 :
95 : /// sets check for single component
96 : /// \{
97 : void set_component_check(const std::string& vFctName,
98 : const std::vector<number>& vMinDefect,
99 : const std::vector<number>& vRelReduction);
100 :
101 : void set_component_check(const std::vector<std::string>& vFctName,
102 : const std::vector<number>& vMinDefect,
103 : const std::vector<number>& vRelReduction);
104 :
105 : void set_component_check(const std::vector<std::string>& vFctName,
106 : const number minDefect,
107 : const number relReduction);
108 :
109 : void set_component_check(const std::string& fctName,
110 : const number minDefect,
111 : const number relReduction);
112 : /// \}
113 :
114 : /// sets check for all components in approximation space
115 : void set_all_component_check(const number minDefect,
116 : const number relReduction);
117 :
118 : /// sets check for group of components
119 : /// \{
120 : void set_group_check(const std::vector<std::string>& vFctName,
121 : const number minDefect,
122 : const number relReduction);
123 :
124 : void set_group_check(const std::string& fctNames,
125 : const number minDefect,
126 : const number relReduction);
127 : /// \}
128 :
129 : /// defect control
130 : void start_defect(number initialDefect);
131 : void start(const TVector& d);
132 : void update_defect(number newDefect);
133 : void update(const TVector& d);
134 : bool iteration_ended();
135 : bool post();
136 :
137 : /// information about current status
138 0 : int step() const {return m_currentStep;}
139 0 : number defect() const {return defect_all();};
140 0 : number reduction() const {return defect_all()/initial_defect_all();};
141 0 : number rate() const {return defect_all()/last_defect_all();}
142 0 : number avg_rate() const {return std::pow(defect_all()/initial_defect_all(), 1.0/m_currentStep);}
143 :
144 : /// output
145 0 : int get_offset() const {return m_offset;};
146 0 : void set_offset(int offset){m_offset = offset;};
147 0 : void set_symbol(char symbol){m_symbol = symbol;};
148 0 : void set_name(std::string name) {m_name = name;};
149 0 : void set_info(std::string info) {m_info = info;};
150 :
151 : /// sets if verbose
152 0 : void set_verbose(bool level) {m_verbose = level;};
153 :
154 : /// set whether always to report success when max iter is reached (useful for LIMEX)
155 0 : void set_supress_unsuccessful(bool bsupress) {m_supress_unsuccessful = bsupress;}
156 :
157 : /// enables time measurement
158 0 : void set_time_measurement(bool yesOrNo) {m_bTimeMeas = yesOrNo;};
159 :
160 : /// whether or not the underlying approximatioon space is adaptive
161 0 : void set_adaptive(bool adapt) {m_bAdaptive = adapt;}
162 :
163 : /// clones this instance
164 : virtual SmartPtr<IConvergenceCheck<TVector> > clone();
165 :
166 : /// prints a line using prefixes
167 : void print_line(std::string line);
168 :
169 0 : virtual std::string config_string() const
170 : {
171 0 : std::stringstream ss;
172 0 : ss << "CompositeConvCheck( max steps = " << m_maxSteps << ")";
173 0 : ss << " Components:\n";
174 0 : for(size_t i=0; i<m_CmpInfo.size(); i++)
175 0 : ss << " | " << m_CmpInfo[i].config_string() << "\n";
176 0 : return ss.str();
177 0 : }
178 :
179 : protected:
180 : void print_offset();
181 : bool is_valid_number(number value);
182 : const std::string& fctName(size_t i) {return m_CmpInfo[i].name;};
183 :
184 : /// extracts multi-indices for a fct-comp on a element type
185 : template <typename TBaseElem>
186 : void extract_dof_indices(ConstSmartPtr<DoFDistribution> dd);
187 :
188 : /// extracts multi-indices from dof distribution
189 : void extract_dof_indices(ConstSmartPtr<DoFDistribution> dd);
190 :
191 : /// calculates the 2-norm of the entries of the vector vec specified by index
192 : number norm(const TVector& vec, const std::vector<DoFIndex>& index);
193 :
194 : protected:
195 : /// ApproxSpace
196 : SmartPtr<ApproximationSpace<TDomain> > m_spApprox;
197 :
198 0 : struct NativCmpInfo{
199 : std::string name; ///< Name of components
200 :
201 : number initDefect; ///< Initial Defect of component
202 : number currDefect; ///< Current Defect of component
203 : number lastDefect; ///< Last Defect if component
204 :
205 : std::vector<DoFIndex> vMultiIndex; ///< associated indices
206 : };
207 :
208 : /// info on natural components
209 : std::vector<NativCmpInfo> m_vNativCmpInfo;
210 : size_t m_numAllDoFs;
211 :
212 : /// returns defect for all components
213 0 : number defect_all() const {
214 : number defect = 0.0;
215 0 : for(size_t fct = 0; fct < m_vNativCmpInfo.size(); ++fct)
216 0 : defect += pow(m_vNativCmpInfo[fct].currDefect, 2);
217 0 : return sqrt(defect);
218 : }
219 :
220 : /// returns last defect for all components
221 0 : number last_defect_all() const {
222 : number defect = 0.0;
223 0 : for(size_t fct = 0; fct < m_vNativCmpInfo.size(); ++fct)
224 0 : defect += pow(m_vNativCmpInfo[fct].lastDefect, 2);
225 0 : return sqrt(defect);
226 : }
227 :
228 : /// returns initial defect for all components
229 0 : number initial_defect_all() const {
230 : number defect = 0.0;
231 0 : for(size_t fct = 0; fct < m_vNativCmpInfo.size(); ++fct)
232 0 : defect += pow(m_vNativCmpInfo[fct].initDefect, 2);
233 0 : return sqrt(defect);
234 : }
235 :
236 : protected:
237 0 : struct CmpInfo
238 : {
239 0 : CmpInfo() : isRest(false) {}
240 :
241 : std::vector<int> vFct; ///< Index of components
242 : std::string name; ///< Name of components
243 :
244 : number initDefect; ///< Initial Defect of component
245 : number currDefect; ///< Current Defect of component
246 : number lastDefect; ///< Last Defect if component
247 :
248 : number minDefect; ///< Minimal required Defect of component
249 : number relReduction;///< Relative reduction required for component
250 :
251 : bool isRest; ///< Shows, that this is group of remaining cmps
252 :
253 0 : std::string config_string() const
254 : {
255 0 : std::stringstream ss;
256 0 : if(isRest) ss << "[Remaining Components]";
257 : else ss << "Component " << name;
258 0 : ss << ": minDefect = " << minDefect << ", relReduction = " << relReduction;
259 0 : return ss.str();
260 0 : }
261 :
262 : };
263 :
264 : /// infos for each component
265 : std::vector<CmpInfo> m_CmpInfo;
266 :
267 : /// default Values
268 : bool m_bCheckRest;
269 : number m_restMinDefect;
270 : number m_restRelReduction;
271 : void update_rest_check();
272 :
273 : /// current step
274 : int m_currentStep;
275 :
276 : /// maximum number of steps to be performed
277 : int m_maxSteps;
278 :
279 : protected:
280 : /// verbose level
281 : bool m_verbose;
282 :
283 : /// whether to always report success once max iter is reached
284 : bool m_supress_unsuccessful;
285 :
286 : /// number of spaces inserted before output
287 : int m_offset;
288 :
289 : /// symbol for output appearance
290 : char m_symbol;
291 :
292 : /// name of iteration
293 : std::string m_name;
294 :
295 : /// info for iteration (e.g. preconditioner type)
296 : std::string m_info;
297 :
298 : protected:
299 : /// enables time measurement
300 : bool m_bTimeMeas;
301 :
302 : /// a stopwatch
303 : Stopwatch m_stopwatch;
304 :
305 : // TODO: maybe solve this using an adaption/distribution event listener
306 : /// adaptivity flag
307 : bool m_bAdaptive;
308 : };
309 :
310 : } // end namespace ug
311 :
312 : #include "composite_conv_check_impl.h"
313 :
314 : #endif /* __H__LIB_DISC__OPERATOR__COMPOSITE_CONVERGENCE_CHECK__ */
|