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/io/eclipse/SummaryNode.hpp>
26 #include <opm/common/utility/TimeService.hpp>
27 
28 #include <array>
29 #include <chrono>
30 #include <ios>
31 #include <memory>
32 #include <optional>
33 #include <string>
34 #include <vector>
35 
36 namespace Opm { namespace EclIO {
37 
38  class EclOutput;
39 
40 }} // namespace Opm::EclIO
41 
42 namespace Opm { namespace EclIO { namespace OutputStream {
43 
44  struct Formatted { bool set; };
45  struct Unified { bool set; };
46 
48  struct ResultSet
49  {
51  std::string outputDir;
52 
54  std::string baseName;
55  };
56 
58  class Init
59  {
60  public:
68  explicit Init(const ResultSet& rset,
69  const Formatted& fmt);
70 
71  ~Init();
72 
73  Init(const Init& rhs) = delete;
74  Init(Init&& rhs);
75 
76  Init& operator=(const Init& rhs) = delete;
77  Init& operator=(Init&& rhs);
78 
84  void write(const std::string& kw,
85  const std::vector<int>& data);
86 
92  void write(const std::string& kw,
93  const std::vector<bool>& data);
94 
101  void write(const std::string& kw,
102  const std::vector<float>& data);
103 
110  void write(const std::string& kw,
111  const std::vector<double>& data);
112 
119  void write(const std::string& kw,
120  const std::vector<PaddedOutputString<8>>& data);
121 
122 
124  void message(const std::string& msg);
125 
126  private:
128  std::unique_ptr<EclOutput> stream_;
129 
138  void open(const std::string& fname,
139  const bool formatted);
140 
142  EclOutput& stream();
143 
145  template <typename T>
146  void writeImpl(const std::string& kw,
147  const std::vector<T>& data);
148  };
149 
151  class Restart
152  {
153  public:
171  explicit Restart(const ResultSet& rset,
172  const int seqnum,
173  const Formatted& fmt,
174  const Unified& unif);
175 
176  ~Restart();
177 
178  Restart(const Restart& rhs) = delete;
179  Restart(Restart&& rhs);
180 
181  Restart& operator=(const Restart& rhs) = delete;
182  Restart& operator=(Restart&& rhs);
183 
188  void message(const std::string& msg);
189 
195  void write(const std::string& kw,
196  const std::vector<int>& data);
197 
203  void write(const std::string& kw,
204  const std::vector<bool>& data);
205 
212  void write(const std::string& kw,
213  const std::vector<float>& data);
214 
221  void write(const std::string& kw,
222  const std::vector<double>& data);
223 
229  void write(const std::string& kw,
230  const std::vector<std::string>& data);
231 
238  void write(const std::string& kw,
239  const std::vector<PaddedOutputString<8>>& data);
240 
241  private:
243  std::unique_ptr<EclOutput> stream_;
244 
257  void openUnified(const std::string& fname,
258  const bool formatted,
259  const int seqnum);
260 
270  void openNew(const std::string& fname,
271  const bool formatted);
272 
283  void openExisting(const std::string& fname,
284  const bool formatted,
285  const std::streampos writePos);
286 
290  EclOutput& stream();
291 
293  template <typename T>
294  void writeImpl(const std::string& kw,
295  const std::vector<T>& data);
296  };
297 
299  class RFT
300  {
301  public:
302  struct OpenExisting { bool set; };
303 
313  explicit RFT(const ResultSet& rset,
314  const Formatted& fmt,
315  const OpenExisting& existing);
316 
317  ~RFT();
318 
319  RFT(const RFT& rhs) = delete;
320  RFT(RFT&& rhs);
321 
322  RFT& operator=(const RFT& rhs) = delete;
323  RFT& operator=(RFT&& rhs);
324 
330  void write(const std::string& kw,
331  const std::vector<int>& data);
332 
339  void write(const std::string& kw,
340  const std::vector<float>& data);
341 
348  void write(const std::string& kw,
349  const std::vector<PaddedOutputString<8>>& data);
350 
351  private:
353  std::unique_ptr<EclOutput> stream_;
354 
366  void open(const std::string& fname,
367  const bool formatted,
368  const bool existing);
369 
371  EclOutput& stream();
372 
374  template <typename T>
375  void writeImpl(const std::string& kw,
376  const std::vector<T>& data);
377  };
378 
380  {
381  public:
382  using StartTime = time_point;
383 
384  enum class UnitConvention
385  {
386  Metric = 1,
387  Field = 2,
388  Lab = 3,
389  Pvt_M = 4,
390  };
391 
393  {
394  std::string root;
395  int step;
396  };
397 
399  {
400  public:
401  void add(const std::string& keyword,
402  const std::string& wgname,
403  const int num,
404  const std::string& unit);
405 
406  void add(const std::string& keyword,
407  const std::string& wgname,
408  const int num,
409  const std::string& unit,
410  const std::optional<Opm::EclIO::lgr_info>& lgr);
411 
421  void setNumber(const std::size_t paramIx,
422  const int num);
423 
424  friend class SummarySpecification;
425 
426  private:
427  std::vector<PaddedOutputString<8>> keywords{};
428  std::vector<PaddedOutputString<8>> wgnames{};
429  std::vector<int> nums{};
430  std::vector<PaddedOutputString<8>> units{};
431  std::vector<PaddedOutputString<8>> lgrs{};
432  std::vector<int> numlx{};
433  std::vector<int> numly{};
434  std::vector<int> numlz{};
435  };
436 
437  explicit SummarySpecification(const ResultSet& rset,
438  const Formatted& fmt,
439  const UnitConvention uconv,
440  const std::array<int,3>& cartDims,
441  const RestartSpecification& restart,
442  const StartTime start,
443  const StartTime computeStart);
444 
446 
447  SummarySpecification(const SummarySpecification& rhs) = delete;
449 
450  SummarySpecification& operator=(const SummarySpecification& rhs) = delete;
451  SummarySpecification& operator=(SummarySpecification&& rhs);
452 
460  void write(const Parameters& params, const bool simulationFinished,
461  const int currentStep, const int basic);
462 
463  private:
464  int unit_;
465  int restartStep_;
466  std::array<int,3> cartDims_;
467  StartTime startDate_;
469  StartTime computeStart_;
470  std::vector<PaddedOutputString<8>> restart_;
471 
473  std::unique_ptr<EclOutput> stream_;
474 
475  void rewindStream();
476  void flushStream();
477  void writeLgrMetadata(const Parameters& params, int currentStep);
478 
479  EclOutput& stream();
480  };
481 
482  std::unique_ptr<EclOutput>
483  createSummaryFile(const ResultSet& rset,
484  const int seqnum,
485  const Formatted& fmt,
486  const Unified& unif);
487 
501  std::string outputFileName(const ResultSet& rsetDescriptor,
502  const std::string& ext);
503 
504 }}} // namespace Opm::EclIO::OutputStream
505 
506 #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:861
void message(const std::string &msg)
Generate a message string (keyword type &#39;MESS&#39;) in underlying output stream.
Definition: OutputStream.cpp:346
Init(const ResultSet &rset, const Formatted &fmt)
Constructor.
Definition: OutputStream.cpp:215
Definition: OutputStream.hpp:379
void write(const std::string &kw, const std::vector< int > &data)
Write integer data to underlying output stream.
Definition: OutputStream.cpp:240
File manager for restart output streams.
Definition: OutputStream.hpp:151
File manager for "init" output streams.
Definition: OutputStream.hpp:58
void message(const std::string &msg)
Definition: OutputStream.cpp:274
Definition: OutputStream.hpp:44
Definition: OutputStream.hpp:45
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
Definition: EclOutput.hpp:39
Definition: OutputStream.hpp:302
void setNumber(const std::size_t paramIx, const int num)
Assign NUMBER value of a single summary vector.
Definition: OutputStream.cpp:793
File manager for RFT output streams.
Definition: OutputStream.hpp:299
std::string baseName
Base name of simulation run.
Definition: OutputStream.hpp:54
std::string outputDir
Output directory. Commonly "." or location of run&#39;s .DATA file.
Definition: OutputStream.hpp:51
RFT(const ResultSet &rset, const Formatted &fmt, const OpenExisting &existing)
Constructor.
Definition: OutputStream.cpp:493
void write(const std::string &kw, const std::vector< int > &data)
Write integer data to underlying output stream.
Definition: OutputStream.cpp:353
void write(const std::string &kw, const std::vector< int > &data)
Write integer data to underlying output stream.
Definition: OutputStream.cpp:519
Abstract representation of an ECLIPSE-style result set.
Definition: OutputStream.hpp:48
Restart(const ResultSet &rset, const int seqnum, const Formatted &fmt, const Unified &unif)
Constructor.
Definition: OutputStream.cpp:307
Null-terminated, left adjusted, space padded array of N characters.
Definition: PaddedOutputString.hpp:39