/** @file test_suite.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 "IsingModel.hpp"
#include "testlib.hpp"

/** @brief The analytic expected energy for a \f$ 2 \times 2 \f$ lattice.
 * */
#define EPS_2 (-2 * std::sinh(8.)) / (std::cosh(8.) + 3)

/** @brief The analytic expected magnetization for a \f$ 2 \times 2 \f$
 *  lattice.
 * */
#define MAG_2 (std::exp(8.) + 1) / (2 * (cosh(8.) + 3))

/** @brief The analytic heat capacity for a \f$ 2 \times 2 \f$ lattice.
 * */
#define CV_2                                                                   \
    16 * (3 * std::cosh(8.) + 1) / ((std::cosh(8.) + 3) * (std::cosh(8.) + 3))

/** @brief The analytic susceptibility for a \f$ 2 \times 2 \f$ lattice.*/
#define X_2                                                                    \
    (3 * std::exp(8.) + std::exp(-8.) + 3)                                     \
        / ((std::cosh(8.) + 3) * (std::cosh(8.) + 3))

/** @brief Test class for the Ising model
 * */
class IsingModelTest {
public:
    /** @brief Test that initializing works as intended.
     * */
    void test_init_functions()
    {
        IsingModel test;
        test.L = 3;
        test.T = 1.;

        // Test that initializing the lattice only yields 1s and -1s.
        test.initialize_lattice();
        std::function<bool(int)> f = [](int x) { return x == 1 || x == -1; };
        ASSERT(testlib::assert_each(f, test.lattice),
               "Test lattice initialization.");

        test.initialize_neighbors();
        arma::Mat<int> neighbor_matrix("2, 1 ; 0, 2 ; 1, 0");
        ASSERT(testlib::is_equal(neighbor_matrix, test.neighbors),
               "Test neighbor matrix.");

        // Fill the lattice with 1s to be able to test the next functions.
        test.lattice.fill(1);

        // Test the initial magnetization.
        test.initialize_magnetization();
        ASSERT(std::fabs(test.M - 9.) < 1e-8, "Test intial magnetization");

        // Test that the initial energy is correct
        test.initialize_energy();
        ASSERT(std::fabs(test.E - (-18)) < 1e-8, "Test initial energy.");
    }

    /** @brief Test numerical data with analytical data.
     *
     *  @param tol The tolerance between the analytical and numerical solution.
     *  @param max_cycles The max number of Monte Carlo cycles.
     *
     *  return int
     * */
    int test_2x2_lattice(double tol, int max_cycles)
    {
        data_t data, tmp;
        size_t L = 2;
        size_t n_spins = L * L;
        double T = 1.;
        size_t cycles = 0;

        // Create random engine using the mersenne twister
        std::random_device rd;
        std::mt19937 engine(rd());

        IsingModel test(L, T);

        int arr[]{0, 0, 0, 0};

        // Loop through cycles
        while (cycles++ < max_cycles) {
            data += test.Metropolis();
            tmp = data / cycles;
            if (testlib::close_to(EPS_2, tmp.E / n_spins, tol)
                && testlib::close_to(MAG_2, tmp.M_abs / n_spins, tol)
                && testlib::close_to(
                    CV_2, (tmp.E2 - tmp.E * tmp.E) / (T * T) / n_spins, tol)
                && testlib::close_to(
                    X_2, (tmp.M2 - tmp.M_abs * tmp.M_abs) / T / n_spins, tol)) {
                return cycles;
            }
        }
        return 0;
    }
};

/** @brief The main function.*/
int main()
{
    IsingModelTest test;

    test.test_init_functions();

    int res = 0;
    int tmp;
    int iterations = 10000;
    int accepted_values = 0;

    // Run through the test multiple times to get a better estimate.
    for (size_t i = 0; i < iterations; i++) {
        tmp = test.test_2x2_lattice(1e-2, 1e5);
        if (tmp == 0) {
            continue;
        }
        accepted_values++;
        res += tmp;
    }

    std::cout << "Res: " << res / accepted_values << std::endl;

    return 0;
}
