# 2 Dimensional Ising Model: src/test_suite.cpp Source File

Loading...
Searching...
No Matches
test_suite.cpp
Go to the documentation of this file.
/** @file test_suite.cpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 1.0
*
* @brief Sweep over different temperatures and generate data.
*
* @bug No known bugs
* */
#include "IsingModel.hpp"
#include "testlib.hpp"
/** @brief The analytic expected energy for a \f$ 2 \times 2 \f$ lattice.
* */
#define EPS_2 (-2 * std::sinh(8.)) / (std::cosh(8.) + 3)
/** @brief The analytic expected magnetization for a \f$ 2 \times 2 \f$
* lattice.
* */
#define MAG_2 (std::exp(8.) + 1) / (2 * (cosh(8.) + 3))
/** @brief The analytic heat capacity for a \f$ 2 \times 2 \f$ lattice.
* */
#define CV_2
16 * (3 * std::cosh(8.) + 1) / ((std::cosh(8.) + 3) * (std::cosh(8.) + 3))
/** @brief The analytic susceptibility for a \f$ 2 \times 2 \f$ lattice.*/
#define X_2
(3 * std::exp(8.) + std::exp(-8.) + 3)
/ ((std::cosh(8.) + 3) * (std::cosh(8.) + 3))
/** @brief Test class for the Ising model
* */
class IsingModelTest {
public:
/** @brief Test that initializing works as intended.
* */
void test_init_functions()
{
IsingModel test;
test. L = 3;
test. T = 1.;
// Test that initializing the lattice only yields 1s and -1s.
test. initialize_lattice ( );
std::function<bool(int)> f = [](int x) { return x == 1 || x == -1; };
ASSERT(testlib::assert_each(f, test.lattice),
"Test lattice initialization.");
test. initialize_neighbors ( );
arma::Mat<int> neighbor_matrix("2, 1 ; 0, 2 ; 1, 0");
ASSERT(testlib::is_equal(neighbor_matrix, test.neighbors),
"Test neighbor matrix.");
// Fill the lattice with 1s to be able to test the next functions.
test.lattice.fill(1);
// Test the initial magnetization.
test. initialize_magnetization ( );
ASSERT(std::fabs(test. M - 9.) < 1e-8, "Test intial magnetization");
// Test that the initial energy is correct
test. initialize_energy ( );
ASSERT(std::fabs(test. E - (-18)) < 1e-8, "Test initial energy.");
}
/** @brief Test numerical data with analytical data.
*
* @param tol The tolerance between the analytical and numerical solution.
* @param max_cycles The max number of Monte Carlo cycles.
*
* return int
* */
int test_2x2_lattice(double tol, int max_cycles)
{
data_t data, tmp;
size_t L = 2;
size_t n_spins = L * L;
double T = 1.;
size_t cycles = 0;
// Create random engine using the mersenne twister
std::random_device rd;
std::mt19937 engine(rd());
IsingModel test(L, T);
int arr[]{0, 0, 0, 0};
// Loop through cycles
while (cycles++ < max_cycles) {
data += test. Metropolis ( );
tmp = data / cycles;
if (testlib::close_to(EPS_2, tmp. E / n_spins, tol)
&& testlib::close_to(MAG_2, tmp. M_abs / n_spins, tol)
&& testlib::close_to(
CV_2, (tmp. E2 - tmp. E * tmp. E) / (T * T) / n_spins, tol)
&& testlib::close_to(
X_2, (tmp. M2 - tmp. M_abs * tmp. M_abs) / T / n_spins, tol)) {
return cycles;
}
}
return 0;
}
};
/** @brief The main function.*/
int main()
{
IsingModelTest test;
test. test_init_functions ( );
int res = 0;
int tmp;
int iterations = 10000;
int accepted_values = 0;
// Run through the test multiple times to get a better estimate.
for (size_t i = 0; i < iterations; i++) {
tmp = test. test_2x2_lattice (1e-2, 1e5);
if (tmp == 0) {
continue;
}
accepted_values++;
res += tmp;
}
std::cout << "Res: " << res / accepted_values << std::endl;
return 0;
}
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
ASSERT
#define ASSERT(expr, msg)
A prettier assertion function.
Definition: testlib.hpp:31