/*============================================================================
 * User definition of boundary conditions.
 *============================================================================*/

/* Code_Saturne version 5.2.0 */

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

  Copyright (C) 1998-2018 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

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

#include "bft_mem.h"
#include "bft_error.h"
#include "bft_printf.h"

#include "cs_base.h"
#include "cs_boundary_zone.h"
#include "cs_field.h"
#include "cs_field_pointer.h"
#include "cs_field_operator.h"
#include "cs_elec_model.h"
#include "cs_log.h"
#include "cs_mesh.h"
#include "cs_mesh_location.h"
#include "cs_mesh_quantities.h"
#include "cs_mesh_quantities.h"
#include "cs_parameters.h"
#include "cs_time_step.h"
#include "cs_selector.h"

/*----------------------------------------------------------------------------
 * Header for the current file
 *----------------------------------------------------------------------------*/

#include "cs_prototypes.h"

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

BEGIN_C_DECLS

/*=============================================================================
 * Public function definitions
 *============================================================================*/

/*----------------------------------------------------------------------------*/
/*!
 * \brief User definition of boundary conditions
 *
 * \param[in]     nvar          total number of variable BC's
 * \param[in]     bc_type       boundary face types
 * \param[in]     icodcl        boundary face code
 *                                - 1  -> Dirichlet
 *                                - 2  -> convective outlet
 *                                - 3  -> flux density
 *                                - 4  -> sliding wall and u.n=0 (velocity)
 *                                - 5  -> friction and u.n=0 (velocity)
 *                                - 6  -> roughness and u.n=0 (velocity)
 *                                - 9  -> free inlet/outlet (velocity)
 *                                inflowing possibly blocked
 * \param[in]     rcodcl        boundary condition values
 *                                rcodcl(3) = flux density value
 *                                (negative for gain) in W/m2
 */
/*----------------------------------------------------------------------------*/

