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                 cout << "ZoneType = Structured" << endl;
 72 
 73                 cgsize_t size[9] = { 0 };
 74                 char zonename[100];
 75                 result = cg_zone_read(index_file, index_base, index_zone, zonename, size);
 76                 if (CG_OK != result) {
 77                     cout << "Zone Read Error: " << cg_get_error() << endl;
 78                 }
 79                 else {
 80                     cout << "ZoneName = " << zonename << endl;
 81 
 82                     if (zoneType == Structured) {
 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 << "Total Points: " << size[0] << endl;
 97                         cout << "Total Cells: " << size[1] << endl;
 98                     }
 99                     else {
100                         cout << "Unknown ZoneType!";
101                     }
102                 }
103 
104 
105                 /* Flow Solution */
106                 int nsols;
107                 result = cg_nsols(index_file, index_base, index_zone, &nsols);
108                 if (CG_OK != result) {
109                     cout << "Get Num Of Solution Error: " << cg_get_error() << endl;
110                 }
111                 else {
112                     cout << "Solution Num = " << nsols << endl;
113                 }
114 
115                 for (int index_sol = 1; index_sol <= nsols; index_sol++)
116                 {
117                     char solname[100];
118                     GridLocation_t location;
119                     result = cg_sol_info(index_file, index_base, index_zone, index_sol, solname, &location);
120                     if (CG_OK != result) {
121                         cout << "Read Solution Info Error: " << cg_get_error() << endl;
122                     }
123                     else {
124                         cout << "Solution Name = " << solname << endl;
125                         switch (location)
126                         {
127                         case Vertex:
128                             cout << "Solution Location = Vertex" << endl;
129                             break;
130                         case CellCenter:
131                             cout << "Solution Location = CellCenter" << endl;
132                             break;
133                         case IFaceCenter:
134                             cout << "Solution Location = IFaceCenter" << endl;
135                             break;
136                         case JFaceCenter:
137                             cout << "Solution Location = JFaceCenter" << endl;
138                             break;
139                         case KFaceCenter:
140                             cout << "Solution Location = KFaceCenter" << endl;
141                             break;
142                         default:
143                             cout << "Solution Location is bad data! Unknown!" << endl;
144                         }
145                     }
146 
147                     int nfileds;
148                     result = cg_nfields(index_file, index_base, index_zone, index_sol, &nfileds);
149                     if (CG_OK != result) {
150                         cout << "Get Nfields Error: " << cg_get_error() << endl;
151                     }
152                     else {
153                         cout << "nfields = " << nfileds << endl;
154 
155                         for (int index_field = 1; index_field <= nfileds; index_field++)
156                         {
157                             DataType_t dataType;
158                             char fieldname[100];
159                             result = cg_field_info(index_file, index_base, index_zone, index_sol, index_field, &dataType, fieldname);
160                             if (CG_OK != result) {
161                                 cout << "Get Field Info Error: " << cg_get_error() << endl;
162                             }
163                             else {
164                                 cout << "FieldName = " << fieldname << endl;
165                                 cout << "DataType_t(Integer, LongInteger, RealSingle, and RealDouble) = " << dataType << endl;
166 
167                                 /**
168                                  * @todo range_min与range_max的含义
169                                 **/
170                                 cgsize_t range_min[3] = {1};
171                                 cgsize_t range_max[3] = {10};
172                                 double solution_array[10] = {0};
173                                 result = cg_field_read(index_file, index_base, index_zone, index_sol, fieldname, dataType, range_min, range_max, solution_array);
174                                 if (CG_OK != result) {
175                                     cout << "Get Field Error: " << cg_get_error() << endl;
176                                 }
177                                 else {
178                                     for (int i =0; i < 10; i++)
179                                     {
180                                         cout << "solution_array[" << i << "] = " << solution_array[i] << endl;
181                                     }
182                                 }
183                             }
184                         }
185                     }
186                 }
187                 
188             }
189         }
190     }
191     
192 
193 
194     cg_close(index_file);
195 }

运行结果:

CGNS读取网格文件

CGNS读取网格文件

上一篇:在Win7中IIS配置Asp.Net虚拟目录的方法及错误总结!


下一篇:Windows Server 2008 R2--域控制器的搭建