opm-simulators
ParallelOverlappingILU0.hpp
1 /*
2  Copyright 2015 Dr. Blatt - HPC-Simulation-Software & Services
3  Copyright 2015 Statoil AS
4 
5  This file is part of the Open Porous Media project (OPM).
6 
7  OPM is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  OPM is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with OPM. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #ifndef OPM_PARALLELOVERLAPPINGILU0_HEADER_INCLUDED
21 #define OPM_PARALLELOVERLAPPINGILU0_HEADER_INCLUDED
22 #include <opm/common/TimingMacros.hpp>
23 #include <opm/simulators/linalg/MILU.hpp>
24 #include <opm/simulators/linalg/PreconditionerWithUpdate.hpp>
25 #include <dune/istl/paamg/smoother.hh>
26 
27 #include <cstddef>
28 #include <vector>
29 #include <type_traits>
30 
31 namespace Opm
32 {
33 
34 //template<class M, class X, class Y, class C>
35 //class ParallelOverlappingILU0;
36 template<class Matrix, class Domain, class Range, class ParallelInfo>
38 
39 template<class F>
41  : public Dune::Amg::DefaultSmootherArgs<F>
42 {
43  public:
45  : milu_(milu), n_(0)
46  {}
47  void setMilu(MILU_VARIANT milu)
48  {
49  milu_ = milu;
50  }
51  MILU_VARIANT getMilu() const
52  {
53  return milu_;
54  }
55  void setN(int n)
56  {
57  n_ = n;
58  }
59  int getN() const
60  {
61  return n_;
62  }
63  private:
64  MILU_VARIANT milu_;
65  int n_;
66 };
67 } // end namespace Opm
68 
69 namespace Dune
70 {
71 
72 namespace Amg
73 {
74 
75 
76 template<class M, class X, class Y, class C>
77 struct SmootherTraits<Opm::ParallelOverlappingILU0<M,X,Y,C> >
78 {
80 };
81 
88 template<class Matrix, class Domain, class Range, class ParallelInfo>
89 struct ConstructionTraits<Opm::ParallelOverlappingILU0<Matrix,Domain,Range,ParallelInfo> >
90 {
92  using Arguments = DefaultParallelConstructionArgs<T,ParallelInfo>;
93 
94  using ParallelOverlappingILU0Pointer = std::shared_ptr<T>;
95 
96  static inline ParallelOverlappingILU0Pointer construct(Arguments& args)
97  {
98  return ParallelOverlappingILU0Pointer(
99  new T(args.getMatrix(),
100  args.getComm(),
101  args.getArgs().getN(),
102  args.getArgs().relaxationFactor,
103  args.getArgs().getMilu()) );
104  }
105 };
106 
107 } // end namespace Amg
108 
109 } // end namespace Dune
110 
111 namespace Opm
112 {
128 template<class Matrix, class Domain, class Range, class ParallelInfoT>
129 class ParallelOverlappingILU0
130  : public Dune::PreconditionerWithUpdate<Domain,Range>
131 {
132  using ParallelInfo = ParallelInfoT;
133 
134 public:
136  using matrix_type = typename std::remove_const<Matrix>::type;
138  using domain_type = Domain;
140  using range_type = Range;
142  using field_type = typename Domain::field_type;
143 
144  using block_type = typename matrix_type::block_type;
145  using size_type = typename matrix_type::size_type;
146 
147  virtual bool hasPerfectUpdate() const override {
148  return true;
149  }
150 
151 protected:
152  struct CRS
153  {
154  CRS() : nRows_( 0 ) {}
155 
156  size_type rows() const { return nRows_; }
157 
158  size_type nonZeros() const
159  {
160  assert( rows_[ rows() ] != size_type(-1) );
161  return rows_[ rows() ];
162  }
163 
164  void resize( const size_type nRows )
165  {
166  if( nRows_ != nRows )
167  {
168  nRows_ = nRows ;
169  rows_.resize( nRows_+1, size_type(-1) );
170  }
171  }
172 
173  void reserveAdditional( const size_type nonZeros )
174  {
175  const size_type needed = values_.size() + nonZeros ;
176  if( values_.capacity() < needed )
177  {
178  const size_type estimate = needed * 1.1;
179  values_.reserve( estimate );
180  cols_.reserve( estimate );
181  }
182  }
183 
184  void push_back( const block_type& value, const size_type index )
185  {
186  values_.push_back( value );
187  cols_.push_back( index );
188  }
189 
190  void clear()
191  {
192  rows_.clear();
193  values_.clear();
194  cols_.clear();
195  nRows_= 0;
196  }
197 
198  std::vector<size_type> rows_;
199  std::vector<block_type> values_;
200  std::vector<size_type> cols_;
201  size_type nRows_;
202  };
203 
204 public:
205  Dune::SolverCategory::Category category() const override;
206 
220  ParallelOverlappingILU0 (const Matrix& A,
221  const int n, const field_type w,
222  MILU_VARIANT milu, bool redblack = false,
223  bool reorder_sphere = true);
224 
237  ParallelOverlappingILU0 (const Matrix& A,
238  const ParallelInfo& comm, const int n, const field_type w,
239  MILU_VARIANT milu, bool redblack = false,
240  bool reorder_sphere = true);
241 
254  ParallelOverlappingILU0 (const Matrix& A,
255  const field_type w, MILU_VARIANT milu,
256  bool redblack = false,
257  bool reorder_sphere = true);
258 
272  ParallelOverlappingILU0 (const Matrix& A,
273  const ParallelInfo& comm, const field_type w,
274  MILU_VARIANT milu, bool redblack = false,
275  bool reorder_sphere = true);
276 
291  ParallelOverlappingILU0 (const Matrix& A,
292  const ParallelInfo& comm,
293  const field_type w, MILU_VARIANT milu,
294  size_type interiorSize, bool redblack = false,
295  bool reorder_sphere = true);
296 
302  void pre (Domain&, Range&) override
303  {}
304 
310  void apply (Domain& v, const Range& d) override;
311 
312  template <class V>
313  void copyOwnerToAll( V& v ) const;
314 
320  void post (Range&) override
321  {}
322 
323  void update() override;
324 
325 protected:
327  Range& reorderD(const Range& d);
328 
330  Domain& reorderV(Domain& v);
331 
332  void reorderBack(const Range& reorderedV, Range& v);
333 
335  std::unique_ptr<Matrix> ILU_;
336  CRS lower_;
337  CRS upper_;
338  std::vector< block_type > inv_;
340  std::vector< std::size_t > ordering_;
342  Range reorderedD_;
344  Domain reorderedV_;
345 
346  const ParallelInfo* comm_;
348  const field_type w_;
349  const bool relaxation_;
350  size_type interiorSize_;
351  const Matrix* A_;
352  int iluIteration_;
353  MILU_VARIANT milu_;
354  bool redBlack_;
355  bool reorderSphere_;
356 };
357 
358 } // end namespace Opm
359 #endif
A two-step version of an overlapping Schwarz preconditioner using one step ILU0 as.
Definition: ParallelOverlappingILU0.hpp:37
MILU_VARIANT
Definition: MILU.hpp:34
typename std::remove_const< Matrix >::type matrix_type
The matrix type the preconditioner is for.
Definition: ParallelOverlappingILU0.hpp:136
const field_type w_
The relaxation factor to use.
Definition: ParallelOverlappingILU0.hpp:348
Do not perform modified ILU.
Range & reorderD(const Range &d)
Reorder D if needed and return a reference to it.
Definition: ParallelOverlappingILU0_impl.hpp:566
Range range_type
The range type of the preconditioner.
Definition: ParallelOverlappingILU0.hpp:140
Definition: fvbaseprimaryvariables.hh:161
Definition: ParallelOverlappingILU0.hpp:40
std::vector< std::size_t > ordering_
the reordering of the unknowns
Definition: ParallelOverlappingILU0.hpp:340
void apply(Domain &v, const Range &d) override
Apply the preconditoner.
Definition: ParallelOverlappingILU0_impl.hpp:323
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
Domain domain_type
The domain type of the preconditioner.
Definition: ParallelOverlappingILU0.hpp:138
std::unique_ptr< Matrix > ILU_
The ILU0 decomposition of the matrix.
Definition: ParallelOverlappingILU0.hpp:335
Interface class adding the update() method to the preconditioner interface.
Definition: PreconditionerWithUpdate.hpp:33
typename Domain::field_type field_type
The field type of the preconditioner.
Definition: ParallelOverlappingILU0.hpp:142
Domain reorderedV_
The reordered left hand side.
Definition: ParallelOverlappingILU0.hpp:344
void post(Range &) override
Clean up.
Definition: ParallelOverlappingILU0.hpp:320
Definition: ParallelOverlappingILU0.hpp:152
Range reorderedD_
The reordered right hand side.
Definition: ParallelOverlappingILU0.hpp:342
Domain & reorderV(Domain &v)
Reorder V if needed and return a reference to it.
Definition: ParallelOverlappingILU0_impl.hpp:590
void pre(Domain &, Range &) override
Prepare the preconditioner.
Definition: ParallelOverlappingILU0.hpp:302
ParallelOverlappingILU0(const Matrix &A, const int n, const field_type w, MILU_VARIANT milu, bool redblack=false, bool reorder_sphere=true)
Constructor.
Definition: ParallelOverlappingILU0_impl.hpp:233