version 3.10-dev
scalarproducts.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// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_LINEAR_SCALAR_PRODUCTS_HH
13#define DUMUX_LINEAR_SCALAR_PRODUCTS_HH
14
15#include <array>
16#include <memory>
17#include <algorithm>
18
19#include <dune/common/ftraits.hh>
20#include <dune/common/hybridutilities.hh>
21
22#include <dune/istl/solvercategory.hh>
23#include <dune/istl/scalarproducts.hh>
24
25namespace Dumux {
26
41template<class X, class C>
42class ParallelMultiTypeScalarProduct : public Dune::ScalarProduct<X>
43{
44 static constexpr std::size_t numSubDomains = X::size();
45public:
46 // public aliases for a dune-istl like interface
47 using domain_type = X;
48 using field_type = typename X::field_type;
49 using real_type = typename Dune::FieldTraits<field_type>::real_type;
51
52 ParallelMultiTypeScalarProduct (const std::array<std::shared_ptr<const communication_type>, numSubDomains>& comms)
53 : comms_(comms)
54 {}
55
63 field_type dot (const X& x, const X& y) const override
64 {
65 field_type result = 0.0;
66
67 // use the communicators for the subdomain scalar products and sum up
68 using namespace Dune::Hybrid;
69 forEach(integralRange(Dune::Hybrid::size(y)), [&](auto&& i)
70 {
71 field_type subResult = 0.0;
72 comms_[i]->dot(x[i], y[i], subResult);
73 result += subResult;
74 });
75
76 return result;
77 }
78
82 real_type norm (const X& x) const override
83 {
84 using std::sqrt;
85 return sqrt(dot(x, x));
86 }
87
99 Dune::SolverCategory::Category category() const override
100 {
101 if (std::all_of(comms_.begin(), comms_.end(),
102 [](auto& c){ return c->category() == Dune::SolverCategory::sequential; }
103 ))
104 return Dune::SolverCategory::sequential;
105 else if (std::all_of(comms_.begin(), comms_.end(),
106 [](auto& c){ return c->category() == Dune::SolverCategory::nonoverlapping; }
107 ))
108 return Dune::SolverCategory::nonoverlapping;
109 else
110 return Dune::SolverCategory::overlapping;
111 }
112
113private:
114 std::array<std::shared_ptr<const communication_type>, numSubDomains> comms_;
115};
116
117} // end namespace Dumux
118
119#endif
A scalar product for multi-type vectors.
Definition: scalarproducts.hh:43
typename X::field_type field_type
Definition: scalarproducts.hh:48
ParallelMultiTypeScalarProduct(const std::array< std::shared_ptr< const communication_type >, numSubDomains > &comms)
Definition: scalarproducts.hh:52
C communication_type
Definition: scalarproducts.hh:50
X domain_type
Definition: scalarproducts.hh:47
Dune::SolverCategory::Category category() const override
category of the scalar product
Definition: scalarproducts.hh:99
typename Dune::FieldTraits< field_type >::real_type real_type
Definition: scalarproducts.hh:49
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 parti...
Definition: scalarproducts.hh:63
real_type norm(const X &x) const override
compute 2-norm of a right-hand side vector
Definition: scalarproducts.hh:82
Definition: adapt.hh:17