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 
29 #include <dune/grid/common/gridenums.hh>
30 #include <algorithm>
31 #include <cmath>
32 #include <memory>
33 #include <stdexcept>
34 #include <type_traits>
35 #include <unordered_map>
36 #include <utility>
37 #include <vector>
38 
39 namespace Opm {
40  namespace RegionAttributeHelpers {
45  namespace Select {
46  template <class RegionID, bool>
48  {
49  using type =
50  typename std::remove_reference<RegionID>::type &;
51  };
52 
53  template <class RegionID>
54  struct RegionIDParameter<RegionID, true>
55  {
56  using type = RegionID;
57  };
58  } // Select
59 
66  template<bool is_parallel>
68  {
80  std::tuple<double, double, double, double, int>
81  operator()(const std::vector<double>& pressure,
82  const std::vector<double>& temperature,
83  const std::vector<double>& rs,
84  const std::vector<double>& rv,
85  const std::vector<double>& ownership,
86  std::size_t cell){
87  if ( ownership[cell] )
88  {
89  return std::make_tuple(pressure[cell],
90  temperature[cell],
91  rs[cell],
92  rv[cell],
93  1);
94  }
95  else
96  {
97  return std::make_tuple(0, 0, 0, 0, 0);
98  }
99  }
100  };
101  template<>
103  {
104  std::tuple<double, double, double, double, int>
105  operator()(const std::vector<double>& pressure,
106  const std::vector<double>& temperature,
107  const std::vector<double>& rs,
108  const std::vector<double>& rv,
109  const std::vector<double>&,
110  std::size_t cell){
111  return std::make_tuple(pressure[cell],
112  temperature[cell],
113  rs[cell],
114  rv[cell],
115  1);
116  }
117  };
130  template <typename RegionId, class Attributes>
132  {
133  public:
138  using RegionID =
140  <RegionId, std::is_integral<RegionId>::value>::type;
141 
142  using ID =
143  typename std::remove_reference<RegionId>::type;
144 
149  struct Value {
150  Value(const Attributes& attr)
151  : attr_(attr)
152  , cell_(-1)
153  {}
154 
155  Attributes attr_;
156  int cell_;
157  };
158 
159  using AttributeMap =
160  std::unordered_map<ID, std::unique_ptr<Value>>;
161 
162 
176  template <class RMap>
177  RegionAttributes(const RMap& rmap,
178  const Attributes& attr)
179  {
180  using VT = typename AttributeMap::value_type;
181 
182  for (const auto& r : rmap.activeRegions()) {
183  auto v = std::make_unique<Value>(attr);
184 
185  const auto stat = attr_.insert(VT(r, std::move(v)));
186 
187  if (stat.second) {
188  // New value inserted.
189  const auto& cells = rmap.cells(r);
190 
191  assert (! cells.empty());
192 
193  // Region's representative cell.
194  stat.first->second->cell_ = cells[0];
195  }
196  }
197  }
198 
206  int cell(const RegionID reg) const
207  {
208  return this->find(reg).cell_;
209  }
210 
211  bool has(const RegionID reg) const
212  {
213  return this->attr_.find(reg) != this->attr_.end();
214  }
215 
216  void insert(const RegionID r, const Attributes& attr)
217  {
218  auto [pos, inserted] = this->attr_.try_emplace(r, std::make_unique<Value>(attr));
219  if (inserted) {
220  pos->second->cell_ = -1; // NOT -1.0 -- "cell_" is 'int'
221  }
222  }
223 
230  const AttributeMap& attributes() const
231  {
232  return attr_;
233  }
234 
235 
244  const Attributes& attributes(const RegionID reg) const
245  {
246  return this->find(reg).attr_;
247  }
248 
257  Attributes& attributes(const RegionID reg)
258  {
259  return this->find(reg).attr_;
260  }
261 
262  private:
263 
264  AttributeMap attr_;
265 
269  const Value& find(const RegionID reg) const
270  {
271  const auto& i = attr_.find(reg);
272 
273  if (i == attr_.end()) {
274  throw std::invalid_argument("Unknown region ID");
275  }
276 
277  return *i->second;
278  }
279 
283  Value& find(const RegionID reg)
284  {
285  const auto& i = attr_.find(reg);
286 
287  if (i == attr_.end()) {
288  throw std::invalid_argument("Unknown region ID");
289  }
290 
291  return *i->second;
292  }
293  };
294 
299  namespace PhaseUsed {
307  inline bool
308  water(const PhaseUsage& pu)
309  {
310  return pu.phase_used[ BlackoilPhases::Aqua ] != 0;
311  }
312 
320  inline bool
321  oil(const PhaseUsage& pu)
322  {
323  return pu.phase_used[ BlackoilPhases::Liquid ] != 0;
324  }
325 
333  inline bool
334  gas(const PhaseUsage& pu)
335  {
336  return pu.phase_used[ BlackoilPhases::Vapour ] != 0;
337  }
338  } // namespace PhaseUsed
339 
344  namespace PhasePos {
353  inline int
354  water(const PhaseUsage& pu)
355  {
356  int p = -1;
357 
358  if (PhaseUsed::water(pu)) {
359  p = pu.phase_pos[ BlackoilPhases::Aqua ];
360  }
361 
362  return p;
363  }
364 
373  inline int
374  oil(const PhaseUsage& pu)
375  {
376  int p = -1;
377 
378  if (PhaseUsed::oil(pu)) {
379  p = pu.phase_pos[ BlackoilPhases::Liquid ];
380  }
381 
382  return p;
383  }
384 
393  inline int
394  gas(const PhaseUsage& pu)
395  {
396  int p = -1;
397 
398  if (PhaseUsed::gas(pu)) {
399  p = pu.phase_pos[ BlackoilPhases::Vapour ];
400  }
401 
402  return p;
403  }
404  } // namespace PhasePos
405 
406  } // namespace RegionAttributesHelpers
407 } // namespace Opm
408 
409 #endif /* OPM_REGIONATTRIBUTEHELPERS_HPP_HEADER_INCLUDED */
Provide mapping from Region IDs to user-specified collection of per-region attributes.
Definition: RegionAttributeHelpers.hpp:132
Attributes & attributes(const RegionID reg)
Request modifiable access to region's attributes.
Definition: RegionAttributeHelpers.hpp:257
int cell(const RegionID reg) const
Retrieve representative cell in region.
Definition: RegionAttributeHelpers.hpp:206
const AttributeMap & attributes() const
Request read-only access to region's attributes.
Definition: RegionAttributeHelpers.hpp:230
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:140
RegionAttributes(const RMap &rmap, const Attributes &attr)
Constructor.
Definition: RegionAttributeHelpers.hpp:177
const Attributes & attributes(const RegionID reg) const
Request read-only access to region's attributes.
Definition: RegionAttributeHelpers.hpp:244
int gas(const PhaseUsage &pu)
Numerical ID of active gas phase.
Definition: RegionAttributeHelpers.hpp:394
int oil(const PhaseUsage &pu)
Numerical ID of active oil phase.
Definition: RegionAttributeHelpers.hpp:374
int water(const PhaseUsage &pu)
Numerical ID of active water phase.
Definition: RegionAttributeHelpers.hpp:354
bool water(const PhaseUsage &pu)
Active water predicate.
Definition: RegionAttributeHelpers.hpp:308
bool oil(const PhaseUsage &pu)
Active oil predicate.
Definition: RegionAttributeHelpers.hpp:321
bool gas(const PhaseUsage &pu)
Active gas predicate.
Definition: RegionAttributeHelpers.hpp:334
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:68
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:81
Aggregate per-region attributes along with region's representative cell.
Definition: RegionAttributeHelpers.hpp:149
Definition: RegionAttributeHelpers.hpp:48