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
14#include <cmath>
15#include <random>
16
18{
19}
20
21IsingModel::IsingModel(int L, double T)
22{
23 this->L = L;
24 this->T = T;
25 this->initialize_lattice();
29 this->initialize_energy();
30}
31
32IsingModel::IsingModel(int L, double T, int val)
33{
34 this->L = L;
35 this->T = T;
36 this->lattice.set_size(this->L, this->L);
37 this->lattice.fill(val);
41 this->initialize_energy();
42}
43
45{
46 this->lattice.set_size(this->L, this->L);
47 std::random_device rd{};
48 std::mt19937 engine{rd()};
49
50 std::uniform_int_distribution<> coin_flip(0, 1);
51
52 for (size_t i = 0; i < this->lattice.n_elem; i++)
53 this->lattice(i) = 2 * coin_flip(engine) - 1;
54}
55
57{
58 this->neighbors.set_size(this->L, 2);
59
60 // Having i as a signed integer is necessary in this case.
61 for (int i = 0; i < (int)this->L; i++) {
62 this->neighbors(i, UP) = INDEX(i - 1, this->L);
63 this->neighbors(i, DOWN) = INDEX(i + 1, this->L);
64 }
65}
66
68{
69 for (int i = -8; i <= 8; i += 4) {
70 this->energy_diff.insert({i, std::exp(-(double)i / this->T)});
71 }
72}
73
75{
76 this->M = 0.;
77 for (size_t i = 0; i < this->lattice.n_elem; i++) {
78 this->M += this->lattice(i);
79 }
80}
81
83{
84 this->E = 0.;
85
86 // Loop through the matrix
87 for (size_t j = 0; j < this->L; j++) {
88 for (size_t i = 0; i < this->L; i++) {
89 this->E -= this->lattice(i, j)
90 * (this->lattice(i, this->neighbors(j, RIGHT))
91 + this->lattice(this->neighbors(i, DOWN), j));
92 }
93 }
94}
95
97{
98 std::random_device rd{};
99 std::mt19937_64 engine{rd()};
100
101 int ri, rj;
102 int dE;
103
104 // Create random distribution for indeces
105 std::uniform_int_distribution<> random_index(0, this->L - 1);
106 // Create random distribution for acceptance
107 std::uniform_real_distribution<> random_number(0., 1.);
108
109 // Loop over the number of spins
110 for (size_t i = 0; i < this->lattice.n_elem; i++) {
111 // Get random indeces
112 ri = random_index(engine);
113 rj = random_index(engine);
114
115 // Calculate the difference in energy
116 dE = 2 * this->lattice(ri, rj)
117 * (this->lattice(ri, this->neighbors(rj, LEFT))
118 + this->lattice(ri, this->neighbors(rj, RIGHT))
119 + this->lattice(this->neighbors(ri, UP), rj)
120 + this->lattice(this->neighbors(ri, DOWN), rj));
121
122 // Choose whether or not to accept the new configuration
123 if (random_number(engine) <= this->energy_diff[dE]) {
124 // Update if the configuration is accepted
125 this->lattice(ri, rj) *= -1;
126 this->M += 2 * this->lattice(ri, rj);
127 this->E += dE;
128 }
129 }
130
131 return data_t((double)this->E, (double)(this->E * this->E), (double)this->M,
132 (double)(this->M * this->M), std::fabs((double)this->M));
133}
134
136{
137 return this->E;
138}
139
141{
142 return this->M;
143}
The definition of the Ising model.
int M
The current magnetic strength. unit: Unitless.
Definition: IsingModel.hpp:72
std::unordered_map< int, double > energy_diff
A hash map containing all possible energy changes.
Definition: IsingModel.hpp:56
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 get_M()
Get the current magnetization.
Definition: IsingModel.cpp:140
int get_E()
Get the current energy.
Definition: IsingModel.cpp:135
IsingModel()
Constructor used for testing.
Definition: IsingModel.cpp:17
int E
The current energy state. unit: .
Definition: IsingModel.hpp:68
void initialize_energy_diff()
Initialize the hashmap with the correct values.
Definition: IsingModel.cpp:67