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

Go to the source code of this file.

Macros

#define NX   4
 
#define NY   2
 
#define NZ   2
 

Functions

static void chk (int ier, const char *what)
 
static int vid (int ix0, int ix1, int i, int j, int k)
 
static void writeZone (int fn, int B, const char *zname, int ix0, int ix1, int cutLo, int cutHi, cgsize_t *ifaceRange)
 
int main (int, char **argv)
 

Variables

static const int faceOf [6][4][3]
 
static const char * faceName [6]
 

Macro Definition Documentation

◆ NX

#define NX   4

Definition at line 66 of file MakeTwoZoneMesh.cpp.

◆ NY

#define NY   2

Definition at line 67 of file MakeTwoZoneMesh.cpp.

◆ NZ

#define NZ   2

Definition at line 68 of file MakeTwoZoneMesh.cpp.

Function Documentation

◆ chk()

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

Definition at line 70 of file MakeTwoZoneMesh.cpp.

71{
72 if (ier)
73 {
74 printf("FAILED: %s\n", what);
75 cg_error_print();
76 exit(1);
77 }
78}

Referenced by main(), and writeZone().

◆ main()

int main ( int  ,
char **  argv 
)

Definition at line 264 of file MakeTwoZoneMesh.cpp.

265{
266 int fn, B;
267 cgsize_t dummy[2], ifA[2], ifB[2];
268
269 /* one zone */
270 chk(cg_open(argv[1], CG_MODE_WRITE, &fn), "open one");
271 chk(cg_base_write(fn, "Base", 3, 3, &B), "base one");
272 for (int f = 0; f < 6; ++f)
273 {
274 int F;
275 chk(cg_family_write(fn, B, faceName[f], &F), "family");
276 }
277 writeZone(fn, B, "fluid", 0, NX, 0, 0, dummy);
278 chk(cg_close(fn), "close one");
279 printf("%s: one zone, %d hexes\n", argv[1], NX * NY * NZ);
280
281 /* two zones, cut at NX/2 */
282 chk(cg_open(argv[2], CG_MODE_WRITE, &fn), "open two");
283 chk(cg_base_write(fn, "Base", 3, 3, &B), "base two");
284 for (int f = 0; f < 6; ++f)
285 {
286 int F;
287 chk(cg_family_write(fn, B, faceName[f], &F), "family");
288 }
289 writeZone(fn, B, "lower", 0, NX / 2, 0, 1, ifA);
290 writeZone(fn, B, "upper", NX / 2, NX, 1, 0, ifB);
291
292 /* Declare the two copies of the cut to abut, face for face. */
293 cgsize_t n = NY * NZ;
294 std::vector<cgsize_t> a(n), b(n);
295 for (cgsize_t i = 0; i < n; ++i)
296 {
297 a[i] = ifA[0] + i;
298 b[i] = ifB[0] + i;
299 }
300 int I;
301 chk(cg_conn_write(fn, B, 1, "Conn_1_2", CGNS_ENUMV(FaceCenter),
302 CGNS_ENUMV(Abutting1to1), CGNS_ENUMV(PointList), n,
303 a.data(), "upper", CGNS_ENUMV(Unstructured),
304 CGNS_ENUMV(PointListDonor), CGNS_ENUMV(LongInteger), n,
305 b.data(), &I),
306 "conn 1->2");
307 chk(cg_conn_write(fn, B, 2, "Conn_2_1", CGNS_ENUMV(FaceCenter),
308 CGNS_ENUMV(Abutting1to1), CGNS_ENUMV(PointList), n,
309 b.data(), "lower", CGNS_ENUMV(Unstructured),
310 CGNS_ENUMV(PointListDonor), CGNS_ENUMV(LongInteger), n,
311 a.data(), &I),
312 "conn 2->1");
313 chk(cg_close(fn), "close two");
314 printf("%s: two zones, %d + %d hexes, cut of %ld faces duplicated\n",
315 argv[2], NX / 2 * NY * NZ, NX / 2 * NY * NZ, (long)n);
316 return 0;
317}
static void chk(int ier, const char *what)
#define NX
static const char * faceName[6]
#define NY
#define NZ
static void writeZone(int fn, int B, const char *zname, int ix0, int ix1, int cutLo, int cutHi, cgsize_t *ifaceRange)

References chk(), faceName, NX, NY, NZ, and writeZone().

◆ vid()

