/*============================================================================
 * User initialization prior to solving time steps.
 *============================================================================*/

/* VERS */

/*
  This file is part of Code_Saturne, a general-purpose CFD tool.

  Copyright (C) 1998-2021 EDF S.A.

  This program is free software; you can redistribute it and/or modify it under
  the terms of the GNU General Public License as published by the Free Software
  Foundation; either version 2 of the License, or (at your option) any later
  version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
  Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/*----------------------------------------------------------------------------*/

#include "cs_defs.h"

/*----------------------------------------------------------------------------
 * Standard C library headers
 *----------------------------------------------------------------------------*/

#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#if defined(HAVE_MPI)
#include <mpi.h>
#endif

/*----------------------------------------------------------------------------
 * PLE library headers
 *----------------------------------------------------------------------------*/

#include <ple_coupling.h>

/*----------------------------------------------------------------------------
 * Local headers
 *----------------------------------------------------------------------------*/

#include "cs_headers.h"

/*----------------------------------------------------------------------------*/

BEGIN_C_DECLS

/*----------------------------------------------------------------------------*/
/*!
 * \file cs_user_initialization-base.c
 *
 * \brief Initialization prior to solving time steps.
 *        Basic examples
 *
 * See \ref cs_user_initialization for examples.
 */
/*----------------------------------------------------------------------------*/

/*============================================================================
 * User function definitions
 *============================================================================*/

/*----------------------------------------------------------------------------*/
/*!
 * \brief Initialize variables.
 *
 * This function is called at beginning of the computation
 * (restart or not) before the time step loop.
 *
 * This is intended to initialize or modify (when restarted)
 * variable and time step values.
 *
 * \param[in, out]  domain   pointer to a cs_domain_t structure
 */
/*----------------------------------------------------------------------------*/

/* Array size definition */
#define ARRAY_SIZE 81897

