PeriDEM 0.2.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
util::io Namespace Reference

Provides geometrical methods such as point inside rectangle. More...

Data Structures

class  InputParser
 Input command line argument parser. More...
 
class  Logger
 Prints log to std::cout and also write to the file. More...
 
struct  LoggerDeck
 Deck to store log parameters. More...
 

Functions

std::string getTabS (int nt)
 Returns tab spaces of given size.
 
template<class T >
std::string printStr (const T &msg, int nt=print_default_tab)
 Returns formatted string for output.
 
template<class T >
std::string printStr (const std::vector< T > &list, int nt=print_default_tab)
 Returns formatted string for output.
 
template<>
std::string printStr (const std::vector< util::Point > &list, int nt)
 Returns formatted string for output.
 
template<class T >
void print (const T &msg, int nt=print_default_tab, int printMpiRank=print_default_mpi_rank)
 Prints formatted information.
 
template<class T >
void print (const std::vector< T > &list, int nt=print_default_tab, int printMpiRank=print_default_mpi_rank)
 Prints formatted information.
 
template<class T >
std::string printStr (const std::vector< std::vector< T > > &list, int nt=print_default_tab)
 Returns formatted string for output.
 
template<class T >
void print (const std::vector< std::vector< T > > &list, int nt=print_default_tab, int printMpiRank=print_default_mpi_rank)
 Prints formatted information.
 
std::string printBoxStr (const std::pair< util::Point, util::Point > &box, int nt=print_default_tab)
 Returns formatted string for output.
 
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.
 
std::string printBoxStr (const std::pair< std::vector< double >, std::vector< double > > &box, int nt=print_default_tab)
 Returns formatted string for output.
 
void printBox (const std::pair< std::vector< double >, std::vector< double > > &box, int nt=print_default_tab, int printMpiRank=print_default_mpi_rank)
 Prints formatted string for output.
 
void initLogger (int debug_level=logger_default_debug_lvl, std::string filename="")
 Initializes the logger.
 
void log (std::ostringstream &oss, bool screen_out=false, int printMpiRank=print_default_mpi_rank)
 Global method to log the message.
 
void log (const std::string &str, bool screen_out=false, int printMpiRank=print_default_mpi_rank)
 Global method to log the message.
 
std::string getFilenameFromPath (std::string const &path, std::string const &delims="/\\")
 Get filename removing path from the string Source - https://stackoverflow.com/a/24386991.
 
std::string removeExtensionFromFile (std::string const &filename)
 Remove extension from the filename Source - https://stackoverflow.com/a/24386991.
 
std::string getExtensionFromFile (std::string const &filename)
 Get extension from the filename.
 
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.
 
bool isFileEmpty (std::string filename)
 Check if file is empty or null.
 
bool isFileEmpty (std::ifstream &pFile)
 Check if file is empty or null.
 

Variables

const int print_default_tab = 0
 Default value of tab used in outputting formatted information.
 
const int print_default_mpi_rank = 0
 Default mpi rank in printing information.
 
const int logger_default_debug_lvl = 5
 Default debug level for logger.
 

Detailed Description

Provides geometrical methods such as point inside rectangle.

Function Documentation

◆ checkAndCreateNewFilename()

std::string util::io::checkAndCreateNewFilename ( std::string const &  filename,
std::string  filename_ext 
)
inline

Check for extension and if possible create new filename from a given filename and a given extension.

Parameters
filenameFilename with/without extension
filename_extExtension to check for and append if needed
Returns
string New filename if successful

Definition at line 443 of file io.h.

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}
std::string getExtensionFromFile(std::string const &filename)
Get extension from the filename.
Definition io.h:428

References getExtensionFromFile().

Referenced by rw::writer::MshWriter::MshWriter(), rw::reader::VtkParticleReader::VtkParticleReader(), and rw::reader::VtkReader::VtkReader().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getExtensionFromFile()

std::string util::io::getExtensionFromFile ( std::string const &  filename)
inline

Get extension from the filename.

Parameters
filenameFilename with extension
Returns
string Extension

Definition at line 428 of file io.h.

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}

Referenced by checkAndCreateNewFilename(), fe::Mesh::createData(), and fe::Mesh::readElementData().

Here is the caller graph for this function:

◆ getFilenameFromPath()

std::string util::io::getFilenameFromPath ( std::string const &  path,
std::string const &  delims = "/\\" 
)
inline

Get filename removing path from the string Source - https://stackoverflow.com/a/24386991.

Parameters
pathFilename with path
delimsDelimiter to separate the filename from the path
Returns
string Filename

Definition at line 404 of file io.h.

405{
406 return path.substr(path.find_last_of(delims) + 1);
407}

