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

Loading...
Searching...
No Matches
IsingModel.cpp
Go to the documentation of this file.
/** @file IsingModel.cpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 0.1
*
* @brief The implementation of the Ising model
*
* @bug No known bugs
* */
#include "IsingModel.hpp"
IsingModel::IsingModel()
{
this -> initialize_engine ( );
}
IsingModel::IsingModel(int L, double T)
{
this -> L = L;
this -> T = T;
this -> initialize_engine ( );
this -> initialize_lattice ( );
this -> initialize_neighbors ( );
this -> initialize_energy_diff ( );
this -> initialize_magnetization ( );
this -> initialize_energy ( );
}
IsingModel::IsingModel(int L, double T, int val)
{
this -> L = L;
this -> T = T;
this -> initialize_engine ( );
this -> initialize_lattice (val);
this -> initialize_neighbors ( );
this -> initialize_energy_diff ( );
this -> initialize_magnetization ( );
this -> initialize_energy ( );
}
void IsingModel::initialize_engine()
{
std::random_device rd{};
this -> engine = std::mt19937{rd()};
}
void IsingModel::initialize_lattice()
{
this->lattice.set_size(this -> L, this -> L);
std::uniform_int_distribution<> coin_flip(0, 1);
for (size_t i = 0; i < this->lattice.n_elem; i++)
this->lattice(i) = 2 * coin_flip(this -> engine) - 1;
}
void IsingModel::initialize_lattice(int val)
{
// If val is neither 1 or -1, then initialize random values.
if (val != 1 && val != -1) {
this -> initialize_lattice ( );
return;
}
this->lattice.set_size(this -> L, this -> L);
this->lattice.fill(val);
}
void IsingModel::initialize_neighbors()
{
this->neighbors.set_size(this -> L, 2);
// Having i as a signed integer is necessary in this case.
for (int i = 0; i < (int)this -> L; i++) {
this->neighbors(i, UP) = INDEX(i - 1, this -> L);
this->neighbors(i, DOWN) = INDEX(i + 1, this -> L);
}
}
void IsingModel::initialize_energy_diff()
{
for (int i = -8; i <= 8; i += 4) {
this -> energy_diff[i+8] = std::exp(-(double)i / this -> T);
}
}
void IsingModel::initialize_magnetization()
{
this -> M = 0.;
for (size_t i = 0; i < this->lattice.n_elem; i++) {
this -> M += this->lattice(i);
}
}
void IsingModel::initialize_energy()
{
this -> E = 0.;
// Loop through the matrix
for (size_t j = 0; j < this -> L; j++) {
for (size_t i = 0; i < this -> L; i++) {
this -> E -= this->lattice(i, j)
* (this->lattice(i, this->neighbors(j, RIGHT))
+ this->lattice(this->neighbors(i, DOWN), j));
}
}
}
data_t IsingModel::Metropolis()
{
int ri, rj;
int dE;
// Create random distribution for indeces
std::uniform_int_distribution<> random_index(0, this->L - 1);
// Create random distribution for acceptance
std::uniform_real_distribution<> random_number(0., 1.);
// Loop over the number of spins
for (size_t i = 0; i < this->lattice.n_elem; i++) {
// Get random indeces
ri = random_index(engine);
rj = random_index(engine);
// Calculate the difference in energy
dE = 2 * this->lattice(ri, rj)
* (this->lattice(ri, this->neighbors(rj, LEFT))
+ this->lattice(ri, this->neighbors(rj, RIGHT))
+ this->lattice(this->neighbors(ri, UP), rj)
+ this->lattice(this->neighbors(ri, DOWN), rj));
// Choose whether or not to accept the new configuration
if (random_number(engine) <= this -> energy_diff[dE+8]) {
// Update if the configuration is accepted
this->lattice(ri, rj) *= -1;
this -> M += 2 * this->lattice(ri, rj);
this -> E += dE;
}
}
return data_t ((double)this -> E , (double)(this -> E * this -> E), (double)this -> M ,
(double)(this -> M * this -> M), std::fabs((double)this -> M));
}
UP
#define UP
Used for the neighbor matrix in the class.
Definition: IsingModel.hpp:26
INDEX
#define INDEX(I, N)
I modulo N.
Definition: IsingModel.hpp:23
DOWN
#define DOWN
Used for the neighbor matrix in the class.
Definition: IsingModel.hpp:28
LEFT
#define LEFT
Used for the neighbor matrix in the class.
Definition: IsingModel.hpp:27
RIGHT
#define RIGHT
Used for the neighbor matrix in the class.
Definition: IsingModel.hpp:29
IsingModel
The Ising model in 2 dimensions.
Definition: IsingModel.hpp:36
IsingModel::engine
std::mt19937 engine
The RNG that is used for the Metropolis algorithm.
Definition: IsingModel.hpp:78
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::IsingModel
IsingModel(int L, double T, int val)
Constructor for the Ising model.
Definition: IsingModel.cpp:31
IsingModel::IsingModel
IsingModel(int L, double T)
Constructor for the Ising model.
Definition: IsingModel.cpp:19
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::energy_diff
double energy_diff[17]
An array containing all possible energy differences.
Definition: IsingModel.hpp:58
IsingModel::initialize_magnetization
void initialize_magnetization()
Initialize the magnetization of the system.
Definition: IsingModel.cpp:88
IsingModel::initialize_engine
void initialize_engine()
Initialize the RNG.
Definition: IsingModel.cpp:43
IsingModel::initialize_lattice
void initialize_lattice(int val)
Initialize the lattice with a specific value.
Definition: IsingModel.cpp:59
IsingModel::IsingModel
IsingModel()
Constructor used for testing.
Definition: IsingModel.cpp:14
IsingModel::M
int64_t M
The current magnetic strength. unit: Unitless.
Definition: IsingModel.hpp:74
IsingModel::initialize_energy_diff
void initialize_energy_diff()
Initialize the energy_diff array with the correct values.
Definition: IsingModel.cpp:81
data_t
Type to use with the IsingModel class and montecarlo module.
Definition: data_type.hpp:19
data_t::data_t
data_t(double E, double E2, double M, double M2, double M_abs)
Constructor with parameters.
Definition: data_type.hpp:45