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/** @file utils.cpp
2 *
3 * @author Cory Alexander Balaton (coryab)
4 * @author Janita Ovidie Sandtrøen Willumsen (janitaws)
5 *
6 * @version 1.0
7 *
8 * @brief Implementation of the utils
9 *
10 * @bug No known bugs
11 * */
12#include "utils.hpp"
13
14namespace 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
32bool 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
63std::string concatpath(const std::string &left, const std::string &right)
64{
65 if (left.back() != '/' and right.front() != '/') {
66 return left + '/' + right;
67 }
68 else {
69 return left + right;
70 }
71}
72
73} // namespace utils
bool mkpath(std::string path, int mode=0777)
Make path given.
Definition: utils.cpp:32
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
std::string scientific_format(const std::vector< double > &v, int width=20, int prec=10)
Turns a vector of doubles into a string written in scientific format.
Definition: utils.cpp:23
std::string concatpath(const std::string &left, const std::string &right)
Take 2 strings and concatenate them and make sure there is a directory separator (/) between them.
Definition: utils.cpp:63
std::string dirname(const std::string &path)
Get the directory name of the path.
Definition: utils.cpp:58