COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
smurf.cpp
Go to the documentation of this file.
1 
2 #include <iostream>
3 #include <sstream>
4 #include <algorithm>
5 #include "smurf.h"
6 
7 
8 #define ZONEMARKER 299.0
9 #define EOHMARKER 357.0
10 
11 using namespace std;
12 namespace SmURF {
13 
14 // ------------------------------------------------------------------------ //
15 // utilities
16 
17 template< typename T >
18 void bwrite(FILE *F, T v, bool reverse)
19 {
20  if (reverse) {
21  const unsigned len = sizeof(v);
22  char *p = (char*) &v;
23  char r_buffer[256]; // should be enough...
24  for (unsigned i=0; i<len; ++i)
25  r_buffer[i] = p[len-1-i];
26  for (unsigned i=0; i<len; ++i)
27  p[i] = r_buffer[i];
28  }
29  size_t s = fwrite(&v,sizeof(v),1,F);
30  s=s;
31 }
32 
33 
34 template< >
35 void bwrite< string >(FILE *F, string v, bool reverse)
36 {
37  for (unsigned i=0; i<v.size(); ++i)
38  bwrite< int >(F,v[i],reverse);
39  bwrite< int >(F,0,reverse);
40 }
41 
42 
43 template< typename T >
44 T bread(ifstream& F)
45 {
46  T v;
47  F.read((char*)(&v),sizeof(T));
48  return v;
49 }
50 
51 
52 template< >
53 string bread(ifstream& F)
54 {
55  string s;
56  while (true) {
57  const char c = bread< int >(F);
58  if (!c)
59  break;
60  s.push_back(c);
61  }
62  return s;
63 }
64 
65 
66 // ------------------------------------------------------------------------ //
67 // MeshWriter
68 
69 MeshWriter::MeshWriter(const std::string& fname, const DataType _datatype, const bool _reverse, const unsigned _version, const double _solutiontime) :
70  m_file(NULL),
71  m_datatype(_datatype),
72  m_reverse(_reverse),
73  m_version(_version),
74  m_solutiontime(_solutiontime)
75 {
76  // open file for writing
77  m_file = fopen(fname.c_str(),"wb");
78  if (m_file==NULL) {
79  throw "SmURF: error accessing file!";
80  }
81 }
82 
83 
85 {
86  // close file
87  fclose(m_file);
88  m_file = NULL;
89 }
90 
91 
92 void MeshWriter::writeMainHeader(const std::string& htitle, const vector< string >& vnames)
93 {
94  m_nvars = (unsigned) vnames.size();
95 
96  // main header information
97  ostringstream s;
98  s << "#!TDV" << m_version;
99  fprintf(m_file,"%s",s.str().c_str());
100 
101  bwrite< int >(m_file,1,m_reverse); // byte order of reader
102  if (m_version>107)
103  bwrite< int >(m_file,0,m_reverse); // filetype: FULL
104  bwrite< string >(m_file,htitle,m_reverse); // title
105  bwrite< int >(m_file,m_nvars,m_reverse); // variables number
106  for (unsigned i=0; i<m_nvars; ++i) // variables names
107  bwrite(m_file,vnames[i],m_reverse);
108 
109  m_eohmarker = false; // only set EOH marker when starting data section
110 }
111 
112 
114  const ZonePack& pack,
115  const std::string& title,
116  const unsigned I, const unsigned J, const unsigned K,
117  const int& strand_id)
118 {
119  bwrite< float >(m_file,ZONEMARKER,m_reverse);
120 
121  // zone header information
122  bwrite< string >(m_file,title,m_reverse); // title
123  bwrite< int > (m_file,-1,m_reverse); // BAD_SET_VALUE
124  bwrite< int > (m_file,strand_id,m_reverse); // strand ID: pending assignment by Tecplot
125  bwrite< double >(m_file,m_solutiontime,m_reverse);// solution time
126  bwrite< int > (m_file,-1,m_reverse); // color
127  bwrite< int > (m_file,type,m_reverse); // type
128  if (m_version<112)
129  bwrite< int > (m_file,pack,m_reverse); // data packing
130  bwrite< int > (m_file,0,m_reverse); // specify var location: no cell centered vars
131  if (m_version>107)
132  bwrite< int > (m_file,0,m_reverse); // no face neighbor array
133  bwrite< int > (m_file,0,m_reverse); // no face neighbor connections
134 
135  if (type==ORDERED) {
136  bwrite< int >(m_file,max< unsigned >(I,1),m_reverse);
137  bwrite< int >(m_file,max< unsigned >(J,1),m_reverse);
138  bwrite< int >(m_file,max< unsigned >(K,1),m_reverse);
139  }
140  else {
141  if (type==FEPOLYGON || type==FEPOLYHEDRON) {
142  cerr << "SmURF: error FEPOLYGON and FEPOLYHEDRON are not implemented!" << endl;
143  throw 42;
144  const unsigned L = 9,
145  M = 9;
146  bwrite< int >(m_file,L,m_reverse); // NumFaces
147  bwrite< int >(m_file,type==FEPOLYGON? L*2:M,m_reverse); // Number of face nodes
148  bwrite< int >(m_file,0,m_reverse); // Number of boundary faces
149  bwrite< int >(m_file,0,m_reverse); // Number of boundary connections
150  }
151  else {
152  bwrite< int >(m_file,I,m_reverse);
153  bwrite< int >(m_file,J,m_reverse);
154  bwrite< int >(m_file,0,m_reverse); // no ICellDim, reserved for the future
155  bwrite< int >(m_file,0,m_reverse); // ... JCellDim
156  bwrite< int >(m_file,0,m_reverse); // ... KCellDim
157  }
158  }
159 
160  bwrite< int >(m_file,0,m_reverse); // no auxiliary data
161 }
162 
163 
164 void MeshWriter::writeZoneData(const ZoneType& type, const ZonePack& pack, const vector< vector< unsigned > >& ve, const vector< vector< double > >& vv, const int sharefrom)
165 {
166  // place end of header marker before writing zone data sections
167  if (!m_eohmarker) {
168  bwrite< float >(m_file,EOHMARKER,m_reverse);
169  m_eohmarker = true;
170  }
171  bwrite< float >(m_file,ZONEMARKER,m_reverse);
172 
173  // variable data types
174  for (unsigned N=0; N<m_nvars; ++N)
175  bwrite< int >(m_file,m_datatype,m_reverse);
176  bwrite< int >(m_file,0,m_reverse); // no passive variables
177 
178  // shared variables
179  const bool isshared = (sharefrom>=0);
180  bwrite< int >(m_file,(isshared? 1:0),m_reverse);
181  if (isshared)
182  for (unsigned N=0; N<m_nvars; ++N)
183  bwrite< int >(m_file,sharefrom,m_reverse);
184  bwrite< int >(m_file,-1,m_reverse); // no shared connectivity
185 
186  // data section
187  if (!isshared) {
188  const unsigned Nnode = vv[0].size();
189 
190  // variables min/max values
191  for (unsigned N=0; N<m_nvars; ++N) {
192  bwrite< double >(m_file,*min_element(vv[N].begin(),vv[N].end()),m_reverse);
193  bwrite< double >(m_file,*max_element(vv[N].begin(),vv[N].end()),m_reverse);
194  }
195 
196  if (pack==POINT && m_version<112) {
197 
198  for (unsigned i=0; i<Nnode; ++i)
199  for (unsigned N=0; N<m_nvars; ++N)
200  if (m_datatype==DOUBLE) bwrite< double >(m_file,vv[N][i],m_reverse);
201  else bwrite< float >(m_file,(float) vv[N][i],m_reverse);
202 
203  }
204  else {
205 
206  for (unsigned N=0; N<m_nvars; ++N)
207  for (unsigned i=0; i<Nnode; ++i)
208  if (m_datatype==DOUBLE) bwrite< double >(m_file,vv[N][i],m_reverse);
209  else bwrite< float >(m_file,(float) vv[N][i],m_reverse);
210 
211  }
212  }
213 
214  // connectivity
215  if (type!=ORDERED) {
216  if (type==FEPOLYGON || type==FEPOLYHEDRON) {
217 
218  // face map data
219  cerr << "SmURF: error FEPOLYGON and FEPOLYHEDRON are not implemented!" << endl;
220  throw 42;
221  bwrite< int >(m_file,9,m_reverse); // INT32*F | Face node offsets into the face nodes array below. Does not exist for FEPOLYGON zones. F = NumFaces+1.
222  bwrite< int >(m_file,9,m_reverse); // INT32*FN | Face nodes array containing the node numbers for all nodes in all faces. FN = total number of face nodes.
223  bwrite< int >(m_file,9,m_reverse); // INT32*F | Elements on the left side of all faces. Boundary faces use a negative value which is the negated offset into the face boundary connection offsets array. A value of "-1" indicates there is no left element. F = NumFaces.
224  bwrite< int >(m_file,9,m_reverse); // INT32*F | Elements on the right side of all faces. F = NumFaces.
225 
226  }
227  else {
228 
229  // zone connectivity data
230  const unsigned Nelem = ve.size();
231  const unsigned L = ve[0].size();
232  for (unsigned j=0; j<Nelem; ++j)
233  for (unsigned i=0; i<L; ++i)
234  bwrite< unsigned >(m_file,ve[j][i],m_reverse);
235 
236  }
237  }
238 }
239 
240 // ------------------------------------------------------------------------ //
241 // MeshReader
242 
243 MeshReader::MeshReader(const std::string& fname)
244 {
245  m_file.open(fname.c_str(),ios::binary);
246  if (!m_file) {
247  throw "SmURF: error accessing file!";
248  }
249 }
250 
251 
253 {
254  // close file
255  m_file.close();
256 }
257 
258 
259 void MeshReader::readMainHeader(string& htitle, vector< string >& hvnames)
260 {
261  // version
262  string s(8,'?');
263  for (unsigned i=0; i<8; ++i)
264  m_file >> s[i];
265  istringstream ss(s.substr(5));
266  ss >> m_version;
267  //std::cout << "SmURF: version number: \"" << m_version << "\"" << std::endl;
268 
269  // byte order, filetype and title
270  int i;
271  i = bread< int >(m_file); // 1
272  if (m_version>107)
273  i = bread< int >(m_file); // 0
274  htitle = bread< string >(m_file);
275 
276  // variables
277  m_nvars = (unsigned) bread< int >(m_file);
278  hvnames.resize(m_nvars);
279  for (unsigned i=0; i<hvnames.size(); ++i)
280  hvnames[i] = bread< string >(m_file);
281 }
282 
283 
284 vector< TecZone > MeshReader::readZoneHeaders()
285 {
286  vector< TecZone > vzones;
287  float marker;
288  for (marker=bread< float >(m_file); marker==ZONEMARKER; marker=bread< float >(m_file)) {
289  TecZone z = {"",0.,AUTO,ORDERED,BLOCK,0,0,0};
290 
291  int i;
292  z.title = bread< string >(m_file); // title
293  i = bread< int >(m_file); // BAD_SET_VALUE
294  i = bread< int >(m_file); // strandid: static
295  z.time = bread< double >(m_file); // solution time
296  z.color = bread< ZoneColor >(m_file); // color
297  z.type = bread< ZoneType >(m_file); // type
298  if (m_version<112)
299  z.pack = bread< ZonePack >(m_file); // data packing
300  i = bread< int >(m_file); // no cell centered vars
301  i = bread< int >(m_file); // no auto-generated face neighbor array
302  if (m_version>107) // i'm not sure this is correct
303  i = bread< int >(m_file); // no FaceNeighborConnections
304 
305  if (z.type==ORDERED) {
306  z.i = (unsigned) bread< int >(m_file);
307  z.j = (unsigned) bread< int >(m_file);
308  z.k = (unsigned) bread< int >(m_file);
309  }
310  else {
311  z.i = (unsigned) bread< int >(m_file);
312  z.j = (unsigned) bread< int >(m_file);
313  i = bread< int >(m_file); // no ICellDim, reserved for the future
314  i = bread< int >(m_file); // ... JCellDim
315  i = bread< int >(m_file); // ... KCellDim
316  }
317 
318  while (bread< int >(m_file)) { // skip auxiliary data
319  bread< string >(m_file); // ... aux name
320  bread< int >(m_file); // ... aux value format
321  bread< string >(m_file); // ... aux value string
322  }
323 
324  vzones.push_back(z);
325  }
326 
327  if (marker!=EOHMARKER) { std::cout << "SmURF: did not detect EOHMARKER!" << std::endl; exit(666); }
328  return vzones;
329 }
330 
331 
332 void MeshReader::readZoneData(const TecZone& z, vector< vector< unsigned > >& ve, vector< vector< double > >& vv)
333 {
334  double d;
335  int i;
336 
337  const float marker = bread< float >(m_file);
338  if (marker!=ZONEMARKER) {
339  cerr << "SmURF: corrupt file!" << endl;
340  throw 42;
341  }
342 
343  vector< DataType > vtypes(m_nvars,FLOAT);
344  for (unsigned j=0; j<m_nvars; ++j)
345  vtypes[j] = bread< DataType >(m_file); // variable data types
346  i = bread< int >(m_file); // no passive variables
347 
348  // shared variables
349  const bool isshared = (bread< int >(m_file)!=0);
350  int sharefrom;
351  if (isshared)
352  for (unsigned N=0; N<m_nvars; ++N)
353  sharefrom = bread< int >(m_file); // shares only from first zone only (yet), sharefrom=0
354  i = bread< int >(m_file); // no shared connectivity
355 
356  // data section
357  if (!(isshared)) {
358  const unsigned Nnode = z.i;
359  vv.assign(m_nvars,vector< double >(Nnode,0.));
360 
361  // variables min/max values
362  for (unsigned N=0; N<m_nvars; ++N) {
363  d = bread< double >(m_file);
364  d = bread< double >(m_file);
365  }
366 
367  if (z.pack==BLOCK) {
368 
369  for (unsigned N=0; N<m_nvars; ++N)
370  for (unsigned i=0; i<Nnode; ++i)
371  if (vtypes[N]==DOUBLE) vv[N][i] = bread< double >(m_file);
372  else vv[N][i] = (double) bread< float >(m_file);
373 
374  }
375  else if (z.pack==POINT) {
376 
377  for (unsigned i=0; i<Nnode; ++i)
378  for (unsigned N=0; N<m_nvars; ++N)
379  if (vtypes[N]==DOUBLE) vv[N][i] = bread< double >(m_file);
380  else vv[N][i] = (double) bread< float >(m_file);
381 
382  }
383  }
384 
385  // FE connectivity
386  if (z.type!=ORDERED) {
387  const unsigned Nelem = z.j;
388  const unsigned L = (z.type==FELINESEG? 2:
389  (z.type==FETRIANGLE? 3:
390  (z.type==FEQUADRILATERAL? 4:
391  (z.type==FETETRAHEDRON? 4:
392  (z.type==FEBRICK? 8:0 )))));
393  ve.assign(Nelem,vector< unsigned >(L,0));
394  for (unsigned j=0; j<Nelem; ++j)
395  for (unsigned i=0; i<L; ++i)
396  ve[j][i] = bread< unsigned >(m_file);
397  }
398 }
399 
400 // ------------------------------------------------------------------------ //
401 
402 }
403 
404 #undef EOHMARKER
405 #undef ZONEMARKER
406 
boost::proto::terminal< SFOp< ShapeFunctionOp > >::type const N
#define F(x, y, z)
FILE * m_file
Definition: smurf.h:38
CGAL::Exact_predicates_inexact_constructions_kernel K
ZonePack pack
Definition: smurf.h:24
void writeZoneData(const ZoneType &type, const ZonePack &pack, const std::vector< std::vector< unsigned > > &ve, const std::vector< std::vector< double > > &vv=std::vector< std::vector< double > >(), const int sharefrom=0)
Definition: smurf.cpp:164
ZoneColor color
Definition: smurf.h:22
STL namespace.
void writeMainHeader(const std::string &htitle, const std::vector< std::string > &hvnames)
Definition: smurf.cpp:92
ZoneType
Definition: smurf.h:15
unsigned m_nvars
Definition: smurf.h:43
#define ZONEMARKER
Definition: smurf.cpp:8
unsigned j
Definition: smurf.h:25
#define EOHMARKER
Definition: smurf.cpp:9
void bwrite< string >(FILE *F, string v, bool reverse)
Definition: smurf.cpp:35
std::string title
Definition: smurf.h:20
ZonePack
Definition: smurf.h:16
unsigned k
Definition: smurf.h:25
void writeZoneHeader(const ZoneType &type, const ZonePack &pack, const std::string &title, const unsigned I=1, const unsigned J=1, const unsigned K=1, const int &strand_id=int(-2))
Definition: smurf.cpp:113
MeshReader(const std::string &fname)
Definition: smurf.cpp:243
string bread(ifstream &F)
Definition: smurf.cpp:53
unsigned i
Definition: smurf.h:25
Definition: smurf.cpp:12
bool m_eohmarker
Definition: smurf.h:44
unsigned m_version
Definition: smurf.h:58
std::ifstream m_file
Definition: smurf.h:57
DataType
Definition: smurf.h:17
void readMainHeader(std::string &htitle, std::vector< std::string > &hvnames)
Definition: smurf.cpp:259
const bool m_reverse
Definition: smurf.h:40
double time
Definition: smurf.h:21
ZoneType type
Definition: smurf.h:23
std::vector< TecZone > readZoneHeaders()
Definition: smurf.cpp:284
const DataType m_datatype
Definition: smurf.h:39
void bwrite(FILE *F, T v, bool reverse)
Definition: smurf.cpp:18
const double m_solutiontime
Definition: smurf.h:42
void readZoneData(const TecZone &z, std::vector< std::vector< unsigned > > &ve, std::vector< std::vector< double > > &vv)
Definition: smurf.cpp:332
const unsigned m_version
Definition: smurf.h:41
unsigned m_nvars
Definition: smurf.h:59
Send comments to:
COOLFluiD Web Admin