Nektar++
Loading...
Searching...
No Matches
HOAlignment.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: HOAlignment.h
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: HO aligment routines.
32//
33////////////////////////////////////////////////////////////////////////////////
34
35#ifndef NEKTAR_SPATIALDOMAINS_HOALIGNMENT
36#define NEKTAR_SPATIALDOMAINS_HOALIGNMENT
37
40#include <array>
41#include <map>
42
44{
45/**
46 * @brief A lightweight struct for dealing with high-order triangle
47 * alignment.
48 *
49 * The logic underlying these routines is taken from the original Nektar
50 * code.
51 */
52template <typename T> struct HOTriangle
53{
54 HOTriangle(std::vector<int> pVertId, std::vector<T> pSurfVerts)
55 : vertId(pVertId), surfVerts(pSurfVerts)
56 {
57 }
58 HOTriangle(std::vector<int> pVertId) : vertId(pVertId)
59 {
60 }
61
62 /// The triangle vertex IDs
63 std::vector<int> vertId;
64
65 /// The triangle surface vertices -- templated so that this can
66 /// either be nodes or IDs.
67 std::vector<T> surfVerts;
68
69 /**
70 * @brief Rotates the triangle of data points inside #surfVerts
71 * counter-clockwise nrot times.
72 *
73 * @param nrot Number of times to rotate triangle.
74 */
75 void Rotate(int nrot)
76 {
77 int n, i, j, cnt;
78 int np = ((int)sqrt(8.0 * surfVerts.size() + 1.0) - 1) / 2;
79 std::vector<T> tmp(np * np);
80
81 for (n = 0; n < nrot; ++n)
82 {
83 for (cnt = i = 0; i < np; ++i)
84 {
85 for (j = 0; j < np - i; ++j, cnt++)
86 {
87 tmp[i * np + j] = surfVerts[cnt];
88 }
89 }
90 for (cnt = i = 0; i < np; ++i)
91 {
92 for (j = 0; j < np - i; ++j, cnt++)
93 {
94 surfVerts[cnt] = tmp[(np - 1 - i - j) * np + i];
95 }
96 }
97 }
98 }
99
100 /**
101 * @brief Reflect data points inside #surfVerts.
102 *
103 * This applies a mapping essentially doing the following
104 * reordering:
105 *
106 * 9 9
107 * 7 8 -> 8 7
108 * 4 5 6 6 5 4
109 * 0 1 2 3 3 2 1 0
110 */
111 void Reflect()
112 {
113 int i, j, cnt;
114 int np = ((int)sqrt(8.0 * surfVerts.size() + 1.0) - 1) / 2;
115 std::vector<T> tmp(np * np);
116
117 for (cnt = i = 0; i < np; ++i)
118 {
119 for (j = 0; j < np - i; ++j, cnt++)
120 {
121 tmp[i * np + np - i - 1 - j] = surfVerts[cnt];
122 }
123 }
124
125 for (cnt = i = 0; i < np; ++i)
126 {
127 for (j = 0; j < np - i; ++j, cnt++)
128 {
129 surfVerts[cnt] = tmp[i * np + j];
130 }
131 }
132 }
133
134 /**
135 * @brief Align this surface to a given vertex ID.
136 */
137 void Align(std::vector<int> vertId)
138 {
139 if (vertId[0] == this->vertId[0])
140 {
141 if (vertId[1] == this->vertId[1] || vertId[1] == this->vertId[2])
142 {
143 if (vertId[1] == this->vertId[2])
144 {
145 Rotate(1);
146 Reflect();
147 }
148 }
149 }
150 else if (vertId[0] == this->vertId[1])
151 {
152 if (vertId[1] == this->vertId[0] || vertId[1] == this->vertId[2])
153 {
154 if (vertId[1] == this->vertId[0])
155 {
156 Reflect();
157 }
158 else
159 {
160 Rotate(2);
161 }
162 }
163 }
164 else if (vertId[0] == this->vertId[2])
165 {
166 if (vertId[1] == this->vertId[0] || vertId[1] == this->vertId[1])
167 {
168 if (vertId[1] == this->vertId[1])
169 {
170 Rotate(2);
171 Reflect();
172 }
173 else
174 {
175 Rotate(1);
176 }
177 }
178 }
179 }
180};
181
182/**
183 * @brief A lightweight struct for dealing with high-order quadrilateral
184 * alignment.
185 */
186template <typename T> struct HOQuadrilateral
187{
188 HOQuadrilateral(std::vector<int> pVertId, std::vector<T> pSurfVerts)
189 : vertId(pVertId), surfVerts(pSurfVerts)
190 {
191 }
192
193 HOQuadrilateral(std::vector<int> pVertId) : vertId(pVertId)
194 {
195 }
196
197 /// The quadrilateral vertex IDs
198 std::vector<int> vertId;
199
200 /// The quadrilateral surface vertices -- templated so that this can either
201 /// be nodes or IDs.
202 std::vector<T> surfVerts;
203
204 void ReverseX()
205 {
206 int np = (int)(sqrt((NekDouble)surfVerts.size()) + 0.5);
207 for (int i = 0; i < np; ++i)
208 {
209 for (int j = 0; j < np / 2; ++j)
210 {
211 std::swap(surfVerts[i * np + j],
212 surfVerts[i * np + np - j - 1]);
213 }
214 }
215 }
216
217 void ReverseY()
218 {
219 int np = (int)(sqrt((NekDouble)surfVerts.size()) + 0.5);
220 // Reverse y direction
221 for (int j = 0; j < np; ++j)
222 {
223 for (int i = 0; i < np / 2; ++i)
224 {
225 std::swap(surfVerts[i * np + j],
226 surfVerts[(np - i - 1) * np + j]);
227 }
228 }
229 }
230
232 {
233 int np = (int)(sqrt((NekDouble)surfVerts.size()) + 0.5);
234 std::vector<T> tmp(surfVerts.size());
235
236 for (int i = 0; i < np; ++i)
237 {
238 for (int j = 0; j < np; ++j)
239 {
240 tmp[i * np + j] = surfVerts[j * np + i];
241 }
242 }
243
244 surfVerts = tmp;
245 }
246
247 /**
248 * @brief Align this surface to a given vertex ID.
249 */
250 void Align(std::vector<int> vertId)
251 {
252
253 int vmap[4] = {-1, -1, -1, -1};
254
255 // Determine which vertices map to vertId
256 for (int i = 0; i < 4; ++i)
257 {
258 for (int j = 0; j < 4; ++j)
259 {
260 if (this->vertId[j] == vertId[i])
261 {
262 vmap[i] = j;
263 break;
264 }
265 }
266
267 ASSERTL1(vmap[i] != -1,
268 "Could not determine mapping between vertex IDs");
269 }
270
272
273 if (vmap[1] == (vmap[0] + 1) % 4)
274 {
275 switch (vmap[0])
276 {
277 case 0:
279 break;
280 case 1:
282 break;
283 case 2:
285 break;
286 case 3:
288 break;
289 }
290 }
291 else
292 {
293 switch (vmap[0])
294 {
295 case 0:
297 break;
298 case 1:
300 break;
301 case 2:
303 break;
304 case 3:
306 break;
307 }
308 }
309
314 {
315 Transpose();
316 }
317
322 {
323 ReverseX();
324 }
325
330 {
331 ReverseY();
332 }
333 }
334};
335
336/**
337 * @brief The interior nodes of a high-order tetrahedron, and the vertices they
338 * are positioned against.
339 *
340 * The counterpart of HOTriangle for the inside of an element rather than a
341 * face. A tetrahedron's nodes are a nodal distribution rather than a grid, so
342 * there is no axis to rotate about and no pair of directions to reflect; what
343 * there is instead is each node's barycentric position, how much of each of the
344 * four vertices it is made of. Those weights belong to the vertices themselves
345 * rather than to any numbering of them, so relabelling the vertices is enough
346 * to say where every node goes.
347 *
348 * Nothing here evaluates a point. Aligning is a permutation of the nodes
349 * already held, so a node read from a file keeps the position the file gave it.
350 */
351template <typename T> struct HOTetrahedron
352{
353 /**
354 * @param pVertId The four vertex IDs these nodes are positioned against.
355 * @param pVolVerts The nodes, in the ordering a nodal tetrahedron puts
356 * them in.
357 * @param pNumPts Points along an edge of the element the nodes came
358 * from, when @p pVolVerts holds every node of it. Leave
359 * this out when it holds only the interior: the two
360 * cannot be told apart by counting, since the interior of
361 * one order has as many nodes as the whole of another.
362 */
363 HOTetrahedron(std::vector<int> pVertId, std::vector<T> pVolVerts,
364 int pNumPts = 0)
365 : vertId(pVertId), volVerts(pVolVerts), numPts(pNumPts)
366 {
367 }
368 HOTetrahedron(std::vector<int> pVertId) : vertId(pVertId)
369 {
370 }
371
372 /// The tetrahedron vertex IDs.
373 std::vector<int> vertId;
374
375 /// The interior nodes, in the ordering a nodal tetrahedron of this order
376 /// puts them in -- templated so that this can be either nodes or IDs.
377 std::vector<T> volVerts;
378
379 /// Points along an edge of the element, or zero if #volVerts is only its
380 /// interior and the order should be worked out from that.
381 int numPts = 0;
382
383 /**
384 * @brief Reorder #volVerts into the frame in which the vertices are
385 * numbered as @p vertId.
386 *
387 * @param vertId The vertex IDs to align to, in the desired order.
388 */
389 void Align(std::vector<int> vertId)
390 {
391 // One node sits at the centre and any number below that is no node at
392 // all; either way there is nothing a relabelling could move.
393 if (volVerts.size() < 2)
394 {
395 return;
396 }
397
398 // Which of the stored vertices each of the wanted ones is.
399 std::array<int, 4> vmap = {-1, -1, -1, -1};
400 for (int i = 0; i < 4; ++i)
401 {
402 for (int j = 0; j < 4; ++j)
403 {
404 if (vertId[i] == this->vertId[j])
405 {
406 vmap[i] = j;
407 break;
408 }
409 }
410 ASSERTL0(vmap[i] != -1, "Tetrahedron vertices do not match.");
411 }
412
413 const int nInt = volVerts.size();
414
415 // Recover the order. Told the element's own size, the nodes are all of
416 // it; otherwise they are the interior of an element with np points
417 // along each edge, of which there are (np-4)(np-3)(np-2)/6.
418 int np = numPts;
419 if (np == 0)
420 {
421 np = 4;
422 while ((np - 4) * (np - 3) * (np - 2) / 6 < nInt)
423 {
424 ++np;
425 }
426 ASSERTL0((np - 4) * (np - 3) * (np - 2) / 6 == nInt,
427 "Number of interior nodes is not a tetrahedral number.");
428 }
429
430 const int nTot = np * (np + 1) * (np + 2) / 6;
431 ASSERTL0(nInt <= nTot, "More nodes than the element can hold.");
432
433 // Where in the distribution the nodes we were given start: the
434 // interior is its tail, and a whole element is all of it.
435 const int first = nTot - nInt;
436
439 LibUtilities::PointsManager()[pKey]->GetPoints(px, py, pz);
440
441 // Barycentric weights of a node, as whole numbers of 1/(np-1) so that
442 // they can be compared exactly.
443 auto weights = [&](int m) {
444 std::array<int, 4> w;
445 w[1] = (int)std::lround(0.5 * (1.0 + px[m]) * (np - 1));
446 w[2] = (int)std::lround(0.5 * (1.0 + py[m]) * (np - 1));
447 w[3] = (int)std::lround(0.5 * (1.0 + pz[m]) * (np - 1));
448 w[0] = (np - 1) - w[1] - w[2] - w[3];
449 return w;
450 };
451 auto key = [np](const std::array<int, 4> &w) {
452 return ((w[0] * np + w[1]) * np + w[2]) * np + w[3];
453 };
454
455 std::map<int, int> slotOf;
456 for (int s = 0; s < nInt; ++s)
457 {
458 slotOf[key(weights(first + s))] = s;
459 }
460
461 std::vector<T> tmp(nInt);
462 for (int s = 0; s < nInt; ++s)
463 {
464 // The same mix of the same four corners, with the labels moved.
465 std::array<int, 4> want = weights(first + s), have{};
466 for (int i = 0; i < 4; ++i)
467 {
468 have[vmap[i]] = want[i];
469 }
470
471 auto it = slotOf.find(key(have));
472 ASSERTL0(it != slotOf.end(), "Could not place a tetrahedron node.");
473 tmp[s] = volVerts[it->second];
474 }
475
476 volVerts = tmp;
477 }
478};
479
480/**
481 * @brief The nodes of a high-order prism, and the vertices they sit against.
482 *
483 * A prism is a triangle extruded along an axis, and the only way one is ever
484 * reoriented is by turning that triangle: NekMesh's OrientPrism leaves the
485 * element unrotated or spins it one way or the other, and either way both
486 * triangular faces turn together and the axis is left alone. So a node is
487 * placed by how much of each corner of the triangle it is made of, carried
488 * along at whatever height up the axis it already had.
489 *
490 * As with HOTetrahedron nothing is evaluated: aligning permutes the nodes that
491 * are already held.
492 */
493template <typename T> struct HOPrism
494{
495 /**
496 * @param pVertId The six vertex IDs, in the standard element's order.
497 * @param pVolVerts Every node of the element, in the ordering a nodal
498 * prism puts them in.
499 * @param pNumPts Points along an edge of the element.
500 */
501 HOPrism(std::vector<int> pVertId, std::vector<T> pVolVerts, int pNumPts)
502 : vertId(pVertId), volVerts(pVolVerts), numPts(pNumPts)
503 {
504 }
505
506 /// The prism vertex IDs.
507 std::vector<int> vertId;
508
509 /// The nodes, in nodal prism ordering.
510 std::vector<T> volVerts;
511
512 /// Points along an edge of the element.
514
515 /**
516 * @brief Turn the triangular cross-section @p nrot thirds of a turn.
517 */
518 void Rotate(int nrot)
519 {
520 nrot = ((nrot % 3) + 3) % 3;
521 if (nrot == 0 || volVerts.empty())
522 {
523 return;
524 }
525
526 const int np = numPts;
527 ASSERTL0(np * (np + 1) / 2 * np == (int)volVerts.size(),
528 "Number of nodes is not a prism number.");
529
532 LibUtilities::PointsManager()[pKey]->GetPoints(px, py, pz);
533
534 // Where a node sits: how much of each corner of the triangular
535 // cross-section it is made of, and how far along the axis it is. The
536 // triangle lies in the first and third directions, the axis is the
537 // second.
538 auto place = [&](int m) {
539 std::array<int, 4> w;
540 w[1] = (int)std::lround(0.5 * (1.0 + px[m]) * (np - 1));
541 w[2] = (int)std::lround(0.5 * (1.0 + pz[m]) * (np - 1));
542 w[0] = (np - 1) - w[1] - w[2];
543 w[3] = (int)std::lround(0.5 * (1.0 + py[m]) * (np - 1));
544 return w;
545 };
546 auto key = [np](const std::array<int, 4> &w) {
547 return ((w[0] * np + w[1]) * np + w[2]) * np + w[3];
548 };
549
550 std::map<int, int> slotOf;
551 for (size_t m = 0; m < volVerts.size(); ++m)
552 {
553 slotOf[key(place(m))] = m;
554 }
555
556 std::vector<T> tmp(volVerts.size());
557 for (size_t m = 0; m < volVerts.size(); ++m)
558 {
559 // Turning the triangle moves each corner's share round to the
560 // next; the height up the axis is untouched.
561 std::array<int, 4> want = place(m), have{};
562 for (int c = 0; c < 3; ++c)
563 {
564 have[(c + nrot) % 3] = want[c];
565 }
566 have[3] = want[3];
567
568 auto it = slotOf.find(key(have));
569 ASSERTL0(it != slotOf.end(), "Could not place a prism node.");
570 tmp[m] = volVerts[it->second];
571 }
572
573 volVerts = tmp;
574 }
575
576 /**
577 * @brief Turn the prism so that its vertices read as @p vertId.
578 */
579 void Align(std::vector<int> vertId)
580 {
581 // The corners of the triangular face, in the standard element's
582 // numbering. Whichever of them the wanted first vertex is says how far
583 // the triangle has been turned.
584 static const int triVerts[3] = {0, 1, 4};
585
586 for (int r = 0; r < 3; ++r)
587 {
588 if (vertId[0] == this->vertId[triVerts[r]])
589 {
590 Rotate(r);
591 return;
592 }
593 }
594
595 ASSERTL0(false, "Prism vertices do not match.");
596 }
597};
598
599} // namespace Nektar::SpatialDomains
600#endif
#define ASSERTL0(condition, msg)
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Defines a specification for a set of points.
Definition Points.h:50
PointsManagerT & PointsManager(void)
@ eNodalPrismEvenlySpaced
3D Evenly-spaced points on a Prism
Definition PointsType.h:86
@ eNodalTetEvenlySpaced
3D Evenly-spaced points on a Tetrahedron
Definition PointsType.h:84
scalarT< T > sqrt(scalarT< T > in)
Definition scalar.hpp:289
The nodes of a high-order prism, and the vertices they sit against.
std::vector< T > volVerts
The nodes, in nodal prism ordering.
std::vector< int > vertId
The prism vertex IDs.
int numPts
Points along an edge of the element.
void Rotate(int nrot)
Turn the triangular cross-section nrot thirds of a turn.
void Align(std::vector< int > vertId)
Turn the prism so that its vertices read as vertId.
HOPrism(std::vector< int > pVertId, std::vector< T > pVolVerts, int pNumPts)
A lightweight struct for dealing with high-order quadrilateral alignment.
void Align(std::vector< int > vertId)
Align this surface to a given vertex ID.
std::vector< T > surfVerts
The quadrilateral surface vertices – templated so that this can either be nodes or IDs.
HOQuadrilateral(std::vector< int > pVertId, std::vector< T > pSurfVerts)
std::vector< int > vertId
The quadrilateral vertex IDs.
HOQuadrilateral(std::vector< int > pVertId)
The interior nodes of a high-order tetrahedron, and the vertices they are positioned against.
void Align(std::vector< int > vertId)
Reorder volVerts into the frame in which the vertices are numbered as vertId.
std::vector< int > vertId
The tetrahedron vertex IDs.
HOTetrahedron(std::vector< int > pVertId)
HOTetrahedron(std::vector< int > pVertId, std::vector< T > pVolVerts, int pNumPts=0)
std::vector< T > volVerts
The interior nodes, in the ordering a nodal tetrahedron of this order puts them in – templated so tha...
int numPts
Points along an edge of the element, or zero if volVerts is only its interior and the order should be...
A lightweight struct for dealing with high-order triangle alignment.
Definition HOAlignment.h:53
void Reflect()
Reflect data points inside surfVerts.
std::vector< int > vertId
The triangle vertex IDs.
Definition HOAlignment.h:63
HOTriangle(std::vector< int > pVertId)
Definition HOAlignment.h:58
std::vector< T > surfVerts
The triangle surface vertices – templated so that this can either be nodes or IDs.
Definition HOAlignment.h:67
void Rotate(int nrot)
Rotates the triangle of data points inside surfVerts counter-clockwise nrot times.
Definition HOAlignment.h:75
void Align(std::vector< int > vertId)
Align this surface to a given vertex ID.
HOTriangle(std::vector< int > pVertId, std::vector< T > pSurfVerts)
Definition HOAlignment.h:54