Nektar++
Loading...
Searching...
No Matches
EnergyKernels.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: EnergyKernels.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: Device-portable kinetic-energy and enstrophy kernels.
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#pragma once
36
38
39namespace Nektar::detail
40{
41
42/**
43 * @brief Write the squared velocity and vorticity magnitudes at storage
44 * index @p idx, and the volume it stands for, each times @p weight, to
45 * vals[0], vals[1] and vals[2].
46 *
47 * The offsets are those of the velocity components and of the gradient
48 * components forming the curl; in 2D the w and z-derivative offsets are not
49 * read.
50 */
51template <unsigned int NDIM, typename TData>
53 const size_t idx, const TData weight, const TData *NEK_RESTRICT velocity,
54 const TData *NEK_RESTRICT gradient, const size_t uOffset,
55 const size_t vOffset, [[maybe_unused]] const size_t wOffset,
56 const size_t dudyOffset, [[maybe_unused]] const size_t dudzOffset,
57 const size_t dvdxOffset, [[maybe_unused]] const size_t dvdzOffset,
58 [[maybe_unused]] const size_t dwdxOffset,
59 [[maybe_unused]] const size_t dwdyOffset, TData *vals)
60{
61 if constexpr (NDIM == 2)
62 {
63 const TData velocitySquared =
64 velocity[uOffset + idx] * velocity[uOffset + idx] +
65 velocity[vOffset + idx] * velocity[vOffset + idx];
66 const TData curl =
67 gradient[dvdxOffset + idx] - gradient[dudyOffset + idx];
68
69 vals[0] = weight * velocitySquared;
70 vals[1] = weight * (curl * curl);
71 }
72 else if constexpr (NDIM == 3)
73 {
74 const TData velocitySquared =
75 velocity[uOffset + idx] * velocity[uOffset + idx] +
76 velocity[vOffset + idx] * velocity[vOffset + idx] +
77 velocity[wOffset + idx] * velocity[wOffset + idx];
78 const TData curlX =
79 gradient[dwdyOffset + idx] - gradient[dvdzOffset + idx];
80 const TData curlY =
81 gradient[dudzOffset + idx] - gradient[dwdxOffset + idx];
82 const TData curlZ =
83 gradient[dvdxOffset + idx] - gradient[dudyOffset + idx];
84
85 vals[0] = weight * velocitySquared;
86 vals[1] = weight * (curlX * curlX + curlY * curlY + curlZ * curlZ);
87 }
88 vals[2] = weight;
89}
90
91/**
92 * @brief Add the integrals of the squared velocity and vorticity magnitudes
93 * and the volume of one block to result[0], result[1] and result[2].
94 *
95 * The velocity, its gradient and the quadrature metric share the block's
96 * storage layout, the caller passing the metric at the interleave width of
97 * the velocity. One storage index then addresses the same point in all three,
98 * whatever that width, and every entry of a component is one term of a
99 * parallel sum: the kernel walks the storage as it is laid out, neighbouring
100 * threads reading neighbouring entries, with no reshape at either width. The
101 * padding of the metric is zero, so the padded entries add nothing. The
102 * gradient holds the derivative of velocity component @c c in direction @c d
103 * as component <tt>c * NDIM + d</tt>.
104 *
105 * Each component holds @p nPlanes homogeneous planes of @p planeSize entries,
106 * one after the other, each weighted by its entry of @p planeWeights, which
107 * the kernel finds from the plane of an entry, its only index arithmetic. A
108 * single plane is that of a field without a homogeneous direction, of unit
109 * weight, and is summed without it.
110 */
111template <typename ExecSpace, unsigned int NDIM, typename TData>
112void EnergyKernelLauncher(const size_t planeSize, const unsigned int nPlanes,
113 const unsigned int velocityComponent0,
114 const unsigned int velocityComponent1,
115 const unsigned int velocityComponent2,
116 const TData *metric, const TData *planeWeights,
117 const TData *velocity, const TData *gradient,
118 TData *result, const unsigned int streamID)
119{
120 static_assert(NDIM == 2 || NDIM == 3,
121 "EnergyOp supports only dimensions 2 and 3.");
122
123 // Entries of a component, over all its planes.
124 const size_t compSize = nPlanes * planeSize;
125
127 if (nPlanes == 1)
128 {
129 const size_t uOffset = velocityComponent0 * compSize;
130 const size_t vOffset = velocityComponent1 * compSize;
131 const size_t wOffset = velocityComponent2 * compSize;
132 const size_t dudyOffset = (velocityComponent0 * NDIM + 1) * compSize;
133 const size_t dudzOffset = (velocityComponent0 * NDIM + 2) * compSize;
134 const size_t dvdxOffset = (velocityComponent1 * NDIM + 0) * compSize;
135 const size_t dvdzOffset = (velocityComponent1 * NDIM + 2) * compSize;
136 const size_t dwdxOffset = (velocityComponent2 * NDIM + 0) * compSize;
137 const size_t dwdyOffset = (velocityComponent2 * NDIM + 1) * compSize;
138
139 Nektar::parallel_reduce<ExecSpace, false, Nektar::ReduceSum<TData>, 3>(
140 0, compSize,
141 NEKTAR_LAMBDA(const size_t idx, TData *vals) {
142 EnergyKernel<NDIM>(idx, metric[idx], velocity, gradient,
143 uOffset, vOffset, wOffset, dudyOffset,
144 dudzOffset, dvdxOffset, dvdzOffset,
145 dwdxOffset, dwdyOffset, vals);
146 },
147 result);
148 }
149 else
150 {
151 // Offsets of the velocity components and of the gradient components
152 // forming the curl; those of w and of the z derivatives are not read in
153 // 2D.
154 const size_t uOffset = velocityComponent0 * compSize;
155 const size_t vOffset = velocityComponent1 * compSize;
156 const size_t wOffset = velocityComponent2 * compSize;
157 const size_t dudyOffset = (velocityComponent0 * NDIM + 1) * compSize;
158 const size_t dudzOffset = (velocityComponent0 * NDIM + 2) * compSize;
159 const size_t dvdxOffset = (velocityComponent1 * NDIM + 0) * compSize;
160 const size_t dvdzOffset = (velocityComponent1 * NDIM + 2) * compSize;
161 const size_t dwdxOffset = (velocityComponent2 * NDIM + 0) * compSize;
162 const size_t dwdyOffset = (velocityComponent2 * NDIM + 1) * compSize;
163
164 Nektar::parallel_reduce<ExecSpace, false, Nektar::ReduceSum<TData>, 3>(
165 0, compSize,
166 NEKTAR_LAMBDA(const size_t idx, TData *vals) {
167 const size_t plane = idx / planeSize;
168 const TData weight =
169 metric[idx - plane * planeSize] * planeWeights[plane];
170 EnergyKernel<NDIM>(idx, weight, velocity, gradient, uOffset,
171 vOffset, wOffset, dudyOffset, dudzOffset,
172 dvdxOffset, dvdzOffset, dwdxOffset,
173 dwdyOffset, vals);
174 },
175 result);
176 }
178}
179
180} // namespace Nektar::detail
#define NEK_RESTRICT
Definition Backends.hpp:254
#define NEK_HOSTDEVICE_INLINE
Definition Backends.hpp:268
#define NEKTAR_LAMBDA
void EnergyKernelLauncher(const size_t planeSize, const unsigned int nPlanes, const unsigned int velocityComponent0, const unsigned int velocityComponent1, const unsigned int velocityComponent2, const TData *metric, const TData *planeWeights, const TData *velocity, const TData *gradient, TData *result, const unsigned int streamID)
Add the integrals of the squared velocity and vorticity magnitudes and the volume of one block to res...
NEK_HOSTDEVICE_INLINE void EnergyKernel(const size_t idx, const TData weight, const TData *NEK_RESTRICT velocity, const TData *NEK_RESTRICT gradient, const size_t uOffset, const size_t vOffset, const size_t wOffset, const size_t dudyOffset, const size_t dudzOffset, const size_t dvdxOffset, const size_t dvdzOffset, const size_t dwdxOffset, const size_t dwdyOffset, TData *vals)
Write the squared velocity and vorticity magnitudes at storage index idx, and the volume it stands fo...
static void LoopExecutionSetStreamID(const unsigned int streamID)