9.1
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-2025 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 cs_real_t cs_cocg_t;
55typedef cs_real_t cs_cocg_6_t[6];
56typedef cs_real_t cs_cocg_33_t[3][3];
57
58/*============================================================================
59 * Global variables
60 *============================================================================*/
61
62/*=============================================================================
63 * Semi-private inline functions
64 *============================================================================*/
65
66#if defined(__cplusplus)
67
68#if defined(HAVE_ACCEL)
69
70/*----------------------------------------------------------------------------
71 * Synchronize strided gradient ghost cell values on accelerator device.
72 *
73 * template parameters:
74 * stride 1 for scalars, 3 for vectors, 6 for symmetric tensors
75 *
76 * parameters:
77 * m <-- pointer to associated mesh structure
78 * halo_type <-- halo type (extended or not)
79 * grad --> gradient of a variable
80 *----------------------------------------------------------------------------*/
81
82inline static void
83cs_sync_scalar_gradient_halo_d(const cs_mesh_t *m,
84 cs_halo_type_t halo_type,
85 cs_real_t (*restrict grad)[3])
86{
87 if (m->halo != NULL) {
88 cs_halo_sync_d(m->halo, halo_type, CS_REAL_TYPE, 3, (cs_real_t *)grad);
89
90 if (m->have_rotation_perio) {
91 cs_sync_d2h((void *)grad);
92 cs_halo_perio_sync_var_vect(m->halo, halo_type, (cs_real_t *)grad, 3);
93 cs_sync_h2d((void *)grad);
94 }
95 }
96}
97
98/*----------------------------------------------------------------------------
99 * Synchronize strided gradient ghost cell values on accelerator device.
100 *
101 * template parameters:
102 * stride 1 for scalars, 3 for vectors, 6 for symmetric tensors
103 *
104 * parameters:
105 * m <-- pointer to associated mesh structure
106 * halo_type <-- halo type (extended or not)
107 * grad --> gradient of a variable
108 *----------------------------------------------------------------------------*/
109
110template <cs_lnum_t stride>
111static void
112cs_sync_strided_gradient_halo_d(const cs_mesh_t *m,
113 cs_halo_type_t halo_type,
114 cs_real_t (*restrict grad)[stride][3])
115{
116 if (m->halo != NULL) {
117 cs_halo_sync_d(m->halo, halo_type, CS_REAL_TYPE, stride*3,
118 (cs_real_t *)grad);
119
120 if (m->have_rotation_perio) {
121 cs_sync_d2h((void *)grad);
122 if (stride == 1)
123 cs_halo_perio_sync_var_vect(m->halo, halo_type, (cs_real_t *)grad, 3);
124 else if (stride == 3)
125 cs_halo_perio_sync_var_tens(m->halo, halo_type, (cs_real_t *)grad);
126 else if (stride == 6)
128 halo_type,
129 (cs_real_t *)grad);
130 cs_sync_h2d((void *)grad);
131 }
132 }
133}
134
135#endif /* defined(HAVE_ACCEL) */
136
137/*=============================================================================
138 * Semi-private function prototypes
139 *============================================================================*/
140
141#if defined(HAVE_CUDA)
142
143/*----------------------------------------------------------------------------
144 * Compute cell gradient using least-squares reconstruction for non-orthogonal
145 * meshes (nswrgp > 1).
146 *
147 * Optionally, a volume force generating a hydrostatic pressure component
148 * may be accounted for.
149 *
150 * cocg is computed to account for variable B.C.'s (flux).
151 *
152 * parameters:
153 * m <-- pointer to associated mesh structure
154 * fvq <-- pointer to associated finite volume quantities
155 * halo_type <-- halo type (extended or not)
156 * val_f <-- face value for gradient
157 * pvar <-- variable
158 * c_weight <-- weighted gradient coefficient variable,
159 * or NULL
160 * cocg <-> associated cell covariance array (on device)
161 * grad <-> gradient of pvar (halo prepared for periodicity
162 * of rotation)
163 *----------------------------------------------------------------------------*/
164
165void
166cs_gradient_scalar_lsq_cuda(const cs_mesh_t *m,
167 const cs_mesh_quantities_t *fvq,
168 cs_halo_type_t halo_type,
169 const cs_real_t val_f[],
170 const cs_real_t pvar[],
171 const cs_real_t *restrict c_weight,
172 cs_cocg_6_t *restrict cocg,
173 cs_real_3_t *restrict grad);
174
175/*----------------------------------------------------------------------------
176 * Compute cell gradient of a vector or tensor using least-squares
177 * reconstruction for non-orthogonal meshes.
178 *
179 * template parameters:
180 * e2n type of assembly algorithm used
181 * stride 3 for vectors, 6 for symmetric tensors
182 *
183 * parameters:
184 * m <-- pointer to associated mesh structure
185 * madj <-- pointer to mesh adjacencies structure
186 * fvq <-- pointer to associated finite volume quantities
187 * halo_type <-- halo type (extended or not)
188 * val_f <-- face value for gradient
189 * pvar <-- variable
190 * c_weight <-- weighted gradient coefficient variable, or NULL
191 * cocg <-> cocg covariance matrix for given cell
192 * grad --> gradient of pvar (du_i/dx_j : grad[][i][j])
193 *----------------------------------------------------------------------------*/
194
195template <cs_lnum_t stride>
196void
197cs_gradient_strided_lsq_cuda
198(
199 const cs_mesh_t *m,
200 const cs_mesh_adjacencies_t *madj,
201 const cs_mesh_quantities_t *fvq,
202 const cs_halo_type_t halo_type,
203 const cs_real_t val_f[][stride],
204 const cs_real_t pvar[][stride],
205 const cs_real_t *c_weight,
206 cs_cocg_6_t *cocg,
207 cs_real_t grad[][stride][3]
208);
209
210/*----------------------------------------------------------------------------
211 * Green-Gauss reconstruction of the gradient of a vector or tensor using
212 * an initial gradient of this quantity (typically lsq).
213 *
214 * parameters:
215 * m <-- pointer to associated mesh structure
216 * madj <-- pointer to mesh adjacencies structure
217 * fvq <-- pointer to associated finite volume quantities
218 * halo_type <-- halo type (extended or not)
219 * warped_correction <-- apply warped faces correction ?
220 * val_f <-- face value for gradient
221 * pvar <-- variable
222 * c_weight <-- weighted gradient coefficient variable
223 * r_grad <-- gradient used for reconstruction
224 * grad --> gradient of pvar (du_i/dx_j : grad[][i][j])
225 *----------------------------------------------------------------------------*/
226
227template <cs_lnum_t stride>
228void
229cs_gradient_strided_gg_r_cuda
230(
231 const cs_mesh_t *m,
232 const cs_mesh_adjacencies_t *madj,
233 const cs_mesh_quantities_t *fvq,
234 cs_halo_type_t halo_type,
235 bool warped_correction,
236 const cs_real_t val_f[][stride],
237 const cs_real_t pvar[][stride],
238 const cs_real_t *c_weight,
239 const cs_real_t r_grad[][stride][3],
240 cs_real_t grad[][stride][3]
241);
242
243/*----------------------------------------------------------------------------
244 * Green-Gauss reconstruction of the gradient of a vector or tensor using
245 * an initial gradient of this quantity (typically lsq).
246 *
247 * parameters:
248 * m <-- pointer to associated mesh structure
249 * fvq <-- pointer to associated finite volume quantities
250 * cpl <-- structure associated with internal coupling, or NULL
251 * inc <-- if 0, solve on increment; 1 otherwise
252 * warped_correction <-- apply warped faces correction ?
253 * coefav <-- B.C. coefficients for boundary face normals
254 * coefbv <-- B.C. coefficients for boundary face normals
255 * pvar <-- variable
256 * c_weight <-- weighted gradient coefficient variable
257 * r_grad <-- gradient used for reconstruction
258 * grad --> gradient of pvar (du_i/dx_j : grad[][i][j])
259 *----------------------------------------------------------------------------*/
260
261template <cs_lnum_t stride>
262void
263cs_gradient_strided_gg_r_cuda
264(
265 const cs_mesh_t *m,
266 const cs_mesh_adjacencies_t *madj,
267 const cs_mesh_quantities_t *fvq,
268 cs_halo_type_t halo_type,
269 int inc,
270 bool warped_correction,
271 const cs_real_t coefav[][stride],
272 const cs_real_t coefbv[][stride][stride],
273 const cs_real_t pvar[][stride],
274 const cs_real_t *c_weight,
275 const cs_real_t r_grad[][stride][3],
276 cs_real_t grad[][stride][3]
277);
278
279#endif /* defined(HAVE_CUDA) */
280
281#endif /* defined(__cplusplus) */
282
285/*----------------------------------------------------------------------------*/
#define restrict
Definition: cs_defs.h:158
double cs_real_t
Floating-point value.
Definition: cs_defs.h:357
#define CS_REAL_TYPE
Definition: cs_defs.h:501
cs_real_t cs_real_3_t[3]
vector of 3 floating-point values
Definition: cs_defs.h:374
cs_halo_type_t
Definition: cs_halo.h:57
void cs_halo_perio_sync_var_vect(const cs_halo_t *halo, cs_halo_type_t sync_mode, cs_real_t var[], int incvar)
Definition: cs_halo_perio.cpp:384
void cs_halo_perio_sync_var_tens(const cs_halo_t *halo, cs_halo_type_t sync_mode, cs_real_t var[])
Definition: cs_halo_perio.cpp:455
void cs_halo_perio_sync_var_sym_tens_grad(const cs_halo_t *halo, cs_halo_type_t sync_mode, cs_real_t var[])
Definition: cs_halo_perio.cpp:595
static void cs_sync_d2h(void *ptr)
Synchronize data from device to host.
Definition: cs_mem.h:1014
static void cs_sync_h2d(const void *ptr)
Synchronize data from host to device.
Definition: cs_mem.h:947
Definition: cs_mesh_adjacencies.h:82
Definition: cs_mesh_quantities.h:92
Definition: cs_mesh.h:85
int have_rotation_perio
Definition: cs_mesh.h:143
cs_halo_t * halo
Definition: cs_mesh.h:156