My Project
RegionAttributeHelpers.hpp
1 /*
2  Copyright 2014, 2015 SINTEF ICT, Applied Mathematics.
3  Copyright 2014, 2015 Statoil ASA.
4  Copyright 2017, IRIS
5  Copyright 2017, Equinor
6 
7  This file is part of the Open Porous Media Project (OPM).
8 
9  OPM is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OPM is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OPM. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #ifndef OPM_REGIONATTRIBUTEHELPERS_HPP_HEADER_INCLUDED
24 #define OPM_REGIONATTRIBUTEHELPERS_HPP_HEADER_INCLUDED
25 
26 #include <opm/core/props/BlackoilPhases.hpp>
27 #include <opm/grid/utility/RegionMapping.hpp>
28 #include <opm/simulators/linalg/ParallelIstlInformation.hpp>
29 
30 #include <dune/grid/common/gridenums.hh>
31 #include <algorithm>
32 #include <cmath>
33 #include <memory>
34 #include <stdexcept>
35 #include <type_traits>
36 #include <unordered_map>
37 #include <utility>
38 #include <vector>
39 
40 namespace Opm {
41  namespace RegionAttributeHelpers {
46  namespace Select {
47  template <class RegionID, bool>
49  {
50  using type =
51  typename std::remove_reference<RegionID>::type &;
52  };
53 
54  template <class RegionID>
55  struct RegionIDParameter<RegionID, true>
56  {
57  using type = RegionID;
58  };
59  } // Select
60 
67  template<bool is_parallel>
69  {
81  std::tuple<double, double, double, double, int>
82  operator()(const std::vector<double>& pressure,
83  const std::vector<double>& temperature,
84  const std::vector<double>& rs,
85  const std::vector<double>& rv,
86  const std::vector<double>& ownership,
87  std::size_t cell){
88  if ( ownership[cell] )
89  {
90  return std::make_tuple(pressure[cell],
91  temperature[cell],
92  rs[cell],
93  rv[cell],
94  1);
95  }
96  else
97  {
98  return std::make_tuple(0, 0, 0, 0, 0);
99  }
100  }
101  };
102  template<>
104  {
105  std::tuple<double, double, double, double, int>
106  operator()(const std::vector<double>& pressure,
107  const std::vector<double>& temperature,
108  const std::vector<double>& rs,
109  const std::vector<double>& rv,
110  const std::vector<double>&,
111  std::size_t cell){
112  return std::make_tuple(pressure[cell],
113  temperature[cell],
114  rs[cell],
115  rv[cell],
116  1);
117  }
118  };
131  template <typename RegionId, class Attributes>
133  {
134  public:
139  using RegionID =
141  <RegionId, std::is_integral<RegionId>::value>::type;
142 
143  using ID =
144  typename std::remove_reference<RegionId>::type;
145 
150  struct Value {
151  Value(const Attributes& attr)
152  : attr_(attr)
153  , cell_(-1)
154  {}
155 
156  Attributes attr_;
157  int cell_;
158  };
159 
160  using AttributeMap =
161  std::unordered_map<ID, std::unique_ptr<Value>>;
162 
163 
177  template <class RMap>
178  RegionAttributes(const RMap& rmap,
179  const Attributes& attr)
180  {
181  using VT = typename AttributeMap::value_type;
182 
183  for (const auto& r : rmap.activeRegions()) {
184  auto v = std::make_unique<Value>(attr);
185 
186  const auto stat = attr_.insert(VT(r, std::move(v)));
187 
188  if (stat.second) {
189  // New value inserted.
190  const auto& cells = rmap.cells(r);
191 
192  assert (! cells.empty());
193 
194  // Region's representative cell.
195  stat.first->second->cell_ = cells[0];
196  }
197  }
198  }
199 
207  int cell(const RegionID reg) const
208  {
209  return this->find(reg).cell_;
210  }
211 
212  bool has(const RegionID reg) const
213  {
214  return this->attr_.find(reg) != this->attr_.end();
215  }
216 
217  void insert(const RegionID r, const Attributes& attr)
218  {
219  auto [pos, inserted] = this->attr_.try_emplace(r, std::make_unique<Value>(attr));
220  if (inserted) {
221  pos->second->cell_ = -1; // NOT -1.0 -- "cell_" is 'int'
222  }
223  }
224 
231  const AttributeMap& attributes() const
232  {
233  return attr_;
234  }
235 
236 
245  const Attributes& attributes(const RegionID reg) const
246  {
247  return this->find(reg).attr_;
248  }
249 
258  Attributes& attributes(const RegionID reg)
259  {
260  return this->find(reg).attr_;
261  }
262 
263  private:
264 
265  AttributeMap attr_;
266 
270  const Value& find(const RegionID reg) const
271  {
272  const auto& i = attr_.find(reg);
273 
274  if (i == attr_.end()) {
275  throw std::invalid_argument("Unknown region ID");
276  }
277 
278  return *i->second;
279  }
280 
284  Value& find(const RegionID reg)
285  {
286  const auto& i = attr_.find(reg);
287 
288  if (i == attr_.end()) {
289  throw std::invalid_argument("Unknown region ID");
290  }
291 
292  return *i->second;
293  }
294  };
295 
300  namespace PhaseUsed {
308  inline bool
309  water(const PhaseUsage& pu)
310  {
311  return pu.phase_used[ BlackoilPhases::Aqua ] != 0;
312  }
313 
321  inline bool
322  oil(const PhaseUsage& pu)
323  {
324  return pu.phase_used[ BlackoilPhases::Liquid ] != 0;
325  }
326 
334  inline bool
335  gas(const PhaseUsage& pu)
336  {
337  return pu.phase_used[ BlackoilPhases::Vapour ] != 0;
338  }
339  } // namespace PhaseUsed
340 
345  namespace PhasePos {
354  inline int
355  water(const PhaseUsage& pu)
356  {
357  int p = -1;
358 
359  if (PhaseUsed::water(pu)) {
360  p = pu.phase_pos[ BlackoilPhases::Aqua ];
361  }
362 
363  return p;
364  }
365 
374  inline int
375  oil(const PhaseUsage& pu)
376  {
377  int p = -1;
378 
379  if (PhaseUsed::oil(pu)) {
380  p = pu.phase_pos[ BlackoilPhases::Liquid ];
381  }
382 
383  return p;
384  }
385 
394  inline int
395  gas(const PhaseUsage& pu)
396  {
397  int p = -1;
398 
399  if (PhaseUsed::gas(pu)) {
400  p = pu.phase_pos[ BlackoilPhases::Vapour ];
401  }
402 
403  return p;
404  }
405  } // namespace PhasePos
406 
407  } // namespace RegionAttributesHelpers
408 } // namespace Opm
409 
410 #endif /* OPM_REGIONATTRIBUTEHELPERS_HPP_HEADER_INCLUDED */
Provide mapping from Region IDs to user-specified collection of per-region attributes.
Definition: RegionAttributeHelpers.hpp:133
Attributes & attributes(const RegionID reg)
Request modifiable access to region's attributes.
Definition: RegionAttributeHelpers.hpp:258
int cell(const RegionID reg) const
Retrieve representative cell in region.
Definition: RegionAttributeHelpers.hpp:207
const AttributeMap & attributes() const
Request read-only access to region's attributes.
Definition: RegionAttributeHelpers.hpp:231
typename Select::RegionIDParameter< RegionId, std::is_integral< RegionId >::value >::type RegionID
Expose RegionId as a vocabulary type for use in query methods.
Definition: RegionAttributeHelpers.hpp:141
RegionAttributes(const RMap &rmap, const Attributes &attr)
Constructor.
Definition: RegionAttributeHelpers.hpp:178
const Attributes & attributes(const RegionID reg) const
Request read-only access to region's attributes.
Definition: RegionAttributeHelpers.hpp:245
int gas(const PhaseUsage &pu)
Numerical ID of active gas phase.
Definition: RegionAttributeHelpers.hpp:395
int oil(const PhaseUsage &pu)
Numerical ID of active oil phase.
Definition: RegionAttributeHelpers.hpp:375
int water(const PhaseUsage &pu)
Numerical ID of active water phase.
Definition: RegionAttributeHelpers.hpp:355
bool water(const PhaseUsage &pu)
Active water predicate.
Definition: RegionAttributeHelpers.hpp:309
bool oil(const PhaseUsage &pu)
Active oil predicate.
Definition: RegionAttributeHelpers.hpp:322
bool gas(const PhaseUsage &pu)
Active gas predicate.
Definition: RegionAttributeHelpers.hpp:335
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: BlackoilPhases.hpp:27
Definition: BlackoilPhases.hpp:46
Computes the temperature, pressure, and counter increment.
Definition: RegionAttributeHelpers.hpp:69
std::tuple< double, double, double, double, int > operator()(const std::vector< double > &pressure, const std::vector< double > &temperature, const std::vector< double > &rs, const std::vector< double > &rv, const std::vector< double > &ownership, std::size_t cell)
Computes the temperature, pressure, and counter increment.
Definition: RegionAttributeHelpers.hpp:82
Aggregate per-region attributes along with region's representative cell.
Definition: RegionAttributeHelpers.hpp:150
Definition: RegionAttributeHelpers.hpp:49