# PhasicFlow: utilities/pFlowToVTK/pointFieldToVTK.cpp Source File
PhasicFlow is an open-source parallel DEM (discrete element method) package for simulating granular flow. It is developed in C++ and can be exectued on both GPU (like CUDA) and CPU.

pointFieldToVTK.cpp
Go to the documentation of this file.
#include <regex>
#include "vtkFile.hpp"
#include "vocabs.hpp"
#include "pointFieldToVTK.hpp"
bool pFlow::PFtoVTK::convertTimeFolderPointFields(
systemControl &control,
const fileSystem &destPath,
const word &bName,
word& filename)
{
fileSystem timeFolder = control.time().path();
// check if pointStructure exist in this folder
IOfileHeader pStructHeader(
objectFile(
pointStructureFile__,
timeFolder,
objectFile::READ_ALWAYS,
objectFile::WRITE_ALWAYS));
if (!pStructHeader.headerOk(true))
{
output << yellowColor << "Time folder " <<
control.time().path() << " does not contain any pStructure data file."<<
" Skipping this folder . . ."<< defaultColor << nl;
return true;
}
vtkFile vtk(destPath, bName, control.time().currentTime(), bindaryOutput__);
if (!vtk)
return false;
filename = vtk.fileName().wordPath();
REPORT(1);
auto pStruct = pointStructure(control);
// get a list of files in this timeFolder;
auto posVec = pStruct.pointPositionHost();
auto *pos = posVec.data();
REPORT(2) << ">>> Writing pointStructure to vtk file with " <<
Yellow_Text(pStruct.numActive())<<
" active particles" << END_REPORT;
addUndstrcuturedGridField(
vtk(),
pos,
pStruct.numActive());
auto fileList = containingFiles(timeFolder);
for (const auto &file : fileList)
{
IOfileHeader fieldHeader(
objectFile(
file.fileName(),
file.dirPath(),
objectFile::READ_ALWAYS,
objectFile::WRITE_ALWAYS));
if (fieldHeader.headerOk(true))
{
// 64-bit intergers are not supported for convertion
if(
convertRealx3TypePointField(vtk(), fieldHeader, pStruct) ||
convertRealTypePointField(vtk(), fieldHeader, pStruct) ||
convertIntPointField<uint32>(vtk(), fieldHeader, pStruct) ||
//convertIntPointField<uint64>(vtk(), fieldHeader, pStruct) ||
convertIntPointField<int32>(vtk(), fieldHeader, pStruct) ||
//convertIntPointField<int64>(vtk(), fieldHeader, pStruct)||
fieldHeader.objectName() == pointStructureFile__ )
{
continue;
}
else
{
WARNING << " This object type, " <<
fieldHeader.objectType() << " is not supported" <<
END_WARNING;
}
}
output << endl;
}
return true;
}
bool pFlow::PFtoVTK::convertTimeFolderPointFieldsSelected(
systemControl &control,
const fileSystem &destPath,
const word &bName,
const wordVector &fieldsName,
bool mustExist,
word& filename)
{
fileSystem timeFolder = control.time().path();
// check if pointStructure exist in this folder
IOfileHeader pStructHeader(
objectFile(
pointStructureFile__,
timeFolder,
objectFile::READ_ALWAYS,
objectFile::WRITE_ALWAYS));
if (!pStructHeader.headerOk(true))
{
output << yellowColor << "Time folder " <<
control.time().path() <<
" does not contain any pStructure data file."<<
" Skipping this folder . . ."<< defaultColor << nl;
return true;
}
vtkFile vtk(destPath, bName, control.time().currentTime(), bindaryOutput__);
if (!vtk)
return false;
filename = vtk.fileName().wordPath();
REPORT(1);
auto pStruct = pointStructure(control);
// get a list of files in this timeFolder;
auto posVec = pStruct.pointPositionHost();
auto *pos = posVec.data();
REPORT(2) << ">>> Writing pointStructure to vtk file with " <<
Yellow_Text(pStruct.numActive())
<< " active particles" << END_REPORT;
addUndstrcuturedGridField(
vtk(),
pos,
pStruct.numActive());
auto fileList = containingFiles(timeFolder);
for (const auto &fname : fieldsName)
{
fileSystem fieldAddress = timeFolder + fname;
IOfileHeader fieldHeader(
objectFile(
fname,
timeFolder,
objectFile::READ_ALWAYS,
objectFile::WRITE_ALWAYS));
if (fieldHeader.headerOk(true))
{
if (
// 64-bit intergers are not supported for convertion
convertRealx3TypePointField(vtk(), fieldHeader, pStruct) ||
convertRealTypePointField(vtk(), fieldHeader, pStruct) ||
convertIntPointField<uint32>(vtk(), fieldHeader, pStruct) ||
// convertIntPointField<uint64>(vtk(), fieldHeader, pStruct) ||
convertIntPointField<int32>(vtk(), fieldHeader, pStruct) ||
//convertIntPointField<int64>(vtk(), fieldHeader, pStruct) ||
fieldHeader.objectName() == pointStructureFile__ )
{
continue;
}
else
{
WARNING << " This object type, " <<
fieldHeader.objectType() << " is not supported" <<
END_WARNING;
}
}
else
{
if (mustExist)
{
fatalErrorInFunction << "Field " << fieldAddress <<
" does not exist." << endl;
return false;
}
else
{
REPORT(2) << "Could not find " << Yellow_Text(fieldAddress) <<
". Skipping this field . . ." << END_REPORT;
}
}
}
return true;
}
bool pFlow::PFtoVTK::addUndstrcuturedGridField(
Ostream &os,
realx3 *position,
uint32 numPoints)
{
os << "DATASET UNSTRUCTURED_GRID\n";
if (numPoints == 0)
return true;
os << "POINTS " << numPoints << " float"<<'\n';
if(bindaryOutput__)
{
for(uint32 i=0; i<numPoints; i++)
{
float x = byteSwaper(static_cast<float>(position[i].x()));
float y = byteSwaper(static_cast<float>(position[i].y()));
float z = byteSwaper(static_cast<float>(position[i].z()));
os.stdStream().write(reinterpret_cast<const char*>(&x), sizeof(x));
os.stdStream().write(reinterpret_cast<const char*>(&y), sizeof(y));
os.stdStream().write(reinterpret_cast<const char*>(&z), sizeof(z));
}
os<<'\n';
os << "CELLS " << numPoints << ' ' << 2 * numPoints<<'\n';
const int32 one_ro = byteSwaper(1);
for (int i = 0; i < numPoints; i++)
{
int pN = byteSwaper(i);
os.stdStream().write(reinterpret_cast<const char*>(&one_ro), sizeof(one_ro));
os.stdStream().write(reinterpret_cast<const char*>(&pN), sizeof(pN));
}
os<<'\n';
os << "CELL_TYPES " << numPoints<<'\n';
for (int32 i = 0; i < numPoints; i++)
{
os.stdStream().write(reinterpret_cast<const char*>(&one_ro), sizeof(one_ro));
}
os<<'\n';
}
else
{
for (uint32 i = 0; i < numPoints; i++)
{
os << position[i].x() <<
' ' << position[i].y() <<
' ' << position[i].z() << '\n';
}
os << "CELLS " << numPoints << ' ' << 2 * numPoints << '\n';
for (uint32 i = 0; i < numPoints; i++)
{
os << 1 << ' ' << i << '\n';
}
os << "CELL_TYPES " << numPoints << '\n';
for (int32 i = 0; i < numPoints; i++)
{
os << 1 << '\n';
}
}
os << "POINT_DATA " << numPoints << endl;
return true;
}
bool pFlow::PFtoVTK::convertRealTypePointField(
Ostream &os,
const IOfileHeader &header,
pointStructure &pStruct)
{
word objectType = header.objectType();
if (!checkFieldType<real>(objectType))
return false;
REPORT(1);
auto field = realPointField_H(
header,
pStruct,
static_cast< real >(0));
real const *data = field.deviceViewAll().data();
REPORT(2) << ">>> Writing " << Green_Text(header.objectName()) <<
" field to vtk." << END_REPORT;
return addRealPointField(
os,
header.objectName(),
data,
pStruct.numActive());
}
bool pFlow::PFtoVTK::convertRealx3TypePointField(
Ostream &os,
const IOfileHeader &header,
pointStructure &pStruct)
{
word objectType = header.objectType();
if (!checkFieldType<realx3>(objectType))
return false;
REPORT(1);
auto field = realx3PointField_H(
header,
pStruct,
{0.0, 0.0, 0.0});
realx3 const *data = field.deviceViewAll().data();
REPORT(2) << ">>> Writing " << Green_Text(header.objectName()) <<
" field to vtk." << END_REPORT;
return addRealx3PointField(
os,
header.objectName(),
data,
pStruct.numActive());
}
bool pFlow::PFtoVTK::addRealPointField(
Ostream &os,
const word &fieldName,
const real *field,
uint32 numData)
{
if (numData == 0)
return true;
os << "FIELD FieldData 1\n"
<< fieldName << " 1 " << numData << " float\n";
if(bindaryOutput__)
{
for (uint32 i = 0; i < numData; ++i)
{
float x = byteSwaper(static_cast<float>(field[i]));
os.stdStream().write(reinterpret_cast<const char*>(&x), sizeof(x));
}
}
else
{
for (uint32 i = 0; i < numData; ++i)
{
os << field[i] << '\n';
}
}
return true;
}
bool pFlow::PFtoVTK::addRealx3PointField(
Ostream &os,
const word &fieldName,
const realx3 *field,
uint32 numData)
{
if (numData == 0)
return true;
os << "FIELD FieldData 1\n"
<< fieldName << " 3 " << numData << " float\n";
if(bindaryOutput__)
{
for(uint32 i=0; i<numData; i++)
{
float x = byteSwaper(static_cast<float>(field[i].x()));
float y = byteSwaper(static_cast<float>(field[i].y()));
float z = byteSwaper(static_cast<float>(field[i].z()));
os.stdStream().write(reinterpret_cast<const char*>(&x), sizeof(x));
os.stdStream().write(reinterpret_cast<const char*>(&y), sizeof(y));
os.stdStream().write(reinterpret_cast<const char*>(&z), sizeof(z));
}
os<<'\n';
}
else
{
for (uint32 i = 0; i < numData; ++i)
{
os << field[i].x() <<
' ' << field[i].y() <<
' ' << field[i].z() << '\n';
}
}
return true;
}
bool pFlow::PFtoVTK::regexCheck(const word &TYPENAME, const word &fieldType)
{
std::regex match("pointField\\<([A-Za-z1-9_]*)\\,([A-Za-z1-9_]*)\\>");
std::smatch search1;
std::smatch search2;
if (!std::regex_match(fieldType, search1, match))
return false;
if (!std::regex_match(TYPENAME, search2, match))
return false;
if (search1.size() != 3)
return false;
if (search1.size() != search2.size())
return false;
return search1[1] == search2[1];
}
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