forked from OPM/opm-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridHelpers.cpp
More file actions
356 lines (307 loc) · 11.9 KB
/
GridHelpers.cpp
File metadata and controls
356 lines (307 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
Copyright 2014, 2015 Dr. Markus Blatt - HPC-Simulation-Software & Services.
Copyright 2014 Statoil AS
Copyright 2015 NTNU
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/>.
*/
#include "config.h"
#include <opm/grid/GridHelpers.hpp>
#include <opm/grid/common/Volumes.hpp>
#if HAVE_OPM_COMMON
#include <opm/common/utility/ActiveGridCells.hpp>
#include <opm/input/eclipse/EclipseState/Grid/EclipseGrid.hpp>
#endif
namespace Opm
{
namespace UgGridHelpers
{
int numCells(const UnstructuredGrid& grid)
{
return grid.number_of_cells;
}
int numFaces(const UnstructuredGrid& grid)
{
return grid.number_of_faces;
}
int dimensions(const UnstructuredGrid& grid)
{
return grid.dimensions;
}
int numCellFaces(const UnstructuredGrid& grid)
{
return grid.cell_facepos[grid.number_of_cells];
}
const int* globalCell(const UnstructuredGrid& grid)
{
return grid.global_cell;
}
const int* cartDims(const UnstructuredGrid& grid)
{
return grid.cartdims;
}
#if HAVE_OPM_COMMON
std::vector<int> createACTNUM(const UnstructuredGrid& grid) {
const int* dims = cartDims(grid);
return ActiveGridCells(dims[0], dims[1], dims[2], globalCell(grid), numCells(grid)).actNum();
}
#endif
const double* beginCellCentroids(const UnstructuredGrid& grid)
{
return grid.cell_centroids;
}
double cellCenterDepth(const UnstructuredGrid& grid, int cell_index)
{
// This method is an alternative to the method cellCentroidCoordinate(...) below.
// The cell center depth is computed as a raw average of cell corner depths.
// For cornerpoint grids, this is likely to give slightly different depths that seem
// to agree with eclipse.
assert(grid.dimensions == 3);
const int nd = 3; // Assuming 3-dimensional grid ...
const int nv = 8; // Assuming 2*4 vertices ...
double zz = 0.0;
// Traverse the bottom and top cell-face
for (unsigned i = grid.cell_facepos[cell_index+1] - 2; i < grid.cell_facepos[cell_index+1]; ++i) {
// Traverse the vertices associated with each face
assert(grid.face_nodepos[grid.cell_faces[i]+1] - grid.face_nodepos[grid.cell_faces[i]] == nv/2);
for (unsigned j = grid.face_nodepos[grid.cell_faces[i]]; j < grid.face_nodepos[grid.cell_faces[i]+1]; ++j) {
zz += (grid.node_coordinates+nd*(grid.face_nodes[j]))[nd-1];
}
}
return zz/nv;
}
Dune::FieldVector<double,3> faceCenterEcl(const UnstructuredGrid& grid, int cell_index, int face_tag)
{
// This method is an alternative to the method faceCentroid(...) below.
// The face center is computed as a raw average of cell corners.
// For faulted cells this gives different results then average of face nodes
// that seems to agree more with eclipse.
// This assumes that the top and bottom face nodes are ordered
// 0--1
// | |
// 3--2
assert(grid.dimensions == 3);
const int nd = 3; // Assuming 3-dimensional grid ...
const int nv = 4; // Assuming 4 vertices ...
Dune::FieldVector<double,3> center(0.0);
//Vector center(0.0);
// Traverse the bottom and top cell-face
for (unsigned i = grid.cell_facepos[cell_index+1] - 2; i < grid.cell_facepos[cell_index+1]; ++i) {
// Traverse the vertices associated with each face
assert(grid.face_nodepos[grid.cell_faces[i]+1] - grid.face_nodepos[grid.cell_faces[i]] == nv);
int start = grid.face_nodepos[grid.cell_faces[i]];
// pick the right nodes. See order assumption above
switch(face_tag) {
case 0: {
for (int indx = 0; indx < nd; ++indx) {
center[indx] += (grid.node_coordinates+nd*(grid.face_nodes[start]))[indx];
center[indx] += (grid.node_coordinates+nd*(grid.face_nodes[start+3]))[indx];
}
}
break;
case 1: {
for (int indx = 0; indx < nd; ++indx) {
center[indx] += (grid.node_coordinates+nd*(grid.face_nodes[start+1]))[indx];
center[indx] += (grid.node_coordinates+nd*(grid.face_nodes[start+2]))[indx];
}
}
break;
case 2: {
for (int indx = 0; indx < nd; ++indx) {
center[indx] += (grid.node_coordinates+nd*(grid.face_nodes[start]))[indx];
center[indx] += (grid.node_coordinates+nd*(grid.face_nodes[start+1]))[indx];
}
}
break;
case 3: {
for (int indx = 0; indx < nd; ++indx) {
center[indx] += (grid.node_coordinates+nd*(grid.face_nodes[start+2]))[indx];
center[indx] += (grid.node_coordinates+nd*(grid.face_nodes[start+3]))[indx];
}
}
break;
case 4: {
if (i == grid.cell_facepos[cell_index+1] - 2) {
for (int indx = 0; indx < nd; ++indx) {
center[indx] += (grid.node_coordinates+nd*(grid.face_nodes[start]))[indx];
center[indx] += (grid.node_coordinates+nd*(grid.face_nodes[start+1]))[indx];
center[indx] += (grid.node_coordinates+nd*(grid.face_nodes[start+2]))[indx];
center[indx] += (grid.node_coordinates+nd*(grid.face_nodes[start+3]))[indx];
}
}
}
break;
case 5: {
if (i == grid.cell_facepos[cell_index+1] - 1) {
for (int indx = 0; indx < nd; ++indx) {
center[indx] += (grid.node_coordinates+nd*(grid.face_nodes[start]))[indx];
center[indx] += (grid.node_coordinates+nd*(grid.face_nodes[start+1]))[indx];
center[indx] += (grid.node_coordinates+nd*(grid.face_nodes[start+2]))[indx];
center[indx] += (grid.node_coordinates+nd*(grid.face_nodes[start+3]))[indx];
}
}
}
break;
}
}
for (int indx = 0; indx < nd; ++indx) {
center[indx] /= nv;
}
return center;
}
Dune::FieldVector<double,3> faceAreaNormalEcl(const UnstructuredGrid& grid, int face_index)
{
// This method is an alternative to the method faceNormal(...) below.
// The face Normal area is computed based on the face corners without introducing
// a center point.
// For cornerpoint grids, this is likely to give slightly different depths that seem
// to agree with eclipse.
assert(grid.dimensions == 3);
const int nd = 3; // Assuming 3-dimensional grid ...
const int nv = grid.face_nodepos[face_index+1] - grid.face_nodepos[face_index];
const int start = grid.face_nodepos[face_index];
typedef Dune::FieldVector<double,3> Vector;
switch (nv)
{
case 0:
case 1:
case 2:
{
return Vector( 0 );
}
break;
case 3:
{
Vector a, b;
for (int i = 0; i < 3; ++i) {
a[i] = (grid.node_coordinates+nd*(grid.face_nodes[start] ))[i] - (grid.node_coordinates+nd*(grid.face_nodes[start+2]))[i];
b[i] = (grid.node_coordinates+nd*(grid.face_nodes[start+1]))[i] - (grid.node_coordinates+nd*(grid.face_nodes[start+2]))[i];
}
Vector areaNormal = cross( a, b);
areaNormal *= 0.5;
return areaNormal;
}
break;
case 4:
{
Vector a, b;
for (int i = 0; i < 3; ++i) {
a[i] = (grid.node_coordinates+nd*(grid.face_nodes[start] ))[i] - (grid.node_coordinates+nd*(grid.face_nodes[start+2]))[i];
b[i] = (grid.node_coordinates+nd*(grid.face_nodes[start+1]))[i] - (grid.node_coordinates+nd*(grid.face_nodes[start+3]))[i];
}
Vector areaNormal = Dune::cross( a, b);
areaNormal *= 0.5;
return areaNormal;
}
break;
default:
{
int h = (nv - 1)/2;
int k = (nv % 2) ? 0 : nv - 1;
Vector areaNormal ( 0 );
Vector a, b;
// First quads
for (int j = 1; j < h; ++j)
{
for (int i = 0; i < 3; ++i) {
a[i] = (grid.node_coordinates+nd*(grid.face_nodes[start+2*j] ))[i] - (grid.node_coordinates+nd*(grid.face_nodes[start]))[i];
b[i] = (grid.node_coordinates+nd*(grid.face_nodes[start+2*j + 1]))[i] - (grid.node_coordinates+nd*(grid.face_nodes[start+ 2*j-1]))[i];
}
areaNormal += cross( a , b ) ;
}
// Last triangle or quad
for (int i = 0; i < 3; ++i) {
a[i] = (grid.node_coordinates+nd*(grid.face_nodes[start+2*h] ))[i] - (grid.node_coordinates+nd*(grid.face_nodes[start]))[i];
b[i] = (grid.node_coordinates+nd*(grid.face_nodes[start+k]))[i] - (grid.node_coordinates+nd*(grid.face_nodes[start+ 2*h-1]))[i];
}
areaNormal += cross( a , b ) ;
areaNormal *= 0.5;
return areaNormal;
}
}
}
double cellCentroidCoordinate(const UnstructuredGrid& grid, int cell_index,
int coordinate)
{
return grid.cell_centroids[grid.dimensions*cell_index+coordinate];
}
const double*
cellCentroid(const UnstructuredGrid& grid, int cell_index)
{
return grid.cell_centroids+(cell_index*grid.dimensions);
}
const double* beginCellVolumes(const UnstructuredGrid& grid)
{
return grid.cell_volumes;
}
const double* endCellVolumes(const UnstructuredGrid& grid)
{
return grid.cell_volumes+numCells(grid);
}
const double* beginFaceCentroids(const UnstructuredGrid& grid)
{
return grid.face_centroids;
}
const double* faceCentroid(const UnstructuredGrid& grid, int face_index)
{
return grid.face_centroids+face_index*grid.dimensions;
}
const double* faceNormal(const UnstructuredGrid& grid, int face_index)
{
return grid.face_normals+face_index*grid.dimensions;
}
double faceArea(const UnstructuredGrid& grid, int face_index)
{
return grid.face_areas[face_index];
}
SparseTableView cell2Faces(const UnstructuredGrid& grid)
{
return SparseTableView(grid.cell_faces, grid.cell_facepos, numCells(grid));
}
SparseTableView face2Vertices(const UnstructuredGrid& grid)
{
return SparseTableView(grid.face_nodes, grid.face_nodepos, numFaces(grid));
}
const double* vertexCoordinates(const UnstructuredGrid& grid, int index)
{
return grid.node_coordinates+dimensions(grid)*index;
}
double cellVolume(const UnstructuredGrid& grid, int cell_index)
{
return grid.cell_volumes[cell_index];
}
FaceCellTraits<UnstructuredGrid>::Type faceCells(const UnstructuredGrid& grid)
{
return FaceCellsProxy(grid);
}
#if HAVE_OPM_COMMON
Opm::EclipseGrid createEclipseGrid(const UnstructuredGrid& grid, const Opm::EclipseGrid& inputGrid ) {
const int * dims = UgGridHelpers::cartDims( grid );
if ((inputGrid.getNX( ) == static_cast<size_t>(dims[0])) &&
(inputGrid.getNY( ) == static_cast<size_t>(dims[1])) &&
(inputGrid.getNZ( ) == static_cast<size_t>(dims[2]))) {
std::vector<int> updatedACTNUM;
const int* global_cell = UgGridHelpers::globalCell( grid );
if (global_cell) {
updatedACTNUM.assign( inputGrid.getCartesianSize( ) , 0 );
for (int c = 0; c < numCells( grid ); c++) {
updatedACTNUM[global_cell[c]] = 1;
}
}
return Opm::EclipseGrid( inputGrid, grid.zcorn, updatedACTNUM );
} else {
throw std::invalid_argument("Size mismatch - dimensions of inputGrid argument and current UnstructuredGrid instance disagree");
}
}
#endif
}
}