33 #ifndef __H__LIB_DISC__OPERATOR__COMPOSITE_CONVERGENCE_CHECK_IMPL__
34 #define __H__LIB_DISC__OPERATOR__COMPOSITE_CONVERGENCE_CHECK_IMPL__
38 #include <boost/core/enable_if.hpp>
46 template <
class TVector,
class TDomain>
49 : m_spApprox(spApproxSpace),
50 m_bCheckRest(true), m_restMinDefect(1-12), m_restRelReduction(1-10),
51 m_currentStep(0), m_maxSteps(100),
52 m_verbose(true), m_supress_unsuccessful(false),
53 m_offset(0), m_symbol(
'%'), m_name(
"Iteration"), m_info(
""),
54 m_bTimeMeas(true), m_bAdaptive(false)
59 template <
class TVector,
class TDomain>
63 : m_spApprox(spApproxSpace),
64 m_bCheckRest(true), m_restMinDefect(minDefect), m_restRelReduction(relReduction),
65 m_currentStep(0), m_maxSteps(maxSteps),
66 m_verbose(true), m_supress_unsuccessful(false),
67 m_offset(0), m_symbol(
'%'), m_name(
"Iteration"), m_info(
""),
68 m_bTimeMeas(true), m_bAdaptive(false)
73 template <
class TVector,
class TDomain>
77 extract_dof_indices(dd);
83 template <
class TVector,
class TDomain>
84 template <
typename TBaseElem>
103 std::vector<DoFIndex> vInd;
104 for (; iter != iterEnd; ++iter)
117 for (
size_t fi = 0; fi < dd->num_fct(); fi++)
120 dd->inner_dof_indices(elem, fi, vInd);
123 for (
size_t dof = 0; dof < vInd.size(); dof++)
124 m_vNativCmpInfo[fi].vMultiIndex.push_back(vInd[dof]);
130 template <
class TVector,
class TDomain>
135 m_vNativCmpInfo.clear();
136 m_vNativCmpInfo.resize(dd->num_fct());
137 if(dd->max_dofs(
VERTEX)) extract_dof_indices<Vertex>(dd);
138 if(dd->max_dofs(
EDGE)) extract_dof_indices<Edge>(dd);
139 if(dd->max_dofs(
FACE)) extract_dof_indices<Face>(dd);
140 if(dd->max_dofs(
VOLUME)) extract_dof_indices<Volume>(dd);
142 for(
size_t i = 0; i < m_vNativCmpInfo.size(); ++i)
143 m_vNativCmpInfo[i].
name = dd->name(i);
146 for(
size_t i = 0; i < m_vNativCmpInfo.size(); ++i)
147 m_numAllDoFs += m_vNativCmpInfo[i].vMultiIndex.size();
151 template <
class TVector,
class TDomain>
153 norm(
const TVector& vec,
const std::vector<DoFIndex>& vMultiIndex)
158 if (!
const_cast<TVector*
>(&vec)->change_storage_type(
PST_UNIQUE))
159 UG_THROW(
"CompositeConvCheck::norm(): Cannot change ParallelStorageType to unique.");
163 size_t sz = vMultiIndex.size();
164 for (
size_t dof = 0; dof < sz; ++dof)
167 norm += (double) (val*val);
194 return sqrt((
number) norm);
198 template <
class TVector,
class TDomain>
209 template <
class TVector,
class TDomain>
212 const std::vector<number>& vMinDefect,
213 const std::vector<number>& vRelReduction)
215 if(vFctName.size() != vMinDefect.size() || vFctName.size() != vRelReduction.size())
216 UG_THROW(
"CompositeConvCheck: Please specify one value for each cmp.");
218 for(
size_t cmp = 0; cmp < vFctName.size(); ++cmp)
219 set_component_check(vFctName[cmp], vMinDefect[cmp], vRelReduction[cmp]);
223 template <
class TVector,
class TDomain>
226 const std::vector<number>& vMinDefect,
227 const std::vector<number>& vRelReduction)
233 template <
class TVector,
class TDomain>
237 const number relReduction)
239 for(
size_t cmp = 0; cmp < vFctName.size(); ++cmp)
240 set_component_check(vFctName[cmp], minDefect, relReduction);
243 template <
class TVector,
class TDomain>
246 const number relReduction)
248 for(
size_t fct = 0; fct < m_spApprox->num_fct(); ++fct){
249 std::string
name = m_spApprox->name(fct);
250 set_component_check(
name, minDefect, relReduction);
254 template <
class TVector,
class TDomain>
258 const number relReduction)
262 for(
size_t i = 0; i < vName.size(); ++i)
264 const int fct = m_spApprox->fct_id_by_name(vName[i].c_str());
268 for(; cmp < m_CmpInfo.size(); ++cmp){
271 if(m_CmpInfo[cmp].isRest)
continue;
274 if(m_CmpInfo[cmp].vFct.size() != 1)
continue;
277 if(m_CmpInfo[cmp].vFct[0] == fct)
282 if(cmp == m_CmpInfo.size())
283 m_CmpInfo.push_back(
CmpInfo());
286 m_CmpInfo[cmp].isRest =
false;
287 m_CmpInfo[cmp].vFct.clear();
288 m_CmpInfo[cmp].vFct.push_back(fct);
289 m_CmpInfo[cmp].name = vName[i];
290 m_CmpInfo[cmp].minDefect = minDefect;
291 m_CmpInfo[cmp].relReduction = relReduction;
298 template <
class TVector,
class TDomain>
302 const number relReduction)
304 std::vector<int> vFct(vFctName.size());
305 for(
size_t i = 0; i < vFctName.size(); ++i){
307 vFct[i] = m_spApprox->fct_id_by_name(
name.c_str());
312 for(; cmp < m_CmpInfo.size(); ++cmp){
315 if(m_CmpInfo[cmp].isRest)
continue;
318 if(m_CmpInfo[cmp].vFct.size() != vFct.size())
continue;
322 for(
size_t i = 0; i < vFct.size(); ++i){
323 if(m_CmpInfo[cmp].vFct[i] != vFct[i])
331 if(cmp == m_CmpInfo.size())
332 m_CmpInfo.push_back(
CmpInfo());
334 std::string
name(
"");
335 for(
size_t fct = 0; fct < vFct.size(); ++fct){
336 if(!
name.empty())
name.append(
", ");
337 name.append(m_vNativCmpInfo[vFct[fct]].
name);
341 m_CmpInfo[cmp].isRest =
false;
342 m_CmpInfo[cmp].vFct = vFct;
343 m_CmpInfo[cmp].name =
name;
344 m_CmpInfo[cmp].minDefect = minDefect;
345 m_CmpInfo[cmp].relReduction = relReduction;
351 template <
class TVector,
class TDomain>
355 const number relReduction)
360 template <
class TVector,
class TDomain>
365 for(
size_t cmp = 0; cmp < m_CmpInfo.size(); ++cmp)
366 if(m_CmpInfo[cmp].isRest)
367 m_CmpInfo.erase(m_CmpInfo.begin()+cmp);
370 if(!m_bCheckRest)
return;
373 std::vector<bool> vUsed(m_vNativCmpInfo.size(),
false);
374 for(
size_t cmp = 0; cmp < m_CmpInfo.size(); ++cmp)
376 for(
size_t i = 0; i < m_CmpInfo[cmp].vFct.size(); ++i)
377 vUsed[ m_CmpInfo[cmp].vFct[i] ] =
true;
380 std::vector<int> vFct;
381 std::string
name(
"");
383 for(
size_t fct = 0; fct < vUsed.size(); ++fct){
384 if(vUsed[fct])
continue;
387 if(!
name.empty())
name.append(
", ");
388 name.append(m_vNativCmpInfo[fct].
name);
392 if(vFct.empty())
return;
395 m_CmpInfo.push_back(
CmpInfo());
396 m_CmpInfo.back().isRest =
true;
397 m_CmpInfo.back().vFct = vFct;
398 m_CmpInfo.back().name =
name;
399 m_CmpInfo.back().minDefect = m_restMinDefect;
400 m_CmpInfo.back().relReduction = m_restRelReduction;
403 template <
class TVector,
class TDomain>
406 UG_THROW(
"This method cannot be used to set defect values,\n"
407 "since obviously this class is meant for an individual\n"
408 "defect calculation of more than one function\n"
409 "(use start(TVector& d) instead).");
413 template <
typename TVector,
typename Enable =
void>
420 template <
typename TVector>
423 static const size_t block_size = TVector::value_type::static_size;
427 template <
class TVector,
class TDomain>
437 UG_THROW(
"Number of dofs in CompositeConvCheck does not match "
438 "number of dofs given in vector from algorithm (" << m_numAllDoFs
439 <<
", but " << vec.size() <<
" given). \nMake sure that you set "
440 "the right grid level via set_level().");
444 if (m_bTimeMeas) m_stopwatch.start();
447 for (
size_t fct = 0; fct < m_vNativCmpInfo.size(); fct++){
448 m_vNativCmpInfo[fct].initDefect = norm(vec, m_vNativCmpInfo[fct].vMultiIndex);
449 m_vNativCmpInfo[fct].currDefect = m_vNativCmpInfo[fct].initDefect;
453 for (
size_t cmp = 0; cmp < m_CmpInfo.size(); cmp++){
454 CmpInfo& cmpInfo = m_CmpInfo[cmp];
457 for(
size_t i = 0; i < cmpInfo.
vFct.size(); ++i)
458 cmpInfo.
currDefect += pow(m_vNativCmpInfo[cmpInfo.
vFct[i]].currDefect, 2);
472 int num_line_length = 80;
474 int max_length = std::max(m_name.length(), m_info.length());
475 int space_left = std::max(num_line_length - max_length - num_sym, 0);
480 int pre_space = (int)(max_length -(
int)m_name.length()) / 2;
482 UG_LOG(
" "<< m_name <<
" ");
483 UG_LOG(
repeat(
' ', max_length - pre_space -m_name.length()));
488 if (m_info.length() > 0)
491 UG_LOG(
" "<< m_info <<
" ");
503 UG_LOG(
" Iter Defect Required Rate "
504 "Reduction Required Component(s)\n");
506 for (
size_t cmp = 0; cmp < m_CmpInfo.size(); cmp++)
508 CmpInfo& cmpInfo = m_CmpInfo[cmp];
511 if(cmp != 0) {
UG_LOG(
" " );}
512 else {
UG_LOG(std::right << std::setw(5) << step() <<
" ");}
515 UG_LOG(std::scientific << std::setprecision(3) << cmpInfo.
minDefect <<
" " << std::setprecision(6) );
516 UG_LOG(std::scientific <<
" ----- " <<
" ");
517 UG_LOG(std::scientific <<
" -------- " <<
" ");
518 UG_LOG(std::scientific << std::setprecision(3) << cmpInfo.
relReduction <<
" " << std::setprecision(6));
519 UG_LOG(std::scientific << cmpInfo.
name <<
"\n");
525 template <
class TVector,
class TDomain>
528 UG_THROW(
"This method cannot be used to update defect values,\n"
529 "since obviously this class is meant for an individual\n"
530 "defect calculation of more than one function\n"
531 "(use update(TVector& d) instead).");
535 template <
class TVector,
class TDomain>
541 UG_THROW(
"Number of dofs in CompositeConvCheck does not match"
542 "number of dofs given in vector from algorithm (" << m_numAllDoFs <<
543 ", but " << vec.size() <<
" given). \nMake sure that you set the "
544 "right grid level via set_level().");
548 for (
size_t fct = 0; fct < m_vNativCmpInfo.size(); fct++){
549 m_vNativCmpInfo[fct].lastDefect = m_vNativCmpInfo[fct].currDefect;
550 m_vNativCmpInfo[fct].currDefect = norm(vec, m_vNativCmpInfo[fct].vMultiIndex);
554 for (
size_t cmp = 0; cmp < m_CmpInfo.size(); cmp++){
555 CmpInfo& cmpInfo = m_CmpInfo[cmp];
560 for(
size_t i = 0; i < cmpInfo.
vFct.size(); ++i)
561 cmpInfo.
currDefect += pow(m_vNativCmpInfo[cmpInfo.
vFct[i]].currDefect, 2);
569 for (
size_t cmp = 0; cmp < m_CmpInfo.size(); cmp++)
571 CmpInfo& cmpInfo = m_CmpInfo[cmp];
574 if(cmp != 0) {
UG_LOG(
" " );}
575 else {
UG_LOG(std::right << std::setw(5) << step() <<
" ");}
578 UG_LOG(std::scientific << std::setprecision(3) << cmpInfo.
minDefect <<
" " << std::setprecision(6));
582 UG_LOG(std::scientific <<
" ----- " <<
" ");
587 UG_LOG(std::scientific <<
" -------- " <<
" ");
589 UG_LOG(std::scientific << std::setprecision(3) << cmpInfo.
relReduction <<
" " << std::setprecision(6));
590 UG_LOG(std::scientific << cmpInfo.
name <<
"\n");
596 template <
class TVector,
class TDomain>
599 if (step() >= m_maxSteps)
return true;
602 for (
size_t cmp = 0; cmp < m_CmpInfo.size(); cmp++)
604 CmpInfo& cmpInfo = m_CmpInfo[cmp];
606 if (!is_valid_number(cmpInfo.
currDefect))
return true;
608 bool cmpFinished =
false;
614 ended = ended && cmpFinished;
621 template <
class TVector,
class TDomain>
624 if (m_bTimeMeas) m_stopwatch.stop();
628 if (step() > m_maxSteps){
630 UG_LOG(
"Maximum numbers of "<< m_maxSteps <<
631 " iterations reached without convergence.\n");
635 for (
size_t cmp = 0; cmp < m_CmpInfo.size(); cmp++)
637 CmpInfo& cmpInfo = m_CmpInfo[cmp];
645 UG_LOG(
"Current defect for '" << cmpInfo.
name <<
646 "' is not a valid number.\n");
650 bool cmpFinished =
false;
657 << cmpInfo.
name <<
"' reached after " << step() <<
" steps.\n");
670 << cmpInfo.
name <<
"' reached after " << step() <<
" steps.\n");
676 if (!cmpFinished) success =
false;
683 std::stringstream tmsg;
686 number time = m_stopwatch.ms()/1000.0;
687 tmsg <<
" (t: " << std::setprecision(3) << time <<
"s; t/it: "
688 << time / step() <<
"s)";
690 if (success) {
UG_LOG(
" Iteration converged" << tmsg.str() <<
" ");}
691 else {
UG_LOG(
" Iteration not successful" << tmsg.str() <<
" ");}
696 return success || m_supress_unsuccessful;
700 template <
class TVector,
class TDomain>
710 template <
class TVector,
class TDomain>
718 template <
class TVector,
class TDomain>
721 if (value == 0.0)
return true;
722 else return (value >= std::numeric_limits<number>::min()
723 && value <= std::numeric_limits<number>::max()
724 && value == value && value >= 0.0);
location name
Definition: checkpoint_util.lua:128
Definition: smart_pointer.h:296
Definition: smart_pointer.h:108
Definition: pcl_process_communicator.h:70
void allreduce(const void *sendBuf, void *recBuf, int count, DataType type, ReduceOperation op) const
performs MPI_Allreduce on the processes of the communicator.
Definition: pcl_process_communicator.cpp:318
base class for approximation spaces without type of algebra or dof distribution
Definition: approximation_space.h:279
Definition: composite_conv_check.h:67
void set_group_check(const std::vector< std::string > &vFctName, const number minDefect, const number relReduction)
Definition: composite_conv_check_impl.h:300
void set_level(int level)
set level of grid, where defect vectors come from
Definition: composite_conv_check_impl.h:74
CompositeConvCheck(SmartPtr< ApproximationSpace< TDomain > > approx)
Definition: composite_conv_check_impl.h:48
void start(const TVector &d)
computes the start defect and set it
Definition: composite_conv_check_impl.h:428
void print_offset()
Definition: composite_conv_check_impl.h:701
bool is_valid_number(number value)
Definition: composite_conv_check_impl.h:719
void set_component_check(const std::string &vFctName, const std::vector< number > &vMinDefect, const std::vector< number > &vRelReduction)
Definition: composite_conv_check_impl.h:225
void print_line(std::string line)
prints a line using prefixes
Definition: composite_conv_check_impl.h:711
void extract_dof_indices(ConstSmartPtr< DoFDistribution > dd)
extracts multi-indices for a fct-comp on a element type
Definition: composite_conv_check_impl.h:86
void update_rest_check()
Definition: composite_conv_check_impl.h:362
number norm(const TVector &vec, const std::vector< DoFIndex > &index)
calculates the 2-norm of the entries of the vector vec specified by index
Definition: composite_conv_check_impl.h:153
void update(const TVector &d)
computes the defect and sets it a the next defect value
Definition: composite_conv_check_impl.h:536
void update_defect(number newDefect)
sets the update for the current defect
Definition: composite_conv_check_impl.h:526
bool iteration_ended()
Definition: composite_conv_check_impl.h:597
virtual SmartPtr< IConvergenceCheck< TVector > > clone()
clones this instance
Definition: composite_conv_check_impl.h:199
void start_defect(number initialDefect)
defect control
Definition: composite_conv_check_impl.h:404
void set_all_component_check(const number minDefect, const number relReduction)
sets check for all components in approximation space
Definition: composite_conv_check_impl.h:245
bool post()
Definition: composite_conv_check_impl.h:622
Definition: grid_level.h:42
@ TOP
Definition: grid_level.h:45
@ SURFACE
Definition: grid_level.h:48
Definition: multi_grid.h:72
TChild * get_child(TElem *elem, size_t ind) const
returns the i-th child of the given child-type
Definition: multi_grid.h:268
size_t num_children(TElem *elem) const
returns the number of children of the given child-type
Definition: multi_grid.h:225
Const iterator to traverse the surface of a multi-grid hierarchy.
Definition: surface_view.h:237
Represents the surface view of a multi-grid hierarchy.
Definition: surface_view.h:61
bool is_contained(TGeomObj *obj, const GridLevel &gl, SurfaceState validStates=ALL) const
returns if the element is contained in the surface view
Definition: surface_view_impl.hpp:442
@ SURFACE_RIM
Definition: surface_view.h:94
@ ALL
Definition: surface_view.h:102
@ SHADOW_RIM_COPY
Definition: surface_view.h:95
@ PST_UNIQUE
Definition: parallel_storage_type.h:70
#define PCL_RO_SUM
Definition: pcl_methods.h:63
base_type::TBaseElem TBaseElem
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
Definition: boost_serialization_routines.h:49
number & DoFRef(TMatrix &mat, const DoFIndex &iInd, const DoFIndex &jInd)
Definition: multi_index.h:276
void TokenizeTrimString(const string &str, vector< string > &vToken, const char delimiter)
Definition: string_util.cpp:83
string TrimString(const string &str)
Definition: string_util.cpp:104
@ VOLUME
Definition: grid_base_objects.h:63
@ VERTEX
Definition: grid_base_objects.h:60
@ EDGE
Definition: grid_base_objects.h:61
@ FACE
Definition: grid_base_objects.h:62
Definition: composite_conv_check.h:238
number relReduction
Relative reduction required for component.
Definition: composite_conv_check.h:249
std::vector< int > vFct
Index of components.
Definition: composite_conv_check.h:241
number currDefect
Current Defect of component.
Definition: composite_conv_check.h:245
std::string name
Name of components.
Definition: composite_conv_check.h:242
number initDefect
Initial Defect of component.
Definition: composite_conv_check.h:244
number minDefect
Minimal required Defect of component.
Definition: composite_conv_check.h:248
number lastDefect
Last Defect if component.
Definition: composite_conv_check.h:246
Definition: composite_conv_check_impl.h:415
static const size_t block_size
Definition: composite_conv_check_impl.h:416