File: PViewDataGModel.h

package info (click to toggle)
gmsh 4.8.4%2Bds2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 87,812 kB
  • sloc: cpp: 378,014; ansic: 99,669; yacc: 7,216; python: 6,680; java: 3,486; lisp: 659; lex: 621; perl: 571; makefile: 470; sh: 440; xml: 415; javascript: 113; pascal: 35; modula3: 32
file content (300 lines) | stat: -rw-r--r-- 11,385 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Gmsh - Copyright (C) 1997-2021 C. Geuzaine, J.-F. Remacle
//
// See the LICENSE.txt file for license information. Please report all
// issues on https://gitlab.onelab.info/gmsh/gmsh/issues.

#ifndef PVIEW_DATA_GMODEL_H
#define PVIEW_DATA_GMODEL_H

#include "PViewData.h"
#include "GModel.h"
#include "SBoundingBox3d.h"

template <class Real> class stepData {
private:
  // a pointer to the underlying model
  GModel *_model;
  // the unrolled list of all geometrical entities in the model
  std::vector<GEntity *> _entities;
  // the bounding box of the view
  SBoundingBox3d _bbox;
  // the file the data was read from (if empty, refer to PViewData)
  std::string _fileName;
  // the index in the file (if negative, refer to PViewData)
  int _fileIndex;
  // the value of the time step and value min/max
  double _time, _min, _max;
  // the number of components in the data (one stepData contains only
  // a single field type)
  int _numComp;
  // the values, indexed by MVertex or MElement id numbers (If the
  // numbering is sparse, or if we only have data for high-id
  // entities, the vector has zero entries and is thus not
  // optimal. This is the price to pay if we want 1) rapid access to
  // the data and 2) not to store any additional info in MVertex or
  // MElement)
  //
  // FIXME: we should change this design and store a vector<int> of tags, and do
  // indirect addressing, even if it's a bit slower...
  std::vector<Real *> *_data;
  // a vector containing the multiplying factor allowing to compute
  // the number of values stored in _data for each index (number of
  // values = getMult() * getNumComponents()). If _mult is empty, a
  // default value of "1" is assumed
  std::vector<int> _mult;
  // a vector, indexed by MSH element type, of Gauss point locations
  // in parametric space
  std::vector<std::vector<double> > _gaussPoints;
  // a set of all "partitions" encountered in the data
  std::set<int> _partitions;

public:
  stepData(GModel *model, int numComp, const std::string &fileName = "",
           int fileIndex = -1, double time = 0., double min = VAL_INF,
           double max = -VAL_INF)
    : _model(model), _fileName(fileName), _fileIndex(fileIndex), _time(time),
      _min(min), _max(max), _numComp(numComp), _data(0)
  {
  }
  stepData(stepData<Real> &other) : _data(0)
  {
    _model = other._model;
    _entities = other._entities;
    _bbox = other._bbox;
    _fileName = other._fileName;
    _fileIndex = other._fileIndex;
    _time = other._time;
    _min = other._min;
    _max = other._max;
    _numComp = other._numComp;
    if(other._data) {
      std::size_t n = other.getNumData();
      _data = new std::vector<Real *>(n, (Real *)0);
      for(std::size_t i = 0; i < n; i++) {
        Real *d = other.getData(i);
        if(d) {
          int m = other.getMult(i) * _numComp;
          (*_data)[i] = new Real[m];
          for(int j = 0; j < m; j++) (*_data)[i][j] = d[j];
        }
      }
    }
    _mult = other._mult;
    _gaussPoints = other._gaussPoints;
    _partitions = other._partitions;
  }
  ~stepData() { destroyData(); }
  void fillEntities() { _model->getEntities(_entities); }
  void computeBoundingBox() { _bbox = _model->bounds(); }
  GModel *getModel() { return _model; }
  SBoundingBox3d getBoundingBox() { return _bbox; }
  int getNumEntities() { return _entities.size(); }
  GEntity *getEntity(int ent) { return _entities[ent]; }
  int getNumComponents() { return _numComp; }
  int getMult(int index)
  {
    if(index < 0 || index >= (int)_mult.size()) return 1;
    return _mult[index];
  }
  std::string getFileName() { return _fileName; }
  void setFileName(const std::string &name) { _fileName = name; }
  int getFileIndex() { return _fileIndex; }
  void setFileIndex(int index) { _fileIndex = index; }
  double getTime() { return _time; }
  void setTime(double time) { _time = time; }
  double getMin() { return _min; }
  void setMin(double min) { _min = min; }
  double getMax() { return _max; }
  void setMax(double max) { _max = max; }
  std::size_t getNumData()
  {
    if(!_data) return 0;
    return _data->size();
  }
  void resizeData(int n)
  {
    if(!_data) _data = new std::vector<Real *>(n, (Real *)0);
    if(n > (int)_data->size()) _data->resize(n, (Real *)0);
  }
  Real *getData(int index, bool allocIfNeeded = false, int mult = 1)
  {
    if(index < 0) return 0;
    if(allocIfNeeded) {
      if(index >= (int)getNumData()) resizeData(index + 100); // optimize this
      if(!(*_data)[index]) {
        (*_data)[index] = new Real[_numComp * mult];
        for(int i = 0; i < _numComp * mult; i++) (*_data)[index][i] = 0.;
      }
      if(mult > 1) {
        if(index >= (int)_mult.size())
          _mult.resize(index + 100, 1); // optimize this
        _mult[index] = mult;
      }
    }
    else {
      if(index >= (int)getNumData()) return 0;
    }
    return (*_data)[index];
  }
  void destroyData()
  {
    if(_data) {
      for(unsigned int i = 0; i < _data->size(); i++)
        if((*_data)[i]) delete[](*_data)[i];
      delete _data;
      _data = 0;
    }
  }
  std::vector<double> &getGaussPoints(int msh)
  {
    if((int)_gaussPoints.size() <= msh) _gaussPoints.resize(msh + 1);
    return _gaussPoints[msh];
  }
  std::set<int> &getPartitions() { return _partitions; }
  double getMemoryInMb()
  {
    double b = 0.;
    for(std::size_t i = 0; i < getNumData(); i++) b += getMult(i);
    return b * getNumComponents() * sizeof(Real) / 1024. / 1024.;
  }
};

