2
3
4
5
6
7
8
9
10
11
45 std::random_device rd{};
51 this->lattice.set_size(
this->L,
this->L);
53 std::uniform_int_distribution<> coin_flip(0, 1);
55 for (size_t i = 0; i <
this->lattice.n_elem; i++)
56 this->lattice(i) = 2 * coin_flip(
this->engine) - 1;
62 if (val != 1 && val != -1) {
66 this->lattice.set_size(
this->L,
this->L);
67 this->lattice.fill(val);
72 this->neighbors.set_size(
this->L, 2);
75 for (
int i = 0; i < (
int)
this->L; i++) {
76 this->neighbors(i,
UP) =
INDEX(i - 1,
this->L);
83 for (
int i = -8; i <= 8; i += 4) {
91 for (size_t i = 0; i <
this->lattice.n_elem; i++) {
92 this->M +=
this->lattice(i);
101 for (size_t j = 0; j <
this->L; j++) {
102 for (size_t i = 0; i <
this->L; i++) {
103 this->E -=
this->lattice(i, j)
104 * (
this->lattice(i,
this->neighbors(j,
RIGHT))
105 +
this->lattice(
this->neighbors(i,
DOWN), j));
116 std::uniform_int_distribution<> random_index(0,
this->L - 1);
118 std::uniform_real_distribution<> random_number(0., 1.);
121 for (size_t i = 0; i <
this->lattice.n_elem; i++) {
123 ri = random_index(
engine);
124 rj = random_index(
engine);
127 dE = 2 *
this->lattice(ri, rj)
128 * (
this->lattice(ri,
this->neighbors(rj,
LEFT))
129 +
this->lattice(ri,
this->neighbors(rj,
RIGHT))
130 +
this->lattice(
this->neighbors(ri,
UP), rj)
131 +
this->lattice(
this->neighbors(ri,
DOWN), rj));
136 this->lattice(ri, rj) *= -1;
137 this->M += 2 *
this->lattice(ri, rj);
143 (
double)(
this->M *
this->M)
, std::fabs((
double)
this->M)
);
#define UP
Used for the neighbor matrix in the class.
#define INDEX(I, N)
I modulo N.
#define DOWN
Used for the neighbor matrix in the class.
#define LEFT
Used for the neighbor matrix in the class.
#define RIGHT
Used for the neighbor matrix in the class.
The Ising model in 2 dimensions.
std::mt19937 engine
The RNG that is used for the Metropolis algorithm.
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.
IsingModel(int L, double T, int val)
Constructor for the Ising model.
IsingModel(int L, double T)
Constructor for the Ising model.
data_t Metropolis()
The Metropolis algorithm.
void initialize_energy()
Initialize the energy of the system.
void initialize_neighbors()
initialize the neighbors matrix.
double energy_diff[17]
An array containing all possible energy differences.
void initialize_magnetization()
Initialize the magnetization of the system.
void initialize_engine()
Initialize the RNG.
void initialize_lattice(int val)
Initialize the lattice with a specific value.
IsingModel()
Constructor used for testing.
int64_t M
The current magnetic strength. unit: Unitless.
void initialize_energy_diff()
Initialize the energy_diff array with the correct values.
Type to use with the IsingModel class and montecarlo module.
data_t(double E, double E2, double M, double M2, double M_abs)
Constructor with parameters.