9.2
general documentation
cs_gradient_priv.h
Go to the documentation of this file.
1#pragma once
2
3/*============================================================================
4 * Private functions for gradient reconstruction.
5 *============================================================================*/
6
7/*
8 This file is part of code_saturne, a general-purpose CFD tool.
9
10 Copyright (C) 1998-2026 EDF S.A.
11
12 This program is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free Software
14 Foundation; either version 2 of the License, or (at your option) any later
15 version.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
24 Street, Fifth Floor, Boston, MA 02110-1301, USA.
25*/
26
27/*----------------------------------------------------------------------------*/
28
29/*----------------------------------------------------------------------------
30 * Local headers
31 *----------------------------------------------------------------------------*/
32
33#include "base/cs_base.h"
34#include "base/cs_base_accel.h"
35#include "base/cs_halo_perio.h"
38
39/*----------------------------------------------------------------------------*/
40
43/*============================================================================
44 * Macro definitions
45 *============================================================================*/
46
47/*=============================================================================
48 * Local type definitions
49 *============================================================================*/
50
51/* Type for symmetric least-squares covariance matrices
52 as they are adimensional, single-precision should be usable here */
53
54typedef float cs_cocg_t;
55typedef float cs_cocg_6_t[6];
56typedef float cs_cocg_33_t[3][3];
57
58/*============================================================================
59 * Global variables
60 *============================================================================*/
61
62/*=============================================================================
63 * Semi-private inline functions
64 *============================================================================*/
65
66#if defined(HAVE_ACCEL)
67
68/*----------------------------------------------------------------------------
69 * Synchronize strided gradient ghost cell values on accelerator device.
70 *
71 * template parameters:
72 * stride 1 for scalars, 3 for vectors, 6 for symmetric tensors
73 *
74 * parameters:
75 * m <-- pointer to associated mesh structure
76 * halo_type <-- halo type (extended or not)
77 * grad --> gradient of a variable
78 *----------------------------------------------------------------------------*/
79
80template <typename T>
81inline static void
82cs_sync_scalar_gradient_halo_d(const cs_mesh_t *m,
83 cs_halo_type_t halo_type,
84 T (*restrict grad)[3])
85{
86 if (m->halo != nullptr) {
87
88 cs_datatype_t datatype = cs_datatype_from_type<T>();
89 cs_halo_sync_d(m->halo, halo_type, datatype, 3, (T *)grad);
90
91 if (m->have_rotation_perio) {
92 cs_sync_d2h((void *)grad);
93 cs_halo_perio_sync_var_vect(m->halo, halo_type, (T *)grad, 3);
94 cs_sync_h2d((void *)grad);
95 }
96 }
97}
98
99/*----------------------------------------------------------------------------
100 * Synchronize strided gradient ghost cell values on accelerator device.
101 *
102 * template parameters:
103 * stride 1 for scalars, 3 for vectors, 6 for symmetric tensors
104 *
105 * parameters:
106 * m <-- pointer to associated mesh structure
107 * halo_type <-- halo type (extended or not)
108 * grad --> gradient of a variable
109 *----------------------------------------------------------------------------*/
110
111template <cs_lnum_t stride, typename T>
112static void
113cs_sync_strided_gradient_halo_d(const cs_mesh_t *m,
114 cs_halo_type_t halo_type,
115 T (*restrict grad)[stride][3])
116{
117 if (m->halo != nullptr) {
118
119 cs_datatype_t datatype = cs_datatype_from_type<T>();
120
121 cs_halo_sync_d(m->halo, halo_type, datatype, stride*3, (T *)grad);
122
123 if (m->have_rotation_perio) {
124 cs_sync_d2h((void *)grad);
125 if (stride == 1)
126 cs_halo_perio_sync_var_vect(m->halo, halo_type, (T *)grad, 3);
127 else if (stride == 3)
128 cs_halo_perio_sync_var_tens(m->halo, halo_type, (T *)grad);
129 else if (stride == 6)
131 halo_type,
132 (T *)grad);
133 cs_sync_h2d((void *)grad);
134 }
135 }
136}
137
138#endif /* defined(HAVE_ACCEL) */
139
140/*=============================================================================
141 * Semi-private function prototypes
142 *============================================================================*/
143
144/*----------------------------------------------------------------------------
145 * Return current symmetric 3x3 matrix cocg for least squares algorithm
146 *
147 * parameters:
148 * m <-- mesh
149 * halo_type <-- halo type
150 * accel <-- use accelerator device (if true, cocg and cocgb
151 * pointers returned are device pointers)
152 * fvq <-- mesh quantities
153 *
154 * return:
155 * pointer to cocg coupling coefficients (covariance matrices)
156 *----------------------------------------------------------------------------*/
157
158cs_cocg_6_t *
159cs_gradient_get_cell_cocg_lsq(const cs_mesh_t *m,
160 cs_halo_type_t halo_type,
161 bool accel,
162 const cs_mesh_quantities_t *fvq);
163
164#if defined(HAVE_CUDA)
165
166/*----------------------------------------------------------------------------
167 * Compute cell gradient using least-squares reconstruction for non-orthogonal
168 * meshes (nswrgp > 1).
169 *
170 * Optionally, a volume force generating a hydrostatic pressure component
171 * may be accounted for.
172 *
173 * cocg is computed to account for variable B.C.'s (flux).
174 *
175 * parameters:
176 * m <-- pointer to associated mesh structure
177 * fvq <-- pointer to associated finite volume quantities
178 * halo_type <-- halo type (extended or not)
179 * val_f <-- face value for gradient
180 * pvar <-- variable
181 * c_weight <-- weighted gradient coefficient variable,
182 * or NULL
183 * cocg <-> associated cell covariance array (on device)
184 * grad <-> gradient of pvar (halo prepared for periodicity
185 * of rotation)
186 *----------------------------------------------------------------------------*/
187
188template <typename T>
189void
190cs_gradient_scalar_lsq_cuda(const cs_mesh_t *m,
191 const cs_mesh_quantities_t *fvq,
192 cs_halo_type_t halo_type,
193 const cs_real_t val_f[],
194 const cs_real_t pvar[],
195 const cs_real_t *restrict c_weight,
196 cs_cocg_6_t *restrict cocg,
197 T (*restrict grad)[3]);
198
199/*----------------------------------------------------------------------------
200 * Compute cell gradient of a vector or tensor using least-squares
201 * reconstruction for non-orthogonal meshes.
202 *
203 * template parameters:
204 * e2n type of assembly algorithm used
205 * stride 3 for vectors, 6 for symmetric tensors
206 *
207 * parameters:
208 * m <-- pointer to associated mesh structure
209 * madj <-- pointer to mesh adjacencies structure
210 * fvq <-- pointer to associated finite volume quantities
211 * halo_type <-- halo type (extended or not)
212 * val_f <-- face value for gradient
213 * pvar <-- variable
214 * c_weight <-- weighted gradient coefficient variable, or NULL
215 * cocg <-> cocg covariance matrix for given cell
216 * grad --> gradient of pvar (du_i/dx_j : grad[][i][j])
217 *----------------------------------------------------------------------------*/
218
219template <cs_lnum_t stride, typename T>
220void
221cs_gradient_strided_lsq_cuda
222(
223 const cs_mesh_t *m,
224 const cs_mesh_adjacencies_t *madj,
225 const cs_mesh_quantities_t *fvq,
226 const cs_halo_type_t halo_type,
227 const cs_real_t val_f[][stride],
228 const cs_real_t pvar[][stride],
229 const cs_real_t *c_weight,
230 cs_cocg_6_t *cocg,
231 T grad[][stride][3]
232);
233
234/*----------------------------------------------------------------------------
235 * Green-Gauss reconstruction of the gradient of a vector or tensor using
236 * an initial gradient of this quantity (typically lsq).
237 *
238 * parameters:
239 * m <-- pointer to associated mesh structure
240 * madj <-- pointer to mesh adjacencies structure
241 * fvq <-- pointer to associated finite volume quantities
242 * halo_type <-- halo type (extended or not)
243 * warped_correction <-- apply warped faces correction ?
244 * val_f <-- face value for gradient
245 * pvar <-- variable
246 * c_weight <-- weighted gradient coefficient variable
247 * r_grad <-- gradient used for reconstruction
248 * grad --> gradient of pvar (du_i/dx_j : grad[][i][j])
249 *----------------------------------------------------------------------------*/
250
251template <cs_lnum_t stride, typename T>
252void
253cs_gradient_strided_gg_r_cuda
254(
255 const cs_mesh_t *m,
256 const cs_mesh_adjacencies_t *madj,
257 const cs_mesh_quantities_t *fvq,
258 cs_halo_type_t halo_type,
259 bool warped_correction,
260 const cs_real_t val_f[][stride],
261 const cs_real_t pvar[][stride],
262 const cs_real_t *c_weight,
263 const T r_grad[][stride][3],
264 T grad[][stride][3]
265);
266
267/*----------------------------------------------------------------------------
268 * Green-Gauss reconstruction of the gradient of a vector or tensor using
269 * an initial gradient of this quantity (typically lsq).
270 *
271 * parameters:
272 * m <-- pointer to associated mesh structure
273 * fvq <-- pointer to associated finite volume quantities
274 * cpl <-- structure associated with internal coupling, or NULL
275 * inc <-- if 0, solve on increment; 1 otherwise
276 * warped_correction <-- apply warped faces correction ?
277 * coefav <-- B.C. coefficients for boundary face normals
278 * coefbv <-- B.C. coefficients for boundary face normals
279 * pvar <-- variable
280 * c_weight <-- weighted gradient coefficient variable
281 * r_grad <-- gradient used for reconstruction
282 * grad --> gradient of pvar (du_i/dx_j : grad[][i][j])
283 *----------------------------------------------------------------------------*/
284
285template <cs_lnum_t stride>
286void
287cs_gradient_strided_gg_r_cuda
288(
289 const cs_mesh_t *m,
290 const cs_mesh_adjacencies_t *madj,
291 const cs_mesh_quantities_t *fvq,
292 cs_halo_type_t halo_type,
293 int inc,
294 bool warped_correction,
295 const cs_real_t coefav[][stride],
296 const cs_real_t coefbv[][stride][stride],
297 const cs_real_t pvar[][stride],
298 const cs_real_t *c_weight,
299 const cs_real_t r_grad[][stride][3],
300 cs_real_t grad[][stride][3]
301);
302
303#endif /* defined(HAVE_CUDA) */
304
305#if defined(HAVE_HIP)
306
307/*----------------------------------------------------------------------------
308 * Compute cell gradient using least-squares reconstruction for non-orthogonal
309 * meshes (nswrgp > 1).
310 *
311 * Optionally, a volume force generating a hydrostatic pressure component
312 * may be accounted for.
313 *
314 * cocg is computed to account for variable B.C.'s (flux).
315 *
316 * parameters:
317 * m <-- pointer to associated mesh structure
318 * fvq <-- pointer to associated finite volume quantities
319 * halo_type <-- halo type (extended or not)
320 * recompute_cocg <-- flag to recompute cocg
321 * val_f <-- face value for gradient
322 * pvar <-- variable
323 * c_weight <-- weighted gradient coefficient variable,
324 * or NULL
325 * cocg <-> associated cell covariance array (on device)
326 * grad <-> gradient of pvar (halo prepared for periodicity
327 * of rotation)
328 *----------------------------------------------------------------------------*/
329
330void
332 const cs_mesh_quantities_t *fvq,
333 cs_halo_type_t halo_type,
334 const cs_real_t val_f[],
335 const cs_real_t *pvar,
336 const cs_real_t *restrict c_weight,
337 cs_cocg_6_t *restrict cocg,
338 cs_real_3_t *restrict grad);
339
340/*----------------------------------------------------------------------------
341 * Compute cell gradient of a vector or tensor using least-squares
342 * reconstruction for non-orthogonal meshes.
343 *
344 * template parameters:
345 * e2n type of assembly algorithm used
346 * stride 3 for vectors, 6 for symmetric tensors
347 *
348 * parameters:
349 * m <-- pointer to associated mesh structure
350 * madj <-- pointer to mesh adjacencies structure
351 * fvq <-- pointer to associated finite volume quantities
352 * halo_type <-- halo type (extended or not)
353 * val_f <-- face value for gradient
354 * pvar <-- variable
355 * c_weight <-- weighted gradient coefficient variable, or NULL
356 * cocg <-> cocg covariance matrix for given cell
357 * grad --> gradient of pvar (du_i/dx_j : grad[][i][j])
358 *----------------------------------------------------------------------------*/
359
360template <cs_lnum_t stride>
361void
363(
364 const cs_mesh_t *m,
365 const cs_mesh_adjacencies_t *madj,
366 const cs_mesh_quantities_t *fvq,
367 const cs_halo_type_t halo_type,
368 const cs_real_t val_f[][stride],
369 const cs_real_t pvar[][stride],
370 const cs_real_t *c_weight,
371 cs_cocg_6_t *cocg,
372 cs_real_t grad[][stride][3]
373);
374
375/*----------------------------------------------------------------------------
376 * Green-Gauss reconstruction of the gradient of a vector or tensor using
377 * an initial gradient of this quantity (typically lsq).
378 *
379 * parameters:
380 * m <-- pointer to associated mesh structure
381 * madj <-- pointer to mesh adjacencies structure
382 * fvq <-- pointer to associated finite volume quantities
383 * halo_type <-- halo type (extended or not)
384 * warped_correction <-- apply warped faces correction ?
385 * val_f <-- face value for gradient
386 * pvar <-- variable
387 * c_weight <-- weighted gradient coefficient variable
388 * r_grad <-- gradient used for reconstruction
389 * grad --> gradient of pvar (du_i/dx_j : grad[][i][j])
390 *----------------------------------------------------------------------------*/
391
392template <cs_lnum_t stride>
393void
395(
396 const cs_mesh_t *m,
397 const cs_mesh_adjacencies_t *madj,
398 const cs_mesh_quantities_t *fvq,
399 cs_halo_type_t halo_type,
400 bool warped_correction,
401 const cs_real_t val_f[][stride],
402 const cs_real_t pvar[][stride],
403 const cs_real_t *c_weight,
404 const cs_real_t r_grad[][stride][3],
405 cs_real_t grad[][stride][3]
406);
407
408#endif /* defined(HAVE_HIP) */
409
412/*----------------------------------------------------------------------------*/
cs_datatype_t
Definition: cs_defs.h:290
#define restrict
Definition: cs_defs.h:148
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
void cs_gradient_strided_lsq_hip(const cs_mesh_t *m, const cs_mesh_adjacencies_t *madj, const cs_mesh_quantities_t *fvq, const cs_halo_type_t halo_type, const cs_real_t val_f[][stride], const cs_real_t pvar[][stride], const cs_real_t *c_weight, cs_cocg_6_t *cocg, cs_real_t grad[][stride][3])
Definition: cs_gradient_hip.cpp:1086
void cs_gradient_strided_gg_r_hip(const cs_mesh_t *m, const cs_mesh_adjacencies_t *madj, const cs_mesh_quantities_t *fvq, cs_halo_type_t halo_type, bool warped_correction, const cs_real_t val_f[][stride], const cs_real_t pvar[][stride], const cs_real_t *c_weight, const cs_real_t r_grad[][stride][3], cs_real_t grad[][stride][3])
Definition: cs_gradient_hip.cpp:1381
void cs_gradient_scalar_lsq_hip(const cs_mesh_t *m, const cs_mesh_quantities_t *fvq, cs_halo_type_t halo_type, const cs_real_t val_f[], const cs_real_t *pvar, const cs_real_t *restrict c_weight, cs_cocg_6_t *restrict cocg, cs_real_3_t *restrict grad)
Definition: cs_gradient_hip.cpp:915
cs_halo_type_t
Definition: cs_halo.h:53
void cs_halo_perio_sync_var_vect(const cs_halo_t *halo, cs_halo_type_t sync_mode, T var[], int incvar)
Definition: cs_halo_perio.cpp:385
void cs_halo_perio_sync_var_sym_tens_grad(const cs_halo_t *halo, cs_halo_type_t sync_mode, T var[])
Definition: cs_halo_perio.cpp:624
void cs_halo_perio_sync_var_tens(const cs_halo_t *halo, cs_halo_type_t sync_mode, T var[])
Definition: cs_halo_perio.cpp:471
void cs_sync_h2d(const void *ptr)
Synchronize data from host to device.
Definition: cs_mem.h:935
void cs_sync_d2h(void *ptr)
Synchronize data from device to host.
Definition: cs_mem.h:1000
Definition: cs_mesh_adjacencies.h:82
Definition: cs_mesh_quantities.h:88
Definition: cs_mesh.h:85
int have_rotation_perio
Definition: cs_mesh.h:143
cs_halo_t * halo
Definition: cs_mesh.h:156