opm-simulators
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/grid/utility/RegionMapping.hpp>
27 
28 #include <dune/grid/common/gridenums.hh>
29 
30 #include <algorithm>
31 #include <memory>
32 #include <stdexcept>
33 #include <type_traits>
34 #include <unordered_map>
35 #include <utility>
36 #include <vector>
37 
38 namespace Opm {
39  namespace RegionAttributeHelpers {
44  namespace Select {
45  template <class RegionID, bool>
47  {
48  using type =
49  typename std::remove_reference<RegionID>::type &;
50  };
51 
52  template <class RegionID>
53  struct RegionIDParameter<RegionID, true>
54  {
55  using type = RegionID;
56  };
57  } // Select
58 
65  template<class Scalar, bool is_parallel>
67  {
79  std::tuple<Scalar, Scalar, Scalar, Scalar, int>
80  operator()(const std::vector<Scalar>& pressure,
81  const std::vector<Scalar>& temperature,
82  const std::vector<Scalar>& rs,
83  const std::vector<Scalar>& rv,
84  const std::vector<Scalar>& ownership,
85  std::size_t cell){
86  if ( ownership[cell] )
87  {
88  return std::make_tuple(pressure[cell],
89  temperature[cell],
90  rs[cell],
91  rv[cell],
92  1);
93  }
94  else
95  {
96  return std::make_tuple(0, 0, 0, 0, 0);
97  }
98  }
99  };
100  template<class Scalar>
101  struct AverageIncrementCalculator<Scalar, false>
102  {
103  std::tuple<Scalar, Scalar, Scalar, Scalar, int>
104  operator()(const std::vector<Scalar>& pressure,
105  const std::vector<Scalar>& temperature,
106  const std::vector<Scalar>& rs,
107  const std::vector<Scalar>& rv,
108  const std::vector<Scalar>&,
109  std::size_t cell){
110  return std::make_tuple(pressure[cell],
111  temperature[cell],
112  rs[cell],
113  rv[cell],
114  1);
115  }
116  };
129  template <typename RegionId, class Attributes>
131  {
132  public:
137  using RegionID =
139  <RegionId, std::is_integral<RegionId>::value>::type;
140 
141  using ID =
142  typename std::remove_reference<RegionId>::type;
143 
148  struct Value {
149  explicit Value(const Attributes& attr)
150  : attr_(attr)
151  , cell_(-1)
152  {}
153 
154  Attributes attr_;
155  int cell_;
156  };
157 
158  using AttributeMap =
159  std::unordered_map<ID, std::unique_ptr<Value>>;
160 
161 
175  template <class RMap>
176  RegionAttributes(const RMap& rmap,
177  const Attributes& attr)
178  {
179  using VT = typename AttributeMap::value_type;
180 
181  for (const auto& r : rmap.activeRegions()) {
182  auto v = std::make_unique<Value>(attr);
183 
184  const auto stat = attr_.insert(VT(r, std::move(v)));
185 
186  if (stat.second) {
187  // New value inserted.
188  const auto& cells = rmap.cells(r);
189 
190  assert (! cells.empty());
191 
192  // Region's representative cell.
193  stat.first->second->cell_ = cells[0];
194  }
195  }
196  }
197 
205  int cell(const RegionID reg) const
206  {
207  return this->find(reg).cell_;
208  }
209 
210  bool has(const RegionID reg) const
211  {
212  return this->attr_.find(reg) != this->attr_.end();
213  }
214 
215  void insert(const RegionID r, const Attributes& attr)
216  {
217  auto [pos, inserted] = this->attr_.try_emplace(r, std::make_unique<Value>(attr));
218  if (inserted) {
219  pos->second->cell_ = -1; // NOT -1.0 -- "cell_" is 'int'
220  }
221  }
222 
229  const AttributeMap& attributes() const
230  {
231  return attr_;
232  }
233 
234 
243  const Attributes& attributes(const RegionID reg) const
244  {
245  return this->find(reg).attr_;
246  }
247 
256  Attributes& attributes(const RegionID reg)
257  {
258  return this->find(reg).attr_;
259  }
260 
261  private:
262 
263  AttributeMap attr_;
264 
268  const Value& find(const RegionID reg) const
269  {
270  const auto& i = attr_.find(reg);
271 
272  if (i == attr_.end()) {
273  throw std::invalid_argument("Unknown region ID");
274  }
275 
276  return *i->second;
277  }
278 
282  Value& find(const RegionID reg)
283  {
284  const auto& i = attr_.find(reg);
285 
286  if (i == attr_.end()) {
287  throw std::invalid_argument("Unknown region ID");
288  }
289 
290  return *i->second;
291  }
292  };
293  } // namespace RegionAttributesHelpers
294 } // namespace Opm
295 
296 #endif /* OPM_REGIONATTRIBUTEHELPERS_HPP_HEADER_INCLUDED */
const AttributeMap & attributes() const
Request read-only access to region&#39;s attributes.
Definition: RegionAttributeHelpers.hpp:229
Computes the temperature, pressure, and counter increment.
Definition: RegionAttributeHelpers.hpp:66
Aggregate per-region attributes along with region&#39;s representative cell.
Definition: RegionAttributeHelpers.hpp:148
Attributes & attributes(const RegionID reg)
Request modifiable access to region&#39;s attributes.
Definition: RegionAttributeHelpers.hpp:256
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:80
int cell(const RegionID reg) const
Retrieve representative cell in region.
Definition: RegionAttributeHelpers.hpp:205
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
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:139
Definition: RegionAttributeHelpers.hpp:46
RegionAttributes(const RMap &rmap, const Attributes &attr)
Constructor.
Definition: RegionAttributeHelpers.hpp:176
Provide mapping from Region IDs to user-specified collection of per-region attributes.
Definition: RegionAttributeHelpers.hpp:130
const Attributes & attributes(const RegionID reg) const
Request read-only access to region&#39;s attributes.
Definition: RegionAttributeHelpers.hpp:243