/*============================================================================
 * Define postprocessing output.
 *============================================================================*/

/* Code_Saturne version 6.0.2 */

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

#include "cs_defs.h"

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

#include "stdlib.h"
#include "string.h"

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

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

#include "cs_base.h"
#include "cs_field.h"
#include "cs_geom.h"
#include "cs_interpolate.h"
#include "cs_mesh.h"
#include "cs_selector.h"
#include "cs_parall.h"
#include "cs_post.h"
#include "cs_post_util.h"
#include "cs_probe.h"
#include "cs_time_plot.h"

#include "cs_field_pointer.h"
#include "cs_parameters.h"
#include "cs_physical_constants.h"
#include "cs_turbulence_model.h"

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

#include "cs_prototypes.h"

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

BEGIN_C_DECLS

/*----------------------------------------------------------------------------*/
/*!
 * \brief User function for output of values on a post-processing mesh.
 *
 * \param[in]       mesh_name    name of the output mesh for the current call
 * \param[in]       mesh_id      id of the output mesh for the current call
 * \param[in]       cat_id       category id of the output mesh for the
 *                               current call
 * \param[in]       probes       pointer to associated probe set structure if
 *                               the mesh is a probe set, NULL otherwise
 * \param[in]       n_cells      local number of cells of post_mesh
 * \param[in]       n_i_faces    local number of interior faces of post_mesh
 * \param[in]       n_b_faces    local number of boundary faces of post_mesh
 * \param[in]       n_vertices   local number of vertices faces of post_mesh
 * \param[in]       cell_list    list of cells (0 to n-1) of post-processing
 *                               mesh
 * \param[in]       i_face_list  list of interior faces (0 to n-1) of
 *                               post-processing mesh
 * \param[in]       b_face_list  list of boundary faces (0 to n-1) of
 *                               post-processing mesh
 * \param[in]       vertex_list  list of vertices (0 to n-1) of
 *                               post-processing mesh
 * \param[in]       ts           time step status structure, or NULL
 */
/*----------------------------------------------------------------------------*/

void
cs_user_postprocess_values(const char            *mesh_name,
                           int                    mesh_id,
                           int                    cat_id,
                           cs_probe_set_t        *probes,
                           cs_lnum_t              n_cells,
                           cs_lnum_t              n_i_faces,
                           cs_lnum_t              n_b_faces,
                           cs_lnum_t              n_vertices,
                           const cs_lnum_t        cell_list[],
                           const cs_lnum_t        i_face_list[],
                           const cs_lnum_t        b_face_list[],
                           const cs_lnum_t        vertex_list[],
                           const cs_time_step_t  *ts)
{

  /* Output fluid temperature on surface mesh
     ------------------------------- */

  /*< [postprocess_values_ex_2] */
  if (strcmp(mesh_name, "pressure_surface") == 0) { /* Restrict to this mesh */

    cs_real_t *cvar_scalt = CS_F_(t)->val; /* Temperature */
    /*cs_real_t *cvar_p = CS_F_(p)->val;  pressure */ 
    /*btemp = cs_field_by_name "field_get_val_s_by_name"("Boundary temperature")->val;  Boundary temperature */ 
    /*cs_real_t *cvar_p = cs_field_by_name "field_get_val_s_by_name"("pressure")->val;*/ 
    cs_real_t *cvar_ptt = cs_field_by_name("Boundary temperature")->val;

    /* Ensure variable is synchronized in parallel or periodic cases;
       should already have been done before, repeated for safety */
    cs_mesh_sync_var_scal(cvar_scalt);
    cs_mesh_sync_var_scal(cvar_ptt);

    const cs_mesh_t *m = cs_glob_mesh;

    cs_real_t *s_i_faces = NULL, *s_b_faces = NULL;

    /* Boundary faces  */

    if (n_b_faces > 0) {
      BFT_MALLOC(s_b_faces, n_b_faces, cs_real_t);

      for (cs_lnum_t i = 0; i < n_b_faces; i++) {
        cs_lnum_t face_id = b_face_list[i];
        /* Use adjacent cell value here */
        cs_lnum_t cell_id = m->b_face_cells[face_id];
        s_b_faces[i] = cvar_ptt[face_id];
      } 
    }

    cs_post_write_var(mesh_id,
                      CS_POST_WRITER_ALL_ASSOCIATED,  /* writer id filter */
                      "AFT",                          /* var_name */
                      1,                              /* var_dim */
                      true,                           /* interlace, */
                      false,                          /* use_parent */
                      CS_POST_TYPE_cs_real_t,         /* var_type */
                      NULL,                           /* cel_vals */
                      s_i_faces,                      /* i_face_vals */
                      s_b_faces,                      /* b_face_vals */
                      ts);

    BFT_FREE(s_i_faces);
    BFT_FREE(s_b_faces);
  }

}


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

END_C_DECLS
