/** @file phase_transition_mpi.cpp
 *
 *  @author Cory Alexander Balaton (coryab)
 *  @author Janita Ovidie Sandtrøen Willumsen (janitaws)
 *
 *  @version 1.0
 *
 *  @brief Sweep over different temperatures and generate data.
 *
 *  @bug No known bugs
 * */
#include "data_type.hpp"
#include "monte_carlo.hpp"
#include "utils.hpp"

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <mpi.h>

/** @brief The main function*/
int main(int argc, char **argv)
{
    double start = 1., end = 3.;
    uint points = 1000, L = 20, N;
    double dt = (end - start) / points;
    uint cycles = 10000;
    N = L * L;
    std::ofstream ofile;

    data_t data[points];

    // MPI stuff
    int rank, cluster_size;

    // Initialize MPI
    MPI_Init(&argc, &argv);

    // Get the cluster size and rank
    MPI_Comm_size(MPI_COMM_WORLD, &cluster_size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    uint remainder = points % cluster_size;
    double i_start;
    uint i_points;
    // The last
    if (rank < remainder) {
        i_points = points / cluster_size + 1;
        i_start = start + dt * i_points * rank;
    }
    else {
        i_points = points / cluster_size;
        i_start = start + dt * (i_points * rank + remainder);
    }

    data_t i_data[i_points];
    std::cout << "Rank " << rank << ": " << i_points << ',' << i_start << '\n';

    for (size_t i = 0; i < i_points; i++) {
        monte_carlo_serial(i_data[i], L, i_start + dt * i, cycles);
    }

    if (rank == 0) {
        std::copy_n(i_data, i_points, data);
        for (size_t i = 1; i < cluster_size; i++) {
            if (rank < remainder) {
                MPI_Recv((void *)i_data,
                         sizeof(data_t) * (points / cluster_size + 1), MPI_CHAR,
                         i, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
                std::copy_n(i_data, points / cluster_size + 1,
                            data + (points / cluster_size) * i);
            }
            else {
                MPI_Recv((void *)i_data,
                         sizeof(data_t) * (points / cluster_size), MPI_CHAR, i,
                         MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
                std::copy_n(i_data, points / cluster_size,
                            data + (points / cluster_size) * i + remainder);
            }
        }
    }
    else {
        MPI_Send(i_data, i_points * sizeof(data_t), MPI_CHAR, 0, rank,
                 MPI_COMM_WORLD);
    }

    MPI_Finalize();

    std::string outfile = "output/phase_transition/size_20.txt";
    utils::mkpath(utils::dirname(outfile));
    ofile.open(outfile);

    double temp, CV, X;

    using utils::scientific_format;
    for (size_t i = 0; i < points; i++) {
        temp = start + dt * i;
        CV = (data[i].E2 - data[i].E * data[i].E) / (N * temp * temp);
        X = (data[i].M2 - data[i].M_abs * data[i].M_abs) / (N * temp);

        ofile << scientific_format(temp) << ','
              << scientific_format(data[i].E / N) << ','
              << scientific_format(data[i].M_abs / N) << ','
              << scientific_format(CV) << ',' << scientific_format(X) << '\n';
    }
    ofile.close();
}
