# 2 Dimensional Ising Model: src/time.cpp Source File

Loading...
Searching...
No Matches
time.cpp
Go to the documentation of this file.
/** @file time.cpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 0.1
*
* @brief Timing various things.
*
* @bug No known bugs
* */
#include "data_type.hpp"
#include "monte_carlo.hpp"
#include "utils.hpp"
#include <getopt.h>
#include <omp.h>
#include <ostream>
/** @brief Time phase transition using different lattice sizes.
* */
void time_lattice_sizes()
{
std::string outfile = "data/timing/lattice_sizes.txt";
std::ofstream ofile;
int lattice_sizes[] = {20, 40, 60, 80, 100};
utils:: mkpath (utils:: dirname (outfile) );
ofile.open(outfile);
double t0, t1;
for (int L : lattice_sizes) {
t0 = omp_get_wtime();
montecarlo:: phase_transition(L, 2.1, 2.4, 40, 100000,
montecarlo:: mcmc_parallel, "/dev/null");
t1 = omp_get_wtime();
ofile << utils:: scientific_format (L) << ','
<< utils:: scientific_format (t1 - t0) << '\n';
}
ofile.close();
}
/** @brief Time phase transition using different sample sizes.
* */
void time_sample_sizes()
{
std::string outfile = "data/timing/sample_sizes.txt";
std::ofstream ofile;
int sample_sizes[] = {1000, 10000, 100000};
utils:: mkpath (utils:: dirname (outfile) );
ofile.open(outfile);
double t0, t1;
for (int samples : sample_sizes) {
t0 = omp_get_wtime();
montecarlo:: phase_transition(20, 2.1, 2.4, 40, samples,
montecarlo:: mcmc_parallel, "/dev/null");
t1 = omp_get_wtime();
ofile << utils:: scientific_format (samples) << ','
<< utils:: scientific_format (t1 - t0) << '\n';
}
ofile.close();
}
/** @brief A function that displays how to use the program and quits.*/
void usage(std::string filename)
{
std::cout << "Usage: " << filename << " OPTION ...\n"
<< "At least one option should be used.\n\n"
<< "\t[ -h | --help ]\n"
<< "\t[ --all ]\n"
<< "\t[ --time-lattice-sizes ]\n"
<< "\t[ --time-sample-sizes ]\n";
exit(-1);
}
/** @brief The main function.*/
int main(int argc, char **argv)
{
struct option long_options[] = {{"all", 0, 0, 0},
{"time-lattice-sizes", 0, 0, 0},
{"time-sample-sizes", 0, 0, 0},
{"help", 0, 0, 0},
{NULL, 0, NULL, 0}};
int option_index = -1;
int c;
while (true) {
c = getopt_long(argc, argv, "h", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
switch (option_index) {
case 0:
time_lattice_sizes ( );
time_sample_sizes ( );
break;
case 1:
time_lattice_sizes ( );
break;
case 2:
time_sample_sizes ( );
break;
case 3: // Not a mistake. This just goes to the default.
default:
usage(argv[0]);
}
break;
case 'h':
default:
usage(argv[0]);
}
}
return 0;
}
main
int main(int argc, char **argv)
The main function.
Definition: main.cpp:125
usage
void usage(std::string filename)
A function that displays how to use the program and quits.
Definition: main.cpp:110
montecarlo::phase_transition
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.
Definition: monte_carlo.cpp:169
montecarlo::mcmc_parallel
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.
Definition: monte_carlo.cpp:141
time_lattice_sizes
void time_lattice_sizes()
Time phase transition using different lattice sizes.
Definition: time.cpp:22
time_sample_sizes
void time_sample_sizes()
Time phase transition using different sample sizes.
Definition: time.cpp:45
utils::mkpath
bool mkpath(std::string path, int mode=0777)
Make path given.
Definition: utils.cpp:32
utils::scientific_format
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
utils::dirname
std::string dirname(const std::string &path)
Get the directory name of the path.
Definition: utils.cpp:58