pFlowToVTK enhanced

- pointField conversion is completed, tested (finalized).
- geometry conversion is not complete yet.
This commit is contained in:
Hamidreza Norouzi 2024-03-30 01:42:51 -07:00
parent f288f812fd
commit 815b134e1e
4 changed files with 438 additions and 407 deletions

View File

@ -1,5 +1,6 @@
set(source_files set(source_files
pointFieldToVTK.cpp
pFlowToVTK.cpp pFlowToVTK.cpp
#geometric.cpp #geometric.cpp
) )

View File

@ -78,7 +78,7 @@ int main(int argc, char** argv )
"a space separated lists of time folders, or a strided range begin:stride:end, or an interval begin:end", "a space separated lists of time folders, or a strided range begin:stride:end, or an interval begin:end",
" "); " ");
bool isCoupling = false; //bool isCoupling = false;
if(!cmds.parse(argc, argv)) return 0; if(!cmds.parse(argc, argv)) return 0;
@ -88,8 +88,8 @@ int main(int argc, char** argv )
timeFolder folders(Control); timeFolder folders(Control);
fileSystem destFolder = fileSystem(outFolder)/word(geometryFolder__); auto destFolder = fileSystem(outFolder)/word(geometryFolder__);
fileSystem destFolderField = fileSystem(outFolder); auto destFolderField = fileSystem(outFolder);
wordList geomfiles{"triSurface"}; wordList geomfiles{"triSurface"};
@ -141,9 +141,8 @@ int main(int argc, char** argv )
} }
}else }else
{ {
/*if(!pFlow::PFtoVTK::convertTimeFolderPointFieldsSelected( if(!pFlow::PFtoVTK::convertTimeFolderPointFieldsSelected(
folders.folder(), Control,
folders.time(),
destFolderField, destFolderField,
"sphereFields", "sphereFields",
fields, fields,
@ -151,7 +150,7 @@ int main(int argc, char** argv )
) )
{ {
fatalExit; fatalExit;
}*/ }
} }
} }

View File

@ -0,0 +1,332 @@
#include <regex>
#include "vtkFile.hpp"
#include "vocabs.hpp"
#include "pointFieldToVTK.hpp"
bool pFlow::PFtoVTK::convertTimeFolderPointFields(
systemControl &control,
const fileSystem &destPath,
const word &bName)
{
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());
if (!vtk)
return false;
auto pStruct = pointStructure(control);
// get a list of files in this timeFolder;
auto posVec = pStruct.pointPositionHost();
auto *pos = posVec.data();
REPORT(1) << "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))
{
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)
{
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());
if (!vtk)
return false;
auto pStruct = pointStructure(control);
// get a list of files in this timeFolder;
auto posVec = pStruct.pointPositionHost();
auto *pos = posVec.data();
REPORT(1) << "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 (
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(1) << "Could not find " << Yellow_Text(fieldAddress) <<
". Skipping this field . . ." << END_REPORT;
}
}
}
return true;
}
bool pFlow::PFtoVTK::addUndstrcuturedGridField(
iOstream &os,
realx3 *position,
uint32 numPoints)
{
os << "DATASET UNSTRUCTURED_GRID\n";
os << "POINTS " << numPoints << " float\n";
if (numPoints == 0)
return true;
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(
iOstream &os,
const IOfileHeader &header,
pointStructure &pStruct)
{
word objectType = header.objectType();
if (!checkFieldType<real>(objectType))
return false;
auto field = realPointField_H(
header,
pStruct,
static_cast<real>(0));
real const *data = field.deviceViewAll().data();
REPORT(1) << "writing " << Green_Text(header.objectName()) <<
" field to vtk." << END_REPORT;
return addRealPointField(
os,
header.objectName(),
data,
pStruct.numActive());
}
bool pFlow::PFtoVTK::convertRealx3TypePointField(
iOstream &os,
const IOfileHeader &header,
pointStructure &pStruct)
{
word objectType = header.objectType();
if (!checkFieldType<realx3>(objectType))
return false;
auto field = realx3PointField_H(
header,
pStruct,
{0.0, 0.0, 0.0});
realx3 const *data = field.deviceViewAll().data();
REPORT(1) << "writing " << Green_Text(header.objectName()) <<
" field to vtk." << END_REPORT;
return addRealx3PointField(
os,
header.objectName(),
data,
pStruct.numActive());
}
bool pFlow::PFtoVTK::addRealPointField(
iOstream &os,
const word &fieldName,
const real *field,
uint32 numData)
{
if (numData == 0)
return true;
os << "FIELD FieldData 1\n"
<< fieldName << " 1 " << numData << " float\n";
for (uint32 i = 0; i < numData; ++i)
{
os << field[i] << '\n';
}
return true;
}
bool pFlow::PFtoVTK::addRealx3PointField(
iOstream &os,
const word &fieldName,
const realx3 *field,
uint32 numData)
{
if (numData == 0)
return true;
os << "FIELD FieldData 1\n"
<< fieldName << " 3 " << numData << " float\n";
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];
}

