1 #include "cgnswriter.h"
3 CgnsWriter::CgnsWriter()
5 setFormat("CGNS files(*.cgns)");
8 void CgnsWriter::writeGrid()
11 eg2cgns
.fill(-1, cells
.size());
13 // create the base node
14 if (cg_base_write(fn
, "Base", 3, 3, &B
)) {
15 EG_ERR_RETURN("error creating CGNS base node");
19 foreach (vtkIdType id_cell
, cells
) {
20 if (isVolume(id_cell
, grid
)) {
25 // create the zone node for the main grid
27 size
[0] = nodes
.size();
30 if (cg_zone_write(fn
,B
,"main_grid",size
,Unstructured
,&Z
)) {
31 EG_ERR_RETURN("error creating volume zone");
34 // create a node for the grid coordinates
36 if (cg_grid_write(fn
,B
,Z
,"GridCoordinates",&G
)) {
37 EG_ERR_RETURN("error creating GridCoordinates node");
40 // create the arrays for the x,y, and z coordinates
43 double coord_array
[nodes
.size()];
46 for (int i
= 0; i
< nodes
.size(); ++i
) {
48 EG_ERR_RETURN("unexpected hole in the point list");
50 grid
->GetPoint(nodes
[i
], x
.data());
51 coord_array
[i
] = x
[0];
53 if (cg_coord_write(fn
, B
, Z
, RealDouble
, "CoordinateX", coord_array
, &C
)) {
54 EG_ERR_RETURN("error writing x-coordinates");
58 for (int i
= 0; i
< nodes
.size(); ++i
) {
59 grid
->GetPoint(nodes
[i
], x
.data());
60 coord_array
[i
] = x
[1];
62 if (cg_coord_write(fn
, B
, Z
, RealDouble
, "CoordinateY", coord_array
, &C
)) {
63 EG_ERR_RETURN("error writing y-coordinates");
67 for (int i
= 0; i
< nodes
.size(); ++i
) {
68 grid
->GetPoint(nodes
[i
], x
.data());
69 coord_array
[i
] = x
[2];
71 if (cg_coord_write(fn
, B
, Z
, RealDouble
, "CoordinateZ", coord_array
, &C
)) {
72 EG_ERR_RETURN("error writing z-coordinates");
77 // count occurrences of different element types
84 foreach (vtkIdType id_cell
, cells
) {
85 if (grid
->GetCellType(id_cell
) == VTK_TETRA
) ++ntet
;
86 else if (grid
->GetCellType(id_cell
) == VTK_PYRAMID
) ++npyr
;
87 else if (grid
->GetCellType(id_cell
) == VTK_WEDGE
) ++npri
;
88 else if (grid
->GetCellType(id_cell
) == VTK_HEXAHEDRON
) ++nhex
;
89 else if (grid
->GetCellType(id_cell
) == VTK_TRIANGLE
) ++ntri
;
90 else if (grid
->GetCellType(id_cell
) == VTK_QUAD
) ++nqua
;
93 // write element connectivity for tetras
101 foreach (vtkIdType id_cell
, cells
) {
102 if (grid
->GetCellType(id_cell
) == VTK_TETRA
) {
103 eg2cgns
[id_cell
] = i_cgns
;
105 vtkIdType Npts
, *pts
;
106 grid
->GetCellPoints(id_cell
, Npts
, pts
);
107 elements
[j
++] = pts
[0]+1;
108 elements
[j
++] = pts
[1]+1;
109 elements
[j
++] = pts
[2]+1;
110 elements
[j
++] = pts
[3]+1;
115 if (cg_section_write(fn
, B
, Z
, "Tetras", TETRA_4
, start
, end
, 0, elements
, &S
)) {
116 EG_ERR_RETURN("error writing tetras");
120 // write element connectivity for pyramids
123 int elements
[npyr
*5];
125 foreach (vtkIdType id_cell
, cells
) {
126 if (grid
->GetCellType(id_cell
) == VTK_PYRAMID
) {
127 eg2cgns
[id_cell
] = i_cgns
;
129 vtkIdType Npts
, *pts
;
130 grid
->GetCellPoints(id_cell
, Npts
, pts
);
131 elements
[j
++] = pts
[0]+1;
132 elements
[j
++] = pts
[1]+1;
133 elements
[j
++] = pts
[2]+1;
134 elements
[j
++] = pts
[3]+1;
135 elements
[j
++] = pts
[4]+1;
140 if (cg_section_write(fn
, B
, Z
, "Pyramids", PYRA_5
, start
, end
, 0, elements
, &S
)) {
141 EG_ERR_RETURN("error writing pyramids");
145 // write element connectivity for prisms
148 int elements
[npri
*6];
150 foreach (vtkIdType id_cell
, cells
) {
151 if (grid
->GetCellType(id_cell
) == VTK_WEDGE
) {
152 eg2cgns
[id_cell
] = i_cgns
;
154 vtkIdType Npts
, *pts
;
155 grid
->GetCellPoints(id_cell
, Npts
, pts
);
156 elements
[j
++] = pts
[3]+1;
157 elements
[j
++] = pts
[4]+1;
158 elements
[j
++] = pts
[5]+1;
159 elements
[j
++] = pts
[0]+1;
160 elements
[j
++] = pts
[1]+1;
161 elements
[j
++] = pts
[2]+1;
166 if (cg_section_write(fn
, B
, Z
, "Prisms", PENTA_6
, start
, end
, 0, elements
, &S
)) {
167 EG_ERR_RETURN("error writing prisms");
171 // write element connectivity for hexas
174 int elements
[nhex
*8];
176 foreach (vtkIdType id_cell
, cells
) {
177 if (grid
->GetCellType(id_cell
) == VTK_HEXAHEDRON
) {
178 eg2cgns
[id_cell
] = i_cgns
;
180 vtkIdType Npts
, *pts
;
181 grid
->GetCellPoints(id_cell
, Npts
, pts
);
182 elements
[j
++] = pts
[0]+1;
183 elements
[j
++] = pts
[1]+1;
184 elements
[j
++] = pts
[3]+1;
185 elements
[j
++] = pts
[2]+1;
186 elements
[j
++] = pts
[4]+1;
187 elements
[j
++] = pts
[5]+1;
188 elements
[j
++] = pts
[7]+1;
189 elements
[j
++] = pts
[6]+1;
194 if (cg_section_write(fn
, B
, Z
, "Hexes", HEXA_8
, start
, end
, 0, elements
, &S
)) {
195 EG_ERR_RETURN("error writing hexes");
199 // write element connectivity for triangles
202 int elements
[ntri
*3];
204 foreach (vtkIdType id_cell
, cells
) {
205 if (grid
->GetCellType(id_cell
) == VTK_TRIANGLE
) {
206 eg2cgns
[id_cell
] = i_cgns
;
208 vtkIdType Npts
, *pts
;
209 grid
->GetCellPoints(id_cell
, Npts
, pts
);
210 elements
[j
++] = pts
[2]+1;
211 elements
[j
++] = pts
[1]+1;
212 elements
[j
++] = pts
[0]+1;
217 if (cg_section_write(fn
, B
, Z
, "Triangles", TRI_3
, start
, end
, 0, elements
, &S
)) {
218 EG_ERR_RETURN("error writing triangles");
222 // write element connectivity for quads
225 int elements
[nqua
*4];
227 foreach (vtkIdType id_cell
, cells
) {
228 if (grid
->GetCellType(id_cell
) == VTK_QUAD
) {
229 eg2cgns
[id_cell
] = i_cgns
;
231 vtkIdType Npts
, *pts
;
232 grid
->GetCellPoints(id_cell
, Npts
, pts
);
233 elements
[j
++] = pts
[3]+1;
234 elements
[j
++] = pts
[2]+1;
235 elements
[j
++] = pts
[1]+1;
236 elements
[j
++] = pts
[0]+1;
241 if (cg_section_write(fn
, B
, Z
, "Quads", QUAD_4
, start
, end
, 0, elements
, &S
)) {
242 EG_ERR_RETURN("error writing quads");
247 void CgnsWriter::writeBcs()
249 EG_VTKDCC(vtkIntArray
, cell_code
, grid
, "cell_code");
252 QVector
<vtkIdType
> faces
;
253 getAllSurfaceCells(faces
, grid
);
254 foreach (vtkIdType id_face
, faces
) {
255 bcs
.insert(cell_code
->GetValue(id_face
));
259 foreach (int bc
, bcs
) {
260 QVector
<vtkIdType
> bc_faces
;
263 getSurfaceCells(tmp_bcs
, bc_faces
, grid
);
264 int data
[bc_faces
.size()];
265 for (int i
= 0; i
< bc_faces
.size(); ++i
) {
266 data
[i
] = eg2cgns
[bc_faces
[i
]]+1;
268 if (cg_boco_write(fn
, B
, Z
, getBC(bc
).getName().toAscii().data(), BCTypeNull
, ElementList
, bc_faces
.size(), data
, &BC_cgns
)) {
269 cout
<< cg_get_error() << endl
;
270 EG_ERR_RETURN("error writing boundary condition");
275 void CgnsWriter::operate()
279 EG_ERR_RETURN("CGNS support has not been compiled");
283 readOutputFileName();
285 QString file_name
= getFileName();
286 if (cg_open(file_name
.toAscii().data(), MODE_WRITE
, &fn
)) {
287 EG_ERR_RETURN("error while opening CGNS file for writing");
292 EG_ERR_RETURN("error while closing CGNS file");
295 } catch (Error err
) {