opm-simulators
RegionPhasePVAverage.hpp
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  Copyright 2024 Equinor 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_REGION_PHASE_POREVOL_AVERAGE_MODULE_HPP
23 #define OPM_REGION_PHASE_POREVOL_AVERAGE_MODULE_HPP
24 
25 #include <opm/simulators/utils/ParallelCommunication.hpp>
26 
27 #include <cstddef>
28 #include <functional>
29 #include <string>
30 #include <string_view>
31 #include <vector>
32 
33 namespace Opm {
34 
42  {
43  public:
45  struct CellValue
46  {
48  double value { 0.0 };
49 
51  double sat { 0.0 };
52 
54  double porv { 0.0 };
55  };
56 
58  struct Phase
59  {
61  unsigned int ix;
62  };
63 
65  struct Region
66  {
68  unsigned int ix;
69  };
70 
72  using RegionArrayAccessor = std::function<const std::vector<int>&(const std::string&)>;
73 
87  explicit RegionPhasePoreVolAverage(const Parallel::Communication& comm,
88  std::size_t numPhases,
89  const std::vector<std::string>& regionNames,
90  RegionArrayAccessor getRegionArray);
91 
98  double fieldValue(const Phase& p) const;
99 
112  double value(std::string_view rset, const Phase& p, const Region& r) const;
113 
116  void prepareAccumulation();
117 
126  void addCell(std::size_t activeCell, const Phase& p, const CellValue& cv);
127 
133  void accumulateParallel();
134 
135  private:
137  using Ix = std::vector<double>::size_type;
138 
140  enum AvgType {
141  SatPV, //< Weighted by phase-filled pore-volume.
142  PV, //< Weighted by total pore-volume (s=1).
143 
144  // ---- Must be last enumerator ----
145  NumTypes, //< Number of averge types.
146  };
147 
149  enum Element {
150  Value, //< Running sum of weighted function values.
151  Weight, //< Running sum of weights.
152 
153  // ---- Must be last enumerator ----
154  NumElem, //< Number of items/elements per average type.
155  };
156 
158  std::reference_wrapper<const Parallel::Communication> comm_;
159 
161  std::size_t np_{};
162 
164  std::vector<std::string> rsetNames_{};
165 
167  RegionArrayAccessor getRegionArray_;
168 
173  std::vector<Ix> rsStart_{};
174 
190  std::vector<double> x_{};
191 
204  double averageValueWithFallback(Ix start) const;
205 
216  double averageValue(Ix start, AvgType type) const;
217 
224  Ix fieldStartIx(unsigned int phase) const;
225 
236  Ix rsetStartIx(std::size_t rset, int region, unsigned int phase) const;
237 
246  Ix startIx(std::size_t offset, unsigned int phase) const;
247 
257  int regionIndex(std::size_t rset, std::size_t activeCell) const;
258 
267  void add(Ix start, const CellValue& cv);
268 
281  void add(Ix start, AvgType type, double x, double w);
282 
294  double& value(Ix start, AvgType type);
295 
307  double& weight(Ix start, AvgType type);
308 
319  double value(Ix start, AvgType type) const;
320 
331  double weight(Ix start, AvgType type) const;
332 
349  Ix valueArrayIndex(Ix start, AvgType type, Element element) const;
350  };
351 } // namespace Opm
352 
353 #endif // OPM_REGION_PHASE_POREVOL_AVERAGE_MODULE_HPP
double value
Function value.
Definition: RegionPhasePVAverage.hpp:48
unsigned int ix
Phase index.
Definition: RegionPhasePVAverage.hpp:61
double fieldValue(const Phase &p) const
Retrieve field-level average function value for specific phase.
Definition: RegionPhasePVAverage.cpp:90
RegionPhasePoreVolAverage(const Parallel::Communication &comm, std::size_t numPhases, const std::vector< std::string > &regionNames, RegionArrayAccessor getRegionArray)
Constructor.
Definition: RegionPhasePVAverage.cpp:78
void prepareAccumulation()
Clear internal arrays in preparation of accumulating region-level averages from per-cell contribution...
Definition: RegionPhasePVAverage.cpp:111
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
double porv
Reservoir condition pore-volume.
Definition: RegionPhasePVAverage.hpp:54
double sat
Phase saturation.
Definition: RegionPhasePVAverage.hpp:51
void addCell(std::size_t activeCell, const Phase &p, const CellValue &cv)
Incorporate contributions from a single cell.
Definition: RegionPhasePVAverage.cpp:117
Facility for calculating volume-weighted average function values over user-defined regions in paralle...
Definition: RegionPhasePVAverage.hpp:41
double value(std::string_view rset, const Phase &p, const Region &r) const
Retrieve region-level average function value for specific phase in specific region of named region se...
Definition: RegionPhasePVAverage.cpp:97
Compile-time disambiguation type for phases.
Definition: RegionPhasePVAverage.hpp:58
std::function< const std::vector< int > &(const std::string &)> RegionArrayAccessor
Call-back function type for accessing region arrays–typically the FIP* arrays.
Definition: RegionPhasePVAverage.hpp:72
Minimal characteristics of a cell from a simulation grid.
Definition: RegionPhasePVAverage.hpp:45
Compile-time disambiguation type for regions.
Definition: RegionPhasePVAverage.hpp:65
void accumulateParallel()
Accumulate region-level average values across MPI ranks.
Definition: RegionPhasePVAverage.cpp:128
unsigned int ix
Region index.
Definition: RegionPhasePVAverage.hpp:68