/*
 * Copyright (C) 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "allocate.h"
#include "comm.h"
#include "discretization.h"
#include "grid.h"
#include "parameter.h"
#include "particletracing.h"
#include "progress.h"
#include "timing.h"

static FILE* initResidualWriter()
{
    FILE* fp;
    fp = fopen("residual.dat", "w");

    if (fp == NULL) {
        printf("Error!\n");
        exit(-1);
    }

    return fp;

}

static void writeResidual(FILE* fp, double ts, double res)
{
    fprintf(fp, "%f, %f\n", ts, res);
}

static void writeResults(Discretization* s)
{
#ifdef _MPI
    size_t bytesize = (s->grid.imax + 2) * (s->grid.jmax + 2) * sizeof(double);

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

    commCollectResult(&s->comm, ug, vg, pg, s->u, s->v, s->p, s->grid.imax, s->grid.jmax);
    if (commIsMaster(&s->comm)) {
        writeResult(s, ug, vg, pg);
    }

    free(ug);
    free(vg);
    free(pg);
#else
    writeResult(s, s->u, s->v, s->p);
#endif
}

int main(int argc, char** argv)
{
    int rank;
    double timeStart, timeStop;
    Parameter p;
    Discretization d;
    Solver s;
    ParticleTracer particletracer;

    commInit(&d.comm, argc, argv);
    initParameter(&p);
    FILE* fp;
    fp = initResidualWriter();

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

    readParameter(&p, argv[1]);
    commPartition(&d.comm, p.jmax, p.imax);
    if (commIsMaster(&d.comm)) {
        printParameter(&p);
    }

    initDiscretiztion(&d, &p);
    initSolver(&s, &d, &p);
    initParticleTracer(&particletracer, &d, &p);

#ifdef TEST
    commPrintConfig(&d.comm);
    commTestInit(&d.comm, d.p, d.f, d.g);
    commExchange(&d.comm, d.p);
    commShift(&d.comm, d.f, d.g);
    commTestWrite(&d.comm, d.p, d.f, d.g);
    writeResults(&d);
    commFinalize(&d.comm);
    exit(EXIT_SUCCESS);
#endif
#ifndef VERBOSE
    initProgress(d.te);
#endif

    double tau = d.tau;
    double te  = d.te;
    double t   = 0.0;
    double res = 0.0;
    timeStart = getTimeStamp();
    while (t <= te) {
        if (tau > 0.0) computeTimestep(&d);
        setBoundaryConditions(&d);
        setSpecialBoundaryCondition(&d);
        setObjectBoundaryCondition(&d);
        computeFG(&d);
        computeRHS(&d);
        res = solve(&s, d.p, d.rhs);
        adaptUV(&d);
        trace(&particletracer, &d, t);

        writeResidual(fp, t, res);

        t += d.dt;

#ifdef VERBOSE
        if (commIsMaster(s.comm)) {
            printf("TIME %f , TIMESTEP %f\n", t, d.dt);
        }
#else
        printProgress(t);
#endif
    }
    timeStop = getTimeStamp();
#ifndef VERBOSE
    stopProgress();
#endif
    if (commIsMaster(s.comm)) {
        printf("Solution took %.2fs\n", timeStop - timeStart);
    }
    fclose(fp);
    freeParticles(&particletracer);
    writeResults(&d);
    commFinalize(s.comm);
    return EXIT_SUCCESS;
}
