opm-grid
GraphOfGrid.hpp
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 /*
4  Copyright 2024 Equinor ASA.
5 
6  This file is part of the Open Porous Media project (OPM).
7 
8  OPM is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  OPM is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with OPM. If not, see <http://www.gnu.org/licenses/>.
20 
21  Consult the COPYING file in the top-level source directory of this
22  module for the precise wording of the license and the list of
23  copyright holders.
24 */
25 
26 #ifndef OPM_GRAPH_OF_GRID_HEADER
27 #define OPM_GRAPH_OF_GRID_HEADER
28 
29 #include <opm/grid/CpGrid.hpp>
30 
31 namespace Opm {
32 
42 template<typename Grid>
44  using WeightType = float;
45  using EdgeList = std::map<int,WeightType>;
46 
47  struct VertexProperties
48  {
49  int nproc = 0; // number of processor
50  WeightType weight = 1; // vertex weight
51  EdgeList edges;
52  };
53 
54 public:
55  explicit GraphOfGrid (const Grid& grid_,
56  const double* transmissibilities=nullptr,
57  const Dune::EdgeWeightMethod edgeWeightMethod=Dune::EdgeWeightMethod::defaultTransEdgeWgt,
58  int level = -1)
59  : grid(grid_)
60  {
61  createGraph(transmissibilities,edgeWeightMethod, level);
62  }
63 
64  const Grid& getGrid() const
65  {
66  return grid;
67  }
68 
70  int size () const
71  {
72  return graph.size();
73  }
74 
75  auto begin() const
76  {
77  return graph.begin();
78  }
79  auto end() const
80  {
81  return graph.end();
82  }
83 
86  auto find(int gID) const
87  {
88  // search the graph first, and then wells
89  auto pgID = graph.find(gID);
90  if (pgID == graph.end())
91  {
92  gID = wellID(gID);
93  if (gID == -1)
94  {
95  return graph.end();
96  }
97  pgID = graph.find(gID);
98  }
99  return pgID;
100  }
101 
107  const VertexProperties& getVertex (int gID) const
108  {
109  auto pgID = find(gID);
110  if (pgID == graph.end())
111  {
112  OPM_THROW(std::logic_error, "GraphOfGrid::getVertex: gID is not in the graph!");
113  }
114  return pgID->second;
115  }
116 
119  // returns -1 if vertex with such global ID is not in the graph (or wells)
120  int numEdges (int gID) const
121  {
122  auto pgID = find(gID);
123  if (pgID == graph.end())
124  {
125  return -1;
126  }
127  else
128  {
129  return pgID->second.edges.size();
130  }
131  }
132 
134  const EdgeList& edgeList(int gID) const
135  {
136  // get iterator to the vertex or the well containing it
137  auto pgID = find(gID);
138  if (pgID==graph.end())
139  {
140  OPM_THROW(std::logic_error, "GraphOfGrid::edgeList: gID is not in the graph!");
141  }
142  return pgID->second.edges;
143  }
144 
151  int contractVertices (int gID1, int gID2);
152 
160  void addWell (const std::set<int>& well, bool checkIntersection=true);
161 
163  const auto& getWells () const
164  {
165  return wells;
166  }
167 
174  void addNeighboringCellsToWells (int layers)
175  {
176  for (int i=0; i<layers; ++i)
177  {
179  }
180  }
181 
182 private:
188  void createGraph (const double* transmissibilities=nullptr,
189  const Dune::EdgeWeightMethod edgeWeightMethod=Dune::EdgeWeightMethod::defaultTransEdgeWgt,
190  int level = -1);
191 
196  int wellID (int gID) const;
197 
206  void mergeWellIndices(const std::set<int>& well);
207 
211  void contractWellAndAdd(const std::set<int>& well);
212 
213  const Grid& grid;
214  std::unordered_map<int, VertexProperties> graph; // <gID, VertexProperties>
215  std::list<std::set<int>> wells;
216 };
217 
218 } // namespace Opm
219 
220 #endif // OPM_GRAPH_OF_GRID_HEADER
int size() const
Number of graph vertices.
Definition: GraphOfGrid.hpp:70
Holds the implementation of the CpGrid as a pimple.
Definition: CellQuadrature.cpp:71
int contractVertices(int gID1, int gID2)
Contract two vertices.
Definition: GraphOfGrid.cpp:201
const EdgeList & edgeList(int gID) const
List of neighbors for given vertex.
Definition: GraphOfGrid.hpp:134
int numEdges(int gID) const
Number of vertices for given vertex.
Definition: GraphOfGrid.hpp:120
const auto & getWells() const
Return the list of wells.
Definition: GraphOfGrid.hpp:163
A class storing a graph representation of the grid.
Definition: GraphOfGrid.hpp:43
auto find(int gID) const
Get iterator to the vertex with this global ID or ID of the well containing it.
Definition: GraphOfGrid.hpp:86
void addNeighboringCellsToWells()
Contract a layer of verices around each well into it.
Definition: GraphOfGrid.cpp:336
void addWell(const std::set< int > &well, bool checkIntersection=true)
Register the well to the list of wells.
Definition: GraphOfGrid.cpp:322
const VertexProperties & getVertex(int gID) const
Return properties of vertex of given ID.
Definition: GraphOfGrid.hpp:107
EdgeWeightMethod
enum for choosing Methods for weighting graph-edges correspoding to cell interfaces in Zoltan&#39;s or Me...
Definition: GridEnums.hpp:34