opm-common
NNC.hpp
1 /*
2  Copyright 2015 IRIS
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef OPM_PARSER_NNC_HPP
21 #define OPM_PARSER_NNC_HPP
22 
23 #include <cstddef>
24 #include <optional>
25 #include <tuple>
26 #include <map>
27 #include <vector>
28 
29 #include <opm/common/OpmLog/KeywordLocation.hpp>
30 
31 namespace Opm
32 {
33 
34 class GridDims;
35 
36 struct NNCdata {
37  NNCdata(size_t c1, size_t c2, double t)
38  : cell1(c1), cell2(c2), trans(t)
39  {}
40  NNCdata() = default;
41 
42  bool operator==(const NNCdata& data) const
43  {
44  return cell1 == data.cell1 &&
45  cell2 == data.cell2 &&
46  trans == data.trans;
47  }
48 
49  template<class Serializer>
50  void serializeOp(Serializer& serializer)
51  {
52  serializer(cell1);
53  serializer(cell2);
54  serializer(trans);
55  }
56 
57  // Observe that the operator< is only for cell ordering and does not consider the
58  // trans member
59  bool operator<(const NNCdata& other) const
60  {
61  return std::tie(this->cell1, this->cell2) < std::tie(other.cell1, other.cell2);
62  }
63 
64  size_t cell1{};
65  size_t cell2{};
66  double trans{};
67 };
68 
69 
70 
71 class Deck;
72 class EclipseGrid;
73 
74 /*
75  This class is an internalization of the NNC and EDITNNC keywords. Because the
76  opm-common codebase does not itself manage the simulation grid the purpose of
77  the NNC class is mainly to hold on to the NNC/EDITNNC input and pass it on to
78  the grid construction proper.
79 
80  The EDITNNC keywords can operate on two different types of NNCs.
81 
82  1. NNCs which have been explicitly entered using the NNC keyword.
83  2. NNCs which are inderectly inferred from the grid - e.g. due to faults.
84 
85  When processing the EDITNNC keyword the class will search through the NNCs
86  configured explicitly with the NNC keyword and apply the edit transformation
87  on those NNCs, EDITNNCs which affect NNCs which are not configured explicitly
88  are stored for later use by the simulator.
89 
90  The class guarantees the following ordering:
91 
92  1. For all NNC / EDITNNC records we will have cell1 <= cell2
93  2. The vectors NNC::input() and NNC::edit() will be ordered in ascending
94  order.
95 
96  While constructing from a deck NNCs connected to inactive cells will be
97  silently ignored. Do observe though that the addNNC() function does not check
98  the arguments and alas there is no guarantee that only active cells are
99  involved.
100 */
101 
102 class NNC
103 {
104 public:
105  NNC() = default;
106  virtual ~NNC() = default;
108  NNC(const EclipseGrid& grid, const Deck& deck);
109 
110  static NNC serializationTestObject();
111 
112  virtual bool addNNC(const size_t cell1, const size_t cell2, const double trans);
113 
115  virtual void merge(const std::vector<NNCdata>& nncs);
117  const std::vector<NNCdata>& input() const { return m_input; }
119  const std::vector<NNCdata>& edit() const { return m_edit; }
121  const std::vector<NNCdata>& editr() const { return m_editr; }
122  KeywordLocation input_location(const NNCdata& nnc) const;
123  KeywordLocation edit_location(const NNCdata& nnc) const;
124  KeywordLocation editr_location(const NNCdata& nnc) const;
125 
126 
127  bool operator==(const NNC& data) const;
128 
129  template<class Serializer>
130  void serializeOp(Serializer& serializer)
131  {
132  serializer(m_input);
133  serializer(m_edit);
134  serializer(m_editr);
135  serializer(m_nnc_location);
136  serializer(m_edit_location);
137  serializer(m_editr_location);
138  }
139 
140 private:
141 
142  void load_input(const EclipseGrid& grid, const Deck& deck);
143  void load_edit(const EclipseGrid& grid, const Deck& deck);
144  void load_editr(const EclipseGrid& grid, const Deck& deck);
145  void add_edit(const NNCdata& edit_node);
146 
148  std::vector<NNCdata> m_input;
150  std::vector<NNCdata> m_edit;
152  std::vector<NNCdata> m_editr;
153  std::optional<KeywordLocation> m_nnc_location;
154  std::optional<KeywordLocation> m_edit_location;
155  std::optional<KeywordLocation> m_editr_location;
156 
157  friend class NNCDiffGrid;
158 };
159 
160 
162 {
163  public:
164  NNCDataContainer() = default;
165  virtual ~NNCDataContainer() = default;
166 
167  virtual bool addNNC(const size_t cell1, const size_t cell2, const double trans);
168  bool addNNC(const NNCdata nnc_data);
169 
170  const std::vector<NNCdata>& input() const { return nnc_container; }
171 
172  bool operator==(const NNCDataContainer& other) const;
173 
174  protected:
175  std::vector<NNCdata> nnc_container;
176 };
177 
178 
179 
180 /*
181  NNCDiffGrid is derived class of NNC. NNC Class as it does not preserve the
182  order of NNC, therefore not suitable for storing NNC data from different grids.
183 */
185 {
186 public:
187  NNCDataContainerDiffGrid() = default;
188  ~NNCDataContainerDiffGrid() override = default;
189 
190  bool addNNC(const size_t cell1, const size_t cell2,
191  const double trans) override;
192 
193  void swap_adj(std::size_t grid1, std::size_t grid2);
194 
195  bool operator==(const NNCDataContainerDiffGrid& other) const;
196 };
197 
198 
200 {
201 public:
202  NNCCollection() = default;
203  explicit NNCCollection(NNCDataContainer nnc_global);
204 
205  // ---- insertion --------------------------------------------------------
206 
208  void addNNC(std::size_t grid1, std::size_t grid2, NNCDataContainerDiffGrid nnc);
209 
211  void addNNC(std::size_t grid, NNCDataContainer nnc);
212 
214  void addNNC(NNCDataContainer nnc);
215 
216  // ---- cross-grid access ------------------------------------------------
217 
218  const NNCDataContainerDiffGrid& getNNC(std::size_t grid1, std::size_t grid2) const;
219  NNCDataContainerDiffGrid& getNNC(std::size_t grid1, std::size_t grid2);
220 
221  bool hasCrossGridNNC(std::size_t grid1, std::size_t grid2) const;
222  bool empty() const { return m_sameGridNNCs.empty() && m_diffGridNNCs.empty(); }
223  // ---- same-grid access -------------------------------------------------
224 
225  const NNCDataContainer& getNNC(std::size_t grid) const;
226  NNCDataContainer& getNNC(std::size_t grid);
227 
228  bool hasSameGridNNC(std::size_t grid) const;
229 
230  // ---- global (grid 0) access -------------------------------------------
231 
232  const NNCDataContainer& getGlobalNNC() const;
234 
235  bool hasGlobalNNC() const { return hasSameGridNNC(0); };
236 
239  bool hasNNCForGrid(std::size_t grid_index) const
240  {
241  if (hasSameGridNNC(grid_index))
242  return true;
243  for (const auto& entry : m_diffGridNNCs) {
244  if (entry.first.first == grid_index || entry.first.second == grid_index)
245  return true;
246  }
247  return false;
248  }
249 
250 
252  const std::map<std::size_t, NNCDataContainer>& same_grid_nnc() const
253  {
254  return m_sameGridNNCs;
255  }
256 
258  const std::map<std::pair<std::size_t,std::size_t>, NNCDataContainerDiffGrid>& diff_grid_nnc() const
259  {
260  return m_diffGridNNCs;
261  }
262 
263  // ---- factory ----------------------------------------------------------
264 
284  const std::vector<std::vector<NNCdata>>& outputNnc,
285  const std::vector<std::vector<NNCdata>>& outputNncGlobalLocal,
286  const std::vector<std::vector<std::vector<NNCdata>>>& outputAmalgamatedNnc);
287 
288  bool operator==(const NNCCollection& other) const
289  {
290  return m_sameGridNNCs == other.m_sameGridNNCs &&
291  m_diffGridNNCs == other.m_diffGridNNCs;
292  }
293 
294 private:
295 
297  std::map<std::size_t, NNCDataContainer> m_sameGridNNCs;
298 
300  std::map<std::pair<std::size_t,std::size_t>, NNCDataContainerDiffGrid> m_diffGridNNCs;
301 };
302 
303 } // namespace Opm
304 
305 
306 #endif // OPM_PARSER_NNC_HPP
const NNCDataContainerDiffGrid & getNNC(std::size_t grid1, std::size_t grid2) const
Returns a const reference to the cross-grid NNC for the (grid1, grid2) pair.
Definition: NNC.cpp:462
const std::map< std::pair< std::size_t, std::size_t >, NNCDataContainerDiffGrid > & diff_grid_nnc() const
Returns a view of all cross-grid NNCs keyed by normalised (g1,g2) pairs.
Definition: NNC.hpp:258
void swap_adj(std::size_t grid1, std::size_t grid2)
Swaps cell1 and cell2 in every entry when grid1 > grid2, so that the container is always stored in no...
Definition: NNC.cpp:414
Definition: KeywordLocation.hpp:27
static NNCCollection fromLGROutputContainers(const std::vector< std::vector< NNCdata >> &outputNnc, const std::vector< std::vector< NNCdata >> &outputNncGlobalLocal, const std::vector< std::vector< std::vector< NNCdata >>> &outputAmalgamatedNnc)
Build an NNCCollection from the three output containers produced by EclGenericWriter::exportNncStruct...
Definition: NNC.cpp:564
About cell information and dimension: The actual grid information is held in a pointer to an ERT ecl_...
Definition: EclipseGrid.hpp:62
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
bool addNNC(const size_t cell1, const size_t cell2, const double trans) override
Inserts a cross-grid NNC entry without enforcing any cell ordering, since cell1 and cell2 belong to d...
Definition: NNC.cpp:406
Definition: NNC.hpp:199
bool hasNNCForGrid(std::size_t grid_index) const
Returns true if the given grid has any NNC involvement: same-grid NNCs, or cross-grid NNCs with any o...
Definition: NNC.hpp:239
Definition: NNC.hpp:184
const std::vector< NNCdata > & edit() const
Get the information from EDITNNC keyword.
Definition: NNC.hpp:119
const std::vector< NNCdata > & input() const
Get the combined information from NNC.
Definition: NNC.hpp:117
Definition: NNC.hpp:161
virtual bool addNNC(const size_t cell1, const size_t cell2, const double trans)
Inserts an NNC entry (cell1, cell2, trans) into the container, enforcing cell1 <= cell2...
Definition: NNC.cpp:379
Definition: NNC.hpp:102
const std::map< std::size_t, NNCDataContainer > & same_grid_nnc() const
Returns a view of all same-grid NNCs as (grid_index, NNC) pairs.
Definition: NNC.hpp:252
void addNNC(std::size_t grid1, std::size_t grid2, NNCDataContainerDiffGrid nnc)
Add a cross-grid NNC between grid1 and grid2.
Definition: NNC.cpp:443
const NNCDataContainer & getGlobalNNC() const
Returns a const reference to the global (grid 0) same-grid NNC.
Definition: NNC.cpp:542
Definition: Deck.hpp:46
const std::vector< NNCdata > & editr() const
Get the information from EDITNNCR keyword.
Definition: NNC.hpp:121
Class for (de-)serializing.
Definition: Serializer.hpp:94
virtual void merge(const std::vector< NNCdata > &nncs)
Merge additional NNCs into sorted NNCs.
Definition: NNC.cpp:289
Definition: NNC.hpp:36