/*============================================================================
 * User functions for input of calculation parameters.
 *============================================================================*/

/* VERS */

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

  Copyright (C) 1998-2020 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 <string.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_parameters-base.c
 *
 * \brief User functions for input of calculation parameters.
 *
 * See \ref parameters for examples.
 */
/*----------------------------------------------------------------------------*/

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

/*----------------------------------------------------------------------------*/
/*!
 * \brief Select physical model options, including user fields.
 *
 * This function is called at the earliest stages of the data setup,
 * so field ids are not available yet.
 */
/*----------------------------------------------------------------------------*/

void
cs_user_model(void)
{
  /*--------------------------------------------------------------------------*/
  cs_glob_atmo_option->meteo_profile = 1;

  /* Inverse LMO length (m^-1) */
  cs_glob_atmo_option->meteo_dlmo = 0.;
  /* Large scale roughness (m) */
  cs_glob_atmo_option->meteo_z0 = 0.1;
  /* Elevation for reference velocity (m) */
  cs_glob_atmo_option->meteo_zref = 1.;
  /* Friction velocity (m/s) */
  cs_glob_atmo_option->meteo_ustar0 = 1.;
  /* Velocity direction (degrees from North) */
  cs_glob_atmo_option->meteo_angle = 270.;
  /* Temperature at 2m (K) */
  cs_glob_atmo_option->meteo_t0 = 278.;
  /* Pressure at sea level (Pa) */
  cs_glob_atmo_option->meteo_psea = 1.01325e5;

  cs_glob_atmo_option->open_bcs_treatment = 1;
  /* Time of the simulation (for radiative model or chemistry)
   * syear:  starting year
   * squant: starting quantile
   * shour:  starting hour (UTC)
   * smin:   starting minute
   * ssec:   starting second
   */
  cs_glob_atmo_option->syear = 2024;
  cs_glob_atmo_option->squant = 25; // démarrage au 27 février
  cs_glob_atmo_option->shour = 1;
  cs_glob_atmo_option->smin = 0;
  cs_glob_atmo_option->ssec = 0.;

  /* Geographic position
   *  longitude: longitude of the domain origin
   *  latitude: latitude of the domain origin
   */
  cs_glob_atmo_option->longitude = -71.03;
  cs_glob_atmo_option->latitude = 42.39;

  /* Chemistry:
   *   model: choice of chemistry resolution scheme
   *     0: no atmospheric chemistry
   *     1: quasi steady equilibrium NOx scheme with 4 species and 5 reactions
   *     2: scheme with 20 species and 34 reactions
   *     3: scheme CB05 with 52 species and 155 reactions
   *     4: user defined scheme
   *      for model = 4, a SPACK file must be provided using
   *        cs_atmo_chemistry_set_spack_file_name("species.spack.dat")
   *      the following sources generated by SPACK should be included in the SRC folder
   *        kinetic.f90, fexchem.f90, jacdchemdc.f90, rates.f90, dratedc.f90
   *        dimensions.f90, LU_decompose.f90, LU_solve.f90
   */
   
   cs_glob_atmo_chemistry->model = 4;
   cs_atmo_chemistry_set_spack_file_name("species.spack.dat");
 
   cs_glob_atmo_chemistry->chemistry_with_photolysis = true;
   cs_glob_atmo_chemistry->chemistry_sep_mode = 1;
   cs_atmo_set_chem_conc_file_name("chemistry");
   
   /*cs_glob_atmo_chemistry->aerosol_model = CS_ATMO_AEROSOL_SSH;*/

  /* Default file for the aerosol profile is "aerosols" */
 

    /* Example: compute porosity from a scan of points
   * ------------------------------------------------*/
   cs_porosity_from_scan_set_file_name("building_clean.pts");
   cs_porosity_from_scan_set_output_name("chelsea");

      
    /* Add some sources from a file
   * The file contains lines with x,y,z (in meters) format */
    cs_ibm_add_sources_by_file_name("posi_scan.csv");
   
    int n_headers = 11;

    cs_glob_porosity_from_scan_opt->n_headers = n_headers;

    const char *local_headers[] = {"X", "Y", "Z", "Intensity", "Return_Number",
                                 "Number_Of_Returns", "Scan_Direction_Flag",
                                 "Classification", "Scan_Angle",
                                 "Point_Source_ID", "Gps_Time"};

    int local_type[] = {1,1,1,0,0,0,0,0,0,0,0};

    CS_MALLOC(cs_glob_porosity_from_scan_opt->headers, n_headers, char *);
    CS_MALLOC(cs_glob_porosity_from_scan_opt->header_type, n_headers, int);

    // Copy each string into modifiable memory
    for (int i = 0; i < n_headers; i++) {
      CS_MALLOC(cs_glob_porosity_from_scan_opt->headers[i],
              strlen(local_headers[i]) + 1, char);
      strcpy(cs_glob_porosity_from_scan_opt->headers[i], local_headers[i]);
      cs_glob_porosity_from_scan_opt->header_type[i] = local_type[i];
   }

  


    
}

void
cs_user_parameters(cs_domain_t *domain)
{
  /* loop over all species to deactivate flux reconstruction */
  const cs_atmo_chemistry_t *atmo_chem = cs_glob_atmo_chemistry;
  for (int s_id = 0; s_id < atmo_chem->n_species; s_id++) {

    /* Code_Saturne field id */
    int f_id = atmo_chem->species_to_field_id[s_id];
    cs_field_t *f_conc;
    f_conc = cs_field_by_id(f_id);

    cs_equation_param_t *eqp = cs_field_get_equation_param(f_conc);
    eqp->ircflu = 0;

  }

}

/*----------------------------------------------------------------------------*/
/*!
 *  \brief Define or modify log user parameters.
 */
/*----------------------------------------------------------------------------*/

void
cs_user_finalize_setup(cs_domain_t     *domain)
{
}

/*----------------------------------------------------------------------------*/
/*!
 *  \brief Define or modify log user parameters.
 */
/*----------------------------------------------------------------------------*/



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

END_C_DECLS
