2 Dimensional Ising Model
Simulate the change in energy and magnetization in a ferro magnet
Loading...
Searching...
No Matches
phase_transition.cpp
Go to the documentation of this file.
1/** @file phase_transition.cpp
2 *
3 * @author Cory Alexander Balaton (coryab)
4 * @author Janita Ovidie Sandtrøen Willumsen (janitaws)
5 *
6 * @version 1.0
7 *
8 * @brief Sweep over different temperatures and generate data.
9 *
10 * @details This program takes in 4 arguments: the start temperature,
11 * the end temperature, the amount of temperature points to simulate, and
12 * the amount of monte carlo samples to collect, in that order.
13 *
14 * @bug No known bugs
15 * */
16#include "data_type.hpp"
17#include "monte_carlo.hpp"
18#include "utils.hpp"
19
20#include <getopt.h>
21#include <omp.h>
22#include <string>
23
24/** @brief A function that displays how to use the program and quits.*/
25void usage(std::string filename)
26{
27 std::cout << "Usage: " << filename
28 << " <start temperature> <end temperature> <points> "
29 "<lattice size> <cycles> <burn-in-time> <output file>\n\n"
30 << "\t[ -h | --help ]\n";
31 exit(-1);
32}
33
34/** @brief The main function.*/
35int main(int argc, char **argv)
36{
37 // Command options
38 struct option long_options[] = {{"help", 0, 0, 0}, {NULL, 0, NULL, 0}};
39
40 int option_index = -1;
41 int c;
42
43 while (true) {
44 c = getopt_long(argc, argv, "h", long_options, &option_index);
45
46 if (c == -1)
47 break;
48
49 switch (c) {
50 case 0:
51 switch (option_index) {
52 case 0: // Not a mistake. This just goes to the default.
53 default:
54 usage(argv[0]);
55 }
56 break;
57 case 'h':
58 default:
59 usage(argv[0]);
60 }
61 }
62 // Check that the number of arguments is at least 8.
63 if (argc < 8) {
64 usage(argv[0]);
65 }
66
67 // Timing variables
68 double t0, t1;
69 t0 = omp_get_wtime();
70
71 // Define/initialize variables
72 double start = atof(argv[1]), end = atof(argv[2]);
73 int points = atoi(argv[3]), cycles = atoi(argv[5]), L = atoi(argv[4]),
74 burn_in_time = atoi(argv[6]), N = L * L;
75 std::string outfile = argv[7];
76
77 montecarlo::phase_transition(L, start, end, points, cycles,
78 montecarlo::mcmc_parallel, outfile, burn_in_time);
79
80 t1 = omp_get_wtime();
81
82 std::cout << "Time: " << t1 - t0 << " seconds\n";
83}
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.