opm-common
EclOutput.hpp
1 /*
2  Copyright 2019 Statoil 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 #ifndef OPM_IO_ECLOUTPUT_HPP
19 #define OPM_IO_ECLOUTPUT_HPP
20 
21 #include <opm/io/eclipse/EclIOdata.hpp>
22 #include <opm/io/eclipse/PaddedOutputString.hpp>
23 
24 #include <fstream>
25 #include <ios>
26 #include <stdexcept>
27 #include <string>
28 #include <type_traits>
29 #include <vector>
30 
31 namespace Opm::EclIO::OutputStream {
32  class Restart;
33  class SummarySpecification;
34 } // namespace Opm::EclIO::OutputStream
35 
36 namespace Opm::EclIO {
37 
38 class EclOutput
39 {
40 public:
41  EclOutput(const std::string& filename,
42  const bool formatted,
43  const std::ios_base::openmode mode = std::ios::out);
44 
45  template<typename T>
46  void write(const std::string& name,
47  const std::vector<T>& data)
48  {
49  static_assert(std::is_same_v<T, int> ||
50  std::is_same_v<T, float> ||
51  std::is_same_v<T, double> ||
52  std::is_same_v<T, bool> ||
53  std::is_same_v<T, char>,
54  "EclOutput::write<T>: T must be int, float, double, bool, or char");
55 
56  eclArrType arrType = MESS;
57  int element_size = 4;
58 
59  if constexpr (std::is_same_v<T, int>) {
60  arrType = INTE;
61  }
62  else if constexpr (std::is_same_v<T, float>) {
63  arrType = REAL;
64  }
65  else if constexpr (std::is_same_v<T, double>) {
66  arrType = DOUB;
67  element_size = 8;
68  }
69  else if constexpr (std::is_same_v<T, bool>) {
70  arrType = LOGI;
71  }
72  else if constexpr (std::is_same_v<T, char>) {
73  if (!data.empty()) {
74  throw std::invalid_argument {
75  "EclOutput::write<char>: non-empty data is not supported; "
76  "use message() for MESS-type records"
77  };
78  }
79  }
80 
81  if (isFormatted) {
82  writeFormattedHeader(name, data.size(), arrType, element_size);
83  if (arrType != MESS) {
84  writeFormattedArray(data);
85  }
86  }
87  else {
88  writeBinaryHeader(name, data.size(), arrType, element_size);
89  if (arrType != MESS) {
90  writeBinaryArray(data);
91  }
92  }
93  }
94 
95  // when this function is used array type will be assumed C0NN (not CHAR).
96  // Also in cases where element size is 8 or less, element size will be 8.
97 
98  void write(const std::string& name, const std::vector<std::string>& data, int element_size);
99 
100  void message(const std::string& msg);
101  void flushStream();
102 
103  void set_ix() { ix_standard = true; }
104 
105  friend class OutputStream::Restart;
107 
108 private:
109  void writeBinaryHeader(const std::string& arrName, int64_t size, eclArrType arrType, int element_size);
110 
111  template <typename T>
112  void writeBinaryArray(const std::vector<T>& data);
113 
114  void writeBinaryCharArray(const std::vector<std::string>& data, int element_size);
115  void writeBinaryCharArray(const std::vector<PaddedOutputString<8>>& data);
116 
117  void writeFormattedHeader(const std::string& arrName, int size, eclArrType arrType, int element_size);
118 
119  template <typename T>
120  void writeFormattedArray(const std::vector<T>& data);
121 
122  void writeFormattedCharArray(const std::vector<std::string>& data, int element_size);
123  void writeFormattedCharArray(const std::vector<PaddedOutputString<8>>& data);
124 
125  void writeArrayType(const eclArrType arrType);
126  std::string make_real_string_ecl(float value) const;
127  std::string make_real_string_ix(float value) const;
128  std::string make_doub_string_ecl(double value) const;
129  std::string make_doub_string_ix(double value) const;
130 
131  bool isFormatted, ix_standard;
132  std::ofstream ofileH;
133 };
134 
135 
136 template<>
137 void EclOutput::write<std::string>(const std::string& name,
138  const std::vector<std::string>& data);
139 
140 template <>
141 void EclOutput::write<PaddedOutputString<8>>
142  (const std::string& name,
143  const std::vector<PaddedOutputString<8>>& data);
144 
145 } // namespace Opm::EclIO
146 
147 #endif // OPM_IO_ECLOUTPUT_HPP
Definition: OutputStream.hpp:377
File manager for restart output streams.
Definition: OutputStream.hpp:149
Definition: EclOutput.hpp:38
Definition: EclipseGrid.hpp:42
Definition: EclOutput.hpp:31
Null-terminated, left adjusted, space padded array of N characters.
Definition: PaddedOutputString.hpp:39