// The data container using elements from one or more GModel(s).
class PViewDataGModel : public PViewData {
public:
  enum DataType {
    NodeData = 1,
    ElementData = 2,
    ElementNodeData = 3,
    GaussPointData = 4,
    BeamData = 5
  };

private:
  // the data, indexed by time step
  std::vector<stepData<double> *> _steps;
  // the global min/max of the view
  double _min, _max;
  // the type of the dataset
  DataType _type;
  // cache last element to speed up loops
  MElement *_getElement(int step, int ent, int ele);
  MVertex *_getNode(MElement *e, int nod);

public:
  PViewDataGModel(DataType type = NodeData);
  ~PViewDataGModel();
  bool finalize(bool computeMinMax = true,
                const std::string &interpolationScheme = "");
  std::string getFileName(int step = -1);
  int getNumTimeSteps();
  int getFirstNonEmptyTimeStep(int start = 0);
  double getTime(int step);
  double getMin(int step = -1, bool onlyVisible = false, int tensorRep = 0,
                int forceNumComponents = 0, int componentMap[9] = nullptr);
  double getMax(int step = -1, bool onlyVisible = false, int tensorRep = 0,
                int forceNumComponents = 0, int componentMap[9] = nullptr);
  void setMin(double min) { _min = min; }
  void setMax(double max) { _max = max; }
  SBoundingBox3d getBoundingBox(int step = -1);
  void setBoundingBox(SBoundingBox3d &box) {}
  int getNumScalars(int step = -1);
  int getNumVectors(int step = -1);
  int getNumTensors(int step = -1);
  int getNumPoints(int step = -1);
  int getNumLines(int step = -1);
  int getNumTriangles(int step = -1);
  int getNumQuadrangles(int step = -1);
  int getNumPolygons(int step = -1);
  int getNumTetrahedra(int step = -1);
  int getNumHexahedra(int step = -1);
  int getNumPrisms(int step = -1);
  int getNumPyramids(int step = -1);
  int getNumTrihedra(int step = -1);
  int getNumPolyhedra(int step = -1);
  int getNumEntities(int step = -1);
  int getNumElements(int step = -1, int ent = -1);
  int getDimension(int step, int ent, int ele);
  int getNumNodes(int step, int ent, int ele);
  int getNode(int step, int ent, int ele, int nod, double &x, double &y,
              double &z);
  void setNode(int step, int ent, int ele, int nod, double x, double y,
               double z);
  void tagNode(int step, int ent, int ele, int nod, int tag);
  int getNumComponents(int step, int ent, int ele);
  int getNumValues(int step, int ent, int ele);
  void getValue(int step, int ent, int ele, int idx, double &val);
  void getValue(int step, int ent, int ele, int node, int comp, double &val);
  void setValue(int step, int ent, int ele, int node, int comp, double val);
  int getNumEdges(int step, int ent, int ele);
  int getType(int step, int ent, int ele);
  void reverseElement(int step, int ent, int ele);
  void smooth();
  double getMemoryInMb();
  bool combineTime(nameData &nd);
  bool skipEntity(int step, int ent);
  bool skipElement(int step, int ent, int ele, bool checkVisibility = false,
                   int samplingRate = 1);
  bool hasTimeStep(int step);
  bool hasPartition(int step, int part);
  bool hasMultipleMeshes();
  bool hasModel(GModel *model, int step = -1);
  bool isNodeData() { return _type == NodeData; }
  bool useGaussPoints() { return _type == GaussPointData; }
  GModel *getModel(int step) { return _steps[step]->getModel(); }
  GEntity *getEntity(int step, int ent);
  MElement *getElement(int step, int entity, int element);

