1 #ifndef __CS_MESH_QUANTITIES_H__
2 #define __CS_MESH_QUANTITIES_H__
3 
4 /*============================================================================
5  * Management of mesh quantities
6  *============================================================================*/
7 
8 /*
9   This file is part of Code_Saturne, a general-purpose CFD tool.
10 
11   Copyright (C) 1998-2021 EDF S.A.
12 
13   This program is free software; you can redistribute it and/or modify it under
14   the terms of the GNU General Public License as published by the Free Software
15   Foundation; either version 2 of the License, or (at your option) any later
16   version.
17 
18   This program is distributed in the hope that it will be useful, but WITHOUT
19   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
21   details.
22 
23   You should have received a copy of the GNU General Public License along with
24   this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
25   Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 */
27 
28 /*----------------------------------------------------------------------------*/
29 
30 /*----------------------------------------------------------------------------
31  *  Local headers
32  *----------------------------------------------------------------------------*/
33 
34 #include "cs_base.h"
35 #include "cs_mesh.h"
36 
37 /*----------------------------------------------------------------------------*/
38 
39 BEGIN_C_DECLS
40 
41 /*=============================================================================
42  * Macro definitions
43  *============================================================================*/
44 
45 /*!
46  * @defgroup bad_cells_flags Flags specifying bad cells treatment
47  *
48  * @{
49  */
50 
51 /*
52  * Cell quantities correction types
53  */
54 
55 /*! Correct bad cells warping for gradients */
56 #define CS_BAD_CELLS_WARPED_CORRECTION (1 << 0)
57 
58 /*! Regularise on bad cells */
59 #define CS_BAD_CELLS_REGULARISATION (1 << 1)
60 
61 /*! Recompute face centers */
62 #define CS_CELL_FACE_CENTER_CORRECTION (1 << 2)
63 
64 /*! Recompute cell centers */
65 #define CS_CELL_CENTER_CORRECTION (1 << 3)
66 
67 /*! Clip face distance when negative or too small */
68 #define CS_FACE_DISTANCE_CLIP (1 << 4)
69 
70 /*! Clip geometrical quantities used in flux reconstuction */
71 #define CS_FACE_RECONSTRUCTION_CLIP (1 << 5)
72 
73 /*! Limit cells volume ratio */
74 #define CS_CELL_VOLUME_RATIO_CORRECTION (1 << 6)
75 
76 /*! Refine face center computation for warped cells
77     (iteratively compute center using previous position instead
78     of using only the initial estimate based on the vertices center) */
79 #define CS_FACE_CENTER_REFINE (1 << 7)
80 
81 /*! @} */
82 
83 /*============================================================================
84  * Type definition
85  *============================================================================*/
86 
87 /* Structure associated to mesh quantities management */
88 
89 typedef struct {
90 
91   cs_real_t     *cell_cen;       /* Cell center coordinates  */
92   cs_real_t     *cell_vol;       /* Cell volume */
93   cs_real_t     *cell_f_vol;     /* Cell fluid volume */
94 
95   cs_real_t     *i_face_normal;  /* Surface normal of interior faces.
96                                     (L2 norm equals area of the face) */
97   cs_real_t     *b_face_normal;  /* Surface normal of border faces.
98                                     (L2 norm equals area of the face) */
99   cs_real_t     *i_f_face_normal;/* Fluid surface normal of interior faces.
100                                     (L2 norm equals area of the face) */
101   cs_real_t     *b_f_face_normal;/* Fluid surface normal of border faces.
102                                     (L2 norm equals area of the face) */
103   cs_real_t     *i_face_cog;     /* Center of gravity of interior faces */
104   cs_real_t     *b_face_cog;     /* Center of gravity of border faces */
105 
106   cs_real_t     *i_face_surf;    /* Surface of interior faces. */
107   cs_real_t     *b_face_surf;    /* Surface of boundary faces. */
108 
109   cs_real_t     *i_f_face_surf;  /* Fluid surface of interior faces. */
110   cs_real_t     *b_f_face_surf;  /* Fluid surface of boundary faces. */
111 
112   cs_real_2_t   *i_f_face_factor;/* Fluid surface factor of interior faces. */
113   cs_real_t     *b_f_face_factor;/* Fluid surface factor of boundary faces. */
114 
115   cs_real_t     *dijpf;          /* Vector I'J' for interior faces */
116   cs_real_t     *diipb;          /* Vector II'  for border faces */
117   cs_real_t     *dofij;          /* Vector OF   for interior faces */
118   cs_real_t     *diipf;          /* Vector II'  for interior faces */
119   cs_real_t     *djjpf;          /* Vector JJ'  for interior faces */
120 
121   cs_real_t     *i_dist;         /* Distance between the centers of the two
122                                     cells sharing an interior face */
123   cs_real_t     *b_dist;         /* Distance between the cell center and
124                                     the center of gravity of border faces */
125 
126   cs_real_t     *weight;         /* Interior faces weighting factor */
127 
128   cs_real_t      min_vol;        /* Minimum cell volume */
129   cs_real_t      max_vol;        /* Maximum cell volume */
130   cs_real_t      tot_vol;        /* Total volume */
131 
132   cs_real_t      min_f_vol;      /* Minimum cell volume */
133   cs_real_t      max_f_vol;      /* Maximum cell volume */
134   cs_real_t      tot_f_vol;      /* Total volume */
135 
136   cs_real_t     *corr_grad_lin_det; /* Determinant of geometrical matrix
137                                        linear gradient correction */
138   cs_real_33_t  *corr_grad_lin;     /* Geometrical matrix
139                                        linear gradient correction */
140 
141   int          *b_sym_flag;         /* Symmetry flag for boundary faces */
142   int           has_disable_flag;   /* Is the cell disabled?
143                                        0: unactivated
144                                        1: activated */
145   int          *c_disable_flag;     /* Is the cell disabled?
146                                        used for fluid solid and porous models */
147   unsigned     *bad_cell_flag;      /* Flag (mask) for bad cells detected */
148 
149 } cs_mesh_quantities_t ;
150 
151 /*============================================================================
152  * Global variables
153  *============================================================================*/
154 
155 /* Pointer to mesh quantities structure associated to the main mesh */
156 
157 extern cs_mesh_quantities_t  *cs_glob_mesh_quantities;
158 
159 /* Flag (mask) to activate bad cells correction */
160 extern unsigned cs_glob_mesh_quantities_flag;
161 
162 /*=============================================================================
163  * Public function prototypes
164  *============================================================================*/
165 
166 /*----------------------------------------------------------------------------*/
167 /*!
168  * \brief  Query or modification of the option for computing cell centers.
169  *
170  * \param[in]  algo_choice  < 0 : query
171  *                            0 : computation based on face centers (default)
172  *                            1 : computation by cell sub-volumes
173  *
174  * \return  0 or 1 according to the selected algorithm
175  */
176 /*----------------------------------------------------------------------------*/
177 
178 int
179 cs_mesh_quantities_cell_cen_choice(int  algo_choice);
180 
181 /*----------------------------------------------------------------------------*/
182 /*!
183  * \brief  Query or modification of the option for computing face centers.
184  *
185  * \param[in]  algo_choice  < 0 : query
186  *                            0 : standard computation
187  *                            1 : use adjustment for volume
188  *                                from versions 1.1 to 5.3
189  *
190  * \return  0 or 1 according to the selected algorithm
191  */
192 /*----------------------------------------------------------------------------*/
193 
194 int
195 cs_mesh_quantities_face_cog_choice(int  algo_choice);
196 
197 /*----------------------------------------------------------------------------*/
198 /*!
199  * \brief  Create a mesh quantities structure.
200  *
201  * \return  pointer to created cs_mesh_quantities_t structure
202  */
203 /*----------------------------------------------------------------------------*/
204 
205 cs_mesh_quantities_t *
206 cs_mesh_quantities_create(void);
207 
208 /*----------------------------------------------------------------------------*/
209 /*!
210  * \brief  Destroy a mesh quantities structure.
211  *
212  * \param[in]  mq  pointer to mesh quantities structures
213  *
214  * \return  NULL
215  */
216 /*----------------------------------------------------------------------------*/
217 
218 cs_mesh_quantities_t *
219 cs_mesh_quantities_destroy(cs_mesh_quantities_t  *mq);
220 
221 /*----------------------------------------------------------------------------*/
222 /*!
223  * \brief  Reset a mesh quantities structure to its empty initial state.
224  *
225  * \param[in]  mq  pointer to mesh quantities structures
226  */
227 /*----------------------------------------------------------------------------*/
228 
229 void
230 cs_mesh_quantities_free_all(cs_mesh_quantities_t  *mq);
231 
232 /*----------------------------------------------------------------------------*/
233 /*!
234  * \brief  Compute mesh quantities needed for preprocessing.
235  *
236  * \param[in]       m   pointer to mesh structure
237  * \param[in, out]  mq  pointer to mesh quantities structures
238  */
239 /*----------------------------------------------------------------------------*/
240 
241 void
242 cs_mesh_quantities_compute_preprocess(const cs_mesh_t       *m,
243                                       cs_mesh_quantities_t  *mq);
244 
245 /*----------------------------------------------------------------------------*/
246 /*!
247  * \brief  Compute mesh quantities.
248  *
249  * \param[in]       m   pointer to mesh structure
250  * \param[in, out]  mq  pointer to mesh quantities structures.
251  */
252 /*----------------------------------------------------------------------------*/
253 
254 void
255 cs_mesh_quantities_compute(const cs_mesh_t       *m,
256                            cs_mesh_quantities_t  *mq);
257 
258 /*----------------------------------------------------------------------------
259  * Compute fluid mesh quantities
260  *
261  * parameters:
262  *   mesh            <-- pointer to a cs_mesh_t structure
263  *   mesh_quantities <-> pointer to a cs_mesh_quantities_t structure
264  *----------------------------------------------------------------------------*/
265 
266 void
267 cs_mesh_quantities_fluid_compute(const cs_mesh_t       *mesh,
268                                  cs_mesh_quantities_t  *mesh_quantities);
269 
270 /*----------------------------------------------------------------------------
271  * Compute the total, min, and max fluid volumes of cells
272  *
273  * parameters:
274  *   mesh            <-- pointer to mesh structure
275  *   mesh_quantities <-> pointer to a mesh quantities structure
276  *----------------------------------------------------------------------------*/
277 
278 void
279 cs_mesh_quantities_fluid_vol_reductions(const cs_mesh_t       *mesh,
280                                         cs_mesh_quantities_t  *mesh_quantities);
281 
282 /*----------------------------------------------------------------------------
283  * Compute fluid section mesh quantities at the initial step
284  *
285  * parameters:
286  *   mesh            <-- pointer to a cs_mesh_t structure
287  *   mesh_quantities <-> pointer to a cs_mesh_quantities_t structure
288  *----------------------------------------------------------------------------*/
289 
290 void
291 cs_mesh_init_fluid_sections(const cs_mesh_t       *mesh,
292                             cs_mesh_quantities_t  *mesh_quantities);
293 
294 /*----------------------------------------------------------------------------
295  * Compute mesh quantities
296  *
297  * parameters:
298  *   mesh            <-- pointer to a cs_mesh_t structure
299  *   mesh_quantities <-> pointer to a cs_mesh_quantities_t structure
300  *----------------------------------------------------------------------------*/
301 
302 void
303 cs_mesh_quantities_sup_vectors(const cs_mesh_t       *mesh,
304                                cs_mesh_quantities_t  *mesh_quantities);
305 
306 /*----------------------------------------------------------------------------
307  * Compute internal and border face normal.
308  *
309  * parameters:
310  *   mesh            <-- pointer to a cs_mesh_t structure
311  *   p_i_face_normal <-> pointer to the internal face normal array
312  *   p_b_face_normal <-> pointer to the border face normal array
313  *----------------------------------------------------------------------------*/
314 
315 void
316 cs_mesh_quantities_face_normal(const cs_mesh_t   *mesh,
317                                cs_real_t         *p_i_face_normal[],
318                                cs_real_t         *p_b_face_normal[]);
319 
320 /*----------------------------------------------------------------------------
321  * Compute interior face centers and normals.
322  *
323  * The corresponding arrays are allocated by this function, and it is the
324  * caller's responsibility to free them when they are no longer needed.
325  *
326  * parameters:
327  *   mesh            <-- pointer to a cs_mesh_t structure
328  *   p_i_face_cog    <-> pointer to the interior face center array
329  *   p_i_face_normal <-> pointer to the interior face normal array
330  *----------------------------------------------------------------------------*/
331 
332 void
333 cs_mesh_quantities_i_faces(const cs_mesh_t   *mesh,
334                            cs_real_t         *p_i_face_cog[],
335                            cs_real_t         *p_i_face_normal[]);
336 
337 /*----------------------------------------------------------------------------
338  * Compute border face centers and normals.
339  *
340  * The corresponding arrays are allocated by this function, and it is the
341  * caller's responsibility to free them when they are no longer needed.
342  *
343  * parameters:
344  *   mesh            <-- pointer to a cs_mesh_t structure
345  *   p_b_face_cog    <-> pointer to the border face center array
346  *   p_b_face_normal <-> pointer to the border face normal array
347  *----------------------------------------------------------------------------*/
348 
349 void
350 cs_mesh_quantities_b_faces(const cs_mesh_t   *mesh,
351                            cs_real_t         *p_b_face_cog[],
352                            cs_real_t         *p_b_face_normal[]);
353 
354 /*----------------------------------------------------------------------------*/
355 /*!
356  * \brief  Compute approximate cells centers as the mean of the given face
357  *         centers weighted by the associated surfaces.
358  *
359  *           n-1
360  *           Sum  Surf(Fi) G(Fi)
361  *           i=0
362  *  G(C) = -----------------------
363  *           n-1
364  *           Sum  Surf(Fi)
365  *           i=0
366  *
367  * \param[in]   mesh         pointer to mesh structure
368  * \param[in]   i_face_norm  surface normal of internal faces
369  * \param[in]   i_face_cog   center of gravity of internal faces
370  * \param[in]   b_face_norm  surface normal of border faces
371  * \param[in]   b_face_cog   center of gravity of border faces
372  * \param[out]  cell_cen     cell centers
373  */
374 /*----------------------------------------------------------------------------*/
375 
376 void
377 cs_mesh_quantities_cell_faces_cog(const cs_mesh_t  *mesh,
378                                   const cs_real_t   i_face_norm[],
379                                   const cs_real_t   i_face_cog[],
380                                   const cs_real_t   b_face_norm[],
381                                   const cs_real_t   b_face_cog[],
382                                   cs_real_t         cell_cen[]);
383 
384 /*----------------------------------------------------------------------------
385  * Compute cell volumes.
386  *
387  * The corresponding array is allocated by this function, and it is the
388  * caller's responsability to free it when they are no longer needed.
389  *
390  * parameters:
391  *   mesh     <-- pointer to a cs_mesh_t structure
392  *
393  * return:
394  *   pointer to newly allocated cell volumes array
395  *----------------------------------------------------------------------------*/
396 
397 cs_real_t *
398 cs_mesh_quantities_cell_volume(const cs_mesh_t  *mesh);
399 
400 /*----------------------------------------------------------------------------
401  * Check that no negative volumes are present, and exit on error otherwise.
402  *
403  * parameters:
404  *   mesh            <-- pointer to mesh structure
405  *   mesh_quantities <-- pointer to mesh quantities structure
406  *   allow_error     <-- 1 if errors are allowed, 0 otherwise
407  *----------------------------------------------------------------------------*/
408 
409 void
410 cs_mesh_quantities_check_vol(const cs_mesh_t             *mesh,
411                              const cs_mesh_quantities_t  *mesh_quantities,
412                              int                          allow_error);
413 
414 /*----------------------------------------------------------------------------*/
415 /*!
416  * \brief  Compute the bounding box for cells.
417  *
418  * The corresponding array is allocated by this function, and it is the
419  * caller's responsability to free it when they are no longer needed.
420  *
421  * \param[in]   m          pointer to mesh structure
422  * \param[in]   tolerance  addition to local extents of each element:
423  *                         extent = base_extent * (1 + tolerance)
424  *
425  * \return  pointer to newly allocated cell volumes array
426  */
427 /*----------------------------------------------------------------------------*/
428 
429 cs_real_6_t *
430 cs_mesh_quantities_cell_extents(const cs_mesh_t  *m,
431                                 cs_real_t         tolerance);
432 
433 /*----------------------------------------------------------------------------
434  * Return the number of times mesh quantities have been computed.
435  *
436  * returns:
437  *   number of times mesh quantities have been computed
438  *----------------------------------------------------------------------------*/
439 
440 int
441 cs_mesh_quantities_compute_count(void);
442 
443 /*----------------------------------------------------------------------------*/
444 /*!
445  * \brief  Determine local boundary thickness around each vertex.
446  *
447  * \param[in]   m            pointer to mesh structure
448  * \param[in]   mq           pointer to mesh quantities structures.
449  * \param[in]   n_passes     number of smoothing passes
450  * \param[out]  b_thickness  thickness for each mesh vertex
451  *                           (0 at non-boundary vertices)
452  */
453 /*----------------------------------------------------------------------------*/
454 
455 void
456 cs_mesh_quantities_b_thickness_v(const cs_mesh_t             *m,
457                                  const cs_mesh_quantities_t  *mq,
458                                  int                          n_passes,
459                                  cs_real_t                    b_thickness[]);
460 
461 /*----------------------------------------------------------------------------*/
462 /*!
463  * \brief  Determine local boundary thickness around each boundary face.
464  *
465  * \param[in]   m            pointer to mesh structure
466  * \param[in]   mq           pointer to mesh quantities structures.
467  * \param[in]   n_passes     number of optional smoothing passes
468  * \param[out]  b_thickness  thickness for each mesh boundary face
469  */
470 /*----------------------------------------------------------------------------*/
471 
472 void
473 cs_mesh_quantities_b_thickness_f(const cs_mesh_t             *m,
474                                  const cs_mesh_quantities_t  *mq,
475                                  int                          n_passes,
476                                  cs_real_t                    b_thickness[]);
477 
478 /*----------------------------------------------------------------------------*/
479 /*!
480  * \brief  Log mesh quantities options to setup file.
481  */
482 /*----------------------------------------------------------------------------*/
483 
484 void
485 cs_mesh_quantities_log_setup(void);
486 
487 /*----------------------------------------------------------------------------
488  * Dump a cs_mesh_quantities_t structure
489  *
490  * parameters:
491  *   mesh            <-- pointer to a cs_mesh_t structure
492  *   mesh_quantities <-- pointer to a cs_mesh_quantities_t structure
493  *----------------------------------------------------------------------------*/
494 
495 void
496 cs_mesh_quantities_dump(const cs_mesh_t             *mesh,
497                         const cs_mesh_quantities_t  *mesh_quantities);
498 
499 /*----------------------------------------------------------------------------*/
500 
501 END_C_DECLS
502 
503 #endif /* __CS_MESH_QUANTITIES_H__ */
504