File: PViewIO.cpp

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 (447 lines) | stat: -rw-r--r-- 13,614 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// 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.

#include "GmshConfig.h"
#include "GmshMessage.h"
#include "PView.h"
#include "PViewData.h"
#include "PViewOptions.h"
#include "PViewDataList.h"
#include "PViewDataGModel.h"
#include "VertexArray.h"
#include "StringUtils.h"
#include "Context.h"
#include "OS.h"
#include "adaptiveData.h"

bool PView::readPOS(const std::string &fileName, int fileIndex)
{
  FILE *fp = Fopen(fileName.c_str(), "rb");
  if(!fp) {
    Msg::Error("Unable to open file '%s'", fileName.c_str());
    return false;
  }

  char str[256] = "XXX";
  double version = -1.;
  int format = -1, size = -1, index = -1;

  while(1) {
    while(str[0] != '$') {
      if(!fgets(str, sizeof(str), fp) || feof(fp)) break;
    }

    if(feof(fp)) break;

    if(!strncmp(&str[1], "PostFormat", 10)) {
      if(!fscanf(fp, "%lf %d %d\n", &version, &format, &size)) {
        Msg::Error("Read error");
        fclose(fp);
        return false;
      }
      if(version < 1.0) {
        Msg::Error("Post-processing file too old (ver. %g < 1.0)", version);
        fclose(fp);
        return false;
      }
      if(size == sizeof(double))
        Msg::Debug("Data is in double precision format (size==%d)", size);
      else {
        Msg::Error("Unknown data size (%d) in post-processing file", size);
        fclose(fp);
        return false;
      }
    }
    else if(!strncmp(&str[1], "View", 4)) {
      index++;
      if(fileIndex < 0 || fileIndex == index) {
        PViewDataList *d = new PViewDataList();
        if(!d->readPOS(fp, version, format ? true : false)) {
          Msg::Error("Could not read data in list format");
          delete d;
          fclose(fp);
          return false;
        }
        else {
          d->setFileName(fileName);
          d->setFileIndex(index);
          new PView(d);
        }
      }
    }

    do {
      if(!fgets(str, sizeof(str), fp) || feof(fp)) break;
    } while(str[0] != '$');
  }

  fclose(fp);
  return true;
}

