Nektar++
Loading...
Searching...
No Matches
ReOrientFaceKernel.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: ReOrientFaceKernel.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: Reorientation of face data between the element and trace frames
32//
33///////////////////////////////////////////////////////////////////////////////
34#pragma once
35
39
40#ifndef NEK_HOSTDEVICE_INLINE
41#define NEK_HOSTDEVICE_INLINE inline
42#endif
43
45{
46
47/**
48 * @brief Move face trace data between an element's local face ordering and
49 * the global trace ordering, applying the trace orientation.
50 *
51 * @p Forwards selects the direction the data travels:
52 *
53 * - `true` - @p in is the element's local face, @p out the global trace.
54 * This is the gather, from the element's local face layout
55 * to the shared trace layout.
56 * - `false` - @p in is the global trace, @p out the element's local face.
57 * This is the scatter, the inverse of the gather.
58 *
59 * The two are inverses of each other for the same @p orient, so a gather
60 * followed by a scatter returns the original data.
61 *
62 * Note that in both directions the routine is written as a *gather over its
63 * own output*: it walks the output points and fetches the corresponding
64 * input point for each. The index arithmetic below therefore runs opposite
65 * to the direction the data travels - with @p Forwards it computes, for a
66 * global point, which local point feeds it. That is worth keeping in mind
67 * when reading the cases, and it is the sense in which
68 * TraceFluxOpImpl::ComposeOrient() combines two orientations.
69 *
70 * @p orient describes how the local face sits on the global trace. For a
71 * face there are eight possibilities, the symmetries of a square: each of
72 * the two directions may be reversed, and the pair may be transposed. The
73 * enum orders them so that, counting from eDir1FwdDir1_Dir2FwdDir2, bit 0
74 * reverses trace direction 1, bit 1 reverses direction 0 and bit 2
75 * transposes the two.
76 *
77 * @param orient - Orientation of the local face relative to the global
78 * trace.
79 * @param nq0 - Points in local face direction 0.
80 * @param nq1 - Points in local face direction 1. A transposed
81 * orientation swaps the two on the global trace, so the
82 * caller must pass the *local* extents; see
83 * ReorientedFaceExtents().
84 * @param in - Source, of `nq0 * nq1` points.
85 * @param out - Destination, of `nq0 * nq1` points. Must not alias
86 * @p in; only checked in a debug build, as the assert
87 * has to compile out for the device.
88 * @param Forwards - Direction of travel, as above.
89 *
90 * @tparam APPEND Accumulate into @p out rather than overwrite it.
91 * @tparam NEGATE_INPUT Negate on the way through. Used to give the backward
92 * side of an interior trace the opposite normal
93 * without a second pass over the data.
94 */
95// Called from host code and from inside device kernels; its checks are
96// host-device asserts, live on the host and nothing in device code, where an
97// assert message cannot be built.
98template <bool APPEND, bool NEGATE_INPUT, typename TData>
100 const StdRegions::Orientation orient, const unsigned nq0,
101 const unsigned nq1, const TData *in, TData *out, bool Forwards)
102{
104 in != out, "This routine cannot use the same input and output");
105
106 // Input sign change if required.
107 TData sign = (NEGATE_INPUT) ? -1.0 : 1.0;
108
109 switch (orient)
110 {
111 case StdRegions::eDir1FwdDir1_Dir2FwdDir2: // used for Tris & Quads
112 {
113 // straight copy
114 if constexpr (APPEND)
115 {
116 for (unsigned i = 0; i < nq0 * nq1; ++i)
117 {
118 out[i] += sign * in[i];
119 }
120 }
121 else
122 {
123 for (unsigned i = 0; i < nq0 * nq1; ++i)
124 {
125 out[i] = sign * in[i];
126 }
127 }
128 }
129 break;
130 case StdRegions::eDir1BwdDir1_Dir2FwdDir2: // used for Tris & Quads
131 {
132 // Direction A negative and B positive
133 TData store;
134 unsigned nq0half = (nq0 + 1u) / 2;
135 if constexpr (APPEND)
136 {
137 for (unsigned j = 0; j < nq1; j++)
138 {
139 for (unsigned i = 0; i < nq0half; ++i)
140 {
141 store = sign * in[j * nq0 + i];
142 out[j * nq0 + i] += sign * in[j * nq0 + nq0 - 1u - i];
143 out[j * nq0 + nq0 - 1u - i] += store;
144 }
145 }
146 }
147 else
148 {
149 for (unsigned j = 0; j < nq1; j++)
150 {
151 for (unsigned i = 0; i < nq0half; ++i)
152 {
153 store = sign * in[j * nq0 + i];
154 out[j * nq0 + i] = sign * in[j * nq0 + nq0 - 1u - i];
155 out[j * nq0 + nq0 - 1u - i] = store;
156 }
157 }
158 }
159 }
160 break;
162 {
163 // Direction A positive and B negative
164 if constexpr (APPEND)
165 {
166 for (int j = 0; j < nq1; j++)
167 {
168 for (int i = 0; i < nq0; ++i)
169 {
170 out[j * nq0 + i] += sign * in[nq0 * (nq1 - 1u - j) + i];
171 }
172 }
173 }
174 else
175 {
176 for (int j = 0; j < nq1; j++)
177 {
178 for (int i = 0; i < nq0; ++i)
179 {
180 out[j * nq0 + i] = sign * in[nq0 * (nq1 - 1u - j) + i];
181 }
182 }
183 }
184 }
185 break;
187 {
188 // Direction A positive and B negative
189 if constexpr (APPEND)
190 {
191 for (int j = 0; j < nq1; j++)
192 {
193 for (int i = 0; i < nq0; ++i)
194 {
195 out[j * nq0 + i] +=
196 sign * in[nq0 * nq1 - 1u - j * nq0 - i];
197 }
198 }
199 }
200 else
201 {
202 for (int j = 0; j < nq1; j++)
203 {
204 for (int i = 0; i < nq0; ++i)
205 {
206 out[j * nq0 + i] =
207 sign * in[nq0 * nq1 - 1u - j * nq0 - i];
208 }
209 }
210 }
211 }
212 break;
214 {
215 // Transposed, Direction A and B positive
216 if constexpr (APPEND)
217 {
218 if (Forwards)
219 {
220 for (int j = 0; j < nq1; ++j)
221 {
222 for (int i = 0; i < nq0; ++i)
223 {
224 out[i * nq1 + j] += sign * in[i + j * nq0];
225 }
226 }
227 }
228 else // inverse case - different if nq0 != nq1
229 {
230 for (int j = 0; j < nq1; ++j)
231 {
232 for (int i = 0; i < nq0; ++i)
233 {
234 out[j * nq0 + i] += sign * in[i * nq1 + j];
235 }
236 }
237 }
238 }
239 else
240 {
241 if (Forwards)
242 {
243 for (int j = 0; j < nq1; ++j)
244 {
245 for (int i = 0; i < nq0; ++i)
246 {
247 out[i * nq1 + j] = sign * in[i + j * nq0];
248 }
249 }
250 }
251 else // inverse case - different if nq0 != nq1
252 {
253 for (int j = 0; j < nq1; ++j)
254 {
255 for (int i = 0; i < nq0; ++i)
256 {
257 out[j * nq0 + i] = sign * in[i * nq1 + j];
258 }
259 }
260 }
261 }
262 }
263 break;
265 {
266 if constexpr (APPEND)
267 {
268 if (Forwards)
269 {
270 // forward case (element to trace) or (loc trace to trace)
271 // Transposed, Direction A positive and B negative
272 for (int i = 0; i < nq0; ++i)
273 {
274 for (int j = 0; j < nq1; ++j)
275 {
276
277 out[i * nq1 + j] +=
278 sign * in[i + nq0 * (nq1 - 1u) - j * nq0];
279 }
280 }
281 }
282 else
283 {
284 // inverse case (trace to element)
285 // Transposed, Direction A positive and B negative
286 for (int j = 0; j < nq1; ++j)
287 {
288 for (int i = 0; i < nq0; ++i)
289 {
290 out[j * nq0 + i] +=
291 sign * in[nq1 - 1u - j + i * nq1];
292 }
293 }
294 }
295 }
296 else
297 {
298 if (Forwards)
299 {
300 // forward case (element to trace) or (loc trace to trace)
301 // Transposed, Direction A positive and B negative
302 for (int i = 0; i < nq0; ++i)
303 {
304 for (int j = 0; j < nq1; ++j)
305 {
306
307 out[i * nq1 + j] =
308 sign * in[i + nq0 * (nq1 - 1u) - j * nq0];
309 }
310 }
311 }
312 else
313 {
314 // inverse case (trace to element)
315 // Transposed, Direction A positive and B negative
316 for (int j = 0; j < nq1; ++j)
317 {
318 for (int i = 0; i < nq0; ++i)
319 {
320 out[j * nq0 + i] =
321 sign * in[nq1 - 1u - j + i * nq1];
322 }
323 }
324 }
325 }
326 }
327 break;
329 {
330 // Transposed, Direction A negative and B positive
331 if constexpr (APPEND)
332 {
333 if (Forwards)
334 {
335 for (int i = 0; i < nq0; ++i)
336 {
337 for (int j = 0; j < nq1; ++j)
338 {
339 out[i * nq1 + j] +=
340 sign * in[nq0 - 1u - i + j * nq0];
341 }
342 }
343 }
344 else
345 {
346 for (int j = 0; j < nq1; ++j)
347 {
348 for (int i = 0; i < nq0; ++i)
349 {
350 out[i + j * nq0] +=
351 sign * in[nq1 * (nq0 - 1u) - i * nq1 + j];
352 }
353 }
354 }
355 }
356 else
357 {
358 if (Forwards)
359 {
360 for (int i = 0; i < nq0; ++i)
361 {
362 for (int j = 0; j < nq1; ++j)
363 {
364 out[i * nq1 + j] =
365 sign * in[nq0 - 1u - i + j * nq0];
366 }
367 }
368 }
369 else
370 {
371 for (int j = 0; j < nq1; ++j)
372 {
373 for (int i = 0; i < nq0; ++i)
374 {
375 out[i + j * nq0] =
376 sign * in[nq1 * (nq0 - 1u) - i * nq1 + j];
377 }
378 }
379 }
380 }
381 }
382 break;
384 {
385 // Transposed, Direction A and B negative
386 if constexpr (APPEND)
387 {
388 if (Forwards)
389 {
390 for (int i = 0; i < nq0; ++i)
391 {
392 for (int j = 0; j < nq1; ++j)
393 {
394 out[i * nq1 + j] +=
395 sign * in[nq0 * nq1 - 1u - i - j * nq0];
396 }
397 }
398 }
399 else // inverse case - different if nq0 != nq1
400 {
401 for (int j = 0; j < nq1; ++j)
402 {
403 for (int i = 0; i < nq0; ++i)
404 {
405 out[i + j * nq0] +=
406 sign * in[nq0 * nq1 - 1u - j - i * nq1];
407 }
408 }
409 }
410 }
411 else
412 {
413 if (Forwards)
414 {
415 for (int i = 0; i < nq0; ++i)
416 {
417 for (int j = 0; j < nq1; ++j)
418 {
419 out[i * nq1 + j] =
420 sign * in[nq0 * nq1 - 1u - i - j * nq0];
421 }
422 }
423 }
424 else // inverse case - different if nq0 != nq1
425 {
426 for (int j = 0; j < nq1; ++j)
427 {
428 for (int i = 0; i < nq0; ++i)
429 {
430 out[i + j * nq0] =
431 sign * in[nq0 * nq1 - 1u - j - i * nq1];
432 }
433 }
434 }
435 }
436 }
437 break;
438 default:
439 NEK_HOSTDEVICE_ASSERTL1(false, "Unknown orientation");
440 break;
441 }
442}
443
444} // namespace Nektar::LocalRegions
#define NEK_HOSTDEVICE_INLINE
Definition Backends.hpp:268
#define NEK_HOSTDEVICE_ASSERTL1(condition, msg)
#define sign(a, b)
return the sign(b)*a
Definition Polylib.cpp:47
static NEK_HOSTDEVICE_INLINE void ReOrientFaceKernel(const StdRegions::Orientation orient, const unsigned nq0, const unsigned nq1, const TData *in, TData *out, bool Forwards)
Move face trace data between an element's local face ordering and the global trace ordering,...