void
cs_user_boundary_conditions(int         nvar,
                            int         bc_type[],
                            int         icodcl[],
                            cs_real_t   rcodcl[])
{
  const cs_lnum_t *b_face_cells = cs_glob_mesh->b_face_cells;
  const cs_lnum_t n_b_faces = cs_glob_mesh->n_b_faces;
  const cs_real_3_t *b_face_normal
    = (const cs_real_3_t *) cs_glob_mesh_quantities->b_face_normal;
  cs_lnum_t *lstelt = NULL;
  cs_lnum_t *face_list;
  cs_lnum_t  nelts;
  cs_field_t *f;

  const int keyvar = cs_field_key_id("variable_id");

  BFT_MALLOC(lstelt, n_b_faces, cs_lnum_t);
  
  //top
  cs_selector_get_b_face_list("top", &nelts, lstelt);
  for (cs_lnum_t ilelt = 0; ilelt < nelts; ilelt++) {
    cs_lnum_t face_id = lstelt[ilelt];
    bc_type[face_id] = CS_SYMMETRY;

  }

  //bottom
  // cs_selector_get_b_face_list("bottom", &nelts, lstelt);
  // for (cs_lnum_t ilelt = 0; ilelt < nelts; ilelt++) {
  //   cs_lnum_t face_id = lstelt[ilelt];
  //   bc_type[face_id] = CS_SYMMETRY;

  // }
  
  //botom
  cs_selector_get_b_face_list("bottom", &nelts, lstelt);
  for (cs_lnum_t ilelt = 0; ilelt < nelts; ilelt++) {
    cs_lnum_t face_id = lstelt[ilelt];
    bc_type[face_id] = CS_ROUGHWALL;
        // cell_id = b_face_cells[face_id]; // associated boundary cell
    // f = CS_F_(u);
    // int ivar = cs_field_get_key_int(f, keyvar) - 1;
    // icodcl[ivar * n_b_faces + face_id] = 3; //3??
    // rcodcl[ivar * n_b_faces + face_id] = 0.01; //roughness value z0
  }
  // // Inlet
  cs_selector_get_b_face_list("left", &nelts, lstelt);
  for (cs_lnum_t ilelt = 0; ilelt < nelts; ilelt++) {
    cs_lnum_t face_id = lstelt[ilelt];
    bc_type[face_id] = CS_INLET;
    //cell_id = b_face_cells[face_id]; // associated boundary cell

    f = CS_F_(u);//get velocity field
    //f->dim==3
    
    //define Ux
    int ivar = cs_field_get_key_int(f, keyvar) - 1 + 0; 
    icodcl[(ivar) * n_b_faces + face_id] = 1; //Dirihlet value
    rcodcl[(ivar) * n_b_faces + face_id] = 1.0; //Value 

    //define Uy
    ivar = cs_field_get_key_int(f, keyvar) - 1 + 1; //uy
    icodcl[(ivar) * n_b_faces + face_id] = 1; //Dirihlet value
    rcodcl[(ivar) * n_b_faces + face_id] = 0.; //Value  

    //define Uz
    ivar = cs_field_get_key_int(f, keyvar) - 1 + 2; //uz
    icodcl[(ivar) * n_b_faces + face_id] = 1; //Dirihlet value
    rcodcl[(ivar) * n_b_faces + face_id] = 0.; //Value  

    f = CS_F_(k);//get turbulence energy field
    ivar = cs_field_get_key_int(f, keyvar) - 1; 
    icodcl[(ivar) * n_b_faces + face_id] = 1; //Dirihlet value
    rcodcl[(ivar) * n_b_faces + face_id] = 0.0; //Value  

    f = CS_F_(eps);//get turbulence energy field
    ivar = cs_field_get_key_int(f, keyvar) - 1; 
    icodcl[(ivar) * n_b_faces + face_id] = 1; //Dirihlet value
    rcodcl[(ivar) * n_b_faces + face_id] = 0.0; //Value  

  }

  //Outlet 
  cs_selector_get_b_face_list("right", &nelts, lstelt);
  for (cs_lnum_t ilelt = 0; ilelt < nelts; ilelt++) {
    cs_lnum_t face_id = lstelt[ilelt];
    bc_type[face_id] = CS_OUTLET;
    //cell_id = b_face_cells[face_id]; // associated boundary cell

    // f = CS_F_(u);//get velocity field
    //f->dim==3
    
    //define -Ux
    // int ivar = cs_field_get_key_int(f, keyvar) - 1 + 0; 
    // icodcl[(ivar) * n_b_faces + face_id] = 1; //Dirihlet value
    // rcodcl[(ivar) * n_b_faces + face_id] = -1.0; //Value of 

    // //define Uy
    // ivar = cs_field_get_key_int(f, keyvar) - 1 + 1; //uy
    // icodcl[(ivar) * n_b_faces + face_id] = 1; //Dirihlet value
    // rcodcl[(ivar) * n_b_faces + face_id] = 0.; //Value of 

    // //define Uz
    // ivar = cs_field_get_key_int(f, keyvar) - 1 + 2; //uz
    // icodcl[(ivar) * n_b_faces + face_id] = 1; //Dirihlet value
    // rcodcl[(ivar) * n_b_faces + face_id] = 0.; //Value of 

    // f = CS_F_(k);//get turbulence energy field
    // ivar = cs_field_get_key_int(f, keyvar) - 1; 
    // icodcl[(ivar) * n_b_faces + face_id] = 1; //Dirihlet value
    // rcodcl[(ivar) * n_b_faces + face_id] = 0.0; //Value of 

    // f = CS_F_(eps);//get turbulence energy field
    // ivar = cs_field_get_key_int(f, keyvar) - 1; 
    // icodcl[(ivar) * n_b_faces + face_id] = 1; //Dirihlet value
    // rcodcl[(ivar) * n_b_faces + face_id] = 0.0; //Value of 
  }
  BFT_FREE(lstelt);

}

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

END_C_DECLS
