/*============================================================================
 * Code couplings definition with SYRTHES and Code_Saturne.
 *
 * 1) Define conjuguate heat transfer couplings with the SYRTHES code
 * 2) Define couplings with other instances of Code_Saturne
 *============================================================================*/

/* code_saturne version 6.3 */

/*
  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
 *----------------------------------------------------------------------------*/

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

#include <ple_coupling.h>

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

#include "cs_headers.h"

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

BEGIN_C_DECLS

/*----------------------------------------------------------------------------*/
/*!
 * \file cs_user_coupling.c
 *
 * \brief Code couplings definition with SYRTHES and Code_Saturne.
 *
 * See \ref user_coupling for examples.
 */
/*----------------------------------------------------------------------------*/

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

/*----------------------------------------------------------------------------*/
/*!
 * \brief Define couplings with other instances of Code_Saturne.
 *
 * This is done by calling the \ref cs_sat_coupling_define function for each
 * coupling to add.
 */
/*----------------------------------------------------------------------------*/

void
cs_user_saturne_coupling(void)
{

}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Define couplings with SYRTHES code.
 *
 * This is done by calling the \ref cs_syr_coupling_define function for each
 * coupling to add.
 */
/*----------------------------------------------------------------------------*/

void
cs_user_syrthes_coupling(void)
{
  int  verbosity = 1, plot = 1;
  float tolerance = 0.5;//0.1
  bool allow_nonmatching = true;
  extern void read_init(void);
  extern int double_coupling;
  
  // FIRST CALL READ DATA 
  read_init(); 

  // Set the explicit treatment of the solver
  cs_syr_coupling_set_explicit_treatment();  
  if (double_coupling == 1){
    cs_syr_coupling_define("Solid",
                           "SMOOTHWALL_GROUP_3 or SMOOTHWALL_GROUP_2 or SMOOTHWALL_GROUP_1 or ROUGHWALL_GROUP_3 or ROUGHWALL_GROUP_2 or ROUGHWALL_GROUP_1", /* boundary */
    
                           "all[]",               /* volume */
                           ' ',                   /* projection */
                           allow_nonmatching,
                           tolerance,
                           verbosity,
                           plot);

  }
  else {
    cs_syr_coupling_define("Solid",
                           NULL,                                 /* boundary */
                           "all[]", /* volume */
                           ' ',                           /* projection */
                           allow_nonmatching,
                           tolerance,
                           verbosity,
                           plot);
  }

}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Compute a volume exchange coefficient for SYRTHES couplings.
 *
 * \param[in]   coupling_id   Syrthes coupling id
 * \param[in]   syrthes_name  name of associated Syrthes instance
 * \param[in]   n_elts        number of associated cells
 * \param[in]   elt_ids       associated cell ids
 * \param[out]  h_vol         associated exchange coefficient (size: n_elts)
 */
/*----------------------------------------------------------------------------*/

void
cs_user_syrthes_coupling_volume_h(int               coupling_id,
                                  const char       *syrthes_name,
                                  cs_lnum_t         n_elts,
                                  const cs_lnum_t   elt_ids[],
                                  cs_real_t         h_vol[])
{
  CS_UNUSED(coupling_id);
  CS_UNUSED(syrthes_name);
  
  cs_field_t *pins_wall_area = cs_field_by_name("SolidPinsWallArea");
  cs_field_t *pseudo_wall_area = cs_field_by_name("ImmersedPseudoWallArea");
  cs_field_t *pins_heat_coeff = cs_field_by_name("PinsHeatTransferCoefficient");
  cs_field_t *debris_heat_coeff = cs_field_by_name("DebrisHeatCoeff");
  cs_real_t area;
  cs_real_t area_pseudo;  
  
  for (cs_lnum_t i = 0; i < n_elts; i++){
      cs_lnum_t iel = elt_ids[i];
      area = pins_wall_area->val[iel];
      area_pseudo = pseudo_wall_area->val[iel]; 
      if (area > 0.0) 
         h_vol[i] = pins_heat_coeff->val[iel]; 
      if (area_pseudo > 0.0)
         h_vol[i] = debris_heat_coeff->val[iel];   
      } 
}

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

END_C_DECLS
