EclFile.hpp
Go to the documentation of this file.
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_ECLFILE_HPP
20#define OPM_IO_ECLFILE_HPP
21
23
25
26#include <ios>
27#include <string>
28#include <stdexcept>
29#include <tuple>
30#include <unordered_map>
31#include <vector>
32
33namespace Opm { namespace EclIO {
34
36{
37public:
38 struct Formatted {
39 bool value;
40 };
41
42 explicit EclFile(const std::string& filename, bool preload = false);
43 EclFile(const std::string& filename, Formatted fmt, bool preload = false);
44 bool formattedInput() const { return formatted; }
45
46 void loadData(); // load all data
47 void loadData(const std::string& arrName); // load all arrays with array name equal to arrName
48 void loadData(int arrIndex); // load data based on array indices in vector arrIndex
49 void loadData(const std::vector<int>& arrIndex); // load data based on array indices in vector arrIndex
50
51 void clearData()
52 {
53 inte_array.clear();
54 real_array.clear();
55 doub_array.clear();
56 logi_array.clear();
57 char_array.clear();
58 }
59
60 using EclEntry = std::tuple<std::string, eclArrType, int64_t>;
61 std::vector<EclEntry> getList() const;
62
63 const std::vector<int>& getElementSizeList() const { return array_element_size; }
64
65 template <typename T>
66 const std::vector<T>& get(int arrIndex);
67
68 template <typename T>
69 const std::vector<T>& get(const std::string& name);
70
71 bool hasKey(const std::string &name) const;
72 std::size_t count(const std::string& name) const;
73
74 const std::vector<std::string>& arrayNames() const { return array_name; }
75 std::size_t size() const;
76 bool is_ix() const;
77
78protected:
81
82 std::unordered_map<int, std::vector<int>> inte_array;
83 std::unordered_map<int, std::vector<bool>> logi_array;
84 std::unordered_map<int, std::vector<double>> doub_array;
85 std::unordered_map<int, std::vector<float>> real_array;
86 std::unordered_map<int, std::vector<std::string>> char_array;
87
88 std::vector<std::string> array_name;
89 std::vector<eclArrType> array_type;
90 std::vector<int64_t> array_size;
91 std::vector<int> array_element_size;
92
93 std::vector<uint64_t> ifStreamPos;
94
95 std::map<std::string, int> array_index;
96
97 template<class T>
98 const std::vector<T>& getImpl(int arrIndex, eclArrType type,
99 const std::unordered_map<int, std::vector<T>>& array,
100 const std::string& typeStr)
101 {
102 if (array_type[arrIndex] != type) {
103 std::string message = "Array with index " + std::to_string(arrIndex) + " is not of type " + typeStr;
104 OPM_THROW(std::runtime_error, message);
105 }
106
107 if (!arrayLoaded[arrIndex]) {
108 loadData(arrIndex);
109 }
110
111 return array.at(arrIndex);
112 }
113
114 std::streampos
115 seekPosition(const std::vector<std::string>::size_type arrIndex) const;
116
117private:
118 std::vector<bool> arrayLoaded;
119
120 void loadBinaryArray(std::fstream& fileH, std::size_t arrIndex);
121 void loadFormattedArray(const std::string& fileStr, std::size_t arrIndex, int64_t fromPos);
122 void load(bool preload);
123
124 std::vector<unsigned int> get_bin_logi_raw_values(int arrIndex) const;
125 std::vector<std::string> get_fmt_real_raw_str_values(int arrIndex) const;
126
127};
128
129}} // namespace Opm::EclIO
130
131#endif // OPM_IO_ECLFILE_HPP
#define OPM_THROW(Exception, message)
Definition: ErrorMacros.hpp:52
const char *const name
Definition: cJSON.h:258
int cJSON_bool fmt
Definition: cJSON.h:158
const char *const string
Definition: cJSON.h:170
Definition: EclFile.hpp:36
std::unordered_map< int, std::vector< std::string > > char_array
Definition: EclFile.hpp:86
bool formatted
Definition: EclFile.hpp:79
std::vector< int64_t > array_size
Definition: EclFile.hpp:90
EclFile(const std::string &filename, Formatted fmt, bool preload=false)
void loadData(const std::vector< int > &arrIndex)
bool hasKey(const std::string &name) const
std::vector< std::string > array_name
Definition: EclFile.hpp:88
std::vector< int > array_element_size
Definition: EclFile.hpp:91
std::size_t count(const std::string &name) const
std::vector< eclArrType > array_type
Definition: EclFile.hpp:89
void clearData()
Definition: EclFile.hpp:51
std::vector< EclEntry > getList() const
std::tuple< std::string, eclArrType, int64_t > EclEntry
Definition: EclFile.hpp:60
const std::vector< int > & getElementSizeList() const
Definition: EclFile.hpp:63
std::vector< uint64_t > ifStreamPos
Definition: EclFile.hpp:93
std::unordered_map< int, std::vector< double > > doub_array
Definition: EclFile.hpp:84
std::string inputFilename
Definition: EclFile.hpp:80
std::unordered_map< int, std::vector< int > > inte_array
Definition: EclFile.hpp:82
std::map< std::string, int > array_index
Definition: EclFile.hpp:95
std::size_t size() const
std::unordered_map< int, std::vector< float > > real_array
Definition: EclFile.hpp:85
void loadData(int arrIndex)
bool formattedInput() const
Definition: EclFile.hpp:44
const std::vector< T > & get(int arrIndex)
std::streampos seekPosition(const std::vector< std::string >::size_type arrIndex) const
const std::vector< T > & get(const std::string &name)
bool is_ix() const
EclFile(const std::string &filename, bool preload=false)
void loadData(const std::string &arrName)
const std::vector< std::string > & arrayNames() const
Definition: EclFile.hpp:74
std::unordered_map< int, std::vector< bool > > logi_array
Definition: EclFile.hpp:83
const std::vector< T > & getImpl(int arrIndex, eclArrType type, const std::unordered_map< int, std::vector< T > > &array, const std::string &typeStr)
Definition: EclFile.hpp:98
eclArrType
Definition: EclIOdata.hpp:28
Definition: A.hpp:4
Definition: EclFile.hpp:38
bool value
Definition: EclFile.hpp:39