72int main(
int argc,
char **argv)
76 printf(
"usage: %s <in.cgns> <out.cgns>\n", argv[0]);
81 chk(cg_open(argv[1], CG_MODE_READ, &in),
"open in");
85 chk(cg_zone_read(in, 1, 1, zname, size),
"zone_read");
87 const cgsize_t nnode = size[0], ncell = size[1];
90 chk(cg_ncoords(in, 1, 1, &ncoords),
"ncoords");
92 std::vector<double> x[3];
94 for (
int C = 1; C <= ncoords; ++C)
97 chk(cg_coord_info(in, 1, 1, C, &dt, cname[C - 1]),
"coord_info");
98 x[C - 1].resize(nnode * 2);
100 chk(cg_coord_read(in, 1, 1, cname[C - 1], RealDouble, &one, &nnode,
106 double xmin = x[0][0], xmax = x[0][0];
107 for (cgsize_t i = 1; i < nnode; ++i)
109 xmin = x[0][i] < xmin ? x[0][i] : xmin;
110 xmax = x[0][i] > xmax ? x[0][i] : xmax;
112 const double shift = 2.0 * (xmax - xmin) + 1.0;
114 for (
int c = 0; c < ncoords; ++c)
116 for (cgsize_t i = 0; i < nnode; ++i)
118 x[c][nnode + i] = x[c][i] + (c == 0 ? shift : 0.0);
123 chk(cg_open(argv[2], CG_MODE_WRITE, &out),
"open out");
124 chk(cg_base_write(out,
"Base", 3, 3, &B),
"base_write");
126 cgsize_t osize[3] = {nnode * 2, ncell * 2, 0};
127 chk(cg_zone_write(out, B, zname, osize, Unstructured, &
Z),
"zone_write");
129 for (
int c = 0; c < ncoords; ++c)
131 chk(cg_coord_write(out, B,
Z, RealDouble, cname[c], x[c].data(), &C),
136 chk(cg_nsections(in, 1, 1, &nsections),
"nsections");
139 for (
int Sin = 1; Sin <= nsections; ++Sin)
145 chk(cg_section_read(in, 1, 1, Sin, sname, &type, &start, &end, &nbndry,
150 chk(cg_ElementDataSize(in, 1, 1, Sin, &ndata),
"ElementDataSize");
152 const cgsize_t nelem = end - start + 1;
153 std::vector<cgsize_t> conn(ndata * 2);
159 std::vector<cgsize_t> off(nelem * 2 + 1);
161 chk(cg_poly_elements_read(in, 1, 1, Sin, conn.data(), off.data(),
163 "poly_elements_read");
165 cgsize_t i = 0, j = ndata;
169 chk(cg_npe((ElementType_t)conn[i], &npe),
"npe");
170 conn[j++] = conn[i++];
171 for (
int k = 0; k < npe; ++k)
173 conn[j++] = conn[i++] + nnode;
177 for (cgsize_t e = 1; e <= nelem; ++e)
179 off[nelem + e] = off[nelem] + off[e];
182 chk(cg_poly_section_write(out, B,
Z, sname, type, next,
183 next + nelem * 2 - 1, nbndry, conn.data(),
185 "poly_section_write");
190 chk(cg_elements_read(in, 1, 1, Sin, conn.data(), &pd),
193 for (cgsize_t i = 0; i < ndata; ++i)
195 conn[ndata + i] = conn[i] + nnode;
198 chk(cg_section_write(out, B,
Z, sname, type, next,
199 next + nelem * 2 - 1, nbndry, conn.data(), &S),
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));