/*============================================================================
 * General-purpose user-defined functions called before time stepping, at
 * the end of each time step, and after time-stepping.
 *
 * These can be used for operations which do not fit naturally in any other
 * dedicated user function.
 *============================================================================*/

/* code_saturne version 8.0 */

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

  Copyright (C) 1998-2023 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>

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

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

#include "cs_headers.h"

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

BEGIN_C_DECLS

/*----------------------------------------------------------------------------*/
/*!
 * \file cs_user_extra_operations.c
 *
 * \brief This function is called at the end of each time step, and has a very
 * general purpose (i.e. anything that does not have another dedicated
 * user function)
 */
/*----------------------------------------------------------------------------*/

static FILE *f_file = NULL;

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

/*----------------------------------------------------------------------------*/
/*!
 * \brief This function is called at the end of each time step.
 *
 * It has a very general purpose, although it is recommended to handle
 * mainly postprocessing or data-extraction type operations.
 *
 * \param[in, out]  domain   pointer to a cs_domain_t structure
 */
/*----------------------------------------------------------------------------*/

void
cs_user_extra_operations(cs_domain_t  *domain)
{
  CS_UNUSED(domain);

  const cs_lnum_t *b_face_cells = domain->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;

  const cs_real_3_t  *forbr
    = (const cs_real_3_t *)cs_field_by_name("boundary_forces")->val;
  const cs_real_t  *cvar_p = CS_F_(p)->val;

  const cs_zone_t *z = cs_boundary_zone_by_name("wing");

  double forces[4] = {0, 0, 0, 0};

  for (cs_lnum_t i = 0; i < z->n_elts; i++) {
    cs_lnum_t face_id = z->elt_ids[i];
    cs_lnum_t cell_id = b_face_cells[face_id];

    forces[0] += forbr[face_id][0];
    forces[1] += forbr[face_id][2];
    forces[2] += cvar_p[cell_id]*b_face_normal[face_id][0];
    forces[3] += cvar_p[cell_id]*b_face_normal[face_id][2];
  }

  cs_parall_sum(4, CS_DOUBLE, forces);

  if (cs_glob_rank_id == 0) {
    if (f_file == NULL) {
      f_file = fopen("lift_and_drag.txt", "w");
      fprintf(f_file, "# time_step lift drag pressure_sum_z pressure_sum_x\n");
    }
    fprintf(f_file, "%d %g %g %g %g\n", domain->time_step->nt_cur,
            forces[1], forces[2], forces[3], forces[2]);
    if (cs_glob_time_step->nt_cur == cs_glob_time_step->nt_max) {
      fclose(f_file);
      f_file = NULL;
    }
  }
}

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

END_C_DECLS
