opm-common
ESmry.hpp
1 /*
2  Copyright 2019 Equinor ASA.
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  OPM is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with OPM. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef OPM_IO_ESMRY_HPP
20 #define OPM_IO_ESMRY_HPP
21 
22 #include <opm/common/utility/TimeService.hpp>
23 #include <opm/io/eclipse/SummaryNode.hpp>
24 
25 #include <algorithm>
26 #include <chrono>
27 #include <cstddef>
28 #include <cstdint>
29 #include <filesystem>
30 #include <iosfwd>
31 #include <map>
32 #include <string>
33 #include <unordered_map>
34 #include <vector>
35 
36 namespace Opm { namespace EclIO {
37 
38 using ArrSourceEntry = std::tuple<std::string, std::string, int, std::uint64_t>;
39 using TimeStepEntry = std::tuple<int, int, std::uint64_t>;
40 using RstEntry = std::tuple<std::string, int>;
41 
42 class ESmry
43 {
44 public:
45 
46  // input is smspec (or fsmspec file)
47  explicit ESmry(const std::string& filename, bool loadBaseRunData=false);
48 
49  int numberOfVectors() const { return nVect; }
50 
51  bool hasKey(const std::string& key) const;
52 
53  const std::vector<float>& get(const std::string& name) const;
54  const std::vector<float>& get(const SummaryNode& node) const;
55  std::vector<time_point> dates() const;
56 
57  std::vector<float> get_at_rstep(const std::string& name) const;
58  std::vector<float> get_at_rstep(const SummaryNode& node) const;
59  std::vector<time_point> dates_at_rstep() const;
60 
61  void loadData(const std::vector<std::string>& vectList) const;
62  void loadData() const;
63 
64  bool make_esmry_file();
65 
66  time_point startdate() const { return tp_startdat; }
67  const std::vector<int>& start_v() const { return start_vect; }
68 
69  const std::vector<std::string>& keywordList() const;
70  std::vector<std::string> keywordList(const std::string& pattern) const;
71  const std::vector<SummaryNode>& summaryNodeList() const;
72 
73  int timestepIdxAtReportstepStart(const int reportStep) const;
74 
75  std::size_t numberOfTimeSteps() const { return nTstep; }
76 
77  const std::string& get_unit(const std::string& name) const;
78  const std::string& get_unit(const SummaryNode& node) const;
79 
80  void write_rsm(std::ostream&) const;
81  void write_rsm_file(std::optional<std::filesystem::path> = std::nullopt) const;
82 
83  bool all_steps_available();
84  std::string rootname() { return inputFileName.stem().generic_string(); }
85  std::tuple<double, double> get_io_elapsed() const;
86 
87 private:
88  std::filesystem::path inputFileName;
89  RstEntry restart_info;
90 
91  int nI, nJ, nK, nSpecFiles;
92  bool fromSingleRun;
93  std::size_t nVect, nTstep;
94 
95  std::vector<bool> formattedFiles;
96  std::vector<std::string> dataFileList;
97  mutable std::vector<std::vector<float>> vectorData;
98  mutable std::vector<bool> vectorLoaded;
99  std::vector<TimeStepEntry> timeStepList;
100  std::vector<TimeStepEntry> miniStepList;
101  std::vector<std::map<int, int>> arrayPos;
102  std::vector<std::string> keyword;
103  std::map<std::string, int> keyword_index;
104  std::vector<int> nParamsSpecFile;
105 
106  std::vector<std::vector<std::string>> keywordListSpecFile;
107 
108  std::vector<int> seqIndex;
109  std::vector<int> mini_steps;
110 
111  std::vector<std::string> ignore_keyword_list = {"TNAVHEAD", "TNAVTIME"};
112 
113  void ijk_from_global_index(int glob, int &i, int &j, int &k) const;
114 
115  std::vector<SummaryNode> summaryNodes;
116  std::unordered_map<std::string, std::string> kwunits;
117 
118  time_point tp_startdat;
119  std::vector<int> start_vect;
120 
121  mutable double m_io_opening;
122  mutable double m_io_loading;
123 
124  std::vector<std::string> checkForMultipleResultFiles(const std::filesystem::path& rootN, bool formatted) const;
125 
126  void getRstString(const std::vector<std::string>& restartArray,
127  std::filesystem::path& pathRst,
128  std::filesystem::path& rootN) const;
129 
130  void updatePathAndRootName(std::filesystem::path& dir, std::filesystem::path& rootN) const;
131 
132  std::string makeKeyString(const std::string& keyword, const std::string& wgname, int num,
133  const std::optional<Opm::EclIO::lgr_info> lgr_info) const;
134 
135  std::string unpackNumber(const SummaryNode&) const;
136  std::string lookupKey(const SummaryNode&) const;
137 
138  void write_block(std::ostream &, bool write_dates, const std::vector<std::string>& time_column, const std::vector<SummaryNode>&) const;
139 
140  template <typename T>
141  std::vector<T> rstep_vector(const std::vector<T>& full_vector) const {
142  std::vector<T> result;
143  result.reserve(seqIndex.size());
144 
145  std::ranges::transform(seqIndex, std::back_inserter(result),
146  [&full_vector](const auto& ind)
147  { return full_vector[ind]; });
148 
149  return result;
150  }
151 
152  std::vector<std::tuple <std::string, std::uint64_t>>
153  getListOfArrays(const std::string& filename, bool formatted);
154 
155  std::vector<int> makeKeywPosVector(int speInd) const;
156  std::string read_string_from_disk(std::fstream& fileH, std::uint64_t size) const;
157 
158  void read_ministeps_from_disk();
159  int read_ministep_formatted(std::fstream& fileH);
160 };
161 
162 }} // namespace Opm::EclIO
163 
164 inline std::ostream& operator<<(std::ostream& os, const Opm::EclIO::ESmry& smry) {
165  smry.write_rsm(os);
166 
167  return os;
168 }
169 
170 #endif // OPM_IO_ESMRY_HPP
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
Definition: SummaryNode.hpp:31
Definition: ESmry.hpp:42
Definition: SummaryNode.hpp:36