From 815b134e1ea3793f7935312bfc3a0d47d8b89587 Mon Sep 17 00:00:00 2001 From: Hamidreza Norouzi Date: Sat, 30 Mar 2024 01:42:51 -0700 Subject: [PATCH] pFlowToVTK enhanced - pointField conversion is completed, tested (finalized). - geometry conversion is not complete yet. --- utilities/pFlowToVTK/CMakeLists.txt | 1 + utilities/pFlowToVTK/pFlowToVTK.cpp | 13 +- utilities/pFlowToVTK/pointFieldToVTK.cpp | 332 +++++++++++++++ utilities/pFlowToVTK/pointFieldToVTK.hpp | 499 +++++------------------ 4 files changed, 438 insertions(+), 407 deletions(-) create mode 100644 utilities/pFlowToVTK/pointFieldToVTK.cpp diff --git a/utilities/pFlowToVTK/CMakeLists.txt b/utilities/pFlowToVTK/CMakeLists.txt index 353daa6b..15f37471 100644 --- a/utilities/pFlowToVTK/CMakeLists.txt +++ b/utilities/pFlowToVTK/CMakeLists.txt @@ -1,5 +1,6 @@ set(source_files +pointFieldToVTK.cpp pFlowToVTK.cpp #geometric.cpp ) diff --git a/utilities/pFlowToVTK/pFlowToVTK.cpp b/utilities/pFlowToVTK/pFlowToVTK.cpp index 9046fb63..be53c75d 100755 --- a/utilities/pFlowToVTK/pFlowToVTK.cpp +++ b/utilities/pFlowToVTK/pFlowToVTK.cpp @@ -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", " "); - bool isCoupling = false; + //bool isCoupling = false; if(!cmds.parse(argc, argv)) return 0; @@ -88,8 +88,8 @@ int main(int argc, char** argv ) timeFolder folders(Control); - fileSystem destFolder = fileSystem(outFolder)/word(geometryFolder__); - fileSystem destFolderField = fileSystem(outFolder); + auto destFolder = fileSystem(outFolder)/word(geometryFolder__); + auto destFolderField = fileSystem(outFolder); wordList geomfiles{"triSurface"}; @@ -141,9 +141,8 @@ int main(int argc, char** argv ) } }else { - /*if(!pFlow::PFtoVTK::convertTimeFolderPointFieldsSelected( - folders.folder(), - folders.time(), + if(!pFlow::PFtoVTK::convertTimeFolderPointFieldsSelected( + Control, destFolderField, "sphereFields", fields, @@ -151,7 +150,7 @@ int main(int argc, char** argv ) ) { fatalExit; - }*/ + } } } diff --git a/utilities/pFlowToVTK/pointFieldToVTK.cpp b/utilities/pFlowToVTK/pointFieldToVTK.cpp new file mode 100644 index 00000000..f2e076cd --- /dev/null +++ b/utilities/pFlowToVTK/pointFieldToVTK.cpp @@ -0,0 +1,332 @@ +#include + +#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(vtk(), fieldHeader, pStruct) || + convertIntPointField(vtk(), fieldHeader, pStruct) || + convertIntPointField(vtk(), fieldHeader, pStruct) || + convertIntPointField(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(vtk(), fieldHeader, pStruct) || + convertIntPointField(vtk(), fieldHeader, pStruct) || + convertIntPointField(vtk(), fieldHeader, pStruct) || + convertIntPointField(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(objectType)) + return false; + + auto field = realPointField_H( + header, + pStruct, + static_cast(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(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]; +} \ No newline at end of file diff --git a/utilities/pFlowToVTK/pointFieldToVTK.hpp b/utilities/pFlowToVTK/pointFieldToVTK.hpp index 587ee6a4..8b24088c 100755 --- a/utilities/pFlowToVTK/pointFieldToVTK.hpp +++ b/utilities/pFlowToVTK/pointFieldToVTK.hpp @@ -1,443 +1,142 @@ /*------------------------------- phasicFlow --------------------------------- - O C enter of - O O E ngineering and - O O M ultiscale modeling of - OOOOOOO F luid flow + 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 + 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 + 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 - -#include "vtkFile.hpp" #include "systemControl.hpp" #include "pointStructure.hpp" #include "pointFields.hpp" - namespace pFlow::PFtoVTK { -/*template -bool addIntPointField( - iOstream& os, - word fieldName, - int32 numActivePoints, - IntType* field, - IncludeMaskType includeMask ); + bool convertTimeFolderPointFields( + systemControl &control, + const fileSystem &destPath, + const word &bName); -template -bool addRealPointField( - iOstream& os, - word fieldName, - int32 numActivePoints, - real* field, - IncludeMaskType includeMask ); + bool convertTimeFolderPointFieldsSelected( + systemControl &control, + const fileSystem &destPath, + const word &bName, + const wordVector &fieldsName, + bool mustExist); -template -bool addRealx3PointField( - iOstream& os, - word fieldName, - int32 numActivePoints, - realx3* field, - IncludeMaskType includeMask );*/ + bool addUndstrcuturedGridField( + iOstream &os, + realx3 *position, + uint32 numPoints); -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]; -} + bool convertRealTypePointField( + iOstream &os, + const IOfileHeader &header, + pointStructure &pStruct); -template -bool checkFieldType(word objectType) -{ - return regexCheck(pointField::TYPENAME(), objectType); -} + bool convertRealx3TypePointField( + iOstream &os, + const IOfileHeader &header, + pointStructure &pStruct); -/*template -bool convertIntPointField -( - iOstream& os, - const IOfileHeader& header, - const pointStructure& pStruct -) -{ + template + bool addIntPointField( + iOstream &os, + const word &fieldName, + IntType *field, + uint32 numData); - using PointFieldType = pointField; + 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(objectType)) + template + bool checkFieldType(word objectType); + + bool regexCheck(const word &TYPENAME, const word &fieldType); + + template + inline bool checkFieldType(word objectType) { - return false; + return regexCheck(pointField::TYPENAME(), objectType); } - auto objField = IOobject::make - ( - header, - pStruct, - static_cast(0) - ); - - auto& Field = objField().template getObject(); - - T* data = Field.deviceVectorAll().data(); - - REPORT(2)<<"writing "<< greenColor <(objectType))return false; - - auto objField = IOobject::make - ( - header, - pStruct, - static_cast(0) - ); - - auto& Field = objField().getObject(); - - real* data = Field.hostVectorAll().data(); - - REPORT(2)<<"writing "<< greenColor <(objectType))return false; - - auto objField = IOobject::make - ( - header, - pStruct, - static_cast(0) - ); - - auto& Field = objField().getObject(); - - realx3* data = Field.hostVectorAll().data(); - - REPORT(2)<<"writing "<< greenColor < + inline bool convertIntPointField( + iOstream &os, + const IOfileHeader &header, + pointStructure &pStruct) { - os<< position[i].x()<<' '<< position[i].y()<<' '<; + + word objectType = header.objectType(); + + if (!checkFieldType(objectType)) + { + return false; + } + + auto field = PointFieldType( + header, + pStruct, + static_cast(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'; - for(uint32 i=0; 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 -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 -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 -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(vtk(), fieldHeader, pStruct); - convertIntPointField(vtk(), fieldHeader, pStruct); - convertIntPointField(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<(pStructHeader); - auto& pStruct = pStructObjPtr().getObject(); - - // 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"<(vtk(), fieldHeader, pStruct); - convertIntPointField(vtk(), fieldHeader, pStruct); - convertIntPointField(vtk(), fieldHeader, pStruct); - convertRealTypePointField(vtk(), fieldHeader, pStruct); - convertRealx3TypePointField(vtk(), fieldHeader, pStruct); - } - else - { - if(mustExist) - { - fatalErrorInFunction<<"Field " << fieldAddress << - " does not exist."<