/*
 * 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 "parameter.h"
#include "progress.h"
#include "solver.h"
#include "timing.h"
#include "trace.h"

int main(int argc, char** argv)
{
    double timeStart, timeEnd;
    Parameter p;
    Solver s;
    Tracing t;
    initParameter(&p);

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

    readParameter(&p, argv[1]);
    printParameter(&p);
    initSolver(&s, &p);
    initTrace(&t, &p);
#ifndef VERBOSE
    initProgress(s.te);
#endif

    double tau  = s.tau;
    double te   = s.te;
    double time = 0.0;
    int nt      = 0;

    timeStart = getTimeStamp();
    while (time <= te) {
        if (tau > 0.0) computeTimestep(&s);
        setBoundaryConditions(&s);
        setSpecialBoundaryCondition(&s);
        computeFG(&s);
        computeRHS(&s);
        if (nt % 100 == 0) normalizePressure(&s);
        solve(&s);
        adaptUV(&s);
        time += s.dt;
        nt++;
        
        trace(&t, s.u, s.v, time);

/*
#ifdef VERBOSE
        printf("TIME %f , TIMESTEP %f\n", time, s.dt);
#else
        printProgress(time);
#endif
*/
    }
    timeEnd = getTimeStamp();
    stopProgress();
    printf("Solution took %.2fs\n", timeEnd - timeStart);
    writeResult(&s);
    return EXIT_SUCCESS;
}
