NEMoSys  0.63.0
A modular, extensible resource with robust automated mesh generation, mesh quality analysis, adaptive mesh refinement, and data transfer between arbitrary meshes.
cgnsAnalyzer.H
Go to the documentation of this file.
1 /*******************************************************************************
2 * Promesh *
3 * Copyright (C) 2022, IllinoisRocstar LLC. All rights reserved. *
4 * *
5 * Promesh is the property of IllinoisRocstar LLC. *
6 * *
7 * IllinoisRocstar LLC *
8 * Champaign, IL *
9 * www.illinoisrocstar.com *
10 * promesh@illinoisrocstar.com *
11 *******************************************************************************/
12 /*******************************************************************************
13 * This file is part of Promesh *
14 * *
15 * This version of Promesh is free software: you can redistribute it and/or *
16 * modify it under the terms of the GNU Lesser General Public License as *
17 * published by the Free Software Foundation, either version 3 of the License, *
18 * or (at your option) any later version. *
19 * *
20 * Promesh is distributed in the hope that it will be useful, but WITHOUT ANY *
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more *
23 * details. *
24 * *
25 * You should have received a copy of the GNU Lesser General Public License *
26 * along with this program. If not, see <https://www.gnu.org/licenses/>. *
27 * *
28 *******************************************************************************/
29 #ifndef NEMOSYS_CGNSANALYZER_H_
30 #define NEMOSYS_CGNSANALYZER_H_
31 
32 // Nemosys headers
33 #include "nemosys_export.h"
34 #include "Mesh/meshBase.H"
35 
36 // VTK
37 #include <vtkDataSet.h>
38 #include <vtkSmartPointer.h>
39 
40 // MAdLib
41 #include <MAdLib.h>
42 
43 // CGNS
44 #include <cgnslib.h>
45 
46 // ANN
47 #include <ANN/ANN.h>
48 
49 // Eigen
50 //#include <Dense>
51 
52 // others
53 #include <map>
54 #include <string>
55 #include <vector>
56 
57 // enumerations
59 
60 // aliases
61 using keyValueList = std::map<int, std::string>;
62 // using MatrixInt = Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic>;
63 // using VectorInt = Eigen::Matrix<int, Eigen::Dynamic, 1>;
64 
65 class NEMOSYS_EXPORT solutionData {
66  // TODO change vtk specific stuff to use meshBase
67  using vecSlnType = std::vector<double>;
68 
69  public:
70  solutionData(std::string sName, solution_type_t inDataType)
71  : dataName(std::move(sName)), nDim(0), nData(0), dataType(inDataType) {}
72 
73  ~solutionData() = default;
74 
75  void appendData(const vecSlnType &data, int inNData, int inNDim);
76  void getData(vecSlnType &inBuff, int &outNData, int &outNDim) const;
77  void getData(vecSlnType &inBuff, int &outNData, int &outNDim,
78  const std::vector<bool> &mask) const;
79  void rmvDataIdx(const std::vector<int> &rmvIdx);
80 
81  std::string getDataName() const { return dataName; };
82 
83  int getNDim() const { return nDim; };
84 
85  int getNData() const { return nData; };
86 
87  solution_type_t getDataType() const { return dataType; };
88 
89  void clearBuff() {
90  dataName = "";
91  dataType = solution_type_t::UNKNOWN;
92  nDim = 0;
93  nData = 0;
94  slnData.clear();
95  };
96 
97  private:
99  std::string dataName;
100  int nDim;
101  int nData;
103 };
104 
105 class NEMOSYS_EXPORT cgnsAnalyzer {
106  public:
107  explicit cgnsAnalyzer(std::string fname, int verb = 0)
108  : cgFileName(std::move(fname)),
109  isUnstructured(false),
110  zoneType(CGNS_ENUMV(ZoneTypeNull)),
111  indexFile(-1),
112  indexBase(-1),
113  indexZone(-1),
114  indexCoord(-1),
115  cellDim(0),
116  physDim(0),
117  nVertex(0),
118  nRindNdeStr(0),
119  nElem(0),
120  nVrtxElem(0),
121  solutionDataPopulated(false),
122  kdTree(nullptr),
123  kdTreeElem(nullptr),
124  vrtxCrd(nullptr),
125  vrtxIdx(nullptr),
126  searchEps(1e-9),
127  isMltZone(false),
128  vtkMesh(nullptr),
129  _verb(verb),
130  _rindOff(false) {
131  cgRindCellIds.clear();
132  cgRindNodeIds.clear();
133  };
134 
135  // destructor
136  virtual ~cgnsAnalyzer() {
137  // close file if still open
138  if (indexFile > 0) cg_close(indexFile);
139  delete kdTree;
140  delete kdTreeElem;
141  if (vrtxCrd) annDeallocPts(vrtxCrd);
142  if (vrtxIdx) annDeallocPts(vrtxIdx);
143  clearAllSolutionData();
144  };
145 
146  // top-level loading mesh
147  void loadGrid(const std::string &fname, int verb = 0) {
148  cgFileName = fname;
149  loadGrid(verb);
150  };
151 
152  // closing cgns file
153  virtual void closeCG() {
154  if (indexFile > 0) {
155  cg_close(indexFile);
156  indexFile = -1;
157  }
158  };
159 
160  // general purpose IO
161  void loadGrid(int verb = 0);
162  void loadZone(int zIdx, int verb = 0);
163 
164  // mesh information access
165  int getIndexFile();
166  int getIndexBase();
167  int getCellDim();
168  std::string getFileName();
169  std::string getBaseName();
170  std::string getZoneName();
171  std::string getZoneName(int nCgFile);
172  std::string getSectionName();
173  std::string getBaseItrName();
174  int getNZone();
175  int getNTStep();
176  double getTimeStep();
177  std::string getZoneItrName();
178  std::string getGridCrdPntr();
179  std::string getSolutionPntr();
180  int getNVertex();
181  int getNElement();
182  int getPhysDim();
183  int getElementType();
184  int getNVrtxElem();
185  CGNS_ENUMT(MassUnits_t) getMassUnit();
186  CGNS_ENUMT(LengthUnits_t) getLengthUnit();
187  CGNS_ENUMT(TimeUnits_t) getTimeUnit();
188  CGNS_ENUMT(TemperatureUnits_t) getTemperatureUnit();
189  CGNS_ENUMT(AngleUnits_t) getAngleUnit();
190  CGNS_ENUMT(ZoneType_t) getZoneType();
191  bool isStructured();
192  std::vector<double> getVertexCoords();
193  std::vector<double> getVertexCoords(int vrtxId);
194  double getVrtXCrd(int vrtxId);
195  std::vector<double> getVrtXCrd();
196  double getVrtYCrd(int vrtxId);
197  std::vector<double> getVrtYCrd();
198  double getVrtZCrd(int vrtxId);
199  std::vector<double> getVrtZCrd();
200  std::vector<cgsize_t> getElementConnectivity(int elemId);
201 
202  // other general purpose mesh query (such as virtual meshes)
203  void getSectionNames(std::vector<std::string> &secNames);
204  void getSectionConn(std::string secName, std::vector<cgsize_t> &conn,
205  int &nElm);
206  vtkSmartPointer<vtkDataSet> getSectionMesh(std::string secName);
207 
208  // get solution information
209  void clearAllSolutionData();
210  void getSolutionDataNames(std::vector<std::string> &list);
211  solution_type_t getSolutionData(std::string sName,
212  std::vector<double> &slnData);
213  solutionData *getSolutionDataObj(std::string sName); // reads from CGNS file
214  int getNVertexSolution();
215  int getNCellSolution();
216  solution_type_t getSolutionDataStitched(std::string sName,
217  std::vector<double> &slnData,
218  int &outNData, int &outNDim);
219 
220  // append and query new solution/bc fields
221  void appendSolutionData(std::string sName, std::vector<double> &slnData,
222  solution_type_t dt, int inNData, int inNDim);
223  void appendSolutionData(std::string sName, double slnData, solution_type_t dt,
224  int inNData, int inNDim);
225  bool delAppSlnData(std::string sName);
226  void getAppendedSolutionDataName(std::vector<std::string> &appSName);
227 
228  // access solution data related management data structures
229  std::vector<std::string> getSolutionNodeNames();
230  std::vector<CGNS_ENUMT(GridLocation_t)> getSolutionGridLocations();
231  std::map<int, std::pair<int, keyValueList>> getSolutionMap();
232  std::map<std::string, CGNS_ENUMT(GridLocation_t)> getSolutionNameLocMap();
233 
234  // mesh processing
235  void exportToMAdMesh(MAd::pMesh MAdMesh);
236  virtual void stitchMesh(cgnsAnalyzer *inCg, bool withFields = false);
237  void classifyMAdMeshOpt(MAd::pMesh MAdMesh);
238  void classifyMAdMeshBnd(MAd::pMesh MAdMesh);
239  void unclassifyMAdMeshBnd(MAd::pMesh MAdMesh);
240  void checkVertex();
241  bool checkElmConn(int nSharedNde);
242  std::vector<double> getElmCntCoords(MAd::pMesh msh);
243 
244  // export to vtk format without going through madlib/gmsh (needed for
245  // simmetrix interface)
246  void exportToVTKMesh();
247  vtkSmartPointer<vtkDataSet> getVTKMesh();
248 
249  // write data from existing vtkdataset into open cgns file (for replacing data
250  // in file)
251  void overwriteSolData(meshBase *mbObj);
252  void overwriteSolData(const std::string &fname, const std::string &ndeName,
253  int slnIdx, CGNS_ENUMT(DataType_t) dt, void *data);
254 
255  // utilities
256  void writeSampleStructured();
257  void writeSampleUnstructured();
258 
259  // others (special purpose)
260  bool isMultiZone();
261  bool isCgRindNode(int cgNdeId);
262  bool isCgRindCell(int cgCellId);
263  void cleanRind();
264 
265  protected:
266  void populateSolutionDataNames();
267  void buildVertexKDTree();
268  void buildElementKDTree();
269  void loadSolutionDataContainer(int verb = 0);
270  virtual void stitchFields(cgnsAnalyzer *inCg);
271  CGNS_ENUMT(ElementType_t) getSectionType(std::string secName);
272 
273  protected:
274  std::string cgFileName;
275  // type of the grid
276  std::string baseName, zoneName, sectionName;
278  CGNS_ENUMT(ZoneType_t) zoneType;
279  CGNS_ENUMT(ElementType_t) sectionType;
280  // units
281  CGNS_ENUMT(MassUnits_t) massU;
282  CGNS_ENUMT(LengthUnits_t) lengthU;
283  CGNS_ENUMT(TimeUnits_t) timeU;
284  CGNS_ENUMT(TemperatureUnits_t) tempU;
285  CGNS_ENUMT(AngleUnits_t) angleU;
286  // grid ints
287  cgsize_t rmin[3], rmax[3];
288  cgsize_t cgCoreSize[9];
289  int indexFile, indexBase;
290  int indexZone, indexCoord;
292  int cellDim, physDim;
293  int nBase;
294  int nZone;
295  int nVertex;
297  int nElem;
298  int nSection;
300  // time-dependent data
301  std::string baseItrName, zoneItrName;
302  std::string gridCrdPntr, flowSlnPntr;
303  int nTStep;
304  double timeLabel;
305  // mesh data
306  std::vector<double> xCrd, yCrd, zCrd;
307  std::vector<cgsize_t> elemConn;
308  // solution data
311  int nField;
312  std::vector<solutionData *> slnDataCont;
313  std::map<std::string, CGNS_ENUMT(GridLocation_t)> solutionNameLocMap;
314  std::map<int, std::pair<int, keyValueList>>
315  solutionMap; // (#sln, <slnIdx, (fldIdx, fldName)>)
316  std::vector<std::string> solutionName;
317  std::vector<CGNS_ENUMT(GridLocation_t)> solutionGridLocation;
318  std::vector<std::string> appendedSolutionName;
319  // export variables
320  std::map<int, int> MAdToCgnsIds;
321  std::map<int, int> cgnsToMAdIds;
322  // search support data structures
323  ANNkd_tree *kdTree;
324  ANNkd_tree *kdTreeElem;
325  ANNpointArray vrtxCrd;
326  ANNpointArray vrtxIdx;
327  double searchEps;
328  // stitching data support
329  std::vector<std::string> zoneNames;
330  std::vector<bool> vrtDataMask;
331  std::vector<bool> elmDataMask;
332  // special purpose cgns files
333  bool isMltZone;
334  std::vector<std::string> multZoneNames;
335  vtkSmartPointer<vtkDataSet> vtkMesh;
336  std::vector<int> cgRindCellIds;
337  std::vector<int> cgRindNodeIds;
338  // other flags
339  int _verb;
340  bool _rindOff;
341 };
342 
343 #endif // NEMOSYS_CGNSANALYZER_H_
std::vector< double > vecSlnType
Definition: cgnsAnalyzer.H:67
data_type data
Edge/face with sorted point ids (a, b, c, ...) is located at some index i in data[b], with data[b][i].first == [a, c] (for edges, third point id treated as -1).
solutionData(std::string sName, solution_type_t inDataType)
Definition: cgnsAnalyzer.H:70
void clearBuff()
Definition: cgnsAnalyzer.H:89
vecSlnType slnData
Definition: cgnsAnalyzer.H:95
ANNpointArray vrtxIdx
Definition: cgnsAnalyzer.H:326
std::vector< bool > elmDataMask
Definition: cgnsAnalyzer.H:331
std::vector< std::string > appendedSolutionName
Definition: cgnsAnalyzer.H:318
double timeLabel
Definition: cgnsAnalyzer.H:304
std::vector< std::string > solutionName
Definition: cgnsAnalyzer.H:316
std::string zoneName
Definition: cgnsAnalyzer.H:276
A brief description of meshBase.
Definition: meshBase.H:64
int getNDim() const
Definition: cgnsAnalyzer.H:83
STL namespace.
std::vector< int > cgRindNodeIds
Definition: cgnsAnalyzer.H:337
std::string dataName
Definition: cgnsAnalyzer.H:99
std::vector< double > zCrd
Definition: cgnsAnalyzer.H:306
ANNkd_tree * kdTreeElem
Definition: cgnsAnalyzer.H:324
double searchEps
Definition: cgnsAnalyzer.H:327
std::map< int, std::pair< int, keyValueList > > solutionMap
Definition: cgnsAnalyzer.H:315
std::vector< std::string > multZoneNames
Definition: cgnsAnalyzer.H:334
std::map< int, int > MAdToCgnsIds
Definition: cgnsAnalyzer.H:320
ANNpointArray vrtxCrd
Definition: cgnsAnalyzer.H:325
std::map< std::string, CGNS_ENUMT(GridLocation_t)> solutionNameLocMap
Definition: cgnsAnalyzer.H:313
solution_type_t
Definition: cgnsAnalyzer.H:58
vtkSmartPointer< vtkDataSet > vtkMesh
Definition: cgnsAnalyzer.H:335
std::vector< std::string > zoneNames
Definition: cgnsAnalyzer.H:329
solution_type_t dataType
Definition: cgnsAnalyzer.H:102
std::vector< int > cgRindCellIds
Definition: cgnsAnalyzer.H:336
std::string gridCrdPntr
Definition: cgnsAnalyzer.H:302
int getNData() const
Definition: cgnsAnalyzer.H:85
std::vector< bool > vrtDataMask
Definition: cgnsAnalyzer.H:330
bool solutionDataPopulated
Definition: cgnsAnalyzer.H:309
CGNS_ENUMT(MassUnits_t)
Definition: cgnsAnalyzer.C:457
ANNkd_tree * kdTree
Definition: cgnsAnalyzer.H:323
std::string zoneItrName
Definition: cgnsAnalyzer.H:301
void loadGrid(const std::string &fname, int verb=0)
Definition: cgnsAnalyzer.H:147
std::vector< cgsize_t > elemConn
Definition: cgnsAnalyzer.H:307
std::string getDataName() const
Definition: cgnsAnalyzer.H:81
bool isUnstructured
Definition: cgnsAnalyzer.H:277
std::string cgFileName
Definition: cgnsAnalyzer.H:271
std::vector< solutionData * > slnDataCont
Definition: cgnsAnalyzer.H:312
std::vector< CGNS_ENUMT(GridLocation_t)> solutionGridLocation
Definition: cgnsAnalyzer.H:317
std::map< int, std::string > keyValueList
Definition: cgnsAnalyzer.H:61
solution_type_t getDataType() const
Definition: cgnsAnalyzer.H:87
virtual ~cgnsAnalyzer()
Definition: cgnsAnalyzer.H:136
virtual void closeCG()
Definition: cgnsAnalyzer.H:153
cgnsAnalyzer(std::string fname, int verb=0)
Definition: cgnsAnalyzer.H:107
std::map< int, int > cgnsToMAdIds
Definition: cgnsAnalyzer.H:321