Update code

This commit is contained in:
2023-11-21 11:24:34 +01:00
parent 816e38e9e4
commit f07fb8829b
12 changed files with 522 additions and 343 deletions

View File

@@ -11,11 +11,14 @@
* */
#include "IsingModel.hpp"
#include <cmath>
#include <random>
IsingModel::IsingModel()
{
}
IsingModel::IsingModel(uint L, double T)
IsingModel::IsingModel(int L, double T)
{
this->L = L;
this->T = T;
@@ -26,7 +29,7 @@ IsingModel::IsingModel(uint L, double T)
this->initialize_energy();
}
IsingModel::IsingModel(uint L, double T, int val)
IsingModel::IsingModel(int L, double T, int val)
{
this->L = L;
this->T = T;
@@ -41,13 +44,13 @@ IsingModel::IsingModel(uint L, double T, int val)
void IsingModel::initialize_lattice()
{
this->lattice.set_size(this->L, this->L);
std::random_device rd;
std::mt19937 engine(rd());
std::random_device rd{};
std::mt19937 engine{rd()};
std::uniform_int_distribution<int> coin_flip(0, 1);
std::uniform_int_distribution<> coin_flip(0, 1);
for (size_t i = 0; i < this->lattice.n_elem; i++)
this->lattice(i) = coin_flip(engine) == 1 ? 1 : -1;
this->lattice(i) = 2 * coin_flip(engine) - 1;
}
void IsingModel::initialize_neighbors()
@@ -64,7 +67,7 @@ void IsingModel::initialize_neighbors()
void IsingModel::initialize_energy_diff()
{
for (int i = -8; i <= 8; i += 4) {
this->energy_diff.insert({i, std::exp(-((double)i / this->T))});
this->energy_diff.insert({i, std::exp(-(double)i / this->T)});
}
}
@@ -72,7 +75,7 @@ void IsingModel::initialize_magnetization()
{
this->M = 0.;
for (size_t i = 0; i < this->lattice.n_elem; i++) {
this->M += (double)this->lattice(i);
this->M += this->lattice(i);
}
}
@@ -83,21 +86,23 @@ void IsingModel::initialize_energy()
// Loop through the matrix
for (size_t j = 0; j < this->L; j++) {
for (size_t i = 0; i < this->L; i++) {
this->E -= (double)this->lattice(i, j)
this->E -= this->lattice(i, j)
* (this->lattice(i, this->neighbors(j, RIGHT))
+ this->lattice(this->neighbors(i, DOWN), j));
}
}
}
data_t IsingModel::Metropolis(std::mt19937 &engine)
data_t IsingModel::Metropolis()
{
uint ri, rj;
std::random_device rd{};
std::mt19937_64 engine{rd()};
int ri, rj;
int dE;
data_t res;
// Create random distribution for indeces
std::uniform_int_distribution<uint> random_index(0, this->L - 1);
std::uniform_int_distribution<> random_index(0, this->L - 1);
// Create random distribution for acceptance
std::uniform_real_distribution<> random_number(0., 1.);
@@ -118,25 +123,21 @@ data_t IsingModel::Metropolis(std::mt19937 &engine)
if (random_number(engine) <= this->energy_diff[dE]) {
// Update if the configuration is accepted
this->lattice(ri, rj) *= -1;
this->M += 2. * (double)this->lattice(ri, rj);
this->E += (double)dE;
this->M += 2 * this->lattice(ri, rj);
this->E += dE;
}
}
res.E = this->E;
res.E2 = this->E * this->E;
res.M = this->M;
res.M2 = this->M * this->M;
res.M_abs = std::abs(this->M);
return res;
return data_t((double)this->E, (double)(this->E * this->E), (double)this->M,
(double)(this->M * this->M), std::fabs((double)this->M));
}
double IsingModel::get_E()
int IsingModel::get_E()
{
return this->E;
}
double IsingModel::get_M()
int IsingModel::get_M()
{
return this->M;
}

View File

@@ -16,7 +16,7 @@ OPENMP=-fopenmp
# Add a debug flag when compiling (For the DEBUG macro in utils.hpp)
DEBUG ?= 0
ifeq ($(DEBUG), 1)
DBGFLAG=-DDBG
DBGFLAG=-DDBG -g
else
DBGFLAG=
endif

View File

