Implementations of different problems

This commit is contained in:
2023-11-08 17:31:14 +01:00
parent 5ac838a266
commit 090510175b
13 changed files with 704 additions and 98 deletions

View File

@@ -12,7 +12,10 @@
#ifndef __ISING_MODEL__
#define __ISING_MODEL__
#include "constants.hpp"
#include "data_type.hpp"
#include "typedefs.hpp"
#include "utils.hpp"
#include <armadillo>
#include <random>
@@ -27,7 +30,6 @@
#define DOWN 1
#define RIGHT 1
/** @brief The Ising model in 2 dimensions.
*
* @details None of the methods are parallelized, as there is very little
@@ -36,15 +38,15 @@
class IsingModel {
private:
friend class IsingModelTest;
/** @brief \f$ L \cross L \f$ matrix where element $ x \in {-1, 1}$.
/** @brief \f$ L \times L \f$ matrix where element \f$ x \in {-1, 1}\f$.
* */
arma::Mat<int> lattice;
/** @brief \f$ L \cross 2 \f$ matrix with the neighbors of each element
/** @brief \f$ L \times 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
* @details The reason why it's \f$ L \times 2 \f$ instead of
* \f$ L \times 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.
* */
@@ -64,19 +66,11 @@ private:
/** @brief The current energy state. unit: \f$ J \f$.
* */
int E;
double E;
/** @brief The current magnetic strength. unit: Unitless.
* */
int M;
/** @brief Energy per spin. unit: \f$ J \f$.
* */
double eps;
/** @brief Magnetization per spin. unit: Unitless.
* */
double m;
double M;
/** @brief Initialize the lattice with a random distribution of 1s and
* -1s.
@@ -121,19 +115,19 @@ public:
/** @brief The Metropolis algorithm.
* */
void Metropolis(std::mt19937 engine);
data_t Metropolis(std::mt19937 &engine);
/** @brief Get the current energy.
*
* @return int
* @return double
* */
int get_E();
double get_E();
/** @brief Get the current magnetization.
*
* @return int
* @return double
* */
int get_M();
double get_M();
};
#endif

78
include/data_type.hpp Normal file
View File

@@ -0,0 +1,78 @@
/** @file data_type.hpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 1.0
*
* @brief Header for the data_t type.
*
* @bug No known bugs
* */
#ifndef __DATA_TYPE__
#define __DATA_TYPE__
#include <sys/types.h>
#include <type_traits>
/** @brief Data structure that contains the data needed for the project*/
struct data_t {
double E = 0.; ///< The expected energy
double M = 0.; ///< The expected magnetization
double E2 = 0.; ///< The expected variance of the energy
double M2 = 0.; ///< The expected variance of magnetization
double M_abs = 0.; ///< The expected absolute magnetization
};
/** @brief Define dividing data_t by a number
*
* @param data The data to divide
* @param num The number to divide data by
*
* @return data_t
* */
template <class T>
data_t operator/(const data_t &data, T num);
// Explicit instantiation
extern template data_t operator/(const data_t &, uint);
extern template data_t operator/(const data_t &, ulong);
extern template data_t operator/(const data_t &,int);
extern template data_t operator/(const data_t &,double);
/** @brief Define /= on data_t by a number.
*
* @param data The data to divide
* @param num The number to divide data by
*
* @return data_t
* */
template <class T>
data_t& operator/=(data_t &data, T num);
// Explicit instantiation
extern template data_t& operator/=(data_t &, uint);
extern template data_t& operator/=(data_t &, ulong);
extern template data_t& operator/=(data_t &,int);
extern template data_t& operator/=(data_t &,double);
/** @brief Define + on data_t by a data_t.
*
* @param a The left side
* @param b The right side
*
* @return data_t
* */
data_t operator+(const data_t &a, const data_t &b);
/** @brief Define += on data_t by a data_t.
*
* @param a The left side
* @param b The right side
*
* @return data_t
* */
data_t& operator+=(data_t &a, const data_t &b);
#endif

