/*
 * Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
 * All rights reserved. This file is part of nusif-solver.
 * Use of this source code is governed by a MIT style
 * license that can be found in the LICENSE file.
 */
#ifndef __PARTICLETRACING_H_
#define __PARTICLETRACING_H_
#include "allocate.h"
#include "parameter.h"
#include "discretization.h"
#include <mpi.h>
#include <stdbool.h>

#define NDIMS 3

typedef enum COORD { X = 0, Y, NCOORD } COORD;
typedef struct {
    double x, y, z;
    bool flag;
} Particle;

typedef struct {
    int numberOfParticles, totalParticles;
    double startTime, injectTimePeriod, writeTimePeriod, lastInjectTime, lastUpdateTime,
        lastWriteTime;

    int estimatedNumParticles, removedParticles;

    double dx, dy, dz, dt;
    Particle* linSpaceLine;
    Particle* particlePool;

    int pointer;

    double imax, jmax, kmax, xlength, ylength, zlength, imaxLocal, jmaxLocal, kmaxLocal;

    double x1, y1, z1, x2, y2, z2;

#ifdef _MPI
    MPI_Comm comm;
    MPI_Datatype mpi_particle;
#endif

    int rank, size;
    int coords[NDIMS], dims[NDIMS];

    double xLocal, yLocal, zLocal, xOffset, yOffset, zOffset, xOffsetEnd, yOffsetEnd,
        zOffsetEnd;

    double* offset;

} ParticleTracer;

void initParticleTracer(ParticleTracer*, Parameter*, Discretization*);
void injectParticles(ParticleTracer*, double*);
void advanceParticles(ParticleTracer*, double*, double*, double*, double*, double);
void freeParticles(ParticleTracer*);
void writeParticles(ParticleTracer*);
void printParticleTracerParameters(ParticleTracer*);
void printParticles(ParticleTracer*);
void trace(ParticleTracer*, double*, double*, double*, double*, double);
void compress(ParticleTracer*);
extern void particleRandomizer(ParticleTracer*);
#endif