Nektar++
Loading...
Searching...
No Matches
MakePrismStackMesh.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: MakePrismStackMesh.cpp
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: Generates prism_stack.cgns: a boundary-layer-like block of
32// prisms.
33//
34////////////////////////////////////////////////////////////////////////////////
35// An irregular triangulation of a square -- each grid cell split by whichever
36// diagonal its parity picks, so that neighbouring blocks of prisms do not all
37// present their corners in the same order -- extruded into NZ layers of
38// PENTA_6 elements, with the bounding surfaces as named sections.
39//
40// The point of the mesh is the prism topology rather than the geometry. The
41// reader rebuilds each element from its faces instead of from its stored node
42// order, so what it produces depends on how the nodes happen to be numbered,
43// and InputCGNS::ResetNodes has to renumber them so that every prism in a
44// stack collapses onto the same corner of its triangular cross-section. A
45// stack several layers deep whose cross-sections are not all oriented alike
46// is what makes that pass do any work: the node numbering is permuted by a
47// seed so a numbering can be chosen that the pass has to repair.
48//
49// The permutation is a Fisher-Yates shuffle driven by the linear congruential
50// generator below, so a given seed reproduces a given numbering anywhere.
51//
52// Built by utilities/NekMesh/CMakeLists.txt wherever CGNS is enabled, which
53// also adds a test that reruns this and checks the result against the
54// fixture committed beside it. To build it by hand instead:
55//
56// c++ MakePrismStackMesh.cpp -I<cgns>/include -L<cgns>/lib -lcgns \
57// -o MakePrismStackMesh
58// ./MakePrismStackMesh prism_stack.cgns <seed>
59//
60#include <cgnslib.h>
61#include <cstdio>
62#include <cstdlib>
63#include <cstring>
64
65#ifndef NX
66#define NX 6
67#endif
68#ifndef NY
69#define NY 6
70#endif
71#ifndef NZ
72#define NZ 4
73#endif
74
75#define NNODE ((NX + 1) * (NY + 1) * (NZ + 1))
76#define NTRI (2 * NX * NY)
77#define NPRISM (NTRI * NZ)
78
79static void chk(int ier, const char *what)
80{
81 if (ier)
82 {
83 printf("FAILED: %s\n", what);
84 cg_error_print();
85 exit(1);
86 }
87}
88
89/* One-based node id of grid point (i, j, k), before permutation. */
90static cgsize_t gid(int i, int j, int k)
91{
92 return (cgsize_t)(k * (NX + 1) * (NY + 1) + j * (NX + 1) + i) + 1;
93}
94
95int main(int argc, char **argv)
96{
97 if (argc < 2 || argc > 3)
98 {
99 printf("usage: %s <out.cgns> [seed]\n", argv[0]);
100 return 1;
101 }
102
103 const unsigned long seed = (argc == 3) ? strtoul(argv[2], nullptr, 10) : 0;
104
105 /* perm[i] is the zero-based slot the node in slot i moves to. */
106 cgsize_t perm[NNODE];
107 for (int i = 0; i < NNODE; ++i)
108 {
109 perm[i] = i;
110 }
111
112 if (seed)
113 {
114 unsigned long state = seed;
115 for (int i = NNODE - 1; i > 0; --i)
116 {
117 state = state * 6364136223846793005UL + 1442695040888963407UL;
118 int j = (int)((state >> 33) % (unsigned long)(i + 1));
119 cgsize_t t = perm[i];
120 perm[i] = perm[j];
121 perm[j] = t;
122 }
123 }
124
125#define PID(i, j, k) (perm[gid((i), (j), (k)) - 1] + 1)
126
127 double x[NNODE], y[NNODE], z[NNODE];
128 for (int k = 0; k <= NZ; ++k)
129 {
130 for (int j = 0; j <= NY; ++j)
131 {
132 for (int i = 0; i <= NX; ++i)
133 {
134 cgsize_t n = PID(i, j, k) - 1;
135 x[n] = (double)i / NX;
136 y[n] = (double)j / NY;
137 /* Graded in z, as a boundary layer is. */
138 z[n] = 0.25 * ((double)k / NZ) * ((double)k / NZ);
139 }
140 }
141 }
142
143 /* The two triangles of cell (i, j), as grid-point offsets, wound so the
144 extruded prism comes out right-handed. */
145 static int tri[NTRI][3][2];
146 int nt = 0;
147 for (int j = 0; j < NY; ++j)
148 {
149 for (int i = 0; i < NX; ++i)
150 {
151 if ((i + j) % 2 == 0)
152 {
153 int a[3][2] = {{0, 0}, {1, 0}, {1, 1}};
154 int b[3][2] = {{0, 0}, {1, 1}, {0, 1}};
155 memcpy(tri[nt++], a, sizeof(a));
156 memcpy(tri[nt++], b, sizeof(b));
157 }
158 else
159 {
160 int a[3][2] = {{0, 0}, {1, 0}, {0, 1}};
161 int b[3][2] = {{1, 0}, {1, 1}, {0, 1}};
162 memcpy(tri[nt++], a, sizeof(a));
163 memcpy(tri[nt++], b, sizeof(b));
164 }
165 tri[nt - 2][0][0] += i;
166 tri[nt - 2][1][0] += i;
167 tri[nt - 2][2][0] += i;
168 tri[nt - 2][0][1] += j;
169 tri[nt - 2][1][1] += j;
170 tri[nt - 2][2][1] += j;
171 tri[nt - 1][0][0] += i;
172 tri[nt - 1][1][0] += i;
173 tri[nt - 1][2][0] += i;
174 tri[nt - 1][0][1] += j;
175 tri[nt - 1][1][1] += j;
176 tri[nt - 1][2][1] += j;
177 }
178 }
179
180 int fn, B, Z, S;
181 chk(cg_open(argv[1], CG_MODE_WRITE, &fn), "open");
182 chk(cg_base_write(fn, "Base", 3, 3, &B), "base_write");
183
184 cgsize_t size[3] = {NNODE, NPRISM, 0};
185 chk(cg_zone_write(fn, B, "fluid", size, Unstructured, &Z), "zone_write");
186
187 int C;
188 chk(cg_coord_write(fn, B, Z, RealDouble, "CoordinateX", x, &C), "x");
189 chk(cg_coord_write(fn, B, Z, RealDouble, "CoordinateY", y, &C), "y");
190 chk(cg_coord_write(fn, B, Z, RealDouble, "CoordinateZ", z, &C), "z");
191
192 /* Volume: NZ layers of prisms over every triangle.
193
194 Each prism's corners are cyclically rotated by an amount the seed picks.
195 That is the same element written a different way round, and a mesher is
196 free to write it either way, but it moves which face the reader meets
197 first -- and the reader anchors the element it rebuilds on the first
198 triangular and first quadrilateral face it finds. Without this the
199 stacked prisms all present their corners alike and the alignment pass
200 has nothing to repair. */
201 unsigned long rstate = seed ? seed : 1;
202 static cgsize_t penta[NPRISM * 6];
203 cgsize_t *p = penta;
204 for (int k = 0; k < NZ; ++k)
205 {
206 for (int t = 0; t < NTRI; ++t)
207 {
208 rstate = rstate * 6364136223846793005UL + 1442695040888963407UL;
209 int rot = (int)((rstate >> 33) % 3);
210
211 for (int v = 0; v < 3; ++v)
212 {
213 *p++ =
214 PID(tri[t][(v + rot) % 3][0], tri[t][(v + rot) % 3][1], k);
215 }
216 for (int v = 0; v < 3; ++v)
217 {
218 *p++ = PID(tri[t][(v + rot) % 3][0], tri[t][(v + rot) % 3][1],
219 k + 1);
220 }
221 }
222 }
223 chk(cg_section_write(fn, B, Z, "volume", PENTA_6, 1, NPRISM, 0, penta, &S),
224 "penta");
225
226 cgsize_t next = NPRISM + 1;
227
228 /* The two triangulated surfaces. */
229 static cgsize_t surf[NTRI * 3];
230 const char *tnames[2] = {"wall", "farfield"};
231 const int tlayer[2] = {0, NZ};
232 for (int s = 0; s < 2; ++s)
233 {
234 p = surf;
235 for (int t = 0; t < NTRI; ++t)
236 {
237 for (int v = 0; v < 3; ++v)
238 {
239 *p++ = PID(tri[t][v][0], tri[t][v][1], tlayer[s]);
240 }
241 }
242 chk(cg_section_write(fn, B, Z, tnames[s], TRI_3, next, next + NTRI - 1,
243 0, surf, &S),
244 "tri");
245 next += NTRI;
246 }
247
248 /* The four sides, each a strip of quads. */
249 const char *qnames[4] = {"ymin", "xmax", "ymax", "xmin"};
250 static cgsize_t quad[4 * (NX > NY ? NX : NY) * NZ * 4];
251 for (int s = 0; s < 4; ++s)
252 {
253 p = quad;
254 int nq = 0;
255 int lim = (s % 2 == 0) ? NX : NY;
256 for (int k = 0; k < NZ; ++k)
257 {
258 for (int m = 0; m < lim; ++m)
259 {
260 int i0, j0, i1, j1;
261 switch (s)
262 {
263 case 0:
264 i0 = m;
265 j0 = 0;
266 i1 = m + 1;
267 j1 = 0;
268 break;
269 case 1:
270 i0 = NX;
271 j0 = m;
272 i1 = NX;
273 j1 = m + 1;
274 break;
275 case 2:
276 i0 = m + 1;
277 j0 = NY;
278 i1 = m;
279 j1 = NY;
280 break;
281 default:
282 i0 = 0;
283 j0 = m + 1;
284 i1 = 0;
285 j1 = m;
286 break;
287 }
288 *p++ = PID(i0, j0, k);
289 *p++ = PID(i1, j1, k);
290 *p++ = PID(i1, j1, k + 1);
291 *p++ = PID(i0, j0, k + 1);
292 ++nq;
293 }
294 }
295 chk(cg_section_write(fn, B, Z, qnames[s], QUAD_4, next, next + nq - 1,
296 0, quad, &S),
297 "quad");
298 next += nq;
299 }
300
301 chk(cg_close(fn), "close");
302 printf("wrote %s: %d nodes, %d prisms (seed %lu)\n", argv[1], NNODE, NPRISM,
303 seed);
304 return 0;
305}
static cgsize_t gid(int i, int j, int k)
#define NPRISM
static void chk(int ier, const char *what)
#define NX
#define NY
#define NZ
#define NNODE
#define NTRI
#define PID(i, j, k)
static double Z[NNODE]
Definition main.py:1