overlappingscalarproduct.hh
Go to the documentation of this file.
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 /*
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 2 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 
19  Consult the COPYING file in the top-level source directory of this
20  module for the precise wording of the license and the list of
21  copyright holders.
22 */
27 #ifndef EWOMS_OVERLAPPING_SCALAR_PRODUCT_HH
28 #define EWOMS_OVERLAPPING_SCALAR_PRODUCT_HH
29 
30 #include <dune/common/version.hh>
31 #include <dune/common/parallel/mpihelper.hh>
32 #include <dune/istl/scalarproducts.hh>
33 
34 namespace Ewoms {
35 namespace Linear {
36 
40 template <class OverlappingBlockVector, class Overlap>
42  : public Dune::ScalarProduct<OverlappingBlockVector>
43 {
44 public:
45  typedef typename OverlappingBlockVector::field_type field_type;
46  typedef typename Dune::CollectiveCommunication<typename Dune::MPIHelper::MPICommunicator> CollectiveCommunication;
47 
48 #if DUNE_VERSION_NEWER(DUNE_ISTL, 2, 5)
49  typedef typename Dune::ScalarProduct<OverlappingBlockVector>::real_type real_type;
50 #else
51  typedef double real_type;
52 #endif
53 
54  enum { category = Dune::SolverCategory::overlapping };
55 
56  OverlappingScalarProduct(const Overlap& overlap)
57  : overlap_(overlap), comm_( Dune::MPIHelper::getCollectiveCommunication() )
58  {}
59 
60  field_type dot(const OverlappingBlockVector& x,
61  const OverlappingBlockVector& y)
62  {
63  field_type sum = 0;
64  size_t numLocal = overlap_.numLocal();
65  for (unsigned localIdx = 0; localIdx < numLocal; ++localIdx) {
66  if (overlap_.iAmMasterOf(static_cast<int>(localIdx)))
67  sum += x[localIdx] * y[localIdx];
68  }
69 
70  // return the global sum
71  return comm_.sum( sum );
72  }
73 
74  real_type norm(const OverlappingBlockVector& x)
75  { return std::sqrt(dot(x, x)); }
76 
77 private:
78  const Overlap& overlap_;
79  const CollectiveCommunication comm_;
80 };
81 
82 } // namespace Linear
83 } // namespace Ewoms
84 
85 #endif
An overlap aware ISTL scalar product.
Definition: overlappingscalarproduct.hh:41
Definition: baseauxiliarymodule.hh:37
An overlap aware block vector.
Definition: overlappingblockvector.hh:49