File: make_ext_smry.cpp

package info (click to toggle)
opm-common 2025.10%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 96,952 kB
  • sloc: cpp: 291,772; python: 3,609; sh: 198; xml: 174; pascal: 136; makefile: 12
file content (131 lines) | stat: -rw-r--r-- 4,091 bytes parent folder | download | duplicates (2)
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
/*
  Copyright 2019 Equinor 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/>.
*/

#include <algorithm>
#include <filesystem>
#include <iostream>
#include <getopt.h>
#include <string.h>
#include <stdio.h>

#include "config.h"

#if _OPENMP
#include <omp.h>
#endif

#include <opm/io/eclipse/ESmry.hpp>
#include <opm/io/eclipse/EclUtil.hpp>


static void printHelp() {

    std::cout << "\nThis program create one or more lodsmry files, designed for effective load on the demand.   \n"
              << "These files are created with input from the smspec and unsmry file. \n"
              << "\nIn addition, the program takes these options (which must be given before the arguments):\n\n"
              << "-f if ESMRY file exist, this will be replaced. Default behaviour is that existing file is kept.\n"
              << "-n Maximum number of threads to be used if mulitple files should be created.\n"
              << "-h Print help and exit.\n\n";
}


int main(int argc, char **argv) {

    int c                          = 0;
#ifdef _OPENMP
    int max_threads = -1;
#endif
    bool force                     = false;

    while ((c = getopt(argc, argv, "fn:h")) != -1) {
        switch (c) {
        case 'f':
            force = true;
            break;
        case 'h':
            printHelp();
            return 0;
        case 'n':
#ifdef _OPENMP
            max_threads = atoi(optarg);
#else
            std::cerr << "OpenMP is disabled - using single thread only\n";
#endif
            break;
        default:
            return EXIT_FAILURE;
        }
    }

    int argOffset = optind;

#ifdef _OPENMP
    int available_threads = omp_get_max_threads();

    if (max_threads < 0)
        max_threads = available_threads-2;
    else if (max_threads > (available_threads - 1))
        max_threads = available_threads-1;

    if (max_threads > (argc-argOffset))
        max_threads = argc-argOffset;

    omp_set_num_threads(max_threads);
#endif

    auto lap0 = std::chrono::system_clock::now();

    int num_esmry = argc-argOffset;
    std::vector<bool> status(num_esmry, false);

    #pragma omp parallel for
    for (int f = 0; f < num_esmry; f ++){
        std::filesystem::path inputFileName = argv[f + argOffset];

        std::filesystem::path esmryFileName = inputFileName.parent_path() / inputFileName.stem();
        esmryFileName = esmryFileName += ".ESMRY";

        if (Opm::EclIO::fileExists(esmryFileName) && (force))
            remove (esmryFileName);

        try {
            Opm::EclIO::ESmry smry{ argv[f + argOffset] };

            if (smry.numberOfTimeSteps() > 0){
                status[f] = smry.make_esmry_file();
                if (! status[f]) {
                    std::cerr << "\n! Warning, smspec already have one esmry file, existing kept use option -f to replace this\n";
                }
            } else {
                std::cerr << "\n! summary file doesn't hold any time step data, ESMRY file not created. \n";
            }

        } catch (...) {
            std::cerr << "\n! Warning, could not open summary file " << argv[f + argOffset] << '\n';
        }
    }

    const auto n_converted = std::count(status.begin(), status.end(), true);

    auto lap1 = std::chrono::system_clock::now();
    std::chrono::duration<double> elapsed_seconds1 = lap1-lap0;
    std::cout << "\nruntime for creating " << n_converted << " ESMRY files: " << elapsed_seconds1.count() << " seconds\n" << std::endl;

    return 0;
}