PeriDEM 0.2.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
io.h
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2024 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#ifndef UTILS_UTIL_IO_H
12#define UTILS_UTIL_IO_H
13
14#include "point.h"
15#include "parallelUtil.h" // to make prints MPI aware
16#include <fstream>
17#include <iostream>
18#include <vector>
19#include <algorithm>
20
21namespace util {
22
24namespace io {
25
27const int print_default_tab = 0;
28
31
34
40inline std::string getTabS(int nt) {
41 std::string tabS = "";
42 for (int i = 0; i < nt; i++)
43 tabS += "\t";
44
45 return tabS;
46};
47
54template <class T> inline std::string printStr(const T &msg, int nt = print_default_tab) {
55
56 std::ostringstream oss;
57 oss << getTabS(nt) << msg;
58 return oss.str();
59};
60
67template <class T> inline std::string printStr(const std::vector<T> &list,
68 int nt = print_default_tab) {
69
70 auto tabS = getTabS(nt);
71 std::ostringstream oss;
72 oss << tabS;
73 size_t i = 0;
74 for (auto l : list) {
75 oss << l;
76 i++;
77 if (i != list.size())
78 oss << ", ";
79 }
80
81 return oss.str();
82};
83
85template <> inline std::string printStr(const std::vector<util::Point> &list,
86 int nt) {
87
88 auto tabS = getTabS(nt);
89 std::ostringstream oss;
90 oss << tabS;
91 size_t i = 0;
92 for (auto l : list) {
93 oss << "(" << l[0] << ", " << l[1] << ", " << l[2] << ")";
94 i++;
95 if (i != list.size())
96 oss << ", ";
97 }
98
99 return oss.str();
100};
101
108template <class T> inline void print(const T &msg, int nt = print_default_tab,
109 int printMpiRank = print_default_mpi_rank) {
110 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank)
111 std::cout << printStr(msg, nt);
112};
113
120template <class T> inline void print(const std::vector<T> &list, int nt = print_default_tab,
121 int printMpiRank = print_default_mpi_rank) {
122 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank)
123 std::cout << printStr(list, nt);
124};
125
127template <class T>
128inline std::string printStr(const std::vector<std::vector<T>> &list,
129 int nt = print_default_tab) {
130
131 auto tabS = getTabS(nt);
132 std::ostringstream oss;
133 oss << tabS;
134 size_t i = 0;
135 for (auto l : list) {
136 oss << "(";
137 for (size_t k = 0; k < l.size(); k++) {
138 oss << l[k];
139 if (k < l.size() - 1)
140 oss << ", ";
141 }
142 oss << ")";
143
144 i++;
145 if (i != list.size())
146 oss << ", ";
147 }
148
149 return oss.str();
150}
151
154template <class T> inline void print(const std::vector<std::vector<T>> &list,
155 int nt = print_default_tab,
156 int printMpiRank = print_default_mpi_rank) {
157
158 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank)
159 std::cout << printStr(list, nt);
160};
161
168inline std::string printBoxStr(const std::pair<util::Point, util::Point>
169 &box, int nt = print_default_tab) {
170 auto tabS = getTabS(nt);
171 std::ostringstream oss;
172 oss << tabS << "Corner point 1 = " << box.first.printStr(nt, 0) << std::endl;
173 oss << tabS << "Corner point 2 = " << box.second.printStr(nt, 0) << std::endl;
174
175 return oss.str();
176};
177
184inline void printBox(const std::pair<util::Point, util::Point> &box,
185 int nt = print_default_tab,
186 int printMpiRank = print_default_mpi_rank) {
187 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank)
188 std::cout << printBoxStr(box, nt);
189};
190
193inline std::string printBoxStr(const std::pair<std::vector<double>, std::vector<double>> &box,
194 int nt = print_default_tab) {
195 auto tabS = getTabS(nt);
196 std::ostringstream oss;
197 oss << tabS << "Corner point 1 = (" << printStr<double>(box.first, 0)
198 << ")" << std::endl;
199 oss << tabS << "Corner point 2 = (" << printStr<double>(box.second, 0)
200 << ")" << std::endl;
201
202 return oss.str();
203};
204
208inline void printBox(const std::pair<std::vector<double>, std::vector<double>> &box,
209 int nt = print_default_tab, int printMpiRank = print_default_mpi_rank) {
210 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank)
211 std::cout << printBoxStr(box, nt);
212};
213
218
221
223 std::string d_filename;
224
227
230
235
242 LoggerDeck(int debug_level, std::string filename)
243 : d_debugLevel(debug_level), d_filename(filename), d_printScreen
244 (d_debugLevel > 0), d_printFile(!d_filename.empty()) {}
245};
246
247
251class Logger {
252
253public:
254
260 Logger(LoggerDeck *deck = nullptr) : d_deck_p(deck) {
261
262 if (d_deck_p == nullptr)
263 d_deck_p = new LoggerDeck();
264 }
265
270
272 d_logFile.close();
273 }
274
282 void log(std::ostringstream &oss, bool screen_out = false,
283 int printMpiRank = print_default_mpi_rank) {
284
285 log(oss.str(), screen_out, printMpiRank);
286
287 // reset oss
288 oss.str("");
289 oss.clear();
290 };
291
299 void log(const std::string &str, bool screen_out = false,
300 int printMpiRank = print_default_mpi_rank) {
301
302 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank) {
303 if (d_deck_p->d_printScreen || screen_out)
304 std::cout << str << std::flush;
305
306 // log
307 if (d_deck_p->d_printFile) {
308 d_logFile.open(d_deck_p->d_filename, std::ios_base::app);
309 d_logFile << str;
310 d_logFile.close();
311 }
312 }
313 };
314
317
319 std::ofstream d_logFile;
320};
321
327void initLogger(int debug_level = logger_default_debug_lvl, std::string filename = "");
328
336void log(std::ostringstream &oss, bool screen_out = false,
337 int printMpiRank = print_default_mpi_rank);
338
346void log(const std::string &str, bool screen_out = false,
347 int printMpiRank = print_default_mpi_rank);
348
356public:
362 InputParser(int &argc, char **argv) {
363 for (int i = 1; i < argc; ++i)
364 this->tokens.push_back(std::string(argv[i]));
365 }
366
372 const std::string &getCmdOption(const std::string &option) const {
373 std::vector<std::string>::const_iterator itr;
374 itr = std::find(this->tokens.begin(), this->tokens.end(), option);
375 if (itr != this->tokens.end() && ++itr != this->tokens.end())
376 return *itr;
377
378 static const std::string empty_string("");
379 return empty_string;
380 }
381
387 bool cmdOptionExists(const std::string &option) const {
388 return std::find(this->tokens.begin(), this->tokens.end(), option) != this->tokens.end();
389 }
390
391private:
393 std::vector<std::string> tokens;
394};
395
404inline std::string getFilenameFromPath(std::string const & path, std::string const & delims = "/\\")
405{
406 return path.substr(path.find_last_of(delims) + 1);
407}
408
416inline std::string removeExtensionFromFile(std::string const & filename)
417{
418 typename std::string::size_type const p(filename.find_last_of('.'));
419 return p > 0 && p != std::string::npos ? filename.substr(0, p) : filename;
420}
421
428inline std::string getExtensionFromFile(std::string const & filename)
429{
430 typename std::string::size_type const p(filename.find_last_of('.'));
431
432 //return filename.substr(p + 1);
433 return p > 0 && p != std::string::npos ? filename.substr(p+1) : "";
434}
435
443inline std::string checkAndCreateNewFilename(std::string const & filename, std::string filename_ext)
444{
445 auto f_ext = util::io::getExtensionFromFile(filename);
446 std::string f = filename;
447 if (f_ext.empty()) {
448 f = f + "." + filename_ext;
449 }
450 else {
451 if (f_ext != filename_ext) {
452 std::cerr << "checkAndCreateNewFilename(): Argument filename = "
453 << filename << " has extension = "
454 << f_ext << " which does not match expected extension = "
455 << filename_ext << std::endl;
456 exit(EXIT_FAILURE);
457 }
458 }
459
460 return f;
461}
462
469inline bool isFileEmpty(std::string filename) {
470 std::ifstream pFile(filename);
471 return pFile.peek() == std::ifstream::traits_type::eof();
472}
473
480inline bool isFileEmpty(std::ifstream& pFile) {
481 return pFile.peek() == std::ifstream::traits_type::eof();
482}
483
484} // namespace io
485
486} // namespace util
487
488#endif // UTILS_UTIL_IO_H
Input command line argument parser.
Definition io.h:355
std::vector< std::string > tokens
Tokens.
Definition io.h:393
InputParser(int &argc, char **argv)
Constructor.
Definition io.h:362
bool cmdOptionExists(const std::string &option) const
Check if argument exists.
Definition io.h:387
const std::string & getCmdOption(const std::string &option) const
Get value of argument specified by key.
Definition io.h:372
Prints log to std::cout and also write to the file.
Definition io.h:251
Logger(LoggerDeck *deck=nullptr)
Constructor.
Definition io.h:260
std::ofstream d_logFile
Filestream for logging.
Definition io.h:319
void log(std::ostringstream &oss, bool screen_out=false, int printMpiRank=print_default_mpi_rank)
Log the message.
Definition io.h:282
LoggerDeck * d_deck_p
Pointer to logger deck.
Definition io.h:316
void log(const std::string &str, bool screen_out=false, int printMpiRank=print_default_mpi_rank)
Log the message.
Definition io.h:299
~Logger()
Destructor.
Definition io.h:269
std::string printBoxStr(const std::pair< util::Point, util::Point > &box, int nt=print_default_tab)
Returns formatted string for output.
Definition io.h:168
std::string getTabS(int nt)
Returns tab spaces of given size.
Definition io.h:40
std::string printStr(const T &msg, int nt=print_default_tab)
Returns formatted string for output.
Definition io.h:54
const int print_default_tab
Default value of tab used in outputting formatted information.
Definition io.h:27
void print(const T &msg, int nt=print_default_tab, int printMpiRank=print_default_mpi_rank)
Prints formatted information.
Definition io.h:108
const int logger_default_debug_lvl
Default debug level for logger.
Definition io.h:33
std::string getExtensionFromFile(std::string const &filename)
Get extension from the filename.
Definition io.h:428
void initLogger(int debug_level=logger_default_debug_lvl, std::string filename="")
Initializes the logger.
Definition io.cpp:15
bool isFileEmpty(std::string filename)
Check if file is empty or null.
Definition io.h:469
std::string checkAndCreateNewFilename(std::string const &filename, std::string filename_ext)
Check for extension and if possible create new filename from a given filename and a given extension.
Definition io.h:443
void printBox(const std::pair< util::Point, util::Point > &box, int nt=print_default_tab, int printMpiRank=print_default_mpi_rank)
Prints formatted string for output.
Definition io.h:184
std::string removeExtensionFromFile(std::string const &filename)
Remove extension from the filename Source - https://stackoverflow.com/a/24386991.
Definition io.h:416
void log(std::ostringstream &oss, bool screen_out=false, int printMpiRank=print_default_mpi_rank)
Global method to log the message.
Definition io.cpp:38
const int print_default_mpi_rank
Default mpi rank in printing information.
Definition io.h:30
std::string getFilenameFromPath(std::string const &path, std::string const &delims="/\\")
Get filename removing path from the string Source - https://stackoverflow.com/a/24386991.
Definition io.h:404
int mpiRank()
get rank (id) of this processor
Collection of methods useful in simulation.
Deck to store log parameters.
Definition io.h:217
bool d_printFile
Print to file?
Definition io.h:229
LoggerDeck(int debug_level, std::string filename)
Constructor.
Definition io.h:242
std::string d_filename
Filename to print log.
Definition io.h:223
int d_debugLevel
Debug level.
Definition io.h:220
LoggerDeck()
Constructor.
Definition io.h:234
bool d_printScreen
Print to std::cout?
Definition io.h:226