ColumnExtract.hpp
Go to the documentation of this file.
1 #include <opm/core/grid.h>
2 #include <vector>
3 #include <map>
4 #include <algorithm>
5 
6 namespace Opm {
7 
8  namespace {
9 
12  struct ExtractColumnCompare
13  {
14  ExtractColumnCompare(const UnstructuredGrid& g)
15  : grid(g)
16  {
17  // empty
18  }
19 
20  bool operator()(const int i, const int j)
21  {
22  // Extract k-index
23  int index_i = grid.global_cell ? grid.global_cell[i] : i;
24  int k_i = index_i / grid.cartdims[0] / grid.cartdims[1];
25  int index_j = grid.global_cell ? grid.global_cell[j] : j;
26  int k_j = index_j / grid.cartdims[0] / grid.cartdims[1];
27 
28  return k_i < k_j;
29  }
30 
32  };
33 
34 
37  bool neighbours(const UnstructuredGrid& grid, const int c0, const int c1)
38  {
39  for (int hf = grid.cell_facepos[c0]; hf < grid.cell_facepos[c0 + 1]; ++hf) {
40  const int f = grid.cell_faces[hf];
41  if (grid.face_cells[2*f] == c1 || grid.face_cells[2*f+1] == c1) {
42  return true;
43  }
44  }
45  return false;
46  }
47 
48  } // anonymous namespace
49 
50 
57 inline void extractColumn( const UnstructuredGrid& grid, std::vector<std::vector<int> >& columns )
58 {
59  const int* dims = grid.cartdims;
60 
61  // Keeps track of column_index ---> index of vector
62  std::map<int, int> global_to_local;
63  for (int cell = 0; cell < grid.number_of_cells; ++cell) {
64  // Extract Cartesian coordinates
65  int index = grid.global_cell ? grid.global_cell[cell] : cell; // If null, assume mapping is identity.
66  int i_cart = index % dims[0];
67  int k_cart = index / dims[0] / dims[1];
68  int j_cart = (index - k_cart*dims[0]*dims[1])/ dims[0];
69 
70  int local_index;
71  std::map<int, int>::iterator local_index_iterator = global_to_local.find(i_cart+j_cart*dims[0]);
72  if (local_index_iterator != global_to_local.end()) {
73  local_index = local_index_iterator->second;
74  } else {
75  local_index = columns.size();
76  global_to_local[i_cart+j_cart*dims[0]] = local_index;
77  columns.push_back(std::vector<int>());
78  }
79  columns[local_index].push_back(cell);
80  }
81 
82  int num_cols = columns.size();
83  for (int col = 0; col < num_cols; ++col) {
84  std::sort(columns[col].begin(), columns[col].end(), ExtractColumnCompare(grid));
85  }
86 
87  // At this point, a column may contain multiple disjoint sets of cells.
88  // We must split these columns into connected parts.
89  std::vector< std::vector<int> > new_columns;
90  for (int col = 0; col < num_cols; ++col) {
91  const int colsz = columns[col].size();
92  int first_of_col = 0;
93  for (int k = 1; k < colsz; ++k) {
94  const int c0 = columns[col][k-1];
95  const int c1 = columns[col][k];
96  if (!neighbours(grid, c0, c1)) {
97  // Must split. Move the cells [first_of_col, ... , k-1] to
98  // a new column, known to be connected.
99  new_columns.push_back(std::vector<int>());
100  new_columns.back().assign(columns[col].begin() + first_of_col, columns[col].begin() + k);
101  // The working column now starts with index k.
102  first_of_col = k;
103  }
104  }
105  if (first_of_col != 0) {
106  // The column was split, the working part should be
107  // the entire column. We erase the cells before first_of_col.
108  // (Could be more efficient if we instead chop off end.)
109  columns[col].erase(columns[col].begin(), columns[col].begin() + first_of_col);
110  }
111  }
112 
113  // Must tack on the new columns to complete the set.
114  const int num_cols_all = num_cols + new_columns.size();
115  columns.resize(num_cols_all);
116  for (int col = num_cols; col < num_cols_all; ++col) {
117  columns[col].swap(new_columns[col - num_cols]);
118  }
119 
120 }
121 
122 } // namespace Opm
Definition: grid.h:98
Definition: AnisotropicEikonal.hpp:43
int * face_cells
Definition: grid.h:138
void extractColumn(const UnstructuredGrid &grid, std::vector< std::vector< int > > &columns)
Definition: ColumnExtract.hpp:57
int number_of_cells
Definition: grid.h:109
int cartdims[3]
Definition: grid.h:227
int * cell_faces
Definition: grid.h:146
const UnstructuredGrid & grid
Definition: ColumnExtract.hpp:31
int * global_cell
Definition: grid.h:214
int * cell_facepos
Definition: grid.h:152