File: grdecl2vtu.cpp

package info (click to toggle)
opm-grid 2025.10%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,280 kB
  • sloc: cpp: 39,624; ansic: 3,357; sh: 67; makefile: 18
file content (189 lines) | stat: -rw-r--r-- 7,086 bytes parent folder | download
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
/*
  Copyright 2009, 2010, 2013 SINTEF ICT, Applied Mathematics.
  Copyright 2009, 2010, 2013 Statoil ASA.

  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/>.
*/

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <iostream>

// Warning suppression for Dune includes.
#include <opm/grid/utility/platform_dependent/disable_warnings.h>
#include <dune/grid/io/file/vtk/vtkwriter.hh>
#include <opm/grid/utility/platform_dependent/reenable_warnings.h>

#include <opm/grid/CpGrid.hpp>

#include <opm/input/eclipse/Parser/Parser.hpp>
#include <opm/input/eclipse/Deck/Deck.hpp>
#include <opm/input/eclipse/EclipseState/Grid/EclipseGrid.hpp>

using namespace Dune;

/**
 * @file grdecl2vtu.C
 * @brief Converts grdecl (Eclipse grid) files to vtu (VTK/ParaView)
 *
 * Converts a corner-point grid with properties to a vtu-file
 * (to be opened in ParaView for example)
 *
 * Based on make_vtk_test.cpp
 *
 * @author H�vard Berland <havb@statoil.com>
 * @author Atgeirr F Rasmussen <atgeirr@sintef.no>
 * @author B�rd Skaflestad     <bard.skaflestad@sintef.no>
 *
 */

/**
   A function to (conditionally) write a double-field from the grdecl-file to vtk-format
*/
template <class OpmDeck>
void condWriteDoubleField(std::vector<double>& fieldvector,
                          const std::string& fieldname,
                          const OpmDeck& deck,
                          const std::vector<int>& global_cell,
                          const std::array<size_t, 3>& dims,
                          VTKWriter<CpGrid::LeafGridView>& vtkwriter) {
    if (deck.hasKeyword(fieldname)) {
        std::cout << "Found " << fieldname << "..." << std::endl;
        std::vector<double> eclVector = deck[fieldname].back().getRawDoubleData();
        fieldvector.resize(global_cell.size());
        int num_global_cells = dims[0]*dims[1]*dims[2];
        if (int(eclVector.size()) != num_global_cells) {
            OPM_THROW(std::runtime_error,
                      fieldname + " field must have the same size as the "
                      "logical cartesian size of the grid: " +
                      std::to_string(eclVector.size()) + " != " +
                      std::to_string(num_global_cells));
        }

        for (size_t i = 0; i < global_cell.size(); ++i) {
            fieldvector[i] = eclVector[global_cell[i]];
        }
        vtkwriter.addCellData(fieldvector, fieldname);
    }

}

// Now repeat for Integers. I should learn C++ templating...
template <class OpmDeck>
void condWriteIntegerField(std::vector<double>& fieldvector,
                           const std::string& fieldname,
                           const OpmDeck& deck,
                           const std::vector<int>& global_cell,
                           const std::array<size_t, 3>& dims,
                           VTKWriter<CpGrid::LeafGridView>& vtkwriter) {
    if (deck.hasKeyword(fieldname)) {
        std::cout << "Found " << fieldname << "..." << std::endl;
        std::vector<int> eclVector = deck[fieldname].back().getIntData();
        fieldvector.resize(global_cell.size());
        int num_global_cells = dims[0]*dims[1]*dims[2];
        if (int(eclVector.size()) != num_global_cells) {
            OPM_THROW(std::runtime_error,
                      fieldname + " field must have the same size as the "
                      "logical cartesian size of the grid: " +
                      std::to_string(eclVector.size()) + " != " +
                      std::to_string(num_global_cells));
        }

        for (size_t i = 0; i < global_cell.size(); ++i) {
            fieldvector[i] = (double)eclVector[global_cell[i]];
        }
        vtkwriter.addCellData(fieldvector, fieldname);
    }

}


int main(int argc, char** argv)
try
{
    Dune::MPIHelper::instance(argc,argv); // Dummy if no MPI.

    CpGrid grid;

    if (argc != 2) {
        std::cout << "Usage: grdecl2vtu filename.grdecl" << std::endl;
        exit(1);
    }

    const char* eclipsefilename = argv[1];
    Opm::Parser parser;
    auto deck = parser.parseFile(eclipsefilename);

    // Get logical cartesian grid dimensions.
    std::array<size_t, 3> dims;
    if (deck.hasKeyword("SPECGRID")) {
        const auto& specgridRecord = deck["SPECGRID"].back().getRecord(0);
        dims[0] = specgridRecord.getItem("NX").get< int >(0);
        dims[1] = specgridRecord.getItem("NY").get< int >(0);
        dims[2] = specgridRecord.getItem("NZ").get< int >(0);
    } else if (deck.hasKeyword("DIMENS")) {
        const auto& dimensRecord = deck["DIMENS"].back().getRecord(0);
        dims[0] = dimensRecord.getItem("NX").get< int >(0);
        dims[1] = dimensRecord.getItem("NY").get< int >(0);
        dims[2] = dimensRecord.getItem("NZ").get< int >(0);
    } else {
        OPM_THROW(std::runtime_error, "Found neither SPECGRID nor DIMENS in file. At least one is needed.");
    }

    {
        const int* actnum = deck.hasKeyword("ACTNUM") ? deck["ACTNUM"].back().getIntData().data() : nullptr;
        Opm::EclipseGrid ecl_grid(deck , actnum);
        grid.processEclipseFormat(&ecl_grid, nullptr, false);
    }

    VTKWriter<CpGrid::LeafGridView> vtkwriter(grid.leafGridView());

    const std::vector<int>& global_cell = grid.globalCell();

    std::vector<double> poros;
    condWriteDoubleField(poros, "PORO", deck, global_cell, dims, vtkwriter);

    std::vector<double> permxs;
    condWriteDoubleField(permxs, "PERMX", deck, global_cell, dims, vtkwriter);
    std::vector<double> permys;
    condWriteDoubleField(permys, "PERMY", deck, global_cell, dims, vtkwriter);

    std::vector<double> permzs;
    condWriteDoubleField(permzs, "PERMZ", deck, global_cell, dims, vtkwriter);

    std::vector<double> actnums;
    condWriteIntegerField(actnums, "ACTNUM", deck, global_cell, dims, vtkwriter);

    std::vector<double> satnums;
    condWriteIntegerField(satnums, "SATNUM", deck, global_cell, dims, vtkwriter);

    std::vector<double> regnums;
    condWriteIntegerField(regnums, "REGNUM", deck, global_cell, dims, vtkwriter);

    std::vector<double> swats;
    condWriteDoubleField(swats, "SWAT", deck, global_cell, dims, vtkwriter);

    std::string fname(eclipsefilename);
    std::string fnamebase = fname.substr(0, fname.find_last_of('.'));
    std::cout << "Writing to filename " << fnamebase << ".vtu" << std::endl;
    vtkwriter.write(fnamebase, VTK::ascii);
}
catch (const std::exception &e) {
    std::cerr << "Program threw an exception: " << e.what() << "\n";
    throw;
}