2 Dimensional Ising Model
Simulate the change in energy and magnetization in a ferro magnet
Loading...
Searching...
No Matches
IsingModel.cpp
Go to the documentation of this file.
1
12#include "IsingModel.hpp"
13
15{
16 this->initialize_engine();
17}
18
19IsingModel::IsingModel(int L, double T)
20{
21 this->L = L;
22 this->T = T;
23 this->initialize_engine();
24 this->initialize_lattice();
28 this->initialize_energy();
29}
30
31IsingModel::IsingModel(int L, double T, int val)
32{
33 this->L = L;
34 this->T = T;
35 this->initialize_engine();
36 this->initialize_lattice(val);
40 this->initialize_energy();
41}
42
44{
45 std::random_device rd{};
46 this->engine = std::mt19937{rd()};
47}
48
50{
51 this->lattice.set_size(this->L, this->L);
52
53 std::uniform_int_distribution<> coin_flip(0, 1);
54
55 for (size_t i = 0; i < this->lattice.n_elem; i++)
56 this->lattice(i) = 2 * coin_flip(this->engine) - 1;
57}
58
60{
61 // If val is neither 1 or -1, then initialize random values.
62 if (val != 1 && val != -1) {
63 this->initialize_lattice();
64 return;
65 }
66 this->lattice.set_size(this->L, this->L);
67 this->lattice.fill(val);
68}
69
71{
72 this->neighbors.set_size(this->L, 2);
73
74 // Having i as a signed integer is necessary in this case.
75 for (int i = 0; i < (int)this->L; i++) {
76 this->neighbors(i, UP) = INDEX(i - 1, this->L);
77 this->neighbors(i, DOWN) = INDEX(i + 1, this->L);
78 }
79}
80
82{
83 for (int i = -8; i <= 8; i += 4) {
84 this->energy_diff[i+8] = std::exp(-(double)i / this->T);
85 }
86}
87
89{
90 this->M = 0.;
91 for (size_t i = 0; i < this->lattice.n_elem; i++) {
92 this->M += this->lattice(i);
93 }
94}
95
97{
98 this->E = 0.;
99
100 // Loop through the matrix
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));
106 }
107 }
108}
109
111{
112 int ri, rj;
113 int dE;
114
115 // Create random distribution for indeces
116 std::uniform_int_distribution<> random_index(0, this->L - 1);
117 // Create random distribution for acceptance
118 std::uniform_real_distribution<> random_number(0., 1.);
119
120 // Loop over the number of spins
121 for (size_t i = 0; i < this->lattice.n_elem; i++) {
122 // Get random indeces
123 ri = random_index(engine);
124 rj = random_index(engine);
125
126 // Calculate the difference in energy
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));
132
133 // Choose whether or not to accept the new configuration
134 if (random_number(engine) <= this->energy_diff[dE+8]) {
135 // Update if the configuration is accepted
136 this->lattice(ri, rj) *= -1;
137 this->M += 2 * this->lattice(ri, rj);
138 this->E += dE;
139 }
140 }
141
142 return data_t((double)this->E, (double)(this->E * this->E), (double)this->M,
143 (double)(this->M * this->M), std::fabs((double)this->M));
144}
The definition of the Ising model.
#define UP
Used for the neighbor matrix in the class.
Definition: IsingModel.hpp:26
#define INDEX(I, N)
I modulo N.
Definition: IsingModel.hpp:23
#define DOWN
Used for the neighbor matrix in the class.
Definition: IsingModel.hpp:28
#define LEFT
Used for the neighbor matrix in the class.
Definition: IsingModel.hpp:27
#define RIGHT
Used for the neighbor matrix in the class.
Definition: IsingModel.hpp:29
std::mt19937 engine
The RNG that is used for the Metropolis algorithm.
Definition: IsingModel.hpp:78
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
double energy_diff[17]
An array containing all possible energy differences.
Definition: IsingModel.hpp:58
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
void initialize_engine()
Initialize the RNG.
Definition: IsingModel.cpp:43
IsingModel()
Constructor used for testing.
Definition: IsingModel.cpp:14
int64_t M
The current magnetic strength. unit: Unitless.
Definition: IsingModel.hpp:74
void initialize_energy_diff()
Initialize the energy_diff array with the correct values.
Definition: IsingModel.cpp:81
Type to use with the IsingModel class and montecarlo module.
Definition: data_type.hpp:19