ug4
damping.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2015: G-CSC, Goethe University Frankfurt
3  * Authors: Andreas Vogel, Christian Wehner
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__DAMPING__
34 #define __H__LIB_ALGEBRA__OPERATOR__DAMPING__
35 
36 #include "common/common.h"
39 
40 namespace ug{
41 
58 template <typename X, typename Y = X>
59 class IDamping
60 {
61  public:
63 
71  virtual number damping(const Y& c, const X& d, ConstSmartPtr<ILinearOperator<Y,X> > spLinOp) const = 0;
72 
74 
80  virtual bool constant_damping() const = 0;
81 
83  virtual number damping() const = 0;
84 
86  virtual ~IDamping() {}
87 
89 
95  virtual std::string config_string() const = 0;
96 };
97 
99 template <typename X, typename Y = X>
100 class ConstantDamping : public IDamping<X,Y>
101 {
102  public:
103  ConstantDamping(number factor) : m_factor(factor) {}
104 
106  virtual number damping(const Y& c, const X& d, ConstSmartPtr<ILinearOperator<Y,X> > spLinOp) const
107  {
108  return m_factor;
109  }
110 
112  virtual number damping() const
113  {
114  return m_factor;
115  }
116 
118  virtual bool constant_damping() const {return true;};
119 
120  virtual std::string config_string() const
121  {
122  std::stringstream ss; ss << "ConstantDamping(" << m_factor << ")"; return ss.str();
123  }
124 
125  protected:
127 };
128 
130 template <typename X, typename Y = X>
131 class MinimalResiduumDamping : public IDamping<X,Y>
132 {
133  public:
135  virtual number damping(const Y& c, const X& d, ConstSmartPtr<ILinearOperator<Y,X> > spLinOp) const
136  {
137  SmartPtr<X> spAc = d.clone_without_values();
138  X& Ac = *spAc;
139 
140  try{
141  spLinOp.cast_const()->apply(Ac, c);
142  }UG_CATCH_THROW("MinimalResiduumDamping: Computing Ac failed.")
143 
144  // Compute scaling
145  try{
146  const number kappa = VecProd(Ac, d) / VecProd(Ac, Ac);
147 
148  if (kappa<0.3) return 0.3;
149 
150  // return result
151  return kappa;
152  }UG_CATCH_THROW("MinimalResiduumDamping: Computing (d,Ac)/(Ac,Ac) failed.")
153  }
154 
156  virtual bool constant_damping() const {return false;};
157 
159  virtual number damping() const
160  {
161  UG_THROW("MinimalResiduumDamping: non-constant damping.");
162  }
163 
164  virtual std::string config_string() const
165  {
166  return "MinimalResiduumDamping";
167  }
168 };
169 
171 template <typename X, typename Y = X>
172 class MinimalEnergyDamping : public IDamping<X,Y>
173 {
174  public:
176  virtual number damping(const Y& c, const X& d, ConstSmartPtr<ILinearOperator<Y,X> > spLinOp) const
177  {
178  SmartPtr<X> spAc = d.clone_without_values();
179  X& Ac = *spAc;
180  spLinOp.cast_const()->apply(Ac, c);
181 
182  // Compute scaling
183  const number kappa = VecProd(d,c) / VecProd(Ac, c);
184 
185  if (kappa<0.3) return 0.3;
186 
187  // return result
188  return kappa;
189  }
190 
192  virtual bool constant_damping() const {return false;};
193 
195  virtual number damping() const
196  {
197  UG_THROW("MinimalEnergyDamping: non-constant damping.");
198  }
199 
200  virtual std::string config_string() const
201  {
202  return "MinimalEnergyDamping";
203  }
204 };
205 
206 } // end namespace ug
207 
208 #endif /* __H__LIB_ALGEBRA__OPERATOR__DAMPING__ */
Definition: smart_pointer.h:296
Definition: smart_pointer.h:108
ConstSmartPtr< T, FreePolicy > cast_const() const
performs a const cast
Definition: smart_pointer.h:504
constant damping factor
Definition: damping.h:101
virtual number damping() const
returns the constant damping factor
Definition: damping.h:112
virtual number damping(const Y &c, const X &d, ConstSmartPtr< ILinearOperator< Y, X > > spLinOp) const
returns the constant damping factor
Definition: damping.h:106
virtual bool constant_damping() const
returns if damping is constant
Definition: damping.h:118
virtual std::string config_string() const
returns information about configuration parameters
Definition: damping.h:120
number m_factor
constant damping factor
Definition: damping.h:126
ConstantDamping(number factor)
Definition: damping.h:103
Definition: damping.h:60
virtual ~IDamping()
virtual destructor
Definition: damping.h:86
virtual number damping(const Y &c, const X &d, ConstSmartPtr< ILinearOperator< Y, X > > spLinOp) const =0
returns the damping
virtual bool constant_damping() const =0
returns if the damping is constant
virtual std::string config_string() const =0
returns information about configuration parameters
virtual number damping() const =0
returns the constant damping, throws exception if non-constant damping
describes a linear mapping X->Y
Definition: linear_operator.h:80
damping computed based on the minimal energy
Definition: damping.h:173
virtual number damping() const
returns the constant damping factor
Definition: damping.h:195
virtual std::string config_string() const
returns information about configuration parameters
Definition: damping.h:200
virtual number damping(const Y &c, const X &d, ConstSmartPtr< ILinearOperator< Y, X > > spLinOp) const
returns the damping factor
Definition: damping.h:176
virtual bool constant_damping() const
returns if damping is constant
Definition: damping.h:192
damping computed based on the minimal residuum
Definition: damping.h:132
virtual number damping(const Y &c, const X &d, ConstSmartPtr< ILinearOperator< Y, X > > spLinOp) const
returns the damping factor
Definition: damping.h:135
virtual std::string config_string() const
returns information about configuration parameters
Definition: damping.h:164
virtual bool constant_damping() const
returns if damping is constant
Definition: damping.h:156
virtual number damping() const
returns the constant damping factor
Definition: damping.h:159
number kappa
#define UG_CATCH_THROW(msg)
Definition: error.h:64
#define UG_THROW(msg)
Definition: error.h:57
double number
Definition: types.h:124
the ug namespace
double VecProd(const double &a, const double &b)
returns scal<a, b>
Definition: operations_vec.h:84