Referenced by test::testGraphPartitioning(), and test::testMPI().

Here is the caller graph for this function:

◆ getTabS()

std::string util::io::getTabS ( int  nt)
inline

Returns tab spaces of given size.

Parameters
ntNumber of tabs
Returns
string Tab spaces

Definition at line 40 of file io.h.

40 {
41 std::string tabS = "";
42 for (int i = 0; i < nt; i++)
43 tabS += "\t";
44
45 return tabS;
46};

Referenced by model::DEMModel::output(), printBoxStr(), printBoxStr(), printStr(), printStr(), printStr(), printStr(), material::BaseInfluenceFn::printStr(), material::Material::printStr(), util::geometry::GeomObject::printStr(), material::ConstInfluenceFn::printStr(), material::LinearInfluenceFn::printStr(), material::GaussianInfluenceFn::printStr(), material::RnpMaterial::printStr(), material::PmbMaterial::printStr(), material::PdElastic::printStr(), material::PdState::printStr(), util::geometry::NullGeomObject::printStr(), util::geometry::Line::printStr(), util::geometry::Triangle::printStr(), util::geometry::Square::printStr(), util::geometry::Rectangle::printStr(), util::geometry::Hexagon::printStr(), util::geometry::Drum2D::printStr(), util::geometry::Cube::printStr(), util::geometry::Cuboid::printStr(), util::geometry::Circle::printStr(), util::geometry::Sphere::printStr(), util::geometry::Cylinder::printStr(), util::geometry::AnnulusGeomObject::printStr(), util::geometry::ComplexGeomObject::printStr(), fe::Mesh::printStr(), fe::QuadData::printStr(), geometry::Fracture::printStr(), inp::MatData::printStr(), inp::MaterialDeck::printStr(), inp::MeshDeck::printStr(), inp::ModelDeck::printStr(), inp::OutputDeck::printStr(), inp::ContactPairDeck::printStr(), inp::ContactDeck::printStr(), inp::ParticleDeck::printStr(), inp::PBCData::printStr(), inp::PICDeck::printStr(), inp::PNeighborDeck::printStr(), inp::Zone::printStr(), inp::ParticleZone::printStr(), particle::BaseParticle::printStr(), particle::ParticleTransform::printStr(), particle::RefParticle::printStr(), and util::geometry::GeomData::printStr().

◆ initLogger()

void util::io::initLogger ( int  debug_level = logger_default_debug_lvl,
std::string  filename = "" 
)

Initializes the logger.

Parameters
debug_levelSpecify debug level/verbosity
filenameSpecify filename for logs

Definition at line 15 of file io.cpp.

15 {
16
17 if (logger_p != nullptr)
18 return;
19
20 auto deck = new LoggerDeck(debug_level, filename);
21 logger_p = new Logger(deck);
22}
Prints log to std::cout and also write to the file.
Definition io.h:251
util::io::Logger * logger_p
Definition io.cpp:12
Deck to store log parameters.
Definition io.h:217

References anonymous_namespace{io.cpp}::logger_p.

Referenced by model::DEMModel::DEMModel(), and main().

Here is the caller graph for this function:

◆ isFileEmpty() [1/2]

bool util::io::isFileEmpty ( std::ifstream &  pFile)
inline

Check if file is empty or null.

Parameters
pFileFile stream
Returns
bool True if file is empty

Definition at line 480 of file io.h.

480 {
481 return pFile.peek() == std::ifstream::traits_type::eof();
482}

◆ isFileEmpty() [2/2]

bool util::io::isFileEmpty ( std::string  filename)
inline

Check if file is empty or null.

Parameters
filenameFilename with/without extension
Returns
bool True if file is empty

Definition at line 469 of file io.h.

469 {
470 std::ifstream pFile(filename);
471 return pFile.peek() == std::ifstream::traits_type::eof();
472}

Referenced by rw::reader::MshReader::readCells(), rw::reader::MshReader::readMesh(), and rw::reader::MshReader::readNodes().

Here is the caller graph for this function:

◆ log() [1/2]

void util::io::log ( const std::string &  str,
bool  screen_out = false,
int  printMpiRank = print_default_mpi_rank 
)

Global method to log the message.

Parameters
strMessage
screen_outSpecify if it goes to std::cout as well
printMpiRankMPI rank to do log/print. Negative value means all ranks log.

Definition at line 24 of file io.cpp.