@@ -10,63 +10,3 @@
* @bug No known bugs
* */
#include "data_type.hpp"
template <class T>
data_t operator/(const data_t &data, T num)
{
data_t res = data;
res.E /= num;
res.E2 /= num;
res.M /= num;
res.M2 /= num;
res.M_abs /= num;
return res;
}
// Explicit instantiation
template data_t operator/(const data_t &, uint);
template data_t operator/(const data_t &, ulong);
template data_t operator/(const data_t &,int);
template data_t operator/(const data_t &,double);
template <class T>
data_t& operator/=(data_t &data, T num)
{
data.E /= num;
data.E2 /= num;
data.M /= num;
data.M2 /= num;
data.M_abs /= num;
return data;
}
// Explicit instantiation
template data_t& operator/=(data_t &, uint);
template data_t& operator/=(data_t &, ulong);
template data_t& operator/=(data_t &,int);
template data_t& operator/=(data_t &,double);
data_t operator+(const data_t &a, const data_t &b)
{
data_t res = a;
res.E += b.E;
res.E2 += b.E2;
res.M += b.M;
res.M2 += b.M2;
res.M_abs += b.M_abs;
return res;
}
data_t& operator+=(data_t &a, const data_t &b)
{
a.E += b.E;
a.E2 += b.E2;
a.M += b.M;
a.M2 += b.M2;
a.M_abs += b.M_abs;
return a;
}

View File

@@ -9,35 +9,105 @@
*
* @bug No known bugs
* */
#include "data_type.hpp"
#include "monte_carlo.hpp"
#include "utils.hpp"
#include <csignal>
#include <cstdlib>
#include <iostream>
#include <omp.h>
/** @brief The main function.*/
int main()
void create_burn_in_time_data()
{
// uint test_cycles = test_2x2_lattice(1e-7, 10000);
// monte_carlo(1.0, 2, 10000, "output/2_lattice_test.txt");
// Test burn-in time
monte_carlo_progression(1.0, 20, 10000, "output/burn_in_time_1_0.txt");
monte_carlo_progression(2.4, 20, 10000, "output/burn_in_time_2_4.txt");
monte_carlo_progression(1.0, 20, 20000,
"output/burn_in_time/unordered_1_0.txt");
monte_carlo_progression(1.0, 20, 20000, 1,
"output/burn_in_time/ordered_1_0.txt");
monte_carlo_progression(2.4, 20, 20000,
"output/burn_in_time/unordered_2_4.txt");
monte_carlo_progression(2.4, 20, 20000, 1,
"output/burn_in_time/ordered_2_4.txt");
}
void create_pd_estimate_data()
{
// Estimate pd
pd_estimate(1.0, 20, 1000000, "output/pd_estimate/estimate_1_0.txt");
pd_estimate(2.4, 20, 1000000, "output/pd_estimate/estimate_2_4.txt");
}
void test_parallel_speedup()
{
// Test the openmp speedup
data_t data;
double t0, t1, t2;
int tries = 5;
t0 = omp_get_wtime();
phase_transition(20, 2.1, 2.4, 1000, monte_carlo_serial,
"output/phase_transition/size_20.txt");
for (size_t i = 0; i < tries; i++)
monte_carlo_serial(20, 1.0, 10000);
t1 = omp_get_wtime();
phase_transition(20, 2.1, 2.4, 1000, monte_carlo_parallel,
"output/phase_transition/size_20.txt");
for (size_t i = 0; i < tries; i++)
monte_carlo_parallel(20, 1.0, 10000);
t2 = omp_get_wtime();
std::cout << "Time serial : " << t1 - t0 << " seconds" << '\n';
std::cout << "Time parallel : " << t2 - t1 << " seconds" << '\n';
std::cout << "Time serial : " << (t1 - t0) / tries << " seconds"
<< '\n';
std::cout << "Time parallel : " << (t2 - t1) / tries << " seconds"
<< '\n';
std::cout << "Speedup parallel: " << (t1 - t0) / (t2 - t1) << '\n';
}
void create_phase_transition_data()
{
double t0, t1;
t0 = omp_get_wtime();
// Phase transition
phase_transition(20, 2.1, 2.4, 40, monte_carlo_parallel,
"output/phase_transition/size_20.txt");
phase_transition(40, 2.1, 2.4, 40, monte_carlo_parallel,
"output/phase_transition/size_40.txt");
phase_transition(60, 2.1, 2.4, 40, monte_carlo_parallel,
"output/phase_transition/size_60.txt");
phase_transition(80, 2.1, 2.4, 40, monte_carlo_parallel,
"output/phase_transition/size_80.txt");
phase_transition(100, 2.1, 2.4, 40, monte_carlo_parallel,
"output/phase_transition/size_100.txt");
t1 = omp_get_wtime();
std::cout << "Time: " << t1 - t0 << std::endl;
}
/** @brief The main function.*/
int main(int argc, char **argv)
{
if (argc < 2) {
std::cout << "Need at least 1 argument, got " << argc - 1
<< " arguments." << std::endl;
abort();
}
int arg = atoi(argv[1]);
switch (arg) {
case 1:
create_burn_in_time_data();
break;
case 2:
create_pd_estimate_data();
break;
case 3:
test_parallel_speedup();
break;
case 4:
create_phase_transition_data();
break;
default:
std::cout << "Not a valid option!" << std::endl;
abort();
}
return 0;
}

