gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
PViewDataIO.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 "GmshMessage.h"
9 #include "GmshDefines.h"
10 #include "Numeric.h"
11 #include "PViewData.h"
12 #include "adaptiveData.h"
13 #include "OS.h"
14 
15 bool PViewData::writeSTL(const std::string &fileName)
16 {
17  FILE *fp = Fopen(fileName.c_str(), "w");
18  if(!fp) {
19  Msg::Error("Unable to open file '%s'", fileName.c_str());
20  return false;
21  }
22 
23  if(!getNumTriangles() && !getNumQuadrangles()) {
24  Msg::Error("No surface elements to save");
25  fclose(fp);
26  return false;
27  }
28 
29  int step = getFirstNonEmptyTimeStep();
30 
31  fprintf(fp, "solid Created by Gmsh\n");
32  for(int ent = 0; ent < getNumEntities(step); ent++) {
33  for(int ele = 0; ele < getNumElements(step, ent); ele++) {
34  if(getDimension(step, ent, ele) != 2) continue;
35  if(skipElement(step, ent, ele)) continue;
36  int N = getNumNodes(step, ent, ele);
37  if(N != 3 && N != 4) continue;
38  double x[4], y[4], z[4], n[3];
39  for(int i = 0; i < N; i++) getNode(step, ent, ele, i, x[i], y[i], z[i]);
40  normal3points(x[0], y[0], z[0], x[1], y[1], z[1], x[2], y[2], z[2], n);
41  if(N == 3) {
42  fprintf(fp, "facet normal %g %g %g\n", n[0], n[1], n[2]);
43  fprintf(fp, " outer loop\n");
44  fprintf(fp, " vertex %g %g %g\n", x[0], y[0], z[0]);
45  fprintf(fp, " vertex %g %g %g\n", x[1], y[1], z[1]);
46  fprintf(fp, " vertex %g %g %g\n", x[2], y[2], z[2]);
47  fprintf(fp, " endloop\n");
48  fprintf(fp, "endfacet\n");
49  }
50  else {
51  fprintf(fp, "facet normal %g %g %g\n", n[0], n[1], n[2]);
52  fprintf(fp, " outer loop\n");
53  fprintf(fp, " vertex %g %g %g\n", x[0], y[0], z[0]);
54  fprintf(fp, " vertex %g %g %g\n", x[1], y[1], z[1]);
55  fprintf(fp, " vertex %g %g %g\n", x[2], y[2], z[2]);
56  fprintf(fp, " endloop\n");
57  fprintf(fp, "endfacet\n");
58  fprintf(fp, "facet normal %g %g %g\n", n[0], n[1], n[2]);
59  fprintf(fp, " outer loop\n");
60  fprintf(fp, " vertex %g %g %g\n", x[0], y[0], z[0]);
61  fprintf(fp, " vertex %g %g %g\n", x[2], y[2], z[2]);
62  fprintf(fp, " vertex %g %g %g\n", x[3], y[3], z[3]);
63  fprintf(fp, " endloop\n");
64  fprintf(fp, "endfacet\n");
65  }
66  }
67  }
68  fprintf(fp, "endsolid Created by Gmsh\n");
69 
70  fclose(fp);
71  return true;
72 }
73 
74 bool PViewData::writeTXT(const std::string &fileName)
75 {
76  FILE *fp = Fopen(fileName.c_str(), "w");
77  if(!fp) {
78  Msg::Error("Unable to open file '%s'", fileName.c_str());
79  return false;
80  }
81 
82  for(int step = 0; step < getNumTimeSteps(); step++) {
83  for(int ent = 0; ent < getNumEntities(step); ent++) {
84  for(int ele = 0; ele < getNumElements(step, ent); ele++) {
85  if(skipElement(step, ent, ele)) continue;
86  for(int nod = 0; nod < getNumNodes(step, ent, ele); nod++) {
87  double x, y, z;
88  getNode(step, ent, ele, nod, x, y, z);
89  fprintf(fp, "%d %.16g %d %d %.16g %.16g %.16g ", step, getTime(step),
90  ent, ele, x, y, z);
91  for(int comp = 0; comp < getNumComponents(step, ent, ele); comp++) {
92  double val;
93  getValue(step, ent, ele, nod, comp, val);
94  fprintf(fp, "%.16g ", val);
95  }
96  }
97  fprintf(fp, "\n");
98  }
99  }
100  }
101 
102  fclose(fp);
103  return true;
104 }
105 
106 bool PViewData::writePOS(const std::string &fileName, bool binary, bool parsed,
107  bool append)
108 {
109  if(_adaptive) {
110  Msg::Warning(
111  "Writing adapted dataset (will only export current time step)");
112  return _adaptive->getData()->writePOS(fileName, binary, parsed, append);
113  }
114  if(hasMultipleMeshes()) {
115  Msg::Error("Cannot export multi-mesh datasets in .pos format");
116  return false;
117  }
119  Msg::Warning(
120  "Discarding interpolation matrices when saving in .pos format");
121  if(binary || !parsed)
122  Msg::Warning("Only parsed .pos files can be exported for this view type");
123 
124  FILE *fp = Fopen(fileName.c_str(), append ? "a" : "w");
125  if(!fp) {
126  Msg::Error("Unable to open file '%s'", fileName.c_str());
127  return false;
128  }
129 
130  fprintf(fp, "View \"%s\" {\n", getName().c_str());
131 
132  int firstNonEmptyStep = getFirstNonEmptyTimeStep();
133  for(int ent = 0; ent < getNumEntities(firstNonEmptyStep); ent++) {
134  for(int ele = 0; ele < getNumElements(firstNonEmptyStep, ent); ele++) {
135  if(skipElement(firstNonEmptyStep, ent, ele)) continue;
136  int type = getType(firstNonEmptyStep, ent, ele);
137  int numComp = getNumComponents(firstNonEmptyStep, ent, ele);
138  const char *s = nullptr;
139  switch(type) {
140  case TYPE_PNT:
141  s = (numComp == 9) ? "TP" : (numComp == 3) ? "VP" : "SP";
142  break;
143  case TYPE_LIN:
144  s = (numComp == 9) ? "TL" : (numComp == 3) ? "VL" : "SL";
145  break;
146  case TYPE_TRI:
147  s = (numComp == 9) ? "TT" : (numComp == 3) ? "VT" : "ST";
148  break;
149  case TYPE_QUA:
150  s = (numComp == 9) ? "TQ" : (numComp == 3) ? "VQ" : "SQ";
151  break;
152  case TYPE_TET:
153  s = (numComp == 9) ? "TS" : (numComp == 3) ? "VS" : "SS";
154  break;
155  case TYPE_HEX:
156  s = (numComp == 9) ? "TH" : (numComp == 3) ? "VH" : "SH";
157  break;
158  case TYPE_PRI:
159  s = (numComp == 9) ? "TI" : (numComp == 3) ? "VI" : "SI";
160  break;
161  case TYPE_PYR:
162  s = (numComp == 9) ? "TY" : (numComp == 3) ? "VY" : "SY";
163  break;
164  }
165  if(s) {
166  fprintf(fp, "%s(", s);
167  int numNod = getNumNodes(firstNonEmptyStep, ent, ele);
168  for(int nod = 0; nod < numNod; nod++) {
169  double x, y, z;
170  getNode(firstNonEmptyStep, ent, ele, nod, x, y, z);
171  fprintf(fp, "%.16g,%.16g,%.16g", x, y, z);
172  if(nod != numNod - 1) fprintf(fp, ",");
173  }
174  bool first = true;
175  for(int step = 0; step < getNumTimeSteps(); step++) {
176  if(hasTimeStep(step)) {
177  for(int nod = 0; nod < numNod; nod++) {
178  for(int comp = 0; comp < numComp; comp++) {
179  double val;
180  getValue(step, ent, ele, nod, comp, val);
181  if(first) {
182  fprintf(fp, "){%.16g", val);
183  first = false;
184  }
185  else
186  fprintf(fp, ",%.16g", val);
187  }
188  }
189  }
190  }
191  fprintf(fp, "};\n");
192  }
193  }
194  }
195 
196  fprintf(fp, "};\n");
197  fclose(fp);
198 
199  return true;
200 }
201 
202 bool PViewData::writeMSH(const std::string &fileName, double version,
203  bool binary, bool saveMesh, bool multipleView,
204  int partitionNum, bool saveInterpolationMatrices,
205  bool forceNodeData, bool forceElementData)
206 {
207  Msg::Error("MSH export not implemented for this view type");
208  return false;
209 }
210 
211 bool PViewData::writeMED(const std::string &fileName)
212 {
213  Msg::Error("MED export only available for mesh-based post-processing views");
214  return false;
215 }
216 
217 bool PViewData::toVector(std::vector<std::vector<double> > &vec)
218 {
219  vec.resize(getNumTimeSteps());
220  for(int step = 0; step < getNumTimeSteps(); step++) {
221  vec[step].clear();
222  for(int ent = 0; ent < getNumEntities(step); ent++) {
223  for(int ele = 0; ele < getNumElements(step, ent); ele++) {
224  if(skipElement(step, ent, ele)) continue;
225  for(int nod = 0; nod < getNumNodes(step, ent, ele); nod++) {
226  for(int comp = 0; comp < getNumComponents(step, ent, ele); comp++) {
227  double val;
228  getValue(step, ent, ele, nod, comp, val);
229  vec[step].push_back(val);
230  }
231  }
232  }
233  }
234  }
235  return true;
236 }
237 
238 bool PViewData::fromVector(const std::vector<std::vector<double> > &vec)
239 {
240  if(empty() || !getNumTimeSteps()) {
241  Msg::Warning("Cannot import vector in an empty view; skipping");
242  return false;
243  }
244 
245  if((int)vec.size() != getNumTimeSteps()) {
246  Msg::Error(
247  "Incompatible number of steps in vector for view import (%d!=%d)",
248  (int)vec.size(), getNumTimeSteps());
249  return false;
250  }
251 
252  for(int step = 0; step < getNumTimeSteps(); step++) {
253  int i = 0;
254  for(int ent = 0; ent < getNumEntities(step); ent++) {
255  for(int ele = 0; ele < getNumElements(step, ent); ele++) {
256  if(skipElement(step, ent, ele)) continue;
257  for(int nod = 0; nod < getNumNodes(step, ent, ele); nod++) {
258  double x, y, z;
259  getNode(step, ent, ele, nod, x, y, z);
260  for(int comp = 0; comp < getNumComponents(step, ent, ele); comp++) {
261  if(i < (int)vec[step].size()) {
262  setValue(step, ent, ele, nod, comp, vec[step][i++]);
263  }
264  else {
265  Msg::Error("Bad index (%d) in vector (%d) for view import", i,
266  (int)vec[step].size());
267  return false;
268  }
269  }
270  }
271  }
272  }
273  }
274  return true;
275 }
276 
277 void PViewData::importLists(int N[24], std::vector<double> *V[24])
278 {
279  Msg::Error("importLists not available for this view data type");
280 }
281 
282 void PViewData::getListPointers(int N[24], std::vector<double> *V[24])
283 {
284  Msg::Error("getListPointers not available for this view data type");
285 }
286 
287 void PViewData::sendToServer(const std::string &name)
288 {
289  // Vectorize
290  std::vector<std::vector<double> > vec;
291  bool ok = toVector(vec);
292 
293  // Success ?
294  if(!ok) Msg::Error("sendToServer: cannot vectorize PView");
295 
296  // Only one step ?
297  if(vec.size() != 1)
298  Msg::Error("sendToServer: cannot send a PView with more than one step");
299 
300  // Only one data ?
301  if(vec[0].size() != 1)
302  Msg::Error("sendToServer: cannot send a PView with more than one data");
303 
304  // Send data
305  Msg::SetOnelabNumber(name, vec[0][0]);
306 }
PViewData::toVector
virtual bool toVector(std::vector< std::vector< double > > &vec)
Definition: PViewDataIO.cpp:217
PViewData::writeSTL
virtual bool writeSTL(const std::string &fileName)
Definition: PViewDataIO.cpp:15
PViewData::writeTXT
virtual bool writeTXT(const std::string &fileName)
Definition: PViewDataIO.cpp:74
PViewData::skipElement
virtual bool skipElement(int step, int ent, int ele, bool checkVisibility=false, int samplingRate=1)
Definition: PViewData.cpp:90
TYPE_LIN
#define TYPE_LIN
Definition: GmshDefines.h:65
PViewData::getNumTimeSteps
virtual int getNumTimeSteps()=0
OS.h
PViewData::setValue
virtual void setValue(int step, int ent, int ele, int nod, int comp, double val)
Definition: PViewData.cpp:130
Msg::Warning
static void Warning(const char *fmt,...)
Definition: GmshMessage.cpp:543
PViewData::getNode
virtual int getNode(int step, int ent, int ele, int nod, double &x, double &y, double &z)
Definition: PViewData.h:141
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
PViewData::getValue
virtual void getValue(int step, int ent, int ele, int idx, double &val)
Definition: PViewData.h:159
TYPE_PNT
#define TYPE_PNT
Definition: GmshDefines.h:64
TYPE_TRI
#define TYPE_TRI
Definition: GmshDefines.h:66
PViewData::writePOS
virtual bool writePOS(const std::string &fileName, bool binary=false, bool parsed=true, bool append=false)
Definition: PViewDataIO.cpp:106
PViewData::hasMultipleMeshes
virtual bool hasMultipleMeshes()
Definition: PViewData.h:216
PViewData::writeMED
virtual bool writeMED(const std::string &fileName)
Definition: PViewDataIO.cpp:211
GmshMessage.h
PViewData::getNumEntities
virtual int getNumEntities(int step=-1)
Definition: PViewData.h:127
PViewData.h
PViewData::haveInterpolationMatrices
bool haveInterpolationMatrices(int type=0)
Definition: PViewData.cpp:186
TYPE_PRI
#define TYPE_PRI
Definition: GmshDefines.h:70
Fopen
FILE * Fopen(const char *f, const char *mode)
Definition: OS.cpp:273
adaptiveData.h
PViewData::getDimension
virtual int getDimension(int step, int ent, int ele)
Definition: PViewData.h:134
PViewData::getTime
virtual double getTime(int step)
Definition: PViewData.h:94
PViewData::getType
virtual int getType(int step, int ent, int ele)
Definition: PViewData.h:183
Numeric.h
PViewData::hasTimeStep
virtual bool hasTimeStep(int step)
Definition: PViewData.h:211
PViewData::sendToServer
virtual void sendToServer(const std::string &name)
Definition: PViewDataIO.cpp:287
PViewData::getNumQuadrangles
virtual int getNumQuadrangles(int step=-1)
Definition: PViewData.h:117
PViewData::getNumNodes
virtual int getNumNodes(int step, int ent, int ele)
Definition: PViewData.h:137
GmshDefines.h
PViewData::writeMSH
virtual bool writeMSH(const std::string &fileName, double version=2.2, bool binary=false, bool saveMesh=true, bool multipleView=false, int partitionNum=-1, bool saveInterpolationMatrices=true, bool forceNodeData=false, bool forceElementData=false)
Definition: PViewDataIO.cpp:202
adaptiveData::getData
PViewData * getData()
Definition: adaptiveData.h:674
TYPE_PYR
#define TYPE_PYR
Definition: GmshDefines.h:69
PViewData::empty
virtual bool empty()
Definition: PViewData.cpp:85
TYPE_QUA
#define TYPE_QUA
Definition: GmshDefines.h:67
PViewData::getNumComponents
virtual int getNumComponents(int step, int ent, int ele)
Definition: PViewData.h:152
PViewData::getListPointers
virtual void getListPointers(int N[24], std::vector< double > *V[24])
Definition: PViewDataIO.cpp:282
z
const double z
Definition: GaussQuadratureQuad.cpp:56
TYPE_HEX
#define TYPE_HEX
Definition: GmshDefines.h:71
normal3points
void normal3points(double x0, double y0, double z0, double x1, double y1, double z1, double x2, double y2, double z2, double n[3])
Definition: Numeric.cpp:76
TYPE_TET
#define TYPE_TET
Definition: GmshDefines.h:68
PViewData::_adaptive
adaptiveData * _adaptive
Definition: PViewData.h:49
PViewData::importLists
virtual void importLists(int N[24], std::vector< double > *V[24])
Definition: PViewDataIO.cpp:277
PViewData::getNumElements
virtual int getNumElements(int step=-1, int ent=-1)
Definition: PViewData.h:131
PViewData::getName
virtual std::string getName()
Definition: PViewData.h:70
Msg::SetOnelabNumber
static void SetOnelabNumber(const std::string &name, double val, bool visible=true, bool persistent=false, bool readOnly=false, int changedValue=3)
Definition: GmshMessage.cpp:987
PViewData::fromVector
virtual bool fromVector(const std::vector< std::vector< double > > &vec)
Definition: PViewDataIO.cpp:238
PViewData::getNumTriangles
virtual int getNumTriangles(int step=-1)
Definition: PViewData.h:116
PViewData::getFirstNonEmptyTimeStep
virtual int getFirstNonEmptyTimeStep(int start=0)
Definition: PViewData.h:91