opm-simulators
MultisegmentWellEquations.hpp
1 /*
2  Copyright 2017 SINTEF Digital, Mathematics and Cybernetics.
3  Copyright 2017 Statoil ASA.
4  Copyright 2016 - 2017 IRIS AS.
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_MULTISEGMENTWELL_EQUATIONS_HEADER_INCLUDED
23 #define OPM_MULTISEGMENTWELL_EQUATIONS_HEADER_INCLUDED
24 
25 #include <opm/simulators/utils/ParallelCommunication.hpp>
26 #include <opm/simulators/wells/ParallelWellInfo.hpp>
27 #include <opm/simulators/wells/MSWellHelpers.hpp>
28 #include <dune/common/fmatrix.hh>
29 #include <dune/common/fvector.hh>
30 #include <dune/istl/bcrsmatrix.hh>
31 #include <dune/istl/bvector.hh>
32 
33 #include <memory>
34 #include <type_traits>
35 
36 namespace Dune {
37 template<class M> class UMFPack;
38 }
39 
40 namespace Opm
41 {
42 
43 template<class Scalar, typename IndexTraits, int numWellEq, int numEq> class MultisegmentWellEquationAccess;
44 template<class Scalar, typename IndexTraits> class MultisegmentWellGeneric;
45 #if COMPILE_GPU_BRIDGE
46 template<class Scalar> class WellContributions;
47 #endif
48 template<typename Scalar, typename IndexTraits> class WellInterfaceGeneric;
49 template<typename Scalar, typename IndexTraits> class WellState;
50 
51 template<class Scalar, typename IndexTraits, int numWellEq, int numEq>
53 {
54 public:
55  // sparsity pattern for the matrices
56  // [A C^T [x = [ res
57  // B D ] x_well] res_well]
58 
59  // the vector type for the res_well and x_well
60  using VectorBlockWellType = Dune::FieldVector<Scalar,numWellEq>;
61  using BVectorWell = Dune::BlockVector<VectorBlockWellType>;
62 
63  using VectorBlockType = Dune::FieldVector<Scalar,numEq>;
64  using BVector = Dune::BlockVector<VectorBlockType>;
65 
66  // the matrix type for the diagonal matrix D
67  using DiagMatrixBlockWellType = Dune::FieldMatrix<Scalar,numWellEq,numWellEq>;
68  using DiagMatWell = Dune::BCRSMatrix<DiagMatrixBlockWellType>;
69 
70  // the matrix type for the non-diagonal matrix B and C^T
71  using OffDiagMatrixBlockWellType = Dune::FieldMatrix<Scalar,numWellEq,numEq>;
72  using OffDiagMatWell = Dune::BCRSMatrix<OffDiagMatrixBlockWellType>;
73 
75 
82  void init(const int numPerfs,
83  const std::vector<int>& cells,
84  const std::vector<std::vector<int>>& segment_inlets,
85  const std::vector<std::vector<int>>& segment_perforations,
86  const ParallelWellInfo<Scalar>& parallel_well_info);
87 
89  void clear();
90 
92  void apply(const BVector& x, BVector& Ax) const;
93 
95  void apply(BVector& r) const;
96 
98  void createSolver();
99 
101  BVectorWell solve() const;
102 
104  BVectorWell solve(const BVectorWell& rhs) const;
105 
108  void recoverSolutionWell(const BVector& x, BVectorWell& xw) const;
109 
110 #if COMPILE_GPU_BRIDGE
111  void extract(WellContributions<Scalar>& wellContribs) const;
113 #endif
114 
116  template<class SparseMatrixAdapter>
117  void extract(SparseMatrixAdapter& jacobian) const;
118 
120  template<class PressureMatrix>
121  void extractCPRPressureMatrix(PressureMatrix& jacobian,
122  const BVector& weights,
123  const int pressureVarIndex,
124  const bool /*use_well_weights*/,
126  const int seg_pressure_var_ind,
127  const WellState<Scalar, IndexTraits>& well_state) const;
128 
130  void sumDistributed(Parallel::Communication comm);
131 
133  const BVectorWell& residual() const
134  {
135  return resWell_;
136  }
137 
138  private:
139  friend class MultisegmentWellEquationAccess<Scalar,IndexTraits,numWellEq,numEq>;
140  // two off-diagonal matrices
141  OffDiagMatWell duneB_;
142  OffDiagMatWell duneC_;
143  // "diagonal" matrix for the well. It has offdiagonal entries for inlets and outlets.
144  DiagMatWell duneD_;
145 
149  struct EmptyType {};
150  using UMFPackSolver = std::conditional_t<std::is_same_v<Scalar,double>,
151  std::shared_ptr<Dune::UMFPack<DiagMatWell>>,
152  EmptyType>; // TODO: c++20: add no_unique_address
153  mutable UMFPackSolver duneDSolver_;
154 
155  // residuals of the well equations
156  BVectorWell resWell_;
157 
158  const MultisegmentWellGeneric<Scalar, IndexTraits>& well_;
159 
160  // Store the global index of well perforated cells
161  std::vector<int> cells_;
162 
163  // Wrapper for the parallel application of B for distributed wells
164  mswellhelpers::ParallellMSWellB<OffDiagMatWell> parallelB_;
165 };
166 
167 }
168 
169 #endif // OPM_MULTISEGMENTWELLWELL_EQUATIONS_HEADER_INCLUDED
void createSolver()
Compute the LU-decomposition of D matrix.
Definition: MultisegmentWellEquations.cpp:195
Definition: fvbaseprimaryvariables.hh:161
void init(const int numPerfs, const std::vector< int > &cells, const std::vector< std::vector< int >> &segment_inlets, const std::vector< std::vector< int >> &segment_perforations, const ParallelWellInfo< Scalar > &parallel_well_info)
Setup sparsity pattern for the matrices.
Definition: MultisegmentWellEquations.cpp:62
Definition: MultisegmentWellAssemble.hpp:34
void extract(SparseMatrixAdapter &jacobian) const
Add the matrices of this well to the sparse matrix adapter.
Definition: MultisegmentWellEquations.cpp:333
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
BVectorWell solve() const
Apply inverted D matrix to residual and return result.
Definition: MultisegmentWellEquations.cpp:215
void recoverSolutionWell(const BVector &x, BVectorWell &xw) const
Recover well solution.
Definition: MultisegmentWellEquations.cpp:245
void extractCPRPressureMatrix(PressureMatrix &jacobian, const BVector &weights, const int pressureVarIndex, const bool, const WellInterfaceGeneric< Scalar, IndexTraits > &well, const int seg_pressure_var_ind, const WellState< Scalar, IndexTraits > &well_state) const
Extract CPR pressure matrix.
Definition: MultisegmentWellEquations.cpp:372
Class encapsulating some information about parallel wells.
Definition: MSWellHelpers.hpp:34
const BVectorWell & residual() const
Returns a const reference to the residual.
Definition: MultisegmentWellEquations.hpp:133
This class serves to eliminate the need to include the WellContributions into the matrix (with –matr...
Definition: GpuBridge.hpp:30
Class administering assembler access to equation system.
Definition: MultisegmentWellAssemble.cpp:45
Definition: MultisegmentWellEquations.hpp:44
void clear()
Set all coefficients to 0.
Definition: MultisegmentWellEquations.cpp:141
void apply(const BVector &x, BVector &Ax) const
Apply linear operator to vector.
Definition: MultisegmentWellEquations.cpp:154
Definition: BlackoilWellModelGeneric.hpp:75
void sumDistributed(Parallel::Communication comm)
Sum with off-process contribution.
Definition: MultisegmentWellEquations.cpp:457
The state of a set of wells, tailored for use by the fully implicit blackoil simulator.
Definition: TemperatureModel.hpp:61