2 Dimensional Ising Model
Simulate the change in energy and magnetization in a ferro magnet
Loading...
Searching...
No Matches
testlib.hpp
Go to the documentation of this file.
1/** @file testlib.hpp
2 *
3 * @author Cory Alexander Balaton (coryab)
4 * @author Janita Ovidie Sandtrøen Willumsen (janitaws)
5 *
6 * @version 1.0
7 *
8 * @brief A small test library.
9 *
10 * @details This a small testing library that is tailored for the needs of the
11 * project. Anything that is in the details namespace should not be used
12 * directly, or else it might cause undefined behavior if not used correctly.
13 *
14 * @bug No known bugs
15 * */
16#ifndef __TESTLIB__
17#define __TESTLIB__
18
19#include "utils.hpp"
20
21#include <armadillo>
22#include <string>
23#include <type_traits>
24
25/** @def ASSERT(expr)
26 * @brief A prettier assertion function.
27 *
28 * This macro calls the m_assert function which is a more informative
29 * assertion function than the regular assert function from cassert.
30 * */
31#define ASSERT(expr, msg)
32 details::m_assert(expr, #expr, __METHOD_NAME__, __FILE__, __LINE__, msg)
33
34namespace details {
35
36/** @brief Test an expression, confirm that test is ok, or abort execution.
37 *
38 * @details This function takes in an expression and prints an OK message if
39 * it's true, or it prints a fail message and aborts execution if it fails.
40 *
41 * @param expr The expression to be evaluated
42 * @param expr_str The stringified version of the expression
43 * @param func The function name of the caller
44 * @param file The file of the caller
45 * @param line The line number where this function is called from
46 * @param msg The message to be displayed
47 * */
48void m_assert(bool expr, std::string expr_str, std::string func,
49 std::string file, int line, std::string msg);
50} // namespace details
51
52namespace testlib {
53
54/** @brief Test if two armadillo matrices/vectors are close to each other.
55 *
56 * @details This function takes in 2 matrices/vectors and checks if they are
57 * approximately equal to each other given a tolerance.
58 *
59 * @param a Matrix/vector a
60 * @param b Matrix/vector b
61 * @param tol The tolerance
62 *
63 * @return bool
64 * */
65template <class T,
66 class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
67static bool close_to(arma::Mat<T> &a, arma::Mat<T> &b, double tol = 1e-8)
68{
69 if (a.n_elem != b.n_elem) {
70 return false;
71 }
72
73 for (size_t i = 0; i < a.n_elem; i++) {
74 if (!close_to(a(i), b(i))) {
75 return false;
76 }
77 }
78 return true;
79}
80
81/** @brief Test if two numbers are close to each other.
82 *
83 * @details This function takes in 2 matrices/vectors and checks if they are
84 * approximately equal to each other given a tolerance.
85 *
86 * @param a Matrix/vector a
87 * @param b Matrix/vector b
88 * @param tol The tolerance
89 *
90 * @return bool
91 * */
92template <class T,
93 class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
94static bool close_to(T a, T b, double tol = 1e-8)
95{
96 return std::fabs(a - b) < tol;
97}
98
99/** @brief Test if two armadillo matrices/vectors are equal.
100 *
101 * @details This function takes in 2 matrices/vectors and checks if they are
102 * equal to each other. This should only be used for integral types.
103 *
104 * @param a Matrix/vector a
105 * @param b Matrix/vector b
106 *
107 * @return bool
108 * */
109template <class T,
110 class = typename std::enable_if<std::is_integral<T>::value>::type>
111static bool is_equal(arma::Mat<T> &a, arma::Mat<T> &b)
112{
113 for (size_t i = 0; i < a.n_elem; i++) {
114 if (!(a(i) == b(i))) {
115 return false;
116 }
117 }
118 return true;
119}
120
121/** @brief Test that all elements fulfill the condition.
122 *
123 * @param expr The boolean expression to apply to each element
124 * @param M The matrix/vector to iterate over
125 *
126 * @return bool
127 * */
128template <class T,
129 class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
130static bool assert_each(std::function<bool(T)> expr, arma::Mat<T> &M)
131{
132 for (size_t i = 0; i < M.n_elem; i++) {
133 if (!expr(M(i))) {
134 return false;
135 }
136 }
137 return true;
138}
139} // namespace testlib
140#endif
Test class for the Ising model.
Definition: test_suite.cpp:36
int test_2x2_lattice(double tol, int max_cycles)
Test numerical data with analytical data.
Definition: test_suite.cpp:76
void test_init_functions()
Test that initializing works as intended.
Definition: test_suite.cpp:40
The Ising model in 2 dimensions.
Definition: IsingModel.hpp:36
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
void initialize_lattice()
Initialize the lattice with a random distribution of 1s and -1s.
Definition: IsingModel.cpp:49
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
void initialize_magnetization()
Initialize the magnetization of the system.
Definition: IsingModel.cpp:88
int64_t M
The current magnetic strength. unit: Unitless.
Definition: IsingModel.hpp:74
Type to use with the IsingModel class and montecarlo module.
Definition: data_type.hpp:19
double M_abs
Absolute Magnetization.
Definition: data_type.hpp:25
double E
Energy.
Definition: data_type.hpp:21
data_t & operator+=(const data_t &b)
Overload of the addition equals operator.
Definition: data_type.hpp:150
double M2
Magnetization squared.
Definition: data_type.hpp:24
double E2
Energy squared.
Definition: data_type.hpp:23
#define EPS_2
The analytic expected energy for a lattice.
Definition: test_suite.cpp:17
#define MAG_2
The analytic expected magnetization for a lattice.
Definition: test_suite.cpp:22
#define X_2
The analytic susceptibility for a lattice.
Definition: test_suite.cpp:30
int main()
The main function.
Definition: test_suite.cpp:110
#define CV_2
The analytic heat capacity for a lattice.
Definition: test_suite.cpp:26
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
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
static bool is_equal(arma::Mat< T > &a, arma::Mat< T > &b)
Test if two armadillo matrices/vectors are equal.
Definition: testlib.hpp:111
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
#define ASSERT(expr, msg)
A prettier assertion function.
Definition: testlib.hpp:31
static bool assert_each(std::function< bool(T)> expr, arma::Mat< T > &M)
Test that all elements fulfill the condition.
Definition: testlib.hpp:130
#define __METHOD_NAME__
Get the name of the current method/function without the return type.
Definition: utils.hpp:45