static int vid ( int  ix0,
int  ix1,
int  i,
int  j,
int  k 
)
static

◆ writeZone()

static void writeZone ( int  fn,
int  B,
const char *  zname,
int  ix0,
int  ix1,
int  cutLo,
int  cutHi,
cgsize_t *  ifaceRange 
)
static

Definition at line 101 of file MakeTwoZoneMesh.cpp.

103{
104 int nx = ix1 - ix0, nv = (nx + 1) * (NY + 1) * (NZ + 1), Z, S, F;
105 cgsize_t sizes[3] = {nv, nx * NY * NZ, 0};
106 chk(cg_zone_write(fn, B, zname, sizes, CGNS_ENUMV(Unstructured), &Z),
107 "zone");
108
109 std::vector<double> x(nv), y(nv), z(nv);
110 for (int k = 0; k <= NZ; ++k)
111 {
112 for (int j = 0; j <= NY; ++j)
113 {
114 for (int i = ix0; i <= ix1; ++i)
115 {
116 int v = vid(ix0, ix1, i, j, k);
117 x[v] = i;
118 y[v] = j;
119 z[v] = k;
120 }
121 }
122 }
123 chk(cg_coord_write(fn, B, Z, RealDouble, "CoordinateX", x.data(), &F), "x");
124 chk(cg_coord_write(fn, B, Z, RealDouble, "CoordinateY", y.data(), &F), "y");
125 chk(cg_coord_write(fn, B, Z, RealDouble, "CoordinateZ", z.data(), &F), "z");
126
127 /* Boundary quads first, as exporters write them, volume after. */
128 cgsize_t next = 1;
129 for (int f = 0; f < 6; ++f)
130 {
131 std::vector<cgsize_t> q(4 * nx * NY * NZ);
132 cgsize_t n = 0;
133 for (int k = 0; k < NZ; ++k)
134 {
135 for (int j = 0; j < NY; ++j)
136 {
137 for (int i = ix0; i < ix1; ++i)
138 {
139 if (f == 0 && i != ix0)
140 {
141 continue;
142 }
143 if (f == 1 && i != ix1 - 1)
144 {
145 continue;
146 }
147 if (f == 2 && j != 0)
148 {
149 continue;
150 }
151 if (f == 3 && j != NY - 1)
152 {
153 continue;
154 }
155 if (f == 4 && k != 0)
156 {
157 continue;
158 }
159 if (f == 5 && k != NZ - 1)
160 {
161 continue;
162 }
163 /* a cut plane is not a boundary of the model */
164 if (f == 0 && cutLo)
165 {
166 continue;
167 }
168 if (f == 1 && cutHi)
169 {
170 continue;
171 }
172 for (int c = 0; c < 4; ++c)
173 {
174 q[n++] = vid(ix0, ix1, i + faceOf[f][c][0],
175 j + faceOf[f][c][1], k + faceOf[f][c][2]) +
176 1;
177 }
178 }
179 }
180 }
181 if (!n)
182 {
183 continue;
184 }
185 cgsize_t cnt = n / 4;
186 chk(cg_section_write(fn, B, Z, faceName[f], CGNS_ENUMV(QUAD_4), next,
187 next + cnt - 1, 0, q.data(), &S),
188 "bnd section");
189
190 /* A boundary condition over the section, pointing at a family that
191 carries the bare part name, which is where an exporter puts it. The
192 condition's own name is suffixed per zone, so the two zones' halves
193 of one face only come together under the family. */
194 std::vector<cgsize_t> pl(cnt);
195 for (cgsize_t e = 0; e < cnt; ++e)
196 {
197 pl[e] = next + e;
198 }
199 char bcname[64];
200 snprintf(bcname, sizeof(bcname), "%s_%d", faceName[f], Z);
201 int BC;
202 chk(cg_boco_write(fn, B, Z, bcname, CGNS_ENUMV(BCTypeUserDefined),
203 CGNS_ENUMV(PointList), cnt, pl.data(), &BC),
204 "boco");
205 chk(cg_boco_gridlocation_write(fn, B, Z, BC, CGNS_ENUMV(FaceCenter)),
206 "gridloc");
207 chk(cg_goto(fn, B, "Zone_t", Z, "ZoneBC_t", 1, "BC_t", BC, "end"),
208 "goto bc");
209 chk(cg_famname_write(faceName[f]), "famname");
210 next += cnt;
211 }
212
213 /* The cut, written as its own section so a connectivity can name it. */
214 ifaceRange[0] = ifaceRange[1] = 0;
215 if (cutLo || cutHi)
216 {
217 int f = cutLo ? 0 : 1, i = cutLo ? ix0 : ix1 - 1;
218 std::vector<cgsize_t> q(4 * NY * NZ);
219 cgsize_t n = 0;
220 for (int k = 0; k < NZ; ++k)
221 {
222 for (int j = 0; j < NY; ++j)
223 {
224 for (int c = 0; c < 4; ++c)
225 {
226 q[n++] = vid(ix0, ix1, i + faceOf[f][c][0],
227 j + faceOf[f][c][1], k + faceOf[f][c][2]) +
228 1;
229 }
230 }
231 }
232 chk(cg_section_write(fn, B, Z, "Interface", CGNS_ENUMV(QUAD_4), next,
233 next + NY * NZ - 1, 0, q.data(), &S),
234 "iface section");
235 ifaceRange[0] = next;
236 ifaceRange[1] = next + NY * NZ - 1;
237 next += NY * NZ;
238 }
239
240 std::vector<cgsize_t> h(8 * nx * NY * NZ);
241 cgsize_t n = 0;
242 for (int k = 0; k < NZ; ++k)
243 {
244 for (int j = 0; j < NY; ++j)
245 {
246 for (int i = ix0; i < ix1; ++i)
247 {
248 h[n++] = vid(ix0, ix1, i, j, k) + 1;
249 h[n++] = vid(ix0, ix1, i + 1, j, k) + 1;
250 h[n++] = vid(ix0, ix1, i + 1, j + 1, k) + 1;
251 h[n++] = vid(ix0, ix1, i, j + 1, k) + 1;
252 h[n++] = vid(ix0, ix1, i, j, k + 1) + 1;
253 h[n++] = vid(ix0, ix1, i + 1, j, k + 1) + 1;
254 h[n++] = vid(ix0, ix1, i + 1, j + 1, k + 1) + 1;
255 h[n++] = vid(ix0, ix1, i, j + 1, k + 1) + 1;
256 }
257 }
258 }
259 chk(cg_section_write(fn, B, Z, "VolumeElements", CGNS_ENUMV(HEXA_8), next,
260 next + nx * NY * NZ - 1, 0, h.data(), &S),
261 "vol section");
262}
static double Z[NNODE]
static const int faceOf[6][4][3]
static int vid(int ix0, int ix1, int i, int j, int k)