bool PView::readMSH(const std::string &fileName, int fileIndex,
                    int partitionToRead)
{
  FILE *fp = Fopen(fileName.c_str(), "rb");
  if(!fp) {
    Msg::Error("Unable to open file '%s'", fileName.c_str());
    return false;
  }

  GModel *model = GModel::current();
  if(model->empty()) {
    Msg::Error("Model is empty: please load a mesh before loading the dataset");
    fclose(fp);
    return false;
  }

  char str[256] = "XXX";
  int index = -1;
  bool binary = false, swap = false;

  while(1) {
    while(str[0] != '$') {
      if(!fgets(str, sizeof(str), fp) || feof(fp)) break;
    }

    if(feof(fp)) break;

    if(!strncmp(&str[1], "MeshFormat", 10)) {
      double version;
      if(!fgets(str, sizeof(str), fp)) {
        fclose(fp);
        return false;
      }
      int format, size;
      if(sscanf(str, "%lf %d %d", &version, &format, &size) != 3) {
        fclose(fp);
        return false;
      }
      if(format) {
        binary = true;
        Msg::Debug("View data is in binary format");
        int one;
        if(fread(&one, sizeof(int), 1, fp) != 1) {
          fclose(fp);
          return 0;
        }
        if(one != 1) {
          swap = true;
          Msg::Debug("Swapping bytes from binary file");
        }
      }
    }
    else if(!strncmp(&str[1], "InterpolationScheme", 19)) {
      std::string name;
      if(!fgets(str, sizeof(str), fp)) {
        fclose(fp);
        return false;
      }
      name = ExtractDoubleQuotedString(str, sizeof(str));
      Msg::Debug("Reading interpolation scheme '%s'", name.c_str());
      PViewData::removeInterpolationScheme(name);
      int numTypes;
      if(fscanf(fp, "%d", &numTypes) != 1) {
        fclose(fp);
        return false;
      }
      for(int i = 0; i < numTypes; i++) {
        int type, numMatrices;
        if(fscanf(fp, "%d %d", &type, &numMatrices) != 2) {
          fclose(fp);
          return false;
        }
        for(int j = 0; j < numMatrices; j++) {
          int m, n;
          if(fscanf(fp, "%d %d", &m, &n) != 2) {
            fclose(fp);
            return false;
          }
          fullMatrix<double> mat(m, n);
          for(int k = 0; k < m; k++) {
            for(int l = 0; l < n; l++) {
              double d;
              if(fscanf(fp, "%lf", &d) != 1) {
                fclose(fp);
                return false;
              }
              mat.set(k, l, d);
            }
          }
          PViewData::addMatrixToInterpolationScheme(name, type, mat);
        }
      }
    }
    else if(!strncmp(&str[1], "NodeData", 8) ||
            !strncmp(&str[1], "ElementData", 11) ||
            !strncmp(&str[1], "ElementNodeData", 15)) {
      index++;
      if(fileIndex < 0 || fileIndex == index) {
        PViewDataGModel::DataType type;
        if(!strncmp(&str[1], "NodeData", 8))
          type = PViewDataGModel::NodeData;
        else if(!strncmp(&str[1], "ElementData", 11))
          type = PViewDataGModel::ElementData;
        else
          type = PViewDataGModel::ElementNodeData;
        int numTags;
        // string tags
        std::string viewName, interpolationScheme;
        if(!fgets(str, sizeof(str), fp)) {
          fclose(fp);
          return false;
        }
        if(sscanf(str, "%d", &numTags) != 1) {
          fclose(fp);
          return false;
        }
        for(int i = 0; i < numTags; i++) {
          if(!fgets(str, sizeof(str), fp)) {
            fclose(fp);
            return false;
          }
          if(i == 0)
            viewName = ExtractDoubleQuotedString(str, sizeof(str));
          else if(i == 1)
            interpolationScheme = ExtractDoubleQuotedString(str, sizeof(str));
        }
        // double tags
        double time = 0.;
        if(!fgets(str, sizeof(str), fp)) {
          fclose(fp);
          return false;
        }
        if(sscanf(str, "%d", &numTags) != 1) {
          fclose(fp);
          return false;
        }
        for(int i = 0; i < numTags; i++) {
          if(!fgets(str, sizeof(str), fp)) {
            fclose(fp);
            return false;
          }
          if(i == 0) {
            if(sscanf(str, "%lf", &time) != 1) {
              fclose(fp);
              return false;
            }
          }
        }
        // integer tags
        int timeStep = 0, numComp = 0, numEnt = 0, partition = 0;
        long int blocksize = 0;
        if(!fgets(str, sizeof(str), fp)) {
          fclose(fp);
          return false;
        }
        if(sscanf(str, "%d", &numTags) != 1) {
          fclose(fp);
          return false;
        }
        for(int i = 0; i < numTags; i++) {
          if(!fgets(str, sizeof(str), fp)) {
            fclose(fp);
            return false;
          }
          if(i == 0) {
            if(sscanf(str, "%d", &timeStep) != 1) {
              fclose(fp);
              return false;
            }
          }
          else if(i == 1) {
            if(sscanf(str, "%d", &numComp) != 1) {
              fclose(fp);
              return false;
            }
          }
          else if(i == 2) {
            if(sscanf(str, "%d", &numEnt) != 1) {
              fclose(fp);
              return false;
            }
          }
          else if(i == 3) {
            if(sscanf(str, "%d", &partition) != 1) {
              fclose(fp);
              return false;
            }
          }
          else if(i == 4) {
            if(sscanf(str, "%ld", &blocksize) != 1) {
              fclose(fp);
              return false;
            }
          }
        }
        if(partitionToRead == -1 || partitionToRead == partition) {
          // if default (no particular partition requested from MergeFile -> -1)
          // or if current partition corresponds to the requested partition,
          // read the data
          if(numEnt > 0) {
            // either get existing viewData, or create new one
            PView *p = getViewByName(viewName, timeStep, partition);
            PViewDataGModel *d = nullptr;
            if(p) d = dynamic_cast<PViewDataGModel *>(p->getData());
            bool create = d ? false : true;
            if(create) d = new PViewDataGModel(type);
            if(!d->readMSH(viewName, fileName, fileIndex, fp, binary, swap,
                           timeStep, time, partition, numComp, numEnt,
                           interpolationScheme)) {
              Msg::Error("Could not read data in msh file");
              if(create) delete d;
              fclose(fp);
              return false;
            }
            else {
              d->setName(viewName);
              d->setFileName(fileName);
              d->setFileIndex(index);
              if(create) new PView(d);
            }
          }
        }
        else if(blocksize > 0 && partitionToRead != partition) {
          // if current partition does not correspond to the requested partition
          // and if its blocksise has been read (5th integer in the header),
          // jump over it
          fseek(fp, blocksize, SEEK_CUR);
        }
      }
    }

    do {
      if(!fgets(str, sizeof(str), fp) || feof(fp)) break;
    } while(str[0] != '$');
  }

  fclose(fp);
  return true;
}

