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
17 IsingModel::IsingModel()
18{
19}
20
21 IsingModel::IsingModel(int L, double T)
22{
23 this->L = L;
24 this->T = T;
25 this->initialize_lattice();
26 this->initialize_neighbors();
27 this->initialize_energy_diff();
28 this->initialize_magnetization();
29 this->initialize_energy();
30}
31
32 IsingModel::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);
38 this->initialize_neighbors();
39 this->initialize_energy_diff();
40 this->initialize_magnetization();
41 this->initialize_energy();
42}
43
44 void IsingModel::initialize_lattice()
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
56 void IsingModel::initialize_neighbors()
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
67 void IsingModel::initialize_energy_diff()
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
74 void IsingModel::initialize_magnetization()
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
82 void IsingModel::initialize_energy()
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
96 data_t IsingModel::Metropolis()
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
135 int IsingModel::get_E()
136{
137 return this->E;
138}
139
140 int IsingModel::get_M()
141{
142 return this->M;
143}
IsingModel.hpp
The definition of the Ising model.
IsingModel::M
int M
The current magnetic strength. unit: Unitless.
Definition: IsingModel.hpp:72
IsingModel::energy_diff
std::unordered_map< int, double > energy_diff
A hash map containing all possible energy changes.
Definition: IsingModel.hpp:56
IsingModel::T
double T
The temperature of the model.
Definition: IsingModel.hpp:60
IsingModel::L
int L
Size of the lattice.
Definition: IsingModel.hpp:64
IsingModel::lattice
arma::Mat< int > lattice
matrix where element .
Definition: IsingModel.hpp:42
IsingModel::initialize_lattice
void initialize_lattice()
Initialize the lattice with a random distribution of 1s and -1s.
Definition: IsingModel.cpp:44
IsingModel::Metropolis
data_t Metropolis()
The Metropolis algorithm.
Definition: IsingModel.cpp:96
IsingModel::initialize_energy
void initialize_energy()
Initialize the energy.
Definition: IsingModel.cpp:82
IsingModel::initialize_neighbors
void initialize_neighbors()
initialize the neighbors matrix.
Definition: IsingModel.cpp:56
IsingModel::initialize_magnetization
void initialize_magnetization()
Initialize the magnetization.
Definition: IsingModel.cpp:74
IsingModel::neighbors
arma::Mat< int > neighbors
matrix with the neighbors of each element .
Definition: IsingModel.hpp:52
IsingModel::get_M
int get_M()
Get the current magnetization.
Definition: IsingModel.cpp:140
IsingModel::get_E
int get_E()
Get the current energy.
Definition: IsingModel.cpp:135
IsingModel::IsingModel
IsingModel()
Constructor used for testing.
Definition: IsingModel.cpp:17
IsingModel::E
int E
The current energy state. unit: .
Definition: IsingModel.hpp:68
IsingModel::initialize_energy_diff
void initialize_energy_diff()
Initialize the hashmap with the correct values.
Definition: IsingModel.cpp:67
data_t
Definition: data_type.hpp:18