Loading...
Searching...
No Matches
PAvgDynamicSourceData.hpp
/*
Copyright 2023 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 PAVE_DYNAMIC_SOURCE_DATA_HPP
#define PAVE_DYNAMIC_SOURCE_DATA_HPP
#include <algorithm>
#include <cstddef>
#include <optional>
#include <stdexcept>
#include <type_traits>
#include <unordered_map>
#include <vector>
namespace Opm {
template <typename Scalar>
class PAvgDynamicSourceData
{
public:
template<typename T>
class SourceDataSpan
{
private:
friend class PAvgDynamicSourceData<Scalar>;
public:
enum class Item
{
Pressure, //< Dynamic pressure value
MixtureDensity, //< Dynamic mixture density
PoreVol, //< Dynamic pore volume
// ----------------------------------------
Last_Do_Not_Use, //< Simplifies item count
};
using ElmT = std::remove_cv_t<T>;
[[nodiscard]] constexpr ElmT operator[](const Item i) const
{
return this->begin_[this->index(i)];
}
template <typename Ret = SourceDataSpan&>
constexpr std::enable_if_t<! std::is_const_v<T>, Ret>
set(const Item i, const ElmT value)
{
this->begin_[this->index(i)] = value;
return *this;
}
template <typename U, typename Ret = SourceDataSpan&>
constexpr std::enable_if_t<! std::is_const_v<T>, Ret>
operator=(const SourceDataSpan<U> src)
{
std::copy_n(src.begin_, NumItems, this->begin_);
return *this;
}
private:
static constexpr auto NumItems =
static_cast<std::size_t>(Item::Last_Do_Not_Use);
T* begin_{nullptr};
explicit SourceDataSpan(T* begin)
: begin_{begin}
{}
constexpr std::size_t index(const Item i) const
{
const auto ix = static_cast<std::size_t>(i);
if (ix >= NumItems) {
throw std::invalid_argument {
"Index out of bounds"
};
}
return ix;
}
};
explicit PAvgDynamicSourceData(const std::vector<std::size_t>& sourceLocations);
virtual ~PAvgDynamicSourceData() {}
[[nodiscard]] SourceDataSpan<Scalar>
operator[](const std::size_t source);
[[nodiscard]] SourceDataSpan<const Scalar>
operator[](const std::size_t source) const;
protected:
std::vector<Scalar> src_{};
[[nodiscard]] SourceDataSpan<Scalar>
sourceTerm(const std::size_t ix, std::vector<Scalar>& src);
void reconstruct(const std::vector<std::size_t>& sourceLocations);
static constexpr std::size_t numSpanItems() noexcept
{
return SourceDataSpan<const Scalar>::NumItems;
}
private:
std::unordered_map<std::size_t, typename std::vector<Scalar>::size_type> ix_{};
void buildLocationMapping(const std::vector<std::size_t>& sourceLocations);
[[nodiscard]] std::optional<typename std::vector<Scalar>::size_type>
index(const std::size_t source) const;
[[nodiscard]] virtual typename std::vector<Scalar>::size_type
storageIndex(typename std::vector<Scalar>::size_type elemIndex) const
{
return elemIndex;
}
};
} // namespace Opm
#endif // PAVE_DYNAMIC_SOURCE_DATA_HPP
Opm::PAvgDynamicSourceData::SourceDataSpan
Ad hoc implementation of fixed-width span/view of an underlying contiguous range of elements.
Definition PAvgDynamicSourceData.hpp:45
Opm::PAvgDynamicSourceData::SourceDataSpan::operator[]
constexpr ElmT operator[](const Item i) const
Read-only access to numerical value of specified item.
Definition PAvgDynamicSourceData.hpp:67
Opm::PAvgDynamicSourceData::SourceDataSpan::set
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
Opm::PAvgDynamicSourceData::SourceDataSpan::operator=
constexpr std::enable_if_t<! std::is_const_v< T >, Ret > operator=(const SourceDataSpan< U > src)
Assign all items.
Definition PAvgDynamicSourceData.hpp:96
Opm::PAvgDynamicSourceData::SourceDataSpan::Item
Item
Supported items of dynamic data per source location.
Definition PAvgDynamicSourceData.hpp:51
Opm::PAvgDynamicSourceData
Dynamic source data for block-average pressure calculations.
Definition PAvgDynamicSourceData.hpp:36
Opm::PAvgDynamicSourceData::PAvgDynamicSourceData
PAvgDynamicSourceData(const std::vector< std::size_t > &sourceLocations)
Constructor.
Definition PAvgDynamicSourceData.cpp:35
Opm::PAvgDynamicSourceData::~PAvgDynamicSourceData
virtual ~PAvgDynamicSourceData()
Destructor.
Definition PAvgDynamicSourceData.hpp:145
Opm::PAvgDynamicSourceData::operator[]
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
Opm::PAvgDynamicSourceData::numSpanItems
static constexpr std::size_t numSpanItems() noexcept
Provide number of span items using function syntax.
Definition PAvgDynamicSourceData.hpp:210
Opm::PAvgDynamicSourceData::reconstruct
void reconstruct(const std::vector< std::size_t > &sourceLocations)
Reconstruct Source Data backing storage and internal mapping tables.
Definition PAvgDynamicSourceData.cpp:79
Opm::PAvgDynamicSourceData::src_
std::vector< Scalar > src_
Contiguous array of data items for all source locations.
Definition PAvgDynamicSourceData.hpp:178
Opm::PAvgDynamicSourceData::sourceTerm
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
Opm
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30