CGNS接口API读取CGNS格式文件

  1 #include <iostream>
  2 #include <QFile>
  3 
  4 
  5 using namespace std;
  6 
  7 #include "cgnslib.h"
  8 
  9 
 10 int main() {
 11     int result;
 12 
 13     int index_file;
 14     result = cg_open("valve.cgns", CG_MODE_READ, &index_file);
 15     if (CG_OK != result) {
 16         cout << "Open Error: " << cg_get_error() << endl;
 17     }
 18     else {
 19         cout << "Success to open cgns file!" << endl;
 20     }
 21 
 22     int index_cgio_num;
 23     result = cg_get_cgio(index_file, &index_cgio_num);
 24     if (CG_OK != result) {
 25         cout << "Get cgio num Error: " << cg_get_error() << endl;
 26     }
 27 
 28     int nbases;
 29     result = cg_nbases(index_file, &nbases);
 30     if (CG_OK != result) {
 31         cout << "Get nbases Error: " << cg_get_error() << endl;
 32     }
 33     else {
 34         cout << "Base Num = " << nbases << endl;
 35     }
 36 
 37     for (int index_base = 1; index_base <= nbases; index_base++)
 38     {
 39 
 40         char basename[100];
 41         int celldim, physdim;
 42         result = cg_base_read(index_file, index_base, basename, &celldim, &physdim);
 43         if (CG_OK != result) {
 44             cout << "Read Base: " << cg_get_error() << endl;
 45         }
 46         else {
 47             cout << "BaseName = " << basename << endl;
 48             cout << "Dimension of the cells = " << celldim << endl;
 49             cout << "Number of coordinates required to define a vector in the field is " << physdim << endl;
 50         }
 51 
 52         int nzones;
 53         result = cg_nzones(index_file, index_base, &nzones);
 54         if (CG_OK != result) {
 55             cout << "Get nzones Error: " << cg_get_error() << endl;
 56         }
 57         else {
 58             cout << "Zone Num of Base(" << index_base << ") = " << nzones << endl;
 59 
 60             for (int index_zone = 1; index_zone <= nzones; index_zone++)
 61             {
 62 
 63 
 64                 /* Zone Info */
 65                 ZoneType_t zoneType;
 66                 result = cg_zone_type(index_file, index_base, index_zone, &zoneType);
 67                 if (CG_OK != result) {
 68                     cout << "Get Zone Type Error: " << cg_get_error() << endl;
 69                 }
 70 
 71 
 72                 cgsize_t size[9] = { 0 };
 73                 char zonename[100];
 74                 result = cg_zone_read(index_file, index_base, index_zone, zonename, size);
 75                 if (CG_OK != result) {
 76                     cout << "Zone Read Error: " << cg_get_error() << endl;
 77                 }
 78                 else {
 79                     cout << "ZoneName = " << zonename << endl;
 80 
 81                     if (zoneType == Structured) {
 82                         cout << "ZoneType = Structured" << endl;
 83                         if (celldim == 3) {
 84                             cout << "Total Points: " << size[0] * size[1] * size[2] << endl;
 85                             cout << "Total Cells: " << size[3] * size[4] * size[5] << endl;
 86                         }
 87                         else if (celldim == 2) {
 88                             cout << "Total Points: " << size[0] * size[1] << endl;
 89                             cout << "Total Cells: " << size[2] * size[3] << endl;
 90                         }
 91                         else {
 92                             cout << "No 3-D Cell and No 3-D Cell!" << endl;
 93                         }
 94                     }
 95                     else if (zoneType == Unstructured) {
 96                         cout << "ZoneType = Unstructured" << endl;
 97                         cout << "Total Points: " << size[0] << endl;
 98                         cout << "Total Cells: " << size[1] << endl;
 99                     }
100                     else {
101                         cout << "Unknown ZoneType!";
102                     }
103                 }
104 
105 
106                 /* Flow Solution */
107                 int nsols;
108                 result = cg_nsols(index_file, index_base, index_zone, &nsols);
109                 if (CG_OK != result) {
110                     cout << "Get Num Of Solution Error: " << cg_get_error() << endl;
111                 }
112                 else {
113                     cout << "Solution Num = " << nsols << endl;
114                 }
115 
116                 for (int index_sol = 1; index_sol <= nsols; index_sol++)
117                 {
118                     char solname[100];
119                     GridLocation_t location;
120                     result = cg_sol_info(index_file, index_base, index_zone, index_sol, solname, &location);
121                     if (CG_OK != result) {
122                         cout << "Read Solution Info Error: " << cg_get_error() << endl;
123                     }
124                     else {
125                         cout << "Solution Name = " << solname << endl;
126                         switch (location)
127                         {
128                         case Vertex:
129                             cout << "Solution Location = Vertex" << endl;
130                             break;
131                         case CellCenter:
132                             cout << "Solution Location = CellCenter" << endl;
133                             break;
134                         case IFaceCenter:
135                             cout << "Solution Location = IFaceCenter" << endl;
136                             break;
137                         case JFaceCenter:
138                             cout << "Solution Location = JFaceCenter" << endl;
139                             break;
140                         case KFaceCenter:
141                             cout << "Solution Location = KFaceCenter" << endl;
142                             break;
143                         default:
144                             cout << "Solution Location is bad data! Unknown!" << endl;
145                         }
146                     }
147 
148                     int nfileds;
149                     result = cg_nfields(index_file, index_base, index_zone, index_sol, &nfileds);
150                     if (CG_OK != result) {
151                         cout << "Get Nfields Error: " << cg_get_error() << endl;
152                     }
153                     else {
154                         cout << "nfields = " << nfileds << endl;
155 
156                         for (int index_field = 1; index_field <= nfileds; index_field++)
157                         {
158                             DataType_t dataType;
159                             char fieldname[100];
160                             result = cg_field_info(index_file, index_base, index_zone, index_sol, index_field, &dataType, fieldname);
161                             if (CG_OK != result) {
162                                 cout << "Get Field Info Error: " << cg_get_error() << endl;
163                             }
164                             else {
165                                 cout << "FieldName = " << fieldname << endl;
166                                 cout << "DataType_t(Integer, LongInteger, RealSingle, and RealDouble) = " << dataType << endl;
167 
168                                 /**
169                                  * @todo range_min与range_max的含义
170                                 **/
171                                 cgsize_t range_min[3] = {1};
172                                 cgsize_t range_max[3] = {10};
173                                 double solution_array[10] = {0};
174                                 result = cg_field_read(index_file, index_base, index_zone, index_sol, fieldname, dataType, range_min, range_max, solution_array);
175                                 if (CG_OK != result) {
176                                     cout << "Get Field Error: " << cg_get_error() << endl;
177                                 }
178                                 else {
179                                     for (int i =0; i < 10; i++)
180                                     {
181                                         cout << "solution_array[" << i << "] = " << solution_array[i] << endl;
182                                     }
183                                 }
184                             }
185                         }
186                     }
187                 }
188                 
189             }
190         }
191     }
192     
193 
194 
195     cg_close(index_file);
196 }

 

运行结果:

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