Nektar++
Loading...
Searching...
No Matches
MakeTwistedPrismRing.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: MakeTwistedPrismRing.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 twisted_prism_ring.cgns and
32// twisted_prism_ring_p2.cgns, a ring of prisms closed back onto itself with a
33// third of a turn in it.
34//
35////////////////////////////////////////////////////////////////////////////////
36// A prism collapses onto one corner of its triangular cross-section -- that
37// is where the Duffy transformation of the modified C^0 basis sends it -- and
38// the two ends of the prism have to agree on which corner that is, as do two
39// prisms meeting at a triangular face. Walking round this ring the choice is
40// carried from one prism to the next, and the turn means it comes back one
41// corner along from where it started. No numbering of the mesh can mend that,
42// however it is done: the mesh simply has no representation as prisms.
43//
44// InputCGNS::ResetNodes therefore replaces the prism it cannot orient with
45// the elements got by coning each of its faces to a new node at its centre --
46// a tetrahedron on each of the two triangles, a pyramid on each of the three
47// quadrilaterals. Splitting it into three tetrahedra instead would be
48// smaller, but a tetrahedron cannot present the quadrilateral faces it shares
49// with its neighbours; coning keeps all five faces exactly as they were, so
50// the neighbours do not notice and the mesh stays conformal.
51//
52// The ring is written at first and at second order. The second-order one is
53// the sharper test: above first order the reader takes an element's nodes as
54// it finds them rather than rebuilding it from its faces, and a pyramid is
55// never reoriented, so the new elements have to be wound correctly when they
56// are made rather than put right afterwards.
57//
58// Built by utilities/NekMesh/CMakeLists.txt wherever CGNS is enabled, which
59// also adds a test that reruns this and checks the result against the
60// fixture committed beside it. To build it by hand instead:
61//
62// c++ MakeTwistedPrismRing.cpp -I<cgns>/include -L<cgns>/lib -lcgns -lm \
63// -o MakeTwistedPrismRing
64// ./MakeTwistedPrismRing twisted_prism_ring.cgns 1
65// ./MakeTwistedPrismRing twisted_prism_ring_p2.cgns 2
66//
67#include <cgnslib.h>
68#include <cmath>
69#include <cstdio>
70#include <cstdlib>
71
72#define N 12
73#define NV (3 * N)
74#define NNODE (9 * N)
75
76/* vertex (k,j); mid of section k's triangle edge j; mid of element k's
77 extrusion edge at corner j */
78#define V(k, j) ((k) * 3 + (j))
79#define TM(k, j) (NV + (k) * 3 + (j))
80#define AM(k, j) (NV + 3 * N + (k) * 3 + (j))
81
82static double X[NNODE], Y[NNODE], Z[NNODE];
83
84/* M_PI is a POSIX extension rather than standard C++, and this is built
85 without compiler extensions, so the constant is spelt out here. */
86static constexpr double kPi = 3.14159265358979323846;
87
88static void chk(int ier, const char *w)
89{
90 if (ier)
91 {
92 printf("FAILED: %s\n", w);
93 cg_error_print();
94 exit(1);
95 }
96}
97
98static void mid(int m, int a, int b)
99{
100 X[m] = 0.5 * (X[a] + X[b]);
101 Y[m] = 0.5 * (Y[a] + Y[b]);
102 Z[m] = 0.5 * (Z[a] + Z[b]);
103}
104
105int main(int argc, char **argv)
106{
107 if (argc != 3)
108 {
109 printf("usage: %s <out.cgns> <order 1|2>\n", argv[0]);
110 return 1;
111 }
112
113 const int order = atoi(argv[2]);
114 const double R = 3.0, r = 0.7;
115
116 for (int k = 0; k < N; ++k)
117 {
118 const double th = 2.0 * kPi * k / N;
119 const double cx = R * std::cos(th), cy = R * std::sin(th);
120 const double ux = std::cos(th), uy = std::sin(th);
121
122 for (int j = 0; j < 3; ++j)
123 {
124 /* the cross-section turns through a third over the whole ring */
125 const double a = 2.0 * kPi * j / 3.0 + (2.0 * kPi / 3.0) * k / N;
126 const int n = V(k, j);
127 X[n] = cx + r * std::cos(a) * ux;
128 Y[n] = cy + r * std::cos(a) * uy;
129 Z[n] = r * std::sin(a);
130 }
131 }
132
133 for (int k = 0; k < N; ++k)
134 {
135 const int nx = (k + 1) % N, sh = (k == N - 1) ? 1 : 0;
136 for (int j = 0; j < 3; ++j)
137 {
138 mid(TM(k, j), V(k, j), V(k, (j + 1) % 3));
139 mid(AM(k, j), V(k, j), V(nx, (j + sh) % 3));
140 }
141 }
142
143 const int nnode = (order == 2) ? NNODE : NV;
144
145 int fn, B, Z_, C, S;
146 chk(cg_open(argv[1], CG_MODE_WRITE, &fn), "open");
147 chk(cg_base_write(fn, "Base", 3, 3, &B), "base");
148
149 cgsize_t size[3] = {nnode, N, 0};
150 chk(cg_zone_write(fn, B, "fluid", size, Unstructured, &Z_), "zone");
151 chk(cg_coord_write(fn, B, Z_, RealDouble, "CoordinateX", X, &C), "x");
152 chk(cg_coord_write(fn, B, Z_, RealDouble, "CoordinateY", Y, &C), "y");
153 chk(cg_coord_write(fn, B, Z_, RealDouble, "CoordinateZ", Z, &C), "z");
154
155 /* The corners run the other way round from the order they were built in,
156 so that each prism comes out the right way out. */
157 const int np = (order == 2) ? 15 : 6;
158 static cgsize_t p[N * 15];
159
160 for (int k = 0; k < N; ++k)
161 {
162 const int nx = (k + 1) % N, sh = (k == N - 1) ? 1 : 0;
163 cgsize_t *e = p + k * np;
164
165 for (int j = 0; j < 3; ++j)
166 {
167 e[j] = V(k, (3 - j) % 3) + 1;
168 e[3 + j] = V(nx, ((3 - j) % 3 + sh) % 3) + 1;
169 }
170
171 if (order == 2)
172 {
173 e[6] = TM(k, 2) + 1;
174 e[7] = TM(k, 1) + 1;
175 e[8] = TM(k, 0) + 1;
176 e[9] = AM(k, 0) + 1;
177 e[10] = AM(k, 2) + 1;
178 e[11] = AM(k, 1) + 1;
179 e[12] = TM(nx, (2 + sh) % 3) + 1;
180 e[13] = TM(nx, (1 + sh) % 3) + 1;
181 e[14] = TM(nx, (0 + sh) % 3) + 1;
182 }
183 }
184
185 chk(cg_section_write(fn, B, Z_, "volume", (order == 2) ? PENTA_15 : PENTA_6,
186 1, N, 0, p, &S),
187 "penta");
188
189 /* Every quadrilateral face is on the outside of the ring; the triangular
190 ones are all interior, which is what makes the ring close. */
191 const int nq = (order == 2) ? 8 : 4;
192 static cgsize_t q[N * 3 * 8];
193 cgsize_t *w = q;
194
195 for (int k = 0; k < N; ++k)
196 {
197 const int nx = (k + 1) % N, sh = (k == N - 1) ? 1 : 0;
198 for (int j = 0; j < 3; ++j)
199 {
200 const int j2 = (j + 1) % 3;
201 *w++ = V(k, j) + 1;
202 *w++ = V(k, j2) + 1;
203 *w++ = V(nx, (j2 + sh) % 3) + 1;
204 *w++ = V(nx, (j + sh) % 3) + 1;
205
206 if (order == 2)
207 {
208 *w++ = TM(k, j) + 1;
209 *w++ = AM(k, j2) + 1;
210 *w++ = TM(nx, (j + sh) % 3) + 1;
211 *w++ = AM(k, j) + 1;
212 }
213 }
214 }
215
216 chk(cg_section_write(fn, B, Z_, "skin", (order == 2) ? QUAD_8 : QUAD_4,
217 N + 1, N + 3 * N, 0, q, &S),
218 "quad");
219 chk(cg_close(fn), "close");
220
221 printf("wrote %s: %d nodes, %d prisms, order %d\n", argv[1], nnode, N,
222 order);
223 (void)nq;
224 return 0;
225}
#define N
static double Z[NNODE]
static double Y[NNODE]
#define V(k, j)
#define AM(k, j)
#define NNODE
static void chk(int ier, const char *w)
static constexpr double kPi
#define TM(k, j)
static double X[NNODE]
static void mid(int m, int a, int b)
#define NV
Definition main.py:1