2 Dimensional Ising Model Simulate the change in energy and magnetization in a ferro magnet

Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
12#include "data_type.hpp"
13#include "monte_carlo.hpp"
14#include "utils.hpp"
15
16#include <csignal>
17#include <cstdlib>
18#include <iostream>
19#include <omp.h>
20
21 void create_burn_in_time_data()
22{
23 // Test burn-in time
24 monte_carlo_progression(1.0, 20, 20000,
25 "output/burn_in_time/unordered_1_0.txt");
26 monte_carlo_progression(1.0, 20, 20000, 1,
27 "output/burn_in_time/ordered_1_0.txt");
28 monte_carlo_progression(2.4, 20, 20000,
29 "output/burn_in_time/unordered_2_4.txt");
30 monte_carlo_progression(2.4, 20, 20000, 1,
31 "output/burn_in_time/ordered_2_4.txt");
32}
33
34 void create_pd_estimate_data()
35{
36 // Estimate pd
37 pd_estimate(1.0, 20, 1000000, "output/pd_estimate/estimate_1_0.txt");
38 pd_estimate(2.4, 20, 1000000, "output/pd_estimate/estimate_2_4.txt");
39}
40
41 void test_parallel_speedup()
42{
43 // Test the openmp speedup
44 data_t data;
45 double t0, t1, t2;
46 int tries = 5;
47 t0 = omp_get_wtime();
48 for (size_t i = 0; i < tries; i++)
49 monte_carlo_serial(20, 1.0, 10000);
50 t1 = omp_get_wtime();
51 for (size_t i = 0; i < tries; i++)
52 monte_carlo_parallel(20, 1.0, 10000);
53 t2 = omp_get_wtime();
54
55 std::cout << "Time serial : " << (t1 - t0) / tries << " seconds"
56 << '\n';
57 std::cout << "Time parallel : " << (t2 - t1) / tries << " seconds"
58 << '\n';
59 std::cout << "Speedup parallel: " << (t1 - t0) / (t2 - t1) << '\n';
60}
61
62 void create_phase_transition_data()
63{
64 double t0, t1;
65
66 t0 = omp_get_wtime();
67 // Phase transition
68 phase_transition(20, 2.1, 2.4, 40, monte_carlo_parallel,
69 "output/phase_transition/size_20.txt");
70 phase_transition(40, 2.1, 2.4, 40, monte_carlo_parallel,
71 "output/phase_transition/size_40.txt");
72 phase_transition(60, 2.1, 2.4, 40, monte_carlo_parallel,
73 "output/phase_transition/size_60.txt");
74 phase_transition(80, 2.1, 2.4, 40, monte_carlo_parallel,
75 "output/phase_transition/size_80.txt");
76 phase_transition(100, 2.1, 2.4, 40, monte_carlo_parallel,
77 "output/phase_transition/size_100.txt");
78 t1 = omp_get_wtime();
79
80 std::cout << "Time: " << t1 - t0 << std::endl;
81}
82
84 int main(int argc, char **argv)
85{
86 if (argc < 2) {
87 std::cout << "Need at least 1 argument, got " << argc - 1
88 << " arguments." << std::endl;
89 abort();
90 }
91
92 int arg = atoi(argv[1]);
93
94 switch (arg) {
95 case 1:
96 create_burn_in_time_data();
97 break;
98 case 2:
99 create_pd_estimate_data();
100 break;
101 case 3:
102 test_parallel_speedup();
103 break;
104 case 4:
105 create_phase_transition_data();
106 break;
107 default:
108 std::cout << "Not a valid option!" << std::endl;
109 abort();
110 }
111
112 return 0;
113}
data_t
Definition: data_type.hpp:18
data_type.hpp
Header for the data_t type.
monte_carlo.hpp
Functions for monte carlo simulations.
phase_transition
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.
Definition: monte_carlo.cpp:156
monte_carlo_serial
data_t monte_carlo_serial(int L, double T, int cycles)
Execute the Metropolis algorithm for a certain amount of Monte Carlo cycles.
Definition: monte_carlo.cpp:111
pd_estimate
void pd_estimate(double T, int L, int cycles, const std::string filename)
Estimate the probability distribution for the energy.
Definition: monte_carlo.cpp:85
monte_carlo_progression
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.
Definition: monte_carlo.cpp:17
monte_carlo_parallel
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.
Definition: monte_carlo.cpp:128
main
int main()
The main function.
Definition: test_suite.cpp:148
utils.hpp
Function prototypes and macros that are useful.