View File

@@ -12,8 +12,88 @@
#ifndef __MONTE_CARLO__
#define __MONTE_CARLO__
void burn_in_time();
#include "IsingModel.hpp"
#include "data_type.hpp"
#include "utils.hpp"
void pd_estimate();
#include <functional>
#include <string>
#define BURN_IN_TIME 1000
#define EPS_2 (-2 * std::sinh(8.)) / (std::cosh(8.) + 3)
#define MAG_2 (std::exp(8.) + 1) / (2 * cosh(8.) + 3)
#define CV_2 \
16 \
* (3 * std::cosh(8.) + std::cosh(8.) * std::cosh(8.) \
- std::sinh(8.) * std::sinh(8.)) \
/ ((std::cosh(8.) + 3) * (std::cosh(8.) + 3))
#define X_2 \
(3 * std::exp(8.) + std::exp(-8.) + 3) \
/ ((std::cosh(8.) + 3) * (std::cosh(8.) + 3))
/** @brief Test numerical data with analytical data.
*
* @param tol The tolerance between the analytical and numerical solution.
* @param max_cycles The max number of Monte Carlo cycles.
*
* return uint
* */
uint test_2x2_lattice(double tol, uint max_cycles);
/** @brief Write the expected values for each Monte Carlo cycles to file.
*
* @param T Temperature
* @param L The size of the lattice
* @param cycles The amount of Monte Carlo cycles to do
* @param filename The file to write to
* */
void monte_carlo_progression(double T, uint L, uint cycles,
const std::string filename);
/** @brief Estimate the probability distribution for the energy.
*
* @param T The temperature of the Ising model
* @param L The size of the lattice
* @param cycles The amount of Monte Carlo cycles to do
* @param filename The file to write to
* */
void pd_estimate(double T, uint L, uint cycles, const std::string filename);
/** @brief Execute the Metropolis algorithm for a certain amount of Monte
* Carlo cycles.
*
* @param data The data to store the results
* @param L The size of the lattice
* @param T The Temperature for the Ising model
* @param cycles The amount of Monte Carlo cycles to do*/
void monte_carlo_serial(data_t &data, uint L, double T, uint cycles);
/** @brief Execute the Metropolis algorithm for a certain amount of Monte
* Carlo cycles in parallel.
*
* @param data The data to store the results
* @param L The size of the lattice
* @param T The Temperature for the Ising model
* @param cycles The amount of Monte Carlo cycles to do
* */
void monte_carlo_parallel(data_t &data, uint L, double T, uint cycles);
/** @brief Perform the MCMC algorithm using a range of temperatures.
*
* @param L The size of the lattice
* @param start_T The start temperature
* @param end_T The end temperature
* @param point_T The amount of point to measure
* @param monte_carlo Which Monte Carlo implementation to use
* @param outfile The file to write the data to
* */
void phase_transition(
uint L, double start_T, double end_T, uint points_T,
std::function<void(data_t &, uint, double, uint)> monte_carlo,
std::string outfile);
#endif

View File