24 {
25
26 // for now, we do not call assert and rather create a logger if it does not exist
27 /*
28 //assert((logger_p != nullptr) && "logger_p "
29 // "(pointer of type util::io::Logger) is not initialized. "
30 // "Call util::io::initLogger(debug_level, log_filename) once at the beginning.");
31 */
32 if (logger_p == nullptr)
33 logger_p = new Logger();
34
35 logger_p->log(str, screen_out, printMpiRank);
36}
void log(std::ostringstream &oss, bool screen_out=false, int printMpiRank=print_default_mpi_rank)
Log the message.
Definition io.h:282

References util::io::Logger::log(), and anonymous_namespace{io.cpp}::logger_p.

Here is the call graph for this function:

◆ log() [2/2]

void util::io::log ( std::ostringstream &  oss,
bool  screen_out = false,
int  printMpiRank = print_default_mpi_rank 
)

Global method to log the message.

Parameters
ossMessage
screen_outSpecify if it goes to std::cout as well
printMpiRankMPI rank to do log/print. Negative value means all ranks log.

Definition at line 38 of file io.cpp.

38 {
39
40 // for now, we do not call assert and rather create a logger if it does not exist
41 /*
42 //assert((logger_p != nullptr) && "logger_p "
43 // "(pointer of type util::io::Logger) is not initialized. "
44 // "Call util::io::initLogger(debug_level, log_filename) once at the beginning.");
45 */
46 if (logger_p == nullptr)
47 logger_p = new Logger();
48
49 logger_p->log(oss, screen_out, printMpiRank);
50}

References util::io::Logger::log(), and anonymous_namespace{io.cpp}::logger_p.

Referenced by fe::Mesh::createData(), model::DEMModel::log(), model::DEMModel::log(), and fe::Mesh::readElementData().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print() [1/3]

template<class T >
void util::io::print ( const std::vector< std::vector< T > > &  list,
int  nt = print_default_tab,
int  printMpiRank = print_default_mpi_rank 
)
inline

Prints formatted information.

Parameters
listList of objects
ntNumber of tabs
printMpiRankMPI rank to do log/print. Negative value means all ranks log.

Definition at line 154 of file io.h.

156 {
157
158 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank)
159 std::cout << printStr(list, nt);
160};
std::string printStr(const T &msg, int nt=print_default_tab)
Returns formatted string for output.
Definition io.h:54
int mpiRank()
get rank (id) of this processor

References util::parallel::mpiRank(), and printStr().

Here is the call graph for this function:

◆ print() [2/3]

template<class T >
void util::io::print ( const std::vector< T > &  list,
int  nt = print_default_tab,
int  printMpiRank = print_default_mpi_rank 
)
inline

Prints formatted information.

Parameters
listList of objects
ntNumber of tabs
printMpiRankMPI rank to do log/print. Negative value means all ranks log.

Definition at line 120 of file io.h.

121 {
122 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank)
123 std::cout << printStr(list, nt);
124};

References util::parallel::mpiRank(), and printStr().

Here is the call graph for this function:

◆ print() [3/3]

template<class T >
void util::io::print ( const T &  msg,
int  nt = print_default_tab,
int  printMpiRank = print_default_mpi_rank 
)
inline

Prints formatted information.

Parameters
msgMessage
ntNumber of tabs
printMpiRankMPI rank to do log/print. Negative value means all ranks log.

Definition at line 108 of file io.h.

109 {
110 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank)
111 std::cout << printStr(msg, nt);
112};

References util::parallel::mpiRank(), and printStr().

Referenced by anonymous_namespace{testParallelCompLib.cpp}::exchangeDispData(), main(), main(), test::testMPI(), and test::testTaskflow().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ printBox() [1/2]

void util::io::printBox ( const std::pair< std::vector< double >, std::vector< double > > &  box,
int  nt = print_default_tab,
int  printMpiRank = print_default_mpi_rank 
)
inline

Prints formatted string for output.

Parameters
boxPair of corner points of box
ntNumber of tabs to prefix
printMpiRankMPI rank to do log/print. Negative value means all ranks log.

Definition at line 208 of file io.h.

209 {
210 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank)
211 std::cout << printBoxStr(box, nt);
212};
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

References util::parallel::mpiRank(), and printBoxStr().

Here is the call graph for this function:

◆ printBox() [2/2]

void util::io::printBox ( const std::pair< util::Point, util::Point > &  box,
int  nt = print_default_tab,
int  printMpiRank = print_default_mpi_rank 
)
inline

Prints formatted string for output.

Parameters
boxPair of corner points of box
ntNumber of tabs to prefix
printMpiRankMPI rank to do log/print. Negative value means all ranks log.

Definition at line 184 of file io.h.

186 {
187 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank)
188 std::cout << printBoxStr(box, nt);
189};

References util::parallel::mpiRank(), and printBoxStr().

Here is the call graph for this function:

◆ printBoxStr() [1/2]

