18 const std::string filename)
25 std::string directory = utils::dirname(filename);
29 std::random_device rd;
30 uint32_t rd_sample = rd();
31 std::mt19937 engine(rd_sample);
33 std::cout <<
"Seed: " << rd_sample << std::endl;
38 utils::mkpath(directory);
42 for (
size_t i = 1; i <= cycles; i++) {
44 tmp = data / (i * n_spins);
45 ofile << i <<
',' << tmp.E <<
',' << tmp.E2 <<
',' << tmp.M <<
','
46 << tmp.M2 <<
',' << tmp.M_abs <<
'\n';
52 const std::string filename)
59 std::string directory = utils::dirname(filename);
63 std::random_device rd;
64 uint32_t rd_sample = rd();
65 std::mt19937 engine(rd());
67 std::cout <<
"Seed: " << rd_sample << std::endl;
72 utils::mkpath(directory);
76 for (
size_t i = 1; i <= cycles; i++) {
78 tmp = data / (i * n_spins);
79 ofile << i <<
',' << tmp.E <<
',' << tmp.E2 <<
',' << tmp.M <<
','
80 << tmp.M2 <<
',' << tmp.M_abs <<
'\n';
85void pd_estimate(
double T,
int L,
int cycles,
const std::string filename)
92 std::string directory = utils::dirname(filename);
98 utils::mkpath(directory);
102 for (
size_t i = 1; i <= cycles; i++) {
104 ofile << data.E <<
',' << data.E2 <<
',' << data.M <<
',' << data.M2
105 <<
',' << data.M_abs <<
'\n';
116 for (
size_t i = 0; i < BURN_IN_TIME; i++) {
121 for (
size_t i = 0; i < (uint)cycles; i++) {
138 for (
size_t i = 0; i < BURN_IN_TIME; i++) {
145#pragma omp for schedule(static) reduction(+ : data)
146 for (
size_t i = 0; i < (uint)cycles; i++) {
151 double norm = 1. / (double)cycles;
157 std::function<
data_t(
int,
double,
int)> monte_carlo,
160 double dt = (end - start) / (
double)points;
167 utils::mkpath(utils::dirname(outfile));
170 double temp, CV, X, E_var, M_var;
172 using utils::scientific_format;
173 for (
size_t i = 0; i < points; i++) {
174 temp = start + dt * i;
175 data = monte_carlo(L, temp, cycles);
176 E_var = (data.E2 - data.E * data.E) / (
double)N;
177 M_var = (data.M2 - data.M_abs * data.M_abs) / (
double)N;
179 ofile << scientific_format(temp) <<
','
180 << scientific_format(data.E / (
double)N) <<
','
181 << scientific_format(data.M_abs / N) <<
','
182 << scientific_format(E_var / (temp * temp)) <<
','
183 << scientific_format(M_var / temp) <<
'\n';
The Ising model in 2 dimensions.
data_t Metropolis()
The Metropolis algorithm.
data_t monte_carlo_serial(int L, double T, int cycles)
Execute the Metropolis algorithm for a certain amount of Monte Carlo cycles.
void monte_carlo_progression(double T, int L, int cycles, const std::string filename)
Write the expected values for each Monte Carlo cycles to file.
data_t monte_carlo_parallel(int L, double T, int cycles)
Execute the Metropolis algorithm for a certain amount of Monte Carlo cycles in parallel.
Functions for monte carlo simulations.
void phase_transition(int L, double start_T, double end_T, int points_T, std::function< data_t(int, double, int)> monte_carlo, std::string outfile)
Perform the MCMC algorithm using a range of temperatures.
void pd_estimate(double T, int L, int cycles, const std::string filename)
Estimate the probability distribution for the energy.