37 #ifndef __H__LIB_ALGEBRA__OPERATOR__ALGEBRA_CONV_CHECK_IMPL__
38 #define __H__LIB_ALGEBRA__OPERATOR__ALGEBRA_CONV_CHECK_IMPL__
50 template <
class TVector>
53 m_vCmpInfo(ncmp,
CmpInfo(1e-12, 1e-10)),
54 m_maxSteps(100), m_minDefect(1e-12), m_relReduction(1e-10), m_verbose(true),
55 m_currentStep(0), m_offset(0), m_symbol(
'%'), m_name(
"Iteration"), m_info(
""),
60 template <
class TVector>
63 : m_maxSteps(maxSteps), m_minDefect(minDefect), m_relReduction(relReduction), m_verbose(true),
64 m_currentStep(0), m_offset(0), m_symbol(
'%'), m_name(
"Iteration"), m_info(
""),
65 m_bTimeMeas(true), m_vCmpInfo(ncmp,
CmpInfo(minDefect, relReduction))
71 template <
class TVector>
74 : m_maxSteps(maxSteps), m_minDefect(minDefect), m_relReduction(relReduction), m_verbose(
verbose),
75 m_currentStep(0), m_offset(0), m_symbol(
'%'), m_name(
"Iteration"), m_info(
""),
86 template <
class TVector>
98 template <
class TVector>
101 UG_THROW(
"This method cannot be used to set defect values,\n"
102 "since obviously this class is meant for an individual\n"
103 "defect calculation of more than one function\n"
104 "(use start(TVector& d) instead).");
108 template <
class TVector>
112 if (m_bTimeMeas) m_stopwatch.start();
115 for (
size_t fct = 0; fct < m_vCmpInfo.size(); fct++){
116 m_vCmpInfo[fct].currDefect = m_vCmpInfo[fct].initDefect = norm(vec, fct);
127 int num_line_length = 80;
129 int max_length = std::max(m_name.length(), m_info.length());
130 int space_left = std::max(num_line_length - max_length - num_sym, 0);
135 int pre_space = (int)(max_length -(
int)m_name.length()) / 2;
137 UG_LOG(
" "<< m_name <<
" ");
138 UG_LOG(
repeat(
' ', max_length - pre_space -m_name.length()));
143 if (m_info.length() > 0)
146 UG_LOG(
" "<< m_info <<
" ");
158 UG_LOG(
" Iter Defect Rate \n");
160 for (
size_t cmp = 0; cmp < m_vCmpInfo.size(); cmp++)
162 CmpInfo& cmpInfo = m_vCmpInfo[cmp];
165 if(cmp != 0) {
UG_LOG(
" " )}
166 else {
UG_LOG(std::setw(4) << step() <<
": ");}
170 UG_LOG(std::scientific <<
" ----- " <<
" c=" << cmp << std::endl)
182 template <
class TVector>
185 UG_THROW(
"This method cannot be used to update defect values,\n"
186 "since obviously this class is meant for an individual\n"
187 "defect calculation of more than one function\n"
188 "(use update(TVector& d) instead).");
192 template <
class TVector>
196 for (
size_t fct = 0; fct < m_vCmpInfo.size(); fct++){
197 m_vCmpInfo[fct].lastDefect = m_vCmpInfo[fct].currDefect;
198 m_vCmpInfo[fct].currDefect = norm(vec, fct);
207 for (
size_t cmp = 0; cmp < m_vCmpInfo.size(); cmp++)
209 CmpInfo& cmpInfo = m_vCmpInfo[cmp];
212 if(cmp != 0) {
UG_LOG(
" " )}
213 else {
UG_LOG(std::setw(4) << step() <<
": ");}
217 UG_LOG(std::scientific << std::setprecision(6) << cmpInfo.
currDefect / cmpInfo.
lastDefect <<
" c="<< cmp<< std::setprecision(6) << std::endl)
219 UG_LOG(std::scientific <<
" ----- " <<
" c="<< cmp << std::endl)
226 template <
class TVector>
229 if (step() >= m_maxSteps)
return true;
232 for (
size_t cmp = 0; cmp < m_vCmpInfo.size(); cmp++)
234 CmpInfo& cmpInfo = m_vCmpInfo[cmp];
236 if (!is_valid_number(cmpInfo.
currDefect))
return true;
238 bool cmpFinished =
false;
244 ended = ended && cmpFinished;
251 template <
class TVector>
254 if (m_bTimeMeas) m_stopwatch.stop();
258 if (step() >= m_maxSteps){
260 UG_LOG(
"Maximum numbers of "<< m_maxSteps <<
261 " iterations reached without convergence.\n");
265 for (
size_t cmp = 0; cmp < m_vCmpInfo.size(); cmp++)
267 CmpInfo& cmpInfo = m_vCmpInfo[cmp];
273 UG_LOG(
"Current defect for '" << cmpInfo.
name <<
274 "' is not a valid number.\n");
278 bool cmpFinished =
false;
283 << cmpInfo.
name <<
"' reached after " << step() <<
" steps.\n");
292 << cmpInfo.
name <<
"' reached after " << step() <<
" steps.\n");
297 if(!cmpFinished) success =
false;
303 std::stringstream tmsg;
306 number time = m_stopwatch.ms()/1000.0;
307 tmsg <<
" (t: " << std::setprecision(3) << time <<
"s; t/it: "
308 << time / step() <<
"s)";
310 if (success) {
UG_LOG(
" Iteration converged" << tmsg.str() <<
" ");}
311 else {
UG_LOG(
" Iteration not successful" << tmsg.str() <<
" ");}
320 template <
class TVector>
330 template <
class TVector>
338 template <
class TVector>
341 if (value == 0.0)
return true;
342 else return (value >= std::numeric_limits<number>::min()
343 && value <= std::numeric_limits<number>::max()
344 && value == value && value >= 0.0);
351 template <
class TVector>
353 norm(
const TVector& vec,
size_t cmp)
358 if (!
const_cast<TVector*
>(&vec)->change_storage_type(
PST_UNIQUE))
359 UG_THROW(
"AlgebraicConvCheck::norm(): Cannot change ParallelStorageType to unique.");
373 norm = vec.layouts()->proc_comm().allreduce(norm,
PCL_RO_SUM);
377 return sqrt((
number) norm);
location verbose
Definition: checkpoint_util.lua:128
Definition: smart_pointer.h:108
Definition: algebra_conv_check.h:70
virtual SmartPtr< IConvergenceCheck< TVector > > clone()
clones this instance
Definition: algebra_conv_check_impl.h:87
void update(const TVector &d)
computes the defect and sets it a the next defect value
Definition: algebra_conv_check_impl.h:193
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
bool iteration_ended()
Definition: algebra_conv_check_impl.h:227
void start_defect(number initialDefect)
defect control
Definition: algebra_conv_check_impl.h:99
AlgebraicConvCheck(size_t ncmp)
Definition: algebra_conv_check_impl.h:51
void print_line(std::string line)
prints a line using prefixes
Definition: algebra_conv_check_impl.h:331
void start(const TVector &d)
computes the start defect and set it
Definition: algebra_conv_check_impl.h:109
void print_offset()
Definition: algebra_conv_check_impl.h:321
bool post()
Definition: algebra_conv_check_impl.h:252
Definition: scalar_subvector_adapter.hh:53
@ PST_UNIQUE
Definition: parallel_storage_type.h:70
#define PCL_RO_SUM
Definition: pcl_methods.h:63
string repeat(char c, int nr)
Builds a string with specified repetitions of given character.
Definition: string_util.cpp:346
#define UG_THROW(msg)
Definition: error.h:57
#define UG_LOG(msg)
Definition: log.h:367
double number
Definition: types.h:124
double VecNormSquared(const double &a)
returns norm_2^2(a)
Definition: operations_vec.h:100
Definition: algebra_conv_check.h:156
number minDefect
Minimal required Defect of component.
Definition: algebra_conv_check.h:164
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
std::string name
Name of components.
Definition: algebra_conv_check.h:158