std::string util::io::printBoxStr ( const std::pair< std::vector< double >, std::vector< double > > &  box,
int  nt = print_default_tab 
)
inline

Returns formatted string for output.

Parameters
boxPair of corner points of box
ntNumber of tabs to prefix
Returns
string Formatted string

Definition at line 193 of file io.h.

194 {
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};
std::string getTabS(int nt)
Returns tab spaces of given size.
Definition io.h:40

References getTabS().

Here is the call graph for this function:

◆ printBoxStr() [2/2]

std::string util::io::printBoxStr ( const std::pair< util::Point, util::Point > &  box,
int  nt = print_default_tab 
)
inline

Returns formatted string for output.

Parameters
boxPair of corner points of box
ntNumber of tabs to prefix
Returns
string Formatted string

Definition at line 168 of file io.h.

169 {
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};

References getTabS().

Referenced by printBox(), printBox(), util::geometry::Line::printStr(), util::geometry::Square::printStr(), util::geometry::Rectangle::printStr(), util::geometry::Cube::printStr(), util::geometry::Cuboid::printStr(), util::geometry::Circle::printStr(), util::geometry::Sphere::printStr(), util::geometry::Cylinder::printStr(), util::geometry::AnnulusGeomObject::printStr(), util::geometry::ComplexGeomObject::printStr(), and fe::Mesh::printStr().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ printStr() [1/4]

template<class T >
std::string util::io::printStr ( const std::vector< std::vector< T > > &  list,
int  nt = print_default_tab 
)
inline

Returns formatted string for output.

Parameters
listList of objects
ntNumber of tabs to prefix
Returns
string Formatted string

Definition at line 128 of file io.h.

129 {
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}

References getTabS().

Here is the call graph for this function:

◆ printStr() [2/4]

template<class T >
std::string util::io::printStr ( const std::vector< T > &  list,
int  nt = print_default_tab 
)
inline

Returns formatted string for output.

Parameters
listList of objects
ntNumber of tabs to prefix
Returns
string Formatted string

Definition at line 67 of file io.h.

68 {
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};

References getTabS().

Here is the call graph for this function:

◆ printStr() [3/4]

template<>
std::string util::io::printStr ( const std::vector< util::Point > &  list,
int  nt 
)
inline

Returns formatted string for output.

Parameters
listList of objects
ntNumber of tabs to prefix
Returns
string Formatted string

Definition at line 85 of file io.h.

86 {
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};

References getTabS().

Here is the call graph for this function:

◆ printStr() [4/4]

template<class T >
std::string util::io::printStr ( const T &  msg,
int  nt = print_default_tab 
)
inline

Returns formatted string for output.

Parameters
msgMessage
ntNumber of tabs to prefix
Returns
string Formatted string

Definition at line 54 of file io.h.

54 {
55
56 std::ostringstream oss;
57 oss << getTabS(nt) << msg;
58 return oss.str();
59};

References getTabS().

Referenced by util::checkMatrix(), fe::Mesh::computeVol(), model::DEMModel::createGeometryAtSite(), util::geometry::createGeomObjectOld(), model::DEMModel::createParticlesFromFile(), model::DEMModel::output(), print(), print(), print(), anonymous_namespace{geomObjects.cpp}::printErrMsg(), anonymous_namespace{geomObjectsUitl.cpp}::printErrMsg(), util::geometry::Triangle::printStr(), util::geometry::Square::printStr(), util::geometry::Rectangle::printStr(), util::geometry::Hexagon::printStr(), util::geometry::Drum2D::printStr(), util::geometry::Cube::printStr(), util::geometry::Cuboid::printStr(), fe::QuadData::printStr(), util::geometry::GeomData::printStr(), and test::testTetElem().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeExtensionFromFile()

std::string util::io::removeExtensionFromFile ( std::string const &  filename)
inline

Remove extension from the filename Source - https://stackoverflow.com/a/24386991.

Parameters
filenameFilename with extension
Returns
string Filename without extension

Definition at line 416 of file io.h.

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}

Referenced by test::testGraphPartitioning(), and test::testMPI().

Here is the caller graph for this function:

Variable Documentation

◆ logger_default_debug_lvl

const int util::io::logger_default_debug_lvl = 5

Default debug level for logger.

Definition at line 33 of file io.h.

◆ print_default_mpi_rank

const int util::io::print_default_mpi_rank = 0

Default mpi rank in printing information.

Definition at line 30 of file io.h.

◆ print_default_tab

const int util::io::print_default_tab = 0

Default value of tab used in outputting formatted information.

Definition at line 27 of file io.h.

Referenced by anonymous_namespace{testParallelCompLib.cpp}::exchangeDispData(), and test::testMPI().