Nektar++
Loading...
Searching...
No Matches
MakeTwoImpossiblePyramids.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: MakeTwoImpossiblePyramids.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 two_impossible_pyramids.cgns from
32// ImpossiblePyramids_linear.cgns.
33//
34////////////////////////////////////////////////////////////////////////////////
35// Writes two translated copies of that mesh into one zone, so that the file
36// contains two pyramids the reader cannot number as it would like rather than
37// one. The copies are far enough apart to share nothing, so each is shielded
38// exactly as it is on its own and the mesh stays sound.
39//
40// Every pyramid fixture here contains a single such pyramid, which is why it
41// went unnoticed that ResetNodes left its loop over pyramids altogether once
42// one of them had been shielded, instead of going on to the next: the second
43// pyramid and all after it were left without an apex id, and an apex that is
44// not the highest-numbered node of its pyramid is not the collapsed point the
45// geometry requires.
46//
47// Built by utilities/NekMesh/CMakeLists.txt wherever CGNS is enabled, which
48// also adds a test that reruns this and checks the result against the
49// fixture committed beside it. To build it by hand instead:
50//
51// c++ MakeTwoImpossiblePyramids.cpp -I<cgns>/include -L<cgns>/lib -lcgns \
52// -o MakeTwoImpossiblePyramids
53// ./MakeTwoImpossiblePyramids ImpossiblePyramids_linear.cgns \
54// two_impossible_pyramids.cgns
55//
56#include <cgnslib.h>
57#include <cstdio>
58#include <cstdlib>
59#include <cstring>
60#include <vector>
61
62static void chk(int ier, const char *what)
63{
64 if (ier)
65 {
66 printf("FAILED: %s\n", what);
67 cg_error_print();
68 exit(1);
69 }
70}
71
72int main(int argc, char **argv)
73{
74 if (argc != 3)
75 {
76 printf("usage: %s <in.cgns> <out.cgns>\n", argv[0]);
77 return 1;
78 }
79
80 int in;
81 chk(cg_open(argv[1], CG_MODE_READ, &in), "open in");
82
83 char zname[33];
84 cgsize_t size[9];
85 chk(cg_zone_read(in, 1, 1, zname, size), "zone_read");
86
87 const cgsize_t nnode = size[0], ncell = size[1];
88
89 int ncoords;
90 chk(cg_ncoords(in, 1, 1, &ncoords), "ncoords");
91
92 std::vector<double> x[3];
93 char cname[3][33];
94 for (int C = 1; C <= ncoords; ++C)
95 {
96 DataType_t dt;
97 chk(cg_coord_info(in, 1, 1, C, &dt, cname[C - 1]), "coord_info");
98 x[C - 1].resize(nnode * 2);
99 cgsize_t one = 1;
100 chk(cg_coord_read(in, 1, 1, cname[C - 1], RealDouble, &one, &nnode,
101 x[C - 1].data()),
102 "coord_read");
103 }
104
105 /* The second copy is shifted clear of the first along x. */
106 double xmin = x[0][0], xmax = x[0][0];
107 for (cgsize_t i = 1; i < nnode; ++i)
108 {
109 xmin = x[0][i] < xmin ? x[0][i] : xmin;
110 xmax = x[0][i] > xmax ? x[0][i] : xmax;
111 }
112 const double shift = 2.0 * (xmax - xmin) + 1.0;
113
114 for (int c = 0; c < ncoords; ++c)
115 {
116 for (cgsize_t i = 0; i < nnode; ++i)
117 {
118 x[c][nnode + i] = x[c][i] + (c == 0 ? shift : 0.0);
119 }
120 }
121
122 int out, B, Z, C, S;
123 chk(cg_open(argv[2], CG_MODE_WRITE, &out), "open out");
124 chk(cg_base_write(out, "Base", 3, 3, &B), "base_write");
125
126 cgsize_t osize[3] = {nnode * 2, ncell * 2, 0};
127 chk(cg_zone_write(out, B, zname, osize, Unstructured, &Z), "zone_write");
128
129 for (int c = 0; c < ncoords; ++c)
130 {
131 chk(cg_coord_write(out, B, Z, RealDouble, cname[c], x[c].data(), &C),
132 "coord_write");
133 }
134
135 int nsections;
136 chk(cg_nsections(in, 1, 1, &nsections), "nsections");
137
138 cgsize_t next = 1;
139 for (int Sin = 1; Sin <= nsections; ++Sin)
140 {
141 char sname[33];
142 ElementType_t type;
143 cgsize_t start, end;
144 int nbndry, parent;
145 chk(cg_section_read(in, 1, 1, Sin, sname, &type, &start, &end, &nbndry,
146 &parent),
147 "section_read");
148
149 cgsize_t ndata;
150 chk(cg_ElementDataSize(in, 1, 1, Sin, &ndata), "ElementDataSize");
151
152 const cgsize_t nelem = end - start + 1;
153 std::vector<cgsize_t> conn(ndata * 2);
154
155 if (type == MIXED)
156 {
157 /* A MIXED section is read with an offset per element, since its
158 elements are not all the same size. */
159 std::vector<cgsize_t> off(nelem * 2 + 1);
160 cgsize_t pd;
161 chk(cg_poly_elements_read(in, 1, 1, Sin, conn.data(), off.data(),
162 &pd),
163 "poly_elements_read");
164
165 cgsize_t i = 0, j = ndata;
166 while (i < ndata)
167 {
168 int npe;
169 chk(cg_npe((ElementType_t)conn[i], &npe), "npe");
170 conn[j++] = conn[i++];
171 for (int k = 0; k < npe; ++k)
172 {
173 conn[j++] = conn[i++] + nnode;
174 }
175 }
176
177 for (cgsize_t e = 1; e <= nelem; ++e)
178 {
179 off[nelem + e] = off[nelem] + off[e];
180 }
181
182 chk(cg_poly_section_write(out, B, Z, sname, type, next,
183 next + nelem * 2 - 1, nbndry, conn.data(),
184 off.data(), &S),
185 "poly_section_write");
186 }
187 else
188 {
189 cgsize_t pd;
190 chk(cg_elements_read(in, 1, 1, Sin, conn.data(), &pd),
191 "elements_read");
192
193 for (cgsize_t i = 0; i < ndata; ++i)
194 {
195 conn[ndata + i] = conn[i] + nnode;
196 }
197
198 chk(cg_section_write(out, B, Z, sname, type, next,
199 next + nelem * 2 - 1, nbndry, conn.data(), &S),
200 "section_write");
201 }
202
203 next += nelem * 2;
204 }
205
206 chk(cg_close(in), "close in");
207 chk(cg_close(out), "close out");
208 printf("wrote %s: %lld nodes, %lld cells\n", argv[2],
209 (long long)(nnode * 2), (long long)(ncell * 2));
210 return 0;
211}
static double Z[NNODE]
static void chk(int ier, const char *what)
Definition main.py:1