forked from PetroleumCyberneticsGroup/FieldOpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheclgridreader.cpp
More file actions
265 lines (231 loc) · 9.4 KB
/
Copy patheclgridreader.cpp
File metadata and controls
265 lines (231 loc) · 9.4 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
/******************************************************************************
Copyright (C) 2015-2016 Einar J.M. Baumann <[email protected]>
Modified by Alin G. Chitu (2016-2017) <[email protected], [email protected]>
This file is part of the FieldOpt project.
FieldOpt 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.
FieldOpt 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 FieldOpt. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "eclgridreader.h"
#include <iostream>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/lexical_cast.hpp>
#include "ertwrapper_exceptions.h"
namespace ERTWrapper {
namespace ECLGrid {
Eigen::Vector3d ECLGridReader::GetCellCenter(int global_index)
{
double cx, cy, cz;
ecl_grid_get_xyz1(ecl_grid_, global_index, &cx, &cy, &cz);
return Eigen::Vector3d(cx, cy, cz);
}
std::vector<Eigen::Vector3d> ECLGridReader::GetCellCorners(int global_index)
{
std::vector<Eigen::Vector3d> corners;
for (int i = 0; i < 8; ++i) {
double x, y, z;
ecl_grid_get_cell_corner_xyz1(ecl_grid_, global_index, i, &x, &y, &z);
corners.push_back(Eigen::Vector3d(x, y, z));
}
return corners;
}
double ECLGridReader::GetCellVolume(int global_index)
{
return ecl_grid_get_cell_volume1(ecl_grid_, global_index);
}
ECLGridReader::ECLGridReader()
{
ecl_grid_ = 0;
ecl_file_init_ = 0;
poro_kw_ = 0;
permx_kw_ = 0;
permy_kw_ = 0;
permz_kw_ = 0;
}
ECLGridReader::~ECLGridReader()
{
if (ecl_grid_ != 0)
ecl_grid_free(ecl_grid_);
if (ecl_file_init_ != 0)
ecl_file_close(ecl_file_init_);
}
void ECLGridReader::ReadEclGrid(std::string file_name)
{
file_name_ = file_name;
init_file_name_ = file_name;
if (boost::algorithm::ends_with(file_name, ".EGRID"))
boost::replace_all(init_file_name_, ".EGRID", ".INIT");
else if (boost::algorithm::ends_with(file_name, ".GRID"))
boost::replace_all(init_file_name_, ".GRID", ".INIT");
if (ecl_grid_ == 0) {
ecl_grid_ = ecl_grid_alloc(file_name_.c_str());
} else {
ecl_grid_free(ecl_grid_);
ecl_grid_ = ecl_grid_alloc(file_name_.c_str());
}
if (ecl_file_init_ == 0) {
ecl_file_init_ = ecl_file_open(init_file_name_.c_str(), 0);
poro_kw_ = ecl_file_iget_named_kw(ecl_file_init_, "PORO", 0);
permx_kw_ = ecl_file_iget_named_kw(ecl_file_init_, "PERMX", 0);
permy_kw_ = ecl_file_iget_named_kw(ecl_file_init_, "PERMY", 0);
permz_kw_ = ecl_file_iget_named_kw(ecl_file_init_, "PERMZ", 0);
} else {
ecl_file_close(ecl_file_init_);
ecl_file_init_ = ecl_file_open(init_file_name_.c_str(), 0);
poro_kw_ = ecl_file_iget_named_kw(ecl_file_init_, "PORO", 0);
permx_kw_ = ecl_file_iget_named_kw(ecl_file_init_, "PERMX", 0);
permy_kw_ = ecl_file_iget_named_kw(ecl_file_init_, "PERMY", 0);
permz_kw_ = ecl_file_iget_named_kw(ecl_file_init_, "PERMZ", 0);
}
}
int ECLGridReader::ConvertIJKToGlobalIndex(ECLGridReader::IJKIndex ijk)
{
if (ecl_grid_ == 0) throw GridNotReadException("Grid must be read before converting indices.");
return ecl_grid_get_global_index3(ecl_grid_, ijk.i, ijk.j, ijk.k);
}
int ECLGridReader::ConvertIJKToGlobalIndex(int i, int j, int k)
{
if (ecl_grid_ == 0) throw GridNotReadException("Grid must be read before converting indices.");
return ecl_grid_get_global_index3(ecl_grid_, i, j, k);
}
ECLGridReader::IJKIndex ECLGridReader::ConvertGlobalIndexToIJK(int global_index)
{
if (ecl_grid_ == 0) throw GridNotReadException("Grid must be read before converting indices.");
int i, j, k;
ecl_grid_get_ijk1(ecl_grid_, global_index, &i, &j, &k);
ECLGridReader::IJKIndex ijk;
ijk.i = i; ijk.j = j; ijk.k = k;
return ijk;
}
int ECLGridReader::ConvertMatrixActiveIndexToGlobalIndex(int index)
{
if (ecl_grid_ == 0) throw GridNotReadException("Grid must be read before converting indices.");
else return ecl_grid_get_global_index1A(ecl_grid_, index);
}
ECLGridReader::Dims ECLGridReader::Dimensions()
{
ECLGridReader::Dims dims;
if (ecl_grid_ == 0) throw GridNotReadException("Grid must be read before getting dimensions.");
int x, y, z;
ecl_grid_get_dims(ecl_grid_, &x, &y, &z, NULL);
dims.nx = x; dims.ny = y; dims.nz = z;
return dims;
}
int ECLGridReader::NumActiveMatrixCells()
{
if (ecl_grid_ == 0) throw GridNotReadException("Grid must be read before getting the number of active cells.");
else return ecl_grid_get_nactive(ecl_grid_);
}
int ECLGridReader::NumActiveFractureCells()
{
if (ecl_grid_ == 0) throw GridNotReadException("Grid must be read before getting the number of active cells.");
else return ecl_grid_get_nactive_fracture(ecl_grid_);
}
bool ECLGridReader::IsCellActive(int global_index)
{
if (ecl_grid_ == 0) throw GridNotReadException("Grid must be read before getting the active status of cells.");
else return (IsCellMatrixActive(global_index) || IsCellFractureActive(global_index));
}
bool ECLGridReader::IsCellMatrixActive(int global_index)
{
if (ecl_grid_ == 0) throw GridNotReadException("Grid must be read before getting the active status of cells.");
else
{
int active_matrix_index = ecl_grid_get_active_index1(ecl_grid_, global_index);
if (active_matrix_index < 0) return false;
else return true;
}
}
bool ECLGridReader::IsCellFractureActive(int global_index)
{
if (ecl_grid_ == 0) throw GridNotReadException("Grid must be read before getting the active status of cells.");
else
{
int active_fracture_index = ecl_grid_get_active_fracture_index1(ecl_grid_, global_index);
if (active_fracture_index < 0) return false;
else return true;
}
}
std::vector<double> ECLGridReader::GetCellDxDyDz(int global_index) {
std::vector<double> dxdydz(3);
dxdydz[0] = ecl_grid_get_cell_dx1(ecl_grid_, global_index);
dxdydz[1] = ecl_grid_get_cell_dy1(ecl_grid_, global_index);
dxdydz[2] = ecl_grid_get_cell_dz1(ecl_grid_, global_index);
return dxdydz;
}
ECLGridReader::Cell ECLGridReader::GetGridCell(int global_index)
{
if (!GlobalIndexIsInsideGrid(global_index))
throw InvalidIndexException("The global index "
+ boost::lexical_cast<std::string>(global_index)
+ " is outside the grid.");
if (ecl_grid_ == 0) throw GridNotReadException("Grid must be read before getting grid cells.");
ECLGridReader::Cell cell;
cell.global_index = global_index;
cell.volume = GetCellVolume(global_index);
cell.corners = GetCellCorners(global_index);
cell.center = GetCellCenter(global_index);
cell.matrix_active = IsCellMatrixActive(global_index);
cell.fracture_active = IsCellFractureActive(global_index);
auto dxdydz = GetCellDxDyDz(global_index);
cell.dx = dxdydz[0];
cell.dy = dxdydz[1];
cell.dz = dxdydz[2];
// Get properties from the INIT file - only possible if the cell is active
// Matrix grid
int active_index = ecl_grid_get_active_index1(ecl_grid_, global_index);
if (active_index >= 0)
{
cell.porosity.push_back(ecl_kw_iget_as_double(poro_kw_, active_index));
cell.permx.push_back(ecl_kw_iget_as_double(permx_kw_, active_index));
cell.permy.push_back(ecl_kw_iget_as_double(permy_kw_, active_index));
cell.permz.push_back(ecl_kw_iget_as_double(permz_kw_, active_index));
}
// Fracture grid
active_index = NumActiveMatrixCells() + ecl_grid_get_active_fracture_index1(ecl_grid_, global_index);
if (active_index >= NumActiveMatrixCells())
{
cell.porosity.push_back(ecl_kw_iget_as_double(poro_kw_, active_index));
cell.permx.push_back(ecl_kw_iget_as_double(permx_kw_, active_index));
cell.permy.push_back(ecl_kw_iget_as_double(permy_kw_, active_index));
cell.permz.push_back(ecl_kw_iget_as_double(permz_kw_, active_index));
}
return cell;
}
int ECLGridReader::GlobalIndexOfCellEnvelopingPoint(double x, double y, double z, int initial_guess)
{
if (ecl_grid_ == 0) throw GridNotReadException("Grid must be read before searching for cells.");
return ecl_grid_get_global_index_from_xyz(ecl_grid_, x, y, z, initial_guess);
}
bool ECLGridReader::GlobalIndexIsInsideGrid(int global_index)
{
Dims dims = Dimensions();
return global_index < dims.nx * dims.ny * dims.nz;
}
ECLGridReader::Cell ECLGridReader::FindSmallestCell() {
auto dims = Dimensions();
int max_index = dims.nx * dims.ny * dims.nz;
int index_with_smallest_volume = 0;
double smallest_volume = 1e7;
double volume;
for (int global_index = 0; global_index < max_index; ++global_index) {
if (IsCellActive(global_index)) {
volume = GetCellVolume(global_index);
if (volume < smallest_volume) {
index_with_smallest_volume = global_index;
smallest_volume = volume;
}
}
}
return GetGridCell(index_with_smallest_volume);
}
}
}