# PhasicFlow: utilities/pFlowToVTK/pointFieldToVTK.hpp Source File

pointFieldToVTK.hpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
#ifndef __pointFieldToVTK_hpp__
#define __pointFieldToVTK_hpp__
#include <regex>
#include "vtkFile.hpp"
#include "pointFields.hpp"
#include "IOobject.hpp"
namespace pFlow::PFtoVTK
{
template<typename IncludeMaskType>
bool addInt64PointField(
iOstream& os,
word fieldName,
int32 numActivePoints,
int64* field,
IncludeMaskType includeMask );
template<typename IncludeMaskType>
bool addRealPointField(
iOstream& os,
word fieldName,
int32 numActivePoints,
real* field,
IncludeMaskType includeMask );
template<typename IncludeMaskType>
bool addRealx3PointField(
iOstream& os,
word fieldName,
int32 numActivePoints,
realx3* field,
IncludeMaskType includeMask );
bool regexCheck(word TYPENAME, word fieldType)
{
std::regex match("pointField\\<([A-Za-z1-9_]*)\\,([A-Za-z1-9_]*)\\>");
std::smatch search1, 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];
}
template<typename Type>
bool checkFieldType(word objectType)
{
//if( pointField<VectorSingle,Type>::TYPENAME() == objectType )return true;
//if( pointField<VectorSingle,Type, HostSpace>::TYPENAME() == objectType ) return true;
//if( pointField<VectorDual, Type>::TYPENAME() == objectType )return true;
return regexCheck(pointField<VectorSingle,Type>::TYPENAME(), objectType);
}
bool convertIntTypesPointField(
iOstream& os,
const IOfileHeader& header,
const pointStructure& pStruct )
{
word objectType = header.objectType();
if( !(checkFieldType<int8>(objectType) ||
checkFieldType<int16>(objectType) ||
checkFieldType<int32>(objectType) ||
checkFieldType<int64>(objectType) ||
checkFieldType<uint32>(objectType) ||
checkFieldType<label>(objectType))
)
{
return false;
}
auto objField = IOobject::make<int64PointField_H>
(
header,
pStruct,
static_cast< int64 >(0)
);
auto& Field = objField().getObject<int64PointField_H>();
int64* data = Field.hostVectorAll().data();
REPORT(2)<<"writing "<< greenColor <<header.objectName()<<defaultColor<<" field to vtk.\n";
return addInt64PointField(
os,
header.objectName(),
pStruct.numActive(),
data,
pStruct.activePointsMaskH() );
}
bool convertRealTypePointField(
iOstream& os,
const IOfileHeader& header,
const pointStructure& pStruct)
{
word objectType = header.objectType();
if(!checkFieldType<real>(objectType))return false;
auto objField = IOobject::make<realPointField_H>
(
header,
pStruct,
static_cast< real >(0)
);
auto& Field = objField().getObject<realPointField_H>();
real* data = Field.hostVectorAll().data();
REPORT(2)<<"writing "<< greenColor <<header.objectName()<<defaultColor<<" field to vtk."<<endREPORT;
return addRealPointField(
os,
header.objectName(),
pStruct.numActive(),
data,
pStruct.activePointsMaskH() );
}
bool convertRealx3TypePointField(
iOstream& os,
const IOfileHeader& header,
const pointStructure& pStruct)
{
word objectType = header.objectType();
if(!checkFieldType<realx3>(objectType))return false;
auto objField = IOobject::make<realx3PointField_H>
(
header,
pStruct,
static_cast< real >(0)
);
auto& Field = objField().getObject<realx3PointField_H>();
realx3* data = Field.hostVectorAll().data();
REPORT(2)<<"writing "<< greenColor <<header.objectName()<<defaultColor<<" field to vtk."<<endREPORT;
return addRealx3PointField(
os,
header.objectName(),
pStruct.numActive(),
data,
pStruct.activePointsMaskH() );
}
template<typename IncludeMaskType>
bool addUndstrcuturedGridField(
iOstream& os,
int32 numActivePoints,
realx3* position,
IncludeMaskType includeMask)
{
auto [iFirst, iLast] = includeMask.activeRange();
os<< "DATASET UNSTRUCTURED_GRID\n";
os<< "POINTS "<< numActivePoints << " float\n";
if(numActivePoints==0) return true;
for(int32 i=iFirst; i<iLast; ++i)
{
if(includeMask(i))
os<< position[i].x()<<' '<< position[i].y()<<' '<<position[i].z()<<'\n';
}
os<<"CELLS "<< numActivePoints<<' '<< 2*numActivePoints<<'\n';
for(int32 i=0; i<numActivePoints; i++)
{
os<< 1 <<' '<< i<<'\n';
}
os<<"CELL_TYPES "<< numActivePoints<<'\n';
for(int32 i=0; i<numActivePoints; i++)
{
os<< 1 <<'\n';
}
os << "POINT_DATA " << numActivePoints << endl;
return true;
}
template<typename IncludeMaskType>
bool addInt64PointField(
iOstream& os,
word fieldName,
int32 numActivePoints,
int64* field,
IncludeMaskType includeMask )
{
if(numActivePoints==0) return true;
auto [iFirst, iLast] = includeMask.activeRange();
os << "FIELD FieldData 1\n"<<
fieldName << " 1 " << numActivePoints << " int\n";
for(int32 i=iFirst; i<iLast; ++i)
{
if(includeMask(i))
os<< field[i] <<'\n';
}
return true;
}
template<typename IncludeMaskType>
bool addRealPointField(
iOstream& os,
word fieldName,
int32 numActivePoints,
real* field,
IncludeMaskType includeMask )
{
if(numActivePoints==0) return true;
auto [iFirst, iLast] = includeMask.activeRange();
os << "FIELD FieldData 1\n"<<
fieldName << " 1 " << numActivePoints << " float\n";
for(int32 i=iFirst; i<iLast; ++i)
{
if(includeMask(i))
os<< field[i] <<'\n';
}
return true;
}
template<typename IncludeMaskType>
bool addRealx3PointField(
iOstream& os,
word fieldName,
int32 numActivePoints,
realx3* field,
IncludeMaskType includeMask )
{
if(numActivePoints==0) return true;
auto [iFirst, iLast] = includeMask.activeRange();
os << "FIELD FieldData 1\n"<<
fieldName << " 3 " << numActivePoints << " float\n";
for(int32 i=iFirst; i<iLast; ++i)
{
if(includeMask(i))
os<< field[i].x()<<' '<< field[i].y()<<' '<<field[i].z()<<'\n';
}
return true;
}
bool convertTimeFolderPointFields(
fileSystem timeFolder,
real time,
fileSystem destPath,
word bName)
{
// 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 "<< timeFolder <<
" does not contain any pStructure data file. Skipping this folder . . ."
<<defaultColor<<nl;
return true;
}
vtkFile vtk(destPath, bName, time);
if(!vtk) return false;
auto pStructObjPtr = IOobject::make<pointStructure>(pStructHeader);
auto& pStruct = pStructObjPtr().getObject<pointStructure>();
// get a list of files in this timeFolder;
auto posVec = std::as_const(pStruct).pointPosition().hostVectorAll();
auto* pos = posVec.data();
REPORT(1)<<"Writing pointStructure to vtk file with "<< yellowText(pStruct.numActive())
<<" active particles"<<endREPORT;
addUndstrcuturedGridField(
vtk(),
pStruct.numActive(),
pos,
pStruct.activePointsMaskH());
auto fileList = containingFiles(timeFolder);
for(auto& file:fileList)
{
IOfileHeader fieldHeader(
objectFile(
file.wordPath(),
"",
objectFile::READ_ALWAYS,
objectFile::WRITE_ALWAYS) );
if( fieldHeader.headerOk(true) )
{
convertIntTypesPointField(vtk(), fieldHeader, pStruct);
convertRealTypePointField(vtk(), fieldHeader, pStruct);
convertRealx3TypePointField(vtk(), fieldHeader, pStruct);
}
}
return true;
}
bool convertTimeFolderPointFieldsSelected(
fileSystem timeFolder,
real time,
fileSystem destPath,
word bName,
wordVector fieldsName,
bool mustExist)
{
// 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 "<< timeFolder <<
" does not contain any pStructure data file. Skipping this folder . . ."
<<defaultColor<<nl;
return true;
}
vtkFile vtk(destPath, bName, time);
if(!vtk) return false;
auto pStructObjPtr = IOobject::make<pointStructure>(pStructHeader);
auto& pStruct = pStructObjPtr().getObject<pointStructure>();
// get a list of files in this timeFolder;
auto posVec = std::as_const(pStruct).pointPosition().hostVectorAll();
auto* pos = posVec.data();
REPORT(1)<<"Writing pointStructure to vtk file with "<< yellowText(pStruct.numActive())
<<" active particles"<<endREPORT;
addUndstrcuturedGridField(
vtk(),
pStruct.numActive(),
pos,
pStruct.activePointsMaskH());
auto fileList = containingFiles(timeFolder);
for(auto& fname:fieldsName)
{
fileSystem fieldAddress = timeFolder+fname;
IOfileHeader fieldHeader(
objectFile(
fieldAddress.wordPath(),
"",
objectFile::READ_ALWAYS,
objectFile::WRITE_ALWAYS) );
if( fieldHeader.headerOk(true) )
{
convertIntTypesPointField(vtk(), fieldHeader, pStruct);
convertRealTypePointField(vtk(), fieldHeader, pStruct);
convertRealx3TypePointField(vtk(), fieldHeader, pStruct);
}
else
{
if(mustExist)
{
fatalErrorInFunction<<"Field " << fieldAddress <<
" does not exist."<<endl;
return false;
}
else
{
REPORT(1)<<"Could not find "<<yellowText(fieldAddress) <<" skipping . . ."<<endREPORT;
}
}
}
return true;
}
}
#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