2 Dimensional Ising Model
Simulate the change in energy and magnetization in a ferro magnet
Loading...
Searching...
No Matches
test_suite.cpp
Go to the documentation of this file.
1
12#include "IsingModel.hpp"
13#include "testlib.hpp"
14
17#define EPS_2 (-2 * std::sinh(8.)) / (std::cosh(8.) + 3)
18
22#define MAG_2 (std::exp(8.) + 1) / (2 * (cosh(8.) + 3))
23
26#define CV_2 \
27 16 * (3 * std::cosh(8.) + 1) / ((std::cosh(8.) + 3) * (std::cosh(8.) + 3))
28
30#define X_2 \
31 (3 * std::exp(8.) + std::exp(-8.) + 3) \
32 / ((std::cosh(8.) + 3) * (std::cosh(8.) + 3))
33
37public:
41 {
42 IsingModel test;
43 test.L = 3;
44 test.T = 1.;
45
46 // Test that initializing the lattice only yields 1s and -1s.
47 test.initialize_lattice();
48 std::function<bool(int)> f = [](int x) { return x == 1 || x == -1; };
49 ASSERT(testlib::assert_each(f, test.lattice),
50 "Test lattice initialization.");
51
53 arma::Mat<int> neighbor_matrix("2, 1 ; 0, 2 ; 1, 0");
54 ASSERT(testlib::is_equal(neighbor_matrix, test.neighbors),
55 "Test neighbor matrix.");
56
57 // Fill the lattice with 1s to be able to test the next functions.
58 test.lattice.fill(1);
59
60 // Test the initial magnetization.
62 ASSERT(std::fabs(test.M - 9.) < 1e-8, "Test intial magnetization");
63
64 // Test that the initial energy is correct
65 test.initialize_energy();
66 ASSERT(std::fabs(test.E - (-18)) < 1e-8, "Test initial energy.");
67 }
68
76 int test_2x2_lattice(double tol, int max_cycles)
77 {
78 data_t data, tmp;
79 size_t L = 2;
80 size_t n_spins = L * L;
81 double T = 1.;
82 size_t cycles = 0;
83
84 // Create random engine using the mersenne twister
85 std::random_device rd;
86 std::mt19937 engine(rd());
87
88 IsingModel test(L, T);
89
90 int arr[]{0, 0, 0, 0};
91
92 // Loop through cycles
93 while (cycles++ < max_cycles) {
94 data += test.Metropolis();
95 tmp = data / cycles;
96 if (testlib::close_to(EPS_2, tmp.E / n_spins, tol)
97 && testlib::close_to(MAG_2, tmp.M_abs / n_spins, tol)
98 && testlib::close_to(
99 CV_2, (tmp.E2 - tmp.E * tmp.E) / (T * T) / n_spins, tol)
100 && testlib::close_to(
101 X_2, (tmp.M2 - tmp.M_abs * tmp.M_abs) / T / n_spins, tol)) {
102 return cycles;
103 }
104 }
105 return 0;
106 }
107};
108
110int main()
111{
112 IsingModelTest test;
113
114 test.test_init_functions();
115
116 int res = 0;
117 int tmp;
118 int iterations = 10000;
119 int accepted_values = 0;
120
121 // Run through the test multiple times to get a better estimate.
122 for (size_t i = 0; i < iterations; i++) {
123 tmp = test.test_2x2_lattice(1e-2, 1e5);
124 if (tmp == 0) {
125 continue;
126 }
127 accepted_values++;
128 res += tmp;
129 }
130
131 std::cout << "Res: " << res / accepted_values << std::endl;
132
133 return 0;
134}
The definition of the Ising model.
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
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
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
arma::Mat< int > neighbors
matrix with the neighbors of each element .
Definition: IsingModel.hpp:54
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
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
A small test library.
#define ASSERT(expr, msg)
A prettier assertion function.
Definition: testlib.hpp:31