ug4
iterator_product.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2015: G-CSC, Goethe University Frankfurt
3  * Author: Arne Nägel
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__UG__LIB_DISC__OPERATOR__LINEAR_OPERATOR__PRODUCT__
34 #define __H__UG__LIB_DISC__OPERATOR__LINEAR_OPERATOR__PRODUCT__
35 
38 #include <vector>
39 
40 #ifdef UG_PARALLEL
42 #endif
43 
44 namespace ug{
45 
47 template <typename X, typename Y>
49 {
50  public:
52 
53  CombinedLinearIterator(const std::vector<SmartPtr<ILinearIterator<X,Y> > >& vIterator)
54  : m_vIterator(vIterator)
55  {};
56 
57  // Name of Iterator
58  virtual const char* name() const = 0;
59 
61  virtual bool supports_parallel() const
62  {
63  for(size_t i = 0; i < m_vIterator.size(); ++i)
65  return false;
66  return true;
67  }
68 
69  // Prepare for Operator J(u) and linearization point u (current solution)
70  virtual bool init(SmartPtr<ILinearOperator<Y,X> > J, const Y& u) = 0;
71 
72  // Prepare for Linear Operator L
73  virtual bool init(SmartPtr<ILinearOperator<Y,X> > L) = 0;
74 
75  // Compute correction
76  virtual bool apply(Y& c, const X& d) = 0;
77 
78  // Compute correction and update defect
79  virtual bool apply_update_defect(Y& c, X& d) = 0;
80 
82 
83  void add_iterator(SmartPtr<ILinearIterator<X,Y> > I,size_t nr) { for (size_t i=0;i<nr;i++) m_vIterator.push_back(I);}
84 
85  // Clone
87 
88  protected:
89  std::vector<SmartPtr<ILinearIterator<X,Y> > > m_vIterator;
90 };
91 
92 
94 template <typename X, typename Y>
96 {
97  protected:
100 
101  public:
103 
104  LinearIteratorProduct(const std::vector<SmartPtr<ILinearIterator<X,Y> > >& vIterator)
105  : CombinedLinearIterator<X,Y>(vIterator)
106  {};
107 
108  // Name of Iterator
109  virtual const char* name() const {return "IteratorProduct";}
110 
111  // Prepare for Operator J(u) and linearization point u (current solution)
112  virtual bool init(SmartPtr<ILinearOperator<Y,X> > J, const Y& u) {
113  bool bRes = true;
114  for(size_t i = 0; i < m_vIterator.size(); i++){
115  if ((i>0) && (m_vIterator[i]==m_vIterator[i-1])) continue;
116  bRes &= m_vIterator[i]->init(J,u);
117  }
118  return bRes;
119  }
120 
121  // Prepare for Linear Operator L
123  {
124  bool bRes = true;
125  for(size_t i = 0; i < m_vIterator.size(); i++) {
126  if ((i>0) && (m_vIterator[i]==m_vIterator[i-1])) continue;
127  bRes &= m_vIterator[i]->init(L);
128  }
129  return bRes;
130  }
131 
132  // Compute correction
133  virtual bool apply(Y& c, const X& d)
134  {
135  // create temporary defect and forward request
136  SmartPtr<X> spDTmp = d.clone();
137  return apply_update_defect(c, *spDTmp);
138  }
139 
140  // Compute correction and update defect
141  virtual bool apply_update_defect(Y& c, X& d)
142  {
143  SmartPtr<Y> spCTmp = c.clone_without_values();
144 
145  bool bRes = true;
146  c.set(0.0);
147  for(size_t i = 0; i < m_vIterator.size(); i++)
148  {
149  bRes &= m_vIterator[i]->apply_update_defect(*spCTmp,d);
150  c += (*spCTmp);
151  }
152  return bRes;
153  }
154 
155  // Clone
157  return make_sp(new LinearIteratorProduct(*this));
158  }
159 };
160 
162 template <typename X, typename Y>
164 {
165  protected:
168 
169  public:
171 
172  LinearIteratorSum(const std::vector<SmartPtr<ILinearIterator<X,Y> > >& vIterator)
173  : CombinedLinearIterator<X,Y>(vIterator)
174  {};
175 
176  // Name of Iterator
177  virtual const char* name() const {return "IteratorProduct";}
178 
179  // Prepare for Operator J(u) and linearization point u (current solution)
180  virtual bool init(SmartPtr<ILinearOperator<Y,X> > J, const Y& u) {
181  m_spOp = J;
182  bool bRes = true;
183  for(size_t i = 0; i < m_vIterator.size(); i++){
184  if ((i>0) && (m_vIterator[i]==m_vIterator[i-1])) continue;
185  bRes &= m_vIterator[i]->init(J,u);
186  }
187  return bRes;
188  }
189 
190  // Prepare for Linear Operator L
192  {
193  m_spOp = L;
194  bool bRes = true;
195  for(size_t i = 0; i < m_vIterator.size(); i++) {
196  if ((i>0) && (m_vIterator[i]==m_vIterator[i-1])) continue;
197  bRes &= m_vIterator[i]->init(L);
198  }
199  return bRes;
200  }
201 
202  // Compute correction
203  virtual bool apply(Y& c, const X& d)
204  {
205  // create temporary correction
206  SmartPtr<Y> spCTmp = c.clone_without_values();
207 
208  // this part computes all corrections independently
209  c.set(0.0);
210 
211  bool bRes = true;
212  for(size_t i = 0; i < m_vIterator.size(); i++)
213  {
214  bRes &= m_vIterator[i]->apply(*spCTmp,d);
215  c += (*spCTmp);
216  }
217 
218  return bRes;
219  }
220 
221  // Compute correction and update defect
222  virtual bool apply_update_defect(Y& c, X& d)
223  {
224  bool bRet = apply(c, d);
225  m_spOp->apply_sub(d, c);
226  return bRet;
227  }
228 
229  // Clone
231  return make_sp(new LinearIteratorSum(*this));
232  }
233 
234  protected:
236 };
237 
238 
239 } // end namespace ug
240 
241 #endif
Definition: smart_pointer.h:108
Definition: iterator_product.h:49
virtual SmartPtr< ILinearIterator< X, Y > > clone()=0
clone
virtual const char * name() const =0
returns the name of iterator
virtual bool init(SmartPtr< ILinearOperator< Y, X > > L)=0
initialize for linear operator L
virtual bool init(SmartPtr< ILinearOperator< Y, X > > J, const Y &u)=0
initialize for operator J(u) and linearization point u
CombinedLinearIterator()
Definition: iterator_product.h:51
CombinedLinearIterator(const std::vector< SmartPtr< ILinearIterator< X, Y > > > &vIterator)
Definition: iterator_product.h:53
virtual bool apply(Y &c, const X &d)=0
compute new correction c = B*d
void add_iterator(SmartPtr< ILinearIterator< X, Y > > I, size_t nr)
Definition: iterator_product.h:83
virtual bool apply_update_defect(Y &c, X &d)=0
compute new correction c = B*d and update defect d := d - A*c
void add_iterator(SmartPtr< ILinearIterator< X, Y > > I)
Definition: iterator_product.h:81
std::vector< SmartPtr< ILinearIterator< X, Y > > > m_vIterator
Definition: iterator_product.h:89
virtual bool supports_parallel() const
returns if parallel solving is supported
Definition: iterator_product.h:61
describes a linear iterator
Definition: linear_iterator.h:81
describes a linear mapping X->Y
Definition: linear_operator.h:80
Definition: iterator_product.h:96
virtual bool apply(Y &c, const X &d)
compute new correction c = B*d
Definition: iterator_product.h:133
virtual const char * name() const
returns the name of iterator
Definition: iterator_product.h:109
virtual bool apply_update_defect(Y &c, X &d)
compute new correction c = B*d and update defect d := d - A*c
Definition: iterator_product.h:141
virtual bool init(SmartPtr< ILinearOperator< Y, X > > L)
initialize for linear operator L
Definition: iterator_product.h:122
LinearIteratorProduct(const std::vector< SmartPtr< ILinearIterator< X, Y > > > &vIterator)
Definition: iterator_product.h:104
virtual bool init(SmartPtr< ILinearOperator< Y, X > > J, const Y &u)
initialize for operator J(u) and linearization point u
Definition: iterator_product.h:112
LinearIteratorProduct()
Definition: iterator_product.h:102
CombinedLinearIterator< X, Y > base_type
Definition: iterator_product.h:98
std::vector< SmartPtr< ILinearIterator< X, Y > > > m_vIterator
Definition: iterator_product.h:89
virtual SmartPtr< ILinearIterator< X, Y > > clone()
clone
Definition: iterator_product.h:156
Definition: iterator_product.h:164
LinearIteratorSum()
Definition: iterator_product.h:170
virtual bool init(SmartPtr< ILinearOperator< Y, X > > L)
initialize for linear operator L
Definition: iterator_product.h:191
LinearIteratorSum(const std::vector< SmartPtr< ILinearIterator< X, Y > > > &vIterator)
Definition: iterator_product.h:172
virtual const char * name() const
returns the name of iterator
Definition: iterator_product.h:177
CombinedLinearIterator< X, Y > base_type
Definition: iterator_product.h:166
virtual SmartPtr< ILinearIterator< X, Y > > clone()
clone
Definition: iterator_product.h:230
virtual bool apply(Y &c, const X &d)
compute new correction c = B*d
Definition: iterator_product.h:203
virtual bool init(SmartPtr< ILinearOperator< Y, X > > J, const Y &u)
initialize for operator J(u) and linearization point u
Definition: iterator_product.h:180
std::vector< SmartPtr< ILinearIterator< X, Y > > > m_vIterator
Definition: iterator_product.h:89
SmartPtr< ILinearOperator< Y, X > > m_spOp
Definition: iterator_product.h:235
virtual bool apply_update_defect(Y &c, X &d)
compute new correction c = B*d and update defect d := d - A*c
Definition: iterator_product.h:222
the ug namespace
SmartPtr< T, FreePolicy > make_sp(T *inst)
returns a SmartPtr for the passed raw pointer
Definition: smart_pointer.h:836