22 #ifndef OPM_UTILITY_CSRGRAPHFROMCOORDINATES_HPP 23 #define OPM_UTILITY_CSRGRAPHFROMCOORDINATES_HPP 27 #include <type_traits> 28 #include <unordered_map> 37 namespace Opm {
namespace utility {
54 template <
typename VertexID =
int,
bool TrackCompressedIdx = false,
bool PermitSelfConnections = false>
58 using BaseVertexID = std::remove_cv_t<VertexID>;
60 static_assert(std::is_integral_v<BaseVertexID>,
61 "The VertexID must be an integral type");
68 using Offset =
typename Neighbours::size_type;
71 using Start = std::vector<Offset>;
109 bool expandExistingIdxMap =
false);
123 return this->csr_.startPointers();
130 return this->csr_.columnIndices();
140 template <
typename Ret = const Start&>
143 return this->csr_.compressedIndexMap();
147 template <
class MessageBufferType>
148 void write(MessageBufferType& buffer)
const 150 this->csr_.write(buffer);
154 template <
class MessageBufferType>
155 void read(MessageBufferType& buffer)
161 .add(other.maxRowID(),
163 other.coordinateFormatRowIndices(),
164 other.columnIndices());
191 void add(VertexID v1, VertexID v2);
206 void add(VertexID maxRowIdx,
221 bool isValid()
const;
224 std::optional<BaseVertexID> maxRow()
const;
227 std::optional<BaseVertexID> maxCol()
const;
230 typename Neighbours::size_type numContributions()
const;
239 VertexID findMergedVertexID(VertexID v,
const std::unordered_map<VertexID, VertexID>& vertex_merges)
const;
242 std::unordered_map<VertexID, VertexID>
applyVertexMerges(
const std::unordered_map<VertexID, VertexID>& vertex_merges);
252 std::optional<VertexID> max_i_{};
255 std::optional<VertexID> max_j_{};
280 void merge(
const Connections& conns,
281 const Offset maxNumVertices,
282 const bool expandExistingIdxMap);
288 BaseVertexID maxRowID()
const;
291 BaseVertexID maxColID()
const;
302 Neighbours coordinateFormatRowIndices()
const;
304 template <
typename Ret = const Start&>
307 return this->compressedIdx_;
311 template <
class MessageBufferType>
312 void write(MessageBufferType& buffer)
const 314 this->writeVector(this->ia_, buffer);
315 this->writeVector(this->ja_, buffer);
317 if constexpr (TrackCompressedIdx) {
318 this->writeVector(this->compressedIdx_, buffer);
321 buffer.write(this->numRows_);
322 buffer.write(this->numCols_);
326 template <
class MessageBufferType>
327 void read(MessageBufferType& buffer)
329 this->readVector(buffer, this->ia_);
330 this->readVector(buffer, this->ja_);
332 if constexpr (TrackCompressedIdx) {
333 this->readVector(buffer, this->compressedIdx_);
336 buffer.read(this->numRows_);
337 buffer.read(this->numCols_);
344 struct EmptyPlaceHolder {};
358 std::conditional_t<TrackCompressedIdx, Start, EmptyPlaceHolder> compressedIdx_{};
361 BaseVertexID numRows_{};
365 BaseVertexID numCols_{};
371 template <
typename T,
class A,
class MessageBufferType>
372 void writeVector(
const std::vector<T,A>& vec,
373 MessageBufferType& buffer)
const 375 const auto n = vec.size();
378 for (
const auto& x : vec) {
383 template <
class MessageBufferType,
typename T,
class A>
384 void readVector(MessageBufferType& buffer,
385 std::vector<T,A>& vec)
387 auto n = 0 * vec.size();
392 for (
auto& x : vec) {
431 BaseVertexID maxRowID,
432 BaseVertexID maxColID,
433 bool expandExistingIdxMap);
453 void sortColumnIndicesPerRow();
464 void condenseDuplicates();
483 void preparePushbackRowGrouping(
const int numRows,
497 void groupAndTrackColumnIndicesByRow(
const Neighbours& rowIdx,
526 void condenseAndTrackUniqueColumnsForSingleRow(
typename Neighbours::const_iterator begin,
527 typename Neighbours::const_iterator end);
538 void remapCompressedIndex(
Start&& compressedIdx,
539 std::optional<typename Start::size_type> numOrigNNZ = std::nullopt);
545 Connections uncompressed_;
551 std::unordered_map<VertexID, VertexID> parent_{};
554 std::unordered_map<VertexID, VertexID> vertex_mapping_{};
557 VertexID find(VertexID v);
560 void unionSets(VertexID a, VertexID b);
566 #include <opm/common/utility/CSRGraphFromCoordinates_impl.hpp> 568 #endif // OPM_UTILITY_CSRGRAPHFROMCOORDINATES_HPP Offset numEdges() const
Retrieve number of edges (non-zero matrix elements) in input graph.
Definition: CSRGraphFromCoordinates_impl.hpp:807
VertexID getFinalVertexID(VertexID originalVertexID) const
Get the final vertex ID after all merges and renumbering for a given original vertex ID...
Definition: CSRGraphFromCoordinates_impl.hpp:788
std::vector< BaseVertexID > Neighbours
Representation of neighbouring regions.
Definition: CSRGraphFromCoordinates.hpp:65
const Neighbours & columnIndices() const
Read-only access to compressed structure's column indices, ascendingly sorted per row...
Definition: CSRGraphFromCoordinates.hpp:128
const Start & startPointers() const
Read-only access to compressed structure's start pointers.
Definition: CSRGraphFromCoordinates.hpp:121
Offset numVertices() const
Retrieve number of rows (source entities) in input graph.
Definition: CSRGraphFromCoordinates_impl.hpp:800
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
typename Neighbours::size_type Offset
Offset into neighbour array.
Definition: CSRGraphFromCoordinates.hpp:68
Offset applyVertexMerges()
Apply vertex merges to all vertex groups.
Definition: CSRGraphFromCoordinates_impl.hpp:738
void addVertexGroup(const std::vector< VertexID > &vertices)
Add a group of vertices that should be merged together.
Definition: CSRGraphFromCoordinates_impl.hpp:673
void addConnection(VertexID v1, VertexID v2)
Add flow rate connection between regions.
Definition: CSRGraphFromCoordinates_impl.hpp:650
void compress(Offset maxNumVertices, bool expandExistingIdxMap=false)
Form CSR adjacency matrix representation of input graph from connections established in previous call...
Definition: CSRGraphFromCoordinates_impl.hpp:767
std::enable_if_t< TrackCompressedIdx, Ret > compressedIndexMap() const
Read-only access to mapping from input order vertex pairs to compressed structure's edge index (locat...
Definition: CSRGraphFromCoordinates.hpp:141
Form CSR adjacency matrix representation of unstructured graphs.
Definition: CSRGraphFromCoordinates.hpp:55
void clear()
Clear all internal buffers, but preserve allocated capacity.
Definition: CSRGraphFromCoordinates_impl.hpp:641
std::vector< Offset > Start
CSR start pointers.
Definition: CSRGraphFromCoordinates.hpp:71