opm-common
PAvgDynamicSourceData.hpp
1 /*
2  Copyright 2023 Equinor ASA.
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef PAVE_DYNAMIC_SOURCE_DATA_HPP
21 #define PAVE_DYNAMIC_SOURCE_DATA_HPP
22 
23 #include <algorithm>
24 #include <cstddef>
25 #include <optional>
26 #include <stdexcept>
27 #include <type_traits>
28 #include <unordered_map>
29 #include <vector>
30 
31 namespace Opm {
32 
34 template <typename Scalar>
35 class PAvgDynamicSourceData
36 {
37 public:
43  template <typename T>
45  {
46  private:
47  friend class PAvgDynamicSourceData<Scalar>;
48 
49  public:
51  enum class Item
52  {
53  Pressure, //< Dynamic pressure value
54  MixtureDensity, //< Dynamic mixture density
55  PoreVol, //< Dynamic pore volume
56  Depth, //< Constant depth location
57 
58  // ----------------------------------------
59 
60  Last_Do_Not_Use, //< Simplifies item count
61  };
62 
63  using ElmT = std::remove_cv_t<T>;
64 
69  [[nodiscard]] constexpr ElmT operator[](const Item i) const
70  {
71  return this->begin_[this->index(i)];
72  }
73 
81  template <typename Ret = SourceDataSpan&>
82  constexpr std::enable_if_t<! std::is_const_v<T>, Ret>
83  set(const Item i, const ElmT value)
84  {
85  this->begin_[this->index(i)] = value;
86  return *this;
87  }
88 
95  template <typename U, typename Ret = SourceDataSpan&>
96  constexpr std::enable_if_t<! std::is_const_v<T>, Ret>
98  {
99  std::copy_n(src.begin_, NumItems, this->begin_);
100  return *this;
101  }
102 
103  private:
105  static constexpr auto NumItems =
106  static_cast<std::size_t>(Item::Last_Do_Not_Use);
107 
109  T* begin_{nullptr};
110 
115  explicit SourceDataSpan(T* begin)
116  : begin_{begin}
117  {}
118 
123  constexpr std::size_t index(const Item i) const
124  {
125  const auto ix = static_cast<std::size_t>(i);
126  if (ix >= NumItems) {
127  throw std::invalid_argument {
128  "Index out of bounds"
129  };
130  }
131 
132  return ix;
133  }
134  };
135 
141  explicit PAvgDynamicSourceData(const std::vector<std::size_t>& sourceLocations);
142 
147 
158  [[nodiscard]] SourceDataSpan<Scalar>
159  operator[](const std::size_t source);
160 
171  [[nodiscard]] SourceDataSpan<const Scalar>
172  operator[](const std::size_t source) const;
173 
174 protected:
179  std::vector<Scalar> src_{};
180 
191  [[nodiscard]] SourceDataSpan<Scalar>
192  sourceTerm(const std::size_t ix, std::vector<Scalar>& src);
193 
203  void reconstruct(const std::vector<std::size_t>& sourceLocations);
204 
211  static constexpr std::size_t numSpanItems() noexcept
212  {
214  }
215 
216 private:
219  std::unordered_map<std::size_t, typename std::vector<Scalar>::size_type> ix_{};
220 
229  void buildLocationMapping(const std::vector<std::size_t>& sourceLocations);
230 
236  [[nodiscard]] std::optional<typename std::vector<Scalar>::size_type>
237  index(const std::size_t source) const;
238 
248  [[nodiscard]] virtual typename std::vector<Scalar>::size_type
249  storageIndex(typename std::vector<Scalar>::size_type elemIndex) const
250  {
251  return elemIndex;
252  }
253 };
254 
255 } // namespace Opm
256 
257 #endif // PAVE_DYNAMIC_SOURCE_DATA_HPP
constexpr ElmT operator[](const Item i) const
Read-only access to numerical value of specified item.
Definition: PAvgDynamicSourceData.hpp:69
PAvgDynamicSourceData(const std::vector< std::size_t > &sourceLocations)
Constructor.
Definition: PAvgDynamicSourceData.cpp:35
constexpr std::enable_if_t<! std::is_const_v< T >, Ret > operator=(const SourceDataSpan< U > src)
Assign all items.
Definition: PAvgDynamicSourceData.hpp:97
std::vector< Scalar > src_
Contiguous array of data items for all source locations.
Definition: PAvgDynamicSourceData.hpp:179
SourceDataSpan< Scalar > sourceTerm(const std::size_t ix, std::vector< Scalar > &src)
Form mutable data span into non-default backing store.
Definition: PAvgDynamicSourceData.cpp:71
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
Dynamic source data for block-average pressure calculations.
Definition: PAvgCalculator.hpp:38
Item
Supported items of dynamic data per source location.
Definition: PAvgDynamicSourceData.hpp:51
void reconstruct(const std::vector< std::size_t > &sourceLocations)
Reconstruct Source Data backing storage and internal mapping tables.
Definition: PAvgDynamicSourceData.cpp:79
SourceDataSpan< Scalar > operator[](const std::size_t source)
Acquire read/write span of data items corresponding to a single source location.
Definition: PAvgDynamicSourceData.cpp:43
virtual ~PAvgDynamicSourceData()
Destructor.
Definition: PAvgDynamicSourceData.hpp:146
static constexpr std::size_t numSpanItems() noexcept
Provide number of span items using function syntax.
Definition: PAvgDynamicSourceData.hpp:211
Ad hoc implementation of fixed-width span/view of an underlying contiguous range of elements...
Definition: PAvgDynamicSourceData.hpp:44