SLIDE  3.0.0
A simulator for lithium-ion battery pack degradation
Loading...
Searching...
No Matches
read_CSVfiles.hpp
Go to the documentation of this file.
1/*
2 * ReadCSVfiles.hpp
3 *
4 * groups functions for reading csv files into arrays and matrices
5 *
6 * Copyright (c) 2019, The Chancellor, Masters and Scholars of the University
7 * of Oxford, VITO nv, and the 'Slide' Developers.
8 * See the licence file LICENCE.txt for more information.
9 */
10
11#pragma once
12
13#include "../../types/matrix.hpp"
14#include "../util_debug.hpp"
15
16#include <string>
17#include <iostream>
18#include <fstream>
19#include <cassert>
20#include <sstream>
21#include <vector>
22#include <map>
23#include <span>
24#include <cstring>
25
26namespace slide {
27template <typename Tpath>
28std::string getFileContents(const Tpath &name)
29{
30
31 std::cerr << "NOT IMPLEMENTED YET!\n";
32 throw 1234;
33
35
36 std::ifstream in(name, std::ios::in | std::ios::binary);
37 if (!in.good())
38 {
39 std::cerr << "Error in getFileContents. File " << name << " could not be opened.\n";
40 throw 2;
41 }
42
43 std::ostringstream fileContents;
44 fileContents << in.rdbuf();
45 in.close();
46 return (fileContents.str());
47}
48
49inline auto getFile(std::string name)
50{
51 std::cerr << "NOT IMPLEMENTED YET!\n";
52 throw 1234;
53 static std::map<std::string, std::string> fileMap;
54}
55
56template <typename Tpath, typename T, size_t ROW, size_t COL>
57void loadCSV_mat(const Tpath &name, Matrix<T, ROW, COL> &x)
58{
69 std::ifstream in(name, std::ios_base::in);
70
71 if (!in.good())
72 {
73 std::cerr << "Error in ReadCSVfiles::loadCSV_mat. File " << name << " could not be opened\n";
74 throw 2;
75 }
76
77 char c = '.';
78
79 while (in.peek() <= 32)
80 in >> c;
81
82 for (size_t i = 0; i < ROW; i++) {
84 for (size_t j = 0; j < COL - 1; j++) {
85 in >> x[i][j] >> c;
86 }
87 in >> x[i][COL - 1];
88 }
89
90 in.close();
91}
92
93template <typename Tpath, typename T, size_t ROW>
94void loadCSV_1col(const Tpath &name, std::array<T, ROW> &x)
95{
98 loadCSV_mat(name, temp);
99 std::memcpy(&x, &temp, sizeof temp);
100}
101
102inline void ignoreBOM(std::ifstream &in)
103{
104 char c = '.';
105
106 do {
107 in >> c;
108 } while (c < 45);
109
110 in.putback(c);
111}
112
113template <typename Tpath, typename Tx, typename Ty>
114void loadCSV_2col(const Tpath &name, Tx &x, Ty &y, int n = 0)
115{
116 /*
117 * Reads data from a CSV file with 2 columns
118 *
119 * IN
120 * name the name of the file
121 * n the number of rows to read (if n==0, read all), it is used to read a portion of a *.csv file.
122 *
123 * OUT
124 * x array in which the data from the first column will be put
125 * y array in which the data from the second column will be put
126 *
127 * THROWS
128 * 2 could not open the specified file
129 */
130
131 std::ifstream in(name, std::ios_base::in);
132
133 if (!in.good())
134 {
135 std::cerr << "Error in ReadCSVfiles::loadCSV_2col. File " << name
136 << " could not be opened.\n";
137 throw 2;
138 }
139
140 ignoreBOM(in);
141
142 int j = 0;
143 char c;
144 if constexpr (std::is_same<std::vector<double>, Tx>::value) {
145 x.clear();
146 y.clear();
147
148 double x_i, y_i;
149 while ((n == 0 || j < n) && (in >> x_i >> c >> y_i))
150 {
151 x.push_back(x_i);
152 y.push_back(y_i);
153 j++;
154 }
155 } else {
156 while ((n == 0 || j < n) && (in >> x[j] >> c >> y[j]))
157 j++;
158 }
159}
160
161template <typename Tpath, typename Tx>
162void loadCSV_Ncol(const Tpath &name, DynamicMatrix<Tx> &x, int n = 0)
163{
164 /*
165 * Reads data from a CSV file with 2 columns
166 *
167 * IN
168 * name the name of the file
169 * n the number of rows to read (if n==0, read all), it is used to read a portion of a *.csv file.
170 *
171 * OUT
172 * x array in which the data from the first column will be put
173 * y array in which the data from the second column will be put
174 *
175 * THROWS
176 * 2 could not open the specified file
177 */
178
179 std::ifstream in(name, std::ios_base::in);
180
181 if (!in.good())
182 {
183 std::cerr << "Error in ReadCSVfiles::loadCSV_2col. File " << name
184 << " could not be opened.\n";
185 throw 2;
186 }
187
188 ignoreBOM(in);
189
190 x.data.clear();
191
192 std::string line;
193
194 int n_rows{ 0 };
195 while ((n == 0 || n_rows < n) && std::getline(in, line))
196 {
197 n_rows++;
198 std::istringstream in_line(line);
199 double x_i;
200 char c;
201 while (in_line >> x_i) {
202 x.data.push_back(x_i);
203 in_line >> c;
204 }
205 }
206
207 int n_cols = x.data.size() / n_rows;
208
209 x.reshape(n_cols, n_rows);
210
211 x.data.shrink_to_fit();
212}
213
215{
216 std::vector<double> x_vec, y_vec;
217};
218
220template <typename Tpath>
221void loadCSV_2col(const Tpath &name, std::span<double> &x, std::span<double> &y, int n = 0)
222{
223 /*
224 * Reads data from a CSV file with 2 columns
225 *
226 * IN
227 * name the name of the file
228 * n the number of rows to read (if n==0, read all), it is used to read a portion of a *.csv file.
229 *
230 * OUT
231 * x array in which the data from the first column will be put
232 * y array in which the data from the second column will be put
233 *
234 * THROWS
235 * 2 could not open the specified file
236 */
237
238 static std::map<std::string, XYplain> XYdataMap;
239
240 auto name_str = name.string();
241
242 auto fm = XYdataMap.find(name_str);
243
244 if (fm == XYdataMap.end()) {
245 XYplain xyp{};
246 loadCSV_2col(name, xyp.x_vec, xyp.y_vec);
247
248 XYdataMap[name_str] = std::move(xyp);
249 }
250
251 if (n == 0) {
252 x = std::span<double>(XYdataMap[name_str].x_vec);
253 y = std::span<double>(XYdataMap[name_str].y_vec);
254 } else {
255 x = std::span<double>(&XYdataMap[name_str].x_vec[0], &XYdataMap[name_str].x_vec[0] + n); // #TODO temporary solution using pointers instead of iterators.
256 y = std::span<double>(&XYdataMap[name_str].y_vec[0], &XYdataMap[name_str].y_vec[0] + n);
257 }
258}
259
260} // namespace slide
Definition: DynamicMatrix.hpp:16
void reshape(VarType m_, VarType n_)
Definition: DynamicMatrix.hpp:35
std::vector< Tdata > data
Definition: DynamicMatrix.hpp:22
Slide namespace contains all the types, classes, and functions for the simulation framework.
Definition: Cell.hpp:27
std::array< std::array< T, COL >, ROW > Matrix
See source: http://cpptruths.blogspot.com/2011/10/multi-dimensional-arrays-in-c11....
Definition: matrix.hpp:21
void ignoreBOM(std::ifstream &in)
Definition: read_CSVfiles.hpp:102
void loadCSV_mat(const Tpath &name, Matrix< T, ROW, COL > &x)
Definition: read_CSVfiles.hpp:57
std::string getFileContents(const Tpath &name)
Definition: read_CSVfiles.hpp:28
void loadCSV_2col(Tpath &&name, slide::XYdata_vv &data, int n=0)
Definition: XYdata.hpp:90
auto getFile(std::string name)
Definition: read_CSVfiles.hpp:49
void loadCSV_Ncol(const Tpath &name, DynamicMatrix< Tx > &x, int n=0)
Definition: read_CSVfiles.hpp:162
void loadCSV_1col(const Tpath &name, std::array< T, ROW > &x)
Definition: read_CSVfiles.hpp:94
struct Data
Definition: read_CSVfiles.hpp:215
std::vector< double > x_vec
Definition: read_CSVfiles.hpp:216
std::vector< double > y_vec
Definition: read_CSVfiles.hpp:216