2 Dimensional Ising Model
Simulate the change in energy and magnetization in a ferro magnet
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1/** @file utils.hpp
2 *
3 * @author Cory Alexander Balaton (coryab)
4 * @author Janita Ovidie Sandtrøen Willumsen (janitaws)
5 *
6 * @version 1.0
7 *
8 * @brief Function prototypes and macros that are useful.
9 *
10 * These utility function are mainly for convenience and aren't directly
11 * related to the project. Anything that is in the details namespace should
12 * not be used directly, or else it might cause undefined behavior if not used
13 * correctly.
14 *
15 * @bug No known bugs
16 * */
17#ifndef __UTILS__
18#define __UTILS__
19
20#include <armadillo>
21#include <iomanip>
22#include <sstream>
23#include <string>
24#include <sys/stat.h>
25#include <vector>
26
27/** @def DEBUG(msg)
28 * @brief Writes a debug message
29 *
30 * This macro writes a debug message that includes the filename,
31 * line number, and a custom message. The function is wrapped in an ifdef
32 * that checks if DBG is defined, so one can choose to display the debug
33 * messages by adding the -DDBG flag when compiling.
34 * */
35#ifdef DBG
36#define DEBUG(msg)
37 std::cout << __FILE__ << " " << __LINE__ << ": " << msg << std::endl
38#else
39#define DEBUG(msg)
40#endif
41
42/** @def __METHOD_NAME__
43 * @brief Get the name of the current method/function without the return type.
44 * */
45#define __METHOD_NAME__ details::methodName(__PRETTY_FUNCTION__)
46
47namespace details {
48/** @brief Takes in the __PRETTY_FUNCTION__ string and removes the return type.
49 *
50 * @details This function should only be used for the __METHOD_NAME__ macro,
51 * since it takes the output from __PRETTY_FUNCTION__ and strips the return
52 * type.
53 *
54 * @param pretty_function The string from __PRETTY_FUNCTION__
55 *
56 * @return std::string
57 * */
58inline std::string methodName(const std::string &pretty_function)
59{
60 size_t colons = pretty_function.find("::");
61 size_t begin = pretty_function.substr(0, colons).rfind(" ") + 1;
62 size_t end = pretty_function.rfind("(") - begin;
63
64 return pretty_function.substr(begin, end) + "()";
65}
66
67} // namespace details
68
69namespace utils {
70
71/** @brief Turns a double into a string written in scientific format.
72 *
73 * @details The code is stolen from https://github.com/anderkve/FYS3150.
74 *
75 * @param d The number to stringify
76 * @param width The reserved width of the string
77 * @param prec The precision of the stringified number
78 *
79 * @return std::string
80 * */
81std::string scientific_format(double d, int width = 20, int prec = 10);
82
83/** @brief Turns a vector of doubles into a string written in scientific
84 * format.
85 *
86 * @details The code is stolen from https://github.com/anderkve/FYS3150.
87 *
88 * @param v The vector to stringify
89 * @param width The reserved width of the string
90 * @param prec The precision of the stringified number
91 *
92 * @return std::string
93 * */
94std::string scientific_format(const std::vector<double> &v, int width = 20,
95 int prec = 10);
96
97/** @brief Make path given.
98 *
99 * @details This tries to be the equivalent to "mkdir -p" and creates a new
100 * directory whenever it needs to.
101 *
102 * @param path The path to be created
103 * @param mode The mode/permissions for all the new directories
104 *
105 * @return bool Success/Fail
106 * */
107bool mkpath(std::string path, int mode = 0777);
108
109/** @brief Get the directory name of the path
110 *
111 * @param path The path to use.
112 *
113 * @return string
114 * */
115std::string dirname(const std::string &path);
116
117/** @brief Take 2 strings and concatenate them and make sure there is a
118 * directory separator (/) between them.
119 *
120 * @details This function doesn't care whether or not the values given as
121 * parameters are valid path strings. It is the responsibility of the user to make
122 * sure that the values given are valid path strings.
123 * The function only guarantees that the output string is a valid path string.
124 *
125 * @param left The left hand side of the result string
126 * @param right The right hand side of the result string
127 *
128 * @return string
129 * */
130std::string concatpath(const std::string &left, const std::string &right);
131
132} // namespace utils
133
134#endif
#define UP
Used for the neighbor matrix in the class.
Definition: IsingModel.hpp:26
#define INDEX(I, N)
I modulo N.
Definition: IsingModel.hpp:23
#define DOWN
Used for the neighbor matrix in the class.
Definition: IsingModel.hpp:28
#define LEFT
Used for the neighbor matrix in the class.
Definition: IsingModel.hpp:27
#define RIGHT
Used for the neighbor matrix in the class.
Definition: IsingModel.hpp:29
Test class for the Ising model.
Definition: test_suite.cpp:36
The Ising model in 2 dimensions.
Definition: IsingModel.hpp:36
std::mt19937 engine
The RNG that is used for the Metropolis algorithm.
Definition: IsingModel.hpp:78
int64_t E
The current energy state. unit: .
Definition: IsingModel.hpp:70
double T
The temperature of the model.
Definition: IsingModel.hpp:62
int L
Size of the lattice.
Definition: IsingModel.hpp:66
arma::Mat< int > lattice
matrix where element .
Definition: IsingModel.hpp:44
void initialize_lattice()
Initialize the lattice with a random distribution of 1s and -1s.
Definition: IsingModel.cpp:49
IsingModel(int L, double T, int val)
Constructor for the Ising model.
Definition: IsingModel.cpp:31
IsingModel(int L, double T)
Constructor for the Ising model.
Definition: IsingModel.cpp:19
data_t Metropolis()
The Metropolis algorithm.
Definition: IsingModel.cpp:110
void initialize_energy()
Initialize the energy of the system.
Definition: IsingModel.cpp:96
void initialize_neighbors()
initialize the neighbors matrix.
Definition: IsingModel.cpp:70
double energy_diff[17]
An array containing all possible energy differences.
Definition: IsingModel.hpp:58
void initialize_magnetization()
Initialize the magnetization of the system.
Definition: IsingModel.cpp:88
arma::Mat< int > neighbors
matrix with the neighbors of each element .
Definition: IsingModel.hpp:54
void initialize_engine()
Initialize the RNG.
Definition: IsingModel.cpp:43
void initialize_lattice(int val)
Initialize the lattice with a specific value.
Definition: IsingModel.cpp:59
IsingModel()
Constructor used for testing.
Definition: IsingModel.cpp:14
int64_t M
The current magnetic strength. unit: Unitless.
Definition: IsingModel.hpp:74
void initialize_energy_diff()
Initialize the energy_diff array with the correct values.
Definition: IsingModel.cpp:81
Type to use with the IsingModel class and montecarlo module.
Definition: data_type.hpp:19
data_t(double E, double E2, double M, double M2, double M_abs)
Constructor with parameters.
Definition: data_type.hpp:45
std::string methodName(const std::string &pretty_function)
Takes in the PRETTY_FUNCTION string and removes the return type.
Definition: utils.hpp:58
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