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
35{
36public:
42 template <typename T>
44 {
45 private:
46 friend class PAvgDynamicSourceData;
47
48 public:
50 enum class Item
51 {
52 Pressure, //< Dynamic pressure value
53 MixtureDensity, //< Dynamic mixture density
54 PoreVol, //< Dynamic pore volume
55
56 // ----------------------------------------
57
58 Last_Do_Not_Use, //< Simplifies item count
59 };
60
61 using ElmT = std::remove_cv_t<T>;
62
67 [[nodiscard]] constexpr ElmT operator[](const Item i) const
68 {
69 return this->begin_[this->index(i)];
70 }
71
79 template <typename Ret = SourceDataSpan&>
80 constexpr std::enable_if_t<! std::is_const_v<T>, Ret>
81 set(const Item i, const ElmT value)
82 {
83 this->begin_[this->index(i)] = value;
84 return *this;
85 }
86
94 template <typename U, typename Ret = SourceDataSpan&>
95 constexpr std::enable_if_t<! std::is_const_v<T>, Ret>
97 {
98 std::copy_n(src.begin_, NumItems, this->begin_);
99 return *this;
100 }
101
102 private:
104 static constexpr auto NumItems =
105 static_cast<std::size_t>(Item::Last_Do_Not_Use);
106
108 T* begin_{nullptr};
109
114 explicit SourceDataSpan(T* begin)
115 : begin_{begin}
116 {}
117
122 constexpr std::size_t index(const Item i) const
123 {
124 const auto ix = static_cast<std::size_t>(i);
125 if (ix >= NumItems) {
126 throw std::invalid_argument {
127 "Index out of bounds"
128 };
129 }
130
131 return ix;
132 }
133 };
134
140 explicit PAvgDynamicSourceData(const std::vector<std::size_t>& sourceLocations);
141
146
157 [[nodiscard]] SourceDataSpan<double>
158 operator[](const std::size_t source);
159
170 [[nodiscard]] SourceDataSpan<const double>
171 operator[](const std::size_t source) const;
172
173protected:
178 std::vector<double> src_{};
179
190 [[nodiscard]] SourceDataSpan<double>
191 sourceTerm(const std::size_t ix, std::vector<double>& src);
192
202 void reconstruct(const std::vector<std::size_t>& sourceLocations);
203
210 static constexpr std::size_t numSpanItems() noexcept
211 {
213 }
214
215private:
218 std::unordered_map<std::size_t, std::vector<double>::size_type> ix_{};
219
228 void buildLocationMapping(const std::vector<std::size_t>& sourceLocations);
229
235 [[nodiscard]] std::optional<std::vector<double>::size_type>
236 index(const std::size_t source) const;
237
247 [[nodiscard]] virtual std::vector<double>::size_type
248 storageIndex(std::vector<double>::size_type elemIndex) const
249 {
250 return elemIndex;
251 }
252};
253
254} // namespace Opm
255
256#endif // PAVE_DYNAMIC_SOURCE_DATA_HPP
Ad hoc implementation of fixed-width span/view of an underlying contiguous range of elements.
Definition PAvgDynamicSourceData.hpp:44
constexpr ElmT operator[](const Item i) const
Read-only access to numerical value of specified item.
Definition PAvgDynamicSourceData.hpp:67
constexpr std::enable_if_t<! std::is_const_v< T >, Ret > operator=(const SourceDataSpan< U > src)
Assign all items.
Definition PAvgDynamicSourceData.hpp:96
constexpr std::enable_if_t<! std::is_const_v< T >, Ret > set(const Item i, const ElmT value)
Assign specified item.
Definition PAvgDynamicSourceData.hpp:81
Item
Supported items of dynamic data per source location.
Definition PAvgDynamicSourceData.hpp:51
Dynamic source data for block-average pressure calculations.
Definition PAvgDynamicSourceData.hpp:35
virtual ~PAvgDynamicSourceData()
Destructor.
Definition PAvgDynamicSourceData.hpp:145
SourceDataSpan< double > operator[](const std::size_t source)
Acquire read/write span of data items corresponding to a single source location.
Definition PAvgDynamicSourceData.cpp:40
static constexpr std::size_t numSpanItems() noexcept
Provide number of span items using function syntax.
Definition PAvgDynamicSourceData.hpp:210
void reconstruct(const std::vector< std::size_t > &sourceLocations)
Reconstruct Source Data backing storage and internal mapping tables.
Definition PAvgDynamicSourceData.cpp:73
PAvgDynamicSourceData(const std::vector< std::size_t > &sourceLocations)
Constructor.
Definition PAvgDynamicSourceData.cpp:33
SourceDataSpan< double > sourceTerm(const std::size_t ix, std::vector< double > &src)
Form mutable data span into non-default backing store.
Definition PAvgDynamicSourceData.cpp:66
std::vector< double > src_
Contiguous array of data items for all source locations.
Definition PAvgDynamicSourceData.hpp:178
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30