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/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;
32 for (int L : lattice_sizes) {
33 t0 = omp_get_wtime();
34 montecarlo::phase_transition(L, 2.1, 2.4, 40, 100000,
35 montecarlo::mcmc_parallel, "/dev/null");
36 t1 = omp_get_wtime();
37 ofile << utils::scientific_format(L) << ','
38 << utils::scientific_format(t1 - t0) << '\n';
39 }
40 ofile.close();
41}
42
43/** @brief Time phase transition using different sample sizes.
44 * */
46{
47 std::string outfile = "data/timing/sample_sizes.txt";
48 std::ofstream ofile;
49
50 int sample_sizes[] = {1000, 10000, 100000};
51
52 utils::mkpath(utils::dirname(outfile));
53 ofile.open(outfile);
54 double t0, t1;
55 for (int samples : sample_sizes) {
56 t0 = omp_get_wtime();
57 montecarlo::phase_transition(20, 2.1, 2.4, 40, samples,
58 montecarlo::mcmc_parallel, "/dev/null");
59 t1 = omp_get_wtime();
60 ofile << utils::scientific_format(samples) << ','
61 << utils::scientific_format(t1 - t0) << '\n';
62 }
63 ofile.close();
64}
65
66/** @brief A function that displays how to use the program and quits.*/
67void usage(std::string filename)
68{
69 std::cout << "Usage: " << filename << " OPTION ...\n"
70 << "At least one option should be used.\n\n"
71 << "\t[ -h | --help ]\n"
72 << "\t[ --all ]\n"
73 << "\t[ --time-lattice-sizes ]\n"
74 << "\t[ --time-sample-sizes ]\n";
75 exit(-1);
76}
77
78/** @brief The main function.*/
79int main(int argc, char **argv)
80{
81 struct option long_options[] = {{"all", 0, 0, 0},
82 {"time-lattice-sizes", 0, 0, 0},
83 {"time-sample-sizes", 0, 0, 0},
84 {"help", 0, 0, 0},
85 {NULL, 0, NULL, 0}};
86
87 int option_index = -1;
88 int c;
89
90 while (true) {
91 c = getopt_long(argc, argv, "h", long_options, &option_index);
92
93 if (c == -1)
94 break;
95
96 switch (c) {
97 case 0:
98 switch (option_index) {
99 case 0:
102 break;
103 case 1:
105 break;
106 case 2:
108 break;
109 case 3: // Not a mistake. This just goes to the default.
110 default:
111 usage(argv[0]);
112 }
113 break;
114 case 'h':
115 default:
116 usage(argv[0]);
117 }
118 }
119
120 return 0;
121}
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.
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:45
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