#if defined(HAVE_MED)

extern std::vector<std::string> medGetFieldNames(const std::string &fileName);

bool PView::readMED(const std::string &fileName, int fileIndex)
{
  std::vector<std::string> fieldNames = medGetFieldNames(fileName);

  for(std::size_t index = 0; index < fieldNames.size(); index++) {
    if(fileIndex < 0 || (int)index == fileIndex) {
      PViewDataGModel *d = nullptr;
      // we use the filename as a kind of "partition" indicator, allowing to
      // complete datasets provided in separate files (e.g. coming from DDM)
      PView *p = getViewByName(fieldNames[index], -1, -1, fileName);
      if(p) d = dynamic_cast<PViewDataGModel *>(p->getData());
      bool create = d ? false : true;
      if(create) d = new PViewDataGModel();
      if(!d->readMED(fileName, index)) {
        Msg::Error("Could not read data in MED file");
        if(create) delete d;
        return false;
      }
      else {
        if(create) new PView(d);
      }
    }
  }

  return true;
}

#else

bool PView::readMED(const std::string &fileName, int fileIndex)
{
  Msg::Error("Gmsh must be compiled with MED support to read '%s'",
             fileName.c_str());
  return false;
}

#endif

bool PView::readPCH(const std::string &fileName, int fileIndex)
{
  PViewDataGModel::DataType type = PViewDataGModel::NodeData;
  // PViewDataGModel::ElementData;
  // PViewDataGModel::ElementNodeData;
  PViewDataGModel *d = new PViewDataGModel(type);
  d->setFileName(fileName);
  d->readPCH(fileName, fileIndex);
  new PView(d);
  return true;
}

bool PView::write(const std::string &fileName, int format, bool append)
{
  Msg::StatusBar(true, "Writing '%s'...", fileName.c_str());

  bool ret;
  switch(format) {
  case 0: ret = _data->writePOS(fileName, false, false, append); break; // ASCII
  case 1: ret = _data->writePOS(fileName, true, false, append); break; // binary
  case 2: ret = _data->writePOS(fileName, false, true, append); break; // parsed
  case 3: ret = _data->writeSTL(fileName); break;
  case 4: ret = _data->writeTXT(fileName); break;
  case 5:
    ret = _data->writeMSH(fileName, CTX::instance()->mesh.mshFileVersion,
                          CTX::instance()->mesh.binary,
                          CTX::instance()->post.saveMesh, append, 0,
                          CTX::instance()->post.saveInterpolationMatrices,
                          CTX::instance()->post.forceNodeData,
                          CTX::instance()->post.forceElementData);
    break;
  case 6: ret = _data->writeMED(fileName); break;
  case 7: ret = writeX3D(fileName); break;
  case 10: {
    std::string ext = SplitFileName(fileName)[2];
    if(ext == ".pos")
      ret = _data->writePOS(fileName, false, true, append);
    else if(ext == ".stl")
      ret = _data->writeSTL(fileName);
    else if(ext == ".msh")
      ret = _data->writeMSH(fileName, CTX::instance()->mesh.mshFileVersion,
                            CTX::instance()->mesh.binary,
                            CTX::instance()->post.saveMesh, append, 0,
                            CTX::instance()->post.saveInterpolationMatrices,
                            CTX::instance()->post.forceNodeData,
                            CTX::instance()->post.forceElementData);
    else if(ext == ".med")
      ret = _data->writeMED(fileName);
    else if(ext == ".x3d")
      ret = writeX3D(fileName);
    else
      ret = _data->writeTXT(fileName);
    break;
  }
  default:
    ret = false;
    Msg::Error("Unknown view format %d", format);
    break;
  }

  if(ret) Msg::StatusBar(true, "Done writing '%s'", fileName.c_str());
  return ret;
}

// Routines for export of adapted views to pvtu file format for parallel
// visualization with paraview.
bool PView::writeAdapt(const std::string &guifileName, int useDefaultName,
                       bool isBinary, int adaptLev, double adaptErr, int npart,
                       bool append)
{
  Msg::StatusBar(true, "Writing '%s'...", guifileName.c_str());
  _data->saveAdaptedViewForVTK(guifileName, useDefaultName, _options->timeStep,
                               adaptLev, adaptErr, npart, isBinary);
  return true;
}

void PView::sendToServer(const std::string &name)
{
  Msg::Info("Sending View[%d] to ONELAB as parameter '%s'", _index,
            name.c_str());
  _data->sendToServer(name);
}