CGNS接口API写入CGNS格式文件

 

  1 #include <iostream>
  2 #include <QFile>
  3 
  4 using namespace std;
  5 #include "cgnslib.h"
  6 
  7 int main()
  8 {
  9 
 10     int result;
 11 
 12     int index_file;
 13     result = cg_set_file_type(CG_FILE_ADF);
 14     if (result != CG_OK) {
 15         cout << "SetFileType Error : " << cg_get_error() << endl;
 16     }
 17     else {
 18         cout << "SetFileType result = " << result << endl;
 19     }
 20     
 21 
 22     QFile file("test.cgns");
 23     int mode = CG_MODE_READ;
 24     if (file.exists()) {
 25         mode = CG_MODE_MODIFY;
 26     }
 27     else {
 28         mode = CG_MODE_WRITE;
 29     }
 30     if (CG_OK != cg_open("test.cgns", mode, &index_file))
 31         cg_error_print();
 32 
 33 
 34     int index_base;
 35     result = cg_base_write(index_file, "BaseNode", 3, 3, &index_base);
 36     if (result != CG_OK) {
 37         cout << "Base Error : " << cg_get_error() << endl;
 38     }
 39     else {
 40         cout << "Base result = " << result << endl;
 41     }
 42     
 43 
 44     int nbases;
 45     result = cg_nbases(index_file, &nbases);
 46     if (result != CG_OK) {
 47         cout << "Base Error : " << cg_get_error() << endl;
 48     }
 49     else {
 50         cout << "nbases = " << nbases << endl;
 51     }
 52 
 53     int index_zone;
 54 
 55     /**
 56     * @brief 选择创建结构化网格还是非结构化网格,Zone节点信息的存储方式不一样
 57     */
 58     string structureType = "Unstructured";
 59 
 60     if (structureType == "Unstructured") {
 61         cgsize_t isize[3] = { 8, 1, 0 };
 62         result = cg_zone_write(index_file, nbases, "ZoneNode", isize, Unstructured, &index_zone);
 63     }
 64     else {
 65         cgsize_t isize[3][3];
 66         // 每个维度的顶点数
 67         isize[0][0] = 2;
 68         isize[0][1] = 2;
 69         isize[0][2] = 2;
 70         // 每个维度的单元数
 71         isize[1][0] = isize[0][0] - 1;
 72         isize[1][1] = isize[0][1] - 1;
 73         isize[1][2] = isize[0][2] - 1;
 74         // 每个维度的边界顶点数,结构化网格的边界顶点数永远为0
 75         isize[2][0] = 0;
 76         isize[2][1] = 0;
 77         isize[2][2] = 0;
 78         //create zone
 79         result = cg_zone_write(index_file, nbases, "ZoneNode", *isize, Structured, &index_zone);
 80     }
 81 
 82 
 83     if (result != CG_OK) {
 84         cout << "Zone Error : " << cg_get_error() << endl;
 85     }
 86     else {
 87         cout << "Zone result = " << result << endl;
 88     }
 89 
 90 
 91     int nzones;
 92     result = cg_nzones(index_file, nbases, &nzones);
 93     if (result != CG_OK) {
 94         cout << "Zone Error : " << cg_get_error() << endl;
 95     }
 96     else {
 97         cout << "Zone Num = " << nzones << endl;
 98     }
 99 
100 
101     /* 必须使用cg_goto定位到要写信息的节点,否则不知道该往哪个节点写信息,会报错 */
102     cg_goto(index_file, index_base, "end");
103     result = cg_descriptor_write("Information", "The information text!");
104     if (result != CG_OK) {
105         cout << "descriptor Error : " << cg_get_error() << endl;
106     }
107     else {
108         cout << "descriptor result = " << result << endl;
109     }
110 
111 
112     int ndescriptors;
113     result = cg_ndescriptors(&ndescriptors);
114     if (result != CG_OK) {
115         cout << "Zone Error : " << cg_get_error() << endl;
116     }
117     else {
118         cout << "descriptor Num = " << ndescriptors << endl;
119     }
120 
121 
122     vector<vector<float>> myarray = { {1, 2, 3}, {10, 20, 30}, {100, 200, 300} };
123     result = cg_user_data_write("Array Node");
124     if (CG_OK == result) {
125         cg_goto(index_file, index_base, "UserDefinedData_t", 1, "end");
126         
127         for (int i = 0; i < myarray.size(); i++)
128         {
129             string name = "MyArray" + to_string(i);
130             cgsize_t arraysize = myarray[i].size();
131             result = cg_array_write(name.c_str(), CGNS_ENUMV(RealSingle), 1, &arraysize, &myarray[i][0]);
132             if (CG_OK != result) {
133                 cout << "WriteArray Error :" << cg_get_error() << endl;
134             }
135         }
136     }
137 
138     double x[2][2][2], y[2][2][2], z[2][2][2];
139     int index_coord;
140 
141     int ni = 2;
142     int nj = 2;
143     int nk = 2;
144     for (int k = 0; k < nk; ++k)
145     {
146         for (int j = 0; j < nj; ++j)
147         {
148             for (int i = 0; i < ni; ++i)
149             {
150                 x[k][j][i] = i;
151                 y[k][j][i] = j;
152                 z[k][j][i] = k;
153             }
154         }
155     }
156 
157     result = cg_coord_write(index_file, index_base, index_zone, RealDouble, "CoordinateX", x, &index_coord);
158     if (CG_OK != result) {
159         cout << "Coord Error:" << cg_get_error() << endl;
160     }
161     result = cg_coord_write(index_file, index_base, index_zone, RealDouble, "CoordinateY", y, &index_coord);
162     if (CG_OK != result) {
163         cout << "Coord Error:" << cg_get_error() << endl;
164     }
165     result = cg_coord_write(index_file, index_base, index_zone, RealDouble, "CoordinateZ", z, &index_coord);
166     if (CG_OK != result) {
167         cout << "Coord Error:" << cg_get_error() << endl;
168     }
169 
170 
171     string solname_vertex = "VertexFlowSolution";
172     int index_flow_vertex;
173     result = cg_sol_write(index_file, index_base, index_zone, solname_vertex.c_str(), Vertex, &index_flow_vertex);
174     if (CG_OK != result) {
175         cout << "WriteSol Error:" << cg_get_error() << endl;
176     }
177 
178     int index_field_vertex;
179     float soldata_vertex[2][2][2];
180     for (int k = 0; k < nk; ++k)
181     {
182         for (int j = 0; j < nj; ++j)
183         {
184             for (int i = 0; i < ni; ++i)
185             {
186                 soldata_vertex[k][j][i] = i * j*k;
187             }
188         }
189     }
190 
191     cg_field_write(index_file, index_base, index_zone, index_flow_vertex, RealDouble, "Density", &soldata_vertex, &index_field_vertex);
192     if (CG_OK != result) {
193         cout << "WriteField Error:" << cg_get_error() << endl;
194     }
195 
196 
197 
198     string solname_center = "CellCenterFlowSolution";
199     int index_flow_center;
200     result = cg_sol_write(index_file, index_base, index_zone, solname_center.c_str(), CellCenter, &index_flow_center);
201     if (CG_OK != result) {
202         cout << "WriteSol Error:" << cg_get_error() << endl;
203     }
204 
205     int index_field_center;
206     float soldata_center[1][1][1] = { {{0.5}} };
207 
208     cg_field_write(index_file, index_base, index_zone, index_flow_center, RealDouble, "Pressure", &soldata_center, &index_field_center);
209     if (CG_OK != result) {
210         cout << "WriteField Error:" << cg_get_error() << endl;
211     }
212 
213 
214 
215     int index_family;
216     result = cg_family_write(index_file, index_base, "Family Node", &index_family);
217     if (CG_OK != result) {
218         cout << "Family Error:" << cg_get_error() << endl;
219     }
220 
221     cg_close(index_file);
222 
223     return 0;
224 }

 

paraview预览文件效果(节点数据):

paraview预览文件效果(单元数据):

posted @ 2021-08-31 10:41  禅元天道  阅读(977)  评论(0编辑  收藏  举报