opm-common
OutputStream.hpp
1 /*
2  Copyright (c) 2019 Equinor ASA
3  Copyroght (c) 2026 OPM-OP AS
4 
5  This file is part of the Open Porous Media project (OPM).
6 
7  OPM is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  OPM is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with OPM. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef OPM_IO_OUTPUTSTREAM_HPP_INCLUDED
22 #define OPM_IO_OUTPUTSTREAM_HPP_INCLUDED
23 
24 #include <opm/io/eclipse/PaddedOutputString.hpp>
25 #include <opm/common/utility/TimeService.hpp>
26 
27 #include <array>
28 #include <chrono>
29 #include <ios>
30 #include <memory>
31 #include <string>
32 #include <vector>
33 
34 namespace Opm { namespace EclIO {
35 
36  class EclOutput;
37 
38 }} // namespace Opm::EclIO
39 
40 namespace Opm { namespace EclIO { namespace OutputStream {
41 
42  struct Formatted { bool set; };
43  struct Unified { bool set; };
44 
46  struct ResultSet
47  {
49  std::string outputDir;
50 
52  std::string baseName;
53  };
54 
56  class Init
57  {
58  public:
66  explicit Init(const ResultSet& rset,
67  const Formatted& fmt);
68 
69  ~Init();
70 
71  Init(const Init& rhs) = delete;
72  Init(Init&& rhs);
73 
74  Init& operator=(const Init& rhs) = delete;
75  Init& operator=(Init&& rhs);
76 
82  void write(const std::string& kw,
83  const std::vector<int>& data);
84 
90  void write(const std::string& kw,
91  const std::vector<bool>& data);
92 
99  void write(const std::string& kw,
100  const std::vector<float>& data);
101 
108  void write(const std::string& kw,
109  const std::vector<double>& data);
110 
117  void write(const std::string& kw,
118  const std::vector<PaddedOutputString<8>>& data);
119 
120 
122  void message(const std::string& msg);
123 
124  private:
126  std::unique_ptr<EclOutput> stream_;
127 
136  void open(const std::string& fname,
137  const bool formatted);
138 
140  EclOutput& stream();
141 
143  template <typename T>
144  void writeImpl(const std::string& kw,
145  const std::vector<T>& data);
146  };
147 
149  class Restart
150  {
151  public:
169  explicit Restart(const ResultSet& rset,
170  const int seqnum,
171  const Formatted& fmt,
172  const Unified& unif);
173 
174  ~Restart();
175 
176  Restart(const Restart& rhs) = delete;
177  Restart(Restart&& rhs);
178 
179  Restart& operator=(const Restart& rhs) = delete;
180  Restart& operator=(Restart&& rhs);
181 
186  void message(const std::string& msg);
187 
193  void write(const std::string& kw,
194  const std::vector<int>& data);
195 
201  void write(const std::string& kw,
202  const std::vector<bool>& data);
203 
210  void write(const std::string& kw,
211  const std::vector<float>& data);
212 
219  void write(const std::string& kw,
220  const std::vector<double>& data);
221 
227  void write(const std::string& kw,
228  const std::vector<std::string>& data);
229 
236  void write(const std::string& kw,
237  const std::vector<PaddedOutputString<8>>& data);
238 
239  private:
241  std::unique_ptr<EclOutput> stream_;
242 
255  void openUnified(const std::string& fname,
256  const bool formatted,
257  const int seqnum);
258 
268  void openNew(const std::string& fname,
269  const bool formatted);
270 
281  void openExisting(const std::string& fname,
282  const bool formatted,
283  const std::streampos writePos);
284 
288  EclOutput& stream();
289 
291  template <typename T>
292  void writeImpl(const std::string& kw,
293  const std::vector<T>& data);
294  };
295 
297  class RFT
298  {
299  public:
300  struct OpenExisting { bool set; };
301 
311  explicit RFT(const ResultSet& rset,
312  const Formatted& fmt,
313  const OpenExisting& existing);
314 
315  ~RFT();
316 
317  RFT(const RFT& rhs) = delete;
318  RFT(RFT&& rhs);
319 
320  RFT& operator=(const RFT& rhs) = delete;
321  RFT& operator=(RFT&& rhs);
322 
328  void write(const std::string& kw,
329  const std::vector<int>& data);
330 
337  void write(const std::string& kw,
338  const std::vector<float>& data);
339 
346  void write(const std::string& kw,
347  const std::vector<PaddedOutputString<8>>& data);
348 
349  private:
351  std::unique_ptr<EclOutput> stream_;
352 
364  void open(const std::string& fname,
365  const bool formatted,
366  const bool existing);
367 
369  EclOutput& stream();
370 
372  template <typename T>
373  void writeImpl(const std::string& kw,
374  const std::vector<T>& data);
375  };
376 
378  {
379  public:
380  using StartTime = time_point;
381 
382  enum class UnitConvention
383  {
384  Metric = 1,
385  Field = 2,
386  Lab = 3,
387  Pvt_M = 4,
388  };
389 
391  {
392  std::string root;
393  int step;
394  };
395 
397  {
398  public:
399  void add(const std::string& keyword,
400  const std::string& wgname,
401  const int num,
402  const std::string& unit);
403 
404  friend class SummarySpecification;
405 
406  private:
407  std::vector<PaddedOutputString<8>> keywords{};
408  std::vector<PaddedOutputString<8>> wgnames{};
409  std::vector<int> nums{};
410  std::vector<PaddedOutputString<8>> units{};
411  };
412 
413  explicit SummarySpecification(const ResultSet& rset,
414  const Formatted& fmt,
415  const UnitConvention uconv,
416  const std::array<int,3>& cartDims,
417  const RestartSpecification& restart,
418  const StartTime start,
419  const StartTime computeStart);
420 
422 
423  SummarySpecification(const SummarySpecification& rhs) = delete;
425 
426  SummarySpecification& operator=(const SummarySpecification& rhs) = delete;
427  SummarySpecification& operator=(SummarySpecification&& rhs);
428 
436  void write(const Parameters& params, const bool simulationFinished,
437  const int currentStep, const int basic);
438 
439  private:
440  int unit_;
441  int restartStep_;
442  std::array<int,3> cartDims_;
443  StartTime startDate_;
445  StartTime computeStart_;
446  std::vector<PaddedOutputString<8>> restart_;
447 
449  std::unique_ptr<EclOutput> stream_;
450 
451  void rewindStream();
452  void flushStream();
453 
454  EclOutput& stream();
455  };
456 
457  std::unique_ptr<EclOutput>
458  createSummaryFile(const ResultSet& rset,
459  const int seqnum,
460  const Formatted& fmt,
461  const Unified& unif);
462 
476  std::string outputFileName(const ResultSet& rsetDescriptor,
477  const std::string& ext);
478 
479 }}} // namespace Opm::EclIO::OutputStream
480 
481 #endif // OPM_IO_OUTPUTSTREAM_HPP_INCLUDED
void write(const Parameters &params, const bool simulationFinished, const int currentStep, const int basic)
Write SMSPEC file.
Definition: OutputStream.cpp:806
void message(const std::string &msg)
Generate a message string (keyword type &#39;MESS&#39;) in underlying output stream.
Definition: OutputStream.cpp:344
Init(const ResultSet &rset, const Formatted &fmt)
Constructor.
Definition: OutputStream.cpp:213
Definition: OutputStream.hpp:377
void write(const std::string &kw, const std::vector< int > &data)
Write integer data to underlying output stream.
Definition: OutputStream.cpp:238
File manager for restart output streams.
Definition: OutputStream.hpp:149
File manager for "init" output streams.
Definition: OutputStream.hpp:56
void message(const std::string &msg)
Definition: OutputStream.cpp:272
Definition: OutputStream.hpp:42
Definition: OutputStream.hpp:43
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
Definition: EclOutput.hpp:38
Definition: OutputStream.hpp:300
File manager for RFT output streams.
Definition: OutputStream.hpp:297
std::string baseName
Base name of simulation run.
Definition: OutputStream.hpp:52
std::string outputDir
Output directory. Commonly "." or location of run&#39;s .DATA file.
Definition: OutputStream.hpp:49
RFT(const ResultSet &rset, const Formatted &fmt, const OpenExisting &existing)
Constructor.
Definition: OutputStream.cpp:491
void write(const std::string &kw, const std::vector< int > &data)
Write integer data to underlying output stream.
Definition: OutputStream.cpp:351
void write(const std::string &kw, const std::vector< int > &data)
Write integer data to underlying output stream.
Definition: OutputStream.cpp:517
Abstract representation of an ECLIPSE-style result set.
Definition: OutputStream.hpp:46
Restart(const ResultSet &rset, const int seqnum, const Formatted &fmt, const Unified &unif)
Constructor.
Definition: OutputStream.cpp:305
Null-terminated, left adjusted, space padded array of N characters.
Definition: PaddedOutputString.hpp:39