RegionAttributeHelpers.hpp
Go to the documentation of this file.
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
27
28#include <opm/grid/utility/RegionMapping.hpp>
29
30#include <dune/grid/common/gridenums.hh>
31
32#include <algorithm>
33#include <memory>
34#include <stdexcept>
35#include <type_traits>
36#include <unordered_map>
37#include <utility>
38#include <vector>
39
40namespace 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<class Scalar, bool is_parallel>
69 {
81 std::tuple<Scalar, Scalar, Scalar, Scalar, int>
82 operator()(const std::vector<Scalar>& pressure,
83 const std::vector<Scalar>& temperature,
84 const std::vector<Scalar>& rs,
85 const std::vector<Scalar>& rv,
86 const std::vector<Scalar>& 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<class Scalar>
103 struct AverageIncrementCalculator<Scalar, false>
104 {
105 std::tuple<Scalar, Scalar, Scalar, Scalar, int>
106 operator()(const std::vector<Scalar>& pressure,
107 const std::vector<Scalar>& temperature,
108 const std::vector<Scalar>& rs,
109 const std::vector<Scalar>& rv,
110 const std::vector<Scalar>&,
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
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
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)) {
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)) {
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)) {
401 }
402
403 return p;
404 }
405 } // namespace PhasePos
406
407 } // namespace RegionAttributesHelpers
408} // namespace Opm
409
410#endif /* OPM_REGIONATTRIBUTEHELPERS_HPP_HEADER_INCLUDED */
@ Liquid
Definition: BlackoilPhases.hpp:42
@ Aqua
Definition: BlackoilPhases.hpp:42
@ Vapour
Definition: BlackoilPhases.hpp:42
Definition: RegionAttributeHelpers.hpp:133
bool has(const RegionID reg) const
Definition: RegionAttributeHelpers.hpp:212
int cell(const RegionID reg) const
Definition: RegionAttributeHelpers.hpp:207
const AttributeMap & attributes() const
Definition: RegionAttributeHelpers.hpp:231
typename Select::RegionIDParameter< RegionId, std::is_integral< RegionId >::value >::type RegionID
Definition: RegionAttributeHelpers.hpp:141
const Attributes & attributes(const RegionID reg) const
Definition: RegionAttributeHelpers.hpp:245
RegionAttributes(const RMap &rmap, const Attributes &attr)
Definition: RegionAttributeHelpers.hpp:178
typename std::remove_reference< RegionId >::type ID
Definition: RegionAttributeHelpers.hpp:144
std::unordered_map< ID, std::unique_ptr< Value > > AttributeMap
Definition: RegionAttributeHelpers.hpp:161
void insert(const RegionID r, const Attributes &attr)
Definition: RegionAttributeHelpers.hpp:217
Attributes & attributes(const RegionID reg)
Definition: RegionAttributeHelpers.hpp:258
int gas(const PhaseUsage &pu)
Definition: RegionAttributeHelpers.hpp:395
int oil(const PhaseUsage &pu)
Definition: RegionAttributeHelpers.hpp:375
int water(const PhaseUsage &pu)
Definition: RegionAttributeHelpers.hpp:355
bool water(const PhaseUsage &pu)
Definition: RegionAttributeHelpers.hpp:309
bool oil(const PhaseUsage &pu)
Definition: RegionAttributeHelpers.hpp:322
bool gas(const PhaseUsage &pu)
Definition: RegionAttributeHelpers.hpp:335
Definition: blackoilboundaryratevector.hh:37
int RegionId
Definition: WellConstraints.hpp:37
Definition: BlackoilPhases.hpp:46
std::array< int, MaxNumPhases+NumCryptoPhases > phase_used
Definition: BlackoilPhases.hpp:51
std::array< int, MaxNumPhases+NumCryptoPhases > phase_pos
Definition: BlackoilPhases.hpp:52
std::tuple< Scalar, Scalar, Scalar, Scalar, int > operator()(const std::vector< Scalar > &pressure, const std::vector< Scalar > &temperature, const std::vector< Scalar > &rs, const std::vector< Scalar > &rv, const std::vector< Scalar > &, std::size_t cell)
Definition: RegionAttributeHelpers.hpp:106
Computes the temperature, pressure, and counter increment.
Definition: RegionAttributeHelpers.hpp:69
std::tuple< Scalar, Scalar, Scalar, Scalar, int > operator()(const std::vector< Scalar > &pressure, const std::vector< Scalar > &temperature, const std::vector< Scalar > &rs, const std::vector< Scalar > &rv, const std::vector< Scalar > &ownership, std::size_t cell)
Computes the temperature, pressure, and counter increment.
Definition: RegionAttributeHelpers.hpp:82
Definition: RegionAttributeHelpers.hpp:150
Attributes attr_
Definition: RegionAttributeHelpers.hpp:156
Value(const Attributes &attr)
Definition: RegionAttributeHelpers.hpp:151
int cell_
Definition: RegionAttributeHelpers.hpp:157
RegionID type
Definition: RegionAttributeHelpers.hpp:57
Definition: RegionAttributeHelpers.hpp:49
typename std::remove_reference< RegionID >::type & type
Definition: RegionAttributeHelpers.hpp:51