My Project
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 <fstream>
22 #include <ios>
23 #include <string>
24 #include <typeinfo>
25 #include <vector>
26 
27 #include <opm/io/eclipse/EclIOdata.hpp>
28 #include <opm/io/eclipse/PaddedOutputString.hpp>
29 #include <iostream>
30 
31 namespace Opm { namespace EclIO { namespace OutputStream {
32  class Restart;
33  class SummarySpecification;
34 }}}
35 
36 namespace Opm { namespace 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  eclArrType arrType = MESS;
50  int element_size = 4;
51 
52  if (typeid(T) == typeid(int))
53  arrType = INTE;
54  else if (typeid(T) == typeid(float))
55  arrType = REAL;
56  else if (typeid(T) == typeid(double)){
57  arrType = DOUB;
58  element_size = 8;
59  } else if (typeid(T) == typeid(bool))
60  arrType = LOGI;
61  else if (typeid(T) == typeid(char))
62  arrType = MESS;
63 
64  if (isFormatted)
65  {
66  writeFormattedHeader(name, data.size(), arrType, element_size);
67  if (arrType != MESS)
68  writeFormattedArray(data);
69  }
70  else
71  {
72  writeBinaryHeader(name, data.size(), arrType, element_size);
73  if (arrType != MESS)
74  writeBinaryArray(data);
75  }
76  }
77 
78  // when this function is used array type will be assumed C0NN (not CHAR).
79  // Also in cases where element size is 8 or less, element size will be 8.
80 
81  void write(const std::string& name, const std::vector<std::string>& data, int element_size);
82 
83  void message(const std::string& msg);
84  void flushStream();
85 
86  void set_ix() { ix_standard = true; }
87 
88  friend class OutputStream::Restart;
90 
91 private:
92  void writeBinaryHeader(const std::string& arrName, int64_t size, eclArrType arrType, int element_size);
93 
94  template <typename T>
95  void writeBinaryArray(const std::vector<T>& data);
96 
97  void writeBinaryCharArray(const std::vector<std::string>& data, int element_size);
98  void writeBinaryCharArray(const std::vector<PaddedOutputString<8>>& data);
99 
100  void writeFormattedHeader(const std::string& arrName, int size, eclArrType arrType, int element_size);
101 
102  template <typename T>
103  void writeFormattedArray(const std::vector<T>& data);
104 
105  void writeFormattedCharArray(const std::vector<std::string>& data, int element_size);
106  void writeFormattedCharArray(const std::vector<PaddedOutputString<8>>& data);
107 
108  void writeArrayType(const eclArrType arrType);
109  std::string make_real_string_ecl(float value) const;
110  std::string make_real_string_ix(float value) const;
111  std::string make_doub_string_ecl(double value) const;
112  std::string make_doub_string_ix(double value) const;
113 
114  bool isFormatted, ix_standard;
115  std::ofstream ofileH;
116 };
117 
118 
119 template<>
120 void EclOutput::write<std::string>(const std::string& name,
121  const std::vector<std::string>& data);
122 
123 template <>
124 void EclOutput::write<PaddedOutputString<8>>
125  (const std::string& name,
126  const std::vector<PaddedOutputString<8>>& data);
127 
128 }} // namespace Opm::EclIO
129 
130 #endif // OPM_IO_ECLOUTPUT_HPP
Definition: EclOutput.hpp:39
File manager for restart output streams.
Definition: OutputStream.hpp:136
Definition: OutputStream.hpp:364
Null-terminated, left adjusted, space padded array of N characters.
Definition: PaddedOutputString.hpp:40
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:29