DNDSR 0.1.0.dev1+gcd065ad
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
BenchmarkFiniteVolume.hpp
Go to the documentation of this file.
1#pragma once
2#include "DNDS/Defines.hpp"
4#include "DNDS/Errors.hpp"
5#include "FiniteVolume.hpp"
6#include "DNDS/JsonUtil.hpp"
7#include "Geom/Geometric.hpp"
8
9namespace DNDS::CFV
10{
11 template <DeviceBackend B, bool iVarOne = false>
16 index iCell, real *local_buf, int iVar = UnInitRowsize)
17 {
18 using namespace Geom;
19 auto grad = u_grad[iCell];
20 tPoint gradResultOne;
21 Eigen::Map<Eigen::Matrix<real, 3, DynamicSize>> gradResult(local_buf, 3, grad.cols());
22 if constexpr (iVarOne)
23 gradResultOne.setZero();
24 else
25 DNDS_HD_assert(local_buf),
26 gradResult.setZero();
27 auto &mesh = fv.mesh;
28 // if (iCell != 0)
29 // return;
30 for (rowsize ic2f = 0; ic2f < mesh.cell2face.RowSize(iCell); ic2f++)
31 {
32 index iFace = mesh.cell2face(iCell, ic2f);
33 index iCellOther = mesh.CellFaceOther(iCell, iFace);
34 rowsize if2c = mesh.CellIsFaceBack(iCell, iFace) ? 0 : 1;
35 if (iCellOther == UnInitIndex)
36 continue; //! ignoreing BC here
37 // face-out-area-norm
38 tPoint norm = fv.GetFaceNormFromCell(iFace, iCell, if2c, -1) *
39 ((if2c ? -1.0 : 1.0) * fv.GetFaceArea(iFace));
40 auto uOther = u[iCellOther];
41 auto uI = u[iCell];
42#ifdef DNDS_USE_CUDA
43 if constexpr (B == DeviceBackend::CUDA)
44 {
45 // printf("if2c %d\n", if2c);
46 // printf("iCellOther %lld\n", iCellOther);
47 if constexpr (iVarOne)
48 for (int i = 0; i < 3; i++)
49 gradResultOne(i) += norm(i) * (uOther(iVar) - uI(iVar));
50 else
51 for (int iV = 0; iV < grad.cols(); iV++)
52 {
53 // printf("GetFaceNormFromCell %g\n", fv.GetFaceNormFromCell(iFace, iCell, if2c, -1).norm());
54 // printf("u[iCellOther](iE) %g\n", u[iCellOther](iE));
55 // printf("u[iCell](iE) %g\n", u[iCell](iE));
56 for (int i = 0; i < 3; i++)
57 gradResult(i, iV) += norm(i) * (uOther(iV) - uI(iV));
58 // //! grad({0, 1, 2}, iE) does not work on CUDA!!
59 // grad.col(iE) += norm * (uOther[iE] - uI[iE]);
60 }
61 }
62 else
63#endif
64 {
65 if constexpr (iVarOne)
66 gradResultOne += norm * (uOther(iVar) - uI(iVar));
67 else
68 // for (int iE = 0; iE < grad.cols(); iE++)
69 // do_one_var(iE);
70 gradResult.noalias() += norm * (uOther - uI).transpose();
71 }
72 }
73 if constexpr (iVarOne)
74 {
75 gradResultOne *= 1.0 / fv.GetCellVol(iCell);
76 // grad.col(iVar) = gradResultOne;
77 for (int i = 0; i < 3; i++)
78 grad(i, iVar) = gradResultOne(i);
79 }
80 else
81 {
82 gradResult *= 1.0 / fv.GetCellVol(iCell);
83 grad = gradResult;
84 }
85 }
86
87 template <DeviceBackend B>
91 int nIter,
92 const t_jsonconfig &settings);
93 template <DeviceBackend B>
95 FiniteVolume &fv,
98 int nIter,
99 const t_jsonconfig &settings)
100 {
101 auto u_view = u.deviceView<B>();
102 auto u_grad_view = u_grad.deviceView<B>();
103 auto fv_view = fv.deviceView<B>();
104
105 finiteVolumeCellOpTest_run<B>(fv_view, u_view, u_grad_view, nIter, settings);
106 }
107 /*************************************************************/
108
109 /*************************************************************/
110
111 /*************************************************************/
112
113 /*************************************************************/
114
115 template <DeviceBackend B, int nVarsFixed, bool iVarOne = false>
118 typename tUDof<nVarsFixed>::template t_deviceView<B> &u,
119 typename tUGrad<nVarsFixed, 3>::template t_deviceView<B> &u_grad,
120 index iCell, int iVar = UnInitRowsize)
121 {
122 static_assert(nVarsFixed > 0);
123 using namespace Geom;
124 auto grad = u_grad[iCell];
125 tPoint gradResultOne;
126 Eigen::Matrix<real, 3, nVarsFixed> gradResult;
127 if constexpr (iVarOne)
128 gradResultOne.setZero();
129 else
130 gradResult.setZero();
131 auto &mesh = fv.mesh;
132 // if (iCell != 0)
133 // return;
134 for (rowsize ic2f = 0; ic2f < mesh.cell2face.RowSize(iCell); ic2f++)
135 {
136 index iFace = mesh.cell2face(iCell, ic2f);
137 index iCellOther = mesh.CellFaceOther(iCell, iFace);
138 rowsize if2c = mesh.CellIsFaceBack(iCell, iFace) ? 0 : 1;
139 if (iCellOther == UnInitIndex)
140 continue; //! ignoreing BC here
141 // face-out-area-norm
142 tPoint norm = fv.GetFaceNormFromCell(iFace, iCell, if2c, -1) *
143 ((if2c ? -1.0 : 1.0) * fv.GetFaceArea(iFace));
144 auto uOther = u[iCellOther];
145 auto uI = u[iCell];
146#ifdef DNDS_USE_CUDA
147 if constexpr (B == DeviceBackend::CUDA)
148 {
149 if constexpr (iVarOne)
150 for (int i = 0; i < 3; i++)
151 gradResultOne(i) += norm(i) * (uOther(iVar) - uI(iVar));
152 else
153 for (int iV = 0; iV < grad.cols(); iV++)
154 {
155 for (int i = 0; i < 3; i++)
156 gradResult(i, iV) += norm(i) * (uOther(iV) - uI(iV));
157 // //! grad({0, 1, 2}, iE) does not work on CUDA!!
158 // grad.col(iE) += norm * (uOther[iE] - uI[iE]);
159 }
160 }
161 else
162#endif
163 {
164 if constexpr (iVarOne)
165 gradResultOne.noalias() += norm * (uOther(iVar) - uI(iVar));
166 else
167 gradResult.noalias() += norm * (uOther - uI).transpose();
168 }
169 }
170 if constexpr (iVarOne)
171 {
172 gradResultOne *= 1.0 / fv.GetCellVol(iCell);
173 // grad.col(iVar) = gradResultOne;
174 for (int i = 0; i < 3; i++)
175 grad(i, iVar) = gradResultOne(i);
176 }
177 else
178 {
179 gradResult *= 1.0 / fv.GetCellVol(iCell);
180 grad = gradResult;
181 }
182 }
183
184 template <DeviceBackend B, int nVarsFixed>
186
187#ifdef DNDS_USE_CUDA
188 template <int nVarsFixed>
189 struct finiteVolumeCellOpTest_Fixed_entry<DeviceBackend::CUDA, nVarsFixed>
190 {
191 static constexpr auto B = DeviceBackend::CUDA;
193 typename tUDof<nVarsFixed>::template t_deviceView<B> &u,
194 typename tUGrad<nVarsFixed, 3>::template t_deviceView<B> &u_grad,
195 int nIter,
196 const t_jsonconfig &settings);
197 };
198#endif
199
200 template <int nVarsFixed>
202 {
203 static constexpr auto B = DeviceBackend::Host;
205 typename tUDof<nVarsFixed>::template t_deviceView<B> &u,
206 typename tUGrad<nVarsFixed, 3>::template t_deviceView<B> &u_grad,
207 int nIter,
208 const t_jsonconfig &settings);
209 };
210
211 template <DeviceBackend B, int nVarsFixed>
213 FiniteVolume &fv,
215 tUGrad<nVarsFixed, 3> &u_grad,
216 int nIter,
217 const t_jsonconfig &settings)
218 {
219 auto u_view = u.template deviceView<B>();
220 auto u_grad_view = u_grad.template deviceView<B>();
221 auto fv_view = fv.deviceView<B>();
222
223 finiteVolumeCellOpTest_Fixed_entry<B, nVarsFixed>().run(fv_view, u_view, u_grad_view, nIter, settings);
224 }
225
226 /*************************************************************/
227
228 /*************************************************************/
229
230 /*************************************************************/
231
232 /*************************************************************/
233
234 template <DeviceBackend B, int nVarsFixed, bool iVarOne = false>
237 std::array<tUDof<1>::t_deviceView<B>, nVarsFixed> &u,
238 std::array<tUGrad<1, 3>::t_deviceView<B>, nVarsFixed> &u_grad,
239 index iCell, int iVar = UnInitRowsize)
240 {
241 static_assert(nVarsFixed > 0);
242 using namespace Geom;
243 tPoint gradResultOne;
244 Eigen::Matrix<real, 3, nVarsFixed> gradResult;
245 if constexpr (iVarOne)
246 gradResultOne.setZero();
247 else
248 gradResult.setZero();
249 auto &mesh = fv.mesh;
250 // if (iCell != 0)
251 // return;
252 for (rowsize ic2f = 0; ic2f < mesh.cell2face.RowSize(iCell); ic2f++)
253 {
254 index iFace = mesh.cell2face(iCell, ic2f);
255 index iCellOther = mesh.CellFaceOther(iCell, iFace);
256 rowsize if2c = mesh.CellIsFaceBack(iCell, iFace) ? 0 : 1;
257 if (iCellOther == UnInitIndex)
258 continue; //! ignoring BC here
259 // face-out-area-norm
260 tPoint norm = fv.GetFaceNormFromCell(iFace, iCell, if2c, -1) *
261 ((if2c ? -1.0 : 1.0) * fv.GetFaceArea(iFace));
262
263 if constexpr (iVarOne)
264 for (int i = 0; i < 3; i++)
265 gradResultOne(i) += norm(i) * (u[iVar][iCellOther](0) - u[iVar][iCell](0));
266 else
267 for (int iV = 0; iV < nVarsFixed; iV++)
268 {
269 for (int i = 0; i < 3; i++)
270 gradResult(i, iV) += norm(i) * (u[iV][iCellOther](0) - u[iV][iCell](0));
271 // //! grad({0, 1, 2}, iE) does not work on CUDA!!
272 // grad.col(iE) += norm * (uOther[iE] - uI[iE]);
273 }
274 }
275 if constexpr (iVarOne)
276 {
277 gradResultOne *= 1.0 / fv.GetCellVol(iCell);
278 // grad.col(iVar) = gradResultOne;
279 for (int i = 0; i < 3; i++)
280 u_grad[iVar][iCell](i, 0) = gradResultOne(i);
281 }
282 else
283 {
284 gradResult *= 1.0 / fv.GetCellVol(iCell);
285 for (int iV = 0; iV < nVarsFixed; iV++)
286 for (int i = 0; i < 3; i++)
287 u_grad[iV][iCell](i) = gradResult(i, iV);
288 }
289 }
290
291 template <DeviceBackend B, int nVarsFixed>
293#ifdef DNDS_USE_CUDA
294 template <int nVarsFixed>
296 {
297 static constexpr auto B = DeviceBackend::CUDA;
299 std::array<tUDof<1>::t_deviceView<B>, nVarsFixed> &u,
300 std::array<tUGrad<1, 3>::t_deviceView<B>, nVarsFixed> &u_grad,
301 int nIter,
302 const t_jsonconfig &settings);
303 };
304#endif
305
306 template <int nVarsFixed>
308 {
309 static constexpr auto B = DeviceBackend::Host;
311 std::array<tUDof<1>::t_deviceView<B>, nVarsFixed> &u,
312 std::array<tUGrad<1, 3>::t_deviceView<B>, nVarsFixed> &u_grad,
313 int nIter,
314 const t_jsonconfig &settings);
315 };
316
317 template <DeviceBackend B, int nVarsFixed>
319 FiniteVolume &fv,
320 std::array<tUDof<1>, nVarsFixed> &u,
321 std::array<tUGrad<1, 3>, nVarsFixed> &u_grad,
322 int nIter,
323 const t_jsonconfig &settings)
324 {
325 auto fv_view = fv.deviceView<B>();
326
327 std::array<tUDof<1>::t_deviceView<B>, nVarsFixed> u_view;
328 for (int i = 0; i < nVarsFixed; i++)
329 u_view[i] = u[i].template deviceView<B>();
330 std::array<tUGrad<1, 3>::t_deviceView<B>, nVarsFixed> u_grad_view;
331 for (int i = 0; i < nVarsFixed; i++)
332 u_grad_view[i] = u_grad[i].template deviceView<B>();
333
334 finiteVolumeCellOpTest_SOA_ver0_entry<B, nVarsFixed>().run(fv_view, u_view, u_grad_view, nIter, settings);
335 }
336}
Core type aliases, constants, and metaprogramming utilities for the DNDS framework.
#define DNDS_DEVICE_CALLABLE
Definition Defines.hpp:76
Device memory abstraction layer with backend-specific storage and factory creation.
Assertion / error-handling macros and supporting helper functions.
#define DNDS_HD_assert(cond)
Host-only expansion of DNDS_HD_assert (equivalent to DNDS_assert).
Definition Errors.hpp:189
JSON-to-Eigen conversion utilities and nlohmann_json helper macros.
Mutable device view of an ArrayDof father/son pair.
Definition ArrayDOF.hpp:34
Primary solver state container: an ArrayEigenMatrix pair with MPI-collective vector-space operations.
Definition ArrayDOF.hpp:172
t_deviceView< B > deviceView()
Build a mutable device view (wraps the base-class implementation).
Definition ArrayDOF.hpp:186
Geom::UnstructuredMeshDeviceView< B > mesh
DNDS_DEVICE_CALLABLE real GetFaceArea(index iFace)
DNDS_DEVICE_CALLABLE real GetCellVol(index iCell)
DNDS_DEVICE_CALLABLE Geom::tPoint GetFaceNormFromCell(index iFace, index iCell, rowsize if2c, int iG)
t_deviceView< B > deviceView()
DNDS_DEVICE_CALLABLE void finiteVolumeCellOpTest_SOA_ver0(FiniteVolume::t_deviceView< B > &fv, std::array< tUDof< 1 >::t_deviceView< B >, nVarsFixed > &u, std::array< tUGrad< 1, 3 >::t_deviceView< B >, nVarsFixed > &u_grad, index iCell, int iVar=UnInitRowsize)
void finiteVolumeCellOpTest_Fixed_main(FiniteVolume &fv, tUDof< nVarsFixed > &u, tUGrad< nVarsFixed, 3 > &u_grad, int nIter, const t_jsonconfig &settings)
DNDS_DEVICE_CALLABLE void finiteVolumeCellOpTest_Fixed(FiniteVolume::t_deviceView< B > &fv, typename tUDof< nVarsFixed >::template t_deviceView< B > &u, typename tUGrad< nVarsFixed, 3 >::template t_deviceView< B > &u_grad, index iCell, int iVar=UnInitRowsize)
DNDS_DEVICE_CALLABLE void finiteVolumeCellOpTest(FiniteVolume::t_deviceView< B > &fv, tUDof< DynamicSize >::t_deviceView< B > &u, tUGrad< DynamicSize, 3 >::t_deviceView< B > &u_grad, index iCell, real *local_buf, int iVar=UnInitRowsize)
void finiteVolumeCellOpTest_main(FiniteVolume &fv, tUDof< DynamicSize > &u, tUGrad< DynamicSize, 3 > &u_grad, int nIter, const t_jsonconfig &settings)
void finiteVolumeCellOpTest_run(FiniteVolume::t_deviceView< B > &fv, tUDof< DynamicSize >::t_deviceView< B > &u, tUGrad< DynamicSize, 3 >::t_deviceView< B > &u_grad, int nIter, const t_jsonconfig &settings)
void finiteVolumeCellOpTest_run< B >(FiniteVolume::t_deviceView< B > &fv, tUDof< DynamicSize >::t_deviceView< B > &u, tUGrad< DynamicSize, 3 >::t_deviceView< B > &u_grad, int nIter, const t_jsonconfig &settings)
void finiteVolumeCellOpTest_SOA_ver0_main(FiniteVolume &fv, std::array< tUDof< 1 >, nVarsFixed > &u, std::array< tUGrad< 1, 3 >, nVarsFixed > &u_grad, int nIter, const t_jsonconfig &settings)
DeviceBackend
Enumerates the backends a DeviceStorage / Array can live on.
@ Host
Plain CPU memory.
DNDS_CONSTANT const index UnInitIndex
Sentinel "not initialised" index value (= INT64_MIN).
Definition Defines.hpp:176
int32_t rowsize
Row-width / per-row element-count type (signed 32-bit).
Definition Defines.hpp:109
int64_t index
Global row / DOF index type (signed 64-bit; handles multi-billion-cell meshes).
Definition Defines.hpp:107
double real
Canonical floating-point scalar used throughout DNDSR (double precision).
Definition Defines.hpp:105
nlohmann::ordered_json t_jsonconfig
Project-wide JSON type alias: nlohmann/json with ordered keys.
Definition JsonUtil.hpp:19
DNDS_CONSTANT const rowsize UnInitRowsize
Sentinel "not initialised" rowsize value (= INT32_MIN).
Definition Defines.hpp:179
void run(FiniteVolume::t_deviceView< B > &fv, typename tUDof< nVarsFixed >::template t_deviceView< B > &u, typename tUGrad< nVarsFixed, 3 >::template t_deviceView< B > &u_grad, int nIter, const t_jsonconfig &settings)
void run(FiniteVolume::t_deviceView< B > &fv, std::array< tUDof< 1 >::t_deviceView< B >, nVarsFixed > &u, std::array< tUGrad< 1, 3 >::t_deviceView< B >, nVarsFixed > &u_grad, int nIter, const t_jsonconfig &settings)