This commit adds new helper classes,
Opm::RestartIO::Helpers::CharArrayNullTerm<NChar>
Opm::RestartIO::Helpers::WindowedArray<T>
Opm::RestartIO::Helpers::WindowedMatrix<T>
the first of which represents a nul-terminated character string of
'NChar' user data (total array size is NChar + 1). Mainly intended
for representing Fortran-compatible characters arrays--especially
for ZWEL and similar restart vectors (e.g., ZGRP).
The second two simplify working with subsets of large linear arrays,
particularly those that represent N items per well, group, or
segment. The WindowedArray is a linear array of windows (N items
per window) packed into a std::vector<T> and the WindowedMatrix
additionally imposes a Row-by-Colum structure which is useful for
the case of N windows (each of size K) for each of M entities (e.g.,
K items for each of N connections for each of M wells).
178 lines
4.6 KiB
C++
178 lines
4.6 KiB
C++
/*
|
|
Copyright (c) 2018 Statoil 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_WINDOWED_VECTOR_HPP
|
|
#define OPM_WINDOWED_VECTOR_HPP
|
|
|
|
#include <cassert>
|
|
#include <iterator>
|
|
#include <type_traits>
|
|
#include <vector>
|
|
|
|
#include <boost/range/iterator_range.hpp>
|
|
|
|
namespace Opm { namespace RestartIO { namespace Helpers {
|
|
|
|
template <typename T>
|
|
class WindowedArray
|
|
{
|
|
private:
|
|
using Vector = std::vector<T>;
|
|
|
|
public:
|
|
using WriteWindow = boost::iterator_range<
|
|
typename Vector::iterator>;
|
|
|
|
using ReadWindow = boost::iterator_range<
|
|
typename Vector::const_iterator>;
|
|
|
|
using Idx = typename Vector::size_type;
|
|
|
|
struct NumWindows { Idx value; };
|
|
struct WindowSize { Idx value; };
|
|
|
|
explicit WindowedArray(const NumWindows n, const WindowSize sz)
|
|
: x_ (n.value * sz.value)
|
|
, windowSize_(sz.value)
|
|
{}
|
|
|
|
Idx numWindows() const
|
|
{
|
|
return this->x_.size() / this->windowSize_;
|
|
}
|
|
|
|
Idx windowSize() const
|
|
{
|
|
return this->windowSize_;
|
|
}
|
|
|
|
WriteWindow operator[](const Idx window)
|
|
{
|
|
assert ((window < this->numWindows()) &&
|
|
"Window ID Out of Bounds");
|
|
|
|
auto b = std::begin(this->x_) + window*this->windowSize_;
|
|
auto e = b + this->windowSize_;
|
|
|
|
return { b, e };
|
|
}
|
|
|
|
ReadWindow operator[](const Idx window) const
|
|
{
|
|
assert ((window < this->numWindows()) &&
|
|
"Window ID Out of Bounds");
|
|
|
|
auto b = std::begin(this->x_) + window*this->windowSize_;
|
|
auto e = b + this->windowSize_;
|
|
|
|
return { b, e };
|
|
}
|
|
|
|
const Vector& data() const
|
|
{
|
|
return this->x_;
|
|
}
|
|
|
|
Vector getDataDestructively()
|
|
{
|
|
return std::move(this->x_);
|
|
}
|
|
|
|
private:
|
|
std::vector<T> x_;
|
|
|
|
Idx windowSize_;
|
|
};
|
|
|
|
template <typename T>
|
|
class WindowedMatrix
|
|
{
|
|
private:
|
|
using NumWindows = typename WindowedArray<T>::NumWindows;
|
|
|
|
public:
|
|
using WriteWindow = typename WindowedArray<T>::WriteWindow;
|
|
using ReadWindow = typename WindowedArray<T>::ReadWindow;
|
|
using WindowSize = typename WindowedArray<T>::WindowSize;
|
|
using Idx = typename WindowedArray<T>::Idx;
|
|
|
|
struct NumRows { Idx value; };
|
|
struct NumCols { Idx value; };
|
|
|
|
explicit WindowedMatrix(const NumRows& nRows,
|
|
const NumCols& nCols,
|
|
const WindowSize& sz)
|
|
: data_ (NumWindows{ nRows.value * nCols.value }, sz)
|
|
, numCols_(nCols.value)
|
|
{}
|
|
|
|
Idx numCols() const
|
|
{
|
|
return this->numCols_;
|
|
}
|
|
|
|
Idx numRows() const
|
|
{
|
|
return this->data_.numWindows() / this->numCols();
|
|
}
|
|
|
|
Idx windowSize() const
|
|
{
|
|
return this->data_.windowSize();
|
|
}
|
|
|
|
WriteWindow operator()(const Idx row, const Idx col)
|
|
{
|
|
return this->data_[ this->i(row, col) ];
|
|
}
|
|
|
|
ReadWindow operator()(const Idx row, const Idx col) const
|
|
{
|
|
return this->data_[ this->i(row, col) ];
|
|
}
|
|
|
|
auto data() const
|
|
-> decltype(std::declval<const WindowedArray<T>>().data())
|
|
{
|
|
return this->data_.data();
|
|
}
|
|
|
|
auto getDataDestructively()
|
|
-> decltype(std::declval<WindowedArray<T>>()
|
|
.getDataDestructively())
|
|
{
|
|
return this->data_.getDataDestructively();
|
|
}
|
|
|
|
private:
|
|
WindowedArray<T> data_;
|
|
|
|
Idx numCols_;
|
|
|
|
/// Row major (C) order.
|
|
Idx i(const Idx row, const Idx col) const
|
|
{
|
|
return row*this->numCols() + col;
|
|
}
|
|
};
|
|
|
|
}}} // Opm::RestartIO::Helpers
|
|
|
|
#endif // OPM_WINDOW_VECTOR_HPP
|