# 2 Dimensional Ising Model: include/utils.hpp Source File

Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
/** @file utils.hpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 1.0
*
* @brief Function prototypes and macros that are useful.
*
* These utility function are mainly for convenience and aren't directly
* related to the project. Anything that is in the details namespace should
* not be used directly, or else it might cause undefined behavior if not used
* correctly.
*
* @bug No known bugs
* */
#ifndef __UTILS__
#define __UTILS__
#include <armadillo>
#include <iomanip>
#include <sstream>
#include <string>
#include <sys/stat.h>
#include <vector>
/** @def DEBUG(msg)
* @brief Writes a debug message
*
* This macro writes a debug message that includes the filename,
* line number, and a custom message. The function is wrapped in an ifdef
* that checks if DBG is defined, so one can choose to display the debug
* messages by adding the -DDBG flag when compiling.
* */
#ifdef DBG
#define DEBUG(msg)
std::cout << __FILE__ << " " << __LINE__ << ": " << msg << std::endl
#else
#define DEBUG(msg)
#endif
/** @def __METHOD_NAME__
* @brief Get the name of the current method/function without the return type.
* */
#define __METHOD_NAME__ details::methodName(__PRETTY_FUNCTION__)
namespace details {
/** @brief Takes in the __PRETTY_FUNCTION__ string and removes the return type.
*
* @details This function should only be used for the __METHOD_NAME__ macro,
* since it takes the output from __PRETTY_FUNCTION__ and strips the return
* type.
*
* @param pretty_function The string from __PRETTY_FUNCTION__
*
* @return std::string
* */
inline std::string methodName(const std::string &pretty_function)
{
size_t colons = pretty_function.find("::");
size_t begin = pretty_function.substr(0, colons).rfind(" ") + 1;
size_t end = pretty_function.rfind("(") - begin;
return pretty_function.substr(begin, end) + "()";
}
} // namespace details
namespace utils {
/** @brief Turns a double into a string written in scientific format.
*
* @details The code is stolen from https://github.com/anderkve/FYS3150.
*
* @param d The number to stringify
* @param width The reserved width of the string
* @param prec The precision of the stringified number
*
* @return std::string
* */
std::string scientific_format(double d, int width = 20, int prec = 10);
/** @brief Turns a vector of doubles into a string written in scientific
* format.
*
* @details The code is stolen from https://github.com/anderkve/FYS3150.
*
* @param v The vector to stringify
* @param width The reserved width of the string
* @param prec The precision of the stringified number
*
* @return std::string
* */
std::string scientific_format(const std::vector<double> &v, int width = 20,
int prec = 10);
/** @brief Make path given.
*
* @details This tries to be the equivalent to "mkdir -p" and creates a new
* directory whenever it needs to.
*
* @param path The path to be created
* @param mode The mode/permissions for all the new directories
*
* @return bool Success/Fail
* */
bool mkpath(std::string path, int mode = 0777);
/** @brief Get the directory name of the path
*
* @param path The path to use.
*
* @return string
* */
std::string dirname(const std::string &path);
/** @brief Take 2 strings and concatenate them and make sure there is a
* directory separator (/) between them.
*
* @details This function doesn't care whether or not the values given as
* parameters are valid path strings. It is the responsibility of the user to make
* sure that the values given are valid path strings.
* The function only guarantees that the output string is a valid path string.
*
* @param left The left hand side of the result string
* @param right The right hand side of the result string
*
* @return string
* */
std::string concatpath(const std::string &left, const std::string &right);
} // namespace utils
#endif
UP
#define UP
Used for the neighbor matrix in the class.
Definition: IsingModel.hpp:26
INDEX
#define INDEX(I, N)
I modulo N.
Definition: IsingModel.hpp:23
DOWN
#define DOWN
Used for the neighbor matrix in the class.
Definition: IsingModel.hpp:28
LEFT
#define LEFT
Used for the neighbor matrix in the class.
Definition: IsingModel.hpp:27
RIGHT
#define RIGHT
Used for the neighbor matrix in the class.
Definition: IsingModel.hpp:29
IsingModelTest
Test class for the Ising model.
Definition: test_suite.cpp:36
IsingModel
The Ising model in 2 dimensions.
Definition: IsingModel.hpp:36
IsingModel::engine
std::mt19937 engine
The RNG that is used for the Metropolis algorithm.
Definition: IsingModel.hpp:78
IsingModel::E
int64_t E
The current energy state. unit: .
Definition: IsingModel.hpp:70
IsingModel::T
double T
The temperature of the model.
Definition: IsingModel.hpp:62
IsingModel::L
int L
Size of the lattice.
Definition: IsingModel.hpp:66
IsingModel::lattice
arma::Mat< int > lattice
matrix where element .
Definition: IsingModel.hpp:44
IsingModel::initialize_lattice
void initialize_lattice()
Initialize the lattice with a random distribution of 1s and -1s.
Definition: IsingModel.cpp:49
IsingModel::IsingModel
IsingModel(int L, double T, int val)
Constructor for the Ising model.
Definition: IsingModel.cpp:31
IsingModel::IsingModel
IsingModel(int L, double T)
Constructor for the Ising model.
Definition: IsingModel.cpp:19
IsingModel::Metropolis
data_t Metropolis()
The Metropolis algorithm.
Definition: IsingModel.cpp:110
IsingModel::initialize_energy
void initialize_energy()
Initialize the energy of the system.
Definition: IsingModel.cpp:96
IsingModel::initialize_neighbors
void initialize_neighbors()
initialize the neighbors matrix.
Definition: IsingModel.cpp:70
IsingModel::energy_diff
double energy_diff[17]
An array containing all possible energy differences.
Definition: IsingModel.hpp:58
IsingModel::initialize_magnetization
void initialize_magnetization()
Initialize the magnetization of the system.
Definition: IsingModel.cpp:88
IsingModel::neighbors
arma::Mat< int > neighbors
matrix with the neighbors of each element .
Definition: IsingModel.hpp:54
IsingModel::initialize_engine
void initialize_engine()
Initialize the RNG.
Definition: IsingModel.cpp:43
IsingModel::initialize_lattice
void initialize_lattice(int val)
Initialize the lattice with a specific value.
Definition: IsingModel.cpp:59
IsingModel::IsingModel
IsingModel()
Constructor used for testing.
Definition: IsingModel.cpp:14
IsingModel::M
int64_t M
The current magnetic strength. unit: Unitless.
Definition: IsingModel.hpp:74
IsingModel::initialize_energy_diff
void initialize_energy_diff()
Initialize the energy_diff array with the correct values.
Definition: IsingModel.cpp:81
data_t
Type to use with the IsingModel class and montecarlo module.
Definition: data_type.hpp:19
data_t::data_t
data_t(double E, double E2, double M, double M2, double M_abs)
Constructor with parameters.
Definition: data_type.hpp:45
details::methodName
std::string methodName(const std::string &pretty_function)
Takes in the PRETTY_FUNCTION string and removes the return type.
Definition: utils.hpp:58
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::scientific_format
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
utils::concatpath
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
utils::dirname
std::string dirname(const std::string &path)
Get the directory name of the path.
Definition: utils.cpp:58