2 Dimensional Ising Model Simulate the change in energy and magnetization in a ferro magnet

Loading...
Searching...
No Matches
utils.cpp
Go to the documentation of this file.
1
12#include "utils.hpp"
13
14 namespace utils {
15
16std::string scientific_format(double d, int width, int prec)
17{
18 std::stringstream ss;
19 ss << std::setw(width) << std::setprecision(prec) << std::scientific << d;
20 return ss.str();
21}
22
23std::string scientific_format(const std::vector<double> &v, int width, int prec)
24{
25 std::stringstream ss;
26 for (double elem : v) {
27 ss << scientific_format(elem, width, prec);
28 }
29 return ss.str();
30}
31
32 bool mkpath(std::string path, int mode)
33{
34 std::string cur_dir;
35 std::string::size_type pos = -1;
36 struct stat buf;
37
38 if (path.back() != '/') {
39 path += '/';
40 }
41 while (true) {
42 pos++;
43 pos = path.find('/', pos);
44 if (pos != std::string::npos) {
45 cur_dir = path.substr(0, pos);
46 if (mkdir(cur_dir.c_str(), mode) != 0
47 && stat(cur_dir.c_str(), &buf) != 0) {
48 return -1;
49 }
50 }
51 else {
52 break;
53 }
54 }
55 return 0;
56}
57
58std::string dirname(const std::string &path)
59{
60 return path.substr(0, path.find_last_of("/"));
61}
62
63} // namespace utils
utils.hpp
Function prototypes and macros that are useful.
utils::mkpath
bool mkpath(std::string path, int mode=0777)
Make path given.
Definition: utils.cpp:32
utils::scientific_format
std::string scientific_format(double d, int width=20, int prec=10)
Turns a double into a string written in scientific format.
Definition: utils.cpp:16
utils::dirname
std::string dirname(const std::string &path)
Get the directory name of the path.
Definition: utils.cpp:58