@@ -5,9 +5,10 @@
*
* @version 1.0
*
* @brief Function prototypes and macros that are useful.
* @brief A small test library.
*
* @details This a small testing library that is tailored for the needs of the project.
* @details This a small testing library that is tailored for the needs of the
* project.
*
* @bug No known bugs
* */
@@ -27,8 +28,9 @@
* assertion function than the regular assert function from cassert.
* */
#define ASSERT(expr, msg) \
m_assert(expr, #expr, __METHOD_NAME__, __FILE__, __LINE__, msg)
details::m_assert(expr, #expr, __METHOD_NAME__, __FILE__, __LINE__, msg)
namespace details {
/** @brief Test an expression, confirm that test is ok, or abort execution.
*
* @details This function takes in an expression and prints an OK message if
@@ -43,7 +45,9 @@
* */
void m_assert(bool expr, std::string expr_str, std::string func,
std::string file, int line, std::string msg);
} // namespace details
namespace testlib {
/** @brief Test if two armadillo matrices/vectors are close to each other.
*
* @details This function takes in 2 matrices/vectors and checks if they are
@@ -64,12 +68,29 @@ static bool close_to(arma::Mat<T> &a, arma::Mat<T> &b, double tol = 1e-8)
}
for (size_t i = 0; i < a.n_elem; i++) {
if (std::abs(a(i) - b(i)) >= tol) {
if (!close_to(a(i), b(i))) {
return false;
}
}
return true;
}
/** @brief Test if two numbers are close to each other.
*
* @details This function takes in 2 matrices/vectors and checks if they are
* approximately equal to each other given a tolerance.
*
* @param a Matrix/vector a
* @param b Matrix/vector b
* @param tol The tolerance
*
* @return bool
* */
template <class T,
class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
static bool close_to(T a, T b, double tol = 1e-8)
{
return std::abs(a - b) < tol;
}
/** @brief Test if two armadillo matrices/vectors are equal.
@@ -93,10 +114,11 @@ static bool is_equal(arma::Mat<T> &a, arma::Mat<T> &b)
}
return true;
}
/** @brief Test that all elements fulfill the condition.
*
* @param expr The boolean expression to apply to each element
* @param M The matrix/vector to iterate over
* @param M The matrix/vector to iterate over
*
* @return bool
* */
@@ -111,5 +133,5 @@ static bool assert_each(std::function<bool(T)> expr, arma::Mat<T> &M)
}
return true;
}
} // namespace testlib
#endif

View File

@@ -40,7 +40,31 @@
/** @def __METHOD_NAME__
* @brief Get the name of the current method/function without the return type.
* */
#define __METHOD_NAME__ methodName(__PRETTY_FUNCTION__)
#define __METHOD_NAME__ details::methodName(__PRETTY_FUNCTION__)
namespace details {
/** @brief Takes in the __PRETTY_FUNCTION__ string and removes the return type.
*
* @details This function should only be used for the __METHOD_NAME__ macro,
* since it takes the output from __PRETTY_FUNCTION__ and strips the return
* type.
*
* @param pretty_function The string from __PRETTY_FUNCTION__
*
* @return std::string
* */
inline std::string methodName(const std::string &pretty_function)
{
size_t colons = pretty_function.find("::");
size_t begin = pretty_function.substr(0, colons).rfind(" ") + 1;
size_t end = pretty_function.rfind("(") - begin;
return pretty_function.substr(begin, end) + "()";
}
} // namespace details
namespace utils {
/** @brief Turns a double into a string written in scientific format.
*
@@ -68,26 +92,6 @@ std::string scientific_format(double d, int width = 20, int prec = 10);
std::string scientific_format(const std::vector<double> &v, int width = 20,
int prec = 10);
/** @brief Takes in the __PRETTY_FUNCTION__ string and removes the return type.
*
* @details This function should only be used for the __METHOD_NAME__ macro,
* since it takes the output from __PRETTY_FUNCTION__ and strips the return
* type.
*
* @param pretty_function The string from __PRETTY_FUNCTION__
*
* @return std::string
* */
static inline std::string methodName(const std::string &pretty_function)
{
size_t colons = pretty_function.find("::");
size_t begin = pretty_function.substr(0, colons).rfind(" ") + 1;
size_t end = pretty_function.rfind("(") - begin;
return pretty_function.substr(begin, end) + "()";
}
/** @brief Make path given.
*
* @details This tries to be the equivalent to "mkdir -p" and creates a new
@@ -100,4 +104,14 @@ static inline std::string methodName(const std::string &pretty_function)
* */
bool mkpath(std::string path, int mode = 0777);
/** @brief Get the directory name of the path
*
* @param path The path to use.
*
* @return string
* */
std::string dirname(const std::string &path);
} // namespace utils
#endif