www.cemf.ir
pointFieldToVTK.cpp
Go to the documentation of this file.
1 #include <regex>
2 
3 #include "vtkFile.hpp"
4 #include "vocabs.hpp"
5 #include "pointFieldToVTK.hpp"
6 
7 
9  systemControl &control,
10  const fileSystem &destPath,
11  const word &bName,
12  word& filename)
13 {
14 
15  fileSystem timeFolder = control.time().path();
16  // check if pointStructure exist in this folder
17  IOfileHeader pStructHeader(
18  objectFile(
20  timeFolder,
21  objectFile::READ_ALWAYS,
22  objectFile::WRITE_ALWAYS));
23 
24  if (!pStructHeader.headerOk(true))
25  {
26  output << yellowColor << "Time folder " <<
27  control.time().path() << " does not contain any pStructure data file."<<
28  " Skipping this folder . . ."<< defaultColor << nl;
29  return true;
30  }
31 
32  vtkFile vtk(destPath, bName, control.time().currentTime(), bindaryOutput__);
33 
34  if (!vtk)
35  return false;
36 
37  filename = vtk.fileName().wordPath();
38 
39  REPORT(1);
40  auto pStruct = pointStructure(control);
41 
42  // get a list of files in this timeFolder;
43 
44  auto posVec = pStruct.pointPositionHost();
45  auto *pos = posVec.data();
46 
47  REPORT(2) << ">>> Writing pointStructure to vtk file with " <<
48  Yellow_Text(pStruct.numActive())<<
49  " active particles" << END_REPORT;
50 
52  vtk(),
53  pos,
54  pStruct.numActive());
55 
56  auto fileList = containingFiles(timeFolder);
57 
58  for (const auto &file : fileList)
59  {
60 
61  IOfileHeader fieldHeader(
62  objectFile(
63  file.fileName(),
64  file.dirPath(),
65  objectFile::READ_ALWAYS,
66  objectFile::WRITE_ALWAYS));
67 
68  if (fieldHeader.headerOk(true))
69  {
70  // 64-bit intergers are not supported for convertion
71  if(
72  convertRealx3TypePointField(vtk(), fieldHeader, pStruct) ||
73  convertRealTypePointField(vtk(), fieldHeader, pStruct) ||
74  convertIntPointField<uint32>(vtk(), fieldHeader, pStruct) ||
75  //convertIntPointField<uint64>(vtk(), fieldHeader, pStruct) ||
76  convertIntPointField<int32>(vtk(), fieldHeader, pStruct) ||
77  //convertIntPointField<int64>(vtk(), fieldHeader, pStruct)||
78  fieldHeader.objectName() == pointStructureFile__ )
79  {
80  continue;
81  }
82  else
83  {
84  WARNING << " This object type, " <<
85  fieldHeader.objectType() << " is not supported" <<
87  }
88  }
89  output << endl;
90  }
91 
92  return true;
93 }
94 
96  systemControl &control,
97  const fileSystem &destPath,
98  const word &bName,
99  const wordVector &fieldsName,
100  bool mustExist,
101  word& filename)
102 {
103  fileSystem timeFolder = control.time().path();
104  // check if pointStructure exist in this folder
105  IOfileHeader pStructHeader(
106  objectFile(
108  timeFolder,
109  objectFile::READ_ALWAYS,
110  objectFile::WRITE_ALWAYS));
111 
112  if (!pStructHeader.headerOk(true))
113  {
114  output << yellowColor << "Time folder " <<
115  control.time().path() <<
116  " does not contain any pStructure data file."<<
117  " Skipping this folder . . ."<< defaultColor << nl;
118  return true;
119  }
120 
121  vtkFile vtk(destPath, bName, control.time().currentTime(), bindaryOutput__);
122 
123  if (!vtk)
124  return false;
125 
126  filename = vtk.fileName().wordPath();
127 
128  REPORT(1);
129  auto pStruct = pointStructure(control);
130 
131  // get a list of files in this timeFolder;
132 
133  auto posVec = pStruct.pointPositionHost();
134  auto *pos = posVec.data();
135 
136  REPORT(2) << ">>> Writing pointStructure to vtk file with " <<
137  Yellow_Text(pStruct.numActive())
138  << " active particles" << END_REPORT;
139 
141  vtk(),
142  pos,
143  pStruct.numActive());
144 
145  auto fileList = containingFiles(timeFolder);
146 
147  for (const auto &fname : fieldsName)
148  {
149  fileSystem fieldAddress = timeFolder + fname;
150 
151  IOfileHeader fieldHeader(
152  objectFile(
153  fname,
154  timeFolder,
155  objectFile::READ_ALWAYS,
156  objectFile::WRITE_ALWAYS));
157 
158  if (fieldHeader.headerOk(true))
159  {
160  if (
161  // 64-bit intergers are not supported for convertion
162  convertRealx3TypePointField(vtk(), fieldHeader, pStruct) ||
163  convertRealTypePointField(vtk(), fieldHeader, pStruct) ||
164  convertIntPointField<uint32>(vtk(), fieldHeader, pStruct) ||
165  // convertIntPointField<uint64>(vtk(), fieldHeader, pStruct) ||
166  convertIntPointField<int32>(vtk(), fieldHeader, pStruct) ||
167  //convertIntPointField<int64>(vtk(), fieldHeader, pStruct) ||
168  fieldHeader.objectName() == pointStructureFile__ )
169  {
170  continue;
171  }
172  else
173  {
174 
175  WARNING << " This object type, " <<
176  fieldHeader.objectType() << " is not supported" <<
177  END_WARNING;
178  }
179  }
180  else
181  {
182  if (mustExist)
183  {
184  fatalErrorInFunction << "Field " << fieldAddress <<
185  " does not exist." << endl;
186  return false;
187  }
188  else
189  {
190  REPORT(2) << "Could not find " << Yellow_Text(fieldAddress) <<
191  ". Skipping this field . . ." << END_REPORT;
192  }
193  }
194  }
195 
196  return true;
197 }
198 
199 
201  Ostream &os,
202  realx3 *position,
203  uint32 numPoints)
204 {
205 
206  os << "DATASET UNSTRUCTURED_GRID\n";
207 
208  if (numPoints == 0)
209  return true;
210 
211  os << "POINTS " << numPoints << " float"<<'\n';
212  if(bindaryOutput__)
213  {
214  for(uint32 i=0; i<numPoints; i++)
215  {
216  float x = byteSwaper(static_cast<float>(position[i].x()));
217  float y = byteSwaper(static_cast<float>(position[i].y()));
218  float z = byteSwaper(static_cast<float>(position[i].z()));
219  os.stdStream().write(reinterpret_cast<const char*>(&x), sizeof(x));
220  os.stdStream().write(reinterpret_cast<const char*>(&y), sizeof(y));
221  os.stdStream().write(reinterpret_cast<const char*>(&z), sizeof(z));
222  }
223  os<<'\n';
224  os << "CELLS " << numPoints << ' ' << 2 * numPoints<<'\n';
225 
226  const int32 one_ro = byteSwaper(1);
227  for (int i = 0; i < numPoints; i++)
228  {
229  int pN = byteSwaper(i);
230  os.stdStream().write(reinterpret_cast<const char*>(&one_ro), sizeof(one_ro));
231  os.stdStream().write(reinterpret_cast<const char*>(&pN), sizeof(pN));
232  }
233  os<<'\n';
234  os << "CELL_TYPES " << numPoints<<'\n';
235  for (int32 i = 0; i < numPoints; i++)
236  {
237  os.stdStream().write(reinterpret_cast<const char*>(&one_ro), sizeof(one_ro));
238  }
239  os<<'\n';
240  }
241  else
242  {
243  for (uint32 i = 0; i < numPoints; i++)
244  {
245  os << position[i].x() <<
246  ' ' << position[i].y() <<
247  ' ' << position[i].z() << '\n';
248  }
249 
250  os << "CELLS " << numPoints << ' ' << 2 * numPoints << '\n';
251  for (uint32 i = 0; i < numPoints; i++)
252  {
253  os << 1 << ' ' << i << '\n';
254  }
255 
256  os << "CELL_TYPES " << numPoints << '\n';
257 
258  for (int32 i = 0; i < numPoints; i++)
259  {
260  os << 1 << '\n';
261  }
262 
263  }
264 
265  os << "POINT_DATA " << numPoints << endl;
266 
267  return true;
268 }
269 
271  Ostream &os,
272  const IOfileHeader &header,
274 {
275  word objectType = header.objectType();
276 
277  if (!checkFieldType<real>(objectType))
278  return false;
279 
280  REPORT(1);
281  auto field = realPointField_H(
282  header,
283  pStruct,
284  static_cast<real>(0));
285 
286  real const *data = field.deviceViewAll().data();
287 
288  REPORT(2) << ">>> Writing " << Green_Text(header.objectName()) <<
289  " field to vtk." << END_REPORT;
290 
291  return addRealPointField(
292  os,
293  header.objectName(),
294  data,
295  pStruct.numActive());
296 }
297 
299  Ostream &os,
300  const IOfileHeader &header,
302 {
303  word objectType = header.objectType();
304 
305  if (!checkFieldType<realx3>(objectType))
306  return false;
307 
308  REPORT(1);
309  auto field = realx3PointField_H(
310  header,
311  pStruct,
312  {0.0, 0.0, 0.0});
313 
314  realx3 const *data = field.deviceViewAll().data();
315 
316  REPORT(2) << ">>> Writing " << Green_Text(header.objectName()) <<
317  " field to vtk." << END_REPORT;
318 
319  return addRealx3PointField(
320  os,
321  header.objectName(),
322  data,
323  pStruct.numActive());
324 }
325 
327  Ostream &os,
328  const word &fieldName,
329  const real *field,
330  uint32 numData)
331 {
332  if (numData == 0)
333  return true;
334 
335  os << "FIELD FieldData 1\n"
336  << fieldName << " 1 " << numData << " float\n";
337  if(bindaryOutput__)
338  {
339  for (uint32 i = 0; i < numData; ++i)
340  {
341  float x = byteSwaper(static_cast<float>(field[i]));
342  os.stdStream().write(reinterpret_cast<const char*>(&x), sizeof(x));
343  }
344  }
345  else
346  {
347  for (uint32 i = 0; i < numData; ++i)
348  {
349  os << field[i] << '\n';
350  }
351 
352  }
353 
354  return true;
355 }
356 
358  Ostream &os,
359  const word &fieldName,
360  const realx3 *field,
361  uint32 numData)
362 {
363  if (numData == 0)
364  return true;
365 
366  os << "FIELD FieldData 1\n"
367  << fieldName << " 3 " << numData << " float\n";
368 
369  if(bindaryOutput__)
370  {
371  for(uint32 i=0; i<numData; i++)
372  {
373  float x = byteSwaper(static_cast<float>(field[i].x()));
374  float y = byteSwaper(static_cast<float>(field[i].y()));
375  float z = byteSwaper(static_cast<float>(field[i].z()));
376  os.stdStream().write(reinterpret_cast<const char*>(&x), sizeof(x));
377  os.stdStream().write(reinterpret_cast<const char*>(&y), sizeof(y));
378  os.stdStream().write(reinterpret_cast<const char*>(&z), sizeof(z));
379  }
380  os<<'\n';
381  }
382  else
383  {
384  for (uint32 i = 0; i < numData; ++i)
385  {
386  os << field[i].x() <<
387  ' ' << field[i].y() <<
388  ' ' << field[i].z() << '\n';
389  }
390  }
391 
392 
393  return true;
394 }
395 
396 bool pFlow::PFtoVTK::regexCheck(const word &TYPENAME, const word &fieldType)
397 {
398  std::regex match("pointField\\<([A-Za-z1-9_]*)\\,([A-Za-z1-9_]*)\\>");
399  std::smatch search1;
400  std::smatch search2;
401  if (!std::regex_match(fieldType, search1, match))
402  return false;
403  if (!std::regex_match(TYPENAME, search2, match))
404  return false;
405  if (search1.size() != 3)
406  return false;
407  if (search1.size() != search2.size())
408  return false;
409  return search1[1] == search2[1];
410 }
Green_Text
#define Green_Text(text)
Definition: iOstream.hpp:42
pFlow::vtkFile
Definition: vtkFile.hpp:33
pFlow::PFtoVTK::regexCheck
bool regexCheck(const word &TYPENAME, const word &fieldType)
Definition: pointFieldToVTK.cpp:396
pFlow::IOfileHeader::objectType
const word & objectType() const
Definition: IOfileHeader.hpp:75
pFlow::real
float real
Definition: builtinTypes.hpp:45
REPORT
#define REPORT(n)
Definition: streams.hpp:39
pFlow::PFtoVTK::convertTimeFolderPointFields
bool convertTimeFolderPointFields(systemControl &control, const fileSystem &destPath, const word &bName, word &filename)
Definition: pointFieldToVTK.cpp:8
pFlow::vtkFile::fileName
virtual fileSystem fileName() const
Definition: vtkFile.cpp:79
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::realPointField_H
pointField_H< real > realPointField_H
Definition: pointFields.hpp:50
pFlow::systemControl
Definition: systemControl.hpp:41
pFlow::containingFiles
fileSystemList containingFiles(const fileSystem &path)
A list of file paths that exist in the path.
Definition: fileSystem.cpp:359
pointFieldToVTK.hpp
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
Yellow_Text
#define Yellow_Text(text)
Definition: iOstream.hpp:40
pFlow::triple::y
INLINE_FUNCTION_HD T & y()
access component
Definition: triple.hpp:144
pFlow::fileSystem
Manages file pathes, manupulate and combines them.
Definition: fileSystem.hpp:71
pFlow::pointStructure
Definition: pointStructure.hpp:34
END_WARNING
#define END_WARNING
Definition: streams.hpp:44
pFlow::pointStructureFile__
const char *const pointStructureFile__
Definition: vocabs.hpp:42
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
defaultColor
const char * defaultColor
char constants to alter output format and color
Definition: iOstream.hpp:27
pFlow::systemControl::time
const Time & time() const
Definition: systemControl.hpp:122
pFlow::output
Ostream output
bindaryOutput__
bool bindaryOutput__
Definition: pFlowToVTK.cpp:33
END_REPORT
#define END_REPORT
Definition: streams.hpp:40
pFlow::objectFile
Definition: objectFile.hpp:30
pFlow::triple::z
INLINE_FUNCTION_HD T & z()
access component
Definition: triple.hpp:156
pFlow::repository::path
virtual fileSystem path() const
Definition: repository.cpp:70
pFlow::timeControl::currentTime
real currentTime() const
Definition: timeControl.hpp:127
pStruct
auto & pStruct
Definition: setPointStructure.hpp:24
pFlow::Ostream::stdStream
virtual std::ostream & stdStream()
Access to underlying std::ostream.
Definition: Ostream.hpp:177
pFlow::realx3PointField_H
pointField_H< realx3 > realx3PointField_H
Definition: pointFields.hpp:53
pFlow::IOfileHeader
Definition: IOfileHeader.hpp:35
pFlow::IOfileHeader::headerOk
bool headerOk(bool silent=false)
Definition: IOfileHeader.cpp:92
pFlow::PFtoVTK::addUndstrcuturedGridField
bool addUndstrcuturedGridField(Ostream &os, realx3 *position, uint32 numPoints)
Definition: pointFieldToVTK.cpp:200
pFlow::IOfileHeader::objectName
const word & objectName() const
Definition: IOfileHeader.hpp:69
pFlow::PFtoVTK::convertTimeFolderPointFieldsSelected
bool convertTimeFolderPointFieldsSelected(systemControl &control, const fileSystem &destPath, const word &bName, const wordVector &fieldsName, bool mustExist, word &filename)
Definition: pointFieldToVTK.cpp:95
yellowColor
const char * yellowColor
Definition: iOstream.hpp:31
pFlow::fileSystem::wordPath
word wordPath() const
Path in word type.
Definition: fileSystem.hpp:160
pFlow::triple::x
INLINE_FUNCTION_HD T & x()
access component
Definition: triple.hpp:132
vocabs.hpp
pFlow::triple< real >
pFlow::PFtoVTK::convertRealTypePointField
bool convertRealTypePointField(Ostream &os, const IOfileHeader &header, pointStructure &pStruct)
Definition: pointFieldToVTK.cpp:270
pFlow::PFtoVTK::convertRealx3TypePointField
bool convertRealx3TypePointField(Ostream &os, const IOfileHeader &header, pointStructure &pStruct)
Definition: pointFieldToVTK.cpp:298
pFlow::Ostream
Standard output stream for BINARY and ASCII formats.
Definition: Ostream.hpp:39
pFlow::Vector< word >
pFlow::nl
constexpr char nl
Definition: iOstream.hpp:440
pFlow::byteSwaper
T byteSwaper(const T &p)
Definition: vtkByteSwapper.hpp:81
vtkFile.hpp
pFlow::PFtoVTK::addRealPointField
bool addRealPointField(Ostream &os, const word &fieldName, const real *field, uint32 numData)
Definition: pointFieldToVTK.cpp:326
pFlow::timeFolder
Definition: timeFolder.hpp:32
pFlow::PFtoVTK::addRealx3PointField
bool addRealx3PointField(Ostream &os, const word &fieldName, const realx3 *field, uint32 numData)
Definition: pointFieldToVTK.cpp:357
WARNING
#define WARNING
Definition: streams.hpp:43