gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
PViewIO_CGNS.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #include "GmshConfig.h"
7 #include "GmshMessage.h"
8 #include "PView.h"
9 #include "PViewData.h"
10 #include "PViewDataGModel.h"
11 #include "CGNSCommon.h"
12 #include "CGNSConventions.h"
13 
14 #if defined(HAVE_LIBCGNS)
15 
16 namespace {
17 
18  typedef std::pair<std::string, std::string> SolFieldName;
19 
20  int readFlowSolutionNames(
21  int fileIndex, int baseIndex, int nbZone,
22  std::map<SolFieldName, PViewDataGModel::DataType> &fields)
23  {
24  int cgnsErr;
25 
26  for(int iZone = 1; iZone <= nbZone; iZone++) {
27  // get number of flow solutions in zone
28  int nbZoneSol;
29  cgnsErr = cg_nsols(fileIndex, baseIndex, iZone, &nbZoneSol);
30  if(cgnsErr != CG_OK) return cgnsError(__FILE__, __LINE__, fileIndex);
31 
32  // get names of solution fields in each zone
33  for(int iZoneSol = 1; iZoneSol <= nbZoneSol; iZoneSol++) {
34  // get FlowSolution info
35  char rawSolName[CGNS_MAX_STR_LEN];
36  CGNS_ENUMT(GridLocation_t) location;
37  cgnsErr = cg_sol_info(fileIndex, baseIndex, iZone, iZoneSol, rawSolName,
38  &location);
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)) {
45  if(nbZone > 1) {
47  "Multi-zone node-based solutions not supported in CGNS "
48  "reader, skipping '%s'",
49  rawSolName);
50  continue;
51  }
52  else
54  }
55 #ifdef HAVE_LIBCGNS_CPEX0045
56  else if(location == ElementBased) {
58  }
59 #endif
60  else {
61  Msg::Warning("Unsupported GridLocation in CGNS solution reader, "
62  "skipping '%s'",
63  rawSolName);
64  continue;
65  }
66 
67  // get number of fields in this FlowSolution
68  int nbField;
69  cgnsErr = cg_nfields(fileIndex, baseIndex, iZone, iZoneSol, &nbField);
70  if(cgnsErr != CG_OK) return cgnsError(__FILE__, __LINE__, fileIndex);
71 
72  // get names of fields
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;
81  }
82  }
83  }
84 
85  return 1;
86  }
87 
88  bool
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)
93  {
94  int index = -1;
95  for(auto it = fields.begin(); it != fields.end(); ++it) {
96  // field name and type
97  const SolFieldName &solFieldName = it->first;
98  const PViewDataGModel::DataType &fieldType = it->second;
99  index++;
100 
101  // either get existing view data, or create new one
102  const std::string fullFieldName =
103  solFieldName.first + "_" + solFieldName.second;
105  fullFieldName, -1, -1); // DBGTT: to be checked for multi-file
106  PViewDataGModel *d;
107  bool create;
108  if(p != nullptr) {
109  d = dynamic_cast<PViewDataGModel *>(p->getData());
110  create = false;
111  }
112  else {
113  d = new PViewDataGModel(fieldType);
114  create = true;
115  }
116 
117  // read view data
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());
121  if(create) delete d;
122  return false;
123  }
124  else {
125  d->setName(fullFieldName);
126  d->setFileName(fileName);
127  d->setFileIndex(index);
128  if(create) new PView(d);
129  }
130  }
131 
132  return true;
133  }
134 
135 } // namespace
136 
137 bool PView::readCGNS(const std::vector<std::vector<MVertex *> > &vertPerZone,
138  const std::vector<std::vector<MElement *> > &eltPerZone,
139  const std::string &fileName)
140 {
141  int cgnsErr;
142 
143  // open CGNS file and read scale
144  int fileIndex = 0;
145  cgnsErr = cg_open(fileName.c_str(), CG_MODE_READ, &fileIndex);
146  if(cgnsErr != CG_OK) return cgnsError(__FILE__, __LINE__, fileIndex);
147 
148  // read base node
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);
154 
155  // read number of zones
156  int nbZone = 0;
157  cgnsErr = cg_nzones(fileIndex, baseIndex, &nbZone);
158  if(cgnsErr != CG_OK) return cgnsError(__FILE__, __LINE__, fileIndex);
159 
160  // get flow solutions names
161  std::map<SolFieldName, PViewDataGModel::DataType> fields;
162  int err = readFlowSolutionNames(fileIndex, baseIndex, nbZone, fields);
163  if(err == 0) return 0;
164 
165  // read field data
166  bool err2 = readFieldData(vertPerZone, eltPerZone, fileName, fileIndex,
167  baseIndex, fields);
168  if(!err2) return false;
169 
170  // close file
171  cgnsErr = cg_close(fileIndex);
172  if(cgnsErr != CG_OK) return cgnsError(__FILE__, __LINE__);
173 
174  return true;
175 }
176 
177 #else
178 
179 bool PView::readCGNS(const std::vector<std::vector<MVertex *> > &vertPerZone,
180  const std::vector<std::vector<MElement *> > &eltPerZone,
181  const std::string &fileName)
182 {
183  Msg::Error("Gmsh must be compiled with CGNS support to read '%s'",
184  fileName.c_str());
185  return false;
186 }
187 
188 #endif
PView
Definition: PView.h:27
PViewDataGModel::ElementData
@ ElementData
Definition: PViewDataGModel.h:195
PViewDataGModel
Definition: PViewDataGModel.h:191
PViewDataGModel::NodeData
@ NodeData
Definition: PViewDataGModel.h:194
PView::getViewByName
static PView * getViewByName(const std::string &name, int timeStep=-1, int partition=-1, const std::string &fileName="")
Definition: PView.cpp:347
Msg::Warning
static void Warning(const char *fmt,...)
Definition: GmshMessage.cpp:543
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
Vertex
Definition: Geo.h:29
PView.h
GmshMessage.h
PViewData.h
PViewData::setFileName
virtual void setFileName(const std::string &val)
Definition: PViewData.h:75
PViewDataGModel::readCGNS
bool readCGNS(const std::pair< std::string, std::string > &solFieldName, const std::string &fileName, int index, int fileIndex, int baseIndex, const std::vector< std::vector< MVertex * > > &vertPerZone, const std::vector< std::vector< MElement * > > &eltPerZone)
Definition: PViewDataGModelIO_CGNS.cpp:627
PView::readCGNS
static bool readCGNS(const std::vector< std::vector< MVertex * > > &vertPerZone, const std::vector< std::vector< MElement * > > &eltPerZone, const std::string &fileName)
Definition: PViewIO_CGNS.cpp:179
PView::getData
PViewData * getData(bool useAdaptiveIfAvailable=false)
Definition: PView.cpp:233
PViewData::setName
virtual void setName(const std::string &val)
Definition: PViewData.h:71
PViewDataGModel.h
CGNSConventions.h
CGNSCommon.h
PViewDataGModel::ElementNodeData
@ ElementNodeData
Definition: PViewDataGModel.h:196
PViewDataGModel::DataType
DataType
Definition: PViewDataGModel.h:193
PViewData::setFileIndex
virtual void setFileIndex(int val)
Definition: PViewData.h:87