2
3
4
5
6
7
8
9
10
11
16
17#define EPS_2 (-2
* std::sinh(8.
)) / (std::cosh(8.
) + 3
)
20
21
22#define MAG_2 (std::exp(8.
) + 1
) / (2
* (cosh(8.
) + 3
))
25
27 16
* (3
* std::cosh(8.
) + 1
) / ((std::cosh(8.
) + 3
) * (std::cosh(8.
) + 3
))
31 (3
* std::exp(8.
) + std::exp(-8.
) + 3
)
32 / ((std::cosh(8.
) + 3
) * (std::cosh(8.
) + 3
))
35
39
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.");
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.");
62 ASSERT(std::fabs(test
.M - 9.) < 1e-8,
"Test intial magnetization");
66 ASSERT(std::fabs(test
.E - (-18)) < 1e-8,
"Test initial energy.");
70
71
72
73
74
75
80 size_t n_spins = L * L;
85 std::random_device rd;
86 std::mt19937 engine(rd());
90 int arr[]{0, 0, 0, 0};
93 while (cycles++ < max_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)
99 CV_2, (tmp
.E2 - tmp
.E * tmp
.E) / (T * T) / n_spins, tol)
100 && testlib::close_to(
118 int iterations = 10000;
119 int accepted_values = 0;
122 for (size_t i = 0; i < iterations; i++) {
131 std::cout <<
"Res: " << res / accepted_values << std::endl;
Test class for the Ising model.
int test_2x2_lattice(double tol, int max_cycles)
Test numerical data with analytical data.
void test_init_functions()
Test that initializing works as intended.
The Ising model in 2 dimensions.
int64_t E
The current energy state. unit: .
double T
The temperature of the model.
int L
Size of the lattice.
void initialize_lattice()
Initialize the lattice with a random distribution of 1s and -1s.
data_t Metropolis()
The Metropolis algorithm.
void initialize_energy()
Initialize the energy of the system.
void initialize_neighbors()
initialize the neighbors matrix.
void initialize_magnetization()
Initialize the magnetization of the system.
int64_t M
The current magnetic strength. unit: Unitless.
Type to use with the IsingModel class and montecarlo module.
double M_abs
Absolute Magnetization.
data_t & operator+=(const data_t &b)
Overload of the addition equals operator.
double M2
Magnetization squared.
#define EPS_2
The analytic expected energy for a lattice.
#define MAG_2
The analytic expected magnetization for a lattice.
#define X_2
The analytic susceptibility for a lattice.
int main()
The main function.
#define CV_2
The analytic heat capacity for a lattice.
#define ASSERT(expr, msg)
A prettier assertion function.