opm-simulators
PreconditionerFactory.hpp
1 
2 /*
3  Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
4  Copyright 2019 SINTEF Digital, Mathematics and Cybernetics.
5 
6  This file is part of the Open Porous Media project (OPM).
7 
8  OPM is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  OPM is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with OPM. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #ifndef OPM_PRECONDITIONERFACTORY_HEADER
23 #define OPM_PRECONDITIONERFACTORY_HEADER
24 #include <opm/common/TimingMacros.hpp>
25 #include <opm/simulators/linalg/PreconditionerWithUpdate.hpp>
26 
27 #include <dune/istl/paamg/aggregates.hh>
28 #include <dune/istl/paamg/matrixhierarchy.hh>
29 
30 #include <cstddef>
31 #include <map>
32 #include <memory>
33 #include <limits>
34 #include <string>
35 
36 namespace Opm
37 {
38 
39 class PropertyTree;
40 
41 template <class Operator, class Comm, class Matrix, class Vector>
42 struct AMGHelper
43 {
44  using PrecPtr = std::shared_ptr<Dune::PreconditionerWithUpdate<Vector, Vector>>;
45  using CriterionBase
46  = Dune::Amg::AggregationCriterion<Dune::Amg::SymmetricDependency<Matrix, Dune::Amg::FirstDiagonal>>;
47  using Criterion = Dune::Amg::CoarsenCriterion<CriterionBase>;
48 
49  static Criterion criterion(const PropertyTree& prm);
50 
51  template <class Smoother>
52  static PrecPtr makeAmgPreconditioner(const Operator& op,
53  const PropertyTree& prm,
54  bool useKamg = false);
55 };
56 
62 template <class Operator, class Comm>
64 {
65 public:
67  using Matrix = typename Operator::matrix_type;
68  using Vector = typename Operator::domain_type; // Assuming symmetry: that domain and range types are the same.
69 
71  using PrecPtr = std::shared_ptr<Dune::PreconditionerWithUpdate<Vector, Vector>>;
72 
74  using Creator = std::function<PrecPtr(const Operator&, const PropertyTree&,
75  const std::function<Vector()>&, std::size_t)>;
76  using ParCreator = std::function<PrecPtr(const Operator&, const PropertyTree&,
77  const std::function<Vector()>&, std::size_t, const Comm&)>;
78 
85  static PrecPtr create(const Operator& op, const PropertyTree& prm,
86  const std::function<Vector()>& weightsCalculator = {},
87  std::size_t pressureIndex = std::numeric_limits<std::size_t>::max());
88 
96  static PrecPtr create(const Operator& op, const PropertyTree& prm,
97  const std::function<Vector()>& weightsCalculator, const Comm& comm,
98  std::size_t pressureIndex = std::numeric_limits<std::size_t>::max());
99 
106  static PrecPtr create(const Operator& op, const PropertyTree& prm, const Comm& comm,
107  std::size_t pressureIndex = std::numeric_limits<std::size_t>::max());
108 
116  static void addCreator(const std::string& type, Creator creator);
117 
125  static void addCreator(const std::string& type, ParCreator creator);
126 
127  using CriterionBase
128  = Dune::Amg::AggregationCriterion<Dune::Amg::SymmetricDependency<Matrix, Dune::Amg::FirstDiagonal>>;
129  using Criterion = Dune::Amg::CoarsenCriterion<CriterionBase>;
130 
131 
132 private:
133  // The method that implements the singleton pattern,
134  // using the Meyers singleton technique.
135  static PreconditionerFactory& instance();
136 
137  // Private constructor, to keep users from creating a PreconditionerFactory.
139 
140  // Actually creates the product object.
141  PrecPtr doCreate(const Operator& op, const PropertyTree& prm,
142  const std::function<Vector()> weightsCalculator,
143  std::size_t pressureIndex);
144 
145  PrecPtr doCreate(const Operator& op, const PropertyTree& prm,
146  const std::function<Vector()> weightsCalculator,
147  std::size_t pressureIndex, const Comm& comm);
148 
149  // Actually adds the creator.
150  void doAddCreator(const std::string& type, Creator c);
151 
152  // Actually adds the creator.
153  void doAddCreator(const std::string& type, ParCreator c);
154 
155  // This map contains the whole factory, i.e. all the Creators.
156  std::map<std::string, Creator> creators_;
157  std::map<std::string, ParCreator> parallel_creators_;
158  bool defAdded_= false;
159 };
160 
161 } // namespace Dune
162 
163 #endif // OPM_PRECONDITIONERFACTORY_HEADER
Definition: PreconditionerFactory.hpp:42
typename Operator::matrix_type Matrix
Linear algebra types.
Definition: PreconditionerFactory.hpp:67
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
static void addCreator(const std::string &type, Creator creator)
Add a creator for a serial preconditioner to the PreconditionerFactory.
Definition: PreconditionerFactory_impl.hpp:186
std::function< PrecPtr(const Operator &, const PropertyTree &, const std::function< Vector()> &, std::size_t)> Creator
The type of creator functions passed to addCreator().
Definition: PreconditionerFactory.hpp:75
static PrecPtr create(const Operator &op, const PropertyTree &prm, const std::function< Vector()> &weightsCalculator={}, std::size_t pressureIndex=std::numeric_limits< std::size_t >::max())
Create a new serial preconditioner and return a pointer to it.
Definition: PreconditionerFactory_impl.hpp:154
Hierarchical collection of key/value pairs.
Definition: PropertyTree.hpp:38
This is an object factory for creating preconditioners.
Definition: OwningTwoLevelPreconditioner.hpp:43
std::shared_ptr< Dune::PreconditionerWithUpdate< Vector, Vector > > PrecPtr
The type of pointer returned by create().
Definition: PreconditionerFactory.hpp:71