gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
GModelIO_MSH.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 <stdio.h>
7 #include <string.h>
8 #include "GModel.h"
9 #include "OS.h"
10 #include "GmshMessage.h"
11 
12 int GModel::readMSH(const std::string &name)
13 {
14  FILE *fp = Fopen(name.c_str(), "rb");
15  if(!fp) {
16  Msg::Error("Unable to open file '%s'", name.c_str());
17  return 0;
18  }
19 
20  // detect prehistoric MSH files (without MeshFormat header)
21  char str[256] = "";
22  if(!fgets(str, sizeof(str), fp)) {
23  fclose(fp);
24  return 0;
25  }
26  if(!strncmp(&str[1], "NOD", 3) || !strncmp(&str[1], "NOE", 3)) {
27  fclose(fp);
28  return _readMSH2(name);
29  }
30  strcpy(str, "");
31  rewind(fp);
32 
33  while(1) {
34  while(str[0] != '$') {
35  if(!fgets(str, sizeof(str), fp) || feof(fp)) break;
36  }
37 
38  if(feof(fp)) break;
39 
40  // $MeshFormat section
41  if(!strncmp(&str[1], "MeshFormat", 10)) {
42  if(!fgets(str, sizeof(str), fp)) {
43  fclose(fp);
44  return 0;
45  }
46  double version = 0.;
47  int format, size;
48  if(sscanf(str, "%lf %d %d", &version, &format, &size) != 3) {
49  fclose(fp);
50  return 0;
51  }
52  fclose(fp);
53  if(version < 3.0) { return _readMSH2(name); }
54  else if(version < 4.0) {
55  return _readMSH3(name);
56  }
57  else if(version < 5.0) {
58  return _readMSH4(name);
59  }
60  else {
61  Msg::Error("Unknown MSH file version %g", version);
62  return 0;
63  }
64  }
65 
66  do {
67  if(!fgets(str, sizeof(str), fp) || feof(fp)) break;
68  } while(str[0] != '$');
69  }
70 
71  fclose(fp);
72 
73  return 0;
74 }
75 
76 int GModel::writeMSH(const std::string &name, double version, bool binary,
77  bool saveAll, bool saveParametric, double scalingFactor,
78  int elementStartNum, int saveSinglePartition, bool append)
79 {
80  if(version < 4.0 && getNumPartitions() > 0) {
81  Msg::Warning("Saving a partitioned mesh in a format older than 4.0 may "
82  "cause information loss");
83  }
84 
85  if(version < 3.0) {
86  return _writeMSH2(name, version, binary, saveAll, saveParametric,
87  scalingFactor, elementStartNum, saveSinglePartition,
88  append, true);
89  }
90  else if(version < 4.0) {
91  return _writeMSH3(name, version, binary, saveAll, saveParametric,
92  scalingFactor, elementStartNum, saveSinglePartition,
93  append);
94  }
95  else if(version < 5.0) {
96  return _writeMSH4(name, version, binary, saveAll, saveParametric,
97  scalingFactor, append);
98  }
99 
100  Msg::Error("Unknown MSH file version %g", version);
101  return 0;
102 }
103 
104 int GModel::writePartitionedMSH(const std::string &baseName, double version,
105  bool binary, bool saveAll, bool saveParametric,
106  double scalingFactor)
107 {
108  if(version < 4.0 && getNumPartitions() > 0) {
109  Msg::Warning("Saving a partitioned mesh in a format older than 4.0 may "
110  "cause information loss");
111  }
112 
113  if(version < 3.0) {
114  return _writePartitionedMSH2(baseName, binary, saveAll, saveParametric,
115  scalingFactor);
116  }
117  else if(version < 4.0) {
118  return _writePartitionedMSH3(baseName, version, binary, saveAll,
119  saveParametric, scalingFactor);
120  }
121  else if(version < 5.0) {
122  return _writePartitionedMSH4(baseName, version, binary, saveAll,
123  saveParametric, scalingFactor);
124  }
125 
126  Msg::Error("Unknown MSH file version %g", version);
127  return 0;
128 }
GModel::_readMSH3
int _readMSH3(const std::string &name)
Definition: GModelIO_MSH3.cpp:190
GModel::_writePartitionedMSH3
int _writePartitionedMSH3(const std::string &baseName, double version, bool binary, bool saveAll, bool saveParametric, double scalingFactor)
Definition: GModelIO_MSH3.cpp:841
OS.h
Msg::Warning
static void Warning(const char *fmt,...)
Definition: GmshMessage.cpp:543
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
GModel::_readMSH2
int _readMSH2(const std::string &name)
Definition: GModelIO_MSH2.cpp:100
GModel::_writePartitionedMSH4
int _writePartitionedMSH4(const std::string &baseName, double version, bool binary, bool saveAll, bool saveParametric, double scalingFactor)
Definition: GModelIO_MSH4.cpp:2906
GmshMessage.h
GModel::getNumPartitions
std::size_t getNumPartitions() const
Definition: GModel.h:602
Fopen
FILE * Fopen(const char *f, const char *mode)
Definition: OS.cpp:273
GModel::readMSH
int readMSH(const std::string &name)
Definition: GModelIO_MSH.cpp:12
GModel::_writeMSH4
int _writeMSH4(const std::string &name, double version, bool binary, bool saveAll, bool saveParametric, double scalingFactor, bool append, int partitionToSave=0, std::map< GEntity *, SBoundingBox3d > *entityBounds=nullptr)
Definition: GModelIO_MSH4.cpp:2796
GModel::_writePartitionedMSH2
int _writePartitionedMSH2(const std::string &baseName, bool binary, bool saveAll, bool saveParametric, double scalingFactor)
Definition: GModelIO_MSH2.cpp:1126
GModel::_writeMSH3
int _writeMSH3(const std::string &name, double version, bool binary, bool saveAll, bool saveParametric, double scalingFactor, int elementStartNum, int saveSinglePartition, bool append)
Definition: GModelIO_MSH3.cpp:725
GModel::writeMSH
int writeMSH(const std::string &name, double version=2.2, bool binary=false, bool saveAll=false, bool saveParametric=false, double scalingFactor=1.0, int elementStartNum=0, int saveSinglePartition=0, bool append=false)
Definition: GModelIO_MSH.cpp:76
GModel::_writeMSH2
int _writeMSH2(const std::string &name, double version, bool binary, bool saveAll, bool saveParametric, double scalingFactor, int elementStartNum, int saveSinglePartition, bool append, bool renumberVertices)
Definition: GModelIO_MSH2.cpp:883
GModel::_readMSH4
int _readMSH4(const std::string &name)
Definition: GModelIO_MSH4.cpp:1255
GModel.h
GModel::writePartitionedMSH
int writePartitionedMSH(const std::string &baseName, double version=2.2, bool binary=false, bool saveAll=false, bool saveParametric=false, double scalingFactor=1.0)
Definition: GModelIO_MSH.cpp:104