9.2
general documentation
cs_gradient.h
Go to the documentation of this file.
1#ifndef CS_GRADIENT_H
2#define CS_GRADIENT_H
3
4/*============================================================================
5 * Gradient reconstruction.
6 *============================================================================*/
7
8/*
9 This file is part of code_saturne, a general-purpose CFD tool.
10
11 Copyright (C) 1998-2026 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 "base/cs_base.h"
35#include "base/cs_halo.h"
37#include "mesh/cs_mesh.h"
39
40/*=============================================================================
41 * Local Macro definitions
42 *============================================================================*/
43
44/*============================================================================
45 * Type definition
46 *============================================================================*/
47
48/*----------------------------------------------------------------------------
49 * Gradient reconstruction method
50 *----------------------------------------------------------------------------*/
51
52#define CS_IPRIME_F_LSQ 0
53#define CS_IF_LSQ 1
54
55#define B_DIRECTION_LSQ CS_IPRIME_F_LSQ //CS_IF_LSQ
56
57typedef enum {
58
68
69/*----------------------------------------------------------------------------
70 * Gradient limiter mode
71 *----------------------------------------------------------------------------*/
72
73typedef enum {
74
90
91/*============================================================================
92 * Global variables
93 *============================================================================*/
94
95/* Short names for gradient types */
96
97extern const char *cs_gradient_type_name[];
98
99/*=============================================================================
100 * Public function prototypes
101 *============================================================================*/
102
103/*----------------------------------------------------------------------------
104 * Initialize gradient computation API.
105 *----------------------------------------------------------------------------*/
106
107void
109
110/*----------------------------------------------------------------------------
111 * Finalize gradient computation API.
112 *----------------------------------------------------------------------------*/
113
114void
116
117/*----------------------------------------------------------------------------*/
124/*----------------------------------------------------------------------------*/
125
126void
128
129/*----------------------------------------------------------------------------*/
130/*
131 * \brief Compute cell gradient of scalar field or component of vector or
132 * tensor field.
133 *
134 * \param[in] var_name variable name
135 * \param[in] gradient_type gradient type
136 * \param[in] halo_type halo type
137 * \param[in] inc if 0, solve on increment; 1 otherwise
138 * \param[in] n_r_sweeps if > 1, number of reconstruction sweeps
139 * (only used by CS_GRADIENT_GREEN_ITER)
140 * \param[in] hyd_p_flag flag for hydrostatic pressure
141 * \param[in] w_stride stride for weighting coefficient
142 * \param[in] verbosity verbosity level
143 * \param[in] clip_mode clipping mode
144 * \param[in] epsilon precision for iterative gradient calculation
145 * \param[in] clip_coeff clipping coefficient
146 * \param[in] f_ext exterior force generating the
147 * hydrostatic pressure
148 * \param[in] bc_coeffs boundary condition structure
149 * \param[in, out] var gradient's base variable
150 * \param[in, out] c_weight cell variable weight, or nullptr
151 * \param[in] cpl associated internal coupling, or nullptr
152 * \param[out] grad gradient
153 */
154/*----------------------------------------------------------------------------*/
155
156void
157cs_gradient_scalar(const char *var_name,
158 cs_gradient_type_t gradient_type,
159 cs_halo_type_t halo_type,
160 int inc,
161 int n_r_sweeps,
162 int hyd_p_flag,
163 int w_stride,
164 int verbosity,
165 cs_gradient_limit_t clip_mode,
166 double epsilon,
167 double clip_coeff,
168 cs_real_3_t f_ext[],
169 cs_field_bc_coeffs_t *bc_coeffs,
170 cs_real_t var[],
171 cs_real_t *c_weight,
172 const cs_internal_coupling_t *cpl,
173 cs_real_t grad[][3]);
174
175/*----------------------------------------------------------------------------*/
176/*
177 * \brief Compute cell gradient of vector field.
178 *
179 * \param[in] var_name variable name
180 * \param[in] gradient_type gradient type
181 * \param[in] halo_type halo type
182 * \param[in] inc if 0, solve on increment; 1 otherwise
183 * \param[in] n_r_sweeps if > 1, number of reconstruction sweeps
184 * (only used by CS_GRADIENT_GREEN_ITER)
185 * \param[in] verbosity verbosity level
186 * \param[in] clip_mode clipping mode
187 * \param[in] epsilon precision for iterative gradient calculation
188 * \param[in] clip_coeff clipping coefficient
189 * \param[in] bc_coeffs boundary condition structure
190 * \param[in, out] var gradient's base variable
191 * \param[in, out] c_weight cell variable weight, or nullptr
192 * \param[in] cpl associated internal coupling, or nullptr
193 * \param[out] gradv gradient
194 (\f$ \der{u_i}{x_j} \f$ is gradv[][i][j])
195 */
196/*----------------------------------------------------------------------------*/
197
198void
199cs_gradient_vector(const char *var_name,
200 cs_gradient_type_t gradient_type,
201 cs_halo_type_t halo_type,
202 int inc,
203 int n_r_sweeps,
204 int verbosity,
205 cs_gradient_limit_t clip_mode,
206 double epsilon,
207 double clip_coeff,
208 cs_field_bc_coeffs_t *bc_coeffs,
209 cs_real_t var[][3],
210 cs_real_t *c_weight,
211 const cs_internal_coupling_t *cpl,
212 cs_real_t gradv[][3][3]);
213
214/*----------------------------------------------------------------------------*/
215/*
216 * \brief Compute cell gradient of tensor.
217 *
218 * \param[in] var_name variable name
219 * \param[in] gradient_type gradient type
220 * \param[in] halo_type halo type
221 * \param[in] inc if 0, solve on increment; 1 otherwise
222 * \param[in] n_r_sweeps if > 1, number of reconstruction sweeps
223 * (only used by CS_GRADIENT_GREEN_ITER)
224 * \param[in] verbosity verbosity level
225 * \param[in] clip_mode clipping mode
226 * \param[in] epsilon precision for iterative gradient calculation
227 * \param[in] clip_coeff clipping coefficient
228 * \param[in] bc_coeffs boundary condition structure
229 * \param[in, out] var gradient's base variable
230 * \param[in] cpl associated internal coupling, or nullptr
231 * \param[out] grad gradient
232 (\f$ \der{t_ij}{x_k} \f$ is grad[][ij][k])
233 */
234/*----------------------------------------------------------------------------*/
235
236void
237cs_gradient_tensor(const char *var_name,
238 cs_gradient_type_t gradient_type,
239 cs_halo_type_t halo_type,
240 int inc,
241 int n_r_sweeps,
242 int verbosity,
243 cs_gradient_limit_t clip_mode,
244 double epsilon,
245 double clip_coeff,
246 cs_field_bc_coeffs_t *bc_coeffs,
247 cs_real_6_t *var,
248 const cs_internal_coupling_t *cpl,
249 cs_real_63_t *grad);
250
251/*----------------------------------------------------------------------------*/
252/*
253 * \brief Compute cell gradient of tensor.
254 *
255 * This variant of the \ref cs_gradient_tensor function assumes ghost cell
256 * values for input arrays (var and optionally c_weight)
257 * have already been synchronized.
258 *
259 * \param[in] var_name variable name
260 * \param[in] gradient_type gradient type
261 * \param[in] halo_type halo type
262 * \param[in] inc if 0, solve on increment; 1 otherwise
263 * \param[in] n_r_sweeps if > 1, number of reconstruction sweeps
264 * (only used by CS_GRADIENT_GREEN_ITER)
265 * \param[in] verbosity verbosity level
266 * \param[in] clip_mode clipping mode
267 * \param[in] epsilon precision for iterative gradient calculation
268 * \param[in] clip_coeff clipping coefficient
269 * \param[in] bc_coeffs boundary condition structure
270 * \param[in, out] var gradient's base variable
271 * \param[out] grad gradient
272 (\f$ \der{t_ij}{x_k} \f$ is grad[][ij][k])
273 * \param[out] bounds optional bounds (square distance)
274 * of values in adjacent cells and faces, or null
275 */
276/*----------------------------------------------------------------------------*/
277
278void
279cs_gradient_tensor_synced_input(const char *var_name,
280 cs_gradient_type_t gradient_type,
281 cs_halo_type_t halo_type,
282 int inc,
283 int n_r_sweeps,
284 int verbosity,
285 cs_gradient_limit_t clip_mode,
286 double epsilon,
287 double clip_coeff,
288 const cs_field_bc_coeffs_t *bc_coeffs,
289 const cs_real_t var[][6],
290 cs_real_63_t *grad,
291 cs_real_t *bounds);
292
293/*----------------------------------------------------------------------------*/
294/*
295 * \brief Compute the gradient of a scalar field at a given cell
296 * using least-squares reconstruction.
297 *
298 * This assumes ghost cell values which might be used are already
299 * synchronized.
300 *
301 * When boundary conditions are provided, both the bc_coeff_a and bc_coeff_b
302 * arrays must be given. If boundary values are known, bc_coeff_a
303 * can point to the boundary values array, and bc_coeff_b set to nullptr.
304 * If bc_coeff_a is nullptr, bc_coeff_b is ignored.
305 *
306 * \param[in] m pointer to associated mesh structure
307 * \param[in] fvq pointer to associated finite volume quantities
308 * \param[in] c_id cell id
309 * \param[in] halo_type halo type
310 * \param[in] bc_coeffs boundary condition structure
311 * \param[in] var gradient's base variable
312 * \param[in] c_weight cell variable weight, or nullptr
313 * \param[out] grad gradient
314 */
315/*----------------------------------------------------------------------------*/
316
317void
319 const cs_mesh_quantities_t *fvq,
320 cs_lnum_t c_id,
321 cs_halo_type_t halo_type,
322 cs_field_bc_coeffs_t *bc_coeffs,
323 const cs_real_t var[],
324 const cs_real_t c_weight[],
325 cs_real_t grad[3]);
326
327/*----------------------------------------------------------------------------*/
328/*
329 * \brief Compute the gradient of a vector field at a given cell
330 * using least-squares reconstruction.
331 *
332 * This assumes ghost cell values which might be used are already
333 * synchronized.
334 *
335 * When boundary conditions are provided, both the bc_coeff_a and bc_coeff_b
336 * arrays must be given. If boundary values are known, bc_coeff_a
337 * can point to the boundary values array, and bc_coeff_b set to nullptr.
338 * If bc_coeff_a is nullptr, bc_coeff_b is ignored.
339 *
340 * \param[in] m pointer to associated mesh structure
341 * \param[in] fvq pointer to associated finite volume quantities
342 * \param[in] c_id cell id
343 * \param[in] halo_type halo type
344 * \param[in] bc_coeffs boundary condition structure
345 * \param[in] var gradient's base variable
346 * \param[in] c_weight cell variable weight, or nullptr
347 * \param[out] grad gradient
348 */
349/*----------------------------------------------------------------------------*/
350
351void
353 const cs_mesh_quantities_t *fvq,
354 cs_lnum_t c_id,
355 cs_halo_type_t halo_type,
356 cs_field_bc_coeffs_t *bc_coeffs,
357 const cs_real_t var[][3],
358 const cs_real_t c_weight[],
359 cs_real_t grad[3][3]);
360
361/*----------------------------------------------------------------------------*/
362/*
363 * \brief Compute the gradient of a tensor field at a given cell
364 * using least-squares reconstruction.
365 *
366 * This assumes ghost cell values which might be used are already
367 * synchronized.
368 *
369 * When boundary conditions are provided, both the bc_coeff_a and bc_coeff_b
370 * arrays must be given. If boundary values are known, bc_coeff_a
371 * can point to the boundary values array, and bc_coeff_b set to nullptr.
372 * If bc_coeff_a is nullptr, bc_coeff_b is ignored.
373 *
374 * \param[in] m pointer to associated mesh structure
375 * \param[in] fvq pointer to associated finite volume quantities
376 * \param[in] c_id cell id
377 * \param[in] halo_type halo type
378 * \param[in] bc_coeffs boundary condition structure
379 * \param[in] var gradient's base variable
380 * \param[in] c_weight cell variable weight, or nullptr
381 * \param[out] grad gradient
382 */
383/*----------------------------------------------------------------------------*/
384
385void
387 const cs_mesh_quantities_t *fvq,
388 cs_lnum_t c_id,
389 cs_halo_type_t halo_type,
390 cs_field_bc_coeffs_t *bc_coeffs,
391 const cs_real_t var[][6],
392 const cs_real_t c_weight[],
393 cs_real_t grad[6][3]);
394
395/*----------------------------------------------------------------------------
396 * Determine gradient type by Fortran "imrgra" value
397 *
398 * parameters:
399 * imrgra <-- Fortran gradient option
400 * gradient_type --> gradient type
401 * halo_type --> halo type
402 *----------------------------------------------------------------------------*/
403
404void
406 cs_gradient_type_t *gradient_type,
407 cs_halo_type_t *halo_type);
408
409/*----------------------------------------------------------------------------*/
410/*
411 * \brief Determine halo type based on associated type by legacy "imrgra" value
412 *
413 * \param[in] imrgra gradient option numerical code
414 *
415 * \return associated gradient halo type
416 */
417/*----------------------------------------------------------------------------*/
418
421
422/*----------------------------------------------------------------------------*/
429/*----------------------------------------------------------------------------*/
430
431void
433
434/*----------------------------------------------------------------------------*/
435/*
436 * \brief Compute cell gradient of scalar field or component of vector or
437 * tensor field.
438 *
439 * This variant of the \ref cs_gradient_scalar function assumes ghost cell
440 * values for input arrays (var and optionally c_weight)
441 * have already been synchronized.
442 *
443 * \param[in] var_name variable name
444 * \param[in] gradient_type gradient type
445 * \param[in] halo_type halo type
446 * \param[in] inc if 0, solve on increment; 1 otherwise
447 * \param[in] n_r_sweeps if > 1, number of reconstruction sweeps
448 * (only used by CS_GRADIENT_GREEN_ITER)
449 * \param[in] hyd_p_flag flag for hydrostatic pressure
450 * \param[in] w_stride stride for weighting coefficient
451 * \param[in] verbosity verbosity level
452 * \param[in] clip_mode clipping mode
453 * \param[in] epsilon precision for iterative gradient calculation
454 * \param[in] clip_coeff clipping coefficient
455 * \param[in] f_ext exterior force generating the
456 * hydrostatic pressure
457 * \param[in] bc_coeffs boundary condition structure
458 * \param[in] var gradient's base variable
459 * \param[in] c_weight cell variable weight, or nullptr
460 * \param[out] grad gradient
461 * \param[out] bounds optional minima and maxima of values in
462 * adjacent cells and faces, or null
463 */
464/*----------------------------------------------------------------------------*/
465
466template <typename T>
467void
469 cs_gradient_type_t gradient_type,
470 cs_halo_type_t halo_type,
471 int inc,
472 int n_r_sweeps,
473 int hyd_p_flag,
474 int w_stride,
475 int verbosity,
476 cs_gradient_limit_t clip_mode,
477 double epsilon,
478 double clip_coeff,
479 cs_real_t f_ext[][3],
480 const cs_field_bc_coeffs_t *bc_coeffs,
481 const cs_real_t var[],
482 const cs_real_t *c_weight,
483 T grad[][3],
484 cs_real_t (*bounds)[2]);
485
486/*----------------------------------------------------------------------------*/
487/*
488 * \brief Compute cell gradient of vector field.
489 *
490 * This variant of the \ref cs_gradient_vector function assumes ghost cell
491 * values for input arrays (var and optionally c_weight)
492 * have already been synchronized.
493 *
494 * \param[in] var_name variable name
495 * \param[in] gradient_type gradient type
496 * \param[in] halo_type halo type
497 * \param[in] inc if 0, solve on increment; 1 otherwise
498 * \param[in] n_r_sweeps if > 1, number of reconstruction sweeps
499 * (only used by CS_GRADIENT_GREEN_ITER)
500 * \param[in] verbosity verbosity level
501 * \param[in] clip_mode clipping mode
502 * \param[in] epsilon precision for iterative gradient calculation
503 * \param[in] clip_coeff clipping coefficient
504 * \param[in] bc_coeffs boundary condition structure
505 * \param[in] var gradient's base variable
506 * \param[in] c_weight cell variable weight, or nullptr
507 * \param[out] grad gradient
508 * (\f$ \der{u_i}{x_j} \f$ is gradv[][i][j])
509 * \param[out] bounds optional bounds (square distance)
510 * of values in adjacent cells and faces, or null
511 */
512/*----------------------------------------------------------------------------*/
513
514template <typename T>
515void
516cs_gradient_vector_synced_input(const char *var_name,
517 cs_gradient_type_t gradient_type,
518 cs_halo_type_t halo_type,
519 int inc,
520 int n_r_sweeps,
521 int verbosity,
522 cs_gradient_limit_t clip_mode,
523 double epsilon,
524 double clip_coeff,
525 const cs_field_bc_coeffs_t *bc_coeffs,
526 const cs_real_t var[][3],
527 const cs_real_t c_weight[],
528 T grad[][3][3],
529 cs_real_t *bounds);
530
531/*----------------------------------------------------------------------------*/
532
533#endif /* CS_GRADIENT__ */
Field boundary condition descriptor (for variables)
Definition: cs_field.h:107
double cs_real_t
Floating-point value.
Definition: cs_defs.h:332
cs_real_t cs_real_3_t[3]
vector of 3 floating-point values
Definition: cs_defs.h:349
cs_real_t cs_real_6_t[6]
vector of 6 floating-point values
Definition: cs_defs.h:351
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:325
cs_real_t cs_real_63_t[6][3]
Definition: cs_defs.h:366
void cs_gradient_vector_cell(const cs_mesh_t *m, const cs_mesh_quantities_t *fvq, cs_lnum_t c_id, cs_halo_type_t halo_type, cs_field_bc_coeffs_t *bc_coeffs, const cs_real_t var[][3], const cs_real_t c_weight[], cs_real_t grad[3][3])
Compute the gradient of a vector field at a given cell using least-squares reconstruction.
Definition: cs_gradient.cpp:10244
void cs_gradient_type_by_imrgra(int imrgra, cs_gradient_type_t *gradient_type, cs_halo_type_t *halo_type)
Definition: cs_gradient.cpp:10317
void cs_gradient_scalar_synced_input(const char *var_name, cs_gradient_type_t gradient_type, cs_halo_type_t halo_type, int inc, int n_r_sweeps, int hyd_p_flag, int w_stride, int verbosity, cs_gradient_limit_t clip_mode, double epsilon, double clip_coeff, cs_real_t f_ext[][3], const cs_field_bc_coeffs_t *bc_coeffs, const cs_real_t var[], const cs_real_t *c_weight, T grad[][3], cs_real_t(*bounds)[2])
cs_gradient_type_t
Definition: cs_gradient.h:57
@ CS_GRADIENT_LSQ
Definition: cs_gradient.h:60
@ CS_GRADIENT_GREEN_LSQ
Definition: cs_gradient.h:61
@ CS_GRADIENT_GREEN_R
Definition: cs_gradient.h:65
@ CS_GRADIENT_GREEN_ITER
Definition: cs_gradient.h:59
@ CS_GRADIENT_GREEN_VTX
Definition: cs_gradient.h:63
const char * cs_gradient_type_name[]
cs_halo_type_t cs_gradient_halo_type_by_imrgra(int imrgra)
Determine halo type based on associated type by legacy "imrgra" value.
Definition: cs_gradient.cpp:10369
cs_gradient_limit_t
Definition: cs_gradient.h:73
@ CS_GRADIENT_LIMIT_CELL_RC
Definition: cs_gradient.h:83
@ CS_GRADIENT_LIMIT_CELL
Definition: cs_gradient.h:76
@ CS_GRADIENT_LIMIT_NONE
Definition: cs_gradient.h:75
@ CS_GRADIENT_LIMIT_FACE
Definition: cs_gradient.h:79
void cs_gradient_porosity_balance(int inc)
compute the steady balance due to porous modelling for the pressure gradient.
Definition: cs_gradient.cpp:10401
void cs_gradient_tensor_synced_input(const char *var_name, cs_gradient_type_t gradient_type, cs_halo_type_t halo_type, int inc, int n_r_sweeps, int verbosity, cs_gradient_limit_t clip_mode, double epsilon, double clip_coeff, const cs_field_bc_coeffs_t *bc_coeffs, const cs_real_t var[][6], cs_real_63_t *grad, cs_real_t *bounds)
Compute cell gradient of tensor.
Definition: cs_gradient.cpp:9840
void cs_gradient_vector(const char *var_name, cs_gradient_type_t gradient_type, cs_halo_type_t halo_type, int inc, int n_r_sweeps, int verbosity, cs_gradient_limit_t clip_mode, double epsilon, double clip_coeff, cs_field_bc_coeffs_t *bc_coeffs, cs_real_t var[][3], cs_real_t *c_weight, const cs_internal_coupling_t *cpl, cs_real_t gradv[][3][3])
void cs_gradient_scalar_cell(const cs_mesh_t *m, const cs_mesh_quantities_t *fvq, cs_lnum_t c_id, cs_halo_type_t halo_type, cs_field_bc_coeffs_t *bc_coeffs, const cs_real_t var[], const cs_real_t c_weight[], cs_real_t grad[3])
Compute the gradient of a scalar field at a given cell using least-squares reconstruction.
Definition: cs_gradient.cpp:9992
void cs_gradient_vector_synced_input(const char *var_name, cs_gradient_type_t gradient_type, cs_halo_type_t halo_type, int inc, int n_r_sweeps, int verbosity, cs_gradient_limit_t clip_mode, double epsilon, double clip_coeff, const cs_field_bc_coeffs_t *bc_coeffs, const cs_real_t var[][3], const cs_real_t c_weight[], T grad[][3][3], cs_real_t *bounds)
Compute cell gradient of vector field.
Definition: cs_gradient.cpp:9754
void cs_gradient_tensor_cell(const cs_mesh_t *m, const cs_mesh_quantities_t *fvq, cs_lnum_t c_id, cs_halo_type_t halo_type, cs_field_bc_coeffs_t *bc_coeffs, const cs_real_t var[][6], const cs_real_t c_weight[], cs_real_t grad[6][3])
Compute the gradient of a tensor field at a given cell using least-squares reconstruction.
Definition: cs_gradient.cpp:10288
void cs_gradient_scalar(const char *var_name, cs_gradient_type_t gradient_type, cs_halo_type_t halo_type, int inc, int n_r_sweeps, int hyd_p_flag, int w_stride, int verbosity, cs_gradient_limit_t clip_mode, double epsilon, double clip_coeff, cs_real_3_t f_ext[], cs_field_bc_coeffs_t *bc_coeffs, cs_real_t var[], cs_real_t *c_weight, const cs_internal_coupling_t *cpl, cs_real_t grad[][3])
void cs_gradient_finalize(void)
Finalize gradient computation API.
Definition: cs_gradient.cpp:8768
void cs_gradient_initialize(void)
Initialize gradient computation API.
Definition: cs_gradient.cpp:8747
void cs_gradient_free_quantities(void)
Free saved gradient quantities.
Definition: cs_gradient.cpp:8803
void cs_gradient_tensor(const char *var_name, cs_gradient_type_t gradient_type, cs_halo_type_t halo_type, int inc, int n_r_sweeps, int verbosity, cs_gradient_limit_t clip_mode, double epsilon, double clip_coeff, cs_field_bc_coeffs_t *bc_coeffs, cs_real_6_t *var, const cs_internal_coupling_t *cpl, cs_real_63_t *grad)
cs_halo_type_t
Definition: cs_halo.h:53
Definition: cs_internal_coupling.h:60
Definition: cs_mesh_quantities.h:88
Definition: cs_mesh.h:85