dune-istl  2.8.0
scalarproducts.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_ISTL_SCALARPRODUCTS_HH
4 #define DUNE_ISTL_SCALARPRODUCTS_HH
5 
6 #include <cmath>
7 #include <complex>
8 #include <iostream>
9 #include <iomanip>
10 #include <string>
11 #include <memory>
12 
13 #include <dune/common/exceptions.hh>
14 #include <dune/common/shared_ptr.hh>
15 
16 #include "bvector.hh"
17 #include "solvercategory.hh"
18 
19 
20 namespace Dune {
49  template<class X>
50  class ScalarProduct {
51  public:
53  typedef X domain_type;
54  typedef typename X::field_type field_type;
55  typedef typename FieldTraits<field_type>::real_type real_type;
56 
61  virtual field_type dot (const X& x, const X& y) const
62  {
63  return x.dot(y);
64  }
65 
69  virtual real_type norm (const X& x) const
70  {
71  return x.two_norm();
72  }
73 
76  {
78  }
79 
81  virtual ~ScalarProduct () {}
82  };
83 
95  template<class X, class C>
97  {
98  public:
103  typedef X domain_type;
105  typedef typename X::field_type field_type;
106  typedef typename FieldTraits<field_type>::real_type real_type;
112 
118  ParallelScalarProduct (std::shared_ptr<const communication_type> com, SolverCategory::Category cat)
119  : _communication(com), _category(cat)
120  {}
121 
129  : ParallelScalarProduct(stackobject_to_shared_ptr(com), cat)
130  {}
131 
132 
137  virtual field_type dot (const X& x, const X& y) const override
138  {
139  field_type result(0);
140  _communication->dot(x,y,result); // explicitly loop and apply masking
141  return result;
142  }
143 
147  virtual real_type norm (const X& x) const override
148  {
149  return _communication->norm(x);
150  }
151 
153  virtual SolverCategory::Category category() const override
154  {
155  return _category;
156  }
157 
158  private:
159  std::shared_ptr<const communication_type> _communication;
160  SolverCategory::Category _category;
161  };
162 
164  template<class X>
165  class SeqScalarProduct : public ScalarProduct<X>
166  {
168  };
169 
175  template<class X, class C>
177  {
178  public:
179  NonoverlappingSchwarzScalarProduct (std::shared_ptr<const C> comm) :
180  ParallelScalarProduct<X,C>(comm,SolverCategory::nonoverlapping) {}
181 
183  ParallelScalarProduct<X,C>(comm,SolverCategory::nonoverlapping) {}
184  };
185 
197  template<class X, class C>
199  {
200  public:
201  OverlappingSchwarzScalarProduct (std::shared_ptr<const C> comm) :
202  ParallelScalarProduct<X,C>(comm, SolverCategory::overlapping) {}
203 
205  ParallelScalarProduct<X,C>(comm,SolverCategory::overlapping) {}
206  };
207 
221  template<class X, class Comm>
222  std::shared_ptr<ScalarProduct<X>> makeScalarProduct(std::shared_ptr<const Comm> comm, SolverCategory::Category category)
223  {
224  switch(category)
225  {
227  return
228  std::make_shared<ScalarProduct<X>>();
229  default:
230  return
231  std::make_shared<ParallelScalarProduct<X,Comm>>(comm,category);
232  }
233  }
234 
239  template<class X, class Comm>
240  std::shared_ptr<ScalarProduct<X>> createScalarProduct(const Comm& comm, SolverCategory::Category category)
241  { return makeScalarProduct<X>(stackobject_to_shared_ptr(comm), category); }
242 
243 } // end namespace Dune
244 
245 #endif
This file implements a vector space as a tensor product of a given vector space. The number of compon...
Definition: allocator.hh:9
std::shared_ptr< ScalarProduct< X > > makeScalarProduct(std::shared_ptr< const Comm > comm, SolverCategory::Category category)
Choose the approriate scalar product for a solver category.
Definition: scalarproducts.hh:222
std::shared_ptr< ScalarProduct< X > > createScalarProduct(const Comm &comm, SolverCategory::Category category)
Definition: scalarproducts.hh:240
Base class for scalar product and norm computation.
Definition: scalarproducts.hh:50
virtual field_type dot(const X &x, const X &y) const
Dot product of two vectors. It is assumed that the vectors are consistent on the interior+border part...
Definition: scalarproducts.hh:61
virtual SolverCategory::Category category() const
Category of the scalar product (see SolverCategory::Category)
Definition: scalarproducts.hh:75
X::field_type field_type
Definition: scalarproducts.hh:54
X domain_type
export types, they come from the derived class
Definition: scalarproducts.hh:53
virtual ~ScalarProduct()
every abstract base class has a virtual destructor
Definition: scalarproducts.hh:81
FieldTraits< field_type >::real_type real_type
Definition: scalarproducts.hh:55
virtual real_type norm(const X &x) const
Norm of a right-hand side vector. The vector must be consistent on the interior+border partition.
Definition: scalarproducts.hh:69
Scalar product for overlapping Schwarz methods.
Definition: scalarproducts.hh:97
virtual field_type dot(const X &x, const X &y) const override
Dot product of two vectors. It is assumed that the vectors are consistent on the interior+border part...
Definition: scalarproducts.hh:137
virtual SolverCategory::Category category() const override
Category of the scalar product (see SolverCategory::Category)
Definition: scalarproducts.hh:153
FieldTraits< field_type >::real_type real_type
Definition: scalarproducts.hh:106
C communication_type
The type of the communication object.
Definition: scalarproducts.hh:111
ParallelScalarProduct(const communication_type &com, SolverCategory::Category cat)
Definition: scalarproducts.hh:128
X domain_type
The type of the vector to compute the scalar product on.
Definition: scalarproducts.hh:103
ParallelScalarProduct(std::shared_ptr< const communication_type > com, SolverCategory::Category cat)
Definition: scalarproducts.hh:118
X::field_type field_type
The field type used by the vector type domain_type.
Definition: scalarproducts.hh:105
virtual real_type norm(const X &x) const override
Norm of a right-hand side vector. The vector must be consistent on the interior+border partition.
Definition: scalarproducts.hh:147
Default implementation for the scalar case.
Definition: scalarproducts.hh:166
Nonoverlapping Scalar Product with communication object.
Definition: scalarproducts.hh:177
NonoverlappingSchwarzScalarProduct(std::shared_ptr< const C > comm)
Definition: scalarproducts.hh:179
NonoverlappingSchwarzScalarProduct(const C &comm)
Definition: scalarproducts.hh:182
Scalar product for overlapping Schwarz methods.
Definition: scalarproducts.hh:199
OverlappingSchwarzScalarProduct(std::shared_ptr< const C > comm)
Definition: scalarproducts.hh:201
OverlappingSchwarzScalarProduct(const C &comm)
Definition: scalarproducts.hh:204
Categories for the solvers.
Definition: solvercategory.hh:20
Category
Definition: solvercategory.hh:21
@ sequential
Category for sequential solvers.
Definition: solvercategory.hh:23