Nektar++
Loading...
Searching...
No Matches
Macros | Functions
MakePrismStackMesh.cpp File Reference
#include <cgnslib.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>

Go to the source code of this file.

Macros

#define NX   6
 
#define NY   6
 
#define NZ   4
 
#define NNODE   ((NX + 1) * (NY + 1) * (NZ + 1))
 
#define NTRI   (2 * NX * NY)
 
#define NPRISM   (NTRI * NZ)
 
#define PID(i, j, k)   (perm[gid((i), (j), (k)) - 1] + 1)
 

Functions

static void chk (int ier, const char *what)
 
static cgsize_t gid (int i, int j, int k)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

◆ NNODE

#define NNODE   ((NX + 1) * (NY + 1) * (NZ + 1))

Definition at line 75 of file MakePrismStackMesh.cpp.

◆ NPRISM

#define NPRISM   (NTRI * NZ)

Definition at line 77 of file MakePrismStackMesh.cpp.

◆ NTRI

#define NTRI   (2 * NX * NY)

Definition at line 76 of file MakePrismStackMesh.cpp.

◆ NX

#define NX   6

Definition at line 66 of file MakePrismStackMesh.cpp.

◆ NY

#define NY   6

Definition at line 69 of file MakePrismStackMesh.cpp.

◆ NZ

#define NZ   4

Definition at line 72 of file MakePrismStackMesh.cpp.

◆ PID

#define PID (   i,
  j,
  k 
)    (perm[gid((i), (j), (k)) - 1] + 1)

Function Documentation

◆ chk()

static void chk ( int  ier,
const char *  what 
)
static

Definition at line 79 of file MakePrismStackMesh.cpp.

80{
81 if (ier)
82 {
83 printf("FAILED: %s\n", what);
84 cg_error_print();
85 exit(1);
86 }
87}

Referenced by main().

◆ gid()

static cgsize_t gid ( int  i,
int  j,
int  k 
)
static

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 95 of file MakePrismStackMesh.cpp.

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}
#define NPRISM
static void chk(int ier, const char *what)
#define NZ
#define NNODE
#define NTRI
#define PID(i, j, k)
static double Z[NNODE]
std::vector< double > p(NPUPPER)

References chk(), NNODE, NPRISM, NTRI, NX, NY, NZ, PID, and Z.