Loading...
Searching...
No Matches
InterRegFlows.hpp
Go to the documentation of this file.
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*
Copyright 2022 Equinor AS
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_INTERREG_FLOWS_MODULE_HPP
#define OPM_INTERREG_FLOWS_MODULE_HPP
#include <opm/output/data/InterRegFlowMap.hpp>
#include <algorithm>
#include <cstddef>
#include <string>
#include <utility>
#include <vector>
namespace Opm {
class InterRegFlowMap;
class InterRegFlowMapSingleFIP
{
public:
struct Cell {
int activeIndex{-1};
int cartesianIndex{-1};
bool isInterior{true};
};
friend class InterRegFlowMap;
explicit InterRegFlowMapSingleFIP(const std::vector<int>& region);
void addConnection(const Cell& source,
const Cell& destination,
const data::InterRegFlowMap::FlowRates& rates);
void compress();
void clear();
const data::InterRegFlowMap& getInterRegFlows() const;
std::size_t getLocalMaxRegionID() const;
bool assignGlobalMaxRegionID(const std::size_t regID);
template <class MessageBufferType>
void write(MessageBufferType& buffer) const
{
// Note: this->region_ is *intentionally* omitted here.
buffer.write(this->maxGlobalRegionID_);
this->iregFlow_.write(buffer);
}
template <class MessageBufferType>
void read(MessageBufferType& buffer)
{
auto otherMaxRegionID = 0 * this->maxGlobalRegionID_;
buffer.read(otherMaxRegionID);
this->maxGlobalRegionID_ = std::max(this->maxGlobalRegionID_, otherMaxRegionID);
this->iregFlow_.read(buffer);
this->isReadFromStream_ = true;
}
private:
std::vector<int> region_{};
std::size_t maxLocalRegionID_{0};
std::size_t maxGlobalRegionID_{0};
data::InterRegFlowMap iregFlow_{};
bool isReadFromStream_{false};
InterRegFlowMapSingleFIP() = default;
};
class InterRegFlowMap
{
public:
struct SingleRegion {
std::string name;
std::reference_wrapper<const std::vector<int>> definition;
};
using Cell = InterRegFlowMapSingleFIP::Cell;
InterRegFlowMap() = default;
static InterRegFlowMap
createMapFromNames(std::vector<std::string> names);
explicit InterRegFlowMap(const std::size_t numCells,
const std::vector<SingleRegion>& regions,
const std::size_t declaredMaxRegID = 0);
InterRegFlowMap(const InterRegFlowMap& rhs) = default;
InterRegFlowMap(InterRegFlowMap&& rhs) noexcept = default;
InterRegFlowMap& operator=(const InterRegFlowMap& rhs) = default;
InterRegFlowMap& operator=(InterRegFlowMap&& rhs) noexcept = default;
void addConnection(const Cell& source,
const Cell& destination,
const data::InterRegFlowMap::FlowRates& rates);
void compress();
void clear();
const std::vector<std::string>& names() const;
std::vector<data::InterRegFlowMap> getInterRegFlows() const;
std::vector<std::size_t> getLocalMaxRegionID() const;
bool assignGlobalMaxRegionID(const std::vector<std::size_t>& regID);
bool readIsConsistent() const;
bool wantInterRegflowSummary() const
{
return !this->regionMaps_.empty();
}
template <class MessageBufferType>
void write(MessageBufferType& buffer) const
{
buffer.write(this->names_.size());
for (const auto& name : this->names_) {
buffer.write(name);
}
for (const auto& map : this->regionMaps_) {
map.write(buffer);
}
}
template <class MessageBufferType>
void read(MessageBufferType& buffer)
{
const auto names = this->readNames(buffer);
if (names == this->names_) {
// Input stream holds the maps in expected order (common
// case). Read the maps and merge with internal values.
for (auto& map : this->regionMaps_) {
map.read(buffer);
}
}
else {
// Input stream does not hold the maps in expected order (or
// different number of maps). Unexpected. Read the values
// from the input stream, but do not merge with internal
// values.
auto map = InterRegFlowMapSingleFIP {
std::vector<int>(this->numCells_, 1)
};
const auto numMaps = names.size();
for (auto n = 0*numMaps; n < numMaps; ++n) {
map.read(buffer);
}
this->readIsConsistent_ = false;
}
}
private:
std::vector<InterRegFlowMapSingleFIP> regionMaps_{};
std::vector<std::string> names_;
std::size_t numCells_{0};
bool readIsConsistent_{true};
template <class MessageBufferType>
std::vector<std::string> readNames(MessageBufferType& buffer) const
{
auto numNames = 0 * this->names_.size();
buffer.read(numNames);
auto names = std::vector<std::string>(numNames);
for (auto name = 0*numNames; name < numNames; ++name) {
buffer.read(names[name]);
}
return names;
}
};
} // namespace Opm
#endif // OPM_INTERREG_FLOWS_MODULE_HPP
Opm::InterRegFlowMapSingleFIP
Form CSR adjacency matrix representation of inter-region flow rate graph provided as a list of connec...
Definition InterRegFlows.hpp:47
Opm::InterRegFlowMapSingleFIP::getLocalMaxRegionID
std::size_t getLocalMaxRegionID() const
Retrieve maximum FIP region ID on local MPI rank.
Definition InterRegFlows.cpp:95
Opm::InterRegFlowMapSingleFIP::read
void read(MessageBufferType &buffer)
Reconstitute internal object representation from MPI message buffer.
Definition InterRegFlows.hpp:145
Opm::InterRegFlowMapSingleFIP::compress
void compress()
Form CSR adjacency matrix representation of input graph from connections established in previous call...
Definition InterRegFlows.cpp:78
Opm::InterRegFlowMapSingleFIP::InterRegFlowMapSingleFIP
InterRegFlowMapSingleFIP(const std::vector< int > &region)
Constructor.
Definition InterRegFlows.cpp:32
Opm::InterRegFlowMapSingleFIP::clear
void clear()
Clear all internal buffers, but preserve allocated capacity.
Definition InterRegFlows.cpp:83
Opm::InterRegFlowMapSingleFIP::getInterRegFlows
const data::InterRegFlowMap & getInterRegFlows() const
Get read-only access to the underlying CSR representation.
Definition InterRegFlows.cpp:90
Opm::InterRegFlowMapSingleFIP::write
void write(MessageBufferType &buffer) const
Serialise internal representation to MPI message buffer.
Definition InterRegFlows.hpp:123
Opm::InterRegFlowMapSingleFIP::assignGlobalMaxRegionID
bool assignGlobalMaxRegionID(const std::size_t regID)
Assign maximum FIP region ID across all MPI ranks.
Definition InterRegFlows.cpp:102
Opm::InterRegFlowMapSingleFIP::addConnection
void addConnection(const Cell &source, const Cell &destination, const data::InterRegFlowMap::FlowRates &rates)
Add flow rate connection between regions.
Definition InterRegFlows.cpp:47
Opm::InterRegFlowMap
Inter-region flow accumulation maps for all region definition arrays.
Definition InterRegFlows.hpp:179
Opm::InterRegFlowMap::read
void read(MessageBufferType &buffer)
Reconstitute internal object representation from MPI message buffer.
Definition InterRegFlows.hpp:323
Opm::InterRegFlowMap::Cell
InterRegFlowMapSingleFIP::Cell Cell
Characteristics of a cell from a simulation grid.
Definition InterRegFlows.hpp:193
Opm::InterRegFlowMap::createMapFromNames
static InterRegFlowMap createMapFromNames(std::vector< std::string > names)
Special purpose constructor for global object being collected on the I/O rank.
Definition InterRegFlows.cpp:122
Opm::InterRegFlowMap::write
void write(MessageBufferType &buffer) const
Serialise internal representation to MPI message buffer.
Definition InterRegFlows.hpp:296
Opm::InterRegFlowMap::compress
void compress()
Form CSR adjacency matrix representation of input graph from connections established in previous call...
Definition InterRegFlows.cpp:165
Opm::InterRegFlowMap::InterRegFlowMap
InterRegFlowMap()=default
Default constructor.
Opm::InterRegFlowMap::names
const std::vector< std::string > & names() const
Names of all applicable region definition arrays.
Definition InterRegFlows.cpp:182
Opm::InterRegFlowMap::getLocalMaxRegionID
std::vector< std::size_t > getLocalMaxRegionID() const
Retrieve maximum FIP region ID on local MPI rank.
Definition InterRegFlows.cpp:203
Opm::InterRegFlowMap::addConnection
void addConnection(const Cell &source, const Cell &destination, const data::InterRegFlowMap::FlowRates &rates)
Add flow rate connection between regions for all region definitions.
Definition InterRegFlows.cpp:156
Opm::InterRegFlowMap::clear
void clear()
Clear all internal buffers, but preserve allocated capacity.
Definition InterRegFlows.cpp:172
Opm::InterRegFlowMap::readIsConsistent
bool readIsConsistent() const
Whether or not previous read() operation succeeded.
Definition InterRegFlows.cpp:238
Opm::InterRegFlowMap::getInterRegFlows
std::vector< data::InterRegFlowMap > getInterRegFlows() const
Get read-only access to the underlying CSR representation.
Definition InterRegFlows.cpp:188
Opm::InterRegFlowMap::assignGlobalMaxRegionID
bool assignGlobalMaxRegionID(const std::vector< std::size_t > &regID)
Assign maximum FIP region ID across all MPI ranks.
Definition InterRegFlows.cpp:219
Opm
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition blackoilbioeffectsmodules.hh:43
Opm::InterRegFlowMapSingleFIP::Cell
Minimal characteristics of a cell from a simulation grid.
Definition InterRegFlows.hpp:50
Opm::InterRegFlowMapSingleFIP::Cell::cartesianIndex
int cartesianIndex
Cell's global cell ID.
Definition InterRegFlows.hpp:55
Opm::InterRegFlowMapSingleFIP::Cell::activeIndex
int activeIndex
Cell's active index on local rank.
Definition InterRegFlows.hpp:52
Opm::InterRegFlowMapSingleFIP::Cell::isInterior
bool isInterior
Whether or not cell is interior to local rank.
Definition InterRegFlows.hpp:58
Opm::InterRegFlowMap::SingleRegion
Minimal representation of a single named region defintion.
Definition InterRegFlows.hpp:184
Opm::InterRegFlowMap::SingleRegion::definition
std::reference_wrapper< const std::vector< int > > definition
Region definition array.
Definition InterRegFlows.hpp:189
Opm::InterRegFlowMap::SingleRegion::name
std::string name
Region definition array name.
Definition InterRegFlows.hpp:186