My Project
Loading...
Searching...
No Matches
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
31namespace Opm {
32
34template <typename Scalar>
36{
37public:
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
96 template <typename U, typename Ret = SourceDataSpan&>
97 constexpr std::enable_if_t<! std::is_const_v<T>, Ret>
99 {
100 std::copy_n(src.begin_, NumItems, this->begin_);
101 return *this;
102 }
103
104 private:
106 static constexpr auto NumItems =
107 static_cast<std::size_t>(Item::Last_Do_Not_Use);
108
110 T* begin_{nullptr};
111
116 explicit SourceDataSpan(T* begin)
117 : begin_{begin}
118 {}
119
124 constexpr std::size_t index(const Item i) const
125 {
126 const auto ix = static_cast<std::size_t>(i);
127 if (ix >= NumItems) {
128 throw std::invalid_argument {
129 "Index out of bounds"
130 };
131 }
132
133 return ix;
134 }
135 };
136
142 explicit PAvgDynamicSourceData(const std::vector<std::size_t>& sourceLocations);
143
148
159 [[nodiscard]] SourceDataSpan<Scalar>
160 operator[](const std::size_t source);
161
172 [[nodiscard]] SourceDataSpan<const Scalar>
173 operator[](const std::size_t source) const;
174
175protected:
180 std::vector<Scalar> src_{};
181
192 [[nodiscard]] SourceDataSpan<Scalar>
193 sourceTerm(const std::size_t ix, std::vector<Scalar>& src);
194
204 void reconstruct(const std::vector<std::size_t>& sourceLocations);
205
212 static constexpr std::size_t numSpanItems() noexcept
213 {
215 }
216
217private:
220 std::unordered_map<std::size_t, typename std::vector<Scalar>::size_type> ix_{};
221
230 void buildLocationMapping(const std::vector<std::size_t>& sourceLocations);
231
237 [[nodiscard]] std::optional<typename std::vector<Scalar>::size_type>
238 index(const std::size_t source) const;
239
249 [[nodiscard]] virtual typename std::vector<Scalar>::size_type
250 storageIndex(typename std::vector<Scalar>::size_type elemIndex) const
251 {
252 return elemIndex;
253 }
254};
255
256} // namespace Opm
257
258#endif // PAVE_DYNAMIC_SOURCE_DATA_HPP
Ad hoc implementation of fixed-width span/view of an underlying contiguous range of elements.
Definition PAvgDynamicSourceData.hpp:45
constexpr ElmT operator[](const Item i) const
Read-only access to numerical value of specified item.
Definition PAvgDynamicSourceData.hpp:69
constexpr std::enable_if_t<! std::is_const_v< T >, Ret > set(const Item i, const ElmT value)
Assign specified item.
Definition PAvgDynamicSourceData.hpp:83
constexpr std::enable_if_t<! std::is_const_v< T >, Ret > operator=(const SourceDataSpan< U > src)
Assign all items.
Definition PAvgDynamicSourceData.hpp:98
Item
Supported items of dynamic data per source location.
Definition PAvgDynamicSourceData.hpp:52
Dynamic source data for block-average pressure calculations.
Definition PAvgDynamicSourceData.hpp:36
PAvgDynamicSourceData(const std::vector< std::size_t > &sourceLocations)
Constructor.
Definition PAvgDynamicSourceData.cpp:35
virtual ~PAvgDynamicSourceData()
Destructor.
Definition PAvgDynamicSourceData.hpp:147
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
static constexpr std::size_t numSpanItems() noexcept
Provide number of span items using function syntax.
Definition PAvgDynamicSourceData.hpp:212
void reconstruct(const std::vector< std::size_t > &sourceLocations)
Reconstruct Source Data backing storage and internal mapping tables.
Definition PAvgDynamicSourceData.cpp:79
std::vector< Scalar > src_
Contiguous array of data items for all source locations.
Definition PAvgDynamicSourceData.hpp:180
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