pointFieldToVTK.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 
22 #ifndef __pointFieldToVTK_hpp__
23 #define __pointFieldToVTK_hpp__
24 
25 #include <regex>
26 
27 #include "vtkFile.hpp"
28 #include "pointFields.hpp"
29 #include "IOobject.hpp"
30 
31 namespace pFlow::PFtoVTK
32 {
33 
34 template<typename IntType, typename IncludeMaskType>
35 bool addIntPointField(
36  iOstream& os,
37  word fieldName,
38  int32 numActivePoints,
39  IntType* field,
40  IncludeMaskType includeMask );
41 
42 template<typename IncludeMaskType>
44  iOstream& os,
45  word fieldName,
46  int32 numActivePoints,
47  real* field,
48  IncludeMaskType includeMask );
49 
50 template<typename IncludeMaskType>
52  iOstream& os,
53  word fieldName,
54  int32 numActivePoints,
55  realx3* field,
56  IncludeMaskType includeMask );
57 
58 bool regexCheck(word TYPENAME, word fieldType)
59 {
60  std::regex match("pointField\\<([A-Za-z1-9_]*)\\,([A-Za-z1-9_]*)\\>");
61  std::smatch search1, search2;
62  if(!std::regex_match(fieldType, search1, match))return false;
63  if(!std::regex_match(TYPENAME, search2, match))return false;
64  if(search1.size()!=3)return false;
65  if(search1.size()!=search2.size())return false;
66  return search1[1] == search2[1];
67 }
68 
69 template<typename Type>
70 bool checkFieldType(word objectType)
71 {
72  //if( pointField<VectorSingle,Type>::TYPENAME() == objectType )return true;
73  //if( pointField<VectorSingle,Type, HostSpace>::TYPENAME() == objectType ) return true;
74  //if( pointField<VectorDual, Type>::TYPENAME() == objectType )return true;
76 
77 }
78 
79 template<typename T>
81 (
82  iOstream& os,
83  const IOfileHeader& header,
84  const pointStructure& pStruct
85 )
86 {
87 
88  using PointFieldType = pointField<VectorSingle, T, HostSpace>;
89 
90  word objectType = header.objectType();
91 
92  if(!checkFieldType<T>(objectType))
93  {
94  return false;
95  }
96 
97  auto objField = IOobject::make<PointFieldType>
98  (
99  header,
100  pStruct,
101  static_cast<T>(0)
102  );
103 
104  auto& Field = objField().template getObject<PointFieldType>();
105 
106  T* data = Field.deviceVectorAll().data();
107 
108  REPORT(2)<<"writing "<< greenColor <<header.objectName()<<defaultColor<<" field to vtk.\n";
109 
110  return addIntPointField(
111  os,
112  header.objectName(),
113  pStruct.numActive(),
114  data,
115  pStruct.activePointsMaskH() );
116 }
117 
118 
120  iOstream& os,
121  const IOfileHeader& header,
122  const pointStructure& pStruct)
123 {
124  word objectType = header.objectType();
125 
126  if(!checkFieldType<real>(objectType))return false;
127 
128  auto objField = IOobject::make<realPointField_H>
129  (
130  header,
131  pStruct,
132  static_cast<real>(0)
133  );
134 
135  auto& Field = objField().getObject<realPointField_H>();
136 
137  real* data = Field.hostVectorAll().data();
138 
139  REPORT(2)<<"writing "<< greenColor <<header.objectName()<<defaultColor<<" field to vtk."<<endREPORT;
140 
141  return addRealPointField(
142  os,
143  header.objectName(),
144  pStruct.numActive(),
145  data,
146  pStruct.activePointsMaskH() );
147 }
148 
150  iOstream& os,
151  const IOfileHeader& header,
152  const pointStructure& pStruct)
153 {
154  word objectType = header.objectType();
155 
156  if(!checkFieldType<realx3>(objectType))return false;
157 
158  auto objField = IOobject::make<realx3PointField_H>
159  (
160  header,
161  pStruct,
162  static_cast<real>(0)
163  );
164 
165  auto& Field = objField().getObject<realx3PointField_H>();
166 
167  realx3* data = Field.hostVectorAll().data();
168 
169  REPORT(2)<<"writing "<< greenColor <<header.objectName()<<defaultColor<<" field to vtk."<<endREPORT;
170 
171  return addRealx3PointField(
172  os,
173  header.objectName(),
174  pStruct.numActive(),
175  data,
176  pStruct.activePointsMaskH() );
177 }
178 
179 
180 template<typename IncludeMaskType>
182  iOstream& os,
183  int32 numActivePoints,
184  realx3* position,
185  IncludeMaskType includeMask)
186 {
187 
188  auto [iFirst, iLast] = includeMask.activeRange();
189 
190  os<< "DATASET UNSTRUCTURED_GRID\n";
191  os<< "POINTS "<< numActivePoints << " float\n";
192 
193  if(numActivePoints==0) return true;
194 
195  for(int32 i=iFirst; i<iLast; ++i)
196  {
197  if(includeMask(i))
198  os<< position[i].x()<<' '<< position[i].y()<<' '<<position[i].z()<<'\n';
199  }
200 
201  os<<"CELLS "<< numActivePoints<<' '<< 2*numActivePoints<<'\n';
202  for(int32 i=0; i<numActivePoints; i++)
203  {
204  os<< 1 <<' '<< i<<'\n';
205  }
206 
207  os<<"CELL_TYPES "<< numActivePoints<<'\n';
208 
209  for(int32 i=0; i<numActivePoints; i++)
210  {
211  os<< 1 <<'\n';
212  }
213 
214  os << "POINT_DATA " << numActivePoints << endl;
215 
216  return true;
217 }
218 
219 
220 template<typename IntType, typename IncludeMaskType>
222  iOstream& os,
223  word fieldName,
224  int32 numActivePoints,
225  IntType* field,
226  IncludeMaskType includeMask )
227 {
228  if(numActivePoints==0) return true;
229 
230  auto [iFirst, iLast] = includeMask.activeRange();
231 
232  os << "FIELD FieldData 1\n"<<
233  fieldName << " 1 " << numActivePoints << " int\n";
234  for(int32 i=iFirst; i<iLast; ++i)
235  {
236  if(includeMask(i))
237  os<< field[i] <<'\n';
238  }
239 
240  return true;
241 }
242 
243 template<typename IncludeMaskType>
245  iOstream& os,
246  word fieldName,
247  int32 numActivePoints,
248  real* field,
249  IncludeMaskType includeMask )
250 {
251  if(numActivePoints==0) return true;
252 
253  auto [iFirst, iLast] = includeMask.activeRange();
254 
255  os << "FIELD FieldData 1\n"<<
256  fieldName << " 1 " << numActivePoints << " float\n";
257  for(int32 i=iFirst; i<iLast; ++i)
258  {
259  if(includeMask(i))
260  os<< field[i] <<'\n';
261  }
262  return true;
263 }
264 
265 template<typename IncludeMaskType>
267  iOstream& os,
268  word fieldName,
269  int32 numActivePoints,
270  realx3* field,
271  IncludeMaskType includeMask )
272 {
273  if(numActivePoints==0) return true;
274 
275  auto [iFirst, iLast] = includeMask.activeRange();
276 
277  os << "FIELD FieldData 1\n"<<
278  fieldName << " 3 " << numActivePoints << " float\n";
279  for(int32 i=iFirst; i<iLast; ++i)
280  {
281  if(includeMask(i))
282  os<< field[i].x()<<' '<< field[i].y()<<' '<<field[i].z()<<'\n';
283  }
284 
285  return true;
286 }
287 
288 
291  real time,
292  fileSystem destPath,
293  word bName)
294 {
295 
296  // check if pointStructure exist in this folder
297  IOfileHeader pStructHeader(
298  objectFile(
300  timeFolder,
303  );
304 
305  if( !pStructHeader.headerOk(true) )
306  {
307  output<<yellowColor<<"Time folder "<< timeFolder <<
308  " does not contain any pStructure data file. Skipping this folder . . ."
309  <<defaultColor<<nl;
310  return true;
311  }
312 
313  vtkFile vtk(destPath, bName, time);
314 
315  if(!vtk) return false;
316 
317  auto pStructObjPtr = IOobject::make<pointStructure>(pStructHeader);
318  auto& pStruct = pStructObjPtr().getObject<pointStructure>();
319 
320  // get a list of files in this timeFolder;
321 
322  auto posVec = std::as_const(pStruct).pointPosition().hostVectorAll();
323  auto* pos = posVec.data();
324 
325  REPORT(1)<<"Writing pointStructure to vtk file with "<< yellowText(pStruct.numActive())
326  <<" active particles"<<endREPORT;
328  vtk(),
329  pStruct.numActive(),
330  pos,
331  pStruct.activePointsMaskH());
332 
333  auto fileList = containingFiles(timeFolder);
334 
335 
336  for(auto& file:fileList)
337  {
338  IOfileHeader fieldHeader(
339  objectFile(
340  file.wordPath(),
341  "",
344 
345  if( fieldHeader.headerOk(true) )
346  {
347  convertIntPointField<int32>(vtk(), fieldHeader, pStruct);
348  convertIntPointField<int64>(vtk(), fieldHeader, pStruct);
349  convertIntPointField<int8>(vtk(), fieldHeader, pStruct);
350  convertRealTypePointField(vtk(), fieldHeader, pStruct);
351  convertRealx3TypePointField(vtk(), fieldHeader, pStruct);
352  }
353  }
354 
355  return true;
356 }
357 
358 
359 
362  real time,
363  fileSystem destPath,
364  word bName,
365  wordVector fieldsName,
366  bool mustExist)
367 {
368 
369  // check if pointStructure exist in this folder
370  IOfileHeader pStructHeader(
371  objectFile(
373  timeFolder,
376  );
377 
378  if( !pStructHeader.headerOk(true) )
379  {
380  output<<yellowColor<<"Time folder "<< timeFolder <<
381  " does not contain any pStructure data file. Skipping this folder . . ."
382  <<defaultColor<<nl;
383  return true;
384  }
385 
386  vtkFile vtk(destPath, bName, time);
387 
388  if(!vtk) return false;
389 
390  auto pStructObjPtr = IOobject::make<pointStructure>(pStructHeader);
391  auto& pStruct = pStructObjPtr().getObject<pointStructure>();
392 
393  // get a list of files in this timeFolder;
394 
395  auto posVec = std::as_const(pStruct).pointPosition().hostVectorAll();
396  auto* pos = posVec.data();
397 
398  REPORT(1)<<"Writing pointStructure to vtk file with "<< yellowText(pStruct.numActive())
399  <<" active particles"<<endREPORT;
401  vtk(),
402  pStruct.numActive(),
403  pos,
404  pStruct.activePointsMaskH());
405 
406  auto fileList = containingFiles(timeFolder);
407 
408 
409  for(auto& fname:fieldsName)
410  {
411  fileSystem fieldAddress = timeFolder+fname;
412 
413  IOfileHeader fieldHeader(
414  objectFile(
415  fieldAddress.wordPath(),
416  "",
419 
420  if( fieldHeader.headerOk(true) )
421  {
422  convertIntPointField<int32>(vtk(), fieldHeader, pStruct);
423  convertIntPointField<int64>(vtk(), fieldHeader, pStruct);
424  convertIntPointField<int8>(vtk(), fieldHeader, pStruct);
425  convertRealTypePointField(vtk(), fieldHeader, pStruct);
426  convertRealx3TypePointField(vtk(), fieldHeader, pStruct);
427  }
428  else
429  {
430  if(mustExist)
431  {
432  fatalErrorInFunction<<"Field " << fieldAddress <<
433  " does not exist."<<endl;
434  return false;
435  }
436  else
437  {
438  REPORT(1)<<"Could not find "<<yellowText(fieldAddress) <<" skipping . . ."<<endREPORT;
439  }
440  }
441  }
442 
443  return true;
444 }
445 
446 
447 }
448 
449 
450 
451 #endif
pFlow::PFtoVTK::convertTimeFolderPointFieldsSelected
bool convertTimeFolderPointFieldsSelected(fileSystem timeFolder, real time, fileSystem destPath, word bName, wordVector fieldsName, bool mustExist)
Definition: pointFieldToVTK.hpp:360
pFlow::vtkFile
Definition: vtkFile.hpp:33
pFlow::pointStructureFile__
const char * pointStructureFile__
Definition: vocabs.hpp:42
pFlow::IOfileHeader::objectType
const word & objectType() const
Definition: IOfileHeader.hpp:75
endREPORT
#define endREPORT
Definition: streams.hpp:41
pFlow::PFtoVTK::addRealPointField
bool addRealPointField(iOstream &os, word fieldName, int32 numActivePoints, real *field, IncludeMaskType includeMask)
Definition: pointFieldToVTK.hpp:244
pFlow::real
float real
Definition: builtinTypes.hpp:46
IOobject.hpp
pFlow::PFtoVTK::regexCheck
bool regexCheck(word TYPENAME, word fieldType)
Definition: pointFieldToVTK.hpp:58
REPORT
#define REPORT(n)
Definition: streams.hpp:40
pFlow::VectorSingle::hostVectorAll
const INLINE_FUNCTION_H auto hostVectorAll() const
Definition: VectorSingle.hpp:322
pFlow::PFtoVTK::addIntPointField
bool addIntPointField(iOstream &os, word fieldName, int32 numActivePoints, IntType *field, IncludeMaskType includeMask)
Definition: pointFieldToVTK.hpp:221
pFlow::PFtoVTK::addRealx3PointField
bool addRealx3PointField(iOstream &os, word fieldName, int32 numActivePoints, realx3 *field, IncludeMaskType includeMask)
Definition: pointFieldToVTK.hpp:266
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pointFields.hpp
pFlow::containingFiles
fileSystemList containingFiles(const fileSystem &path)
Definition: fileSystem.cpp:335
pFlow::PFtoVTK::convertIntPointField
bool convertIntPointField(iOstream &os, const IOfileHeader &header, const pointStructure &pStruct)
Definition: pointFieldToVTK.hpp:81
pFlow::objectFile::WRITE_ALWAYS
@ WRITE_ALWAYS
Definition: objectFile.hpp:46
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:320
pFlow::Field
Definition: Field.hpp:33
pFlow::includeMask
Definition: includeMask.hpp:33
pFlow::pointStructure::pointPosition
FUNCTION_H realx3Field_D & pointPosition()
Definition: pointStructure.cpp:65
pFlow::triple::y
INLINE_FUNCTION_HD T & y()
Definition: triple.hpp:141
pFlow::PFtoVTK::convertRealx3TypePointField
bool convertRealx3TypePointField(iOstream &os, const IOfileHeader &header, const pointStructure &pStruct)
Definition: pointFieldToVTK.hpp:149
pFlow::fileSystem
Definition: fileSystem.hpp:63
pFlow::pointField
Definition: pointField.hpp:35
pFlow::pointStructure
Definition: pointStructure.hpp:44
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
defaultColor
const char * defaultColor
Definition: iOstream.hpp:31
pFlow::output
Ostream output
pFlow::objectFile::READ_ALWAYS
@ READ_ALWAYS
Definition: objectFile.hpp:39
pFlow::PFtoVTK::convertRealTypePointField
bool convertRealTypePointField(iOstream &os, const IOfileHeader &header, const pointStructure &pStruct)
Definition: pointFieldToVTK.hpp:119
pFlow::objectFile
Definition: objectFile.hpp:33
pFlow::triple::z
INLINE_FUNCTION_HD T & z()
Definition: triple.hpp:144
pStruct
auto & pStruct
Definition: setPointStructure.hpp:24
pFlow::IOfileHeader
Definition: IOfileHeader.hpp:35
pFlow::IOfileHeader::headerOk
bool headerOk(bool silent=false)
Definition: IOfileHeader.cpp:86
pFlow::PFtoVTK::checkFieldType
bool checkFieldType(word objectType)
Definition: pointFieldToVTK.hpp:70
pFlow::IOfileHeader::objectName
const word & objectName() const
Definition: IOfileHeader.hpp:69
pFlow::PFtoVTK::convertTimeFolderPointFields
bool convertTimeFolderPointFields(fileSystem timeFolder, real time, fileSystem destPath, word bName)
Definition: pointFieldToVTK.hpp:289
yellowColor
const char * yellowColor
Definition: iOstream.hpp:35
pFlow::fileSystem::wordPath
word wordPath() const
Definition: fileSystem.hpp:126
pFlow::triple::x
INLINE_FUNCTION_HD T & x()
Definition: triple.hpp:138
pFlow::PFtoVTK::addUndstrcuturedGridField
bool addUndstrcuturedGridField(iOstream &os, int32 numActivePoints, realx3 *position, IncludeMaskType includeMask)
Definition: pointFieldToVTK.hpp:181
pFlow::triple< real >
yellowText
#define yellowText(text)
Definition: streams.hpp:30
pFlow::Vector< word >
pFlow::nl
constexpr char nl
Definition: iOstream.hpp:417
pFlow::iOstream
Definition: iOstream.hpp:53
greenColor
const char * greenColor
Definition: iOstream.hpp:34
vtkFile.hpp
pFlow::PFtoVTK
Definition: pointFieldToVTK.hpp:31
pFlow::timeFolder
Definition: timeFolder.hpp:32