/** @file main.cpp
 *
 *  @author Cory Alexander Balaton (coryab)
 *  @author Janita Ovidie Sandtrøen Willumsen (janitaws)
 *
 *  @version 0.1
 *
 *  @brief The main program
 *
 *  @bug No known bugs
 * */
#include "data_type.hpp"
#include "monte_carlo.hpp"
#include "utils.hpp"

/** @brief Create the data for the burn-in time for temperatures 1.0 and 2.4
 *  for both unordered and ordered initial states.
 * */
void create_burn_in_time_data()
{
    // Test burn-in time
    montecarlo::progression(1.0, 20, 20000,
                            "../output/burn_in_time/unordered_1_0.txt");
    montecarlo::progression(1.0, 20, 20000, 1,
                            "../output/burn_in_time/ordered_1_0.txt");
    montecarlo::progression(2.4, 20, 20000,
                            "../output/burn_in_time/unordered_2_4.txt");
    montecarlo::progression(2.4, 20, 20000, 1,
                            "../output/burn_in_time/ordered_2_4.txt");
}

/** @brief Create the data used to estimate the probability distribution
 *  for tempratures 1.0 anbd 2.4.
 * */
void create_pd_estimate_data()
{
    // Estimate pd
    montecarlo::pd_estimate(1.0, 20, 1000000,
                            "../output/pd_estimate/estimate_1_0.txt");
    montecarlo::pd_estimate(2.4, 20, 1000000,
                            "../output/pd_estimate/estimate_2_4.txt");
}

void test_burn_in_time()
{
    montecarlo::phase_transition(100, 2.1, 2.4, 40, 1e5, montecarlo::mcmc_serial,
                                 "../output/test_burn_in_time/no_burn_in.txt", 0);
    montecarlo::phase_transition(100, 2.1, 2.4, 40, 1e5, montecarlo::mcmc_serial,
                                 "../output/test_burn_in_time/burn_in.txt", 5000);
}

/** @brief Test how much Openmp speeds up.*/
void test_parallel_speedup()
{
    // Test the openmp speedup
    data_t data;
    double t0, t1, t2;
    int tries = 5;
    t0 = omp_get_wtime();
    for (size_t i = 0; i < tries; i++)
        montecarlo::mcmc_serial(20, 1.0, 10000);
    t1 = omp_get_wtime();
    for (size_t i = 0; i < tries; i++)
        montecarlo::mcmc_parallel(20, 1.0, 10000);
    t2 = omp_get_wtime();

    std::cout << "Time serial     : " << (t1 - t0) / tries << " seconds"
              << '\n';
    std::cout << "Time parallel   : " << (t2 - t1) / tries << " seconds"
              << '\n';
    std::cout << "Speedup parallel: " << (t1 - t0) / (t2 - t1) << '\n';
}

/** @brief Create data for studying phase transition.
 * */
void create_phase_transition_data()
{
    double t0, t1;

    t0 = omp_get_wtime();
    // Phase transition
    montecarlo::phase_transition(20, 2.1, 2.4, 40, 1e4,
                                 montecarlo::mcmc_parallel,
                                 "../output/phase_transition/size_20.txt");
    montecarlo::phase_transition(40, 2.1, 2.4, 40, 1e4,
                                 montecarlo::mcmc_parallel,
                                 "../output/phase_transition/size_40.txt");
    montecarlo::phase_transition(60, 2.1, 2.4, 40, 1e4,
                                 montecarlo::mcmc_parallel,
                                 "../output/phase_transition/size_60.txt");
    montecarlo::phase_transition(80, 2.1, 2.4, 40, 1e4,
                                 montecarlo::mcmc_parallel,
                                 "../output/phase_transition/size_80.txt");
    montecarlo::phase_transition(100, 2.1, 2.4, 40, 1e4,
                                 montecarlo::mcmc_parallel,
                                 "../output/phase_transition/size_100.txt");
    t1 = omp_get_wtime();

    std::cout << "Time: " << t1 - t0 << std::endl;
}

/** @brief The main function.*/
int main(int argc, char **argv)
{
    if (argc < 2) {
        std::cout << "Need at least 1 argument, got " << argc - 1
                  << " arguments." << std::endl;
        abort();
    }

    int arg = atoi(argv[1]);

    switch (arg) {
    case 1:
        create_burn_in_time_data();
        break;
    case 2:
        create_pd_estimate_data();
        break;
    case 3:
        test_parallel_speedup();
        break;
    case 4:
        create_phase_transition_data();
        break;
    case 5:
        test_burn_in_time();
        break;
    default:
        std::cout << "Not a valid option!" << std::endl;
        abort();
    }

    return 0;
}
