Nektar++
Loading...
Searching...
No Matches
DiffusionScalarIPTraceFluxKernels.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: DiffusionScalarIPTraceFluxKernels.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Scalar interior penalty diffusion kernels.
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#pragma once
36
39
40namespace Nektar::detail
41{
42
43/**
44 * @brief Interior-penalty numerical flux for scalar diffusion, evaluated over
45 * one padded trace block.
46 *
47 * For each trace point and field component the flux is
48 *
49 * \f[
50 * \hat{f} = \mathbf{n}^{T} D \{\nabla u\}
51 * + C\,(\mathbf{n}^{T} D \mathbf{n})\,[u]
52 * \f]
53 *
54 * where \f$\{\cdot\}\f$ is the average across the trace, \f$[u]\f$ the jump,
55 * and \f$C\f$ the product of @p penaltyCoeff with the per-point geometric
56 * factor @p penaltyFactorBase. Resolving the diffusion tensor in the normal
57 * direction in both terms is what makes the penalty scale correctly for
58 * anisotropic \f$D\f$.
59 *
60 * The average is the half-and-half \f$\{w\} = (w^+ + w^-)/2\f$ of Zhen-Guo Yan,
61 * *Efficient implicit spectral/hp element DG techniques for compressible
62 * flows*, PhD thesis, Imperial College London, 2021, Ch. 2, Eq. (2.17), and it
63 * is used on boundary traces as well as interior ones. @p IsInterior therefore
64 * affects only how the jump is distributed:
65 *
66 * | Quantity | `IsInterior == true` | `IsInterior == false` |
67 * |-------------------|---------------------------|-----------------------|
68 * | average \f$\{u\}\f$ | \f$(u_f + u_b)/2\f$ | \f$(u_f + u_b)/2\f$ |
69 * | jump \f$[u]\f$ | \f$u_b - u_f\f$ | \f$u_b - u_f\f$ |
70 * | average gradient | \f$(g_f + g_b)/2\f$ | \f$(g_f + g_b)/2\f$ |
71 *
72 * A single averaging rule only imposes the right condition if the backward
73 * state on a boundary is a *ghost* rather than the boundary value itself: to
74 * impose \f$u = g\f$ the caller must pass \f$u_b = 2g - u_f\f$, whose average
75 * with the interior is \f$g\f$ and whose jump is \f$2(g - u_f)\f$. That is the
76 * caller's responsibility - see DiffusionScalarIPTraceFluxOpImpl.
77 *
78 * Legacy did this the other way round, passing \f$g\f$ unmodified and giving
79 * the boundary its own weights (average \f$u_b\f$, jump \f$2(u_b - u_f)\f$).
80 * The two are algebraically identical. Carrying the condition in the state
81 * keeps one rule in the kernel, which matters once boundary conditions differ
82 * in *which* exterior state they build - a reflective wall already supplies a
83 * ghost and must not be reflected again.
84 *
85 * A Neumann component has no imposed value to reflect about and is passed
86 * through with \f$u_b = u_f\f$; the caller likewise passes the interior
87 * gradient as both sides where no gradient condition applies, which leaves the
88 * gradient term equal to the interior value.
89 *
90 * Data layout: all arrays are component-major with @p traceStride between
91 * consecutive components of the same block, and the gradient arrays carry
92 * `NDIM * nvarComps` components ordered component-fastest-outer, direction
93 * inner.
94 *
95 * @tparam ExecSpace Execution space; selects the SIMD width used to walk the
96 * block.
97 * @tparam NDIM Coordinate dimension.
98 * @tparam IsInterior True for interior traces, false for boundary traces.
99 * @tparam TData Floating-point representation.
100 *
101 * @param npts - Padded number of points in the block. Must be an
102 * exact multiple of the vector width.
103 * @param nvarComps - Number of field components.
104 * @param traceStride - Stride between components within the block.
105 * @param penaltyCoeff - User penalty coefficient, `IPPenaltyCoeff`.
106 * @param diffCoeff - Upper triangle of the symmetric diffusion tensor.
107 * @param normbase - Trace normals, @p NDIM components.
108 * @param penaltyFactorBase - Per-point geometric penalty factor.
109 * @param fwdbase - Forward-side solution values.
110 * @param bwdbase - Backward-side solution values.
111 * @param derivfwdbase - Forward-side solution gradient.
112 * @param derivbwdbase - Backward-side solution gradient.
113 * @param fluxbase - Output flux, @p nvarComps components.
114 * @param streamID - Stream the launch runs in; reset to the default
115 * afterwards.
116 *
117 */
118template <typename ExecSpace, unsigned int NDIM, bool IsInterior,
119 typename TData>
121 const size_t npts, const unsigned int nvarComps, const size_t traceStride,
122 const TData penaltyCoeff, const TData *diffCoeff, const TData *normbase,
123 const TData *penaltyFactorBase, const TData *fwdbase, const TData *bwdbase,
124 const TData *derivfwdbase, const TData *derivbwdbase, TData *fluxbase,
125 const unsigned int streamID = 0)
126{
127 using vec_t =
129 TData>::type;
130 constexpr unsigned int vec_width =
131 (std::is_same_v<ExecSpace, NektarSpaces::AVX>)
133 : 1;
134
135 ASSERTL1(npts % vec_width == 0,
136 "Routine assumes blksize is exact integer multiple of vec_width");
137
138 const size_t groupsize = npts / vec_width;
139 const auto normvec = reinterpret_cast<const vec_t *>(normbase);
140 const auto penaltyVec = reinterpret_cast<const vec_t *>(penaltyFactorBase);
141 const auto fwdvec = reinterpret_cast<const vec_t *>(fwdbase);
142 const auto bwdvec = reinterpret_cast<const vec_t *>(bwdbase);
143 const auto derivFwdVec = reinterpret_cast<const vec_t *>(derivfwdbase);
144 const auto derivBwdVec = reinterpret_cast<const vec_t *>(derivbwdbase);
145 auto fluxvec = reinterpret_cast<vec_t *>(fluxbase);
146
147 const size_t traceVecStride = traceStride / vec_width;
148
149 // One averaging rule everywhere - see the note above on the exterior
150 // state. Legacy instead collapsed the boundary average onto the backward
151 // side (bWeightAver 1, fWeightAver 0); the two agree once the caller
152 // supplies a reflected ghost.
153 const TData bWeightAver = TData(0.5);
154 const TData fWeightAver = TData(0.5);
155 const TData bWeightJump = (IsInterior) ? TData(1.0) : TData(0.0);
156 const TData fWeightJump = (IsInterior) ? TData(1.0) : TData(2.0);
157
159
160 Nektar::parallel_for<ExecSpace>(
161 0u, groupsize, NEKTAR_LAMBDA(const size_t i) {
162 const vec_t penalty = vec_t(penaltyCoeff) * penaltyVec[i];
163
164 // Effective diffusion strength in the trace normal
165 // direction:
166 //
167 // normalDiffusionStrength = n^T D n
168 //
169 // For isotropic diffusion D = I and unit normals, this
170 // is 1. For anisotropic/tensor diffusion, this scales the
171 // penalty according to how strongly diffusion acts across
172 // the current face.
173 vec_t normalDiffusionStrength = vec_t(0.0);
174 for (unsigned int n0 = 0; n0 < NDIM; ++n0)
175 {
176 const vec_t normal0 = normvec[n0 * traceVecStride + i];
177
178 for (unsigned int n1 = 0; n1 < NDIM; ++n1)
179 {
180 normalDiffusionStrength +=
181 normal0 *
183 NDIM, n0 * NDIM + n1)]) *
184 normvec[n1 * traceVecStride + i];
185 }
186 }
187
188 for (unsigned int f = 0; f < nvarComps; ++f)
189 {
190 const vec_t uFwd = fwdvec[f * traceVecStride + i];
191 const vec_t uBwd = bwdvec[f * traceVecStride + i];
192 const vec_t uAverage = fWeightAver * uFwd + bWeightAver * uBwd;
193
194 // This is u_bwd - u_fwd.
195 const vec_t bwdMinusFwd = (uAverage - uFwd) * fWeightJump +
196 (uBwd - uAverage) * bWeightJump;
197
198 // normalAverageGradientFlux = {D grad(u)} · n
199 // Since D is symmetric, this is equivalently: n^T D
200 // {grad(u)}
201 vec_t normalAverageGradientFlux = vec_t(0.0);
202
203 for (unsigned int d = 0; d < NDIM; ++d)
204 {
205 const vec_t avgDeriv =
206 fWeightAver *
207 derivFwdVec[(f * NDIM + d) * traceVecStride + i] +
208 bWeightAver *
209 derivBwdVec[(f * NDIM + d) * traceVecStride + i];
210
211 vec_t normalDiffCoeff = vec_t(0.0);
212 for (unsigned int n = 0; n < NDIM; ++n)
213 {
214 const unsigned int coeffId =
215 LibUtilities::GetDiffCoeffMap(NDIM, n * NDIM + d);
216 normalDiffCoeff += normvec[n * traceVecStride + i] *
217 vec_t(diffCoeff[coeffId]);
218 }
219
220 normalAverageGradientFlux += normalDiffCoeff * avgDeriv;
221 }
222
223 // Combine flux avg(D grad u).n - C11 * (u_fwd -
224 // u_bwd) on the fwd side.
225 fluxvec[f * traceVecStride + i] =
226 normalAverageGradientFlux +
227 penalty * normalDiffusionStrength * bwdMinusFwd;
228 }
229 });
230
232}
233
234} // namespace Nektar::detail
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
#define NEKTAR_LAMBDA
#define NEK_FORCE_INLINE
Definition NekInline.hpp:46
NEK_HOSTDEVICE_INLINE unsigned int GetDiffCoeffMap(const unsigned int ncoord, const unsigned int index)
static NEK_FORCE_INLINE void DiffuseScalarTraceFluxKernelLauncher(const size_t npts, const unsigned int nvarComps, const size_t traceStride, const TData penaltyCoeff, const TData *diffCoeff, const TData *normbase, const TData *penaltyFactorBase, const TData *fwdbase, const TData *bwdbase, const TData *derivfwdbase, const TData *derivbwdbase, TData *fluxbase, const unsigned int streamID=0)
Interior-penalty numerical flux for scalar diffusion, evaluated over one padded trace block.
static void LoopExecutionSetStreamID(const unsigned int streamID)
tinysimd::simd< NekDouble > vec_t
typename abi< ScalarType, width >::type simd
Definition tinysimd.hpp:138