cs_user_extra_operations_IC.c 8.77 KB
Newer Older
Alice MAISON's avatar
Alice MAISON committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
/*============================================================================
 * 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)
 *============================================================================*/

/* Code_Saturne version 6.0-beta */

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

  Copyright (C) 1998-2019 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"
#include "cs_math.h"

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

#include <assert.h>
#include <math.h>

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

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

#include <ple_coupling.h>

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

#include "cs_headers.h"

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

#include "cs_prototypes.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)
 */
/*----------------------------------------------------------------------------*/

/*============================================================================
 * 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)
{
  /* Get number of cells */
  const cs_mesh_t *m = domain->mesh;
  const cs_lnum_t n_cells = m->n_cells;

  /* Get physical fields */
  const cs_real_t *prop = cs_field_by_name("traceur")->val;
  const cs_real_t *densite = cs_field_by_name("density")->val;
  const cs_real_3_t *vel = cs_field_by_name("velocity")->val;
  const cs_real_t *k = cs_field_by_name("k")->val;
  const cs_real_t *epsilon = cs_field_by_name("epsilon")->val;

  /* Name of the first zone and first output file */
  char zone[100] = "x>27.5 and x<55 and z>0 and z<14";  
  char fname[100] = "concentration_traceur_rue.dat";
  /* Name of the second zone and output file */
  char zone2[100] = "x>27.5 and x<55 and z>14 and z<28";
  char fname2[100] = "concentration_haut_rue.dat";

  /* Write header at the first time step */
  FILE *f = NULL;
  FILE *f2 = NULL;
  if (cs_glob_time_step->nt_cur == cs_glob_time_step->nt_ini && cs_glob_rank_id <= 0) {
    f = fopen(fname,"w");
    fprintf(f, "#%17s%100s\n", "zone : ", zone);
    fprintf(f, "#%s\n", "temps (sec), traceur (microg/m3), Vx, Vy, Vz, densité (kg/m3), k, epsilon");
    fclose(f);
    f2 = fopen(fname2,"w");                                                                      
    fprintf(f2, "#%17s%100s\n", "zone : ", zone2);                                               
    fprintf(f2, "#%s\n", "temps (sec), traceur (microg/m3), Vx, Vy, Vz, densité (kg/m3), k, epsilon");
    fclose(f2);
  }

  /* Open file and print time */
  if (cs_glob_rank_id <= 0) {
    f = fopen(fname,"a");
    fprintf(f, "%17.9e ", cs_glob_time_step->t_cur);
    f2 = fopen(fname2,"a");
    fprintf(f2, "%17.9e ", cs_glob_time_step->t_cur);
  }

  /* Allocate memory */
  cs_lnum_t n_cells_sel = 0;
  cs_lnum_t *cells_sel_ids = NULL;
  BFT_MALLOC(cells_sel_ids, m->n_cells, cs_lnum_t);

  /* Temporary variables */
  cs_real_t sum, rrho, sumx, sumy, sumz, sumk, sumepsilon, sumvolume;

  /* Get cells for the first zone */
  cs_selector_get_cell_list(zone, &n_cells_sel, cells_sel_ids);

  /* Average various quantities over the first zone */
  {
    sum = 0;
    rrho = 0;
    sumx = 0;
    sumy = 0;
    sumz = 0;
    sumk = 0;
    sumepsilon = 0;
    sumvolume = 0;
    const cs_real_t *restrict volume = domain->mesh_quantities->cell_f_vol;
    for (cs_lnum_t iel = 0; iel < n_cells_sel; iel++) {
      const cs_lnum_t cell_id = cells_sel_ids[iel];
      sum += 1000. * prop[cell_id] * densite[cell_id] * volume[cell_id];
      rrho += densite[cell_id] * volume[cell_id];
      sumx += vel[cell_id][0] * volume[cell_id];
      sumy += vel[cell_id][1] * volume[cell_id];
      sumz += vel[cell_id][2] * volume[cell_id];
      sumk += k[cell_id] * volume[cell_id];
      sumepsilon += epsilon[cell_id] * volume[cell_id];
      sumvolume += volume[cell_id];
    }
  }

  /* Parallel sum then write to file */
  cs_parall_sum(1, CS_DOUBLE, &sum);
  cs_parall_sum(1, CS_DOUBLE, &rrho);
  cs_parall_sum(1, CS_DOUBLE, &sumx);
  cs_parall_sum(1, CS_DOUBLE, &sumy);
  cs_parall_sum(1, CS_DOUBLE, &sumz);
  cs_parall_sum(1, CS_DOUBLE, &sumvolume);
  cs_parall_sum(1, CS_DOUBLE, &sumk);
  cs_parall_sum(1, CS_DOUBLE, &sumepsilon);
  if (cs_glob_rank_id <= 0) fprintf(f," %17.9e", sum/sumvolume);
  if (cs_glob_rank_id <= 0) fprintf(f," %17.9e", sumx/sumvolume);
  if (cs_glob_rank_id <= 0) fprintf(f," %17.9e", sumy/sumvolume);
  if (cs_glob_rank_id <= 0) fprintf(f," %17.9e", sumz/sumvolume);
  if (cs_glob_rank_id <= 0) fprintf(f," %17.9e", rrho/sumvolume);
  if (cs_glob_rank_id <= 0) fprintf(f," %17.9e", sumk/sumvolume);
  if (cs_glob_rank_id <= 0) fprintf(f," %17.9e", sumepsilon/sumvolume);

  /* Print end of line and close the file */
  if (cs_glob_rank_id <= 0) {
    fprintf(f, "\n");
    fclose(f);
  }

  /* Get cells for the second zone */
  cs_selector_get_cell_list(zone2, &n_cells_sel, cells_sel_ids);

  /* Average various quantities over the second zone */
  {
    sum = 0;
    rrho = 0;
    sumx = 0;
    sumy = 0;
    sumz = 0;
    sumk = 0;
    sumepsilon = 0;
    sumvolume = 0;
    const cs_real_t *restrict volume = domain->mesh_quantities->cell_f_vol;
    for (cs_lnum_t iel = 0; iel < n_cells_sel; iel++) {
      const cs_lnum_t cell_id = cells_sel_ids[iel];
      sum += 1000. * prop[cell_id] * densite[cell_id] * volume[cell_id];
      rrho += densite[cell_id] * volume[cell_id];
      sumx += vel[cell_id][0] * volume[cell_id];
      sumy += vel[cell_id][1] * volume[cell_id];
      sumz += vel[cell_id][2] * volume[cell_id];
      sumk += k[cell_id] * volume[cell_id];
      sumepsilon += epsilon[cell_id] * volume[cell_id];
      sumvolume += volume[cell_id];
    }
  }

  /* Parallel sum then write to file */
  cs_parall_sum(1, CS_DOUBLE, &sum);
  cs_parall_sum(1, CS_DOUBLE, &rrho);
  cs_parall_sum(1, CS_DOUBLE, &sumx);
  cs_parall_sum(1, CS_DOUBLE, &sumy);
  cs_parall_sum(1, CS_DOUBLE, &sumz);
  cs_parall_sum(1, CS_DOUBLE, &sumvolume);
  cs_parall_sum(1, CS_DOUBLE, &sumk);
  cs_parall_sum(1, CS_DOUBLE, &sumepsilon);
  if (cs_glob_rank_id <= 0) fprintf(f2," %17.9e", sum/sumvolume);
  if (cs_glob_rank_id <= 0) fprintf(f2," %17.9e", sumx/sumvolume);
  if (cs_glob_rank_id <= 0) fprintf(f2," %17.9e", sumy/sumvolume);
  if (cs_glob_rank_id <= 0) fprintf(f2," %17.9e", sumz/sumvolume);
  if (cs_glob_rank_id <= 0) fprintf(f2," %17.9e", rrho/sumvolume);
  if (cs_glob_rank_id <= 0) fprintf(f2," %17.9e", sumk/sumvolume);
  if (cs_glob_rank_id <= 0) fprintf(f2," %17.9e", sumepsilon/sumvolume);

  /* Print end of line and close the file */
  if (cs_glob_rank_id <= 0) {
    fprintf(f2, "\n");
    fclose(f2);
  }

  /* Free memory */
  BFT_FREE(cells_sel_ids);

}