libROM  v1.0
Data-driven physical simulation library
CSVDatabase.cpp
1 
11 // Description: The concrete database implementation using CSV.
12 
13 #include "CSVDatabase.h"
14 #include "Utilities.h"
15 #include <vector>
16 #include <complex>
17 #include <iomanip>
18 
19 namespace CAROM {
20 
22 {
23 }
24 
26 {
27 }
28 
29 bool
31  const std::string& file_name,
32  const MPI_Comm comm)
33 {
34  Database::create(file_name, comm);
35  return true;
36 }
37 
38 bool
40  const std::string& file_name,
41  const std::string& type,
42  const MPI_Comm comm)
43 {
44  Database::open(file_name, type, comm);
45  return true;
46 }
47 
48 bool
50 {
51  return true;
52 }
53 
54 void
56  const std::string& file_name,
57  const int* const data,
58  int nelements,
59  const bool distributed)
60 {
61  CAROM_VERIFY(!file_name.empty());
62  CAROM_VERIFY(data != nullptr);
63  CAROM_VERIFY(nelements > 0);
64 
65  std::ofstream d_fs(file_name.c_str());
66  for (int i = 0; i < nelements; ++i)
67  {
68  d_fs << data[i] << std::endl;
69  }
70  d_fs.close();
71 }
72 
73 void
75  const std::string& file_name,
76  const double* const data,
77  int nelements,
78  const bool distributed)
79 {
80  CAROM_VERIFY(!file_name.empty());
81  CAROM_VERIFY(data != nullptr);
82  CAROM_VERIFY(nelements > 0);
83 
84  std::ofstream d_fs(file_name.c_str());
85  d_fs << std::setprecision(16) << std::fixed;
86  for (int i = 0; i < nelements; ++i)
87  {
88  d_fs << data[i] << std::endl;
89  }
90  d_fs.close();
91 }
92 
93 void
95  const std::string& file_name,
96  const std::vector<double>& data,
97  int nelements,
98  const bool distributed)
99 {
100  CAROM_VERIFY(!file_name.empty());
101  CAROM_VERIFY(nelements > 0);
102  CAROM_VERIFY(data.size() == nelements);
103 
104  std::ofstream d_fs(file_name.c_str());
105  d_fs << std::setprecision(16) << std::fixed;
106  for (int i = 0; i < nelements; ++i)
107  {
108  d_fs << data[i] << std::endl;
109  }
110  d_fs.close();
111 }
112 
113 void
115  const std::string& file_name,
116  const std::vector<std::complex<double>>& data,
117  int nelements)
118 {
119  CAROM_VERIFY(!file_name.empty());
120  CAROM_VERIFY(nelements > 0);
121  CAROM_VERIFY(data.size() == nelements);
122 
123  std::ofstream d_fs(file_name.c_str());
124  d_fs << std::setprecision(16) << std::fixed;
125  for (int i = 0; i < nelements; ++i)
126  {
127  d_fs << std::real(data[i]) << "," << std::imag(data[i]) << std::endl;
128  }
129  d_fs.close();
130 }
131 
132 void
134  const std::string& file_name,
135  const std::vector<std::string>& data,
136  int nelements)
137 {
138  CAROM_VERIFY(!file_name.empty());
139  CAROM_VERIFY(nelements > 0);
140  CAROM_ASSERT(data != nullptr);
141 
142  std::ofstream d_fs(file_name.c_str());
143  for (int i = 0; i < nelements; ++i)
144  {
145  d_fs << data[i] << std::endl;
146  }
147  d_fs.close();
148 }
149 
150 void
152  const std::string& file_name,
153  int* data,
154  int nelements,
155  const bool distributed)
156 {
157  CAROM_VERIFY(!file_name.empty());
158 #ifndef DEBUG_CHECK_ASSERTIONS
159  CAROM_NULL_USE(nelements);
160 #endif
161 
162  std::ifstream d_fs(file_name.c_str());
163  if (d_fs.fail())
164  return;
165  CAROM_VERIFY(!d_fs.fail());
166  int data_entry = 0;
167  for (int i = 0; i < nelements; ++i)
168  {
169  d_fs >> data_entry;
170  data[i] = data_entry;
171  }
172  d_fs.close();
173 }
174 
175 void
177  const std::string& file_name,
178  std::vector<int> &data,
179  bool append)
180 {
181  CAROM_VERIFY(!file_name.empty());
182  if (!append) data.clear();
183 
184  std::ifstream d_fs(file_name.c_str());
185  int data_entry;
186  while (d_fs >> data_entry)
187  {
188  data.push_back(data_entry);
189  }
190  d_fs.close();
191 }
192 
193 void
195  const std::string& file_name,
196  double* data,
197  int nelements,
198  const bool distributed)
199 {
200  CAROM_VERIFY(!file_name.empty());
201 #ifndef DEBUG_CHECK_ASSERTIONS
202  CAROM_NULL_USE(nelements);
203 #endif
204 
205  std::ifstream d_fs(file_name.c_str());
206  CAROM_VERIFY(!d_fs.fail());
207  double data_entry = 0.0;
208  for (int i = 0; i < nelements; ++i)
209  {
210  d_fs >> data_entry;
211  data[i] = data_entry;
212  }
213  d_fs.close();
214 }
215 
216 void
218  const std::string& file_name,
219  double* data,
220  int nelements,
221  const std::vector<int>& idx,
222  const bool distributed)
223 {
224  CAROM_VERIFY(!file_name.empty());
225 #ifndef DEBUG_CHECK_ASSERTIONS
226  CAROM_NULL_USE(nelements);
227 #endif
228 
229  if (idx.size() == 0)
230  {
231  getDoubleArray(file_name, data, nelements);
232  }
233  else
234  {
235  std::ifstream d_fs(file_name.c_str());
236  CAROM_VERIFY(!d_fs.fail());
237  int k = 0;
238  double data_entry = 0.0;
239  for (int i = 0; i < nelements; ++i)
240  {
241  d_fs >> data_entry;
242  if (idx[k] == i)
243  {
244  data[k++] = data_entry;
245  }
246  if (k == idx.size())
247  {
248  break;
249  }
250  }
251  CAROM_VERIFY(k == idx.size());
252  d_fs.close();
253  }
254 }
255 
256 void
258  const std::string& file_name,
259  double* data,
260  int nelements,
261  int offset,
262  int block_size,
263  int stride,
264  const bool distributed)
265 {
266  CAROM_VERIFY(!file_name.empty());
267 #ifndef DEBUG_CHECK_ASSERTIONS
268  CAROM_NULL_USE(nelements);
269 #endif
270 
271  std::ifstream d_fs(file_name.c_str());
272  CAROM_VERIFY(!d_fs.fail());
273  std::string line, data_entry;
274  int count = 0;
275  int curr_block_remaining = block_size;
276  while (count < nelements && d_fs >> line)
277  {
278  std::stringstream d_ss(line);
279  while (std::getline(d_ss, data_entry, ','))
280  {
281  if (offset > 0)
282  {
283  offset--;
284  }
285  else
286  {
287  data[count++] = std::stod(data_entry);
288  curr_block_remaining--;
289  if (curr_block_remaining == 0)
290  {
291  offset = stride - 1;
292  curr_block_remaining = block_size;
293  }
294  }
295  }
296  }
297  d_fs.close();
298 }
299 
300 void
302  const std::string& file_name,
303  std::vector<double> &data,
304  bool append)
305 {
306  CAROM_VERIFY(!file_name.empty());
307  if (!append) data.clear();
308 
309  std::ifstream d_fs(file_name.c_str());
310  double data_entry;
311  while (d_fs >> data_entry)
312  {
313  data.push_back(data_entry);
314  }
315  d_fs.close();
316 }
317 
318 void
320  const std::string& file_name,
321  std::vector<std::string> &data,
322  bool append)
323 {
324  CAROM_VERIFY(!file_name.empty());
325  if (!append) data.clear();
326 
327  std::ifstream d_fs(file_name.c_str());
328  std::string data_entry;
329  while (std::getline(d_fs, data_entry))
330  {
331  data.push_back(data_entry);
332  }
333  d_fs.close();
334 }
335 
336 int
338  const std::string& file_name)
339 {
340  int count = 0;
341  CAROM_VERIFY(!file_name.empty());
342 
343  std::ifstream d_fs(file_name.c_str());
344  std::string data_entry;
345  while (std::getline(d_fs, data_entry))
346  {
347  count += 1;
348  }
349  return count;
350 }
351 
352 }
void getIntegerVector(const std::string &file_name, std::vector< int > &data, bool append=false)
Reads a vector of integers associated with the supplied filename.
void putDoubleVector(const std::string &file_name, const std::vector< double > &data, int nelements, const bool distributed=false) override
Writes a vector of doubles associated with the supplied filename to the currently open CSV database f...
Definition: CSVDatabase.cpp:94
void putIntegerArray(const std::string &file_name, const int *const data, int nelements, const bool distributed=false) override
Writes an array of integers associated with the supplied filename.
Definition: CSVDatabase.cpp:55
void getIntegerArray(const std::string &file_name, int *data, int nelements, const bool distributed=false) override
Reads an array of integers associated with the supplied filename.
int getLineCount(const std::string &file_name)
Count the number of lines of CSV database file.
CSVDatabase()
Default constructor.
Definition: CSVDatabase.cpp:21
bool create(const std::string &file_name, const MPI_Comm comm=MPI_COMM_NULL) override
Creates a new CSV database file with the supplied name. NOTE: CSVDatabase does not actually create a ...
Definition: CSVDatabase.cpp:30
bool close()
Closes the currently open CSV database file.
Definition: CSVDatabase.cpp:49
virtual ~CSVDatabase()
Destructor.
Definition: CSVDatabase.cpp:25
void getStringVector(const std::string &file_name, std::vector< std::string > &data, bool append=false)
Reads a vector of strings associated with the supplied filename.
void getDoubleVector(const std::string &file_name, std::vector< double > &data, bool append=false)
Reads a vector of doubles associated with the supplied filename.
void putDoubleArray(const std::string &file_name, const double *const data, int nelements, const bool distributed=false) override
Writes an array of doubles associated with the supplied filename.
Definition: CSVDatabase.cpp:74
void getDoubleArray(const std::string &file_name, double *data, int nelements, const bool distributed=false) override
Reads an array of doubles associated with the supplied filename.
void putStringVector(const std::string &file_name, const std::vector< std::string > &data, int nelements)
Writes a vector of strings associated with the supplied filename.
void putComplexVector(const std::string &file_name, const std::vector< std::complex< double >> &data, int nelements)
Writes a vector of complex doubles associated with the supplied filename.
bool open(const std::string &file_name, const std::string &type, const MPI_Comm comm=MPI_COMM_NULL) override
Opens an existing CSV database file with the supplied name. NOTE: CSVDatabase does not actually open ...
Definition: CSVDatabase.cpp:39
virtual bool create(const std::string &file_name, const MPI_Comm comm=MPI_COMM_NULL)
Creates a new database file with the supplied name.
Definition: Database.cpp:29
virtual bool open(const std::string &file_name, const std::string &type, const MPI_Comm comm=MPI_COMM_NULL)
Opens an existing database file with the supplied name.
Definition: Database.cpp:38