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 IncludeMaskType>
36  iOstream& os,
37  word fieldName,
38  int32 numActivePoints,
39  int64* 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 
80  iOstream& os,
81  const IOfileHeader& header,
82  const pointStructure& pStruct )
83 {
84  word objectType = header.objectType();
85 
86  if( !(checkFieldType<int8>(objectType) ||
87  checkFieldType<int16>(objectType) ||
88  checkFieldType<int32>(objectType) ||
89  checkFieldType<int64>(objectType) ||
90  checkFieldType<uint32>(objectType) ||
91  checkFieldType<label>(objectType))
92  )
93  {
94  return false;
95  }
96 
97  auto objField = IOobject::make<int64PointField_H>
98  (
99  header,
100  pStruct,
101  static_cast<int64>(0)
102  );
103 
104  auto& Field = objField().getObject<int64PointField_H>();
105 
106  int64* data = Field.hostVectorAll().data();
107 
108  REPORT(2)<<"writing "<< greenColor <<header.objectName()<<defaultColor<<" field to vtk.\n";
109 
110  return addInt64PointField(
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 
189 
190  auto [iFirst, iLast] = includeMask.activeRange();
191 
192  os<< "DATASET UNSTRUCTURED_GRID\n";
193  os<< "POINTS "<< numActivePoints << " float\n";
194 
195  if(numActivePoints==0) return true;
196 
197  for(int32 i=iFirst; i<iLast; ++i)
198  {
199  if(includeMask(i))
200  os<< position[i].x()<<' '<< position[i].y()<<' '<<position[i].z()<<'\n';
201  }
202 
203  os<<"CELLS "<< numActivePoints<<' '<< 2*numActivePoints<<'\n';
204  for(int32 i=0; i<numActivePoints; i++)
205  {
206  os<< 1 <<' '<< i<<'\n';
207  }
208 
209  os<<"CELL_TYPES "<< numActivePoints<<'\n';
210 
211  for(int32 i=0; i<numActivePoints; i++)
212  {
213  os<< 1 <<'\n';
214  }
215 
216  os << "POINT_DATA " << numActivePoints << endl;
217 
218  return true;
219 }
220 
221 
222 template<typename IncludeMaskType>
224  iOstream& os,
225  word fieldName,
226  int32 numActivePoints,
227  int64* field,
228  IncludeMaskType includeMask )
229 {
230  if(numActivePoints==0) return true;
231 
232  auto [iFirst, iLast] = includeMask.activeRange();
233 
234  os << "FIELD FieldData 1\n"<<
235  fieldName << " 1 " << numActivePoints << " int\n";
236  for(int32 i=iFirst; i<iLast; ++i)
237  {
238  if(includeMask(i))
239  os<< field[i] <<'\n';
240  }
241 
242  return true;
243 }
244 
245 template<typename IncludeMaskType>
247  iOstream& os,
248  word fieldName,
249  int32 numActivePoints,
250  real* field,
251  IncludeMaskType includeMask )
252 {
253  if(numActivePoints==0) return true;
254 
255  auto [iFirst, iLast] = includeMask.activeRange();
256 
257  os << "FIELD FieldData 1\n"<<
258  fieldName << " 1 " << numActivePoints << " float\n";
259  for(int32 i=iFirst; i<iLast; ++i)
260  {
261  if(includeMask(i))
262  os<< field[i] <<'\n';
263  }
264  return true;
265 }
266 
267 template<typename IncludeMaskType>
269  iOstream& os,
270  word fieldName,
271  int32 numActivePoints,
272  realx3* field,
273  IncludeMaskType includeMask )
274 {
275  if(numActivePoints==0) return true;
276 
277  auto [iFirst, iLast] = includeMask.activeRange();
278 
279  os << "FIELD FieldData 1\n"<<
280  fieldName << " 3 " << numActivePoints << " float\n";
281  for(int32 i=iFirst; i<iLast; ++i)
282  {
283  if(includeMask(i))
284  os<< field[i].x()<<' '<< field[i].y()<<' '<<field[i].z()<<'\n';
285  }
286 
287  return true;
288 }
289 
290 
293  real time,
294  fileSystem destPath,
295  word bName)
296 {
297 
298  // check if pointStructure exist in this folder
299  IOfileHeader pStructHeader(
300  objectFile(
302  timeFolder,
305  );
306 
307  if( !pStructHeader.headerOk(true) )
308  {
309  output<<yellowColor<<"Time folder "<< timeFolder <<
310  " does not contain any pStructure data file. Skipping this folder . . ."
311  <<defaultColor<<nl;
312  return true;
313  }
314 
315  vtkFile vtk(destPath, bName, time);
316 
317  if(!vtk) return false;
318 
319  auto pStructObjPtr = IOobject::make<pointStructure>(pStructHeader);
320  auto& pStruct = pStructObjPtr().getObject<pointStructure>();
321 
322  // get a list of files in this timeFolder;
323 
324  auto posVec = std::as_const(pStruct).pointPosition().hostVectorAll();
325  auto* pos = posVec.data();
326 
327  REPORT(1)<<"Writing pointStructure to vtk file with "<< yellowText(pStruct.numActive())
328  <<" active particles"<<endREPORT;
330  vtk(),
331  pStruct.numActive(),
332  pos,
333  pStruct.activePointsMaskH());
334 
335  auto fileList = containingFiles(timeFolder);
336 
337 
338  for(auto& file:fileList)
339  {
340  IOfileHeader fieldHeader(
341  objectFile(
342  file.wordPath(),
343  "",
346 
347  if( fieldHeader.headerOk(true) )
348  {
349  convertIntTypesPointField(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  convertIntTypesPointField(vtk(), fieldHeader, pStruct);
423  convertRealTypePointField(vtk(), fieldHeader, pStruct);
424  convertRealx3TypePointField(vtk(), fieldHeader, pStruct);
425  }
426  else
427  {
428  if(mustExist)
429  {
430  fatalErrorInFunction<<"Field " << fieldAddress <<
431  " does not exist."<<endl;
432  return false;
433  }
434  else
435  {
436  REPORT(1)<<"Could not find "<<yellowText(fieldAddress) <<" skipping . . ."<<endREPORT;
437  }
438  }
439  }
440 
441  return true;
442 }
443 
444 
445 }
446 
447 
448 
449 #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:72
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:246
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:320
pFlow::PFtoVTK::addRealx3PointField
bool addRealx3PointField(iOstream &os, word fieldName, int32 numActivePoints, realx3 *field, IncludeMaskType includeMask)
Definition: pointFieldToVTK.hpp:268
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pointFields.hpp
pFlow::containingFiles
fileSystemList containingFiles(const fileSystem &path)
Definition: fileSystem.cpp:335
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:55
pFlow::objectFile::WRITE_ALWAYS
@ WRITE_ALWAYS
Definition: objectFile.hpp:46
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow::Field
Definition: Field.hpp:33
pFlow::includeMask
Definition: includeMask.hpp:33
pFlow::pointStructure::pointPosition
FUNCTION_H realx3Field_D & pointPosition()
Definition: pointStructure.cpp:64
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::convertIntTypesPointField
bool convertIntTypesPointField(iOstream &os, const IOfileHeader &header, const pointStructure &pStruct)
Definition: pointFieldToVTK.hpp:79
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::PFtoVTK::addInt64PointField
bool addInt64PointField(iOstream &os, word fieldName, int32 numActivePoints, int64 *field, IncludeMaskType includeMask)
Definition: pointFieldToVTK.hpp:223
pFlow::IOfileHeader
Definition: IOfileHeader.hpp:35
pFlow::IOfileHeader::headerOk
bool headerOk(bool silent=false)
Definition: IOfileHeader.cpp:71
pFlow::PFtoVTK::checkFieldType
bool checkFieldType(word objectType)
Definition: pointFieldToVTK.hpp:70
pFlow::IOfileHeader::objectName
const word & objectName() const
Definition: IOfileHeader.hpp:66
pFlow::PFtoVTK::convertTimeFolderPointFields
bool convertTimeFolderPointFields(fileSystem timeFolder, real time, fileSystem destPath, word bName)
Definition: pointFieldToVTK.hpp:291
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:409
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