opm-common
DatumDepth.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 ASA.
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 DATUM_DEPTH_HPP_INCLUDED
23 #define DATUM_DEPTH_HPP_INCLUDED
24 
25 #include <cassert>
26 #include <string>
27 #include <string_view>
28 #include <variant>
29 #include <vector>
30 
35 
36 namespace Opm {
37  class SOLUTIONSection;
38 } // namespace Opm
39 
40 namespace Opm {
41 
44  class DatumDepth
45  {
46  public:
48  DatumDepth() = default;
49 
53  explicit DatumDepth(const SOLUTIONSection& soln);
54 
57  static DatumDepth serializationTestObjectZero();
58 
62  static DatumDepth serializationTestObjectGlobal();
63 
67  static DatumDepth serializationTestObjectDefaultRegion();
68 
72  static DatumDepth serializationTestObjectUserDefined();
73 
81  double operator()(const int region) const
82  {
83  return (*this)("FIPNUM", region);
84  }
85 
94  double operator()(std::string_view rset, const int region) const
95  {
96  return std::visit([rset, region](const auto& datumDepthImpl)
97  { return datumDepthImpl(rset, region); }, this->datum_);
98  }
99 
106  bool operator==(const DatumDepth& that) const
107  {
108  return this->datum_ == that.datum_;
109  }
110 
116  template <class Serializer>
117  void serializeOp(Serializer& serializer)
118  {
119  serializer(this->datum_);
120  }
121 
122 #ifdef __NVCC__
123  // NVCC struggles with the std::variant in this class and requires
124  // that the nested classes are public. This workaround should
125  // be avoided if a better fix is found.
126  public:
127 #else
128  private:
129 #endif
130  class Zero
132  {
133  public:
144  double operator()([[maybe_unused]] std::string_view rset,
145  [[maybe_unused]] const int region) const
146  {
147  return 0.0;
148  }
149 
151  static Zero serializationTestObject() { return {}; }
152 
154  bool operator==(const Zero&) const { return true; }
155 
162  template <class Serializer>
163  void serializeOp([[maybe_unused]] Serializer& serializer)
164  {} // Nothing to do
165  };
166 
168  class Global
169  {
170  public:
172  Global() = default;
173 
177  explicit Global(const SOLUTIONSection& soln);
178 
187  double operator()([[maybe_unused]] std::string_view rset,
188  [[maybe_unused]] const int region) const
189  {
190  return this->depth_;
191  }
192 
194  static Global serializationTestObject();
195 
202  bool operator==(const Global& that) const
203  {
204  return this->depth_ == that.depth_;
205  }
206 
213  template <class Serializer>
214  void serializeOp(Serializer& serializer)
215  {
216  serializer(this->depth_);
217  }
218 
219  private:
222  double depth_{};
223  };
224 
226  class DefaultRegion
227  {
228  public:
230  DefaultRegion() = default;
231 
235  explicit DefaultRegion(const SOLUTIONSection& soln);
236 
238  static DefaultRegion serializationTestObject();
239 
248  double operator()([[maybe_unused]] std::string_view rset,
249  const int region) const
250  {
251  assert (! this->depth_.empty());
252 
253  // Note: depth_.back() is intentional for region >= size().
254  // If the input supplies fewer depth values than there are
255  // regions, then the remaining regions implicitly have the
256  // same datum depth as the last fully specified region.
257  return (region < static_cast<int>(this->depth_.size()))
258  ? this->depth_[region] : this->depth_.back();
259  }
260 
267  bool operator==(const DefaultRegion& that) const
268  {
269  return this->depth_ == that.depth_;
270  }
271 
278  template <class Serializer>
279  void serializeOp(Serializer& serializer)
280  {
281  serializer(this->depth_);
282  }
283 
284  private:
287  std::vector<double> depth_{};
288  };
289 
291  class UserDefined
292  {
293  public:
295  UserDefined() = default;
296 
300  explicit UserDefined(const SOLUTIONSection& soln);
301 
303  static UserDefined serializationTestObject();
304 
313  double operator()(std::string_view rset, const int region) const;
314 
321  bool operator==(const UserDefined& that) const
322  {
323  return (this->rsetNames_ == that.rsetNames_)
324  && (this->rsetStart_ == that.rsetStart_)
325  && (this->depth_ == that.depth_)
326  && (this->default_ == that.default_)
327  && (this->rsetIndex_ == that.rsetIndex_)
328  ;
329  }
330 
337  template <class Serializer>
338  void serializeOp(Serializer& serializer)
339  {
340  serializer(this->rsetNames_);
341  serializer(this->rsetStart_);
342  serializer(this->depth_);
343  serializer(this->default_);
344  serializer(this->rsetIndex_);
345  }
346 
347  private:
350  std::vector<std::string> rsetNames_{};
351 
353  std::vector<std::vector<double>::size_type> rsetStart_{};
354 
357  std::vector<double> depth_{};
358 
361  std::vector<double> default_{};
362 
365  std::vector<std::vector<std::string>::size_type> rsetIndex_{};
366 
384  double depthValue(std::string_view rset,
385  std::vector<double>::const_iterator begin,
386  std::vector<double>::const_iterator end,
387  std::vector<double>::difference_type ix) const;
388  };
389 
390  private:
392  std::variant<Zero, Global, DefaultRegion, UserDefined> datum_{};
393  };
394 
395 } // namespace Opm
396 
397 #endif // DATUM_DEPTH_HPP_INCLUDED
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30