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 <algorithm>
23 #include <chrono>
24 #include <filesystem>
25 #include <iosfwd>
26 #include <string>
27 #include <unordered_map>
28 #include <vector>
29 #include <map>
30 #include <stdint.h>
31 
32 #include <opm/common/utility/TimeService.hpp>
33 #include <opm/io/eclipse/SummaryNode.hpp>
34 
35 namespace Opm { namespace EclIO {
36 
37 using ArrSourceEntry = std::tuple<std::string, std::string, int, uint64_t>;
38 using TimeStepEntry = std::tuple<int, int, uint64_t>;
39 using RstEntry = std::tuple<std::string, int>;
40 
41 class ESmry
42 {
43 public:
44 
45  // input is smspec (or fsmspec file)
46  explicit ESmry(const std::string& filename, bool loadBaseRunData=false);
47 
48  int numberOfVectors() const { return nVect; }
49 
50  bool hasKey(const std::string& key) const;
51 
52  const std::vector<float>& get(const std::string& name) const;
53  const std::vector<float>& get(const SummaryNode& node) const;
54  std::vector<time_point> dates() const;
55 
56  std::vector<float> get_at_rstep(const std::string& name) const;
57  std::vector<float> get_at_rstep(const SummaryNode& node) const;
58  std::vector<time_point> dates_at_rstep() const;
59 
60  void loadData(const std::vector<std::string>& vectList) const;
61  void loadData() const;
62 
63  bool make_esmry_file();
64 
65  time_point startdate() const { return tp_startdat; }
66  const std::vector<int>& start_v() const { return start_vect; }
67 
68  const std::vector<std::string>& keywordList() const;
69  std::vector<std::string> keywordList(const std::string& pattern) const;
70  const std::vector<SummaryNode>& summaryNodeList() const;
71 
72  int timestepIdxAtReportstepStart(const int reportStep) const;
73 
74  size_t numberOfTimeSteps() const { return nTstep; }
75 
76  const std::string& get_unit(const std::string& name) const;
77  const std::string& get_unit(const SummaryNode& node) const;
78 
79  void write_rsm(std::ostream&) const;
80  void write_rsm_file(std::optional<std::filesystem::path> = std::nullopt) const;
81 
82  bool all_steps_available();
83  std::string rootname() { return inputFileName.stem().generic_string(); }
84  std::tuple<double, double> get_io_elapsed() const;
85 
86 private:
87  std::filesystem::path inputFileName;
88  RstEntry restart_info;
89 
90  int nI, nJ, nK, nSpecFiles;
91  bool fromSingleRun;
92  size_t nVect, nTstep;
93 
94  std::vector<bool> formattedFiles;
95  std::vector<std::string> dataFileList;
96  mutable std::vector<std::vector<float>> vectorData;
97  mutable std::vector<bool> vectorLoaded;
98  std::vector<TimeStepEntry> timeStepList;
99  std::vector<TimeStepEntry> miniStepList;
100  std::vector<std::map<int, int>> arrayPos;
101  std::vector<std::string> keyword;
102  std::map<std::string, int> keyword_index;
103  std::vector<int> nParamsSpecFile;
104 
105  std::vector<std::vector<std::string>> keywordListSpecFile;
106 
107  std::vector<int> seqIndex;
108  std::vector<int> mini_steps;
109 
110  std::vector<std::string> ignore_keyword_list = {"TNAVHEAD", "TNAVTIME"};
111 
112  void ijk_from_global_index(int glob, int &i, int &j, int &k) const;
113 
114  std::vector<SummaryNode> summaryNodes;
115  std::unordered_map<std::string, std::string> kwunits;
116 
117  time_point tp_startdat;
118  std::vector<int> start_vect;
119 
120  mutable double m_io_opening;
121  mutable double m_io_loading;
122 
123  std::vector<std::string> checkForMultipleResultFiles(const std::filesystem::path& rootN, bool formatted) const;
124 
125  void getRstString(const std::vector<std::string>& restartArray,
126  std::filesystem::path& pathRst,
127  std::filesystem::path& rootN) const;
128 
129  void updatePathAndRootName(std::filesystem::path& dir, std::filesystem::path& rootN) const;
130 
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 
139  void write_block(std::ostream &, bool write_dates, const std::vector<std::string>& time_column, const std::vector<SummaryNode>&) const;
140 
141  template <typename T>
142  std::vector<T> rstep_vector(const std::vector<T>& full_vector) const {
143  std::vector<T> result;
144  result.reserve(seqIndex.size());
145 
146  std::ranges::transform(seqIndex, std::back_inserter(result),
147  [&full_vector](const auto& ind)
148  { return full_vector[ind]; });
149 
150  return result;
151  }
152 
153  std::vector<std::tuple <std::string, uint64_t>>
154  getListOfArrays(const std::string& filename, bool formatted);
155 
156  std::vector<int> makeKeywPosVector(int speInd) const;
157  std::string read_string_from_disk(std::fstream& fileH, uint64_t size) const;
158 
159  void read_ministeps_from_disk();
160  int read_ministep_formatted(std::fstream& fileH);
161 };
162 
163 }} // namespace Opm::EclIO
164 
165 inline std::ostream& operator<<(std::ostream& os, const Opm::EclIO::ESmry& smry) {
166  smry.write_rsm(os);
167 
168  return os;
169 }
170 
171 #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:41
Definition: SummaryNode.hpp:36