/*
 * Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
 * All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found in the LICENSE file.
 */
#include <float.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "allocate.h"
#include "parameter.h"
#include "progress.h"
#include "solver.h"
#include "timing.h"
#include <mpi.h>


enum VARIANT { SOR = 1, RB, RBA };

int main (int argc, char** argv)
{
    int rank;
    int variant = SOR;

    double S, E;
    Parameter params;
    Solver solver;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    initParameter(&params);

    if (argc < 2) {
        printf("Usage: %s <configFile>\n", argv[0]);
        exit(EXIT_SUCCESS);
    }

    readParameter(&params, argv[1]);
    if (argc == 3) 
    {
        variant = atoi(argv[2]);
    }
    if (rank == 0) {
        printParameter(&params);
    }
    initSolver(&solver, &params);
    /* debugExchange(&solver); */
    /* exit(EXIT_SUCCESS); */
    initProgress(solver.te);

    double tau = solver.tau;
    double te  = solver.te;
    double t   = 0.0;

    S = getTimeStamp();
    
    switch (variant) {
    case SOR:
        printf("Plain SOR\n");
        while (t <= te) {
        if (tau > 0.0) {
            computeTimestep(&solver);
        }

        setBoundaryConditions(&solver);
        setSpecialBoundaryCondition(&solver);
        computeFG(&solver);
        computeRHS(&solver);
        solve(&solver);
        adaptUV(&solver);
        t += solver.dt;
        }

        break;
        
    case RB:
        printf("Red-black SOR\n");
        while (t <= te) {
        if (tau > 0.0) {
            computeTimestep(&solver);
        }

        setBoundaryConditions(&solver);
        setSpecialBoundaryCondition(&solver);
        computeFG(&solver);
        computeRHS(&solver);
        solveRB(&solver);
        adaptUV(&solver);
        t += solver.dt;
        }

        break;

    case RBA:
        printf("Red-black SOR with acceleration\n");
        while (t <= te) {
        if (tau > 0.0) {
            computeTimestep(&solver);
        }

        setBoundaryConditions(&solver);
        setSpecialBoundaryCondition(&solver);
        computeFG(&solver);
        computeRHS(&solver);
        solveRBA(&solver);
        adaptUV(&solver);
        t += solver.dt;
        }

        break;
    }

#ifdef VERBOSE
        if (rank == 0) {
            printf("TIME %f , TIMESTEP %f\n", t, solver.dt);
        }
#else
        printProgress(t);
#endif

    E = getTimeStamp();
    stopProgress();
    if (rank == 0) {
        printf("Solution took %.2fs\n", E - S);
    }
    size_t bytesize = solver.imax * solver.jmax * sizeof(double);

    double* ug = allocate(64, bytesize);
    double* vg = allocate(64, bytesize);
    double* pg = allocate(64, bytesize);

    commCollectResult(&solver.comm,
        ug,
        vg,
        pg,
        solver.u,
        solver.v,
        solver.p,
        solver.jmax,
        solver.imax);
    writeResult(&solver, ug, vg, pg);

    MPI_Finalize();
    return EXIT_SUCCESS;
}
