-
Notifications
You must be signed in to change notification settings - Fork 109
/
InterRegFlow.hpp
361 lines (312 loc) · 11.7 KB
/
InterRegFlow.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
Copyright (c) 2022 Equinor ASA
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_OUTPUT_DATA_INTERREGFLOW_HPP
#define OPM_OUTPUT_DATA_INTERREGFLOW_HPP
#include <algorithm>
#include <cassert>
#include <array>
#include <cmath>
#include <cstddef>
#include <functional>
#include <iterator>
#include <type_traits>
#include <utility>
namespace Opm { namespace data {
/// Intermediary Protocol to Linearise Per-Connection Flow Rates Into Subrange.
///
/// \tparam RandIt Random access iterator type of subrange.
template <typename RandIt>
class InterRegFlow
{
public:
/// Subrange element type.
///
/// Typically \c double or \c float.
using ElmT = std::remove_cv_t<
std::remove_reference_t<
typename std::iterator_traits<RandIt>::value_type
>>;
/// Surface component flow type.
enum class Component : char {
Oil, Gas, Water, Disgas, Vapoil,
// Must be last enumerator
NumComponents,
};
/// Surface flow direction.
enum class Direction : char {
Positive, Negative,
};
/// Connection Flow Rates
class FlowRates
{
public:
/// Constructor.
FlowRates()
{
this->rate_.fill(ElmT{});
}
/// Element access.
///
/// \param[in] i Component ID for specific element.
///
/// \return Read/Write reference to internal flow rate element.
ElmT& operator[](const Component i)
{
return this->rate_[this->index(i)];
}
friend class InterRegFlow;
private:
/// Internal storage.
std::array<ElmT, static_cast<std::size_t>(Component::NumComponents)> rate_{};
/// Convert component ID into linear index.
///
/// \param[in] i Component ID for specific element.
///
/// \return Linear index to internal flow rate element identified by \p i.
std::size_t index(const Component i) const
{
return static_cast<std::size_t>(i);
}
};
/// Constructor.
///
/// \param[in] begin Beginning of subrange viewd by this instance.
/// \param[in] end One-past-the-end of subrange viewed by this instance.
explicit InterRegFlow(RandIt begin, RandIt end)
: elements_(begin, end)
{}
/// Deleted copy constructor.
InterRegFlow(const InterRegFlow&) = delete;
/// Move constructor.
///
/// Invalidates source range.
///
/// \param[in,out] rhs Source range.
InterRegFlow(InterRegFlow&& rhs)
: elements_(rhs.elements_.first, rhs.elements_.second)
{
rhs.elements_.second = rhs.elements_.first; // rhs -> empty
}
/// Assignment operator.
///
/// \param[in] rhs Source range.
///
/// \return \code *this \endcode.
InterRegFlow& operator=(const InterRegFlow& rhs)
{
this->copyIn(rhs);
return *this;
}
/// Move assignment operator.
///
/// Invalidates source range.
///
/// \param[in,out] rhs Source range.
///
/// \return \code *this \endcode.
InterRegFlow& operator=(InterRegFlow&& rhs)
{
if (! this->isValid()) {
this->elements_ = rhs.elements_;
}
else {
this->copyIn(rhs);
}
rhs.elements_.second = rhs.elements_.first; // rhs -> empty
return *this;
}
/// Addition operator
///
/// \tparam OtherRandIt Random access iterator, possibly different
/// from \c RandIt.
///
/// \param[in] rhs Source subrange.
///
/// \return \code *this \endcode.
template <typename OtherRandIt>
std::enable_if_t<
std::is_convertible_v<typename InterRegFlow<OtherRandIt>::ElmT, ElmT>,
InterRegFlow&> operator+=(const InterRegFlow<OtherRandIt>& rhs)
{
std::transform(this->begin(),
this->end(),
rhs .begin(),
this->begin(),
std::plus<>{});
return *this;
}
/// Assignment operator from different, but compatible, subrange.
///
/// Enables assigning into a subrange of \code vector<double>
/// \endcode from a subrange backed by \code array<float,N> \endcode
/// of compatible size.
///
/// \tparam OtherRandIt Random access iterator different from \c RandIt.
///
/// \param[in] rhs Source subrange.
///
/// \return \code *this \endcode.
template <typename OtherRandIt>
std::enable_if_t<
!std::is_same_v<RandIt, OtherRandIt> &&
std::is_convertible_v<typename InterRegFlow<OtherRandIt>::ElmT, ElmT>,
InterRegFlow&> operator=(const InterRegFlow<OtherRandIt>& rhs)
{
this->copyIn(rhs.begin(), rhs.end());
return *this;
}
/// Accumulate connection contribution into subrange.
///
/// \param[in] sign Flow rate sign--e.g., to flip direction if
/// needed in calling context.
///
/// \param[in] q Connection flow rates.
void addFlow(const ElmT sign, const FlowRates& q)
{
assert (this->isValid());
const auto numComp = static_cast<std::size_t>(Component::NumComponents);
for (auto component = 0*numComp; component < numComp; ++component) {
this->add(sign * q.rate_[component], component);
}
}
/// Buffer size (number of elements)
///
/// Storage buffer backing the \c InterRegFlow object must have at
/// least \code InterRegFlow::bufferSize() \endcode contiguous elements.
constexpr static std::size_t bufferSize() noexcept
{
return InterRegFlow::index(Component::NumComponents, Direction::Positive);
}
/// Total accumulated flow rate of particular surface rate component
/// for this region.
///
/// \param[in] component Component ID for specific element.
///
/// \return Flow rate.
constexpr ElmT flow(const Component component) const noexcept
{
// Add components since Positive and Negative are stored as
// signed quantities. In other words flow(x, Negative) <= 0
// while flow(x, Positive) >= 0).
return this->flow(component, Direction::Positive)
+ this->flow(component, Direction::Negative);
}
/// Accumulated flow rate for this region par of particular surface
/// rate component in particular direction.
///
/// Flow from source to destination is \c Positive while from from
/// destination to source is \c Negative. Numerical value in the \c
/// Positive direction is non-negative while the numerical value in
/// the \c Negative direction is non-positive.
///
/// \param[in] component Component ID for specific element.
///
/// \param[in] direction Flow direction.
///
/// \return Component flow rate in specified direction.
constexpr ElmT flow(const Component component,
const Direction direction) const noexcept
{
return *(this->elements_.first + InterRegFlow::index(component, direction));
}
/// Predicate for whether or not this \c InterRegFlow object is
/// backed by an empty range.
constexpr bool empty() const noexcept
{
return this->begin() == this->end();
}
/// Predicate for whether or not this \c InterRegFlow object is
/// backed by a valid range (size \code InterRegFlow::bufferSize()
/// \endcode).
constexpr bool isValid() const noexcept
{
using sz_t = decltype(InterRegFlow::bufferSize());
const auto& [begin, end] = this->elements_;
return static_cast<sz_t>(std::distance(begin, end))
== InterRegFlow::bufferSize();
}
/// Iterator to beginning of subrange.
RandIt begin() const noexcept
{
return this->elements_.first;
}
/// Iterator to one-past-end of subrange.
RandIt end() const noexcept
{
return this->elements_.second;
}
private:
/// Element subrange.
std::pair<RandIt, RandIt> elements_;
/// Convert directional component ID into linear index.
///
/// Overload for already converted component index.
///
/// \param[in] component Component index for specific element.
/// \param[in] direction Flow direction.
///
/// \return Linear index of ordered pair of component ID and flow
/// direction.
constexpr static std::size_t
index(const std::size_t component, const Direction direction)
{
return 2*component + (direction == Direction::Negative);
}
/// Convert directional component ID into linear index.
///
/// \param[in] component Component ID for specific element.
/// \param[in] direction Flow direction.
///
/// \return Linear index of ordered pair of component ID and flow
/// direction.
constexpr static std::size_t
index(const Component component, const Direction direction)
{
return InterRegFlow::index(static_cast<std::size_t>(component), direction);
}
/// Accumulate component flow rate into linearised subrange.
///
/// \param[in] rate Component flow rate.
/// \param[in] component Component index.
void add(const ElmT rate, const std::size_t component)
{
const auto direction = std::signbit(rate)
? Direction::Negative : Direction::Positive;
auto* rateVec = &*this->elements_.first;
rateVec[InterRegFlow::index(component, direction)] += rate;
}
/// Backend for assignment operator
///
/// \param[in] rhs Source subrange.
void copyIn(const InterRegFlow& rhs)
{
if (this->elements_ != rhs.elements_) {
this->copyIn(rhs.elements_.first, rhs.elements_.second);
}
}
/// Backend for assignment operator
///
/// Activated for compatible ranges.
///
/// \param[in] begin Beginning of source subrange.
/// \param[in] end One-past-end of source subrange.
template <typename OtherRandIt>
void copyIn(OtherRandIt begin, OtherRandIt end)
{
std::copy(begin, end, this->elements_.first);
}
};
}} // namespace Opm::data
#endif // OPM_OUTPUT_DATA_INTERREGFLOW_HPP