This commit is contained in:
Cory Balaton 2023-10-31 20:12:30 +01:00
parent 8083d5eb3b
commit d19ca073a0
No known key found for this signature in database
GPG Key ID: 3E5FCEBFD80F432B
2 changed files with 58 additions and 7 deletions

View File

@ -27,10 +27,11 @@
#define DOWN 1 #define DOWN 1
#define RIGHT 1 #define RIGHT 1
/** @brief The Ising model in 2 dimensions. /** @brief The Ising model in 2 dimensions.
* *
* @details Here we set \f$ J = 1 \f$, and the Boltzmann constant * @details None of the methods are parallelized, as there is very little
* \f$ k_B = 1 \f$. * benefit in doing so.
* */ * */
class IsingModel { class IsingModel {
private: private:
@ -39,16 +40,26 @@ private:
* */ * */
arma::Mat<int> lattice; arma::Mat<int> lattice;
/** @brief \f$ L \cross 2 \f$ matrix with the neighbors of each element
* \f$ x_i \f$.
*
* @details The reason why it's \f$ L \cross 2 \f$ instead of
* \f$ L \cross 2 \f$, is that we can see that we can use the same column
* for the left and upper neighbor, and we can use the same column for the
* right and lower neighbor.
* */
arma::Mat<uint> neighbors; arma::Mat<uint> neighbors;
/** @brief A hash map containing all possible energy changes. /** @brief A hash map containing all possible energy changes.
* */ * */
std::unordered_map<int, double> energy_diff; std::unordered_map<int, double> energy_diff;
/** @brief Temperature /** @brief The temperature of the model.
* */ * */
double T; double T;
/** @brief Size of the lattice.
* */
uint L; uint L;
/** @brief The current energy state. unit: \f$ J \f$. /** @brief The current energy state. unit: \f$ J \f$.
@ -72,32 +83,57 @@ private:
* */ * */
void initialize_lattice(); void initialize_lattice();
/** @brief initialize the neighbors matrix.
* */
void initialize_neighbors(); void initialize_neighbors();
/** @brief Initialize the hashmap with the correct values. /** @brief Initialize the hashmap with the correct values.
* */ * */
void initialize_energy_diff(); void initialize_energy_diff();
/** @brief Initialize the model. /** @brief Initialize the magnetization.
* */ * */
void initialize_magnetization(); void initialize_magnetization();
/** @brief Initialize the energy.
* */
void initialize_energy(); void initialize_energy();
/** @brief Constructor used for testing.
* */
IsingModel();
public: public:
/** @brief Constructor for the Ising model. /** @brief Constructor for the Ising model.
* *
* @param L The size of the lattice. * @param L The size of the lattice.
* @param T The temperature for the system.
* */ * */
IsingModel(uint L, double T); IsingModel(uint L, double T);
/** @brief Constructor for the Ising model. /** @brief Constructor for the Ising model.
* *
* @param L The size of the lattice. * @param L The size of the lattice.
* @param T The temperature for the system.
* @param val The value to set for all spins.
* */ * */
IsingModel(uint L, double T, int val); IsingModel(uint L, double T, int val);
/** @brief The Metropolis algorithm.
* */
void Metropolis(std::mt19937 engine); void Metropolis(std::mt19937 engine);
/** @brief Get the current energy.
*
* @return int
* */
int get_E();
/** @brief Get the current magnetization.
*
* @return int
* */
int get_M();
}; };
#endif #endif

View File

@ -14,8 +14,13 @@
#include "constants.hpp" #include "constants.hpp"
#include <cmath>
#include <random> #include <random>
IsingModel::IsingModel()
{
}
IsingModel::IsingModel(uint L, double T) IsingModel::IsingModel(uint L, double T)
{ {
this->L = L; this->L = L;
@ -55,11 +60,11 @@ void IsingModel::initialize_neighbors()
{ {
this->neighbors.set_size(this->L, 2); this->neighbors.set_size(this->L, 2);
for (int i = 0; i < this->L; i++) { // 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, UP) = INDEX(i - 1, this->L);
this->neighbors(i, DOWN) = INDEX(i + 1, this->L); this->neighbors(i, DOWN) = INDEX(i + 1, this->L);
} }
this->neighbors.print();
} }
void IsingModel::initialize_energy_diff() void IsingModel::initialize_energy_diff()
@ -98,7 +103,7 @@ void IsingModel::Metropolis(std::mt19937 engine)
std::uniform_real_distribution<> random_number(0., 1.); std::uniform_real_distribution<> random_number(0., 1.);
// Loop over the number of spins // Loop over the number of spins
for (int i = 0; i < this->lattice.n_elem; i++) { for (size_t i = 0; i < this->lattice.n_elem; i++) {
// Get random indeces // Get random indeces
ri = random_index(engine); ri = random_index(engine);
rj = random_index(engine); rj = random_index(engine);
@ -119,3 +124,13 @@ void IsingModel::Metropolis(std::mt19937 engine)
} }
} }
} }
int IsingModel::get_E()
{
return this->E;
}
int IsingModel::get_M()
{
return this->M;
}