6 #include "GmshConfig.h"
14 #if defined(HAVE_LIBCGNS)
18 typedef std::pair<std::string, std::string> SolFieldName;
20 int readFlowSolutionNames(
21 int fileIndex,
int baseIndex,
int nbZone,
22 std::map<SolFieldName, PViewDataGModel::DataType> &fields)
26 for(
int iZone = 1; iZone <= nbZone; iZone++) {
29 cgnsErr = cg_nsols(fileIndex, baseIndex, iZone, &nbZoneSol);
30 if(cgnsErr != CG_OK)
return cgnsError(__FILE__, __LINE__, fileIndex);
33 for(
int iZoneSol = 1; iZoneSol <= nbZoneSol; iZoneSol++) {
35 char rawSolName[CGNS_MAX_STR_LEN];
36 CGNS_ENUMT(GridLocation_t) location;
37 cgnsErr = cg_sol_info(fileIndex, baseIndex, iZone, iZoneSol, rawSolName,
39 if(cgnsErr != CG_OK)
return cgnsError(__FILE__, __LINE__, fileIndex);
40 const std::string solName(rawSolName);
42 if(location == CGNS_ENUMV(CellCenter))
44 else if(location == CGNS_ENUMV(
Vertex)) {
47 "Multi-zone node-based solutions not supported in CGNS "
48 "reader, skipping '%s'",
55 #ifdef HAVE_LIBCGNS_CPEX0045
56 else if(location == ElementBased) {
61 Msg::Warning(
"Unsupported GridLocation in CGNS solution reader, "
69 cgnsErr = cg_nfields(fileIndex, baseIndex, iZone, iZoneSol, &nbField);
70 if(cgnsErr != CG_OK)
return cgnsError(__FILE__, __LINE__, fileIndex);
73 for(
int iField = 1; iField <= nbField; iField++) {
74 CGNS_ENUMT(DataType_t) dataType;
75 char rawFieldName[CGNS_MAX_STR_LEN];
76 cgnsErr = cg_field_info(fileIndex, baseIndex, iZone, iZoneSol, iField,
77 &dataType, rawFieldName);
78 if(cgnsErr != CG_OK)
return cgnsError(__FILE__, __LINE__, fileIndex);
79 const std::string fieldName(rawFieldName);
80 fields[std::make_pair(solName, fieldName)] = type;
89 readFieldData(
const std::vector<std::vector<MVertex *> > &vertPerZone,
90 const std::vector<std::vector<MElement *> > &eltPerZone,
91 const std::string &fileName,
int fileIndex,
int baseIndex,
92 const std::map<SolFieldName, PViewDataGModel::DataType> &fields)
95 for(
auto it = fields.begin(); it != fields.end(); ++it) {
97 const SolFieldName &solFieldName = it->first;
102 const std::string fullFieldName =
103 solFieldName.first +
"_" + solFieldName.second;
105 fullFieldName, -1, -1);
118 if(!d->
readCGNS(solFieldName, fileName, index, fileIndex, baseIndex,
119 vertPerZone, eltPerZone)) {
120 Msg::Error(
"Could not read data in CGNS file '%s'", fileName.c_str());
128 if(create)
new PView(d);
137 bool PView::readCGNS(
const std::vector<std::vector<MVertex *> > &vertPerZone,
138 const std::vector<std::vector<MElement *> > &eltPerZone,
139 const std::string &fileName)
145 cgnsErr = cg_open(fileName.c_str(), CG_MODE_READ, &fileIndex);
146 if(cgnsErr != CG_OK)
return cgnsError(__FILE__, __LINE__, fileIndex);
149 const int baseIndex = 1;
150 int dim = 0, meshDim = 0;
151 char baseName[CGNS_MAX_STR_LEN];
152 cgnsErr = cg_base_read(fileIndex, baseIndex, baseName, &meshDim, &dim);
153 if(cgnsErr != CG_OK)
return cgnsError(__FILE__, __LINE__, fileIndex);
157 cgnsErr = cg_nzones(fileIndex, baseIndex, &nbZone);
158 if(cgnsErr != CG_OK)
return cgnsError(__FILE__, __LINE__, fileIndex);
161 std::map<SolFieldName, PViewDataGModel::DataType> fields;
162 int err = readFlowSolutionNames(fileIndex, baseIndex, nbZone, fields);
163 if(err == 0)
return 0;
166 bool err2 = readFieldData(vertPerZone, eltPerZone, fileName, fileIndex,
168 if(!err2)
return false;
171 cgnsErr = cg_close(fileIndex);
172 if(cgnsErr != CG_OK)
return cgnsError(__FILE__, __LINE__);
180 const std::vector<std::vector<MElement *> > &eltPerZone,
181 const std::string &fileName)
183 Msg::Error(
"Gmsh must be compiled with CGNS support to read '%s'",