Nektar++
Loading...
Searching...
No Matches
EnforceEntropyKernels.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: EnforceEntropyKernels.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: The subsonic star states of the entropy Riemann boundary
32// conditions.
33//
34///////////////////////////////////////////////////////////////////////////////
35
36#pragma once
37
38#include <cmath>
39
42
43namespace Nektar::detail
44{
45
46/**
47 * @brief The state an entropy Riemann boundary condition imposes at a point.
48 *
49 * Density, pressure and the *inward* normal velocity. The caller turns the
50 * last into a velocity vector, which is where the three conditions agree
51 * again.
52 */
53template <typename TData> struct EntropyStarState
54{
55 TData rho;
56 TData p;
57 TData u;
58};
59
60/**
61 * @brief The incoming Riemann invariant of the interior state.
62 *
63 * \f$R^- = -u_n - 2c/(\gamma-1)\f$ with \f$u_n\f$ the *outward* normal
64 * velocity, so this is the invariant carried along the characteristic
65 * entering the domain. Two of the three conditions solve for the star state
66 * against it; the third replaces it with the interior sound speed directly.
67 */
68template <typename TData>
70 const TData c,
71 const TData twoOverGamM1)
72{
73 return -vn - c * twoOverGamM1;
74}
75
76/**
77 * @brief Subsonic inflow holding the entropy and the velocity.
78 *
79 * The sound speed is carried over from the interior unchanged and the entropy
80 * comes from the prescribed state; density and pressure follow from the pair.
81 * The normal velocity is the prescribed one reflected about the interior,
82 * \f$2(-V_n^\infty) + u_n\f$, which is what leaves the tangential components
83 * of the prescribed velocity untouched once the caller applies the mismatch.
84 */
85template <typename TData>
87 const TData vn, const TData cInt, [[maybe_unused]] const TData pInt,
88 [[maybe_unused]] const TData rhoInt, const TData rhoBC, const TData pBC,
89 const TData VnInf, const TData gamma, const TData gamM1,
90 [[maybe_unused]] const TData twoOverGamM1)
91{
92#if defined(__SYCL_DEVICE_ONLY__)
93 using sycl::pow;
94#else
95 using std::pow;
96#endif
97
98 const TData s = pBC / pow(rhoBC, gamma);
99 const TData c = cInt;
100
102 star.rho = pow(c * c / (gamma * s), TData(1.0) / gamM1);
103 star.p = c * c * star.rho / gamma;
104 star.u = TData(2.0) * (-VnInf) + vn;
105
106 return star;
107}
108
109/**
110 * @brief Subsonic inflow holding the entropy and the pressure.
111 *
112 * Density and pressure are taken from the prescribed state - which fixes the
113 * entropy with them - and the normal velocity is whatever the incoming
114 * invariant then requires. This is the combination the reference singles out
115 * as the stable way to impose a known pressure.
116 */
117template <typename TData>
119 const TData vn, const TData cInt, [[maybe_unused]] const TData pInt,
120 [[maybe_unused]] const TData rhoInt, const TData rhoBC, const TData pBC,
121 [[maybe_unused]] const TData VnInf, const TData gamma,
122 [[maybe_unused]] const TData gamM1, const TData twoOverGamM1)
123{
124 using std::sqrt;
125
126 const TData rR = EntropyRiemannInvariant(vn, cInt, twoOverGamM1);
127
129 star.rho = rhoBC;
130 star.p = pBC;
131
132 const TData c = sqrt(gamma * star.p / star.rho);
133 star.u = rR + c * twoOverGamM1;
134
135 return star;
136}
137
138/**
139 * @brief Subsonic inflow holding the entropy and the total enthalpy.
140 *
141 * The remaining freedom is filled by the stagnation state rather than by a
142 * static quantity, so the sound speed is no longer available directly and
143 * comes from a quadratic: with \f$k = 2/(\gamma-1)\f$,
144 *
145 * \f[ (k^2 + k)\,c_*^2 + 2k\,R^-c_* + \left(R^{-2} - v_n^2
146 * - k\,\gamma p_{BC}/\rho_{BC}\right)(k^2+k) = 0 \f]
147 *
148 * solved for c_*. The entropy of the prescribed state
149 * then gives the density, and the pressure follows.
150 */
151template <typename TData>
153 const TData vn, const TData cInt, [[maybe_unused]] const TData pInt,
154 [[maybe_unused]] const TData rhoInt, const TData rhoBC, const TData pBC,
155 const TData VnInf, const TData gamma, [[maybe_unused]] const TData gamM1,
156 const TData twoOverGamM1)
157{
158#if defined(__SYCL_DEVICE_ONLY__)
159 using sycl::pow;
160 using sycl::sqrt;
161#else
162 using std::pow;
163 using std::sqrt;
164#endif
165
166 const TData rR = EntropyRiemannInvariant(vn, cInt, twoOverGamM1);
167 const TData vnBC = -VnInf;
168
169 TData tmp1 = twoOverGamM1 * rR;
170 tmp1 = tmp1 * tmp1;
171
172 TData tmp2 = rR * rR - vnBC * vnBC - twoOverGamM1 * gamma * pBC / rhoBC;
173 tmp2 = (twoOverGamM1 * twoOverGamM1 + twoOverGamM1) * tmp2;
174
175 TData c = -twoOverGamM1 * rR + sqrt(tmp1 - tmp2);
176 c = c / (twoOverGamM1 * twoOverGamM1 + twoOverGamM1);
177
178 const TData s = pBC / pow(rhoBC, gamma);
179
181 star.u = rR + twoOverGamM1 * c;
182 star.rho = pow(c * c / (gamma * s), TData(0.5) * twoOverGamM1);
183 star.p = star.rho * c * c / gamma;
184
185 return star;
186}
187
188/**
189 * @brief Subsonic outflow imposing the prescribed pressure isentropically.
190 *
191 * Shared by the entropy-pressure and entropy-total-enthalpy conditions. The
192 * density comes from the interior along an isentrope to the imposed pressure,
193 * and the normal velocity from the incoming invariant. The entropy-velocity
194 * condition has no such branch - it imposes the prescribed state at an outflow
195 * instead - so it does not use this.
196 */
197template <typename TData>
199 const TData vn, const TData cInt, const TData pInt, const TData rhoInt,
200 [[maybe_unused]] const TData rhoBC, const TData pBC, const TData gamma,
201 const TData twoOverGamM1)
202{
203#if defined(__SYCL_DEVICE_ONLY__)
204 using sycl::pow;
205 using sycl::sqrt;
206#else
207 using std::pow;
208 using std::sqrt;
209#endif
210
211 const TData rR = EntropyRiemannInvariant(vn, cInt, twoOverGamM1);
212
214 star.p = pBC;
215 star.rho = rhoInt * pow(star.p / pInt, TData(1.0) / gamma);
216
217 const TData c = sqrt(gamma * star.p / star.rho);
218 star.u = rR + c * twoOverGamM1;
219
220 return star;
221}
222
223/**
224 * @brief Reduce a block's session-evaluated conserved state to the density,
225 * pressure, velocity and normal velocity the entropy conditions prescribe.
226 */
227template <typename ExecSpace, typename TData>
229 const size_t stride, const unsigned coordDim, const size_t eng,
230 const TData gamma, const TData *bndPtr, const TData *norms, TData *rhoBC,
231 TData *pBC, TData *velBC, TData *VnInf, const unsigned int streamID = 0)
232{
234
235 const TData gamM1 = gamma - TData(1.0);
236
237 Nektar::parallel_for<ExecSpace>(
238 0u, stride, NEKTAR_LAMBDA(const size_t i) {
239 const TData rho = bndPtr[i];
240
241 TData momSq = TData(0.0);
242 TData vn = TData(0.0);
243
244 for (unsigned d = 0; d < coordDim; ++d)
245 {
246 const TData mom = bndPtr[(d + 1) * stride + i];
247 const TData vel = mom / rho;
248
249 velBC[d * stride + i] = vel;
250 momSq += mom * mom;
251 vn += vel * norms[d * stride + i];
252 }
253
254 rhoBC[i] = rho;
255 VnInf[i] = vn;
256 pBC[i] =
257 gamM1 * (bndPtr[eng * stride + i] - TData(0.5) * momSq / rho);
258 });
259
261}
262
263/**
264 * @brief Build the boundary state on a block from the policy's star state,
265 * chosen by the direction and normal Mach number of the seeded interior
266 * state.
267 */
268template <typename ExecSpace, typename Policy, typename TData>
270 const size_t stride, const unsigned coordDim, const size_t eng,
271 const TData gamma, const TData *rhoBC, const TData *pBC, const TData *velBC,
272 const TData *VnInf, const TData *norms, TData *bndPtr,
273 const unsigned int streamID = 0)
274{
276
277 const TData gamM1 = gamma - TData(1.0);
278 const TData twoOverGamM1 = TData(2.0) / gamM1;
279
280 Nektar::parallel_for<ExecSpace>(
281 0u, stride, NEKTAR_LAMBDA(const size_t i) {
282 using std::abs;
283 using std::sqrt;
284
285 // The storage holds the interior state, seeded by the
286 // caller before this runs.
287 const TData rhoInt = bndPtr[i];
288
289 TData momSq = TData(0.0);
290 TData vn = TData(0.0);
291 for (unsigned d = 0; d < coordDim; ++d)
292 {
293 const TData mom = bndPtr[(d + 1) * stride + i];
294 momSq += mom * mom;
295 vn += mom * norms[d * stride + i];
296 }
297 // Outward normal velocity of the interior state.
298 vn /= rhoInt;
299
300 const TData pInt = gamM1 * (bndPtr[eng * stride + i] -
301 TData(0.5) * momSq / rhoInt);
302 const TData cInt = sqrt(gamma * pInt / rhoInt);
303
304 // It is the normal Mach number that decides how many
305 // characteristics cross, so the test is on n.u rather than
306 // on the speed.
307 const TData machN = abs(vn) / cInt;
308 const bool inflow = (vn <= TData(0.0));
309 const bool subsonic = (machN < TData(1.0));
310
312
313 if (inflow && subsonic)
314 {
315 star =
316 Policy::InflowStar(vn, cInt, pInt, rhoInt, rhoBC[i], pBC[i],
317 VnInf[i], gamma, gamM1, twoOverGamM1);
318 }
319 else if (!inflow && subsonic && Policy::hasSubsonicOutflow)
320 {
321 star = Policy::OutflowStar(vn, cInt, pInt, rhoInt, rhoBC[i],
322 pBC[i], gamma, twoOverGamM1);
323 }
324 else
325 {
326 // Supersonic carries nothing back into the domain, so
327 // the whole prescribed state stands. The conditions
328 // without a subsonic outflow branch land here too,
329 // since switching on the outflow Mach number was found
330 // to hurt convergence.
331 star.rho = rhoBC[i];
332 star.p = pBC[i];
333 star.u = -VnInf[i];
334 }
335
336 // Internal energy from the star pressure; the kinetic part
337 // comes with the velocity below.
338 TData EBC = star.p / gamM1;
339
340 // On inflow the prescribed velocity vector is kept and
341 // only its normal component shifted by the Riemann
342 // mismatch, so the tangential components survive. On
343 // outflow the velocity is purely normal.
344 const TData vnDiff = star.u + VnInf[i];
345
346 for (unsigned d = 0; d < coordDim; ++d)
347 {
348 const TData n = norms[d * stride + i];
349 const TData v =
350 inflow ? velBC[d * stride + i] - vnDiff * n : -star.u * n;
351
352 bndPtr[(d + 1) * stride + i] = star.rho * v;
353 EBC += TData(0.5) * star.rho * v * v;
354 }
355
356 bndPtr[i] = star.rho;
357 bndPtr[eng * stride + i] = EBC;
358 });
359
361}
362
363} // namespace Nektar::detail
#define NEK_HOSTDEVICE_INLINE
Definition Backends.hpp:268
#define NEKTAR_LAMBDA
#define NEK_FORCE_INLINE
Definition NekInline.hpp:46
Whole-field operator base class and factory machinery shared by every operator family in the MultiReg...
static NEK_FORCE_INLINE void ReduceEntropyStateBlock(const size_t stride, const unsigned coordDim, const size_t eng, const TData gamma, const TData *bndPtr, const TData *norms, TData *rhoBC, TData *pBC, TData *velBC, TData *VnInf, const unsigned int streamID=0)
Reduce a block's session-evaluated conserved state to the density, pressure, velocity and normal velo...
NEK_HOSTDEVICE_INLINE EntropyStarState< TData > EntropyTotalEnthalpyStar(const TData vn, const TData cInt, const TData pInt, const TData rhoInt, const TData rhoBC, const TData pBC, const TData VnInf, const TData gamma, const TData gamM1, const TData twoOverGamM1)
Subsonic inflow holding the entropy and the total enthalpy.
NEK_HOSTDEVICE_INLINE EntropyStarState< TData > EntropyPressureOutflowStar(const TData vn, const TData cInt, const TData pInt, const TData rhoInt, const TData rhoBC, const TData pBC, const TData gamma, const TData twoOverGamM1)
Subsonic outflow imposing the prescribed pressure isentropically.
static NEK_FORCE_INLINE void ApplyEnforceEntropyBlock(const size_t stride, const unsigned coordDim, const size_t eng, const TData gamma, const TData *rhoBC, const TData *pBC, const TData *velBC, const TData *VnInf, const TData *norms, TData *bndPtr, const unsigned int streamID=0)
Build the boundary state on a block from the policy's star state, chosen by the direction and normal ...
NEK_HOSTDEVICE_INLINE TData EntropyRiemannInvariant(const TData vn, const TData c, const TData twoOverGamM1)
The incoming Riemann invariant of the interior state.
NEK_HOSTDEVICE_INLINE EntropyStarState< TData > EntropyPressureStar(const TData vn, const TData cInt, const TData pInt, const TData rhoInt, const TData rhoBC, const TData pBC, const TData VnInf, const TData gamma, const TData gamM1, const TData twoOverGamM1)
Subsonic inflow holding the entropy and the pressure.
NEK_HOSTDEVICE_INLINE EntropyStarState< TData > EntropyVelocityStar(const TData vn, const TData cInt, const TData pInt, const TData rhoInt, const TData rhoBC, const TData pBC, const TData VnInf, const TData gamma, const TData gamM1, const TData twoOverGamM1)
Subsonic inflow holding the entropy and the velocity.
static void LoopExecutionSetStreamID(const unsigned int streamID)
scalarT< T > abs(scalarT< T > in)
Definition scalar.hpp:294
scalarT< T > sqrt(scalarT< T > in)
Definition scalar.hpp:289
The state an entropy Riemann boundary condition imposes at a point.