PeriDEM 0.3.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 - 2026 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 "util/io.h"
16#include <stdexcept>
17#include "constants.h"
18#include "logger.h"
19#include <fstream>
20#include <sstream>
21#include <iostream>
22#include <vector>
23#include <algorithm>
24#include <format>
25#include <string>
26#include <map>
27#include <string_view>
28
29// includes for files that include this file
30#include "inputParser.h"
31
32namespace util {
33
35namespace io {
36
52class Msg {
53public:
55 template <typename T> Msg &operator<<(const T &v) {
56 d_oss << v;
57 return *this;
58 }
59
61 Msg &operator<<(std::ostream &(*manip)(std::ostream &)) {
62 d_oss << manip;
63 return *this;
64 }
65
67 std::string str() const { return d_oss.str(); }
68
70 operator std::string() const { return d_oss.str(); }
71
72private:
74 std::ostringstream d_oss;
75};
76
82inline std::string getTabS(int nt) {
83 std::string tabS = "";
84 for (int i = 0; i < nt; i++)
85 tabS += "\t";
86
87 return tabS;
88};
89
96template <class T> inline std::string printStr(const T &msg, int nt = print_default_tab) {
97
98 std::ostringstream oss;
99 oss << getTabS(nt) << msg;
100 return oss.str();
101};
102
109template <class T> inline std::string printStr(const std::vector<T> &list,
110 int nt = print_default_tab) {
111
112 auto tabS = getTabS(nt);
113 std::ostringstream oss;
114 oss << tabS;
115 size_t i = 0;
116 for (auto l : list) {
117 oss << l;
118 i++;
119 if (i != list.size())
120 oss << ", ";
121 }
122
123 return oss.str();
124};
125
127template <> inline std::string printStr(const std::vector<util::Point> &list,
128 int nt) {
129
130 auto tabS = getTabS(nt);
131 std::ostringstream oss;
132 oss << tabS;
133 size_t i = 0;
134 for (auto l : list) {
135 oss << "(" << l[0] << ", " << l[1] << ", " << l[2] << ")";
136 i++;
137 if (i != list.size())
138 oss << ", ";
139 }
140
141 return oss.str();
142};
143
150 template <typename T, typename U> inline std::string printStr(const std::map<T, U> &map,
151 int nt = print_default_tab) {
152
153 auto tabS = getTabS(nt);
154 std::ostringstream oss;
155 oss << tabS;
156 for (const auto &l : map) {
157 oss << "{" << l.first << " : " << l.second << "}";
158 if (std::next(&l) != map.cend())
159 oss << ", ";
160 }
161
162 return oss.str();
163 };
164
171template <class T> inline void print(const T &msg, int nt = print_default_tab,
172 int printMpiRank = print_default_mpi_rank) {
173 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank)
174 std::cout << printStr(msg, nt);
175};
176
183template <class T> inline void print(const std::vector<T> &list, int nt = print_default_tab,
184 int printMpiRank = print_default_mpi_rank) {
185 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank)
186 std::cout << printStr(list, nt);
187};
188
190template <class T>
191inline std::string printStr(const std::vector<std::vector<T>> &list,
192 int nt = print_default_tab) {
193
194 auto tabS = getTabS(nt);
195 std::ostringstream oss;
196 oss << tabS;
197 size_t i = 0;
198 for (auto l : list) {
199 oss << "(";
200 for (size_t k = 0; k < l.size(); k++) {
201 oss << l[k];
202 if (k < l.size() - 1)
203 oss << ", ";
204 }
205 oss << ")";
206
207 i++;
208 if (i != list.size())
209 oss << ", ";
210 }
211
212 return oss.str();
213}
214
217template <class T> inline void print(const std::vector<std::vector<T>> &list,
218 int nt = print_default_tab,
219 int printMpiRank = print_default_mpi_rank) {
220
221 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank)
222 std::cout << printStr(list, nt);
223};
224
231inline std::string printBoxStr(const std::pair<util::Point, util::Point>
232 &box, int nt = print_default_tab) {
233 auto tabS = getTabS(nt);
234 std::ostringstream oss;
235 oss << tabS << "Corner point 1 = " << box.first.printStr(nt, 0) << std::endl;
236 oss << tabS << "Corner point 2 = " << box.second.printStr(nt, 0) << std::endl;
237
238 return oss.str();
239};
240
247inline void printBox(const std::pair<util::Point, util::Point> &box,
248 int nt = print_default_tab,
249 int printMpiRank = print_default_mpi_rank) {
250 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank)
251 std::cout << printBoxStr(box, nt);
252};
253
256inline std::string printBoxStr(const std::pair<std::vector<double>, std::vector<double>> &box,
257 int nt = print_default_tab) {
258 auto tabS = getTabS(nt);
259 std::ostringstream oss;
260 oss << tabS << "Corner point 1 = (" << printStr<double>(box.first, 0)
261 << ")" << std::endl;
262 oss << tabS << "Corner point 2 = (" << printStr<double>(box.second, 0)
263 << ")" << std::endl;
264
265 return oss.str();
266};
267
271inline void printBox(const std::pair<std::vector<double>, std::vector<double>> &box,
272 int nt = print_default_tab, int printMpiRank = print_default_mpi_rank) {
273 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank)
274 std::cout << printBoxStr(box, nt);
275};
276
282void initLogger(int debug_level = logger_default_debug_lvl, std::string filename = "");
283
291void log(std::ostringstream &oss, bool screen_out = false,
292 int printMpiRank = print_default_mpi_rank);
293
301void log(const std::string &str, bool screen_out = false,
302 int printMpiRank = print_default_mpi_rank);
303
307bool logEnabled(int priority, bool check_condition = true,
308 int override_priority = -1);
309
313void log(int priority, const std::string &str, bool check_condition = true,
314 int override_priority = -1, bool screen_out = false);
315
316void log(int priority, std::ostringstream &oss, bool check_condition = true,
317 int override_priority = -1, bool screen_out = false);
318
327inline std::string getFilenameFromPath(std::string const & path, std::string const & delims = "/\\")
328{
329 return path.substr(path.find_last_of(delims) + 1);
330}
331
339inline std::string removeExtensionFromFile(std::string const & filename)
340{
341 typename std::string::size_type const p(filename.find_last_of('.'));
342 return p > 0 && p != std::string::npos ? filename.substr(0, p) : filename;
343}
344
351inline std::string getExtensionFromFile(std::string const & filename)
352{
353 typename std::string::size_type const p(filename.find_last_of('.'));
354
355 //return filename.substr(p + 1);
356 return p > 0 && p != std::string::npos ? filename.substr(p+1) : "";
357}
358
366inline std::string checkAndCreateNewFilename(std::string const & filename, std::string filename_ext)
367{
368 auto f_ext = util::io::getExtensionFromFile(filename);
369 std::string f = filename;
370 if (f_ext.empty()) {
371 f = f + "." + filename_ext;
372 }
373 else {
374 if (f_ext != filename_ext) {
375 throw std::runtime_error(
377 << "checkAndCreateNewFilename(): Argument filename = "
378 << filename << " has extension = "
379 << f_ext << " which does not match expected extension = "
380 << filename_ext << std::endl);
381 }
382 }
383
384 return f;
385}
386
393inline bool isFileEmpty(std::string filename) {
394 std::ifstream pFile(filename);
395 return pFile.peek() == std::ifstream::traits_type::eof();
396}
397
404inline bool isFileEmpty(std::ifstream& pFile) {
405 return pFile.peek() == std::ifstream::traits_type::eof();
406}
407
408} // namespace io
409
410} // namespace util
411
412#endif // UTILS_UTIL_IO_H
Collects a message with stream syntax for use in an exception.
Definition io.h:52
Msg & operator<<(std::ostream &(*manip)(std::ostream &))
Appends a stream manipulator such as std::endl.
Definition io.h:61
std::ostringstream d_oss
Message under construction.
Definition io.h:74
Msg & operator<<(const T &v)
Appends a value to the message.
Definition io.h:55
std::string str() const
The message built so far.
Definition io.h:67
bool logEnabled(int priority, bool check_condition=true, int override_priority=-1)
True if a message at this priority would be logged (uses LoggerDeck::d_debugLevel)
Definition io.cpp:55
std::string printBoxStr(const std::pair< util::Point, util::Point > &box, int nt=print_default_tab)
Returns formatted string for output.
Definition io.h:231
std::string getTabS(int nt)
Returns tab spaces of a given size.
Definition io.h:82
std::string printStr(const T &msg, int nt=print_default_tab)
Returns formatted string for output.
Definition io.h:96
const int print_default_tab
Default value of tab used in outputting formatted information.
Definition constants.h:18
void print(const T &msg, int nt=print_default_tab, int printMpiRank=print_default_mpi_rank)
Prints formatted information.
Definition io.h:171
const int logger_default_debug_lvl
Default debug level for logger.
Definition constants.h:24
std::string getExtensionFromFile(std::string const &filename)
Get extension from the filename.
Definition io.h:351
void initLogger(int debug_level=logger_default_debug_lvl, std::string filename="")
Initializes the logger.
Definition io.cpp:18
bool isFileEmpty(std::string filename)
Check if file is empty or null.
Definition io.h:393
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:366
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:247
std::string removeExtensionFromFile(std::string const &filename)
Remove extension from the filename Source - https://stackoverflow.com/a/24386991.
Definition io.h:339
void log(std::ostringstream &oss, bool screen_out=false, int printMpiRank=print_default_mpi_rank)
Global method to log the message.
Definition io.cpp:41
const int print_default_mpi_rank
Default mpi rank in printing information.
Definition constants.h:21
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:327
int mpiRank()
get rank (id) of this processor
Collection of methods useful in simulation.
Definition constants.h:14