View File

@@ -11,50 +11,15 @@
* */
#include "monte_carlo.hpp"
uint test_2x2_lattice(double tol, uint max_cycles)
{
data_t data, tmp;
size_t L = 2;
size_t n_spins = L * L;
double T = 1.;
size_t cycles = 0;
#include <cmath>
#include <cstdint>
// Create random engine using the mersenne twister
std::random_device rd;
std::mt19937 engine(rd());
IsingModel test(L, T);
double E, M;
std::function<bool(double, double)> is_close = [tol](double a, double b) {
return std::abs(a - b) < tol;
};
// Loop through cycles
while (cycles++ < max_cycles) {
data += test.Metropolis(engine);
tmp = data / (cycles * n_spins);
// if (close(EPS_2, tmp.E)
//&& close(MAG_2, tmp.M)
//&& close(CV_2, (tmp.E2 - tmp.E * tmp.E) / (T * T))
//&& close(X_2, (tmp.M2 - tmp.M_abs * tmp.M_abs) / T)) {
// return cycles;
//}
if (is_close(EPS_2, tmp.E) && is_close(MAG_2, tmp.M)) {
return cycles;
}
}
std::cout << "hello" << std::endl;
return 0;
}
void monte_carlo_progression(double T, uint L, uint cycles,
void monte_carlo_progression(double T, int L, int cycles,
const std::string filename)
{
// Set some variables
data_t data, tmp;
uint n_spins = L * L;
int n_spins = L * L;
// File stuff
std::string directory = utils::dirname(filename);
@@ -62,7 +27,10 @@ void monte_carlo_progression(double T, uint L, uint cycles,
// Create random engine using the mersenne twister
std::random_device rd;
std::mt19937 engine(rd());
uint32_t rd_sample = rd();
std::mt19937 engine(rd_sample);
std::cout << "Seed: " << rd_sample << std::endl;
IsingModel ising(L, T);
@@ -72,7 +40,7 @@ void monte_carlo_progression(double T, uint L, uint cycles,
// Loop through cycles
for (size_t i = 1; i <= cycles; i++) {
data += ising.Metropolis(engine);
data += ising.Metropolis();
tmp = data / (i * n_spins);
ofile << i << ',' << tmp.E << ',' << tmp.E2 << ',' << tmp.M << ','
<< tmp.M2 << ',' << tmp.M_abs << '\n';
@@ -80,11 +48,12 @@ void monte_carlo_progression(double T, uint L, uint cycles,
ofile.close();
}
void pd_estimate(double T, uint L, uint cycles, const std::string filename)
void monte_carlo_progression(double T, int L, int cycles, int value,
const std::string filename)
{
// Set some variables
data_t data;
uint n_spins = L * L;
data_t data, tmp;
int n_spins = L * L;
// File stuff
std::string directory = utils::dirname(filename);
@@ -92,104 +61,126 @@ void pd_estimate(double T, uint L, uint cycles, const std::string filename)
// Create random engine using the mersenne twister
std::random_device rd;
uint32_t rd_sample = rd();
std::mt19937 engine(rd());
std::cout << "Seed: " << rd_sample << std::endl;
IsingModel ising(L, T, value);
// Create path and open file
utils::mkpath(directory);
ofile.open(filename);
// Loop through cycles
for (size_t i = 1; i <= cycles; i++) {
data += ising.Metropolis();
tmp = data / (i * n_spins);
ofile << i << ',' << tmp.E << ',' << tmp.E2 << ',' << tmp.M << ','
<< tmp.M2 << ',' << tmp.M_abs << '\n';
}
ofile.close();
}
void pd_estimate(double T, int L, int cycles, const std::string filename)
{
// Set some variables
data_t data, tmp;
int n_spins = L * L;
// File stuff
std::string directory = utils::dirname(filename);
std::ofstream ofile;
IsingModel ising(L, T);
// Create path and open file
utils::mkpath(directory);
ofile.open(filename);
double E, M;
// Figure out bin widths and such
// Loop through cycles
for (size_t i = 1; i <= cycles; i++) {
data = ising.Metropolis() / n_spins;
ofile << data.E << ',' << data.E2 << ',' << data.M << ',' << data.M2
<< ',' << data.M_abs << '\n';
}
ofile.close();
}
// Code for seeing phase transitions.
void monte_carlo_serial(data_t &data, uint L, double T, uint cycles)
data_t monte_carlo_serial(int L, double T, int cycles)
{
data_t data;
IsingModel model(L, T);
// Create random engine using the mersenne twister
std::random_device rd;
std::mt19937 engine(rd());
for (size_t i = 0; i < BURN_IN_TIME; i++) {
model.Metropolis(engine);
model.Metropolis();
}
for (size_t i = 0; i < cycles; i++) {
data += model.Metropolis(engine);
double E, M;
for (size_t i = 0; i < (uint)cycles; i++) {
data += model.Metropolis();
}
data /= cycles;
return data;
}
void monte_carlo_parallel(data_t &data, uint L, double T, uint cycles)
data_t monte_carlo_parallel(int L, double T, int cycles)
{
data_t data;
#pragma omp parallel
{
// Each thread creates an instance of IsingModel.
IsingModel model(L, T);
// Each thread creates an instance of the mersenne twister
std::random_device rd;
std::mt19937 engine(rd());
data_t tmp;
// Each thread runs the Metropolis algorithm before starting to collect
// samples
for (size_t i = 0; i < BURN_IN_TIME; i++) {
model.Metropolis(engine);
model.Metropolis();
}
// Now each thread work on one loop together, but using their own
// instances of things, but the total of cycles add up.
// static ensure that each thread gets the same amount of iterations
#pragma omp for schedule(static)
for (size_t i = 0; i < cycles; i++) {
tmp = tmp + model.Metropolis(engine);
}
// Combine all the data.
#pragma omp critical
{
data += tmp;
#pragma omp for schedule(static) reduction(+ : data)
for (size_t i = 0; i < (uint)cycles; i++) {
data += model.Metropolis();
}
}
data /= cycles;
double norm = 1. / (double)cycles;
return data * norm;
}
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)
void phase_transition(int L, double start, double end, int points,
std::function<data_t(int, double, int)> monte_carlo,
std::string outfile)
{
double dt_T = (end_T - start_T) / points_T;
uint cycles = 10000;
uint N = L * L;
double dt = (end - start) / (double)points;
int cycles = 10000;
int N = L * L;
std::ofstream ofile;
data_t data[points_T];
for (size_t i = 0; i < points_T; i++) {
monte_carlo(data[i], L, start_T + dt_T * i, cycles);
}
data_t data;
utils::mkpath(utils::dirname(outfile));
ofile.open(outfile);
double temp, CV, X;
double temp, CV, X, E_var, M_var;
using utils::scientific_format;
for (size_t i = 0; i < points_T; i++) {
temp = start_T + dt_T * i;
CV = (data[i].E2 - data[i].E * data[i].E) / (N * temp * temp);
X = (data[i].M2 - data[i].M_abs * data[i].M_abs) / (N * temp);
for (size_t i = 0; i < points; i++) {
temp = start + dt * i;
data = monte_carlo(L, temp, cycles);
E_var = (data.E2 - data.E * data.E) / (double)N;
M_var = (data.M2 - data.M_abs * data.M_abs) / (double)N;
ofile << scientific_format(temp) << ','
<< scientific_format(data[i].E / N) << ','
<< scientific_format(data[i].M_abs / N) << ','
<< scientific_format(CV) << ',' << scientific_format(X) << '\n';
<< scientific_format(data.E / (double)N) << ','
<< scientific_format(data.M_abs / N) << ','
<< scientific_format(E_var / (temp * temp)) << ','
<< scientific_format(M_var / temp) << '\n';
}
ofile.close();
}

View File

@@ -18,15 +18,22 @@
#include <iostream>
#include <iterator>
#include <mpi.h>
#include <sstream>
/** @brief The main function*/
int main(int argc, char **argv)
{
double start = 1., end = 3.;
uint points = 1000, L = 20, N;
if (argc < 5) {
std::cout << "You need at least 4 arguments" << std::endl;
abort();
}
double t0, t1;
t0 = MPI_Wtime();
double start = atof(argv[1]), end = atof(argv[2]);
int points = atoi(argv[3]), N;
int lattice_sizes[] = {20, 40, 60, 80, 100};
double dt = (end - start) / points;
uint cycles = 10000;
N = L * L;
int cycles = atoi(argv[4]);
std::ofstream ofile;
data_t data[points];
@@ -41,10 +48,10 @@ int main(int argc, char **argv)
MPI_Comm_size(MPI_COMM_WORLD, &cluster_size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
uint remainder = points % cluster_size;
int remainder = points % cluster_size;
double i_start;
uint i_points;
// The last
int i_points;
// Distribute temperature points
if (rank < remainder) {
i_points = points / cluster_size + 1;
i_start = start + dt * i_points * rank;
@@ -57,52 +64,65 @@ int main(int argc, char **argv)
data_t i_data[i_points];
std::cout << "Rank " << rank << ": " << i_points << ',' << i_start << '\n';
for (size_t i = 0; i < i_points; i++) {
monte_carlo_serial(i_data[i], L, i_start + dt * i, cycles);
}
for (int L : lattice_sizes) {
N = L * L;
for (size_t i = 0; i < i_points; i++) {
i_data[i] = monte_carlo_parallel(L, i_start + dt * i, cycles);
}
if (rank == 0) {
std::copy_n(i_data, i_points, data);
for (size_t i = 1; i < cluster_size; i++) {
if (rank < remainder) {
MPI_Recv((void *)i_data,
sizeof(data_t) * (points / cluster_size + 1), MPI_CHAR,
i, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
std::copy_n(i_data, points / cluster_size + 1,
data + (points / cluster_size) * i);
if (rank == 0) {
std::copy_n(i_data, i_points, data);
for (size_t i = 1; i < cluster_size; i++) {
if (rank < remainder) {
MPI_Recv((void *)i_data,
sizeof(data_t) * (points / cluster_size + 1),
MPI_CHAR, i, MPI_ANY_TAG, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
std::copy_n(i_data, points / cluster_size + 1,
data + (points / cluster_size) * i);
}
else {
MPI_Recv((void *)i_data,
sizeof(data_t) * (points / cluster_size), MPI_CHAR,
i, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
std::copy_n(i_data, points / cluster_size,
data + (points / cluster_size) * i + remainder);
}
}
else {
MPI_Recv((void *)i_data,
sizeof(data_t) * (points / cluster_size), MPI_CHAR, i,
MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
std::copy_n(i_data, points / cluster_size,
data + (points / cluster_size) * i + remainder);
std::stringstream outfile;
outfile << "output/phase_transition/size_" << L << ".txt";
utils::mkpath(utils::dirname(outfile.str()));
ofile.open(outfile.str());
double temp, CV, X;
using utils::scientific_format;
for (size_t i = 0; i < points; i++) {
temp = start + dt * i;
CV = (data[i].E2 - data[i].E * data[i].E)
/ ((double)N * temp * temp);
X = (data[i].M2 - data[i].M_abs * data[i].M_abs)
/ ((double)N * temp);
ofile << scientific_format(temp) << ','
<< scientific_format(data[i].E / N) << ','
<< scientific_format(data[i].M_abs / N) << ','
<< scientific_format(CV) << ',' << scientific_format(X)
<< '\n';
}
ofile.close();
}
else {
MPI_Send(i_data, i_points * sizeof(data_t), MPI_CHAR, 0, rank,
MPI_COMM_WORLD);
}
}
else {
MPI_Send(i_data, i_points * sizeof(data_t), MPI_CHAR, 0, rank,
MPI_COMM_WORLD);
t1 = MPI_Wtime();
if (rank == 0) {
std::cout << "Time: " << t1 - t0 << " seconds\n";
}
MPI_Finalize();
std::string outfile = "output/phase_transition/size_20.txt";
utils::mkpath(utils::dirname(outfile));
ofile.open(outfile);
double temp, CV, X;
using utils::scientific_format;
for (size_t i = 0; i < points; i++) {
temp = start + dt * i;
CV = (data[i].E2 - data[i].E * data[i].E) / (N * temp * temp);
X = (data[i].M2 - data[i].M_abs * data[i].M_abs) / (N * temp);
ofile << scientific_format(temp) << ','
<< scientific_format(data[i].E / N) << ','
<< scientific_format(data[i].M_abs / N) << ','
<< scientific_format(CV) << ',' << scientific_format(X) << '\n';
}
ofile.close();
}

View File

@@ -12,6 +12,19 @@
#include "IsingModel.hpp"
#include "testlib.hpp"
#include <fstream>
#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.) + 1) / ((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 class for the Ising model
* */
class IsingModelTest {
@@ -31,7 +44,7 @@ public:
"Test lattice initialization.");
test.initialize_neighbors();
arma::Mat<uint> neighbor_matrix("2, 1 ; 0, 2 ; 1, 0");
arma::Mat<int> neighbor_matrix("2, 1 ; 0, 2 ; 1, 0");
ASSERT(testlib::is_equal(neighbor_matrix, test.neighbors),
"Test neighbor matrix.");
@@ -40,11 +53,94 @@ public:
// Test the initial magnetization.
test.initialize_magnetization();
ASSERT(std::abs(test.M - 9.) < 1e-8, "Test intial magnetization");
ASSERT(std::fabs(test.M - 9.) < 1e-8, "Test intial magnetization");
// Test that the initial energy is correct
test.initialize_energy();
ASSERT(std::abs(test.E - (-18)) < 1e-8, "Test initial energy.");
ASSERT(std::fabs(test.E - (-18)) < 1e-8, "Test initial energy.");
}
/** @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 int
* */
int test_2x2_lattice(double tol, int max_cycles)
{
data_t data, tmp;
size_t L = 2;
size_t n_spins = L * L;
double T = 1.;
size_t cycles = 0;
// Create random engine using the mersenne twister
std::random_device rd;
std::mt19937 engine(rd());
IsingModel test(L, T);
int arr[]{0, 0, 0, 0};
// Loop through cycles
//std::ofstream ofile;
//ofile.open("output/test_2x2.txt");
while (cycles++ < max_cycles) {
data += test.Metropolis();
tmp = data / cycles;
//ofile << cycles << ',' << tmp.E / n_spins << ','
//<< tmp.M_abs / n_spins << ','
//<< (tmp.E2 - tmp.E * tmp.E) / (T * T) / n_spins << ','
//<< (tmp.M2 - tmp.M_abs * tmp.M_abs) / T / n_spins << '\n';
if (testlib::close_to(EPS_2, tmp.E / n_spins, tol)
&& testlib::close_to(MAG_2, tmp.M_abs / n_spins, tol)
&& testlib::close_to(CV_2, (tmp.E2 - tmp.E * tmp.E) / (T * T)
/ n_spins, tol)
&& testlib::close_to(X_2, (tmp.M2 - tmp.M_abs * tmp.M_abs) / T
/ n_spins, tol)) {
return cycles;
}
}
//std::cout << EPS_2 << ',' << MAG_2 << ',' << CV_2 << ',' << X_2
//<< std::endl;
//ofile.close();
// cycles = 0;
// data = 0;
// IsingModel test_mag(L, T);
// while (cycles++ < max_cycles) {
// data += test.Metropolis();
// tmp = data / (cycles * n_spins);
// if (testlib::close_to(MAG_2, tmp.M, tol)) {
// arr[1] = cycles;
// break;
//}
//}
// cycles = 0;
// data = 0;
// IsingModel test_CV(L, T);
// while (cycles++ < max_cycles) {
// data += test.Metropolis();
// tmp = data / (cycles * n_spins);
// if (testlib::close_to(CV_2, (tmp.E2 - tmp.E * tmp.E) / (T * T),
// tol)) {
// arr[2] = cycles;
// break;
//}
//}
// cycles = 0;
// data = 0;
// IsingModel test_X(L, T);
// while (cycles++ < max_cycles) {
// data += test.Metropolis();
// tmp = data / (cycles * n_spins);
// if (testlib::close_to(X_2, (tmp.M2 - tmp.M_abs * tmp.M_abs) / T,
// tol)) {
// arr[3] = cycles;
// break;
//}
//}
return 0;
}
};
@@ -54,4 +150,18 @@ int main()
IsingModelTest test;
test.test_init_functions();
int res = 0;
int tmp;
for (size_t i=0; i < 1000; i++) {
tmp = test.test_2x2_lattice(1e-2, 1e5);
if (tmp == 0) {
std::cout << "not enough cycles\n";
break;
}
res += tmp;
}
std::cout << "Res: " << res / 1000 << std::endl;
return 0;
}