-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathexport_grid.cpp
More file actions
236 lines (197 loc) · 7.19 KB
/
Copy pathexport_grid.cpp
File metadata and controls
236 lines (197 loc) · 7.19 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
#include <config.h>
#include <opm/grid/CpGrid.hpp>
#include <opm/grid/cpgrid/GridHelpers.hpp>
#if HAVE_OPM_COMMON
#include <opm/input/eclipse/Deck/Deck.hpp>
#include <opm/input/eclipse/EclipseState/EclipseState.hpp>
#include <opm/input/eclipse/EclipseState/Grid/EclipseGrid.hpp>
#include <opm/input/eclipse/Parser/InputErrorAction.hpp>
#include <opm/input/eclipse/Parser/ParseContext.hpp>
#include <opm/input/eclipse/Parser/Parser.hpp>
#endif
#include <fstream>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
namespace {
#if HAVE_OPM_COMMON
Opm::ParseContext makeFlowLikeParseContext()
{
return Opm::ParseContext({
{Opm::ParseContext::PARSE_RANDOM_SLASH, Opm::InputErrorAction::IGNORE},
{Opm::ParseContext::PARSE_MISSING_DIMS_KEYWORD, Opm::InputErrorAction::WARN},
{Opm::ParseContext::SUMMARY_UNKNOWN_WELL, Opm::InputErrorAction::WARN},
{Opm::ParseContext::SUMMARY_UNKNOWN_GROUP, Opm::InputErrorAction::WARN},
});
}
#endif
void writeGridFromCpGrid(const Dune::CpGrid& grid, const std::string& output_file)
{
const int ndims = 3;
const int ncells = grid.numCells();
const int nfaces = grid.numFaces();
const int nnodes = grid.numVertices();
const int ncellfaces = grid.numCellFaces();
int nfacenodes = 0;
std::vector<int> face_nodepos;
std::vector<int> face_nodes;
face_nodepos.reserve(nfaces + 1);
face_nodes.reserve(4 * nfaces);
face_nodepos.push_back(0);
for (int f = 0; f < nfaces; ++f) {
const int nv = grid.numFaceVertices(f);
for (int lv = 0; lv < nv; ++lv) {
face_nodes.push_back(grid.faceVertex(f, lv));
}
nfacenodes += nv;
face_nodepos.push_back(nfacenodes);
}
std::vector<int> face_cells;
face_cells.reserve(2 * nfaces);
for (int f = 0; f < nfaces; ++f) {
face_cells.push_back(grid.faceCell(f, 0));
face_cells.push_back(grid.faceCell(f, 1));
}
std::vector<int> cell_facepos;
std::vector<int> cell_faces;
std::vector<int> cell_facetag;
cell_facepos.reserve(ncells + 1);
cell_faces.reserve(ncellfaces);
cell_facetag.reserve(ncellfaces);
cell_facepos.push_back(0);
const auto c2f = Opm::UgGridHelpers::cell2Faces(grid);
for (int c = 0; c < ncells; ++c) {
const auto row = c2f[c];
for (auto it = row.begin(); it != row.end(); ++it) {
const int face = *it;
cell_faces.push_back(face);
// Export the exact CpGrid face tag used by transmissibility logic
const int tag = Opm::UgGridHelpers::faceTag(grid, it);
cell_facetag.push_back(tag);
}
cell_facepos.push_back(static_cast<int>(cell_faces.size()));
}
if (static_cast<int>(cell_faces.size()) != ncellfaces) {
throw std::runtime_error("Internal inconsistency: numCellFaces() total mismatch");
}
std::ofstream out(output_file);
if (!out) {
throw std::runtime_error("Failed to open output file: " + output_file);
}
out << std::setprecision(17) << std::fixed;
const int has_tag = 1;
const int has_indexmap = 1;
out << ndims << ' ' << ncells << ' ' << nfaces << ' '
<< nnodes << ' ' << nfacenodes << ' ' << ncellfaces << ' '
<< has_tag << ' ' << has_indexmap << '\n';
const auto cartdims = grid.logicalCartesianSize();
out << cartdims[0] << ' ' << cartdims[1] << ' ' << cartdims[2] << '\n';
for (int v = 0; v < nnodes; ++v) {
const auto& p = grid.vertexPosition(v);
out << p[0] << ' ' << p[1] << ' ' << p[2] << ' ';
}
out << '\n';
for (const int value : face_nodepos) {
out << value << ' ';
}
out << '\n';
for (const int value : face_nodes) {
out << value << ' ';
}
out << '\n';
for (const int value : face_cells) {
out << value << ' ';
}
out << '\n';
for (int f = 0; f < nfaces; ++f) {
out << grid.faceArea(f) << ' ';
}
out << '\n';
for (int f = 0; f < nfaces; ++f) {
const auto& c = grid.faceCentroid(f);
out << c[0] << ' ' << c[1] << ' ' << c[2] << ' ';
}
out << '\n';
for (int f = 0; f < nfaces; ++f) {
const auto& n = grid.faceNormal(f);
out << n[0] << ' ' << n[1] << ' ' << n[2] << ' ';
}
out << '\n';
for (const int value : cell_facepos) {
out << value << ' ';
}
out << '\n';
for (std::size_t i = 0; i < cell_faces.size(); ++i) {
out << cell_faces[i] << ' ' << cell_facetag[i] << ' ';
}
out << '\n';
const auto& global_cell = grid.globalCell();
for (const int value : global_cell) {
out << value << ' ';
}
out << '\n';
for (int c = 0; c < ncells; ++c) {
out << grid.cellVolume(c) << ' ';
}
out << '\n';
for (int c = 0; c < ncells; ++c) {
const auto& cc = grid.cellCentroid(c);
out << cc[0] << ' ' << cc[1] << ' ' << cc[2] << ' ';
}
out << '\n';
}
} // namespace
int main(int argc, char** argv)
{
Dune::MPIHelper::instance(argc, argv);
#if !HAVE_OPM_COMMON
std::cerr << "export_grid requires HAVE_OPM_COMMON to parse .DATA files." << std::endl;
return 2;
#else
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " <case.DATA> <output.grid>" << std::endl;
return 1;
}
const std::string data_file = argv[1];
const std::string output_file = argv[2];
try {
Dune::CpGrid cpgrid;
bool built_from_data = false;
try {
Opm::Parser parser;
const auto deck = parser.parseFile(data_file, makeFlowLikeParseContext());
Opm::EclipseGrid eclipse_grid(deck);
Opm::EclipseState ecl_state(deck);
cpgrid.processEclipseFormat(&eclipse_grid, &ecl_state, false);
built_from_data = true;
} catch (const std::exception& e) {
const std::filesystem::path data_path(data_file);
const std::filesystem::path egrid_path =
data_path.parent_path() / (data_path.stem().string() + ".EGRID");
if (!std::filesystem::exists(egrid_path)) {
throw std::runtime_error(
std::string("Failed to parse DATA and no EGRID fallback found. DATA error: ") + e.what());
}
std::cerr << "Warning: failed to build grid directly from DATA (" << e.what() << ")\n"
<< "Falling back to EGRID: " << egrid_path << "\n";
Opm::EclipseGrid eclipse_grid(egrid_path.string());
cpgrid.processEclipseFormat(&eclipse_grid, nullptr, false);
}
writeGridFromCpGrid(cpgrid, output_file);
std::cout << "Wrote grid: " << output_file << "\n"
<< " source : " << (built_from_data ? "DATA" : "EGRID fallback") << "\n"
<< " cells/faces/vertices/cellFaces = "
<< cpgrid.numCells() << " / "
<< cpgrid.numFaces() << " / "
<< cpgrid.numVertices() << " / "
<< cpgrid.numCellFaces() << "\n";
} catch (const std::exception& e) {
std::cerr << "Failed to export grid from DATA: " << e.what() << std::endl;
return 3;
}
return 0;
#endif
}