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

Loading...
Searching...
No Matches
testlib.hpp
Go to the documentation of this file.
/** @file testlib.hpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 1.0
*
* @brief A small test library.
*
* @details This a small testing library that is tailored for the needs of 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 __TESTLIB__
#define __TESTLIB__
#include "utils.hpp"
#include <armadillo>
#include <string>
#include <type_traits>
/** @def ASSERT(expr)
* @brief A prettier assertion function.
*
* This macro calls the m_assert function which is a more informative
* assertion function than the regular assert function from cassert.
* */
#define ASSERT(expr, msg)
details::m_assert(expr, #expr, __METHOD_NAME__ , __FILE__, __LINE__, msg)
namespace details {
/** @brief Test an expression, confirm that test is ok, or abort execution.
*
* @details This function takes in an expression and prints an OK message if
* it's true, or it prints a fail message and aborts execution if it fails.
*
* @param expr The expression to be evaluated
* @param expr_str The stringified version of the expression
* @param func The function name of the caller
* @param file The file of the caller
* @param line The line number where this function is called from
* @param msg The message to be displayed
* */
void m_assert(bool expr, std::string expr_str, std::string func,
std::string file, int line, std::string msg);
} // namespace details
namespace testlib {
/** @brief Test if two armadillo matrices/vectors are close to each other.
*
* @details This function takes in 2 matrices/vectors and checks if they are
* approximately equal to each other given a tolerance.
*
* @param a Matrix/vector a
* @param b Matrix/vector b
* @param tol The tolerance
*
* @return bool
* */
template <class T,
class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
static bool close_to(arma::Mat<T> &a, arma::Mat<T> &b, double tol = 1e-8)
{
if (a.n_elem != b.n_elem) {
return false;
}
for (size_t i = 0; i < a.n_elem; i++) {
if (!close_to(a(i), b(i))) {
return false;
}
}
return true;
}
/** @brief Test if two numbers are close to each other.
*
* @details This function takes in 2 matrices/vectors and checks if they are
* approximately equal to each other given a tolerance.
*
* @param a Matrix/vector a
* @param b Matrix/vector b
* @param tol The tolerance
*
* @return bool
* */
template <class T,
class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
static bool close_to(T a, T b, double tol = 1e-8)
{
return std::fabs(a - b) < tol;
}
/** @brief Test if two armadillo matrices/vectors are equal.
*
* @details This function takes in 2 matrices/vectors and checks if they are
* equal to each other. This should only be used for integral types.
*
* @param a Matrix/vector a
* @param b Matrix/vector b
*
* @return bool
* */
template <class T,
class = typename std::enable_if<std::is_integral<T>::value>::type>
static bool is_equal(arma::Mat<T> &a, arma::Mat<T> &b)
{
for (size_t i = 0; i < a.n_elem; i++) {
if (!(a(i) == b(i))) {
return false;
}
}
return true;
}
/** @brief Test that all elements fulfill the condition.
*
* @param expr The boolean expression to apply to each element
* @param M The matrix/vector to iterate over
*
* @return bool
* */
template <class T,
class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
static bool assert_each(std::function<bool(T)> expr, arma::Mat<T> &M)
{
for (size_t i = 0; i < M.n_elem; i++) {
if (!expr(M(i))) {
return false;
}
}
return true;
}
} // namespace testlib
#endif
IsingModelTest
Test class for the Ising model.
Definition: test_suite.cpp:36
IsingModelTest::test_2x2_lattice
int test_2x2_lattice(double tol, int max_cycles)
Test numerical data with analytical data.
Definition: test_suite.cpp:76
IsingModelTest::test_init_functions
void test_init_functions()
Test that initializing works as intended.
Definition: test_suite.cpp:40
IsingModel
The Ising model in 2 dimensions.
Definition: IsingModel.hpp:36
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::initialize_lattice
void initialize_lattice()
Initialize the lattice with a random distribution of 1s and -1s.
Definition: IsingModel.cpp:49
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::initialize_magnetization
void initialize_magnetization()
Initialize the magnetization of the system.
Definition: IsingModel.cpp:88
IsingModel::M
int64_t M
The current magnetic strength. unit: Unitless.
Definition: IsingModel.hpp:74
data_t
Type to use with the IsingModel class and montecarlo module.
Definition: data_type.hpp:19
data_t::M_abs
double M_abs
Absolute Magnetization.
Definition: data_type.hpp:25
data_t::E
double E
Energy.
Definition: data_type.hpp:21
data_t::operator+=
data_t & operator+=(const data_t &b)
Overload of the addition equals operator.
Definition: data_type.hpp:150
data_t::M2
double M2
Magnetization squared.
Definition: data_type.hpp:24
data_t::E2
double E2
Energy squared.
Definition: data_type.hpp:23
EPS_2
#define EPS_2
The analytic expected energy for a lattice.
Definition: test_suite.cpp:17
MAG_2
#define MAG_2
The analytic expected magnetization for a lattice.
Definition: test_suite.cpp:22
X_2
#define X_2
The analytic susceptibility for a lattice.
Definition: test_suite.cpp:30
main
int main()
The main function.
Definition: test_suite.cpp:110
CV_2
#define CV_2
The analytic heat capacity for a lattice.
Definition: test_suite.cpp:26
testlib::close_to
static bool close_to(arma::Mat< T > &a, arma::Mat< T > &b, double tol=1e-8)
Test if two armadillo matrices/vectors are close to each other.
Definition: testlib.hpp:67
details::m_assert
void m_assert(bool expr, std::string expr_str, std::string func, std::string file, int line, std::string msg)
Test an expression, confirm that test is ok, or abort execution.
Definition: testlib.cpp:15
testlib::is_equal
static bool is_equal(arma::Mat< T > &a, arma::Mat< T > &b)
Test if two armadillo matrices/vectors are equal.
Definition: testlib.hpp:111
testlib::close_to
static bool close_to(T a, T b, double tol=1e-8)
Test if two numbers are close to each other.
Definition: testlib.hpp:94
ASSERT
#define ASSERT(expr, msg)
A prettier assertion function.
Definition: testlib.hpp:31
testlib::assert_each
static bool assert_each(std::function< bool(T)> expr, arma::Mat< T > &M)
Test that all elements fulfill the condition.
Definition: testlib.hpp:130
__METHOD_NAME__
#define __METHOD_NAME__
Get the name of the current method/function without the return type.
Definition: utils.hpp:45