2 Dimensional Ising Model
Simulate the change in energy and magnetization in a ferro magnet
Loading...
Searching...
No Matches
time.cpp
Go to the documentation of this file.
1/** @file time.cpp
2 *
3 * @author Cory Alexander Balaton (coryab)
4 * @author Janita Ovidie Sandtrøen Willumsen (janitaws)
5 *
6 * @version 0.1
7 *
8 * @brief Timing various things.
9 *
10 * @bug No known bugs
11 * */
12#include "data_type.hpp"
13#include "monte_carlo.hpp"
14#include "utils.hpp"
15
16#include <getopt.h>
17#include <omp.h>
18#include <ostream>
19
20/** @brief Time phase transition using different lattice sizes.
21 * */
23{
24 std::string outfile = "data/hp/timing/lattice_sizes.txt";
25 std::ofstream ofile;
26
27 int lattice_sizes[] = {20, 40, 60, 80, 100};
28
29 utils::mkpath(utils::dirname(outfile));
30 ofile.open(outfile);
31 double t0, t1, t2;
32 for (int L : lattice_sizes) {
33 t0 = omp_get_wtime();
34 montecarlo::phase_transition(L, 2.1, 2.4, 40, 20000,
35 montecarlo::mcmc_parallel, "/dev/null", 0);
36 t1 = omp_get_wtime();
37 montecarlo::phase_transition(L, 2.1, 2.4, 40, 20000,
38 montecarlo::mcmc_serial, "/dev/null", 0);
39 t2 = omp_get_wtime();
40 ofile << utils::scientific_format(L) << ','
41 << utils::scientific_format(t1 - t0) << ','
42 << utils::scientific_format(t2 - t1) << '\n';
43 }
44 ofile.close();
45}
46
47/** @brief Time phase transition using different sample sizes.
48 * */
50{
51 std::string outfile = "data/hp/timing/sample_sizes.txt";
52 std::ofstream ofile;
53
54 int sample_sizes[] = {1000, 10000, 100000};
55
56 utils::mkpath(utils::dirname(outfile));
57 ofile.open(outfile);
58 double t0, t1, t2;
59 for (int samples : sample_sizes) {
60 t0 = omp_get_wtime();
61 montecarlo::phase_transition(20, 2.1, 2.4, 40, samples,
62 montecarlo::mcmc_parallel, "/dev/null", 0);
63 t1 = omp_get_wtime();
64 montecarlo::phase_transition(20, 2.1, 2.4, 40, samples,
65 montecarlo::mcmc_serial, "/dev/null", 0);
66 t2 = omp_get_wtime();
67 ofile << utils::scientific_format(samples) << ','
68 << utils::scientific_format(t1 - t0) << ','
69 << utils::scientific_format(t2 - t1) << '\n';
70 }
71 ofile.close();
72}
73
74/** @brief A function that displays how to use the program and quits.*/
75void usage(std::string filename)
76{
77 std::cout << "Usage: " << filename << " OPTION ...\n"
78 << "At least one option should be used.\n\n"
79 << "\t[ -h | --help ]\n"
80 << "\t[ --all ]\n"
81 << "\t[ --time-lattice-sizes ]\n"
82 << "\t[ --time-sample-sizes ]\n";
83 exit(-1);
84}
85
86/** @brief The main function.*/
87int main(int argc, char **argv)
88{
89 struct option long_options[] = {{"all", 0, 0, 0},
90 {"time-lattice-sizes", 0, 0, 0},
91 {"time-sample-sizes", 0, 0, 0},
92 {"help", 0, 0, 0},
93 {NULL, 0, NULL, 0}};
94
95 int option_index = -1;
96 int c;
97
98 while (true) {
99 c = getopt_long(argc, argv, "h", long_options, &option_index);
100
101 if (c == -1)
102 break;
103
104 switch (c) {
105 case 0:
106 switch (option_index) {
107 case 0:
110 break;
111 case 1:
113 break;
114 case 2:
116 break;
117 case 3: // Not a mistake. This just goes to the default.
118 default:
119 usage(argv[0]);
120 }
121 break;
122 case 'h':
123 default:
124 usage(argv[0]);
125 }
126 }
127
128 return 0;
129}
int main(int argc, char **argv)
The main function.
Definition: main.cpp:125
void usage(std::string filename)
A function that displays how to use the program and quits.
Definition: main.cpp:110
void phase_transition(int L, double start_T, double end_T, int points_T, int cycles, std::function< data_t(int, double, int, int)> monte_carlo, std::string outfile, int burn_in_time=BURN_IN_TIME)
Perform the MCMC algorithm using a range of temperatures.
data_t mcmc_parallel(int L, double T, int cycles, int burn_in_time=BURN_IN_TIME)
Execute the Metropolis algorithm for a certain amount of Monte Carlo cycles in parallel.
data_t mcmc_serial(int L, double T, int cycles, int burn_in_time=BURN_IN_TIME)
Execute the Metropolis algorithm for a certain amount of Monte Carlo cycles.
void time_lattice_sizes()
Time phase transition using different lattice sizes.
Definition: time.cpp:22
void time_sample_sizes()
Time phase transition using different sample sizes.
Definition: time.cpp:49
bool mkpath(std::string path, int mode=0777)
Make path given.
Definition: utils.cpp:32
std::string scientific_format(double d, int width=20, int prec=10)
Turns a double into a string written in scientific format.
Definition: utils.cpp:16
std::string dirname(const std::string &path)
Get the directory name of the path.
Definition: utils.cpp:58