References chk(), faceName, faceOf, NY, NZ, vid(), and Z.

Referenced by main().

Variable Documentation

◆ faceName

const char* faceName[6]
static
Initial value:
= {"xmin", "xmax", "ymin",
"ymax", "zmin", "zmax"}

Definition at line 96 of file MakeTwoZoneMesh.cpp.

96 {"xmin", "xmax", "ymin",
97 "ymax", "zmin", "zmax"};

Referenced by main(), and writeZone().

◆ faceOf

const int faceOf[6][4][3]
static
Initial value:
= {
{{0, 0, 0}, {0, 1, 0}, {0, 1, 1}, {0, 0, 1}},
{{1, 0, 0}, {1, 0, 1}, {1, 1, 1}, {1, 1, 0}},
{{0, 0, 0}, {0, 0, 1}, {1, 0, 1}, {1, 0, 0}},
{{0, 1, 0}, {1, 1, 0}, {1, 1, 1}, {0, 1, 1}},
{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0}},
{{0, 0, 1}, {0, 1, 1}, {1, 1, 1}, {1, 0, 1}},
}

Definition at line 88 of file MakeTwoZoneMesh.cpp.

88 {
89 {{0, 0, 0}, {0, 1, 0}, {0, 1, 1}, {0, 0, 1}}, /* x lo */
90 {{1, 0, 0}, {1, 0, 1}, {1, 1, 1}, {1, 1, 0}}, /* x hi */
91 {{0, 0, 0}, {0, 0, 1}, {1, 0, 1}, {1, 0, 0}}, /* y lo */
92 {{0, 1, 0}, {1, 1, 0}, {1, 1, 1}, {0, 1, 1}}, /* y hi */
93 {{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0}}, /* z lo */
94 {{0, 0, 1}, {0, 1, 1}, {1, 1, 1}, {1, 0, 1}}, /* z hi */
95};

Referenced by writeZone().