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
15#include <fstream>
16
17#define EPS_2 (-2 * std::sinh(8.)) / (std::cosh(8.) + 3)
18
19#define MAG_2 (std::exp(8.) + 1) / (2 * (cosh(8.) + 3))
20
21#define CV_2 \
22 16 * (3 * std::cosh(8.) + 1) / ((std::cosh(8.) + 3) * (std::cosh(8.) + 3))
23
24#define X_2 \
25 (3 * std::exp(8.) + std::exp(-8.) + 3) \
26 / ((std::cosh(8.) + 3) * (std::cosh(8.) + 3))
27
31public:
35 {
36 IsingModel test;
37 test.L = 3;
38 test.T = 1.;
39
40 // Test that initializing the lattice only yields 1s and -1s.
41 test.initialize_lattice();
42 std::function<bool(int)> f = [](int x) { return x == 1 || x == -1; };
43 ASSERT(testlib::assert_each(f, test.lattice),
44 "Test lattice initialization.");
45
47 arma::Mat<int> neighbor_matrix("2, 1 ; 0, 2 ; 1, 0");
48 ASSERT(testlib::is_equal(neighbor_matrix, test.neighbors),
49 "Test neighbor matrix.");
50
51 // Fill the lattice with 1s to be able to test the next functions.
52 test.lattice.fill(1);
53
54 // Test the initial magnetization.
56 ASSERT(std::fabs(test.M - 9.) < 1e-8, "Test intial magnetization");
57
58 // Test that the initial energy is correct
59 test.initialize_energy();
60 ASSERT(std::fabs(test.E - (-18)) < 1e-8, "Test initial energy.");
61 }
62
70 int test_2x2_lattice(double tol, int max_cycles)
71 {
72 data_t data, tmp;
73 size_t L = 2;
74 size_t n_spins = L * L;
75 double T = 1.;
76 size_t cycles = 0;
77
78 // Create random engine using the mersenne twister
79 std::random_device rd;
80 std::mt19937 engine(rd());
81
82 IsingModel test(L, T);
83
84 int arr[]{0, 0, 0, 0};
85
86 // Loop through cycles
87 //std::ofstream ofile;
88 //ofile.open("output/test_2x2.txt");
89 while (cycles++ < max_cycles) {
90 data += test.Metropolis();
91 tmp = data / cycles;
92 //ofile << cycles << ',' << tmp.E / n_spins << ','
93 //<< tmp.M_abs / n_spins << ','
94 //<< (tmp.E2 - tmp.E * tmp.E) / (T * T) / n_spins << ','
95 //<< (tmp.M2 - tmp.M_abs * tmp.M_abs) / T / n_spins << '\n';
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(CV_2, (tmp.E2 - tmp.E * tmp.E) / (T * T)
99 / n_spins, tol)
100 && testlib::close_to(X_2, (tmp.M2 - tmp.M_abs * tmp.M_abs) / T
101 / n_spins, tol)) {
102 return cycles;
103 }
104 }
105 //std::cout << EPS_2 << ',' << MAG_2 << ',' << CV_2 << ',' << X_2
106 //<< std::endl;
107 //ofile.close();
108 // cycles = 0;
109 // data = 0;
110 // IsingModel test_mag(L, T);
111 // while (cycles++ < max_cycles) {
112 // data += test.Metropolis();
113 // tmp = data / (cycles * n_spins);
114 // if (testlib::close_to(MAG_2, tmp.M, tol)) {
115 // arr[1] = cycles;
116 // break;
117 //}
118 //}
119 // cycles = 0;
120 // data = 0;
121 // IsingModel test_CV(L, T);
122 // while (cycles++ < max_cycles) {
123 // data += test.Metropolis();
124 // tmp = data / (cycles * n_spins);
125 // if (testlib::close_to(CV_2, (tmp.E2 - tmp.E * tmp.E) / (T * T),
126 // tol)) {
127 // arr[2] = cycles;
128 // break;
129 //}
130 //}
131 // cycles = 0;
132 // data = 0;
133 // IsingModel test_X(L, T);
134 // while (cycles++ < max_cycles) {
135 // data += test.Metropolis();
136 // tmp = data / (cycles * n_spins);
137 // if (testlib::close_to(X_2, (tmp.M2 - tmp.M_abs * tmp.M_abs) / T,
138 // tol)) {
139 // arr[3] = cycles;
140 // break;
141 //}
142 //}
143 return 0;
144 }
145};
146
148int main()
149{
150 IsingModelTest test;
151
152 test.test_init_functions();
153 int res = 0;
154 int tmp;
155 for (size_t i=0; i < 1000; i++) {
156 tmp = test.test_2x2_lattice(1e-2, 1e5);
157 if (tmp == 0) {
158 std::cout << "not enough cycles\n";
159 break;
160 }
161 res += tmp;
162 }
163
164 std::cout << "Res: " << res / 1000 << std::endl;
165
166 return 0;
167}
The definition of the Ising model.
Test class for the Ising model.
Definition: test_suite.cpp:30
int test_2x2_lattice(double tol, int max_cycles)
Test numerical data with analytical data.
Definition: test_suite.cpp:70
void test_init_functions()
Test That initializing works as intended.
Definition: test_suite.cpp:34
The Ising model in 2 dimensions.
Definition: IsingModel.hpp:37
int M
The current magnetic strength. unit: Unitless.
Definition: IsingModel.hpp:72
double T
The temperature of the model.
Definition: IsingModel.hpp:60
int L
Size of the lattice.
Definition: IsingModel.hpp:64
arma::Mat< int > lattice
matrix where element .
Definition: IsingModel.hpp:42
void initialize_lattice()
Initialize the lattice with a random distribution of 1s and -1s.
Definition: IsingModel.cpp:44
data_t Metropolis()
The Metropolis algorithm.
Definition: IsingModel.cpp:96
void initialize_energy()
Initialize the energy.
Definition: IsingModel.cpp:82
void initialize_neighbors()
initialize the neighbors matrix.
Definition: IsingModel.cpp:56
void initialize_magnetization()
Initialize the magnetization.
Definition: IsingModel.cpp:74
arma::Mat< int > neighbors
matrix with the neighbors of each element .
Definition: IsingModel.hpp:52
int E
The current energy state. unit: .
Definition: IsingModel.hpp:68
int main()
The main function.
Definition: test_suite.cpp:148
A small test library.
#define ASSERT(expr, msg)
A prettier assertion function.
Definition: testlib.hpp:31