opm-simulators
OwningBlockPreconditioner.hpp
1 /*
2  Copyright 2019 SINTEF Digital, Mathematics and Cybernetics.
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 3 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 
20 #ifndef OPM_OWNINGBLOCKPRECONDITIONER_HEADER_INCLUDED
21 #define OPM_OWNINGBLOCKPRECONDITIONER_HEADER_INCLUDED
22 
23 #include <opm/common/TimingMacros.hpp>
24 
25 #include <opm/simulators/linalg/PreconditionerWithUpdate.hpp>
26 
27 #include <dune/istl/schwarz.hh>
28 
29 namespace Dune
30 {
31 
32 template <class OriginalPreconditioner, class Comm>
33 class OwningBlockPreconditioner : public PreconditionerWithUpdate<typename OriginalPreconditioner::domain_type,
34  typename OriginalPreconditioner::range_type>
35 {
36 public:
37  template <class... Args>
38  OwningBlockPreconditioner(const Comm& comm, Args&&... args)
39  : orig_precond_(std::forward<Args>(args)...)
40  , block_precond_(orig_precond_, comm)
41  {
42  }
43 
44  using X = typename OriginalPreconditioner::domain_type;
45  using Y = typename OriginalPreconditioner::range_type;
46 
47  virtual void pre(X& x, Y& b) override
48  {
49  OPM_TIMEBLOCK(pre);
50  block_precond_.pre(x, b);
51  }
52 
53  virtual void apply(X& v, const Y& d) override
54  {
55  OPM_TIMEBLOCK(apply);
56  block_precond_.apply(v, d);
57  }
58 
59  virtual void post(X& x) override
60  {
61  OPM_TIMEBLOCK(post);
62  block_precond_.post(x);
63  }
64 
65  virtual SolverCategory::Category category() const override
66  {
67  return block_precond_.category();
68  }
69 
70  // The update() function does nothing for a wrapped preconditioner.
71  virtual void update() override
72  {
73  OPM_TIMEBLOCK(update);
74  orig_precond_.update();
75  }
76 
77  virtual bool hasPerfectUpdate() const override {
78  return orig_precond_.hasPerfectUpdate();
79  }
80 
81 private:
82  OriginalPreconditioner orig_precond_;
83  BlockPreconditioner<X, Y, Comm, OriginalPreconditioner> block_precond_;
84 };
85 
86 
87 template <class OriginalPreconditioner, class Comm, class... Args>
88 std::shared_ptr<OwningBlockPreconditioner<OriginalPreconditioner, Comm>>
89 wrapBlockPreconditioner(const Comm& comm, Args&&... args)
90 {
91  return std::make_shared<OwningBlockPreconditioner<OriginalPreconditioner, Comm>>(comm, std::forward<Args>(args)...);
92 }
93 
94 } // namespace Dune
95 
96 #endif // OPM_OWNINGBLOCKPRECONDITIONER_HEADER_INCLUDED
Definition: fvbaseprimaryvariables.hh:161
Interface class adding the update() method to the preconditioner interface.
Definition: PreconditionerWithUpdate.hpp:33
Definition: OwningBlockPreconditioner.hpp:33