36 this->
lattice.set_size(this->L, this->L);
47 std::random_device rd{};
48 std::mt19937 engine{rd()};
50 std::uniform_int_distribution<> coin_flip(0, 1);
52 for (
size_t i = 0; i < this->
lattice.n_elem; i++)
53 this->
lattice(i) = 2 * coin_flip(engine) - 1;
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);
69 for (
int i = -8; i <= 8; i += 4) {
70 this->
energy_diff.insert({i, std::exp(-(
double)i / this->
T)});
77 for (
size_t i = 0; i < this->
lattice.n_elem; i++) {
87 for (
size_t j = 0; j < this->
L; j++) {
88 for (
size_t i = 0; i < this->
L; i++) {
98 std::random_device rd{};
99 std::mt19937_64 engine{rd()};
105 std::uniform_int_distribution<> random_index(0, this->
L - 1);
107 std::uniform_real_distribution<> random_number(0., 1.);
110 for (
size_t i = 0; i < this->
lattice.n_elem; i++) {
112 ri = random_index(engine);
113 rj = random_index(engine);
123 if (random_number(engine) <= this->
energy_diff[dE]) {
126 this->
M += 2 * this->
lattice(ri, rj);
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));
The definition of the Ising model.
int M
The current magnetic strength. unit: Unitless.
std::unordered_map< int, double > energy_diff
A hash map containing all possible energy changes.
double T
The temperature of the model.
int L
Size of the lattice.
arma::Mat< int > lattice
matrix where element .
void initialize_lattice()
Initialize the lattice with a random distribution of 1s and -1s.
data_t Metropolis()
The Metropolis algorithm.
void initialize_energy()
Initialize the energy.
void initialize_neighbors()
initialize the neighbors matrix.
void initialize_magnetization()
Initialize the magnetization.
arma::Mat< int > neighbors
matrix with the neighbors of each element .
int get_M()
Get the current magnetization.
int get_E()
Get the current energy.
IsingModel()
Constructor used for testing.
int E
The current energy state. unit: .
void initialize_energy_diff()
Initialize the hashmap with the correct values.