View File

@ -1,443 +1,142 @@
/*------------------------------- phasicFlow --------------------------------- /*------------------------------- phasicFlow ---------------------------------
O C enter of O C enter of
O O E ngineering and O O E ngineering and
O O M ultiscale modeling of O O M ultiscale modeling of
OOOOOOO F luid flow OOOOOOO F luid flow
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
Copyright (C): www.cemf.ir Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
Licence: Licence:
This file is part of phasicFlow code. It is a free software for simulating 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 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. 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 phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/ -----------------------------------------------------------------------------*/
#ifndef __pointFieldToVTK_hpp__ #ifndef __pointFieldToVTK_hpp__
#define __pointFieldToVTK_hpp__ #define __pointFieldToVTK_hpp__
#include <regex>
#include "vtkFile.hpp"
#include "systemControl.hpp" #include "systemControl.hpp"
#include "pointStructure.hpp" #include "pointStructure.hpp"
#include "pointFields.hpp" #include "pointFields.hpp"
namespace pFlow::PFtoVTK namespace pFlow::PFtoVTK
{ {
/*template<typename IntType, typename IncludeMaskType> bool convertTimeFolderPointFields(
bool addIntPointField( systemControl &control,
iOstream& os, const fileSystem &destPath,
word fieldName, const word &bName);
int32 numActivePoints,
IntType* field,
IncludeMaskType includeMask );
template<typename IncludeMaskType> bool convertTimeFolderPointFieldsSelected(
bool addRealPointField( systemControl &control,
iOstream& os, const fileSystem &destPath,
word fieldName, const word &bName,
int32 numActivePoints, const wordVector &fieldsName,
real* field, bool mustExist);
IncludeMaskType includeMask );
template<typename IncludeMaskType> bool addUndstrcuturedGridField(
bool addRealx3PointField( iOstream &os,
iOstream& os, realx3 *position,
word fieldName, uint32 numPoints);
int32 numActivePoints,
realx3* field,
IncludeMaskType includeMask );*/
bool regexCheck(word TYPENAME, word fieldType) bool convertRealTypePointField(
{ iOstream &os,
std::regex match("pointField\\<([A-Za-z1-9_]*)\\,([A-Za-z1-9_]*)\\>"); const IOfileHeader &header,
std::smatch search1, search2; pointStructure &pStruct);
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 convertRealx3TypePointField(
bool checkFieldType(word objectType) iOstream &os,
{ const IOfileHeader &header,
return regexCheck(pointField<Type>::TYPENAME(), objectType); pointStructure &pStruct);
}
/*template<typename T> template <typename IntType>
bool convertIntPointField bool addIntPointField(
( iOstream &os,
iOstream& os, const word &fieldName,
const IOfileHeader& header, IntType *field,
const pointStructure& pStruct uint32 numData);
)
{
using PointFieldType = pointField<VectorSingle, T, HostSpace>; bool addRealPointField(
iOstream &os,
const word &fieldName,
const real *field,
uint32 numData);
word objectType = header.objectType(); bool addRealx3PointField(
iOstream &os,
const word &fieldName,
const realx3 *field,
uint32 numData);
if(!checkFieldType<T>(objectType)) template <typename Type>
bool checkFieldType(word objectType);
bool regexCheck(const word &TYPENAME, const word &fieldType);
template <typename Type>
inline bool checkFieldType(word objectType)
{ {
return false; return regexCheck(pointField<Type>::TYPENAME(), objectType);
} }
auto objField = IOobject::make<PointFieldType> template <typename IntType>
( inline bool convertIntPointField(
header, iOstream &os,
pStruct, const IOfileHeader &header,
static_cast<T>(0) pointStructure &pStruct)
);
auto& Field = objField().template getObject<PointFieldType>();
T* data = Field.deviceVectorAll().data();
REPORT(2)<<"writing "<< greenColor <<header.objectName()<<defaultColor<<" field to vtk.\n";
return addIntPointField(
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() );
}*/
bool addUndstrcuturedGridField(
iOstream& os,
realx3* position,
uint32 numPoints)
{
os<< "DATASET UNSTRUCTURED_GRID\n";
os<< "POINTS "<< numPoints << " float\n";
if(numPoints==0) return true;
for(uint32 i=0; i<numPoints; i++)
{ {
os<< position[i].x()<<' '<< position[i].y()<<' '<<position[i].z()<<'\n';
using PointFieldType = pointField<IntType, HostSpace>;
word objectType = header.objectType();
if (!checkFieldType<IntType>(objectType))
{
return false;
}
auto field = PointFieldType(
header,
pStruct,
static_cast<IntType>(0));
const IntType *data = field.deviceViewAll().data();
REPORT(1) << "writing " << Green_Text(header.objectName()) << " field to vtk.\n";
return addIntPointField(
os,
header.objectName(),
data,
pStruct.numActive());
} }
os<<"CELLS "<< numPoints<<' '<< 2*numPoints<<'\n'; template <typename IntType>
for(uint32 i=0; i<numPoints; i++) inline bool addIntPointField(
iOstream &os,
const word &fieldName,
IntType *field,
uint32 numData)
{ {
os<< 1 <<' '<< i<<'\n'; if (numData == 0)
} return true;
os<<"CELL_TYPES "<< numPoints<<'\n'; os << "FIELD FieldData 1\n"
<< fieldName << " 1 " << numData << " int\n";
for (uint32 i = 0; i < numData; ++i)
{
os << field[i] << '\n';
}
for(int32 i=0; i<numPoints; i++)
{
os<< 1 <<'\n';
}
os << "POINT_DATA " << numPoints << endl;
return true;
}
/*template<typename IntType, typename IncludeMaskType>
bool addIntPointField(
iOstream& os,
word fieldName,
int32 numActivePoints,
IntType* 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(
systemControl& control,
fileSystem destPath,
word bName)
{
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; return true;
} }
vtkFile vtk(destPath, bName, control.time().currentTime());
if(!vtk) return false;
auto pStruct = pointStructure(control);
// get a list of files in this timeFolder;
auto posVec = pStruct.pointPositionHost();
auto* pos = posVec.data();
REPORT(1)<<"Writing pointStructure to vtk file with "<< Yellow_Text(pStruct.numActive())
<<" active particles"<<END_REPORT;
addUndstrcuturedGridField(
vtk(),
pos,
pStruct.numActive());
//auto fileList = containingFiles(timeFolder);
/*for(auto& file:fileList)
{
IOfileHeader fieldHeader(
objectFile(
file.wordPath(),
"",
objectFile::READ_ALWAYS,
objectFile::WRITE_ALWAYS) );
if( fieldHeader.headerOk(true) )
{
convertIntPointField<int32>(vtk(), fieldHeader, pStruct);
convertIntPointField<int64>(vtk(), fieldHeader, pStruct);
convertIntPointField<int8>(vtk(), fieldHeader, pStruct);
convertRealTypePointField(vtk(), fieldHeader, pStruct);
convertRealx3TypePointField(vtk(), fieldHeader, pStruct);
}
}*/
return true;
} }
#endif
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) )
{
convertIntPointField<int32>(vtk(), fieldHeader, pStruct);
convertIntPointField<int64>(vtk(), fieldHeader, pStruct);
convertIntPointField<int8>(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