ug4
preconditioned_linear_operator_inverse.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-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__INTERFACE__PRECONDITIONED_LINEAR_OPERATOR_INVERSE__
34 #define __H__LIB_ALGEBRA__OPERATOR__INTERFACE__PRECONDITIONED_LINEAR_OPERATOR_INVERSE__
35 
37 #include "linear_iterator.h"
41 #include "common/log.h"
43 
44 #undef DEBUG_FOR_AMG
45 
46 namespace ug{
48 // Inverse of a Linear Operator using a ILinearIterator as preconditioner
50 
52 
59 template <typename X>
61  : public ILinearOperatorInverse<X>,
62  public VectorDebugWritingObject<X>
63 {
64  public:
67 
70 
73 
74  protected:
77 
78  public:
80  using base_type::name;
82 
83  public:
86  : m_bRecompute(false), m_spPrecond(NULL)
87 #ifdef DEBUG_FOR_AMG
88 , m_amgDebug(0)
89 #endif
90  {}
91 
94  : m_bRecompute(false), m_spPrecond(spPrecond)
95 #ifdef DEBUG_FOR_AMG
96 , m_amgDebug(0)
97 #endif
98  {}
99 
102  SmartPtr<IConvergenceCheck<X> > spConvCheck)
103  : base_type(spConvCheck),
104  m_bRecompute(false), m_spPrecond(spPrecond)
105 #ifdef DEBUG_FOR_AMG
106 , m_amgDebug(0)
107 #endif
108  {}
109 
112  {
113  m_spPrecond = spPrecond;
114  }
115 
121 
123  virtual bool init(SmartPtr<ILinearOperator<X,X> > J, const X& u)
124  {
125  if(!base_type::init(J, u)) return false;
126 
127  LS_PROFILE_BEGIN(LS_InitPrecond);
128  if(m_spPrecond.valid())
129  if(!m_spPrecond->init(J, u))
130  UG_THROW(name() << "::init: Cannot init Preconditioner "
131  "Operator for Operator J.");
132  LS_PROFILE_END(LS_InitPrecond);
133 
134  return true;
135  }
136 
139  {
140  if(!base_type::init(L)) return false;
141 
142  LS_PROFILE_BEGIN(LS_InitPrecond);
143  if(m_spPrecond.valid())
144  if(!m_spPrecond->init(L))
145  UG_THROW(name() <<"::prepare: Cannot init Preconditioner "
146  "Operator for Operator L.");
147  LS_PROFILE_END(LS_InitPrecond);
148 
149  return true;
150  }
151 
152  virtual bool apply(X& x, const X& b)
153  {
154  // copy defect
155  SmartPtr<X> spB = b.clone(); X& bTmp = *spB;
156 // X bTmp; bTmp.resize(b.size()); bTmp = b;
157 
158  // solve on copy of defect
159  bool bRes = apply_return_defect(x, bTmp);
160 
161  // write updated defect
162  write_debug(bTmp, "LS_UpdatedDefectEnd.vec");
163 
164  // compute defect again, for debug purpose
165  if(m_bRecompute)
166  {
167  // recompute defect
168  bTmp = b; linear_operator()->apply_sub(bTmp, x);
169  number norm = bTmp.norm();
170 
171  // print norm of recomputed defect
172  UG_LOG("%%%% DEBUG "<<name()<<": (Re)computed defect has norm: "
173  <<norm<<"\n");
174 
175  // write true end defect
176  write_debug(bTmp, "LS_TrueDefectEnd.vec");
177  }
178 
179 #ifdef DEBUG_FOR_AMG
180  if (m_amgDebug>0)
181  {
182  // convergence post-check
183  X myError(x.size());
184  myError.set_random(-1.0, 1.0);
185 
186  bTmp.set(0.0);
187 
188  this->write_debug(myError, "AMGDebugPre");
189  apply_return_defect(myError, bTmp);
190  this->write_debug(myError, "AMGDebugPost");
191  }
192 #endif
193 
194 
195  // return
196  return bRes;
197  }
198 
201  {
202  std::stringstream ss;
203  ss << " Convergence Check: ";
204  if(m_spConvCheck.valid()) ss << ConfigShift(m_spConvCheck->config_string()) << "\n";
205  else ss << " NOT SET!\n";
206  ss << " Preconditioner: ";
207  if(m_spPrecond.valid()) ss << ConfigShift(m_spPrecond->config_string()) << "\n";
208  else ss << " NOT SET!\n";
209  return ss.str();
210  }
211 
213  virtual std::string config_string() const
214  {
215  std::stringstream ss;
217  return ss.str();
218  }
219 
222  {
223  m_bRecompute = bRecompute;
224  }
225 
226  protected:
231 
232 #ifdef DEBUG_FOR_AMG
233  public:
234  void set_debug_amg(int b) {m_amgDebug = b;}
235  int m_amgDebug;
236 #endif
237 };
238 
239 }
240 #endif /* __H__LIB_ALGEBRA__OPERATOR__INTERFACE__PRECONDITIONED_LINEAR_OPERATOR_INVERSE__ */
Definition: smart_pointer.h:296
Definition: smart_pointer.h:108
Definition: convergence_check.h:72
describes an inverse linear mapping X->Y
Definition: linear_operator_inverse.h:80
SmartPtr< ILinearOperator< X, X > > linear_operator()
returns the current Operator this Inverse Operator is initialized for
Definition: linear_operator_inverse.h:227
virtual bool apply_return_defect(X &u, X &f)=0
applies inverse operator, i.e. returns u = A^{-1} f and returns defect d := f - A*u
virtual bool init(SmartPtr< ILinearOperator< X, X > > L)
initializes for the inverse for a linear operator
Definition: linear_operator_inverse.h:135
SmartPtr< IConvergenceCheck< X > > m_spConvCheck
smart pointer holding the convergence check
Definition: linear_operator_inverse.h:241
virtual const char * name() const=0
returns the name of the operator inverse
describes an inverse linear mapping X->X
Definition: preconditioned_linear_operator_inverse.h:63
SmartPtr< ILinearIterator< X, X > > m_spPrecond
Iterator used in the iterative scheme to compute the correction and update the defect.
Definition: preconditioned_linear_operator_inverse.h:230
virtual bool init(SmartPtr< ILinearOperator< X, X > > L)
initializes the solver for an operator
Definition: preconditioned_linear_operator_inverse.h:138
ConstSmartPtr< ILinearIterator< X, X > > preconditioner() const
Definition: preconditioned_linear_operator_inverse.h:119
void set_compute_fresh_defect_when_finished(bool bRecompute)
for debug: computes norm again after whole calculation of apply
Definition: preconditioned_linear_operator_inverse.h:221
IPreconditionedLinearOperatorInverse(SmartPtr< ILinearIterator< X, X > > spPrecond, SmartPtr< IConvergenceCheck< X > > spConvCheck)
constructor setting the preconditioner
Definition: preconditioned_linear_operator_inverse.h:101
IPreconditionedLinearOperatorInverse()
Empty constructor.
Definition: preconditioned_linear_operator_inverse.h:85
virtual bool init(SmartPtr< ILinearOperator< X, X > > J, const X &u)
initializes the solver for an operator
Definition: preconditioned_linear_operator_inverse.h:123
X domain_function_type
Domain space.
Definition: preconditioned_linear_operator_inverse.h:66
ILinearOperatorInverse< X, X > base_type
Base class.
Definition: preconditioned_linear_operator_inverse.h:72
bool m_bRecompute
flag if fresh defect should be computed when finish for debug purpose
Definition: preconditioned_linear_operator_inverse.h:228
virtual bool apply(X &x, const X &b)
compute new correction c = B*d
Definition: preconditioned_linear_operator_inverse.h:152
void set_preconditioner(SmartPtr< ILinearIterator< X, X > > spPrecond)
sets the preconditioner
Definition: preconditioned_linear_operator_inverse.h:111
SmartPtr< ILinearIterator< X, X > > preconditioner()
Definition: preconditioned_linear_operator_inverse.h:118
IPreconditionedLinearOperatorInverse(SmartPtr< ILinearIterator< X, X > > spPrecond)
constructor setting the preconditioner
Definition: preconditioned_linear_operator_inverse.h:93
virtual std::string config_string() const
returns information about configuration parameters
Definition: preconditioned_linear_operator_inverse.h:213
X codomain_function_type
Range space.
Definition: preconditioned_linear_operator_inverse.h:69
std::string config_string_preconditioner_convergence_check() const
returns config information of convergence check and preconditioner
Definition: preconditioned_linear_operator_inverse.h:200
Definition: debug_writer.h:266
void write_debug(const vector_type &vec, const char *filename)
writing debug output for a vector (if debug writer set)
Definition: debug_writer.h:293
#define UG_THROW(msg)
Definition: error.h:57
#define UG_LOG(msg)
Definition: log.h:367
double number
Definition: types.h:124
#define LS_PROFILE_BEGIN(name)
Definition: linear_solver_profiling.h:40
#define LS_PROFILE_END(name)
Definition: linear_solver_profiling.h:41
the ug namespace
string ConfigShift(string s)
Definition: string_util.cpp:457