  // get the data type
  DataType getType() { return _type; }
  // direct access to value by index
  bool getValueByIndex(int step, int dataIndex, int node, int comp,
                       double &val);

  // Add some data "on the fly" (data is stored in a map, indexed by
  // node or element number depending on the type of dataset)
  bool addData(GModel *model, const std::map<int, std::vector<double> > &data,
               int step, double time, int partition, int numComp);

  // Add some data "on the fly", without a map
  bool addData(GModel *model, const std::vector<std::size_t> &tags,
               const std::vector<std::vector<double> > &data, int step,
               double time, int partition, int numComp);

  // Add homogeneous data "on the fly", without a map
  bool addData(GModel *model, const std::vector<std::size_t> &tags,
               const std::vector<double> &data, int step, double time,
               int partition, int numComp);

  // Allow to destroy the data
  void destroyData();
  // I/O routines
  bool readMSH(const std::string &viewName, const std::string &fileName,
               int fileIndex, FILE *fp, bool binary, bool swap, int step,
               double time, int partition, int numComp, int numNodes,
               const std::string &interpolationScheme);
  virtual bool writeMSH(const std::string &fileName, double version = 2.2,
                        bool binary = false, bool savemesh = true,
                        bool multipleView = false, int partitionNum = 0,
                        bool saveInterpolationMatrices = true,
                        bool forceNodeData = false,
                        bool forceElementData = false);
  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);
  bool readMED(const std::string &fileName, int fileIndex);
  bool writeMED(const std::string &fileName);
  bool readPCH(const std::string &fileName, int fileIndex);

  void importLists(int N[24], std::vector<double> *V[24]);
  stepData<double> *getStepData(int step)
  {
    if(step >= 0 && step < (int)_steps.size()) return _steps[step];
    return nullptr;
  }
  void sendToServer(const std::string &name);
};

#endif