void
cs_user_initialization(cs_domain_t     *domain)
{
  /*! [init] */
  bft_printf("Field initialization using ANN prediction of thermochemical variables!!!\n");
  double t1 = cs_timer_wtime();

  const cs_mesh_t *m = domain->mesh;

  /* Pinzo@10072024 (Definition of ANN variables)*/
#if 0
  double ym_fuel[ARRAY_SIZE], ym_oxyd[ARRAY_SIZE], ym_prod[ARRAY_SIZE];
  double temp[ARRAY_SIZE], rho[ARRAY_SIZE];
  double vel_x[ARRAY_SIZE], vel_y[ARRAY_SIZE], vel_z[ARRAY_SIZE];
#endif
  /* Pinzo@10072024 END->(Definition of ANN variables)*/

  size_t result1, result2, result3, result4, result5, result6, result7, result8;

  /* If this is restarted computation, do not reinitialize values */
  if (domain->time_step->nt_prev > 0)
    return;

  double *ym_fuel, *ym_oxyd, *ym_prod, *temp, *rho;
  double *vel_x, *vel_y, *vel_z;
  BFT_MALLOC(ym_fuel, ARRAY_SIZE, double);
  BFT_MALLOC(ym_oxyd, ARRAY_SIZE, double);
  BFT_MALLOC(ym_prod, ARRAY_SIZE, double);
  BFT_MALLOC(temp, ARRAY_SIZE, double);
  BFT_MALLOC(rho, ARRAY_SIZE, double);
  BFT_MALLOC(vel_x, ARRAY_SIZE, double);
  BFT_MALLOC(vel_y, ARRAY_SIZE, double);
  BFT_MALLOC(vel_z, ARRAY_SIZE, double);

  /* Pinzo@10072024 (Calling up data values from NN computation) */
  FILE *file1 = fopen("y_fuel.pt", "rb");
  result1 = fread(ym_fuel, sizeof(double), ARRAY_SIZE, file1);
  fclose(file1);
  FILE *file2 = fopen("y_oxy.pt", "rb");
  result2 = fread(ym_prod, sizeof(double), ARRAY_SIZE, file2);
  fclose(file2);
  FILE *file3 = fopen("y_prod.pt", "rb");
  result3 = fread(ym_oxyd, sizeof(double), ARRAY_SIZE, file3);
  fclose(file3);
  FILE *file4 = fopen("temp.pt", "rb");
  result4 = fread(temp, sizeof(double), ARRAY_SIZE, file4);
  fclose(file4);
  FILE *file5 = fopen("rho.pt", "rb");
  result5 = fread(rho, sizeof(double), ARRAY_SIZE, file5);
  fclose(file5);
  FILE *file6 = fopen("vel_x.pt", "rb");
  result6 = fread(vel_x, sizeof(double), ARRAY_SIZE, file6);
  fclose(file6);
  FILE *file7 = fopen("vel_y.pt", "rb");
  result7 = fread(vel_y, sizeof(double), ARRAY_SIZE, file7);
  fclose(file7);
  FILE *file8 = fopen("vel_z.pt", "rb");
  result8 = fread(vel_z, sizeof(double), ARRAY_SIZE, file8);
  fclose(file8);
  /* Pinzo@10072024 END->(Calling up data values from NN computation) */

  /* Initialize "ym_fuel" field only if it exists  */
  cs_field_t *f_f = cs_field_by_name_try("ym_fuel");

  if (f_f != NULL) {
    for (cs_lnum_t cell_id = 0; cell_id < m->n_cells; cell_id++) {
      f_f->val[cell_id] = ym_fuel[cell_id];
    }
  }

  /* Initialize "ym_oxy" field only if it exists  */
  cs_field_t *f_o = cs_field_by_name_try("ym_oxyd");

  if (f_o != NULL) {
    for (cs_lnum_t cell_id = 0; cell_id < m->n_cells; cell_id++) {
      f_o->val[cell_id] = ym_oxyd[cell_id];
    }
  }

  /* Initialize "ym_prod" field only if it exists  */
  cs_field_t *f_p = cs_field_by_name_try("ym_prod");

  if (f_p != NULL) {
    for (cs_lnum_t cell_id = 0; cell_id < m->n_cells; cell_id++) {
      f_p->val[cell_id] = ym_prod[cell_id];
    }
  }

  /* Initialize "temp" field only if it exists  */
  cs_field_t *f_temp = CS_F_(t);

  for (cs_lnum_t cell_id = 0; cell_id < m->n_cells; cell_id++) {
    f_temp->val[cell_id] = temp[cell_id];
  }


  /* Initialize "rho" field only if it exists  */
  cs_field_t *f_rho = CS_F_(rho);

  for (cs_lnum_t cell_id = 0; cell_id < m->n_cells; cell_id++) {
    f_rho->val[cell_id] = rho[cell_id];
  }


  /* Initialize "vel" field only if it exists  */

  cs_real_3_t *cvar_vel = (cs_real_3_t *)CS_F_(vel)->val;
  for (cs_lnum_t c_id = 0; c_id < m->n_cells; c_id++) {
    cvar_vel[c_id][0] = vel_x[c_id];
    cvar_vel[c_id][1] = vel_y[c_id];
    cvar_vel[c_id][2] = vel_z[c_id];
  } 

  double t2 = cs_timer_wtime();
 
  bft_printf("Time in cs_user_initialization-ANN.c: %f seconds \n", t2 - t1);
  bft_printf_flush();
  bft_printf("END!!!");

  BFT_FREE(ym_fuel);
  BFT_FREE(ym_oxyd);
  BFT_FREE(ym_prod);
  BFT_FREE(temp);
  BFT_FREE(rho);
  BFT_FREE(vel_x);
  BFT_FREE(vel_y);
  BFT_FREE(vel_z);
} /*! [init] */


/*----------------------------------------------------------------------------*/

END_C_DECLS
