34template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
36 : clout( std::cout,
"VTKwriter" ), _createFile(false), _name(name), _binary(binary), _compress(compress)
40template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
43 _pointerVec.push_back(&f);
46template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
49 f.getName() = functorName;
50 _pointerVec.push_back(&f);
54template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
55template<
bool vtmAsMaster>
60 std::string pathPrefixFileData;
61 if constexpr(!vtmAsMaster){
65 pathPrefixFileData =
"";
69 dataPVD(iT, fullNamePVD, nameVTM);
72 pathPrefixFileData =
"data/";
77 preambleVTM(fullNameVTM);
79 for (
int iC = 0; iC < rankSize; iC++) {
80 std::string nameFileData = pathPrefixFileData +
createFileName(nameCollection, iT, iC) + fileExtension;
82 dataVTM(iC, fullNameVTM, nameFileData);
86 closeVTM(fullNameVTM);
90template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
91template<
bool vtmAsMaster,
typename F>
95 std::string fileExtension;
99#ifdef PARALLEL_MODE_MPI
103 if ( _pointerVec.empty() ) {
104 clout <<
"Error: Did you add a Functor ?" << std::endl;
108 if constexpr (parallel) {
111 if constexpr(VTKTYPE==
VTI){
112 fileExtension =
".vti";
115 auto& superStructure =
dynamic_cast<SuperF<D,T>*
>(_pointerVec[0])->getSuperStructure();
116 auto const& cGeometry = superStructure.getCuboidGeometry();
117 LoadBalancer<T> const& loadBalancer = superStructure.getLoadBalancer();
120 superStructure.communicate();
122 const T delta = cGeometry.getMotherCuboid().getDeltaR();
125 for (
int iCloc = 0; iCloc < loadBalancer.
size(); iCloc++) {
128 int globIC = loadBalancer.
glob(iCloc);
130 auto blockGeometry = cGeometry.get(globIC);
137 +
"data/" +
createFileName(nameCollection, iT, globIC ) + fileExtension;
141 T originPhysR[D] = {T()};
142 cGeometry.getPhysR(originPhysR, globIC, originCuboid);
144 preambleVTI(fullNameFileData, extent0, extent1, originPhysR, delta);
146 applyFunctors(fullNameFileData, extent1, globIC);
148 closePiece(fullNameFileData);
149 closeVTI(fullNameFileData);
153 }
else if constexpr(VTKTYPE==
VTU){
154 fileExtension =
".vtu";
157 LoadBalancer<T> const& loadBalancer = _pointerVec[0]->getLoadBalancer();
161 for (
int iCloc = 0; iCloc < loadBalancer.
size(); iCloc++) {
163 int globIC = loadBalancer.
glob(iCloc);
165 Vector<int,1> extent1 = _pointerVec[0]->getContainerF(iCloc).getContainerSize();
168 +
"data/" +
createFileName(nameCollection, iT, globIC ) + fileExtension;
170 preambleVTU(fullNameFileData,extent1);
172 applyFunctors(fullNameFileData, extent1, globIC);
174 this->cellDataVTU(fullNameFileData, extent1);
176 this->dataArrayPoints(fullNameFileData, extent1, globIC);
178 closeVTU(fullNameFileData);
183 fileExtension =
".vtp";
184 clout <<
"Error: VTP type not implemented yet" << std::endl;
188 if (rank == 0) { writeVTM<vtmAsMaster>(iT,rankSize,fileExtension,nameCollection); }
192 fileExtension =
".vtu";
202 std::string nameFileData =
"data/" +
createFileName(nameCollection, iT ) + fileExtension;
204 dataPVD(iT, fullNamePVD, nameFileData);
208 preambleVTU(fullNameFileData, extent1);
210 applyFunctors(fullNameFileData, extent1);
212 this->cellDataVTU(fullNameFileData, extent1);
214 this->dataArrayPoints(fullNameFileData, extent1);
216 closeVTU(fullNameFileData);
223template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
227 if constexpr(VTKTYPE==
VTI){
229 auto applyFunctors = [&](std::string fullNameFileData,
Vector<int,D> extent1,
int globIC=0 )
231 for (
int iF=0; iF<_pointerVec.size(); ++iF) {
232 this->dataArraySingleFunctor(fullNameFileData, *_pointerVec[iF], extent1, globIC);
236 write<false>(iT,_name,applyFunctors);
239 }
else if constexpr(VTKTYPE==
VTU){
241 auto applyFunctors = [&](std::string fullNameFileData,
Vector<int,1> extent1,
int globIC=0 )
243 for (std::size_t iF=1; iF<_pointerVec.size(); ++iF) {
244 this->dataArraySingleFunctor(fullNameFileData, *_pointerVec[iF], extent1, globIC);
248 write<false>(iT,_name, applyFunctors);
253template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
257 if constexpr(VTKTYPE==
VTI){
259 auto applyFunctors = [&](std::string fullNameFileData,
Vector<int,D> extent1,
int globIC=0 ){
260 this->dataArraySingleFunctor(fullNameFileData, f, extent1, globIC); };
262 write<true>(iT,f.getName(),applyFunctors);
265 }
else if constexpr(VTKTYPE==
VTU){
267 auto applyFunctors = [&](std::string fullNameFileData,
Vector<int,1> extent1,
int globIC=0 ){
268 this->dataArraySingleFunctor(fullNameFileData, f, extent1, globIC); };
270 write<true>(iT,f.getName(),applyFunctors);
275template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
282template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
286#ifdef PARALLEL_MODE_MPI
292 preamblePVD(fullNamePVD);
293 closePVD(fullNamePVD);
298template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
302 std::ofstream fout(fullName, std::ios::trunc);
304 clout <<
"Error: could not open " << fullName << std::endl;
306 fout <<
"<?xml version=\"1.0\"?>\n";
307 fout <<
"<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" ";
309 fout <<
"byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n";
312 fout <<
"byte_order=\"LittleEndian\">\n";
314 fout <<
"<UnstructuredGrid>" << std::endl;
315 fout <<
"<Piece NumberOfPoints=\"" << extent1[0]
316 <<
"\" NumberOfCells=\"" << extent1[0] <<
"\">"
318 fout <<
"<PointData Vectors=\"Particles\">" << std::endl;
322template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
324 const std::string& fullNamePiece)
326 std::ofstream fout(fullNamePiece.c_str(), std::ios::app);
328 clout <<
"Error: could not open " << fullNamePiece << std::endl;
330 fout <<
"</Piece>" << std::endl;
331 fout <<
"</UnstructuredGrid>\n";
332 fout <<
"</VTKFile>\n";
340template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
342 const std::string& fullName,
346 std::ofstream fout(fullName, std::ios::trunc);
348 clout <<
"Error: could not open " << fullName << std::endl;
350 fout <<
"<?xml version=\"1.0\"?>\n";
351 fout <<
"<VTKFile type=\"ImageData\" version=\"0.1\" ";
353 fout <<
"byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n";
356 fout <<
"byte_order=\"LittleEndian\">\n";
359 fout <<
"<ImageData WholeExtent=\""
360 << extent0[0] <<
" "<< extent1[0];
361 for (
unsigned iDim=1; iDim<D; ++iDim){
362 fout <<
" " << extent0[iDim] <<
" " << extent1[iDim];
364 fout <<
"\" Origin=\"" << origin[0];
365 for (
unsigned iDim=1; iDim<D; ++iDim){
366 fout <<
" " << origin[iDim];
368 fout <<
"\" Spacing=\"" << delta <<
" " << delta <<
" " << delta <<
"\">\n";
369 fout <<
"<Piece Extent=\""
370 << extent0[0] <<
" "<< extent1[0];
371 for (
unsigned iDim=1; iDim<D; ++iDim){
372 fout <<
" " << extent0[iDim] <<
" "<< extent1[iDim];
375 fout <<
"<PointData>\n";
380template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
382 const std::string& fullNamePiece)
384 std::ofstream fout(fullNamePiece, std::ios::app );
386 clout <<
"Error: could not open " << fullNamePiece << std::endl;
388 fout <<
"</ImageData>\n";
389 fout <<
"</VTKFile>\n";
393template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
396 std::ofstream fout(fullNamePiece, std::ios::app );
398 clout <<
"Error: could not open " << fullNamePiece << std::endl;
400 fout <<
"</PointData>\n";
401 fout <<
"</Piece>\n";
406template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
408 const std::string& fullNamePVD)
410 std::ofstream fout(fullNamePVD.c_str(), std::ios::trunc);
412 clout <<
"Error: could not open " << fullNamePVD << std::endl;
415 fout <<
"<?xml version=\"1.0\"?>\n";
416 fout <<
"<VTKFile type=\"Collection\" version=\"0.1\" "
417 <<
"byte_order=\"LittleEndian\">\n" <<
"<Collection>\n";
421template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
423 const std::string& fullNamePVD)
425 std::ofstream fout(fullNamePVD.c_str(), std::ios::app);
427 clout <<
"Error: could not open " << fullNamePVD << std::endl;
429 fout <<
"</Collection>\n";
430 fout <<
"</VTKFile>\n";
437template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
440 std::ofstream fout(fullNameVTM, std::ios::trunc);
442 clout <<
"Error: could not open " << fullNameVTM << std::endl;
444 fout <<
"<?xml version=\"1.0\"?>\n";
445 fout <<
"<VTKFile type=\"vtkMultiBlockDataSet\" version=\"1.0\" "
446 <<
"byte_order=\"LittleEndian\">\n"
447 <<
"<vtkMultiBlockDataSet>\n" ;
451template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
454 std::ofstream fout(fullNameVTM, std::ios::app );
456 clout <<
"Error: could not open " << fullNameVTM << std::endl;
458 fout <<
"</vtkMultiBlockDataSet>\n";
459 fout <<
"</VTKFile>\n";
464template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
466 const std::string& namePiece)
468 std::ofstream fout(fullNameVTM, std::ios::app);
470 clout <<
"Error: could not open " << fullNameVTM << std::endl;
472 fout <<
"<Block index=\"" << iC <<
"\" >\n";
473 fout <<
"<DataSet index= \"0\" " <<
"file=\"" << namePiece <<
"\">\n"
475 fout <<
"</Block>\n";
479template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
481 const std::string& fullNamePVD,
const std::string& namePiece)
483 std::ofstream fout(fullNamePVD.c_str(),
484 std::ios::in | std::ios::out | std::ios::ate);
486 fout.seekp(-25, std::ios::end);
488 fout <<
"<DataSet timestep=\"" << iT <<
"\" "
489 <<
"group=\"\" part=\"\" "
490 <<
"file=\"" << namePiece <<
"\"/>\n";
492 closePVD(fullNamePVD);
495 clout <<
"Error: could not open " << fullNamePVD << std::endl;
499template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
500template<
unsigned sourceDim>
502 const std::string& fullName,
506 std::ofstream fout(fullName.c_str(), std::ios::app);
508 clout <<
"Error: could not open " << fullName << std::endl;
510 fout <<
"<Points>" << std::endl;
512 this->dataArraySingleFunctor(fullName, *_pointerVec[0], extent1, iC);
513 fout <<
"</Points>" << std::endl;
518template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
520 std::ofstream fout(fullName.c_str(), std::ios::app);
522 clout <<
"Error: could not open " << fullName << std::endl;
524 fout <<
"</PointData>" << std::endl;
525 fout <<
"<CellData /> " << std::endl;
526 fout <<
"<Cells>" << std::endl;
527 fout <<
"<DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">"
530 for (
int iTmp=0; iTmp<extent1[0]; ++iTmp){ fout << i32++ <<
" "; }
531 fout <<
"</DataArray>" << std::endl;
532 fout <<
"<DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">"
535 for (
int iTmp=0; iTmp<extent1[0]; ++iTmp){ fout << i32++ <<
" "; }
536 fout <<
"</DataArray>" << std::endl;
537 fout <<
"<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">"
539 for (
int iTmp=0; iTmp<extent1[0]; ++iTmp){ fout << 1 <<
" "; }
540 fout <<
"</DataArray>" << std::endl;
541 fout <<
"</Cells>" << std::endl;
546template<
typename T,
typename FUNCTOR, vtkType VTKTYPE>
547template<
unsigned sourceDim>
549 const std::string& fullName,
553 std::ofstream fout(fullName.c_str(), std::ios::app);
555 clout <<
"Error: could not open " << fullName << std::endl;
557 fout <<
"<DataArray type=\"Float32\" Name=\"" << f.getName() <<
"\" NumberOfComponents=\"" << f.getTargetDim() <<
"\" ";
558 if (_compress || _binary) {
559 fout <<
"format=\"binary\" encoding=\"base64\">\n";
567 std::vector<T> evaluated(f.getTargetDim());
570 if constexpr(parallel){ i[0] = iC;}
573 size_t numberOfFloats;
574 std::unique_ptr<float[]> streamFloat;
578 if constexpr(VTKTYPE==
VTI){
581 numberOfFloats = f.getTargetDim() * (extent1[0]+2) * (extent1[1]+2);
582 streamFloat = std::make_unique<float[]>(numberOfFloats);
583 for (i[2]=-1; i[2]<extent1[1]+1; ++i[2]) {
584 for (i[1]=-1; i[1]<extent1[0]+1; ++i[1]) {
585 f(evaluated.data(),i);
586 for (
int iDim=0; iDim < f.getTargetDim(); ++iDim){
587 streamFloat[itter] = float( evaluated[iDim] );
593 }
else if constexpr(D==3){
594 numberOfFloats = f.getTargetDim() * (extent1[0]+2) * (extent1[1]+2) * (extent1[2]+2);
595 streamFloat = std::make_unique<float[]>(numberOfFloats);
596 for (i[3]=-1; i[3]<extent1[2]+1; ++i[3]) {
597 for (i[2]=-1; i[2]<extent1[1]+1; ++i[2]) {
598 for (i[1]=-1; i[1]<extent1[0]+1; ++i[1]) {
599 f(evaluated.data(),i);
600 for (
int iDim=0; iDim < f.getTargetDim(); ++iDim){
601 streamFloat[itter] = float( evaluated[iDim] );
608 clout <<
"Error: only 2D and 3D supportet" << std::endl;
612 }
else if constexpr(VTKTYPE==
VTU){
613 numberOfFloats = f.getTargetDim() * extent1[0];
614 streamFloat = std::make_unique<float[]>(numberOfFloats);
616 if constexpr(parallel){
617 for (i[1]=0; i[1]<extent1[0]; ++i[1]) {
618 f(evaluated.data(),i);
619 for (
int iDim=0; iDim < f.getTargetDim(); ++iDim){
620 streamFloat[itter] = float( evaluated[iDim] );
626 for (i[0]=0; i[0]<extent1[0]; ++i[0]) {
627 f(evaluated.data(),i);
628 for (
int iDim=0; iDim < f.getTargetDim(); ++iDim){
629 streamFloat[itter] = float( evaluated[iDim] );
635 clout <<
"Error: VTP format not implemented yet" << std::endl;
639 uint32_t binarySize =
static_cast<uint32_t
>( numberOfFloats*
sizeof(float) );
644 const unsigned char* charData =
reinterpret_cast<unsigned char*
>(streamFloat.get());
646 std::unique_ptr<unsigned char[]> comprData(
new unsigned char[ binarySize ]);
649 uLongf sizeCompr = compressBound(binarySize);
650 compress2( comprData.get(), &sizeCompr, charData, binarySize, -1);
654 uint32_t prefix[4] = {1,binarySize,binarySize,
static_cast<uint32_t
>(sizeCompr)};
655 prefixEncoder.
encode(prefix, 4);
659 dataEncoder.
encode(comprData.get(), sizeCompr);
660 }
else if (_binary) {
663 prefixEncoder.
encode(&binarySize, 1);
666 dataEncoder.
encode(streamFloat.get(),numberOfFloats);
668 for (
size_t iOut = 0; iOut < numberOfFloats; ++iOut ) {
669 fout << streamFloat[iOut] <<
" ";
674 std::ofstream ffout( fullName, std::ios::out | std::ios::app );
676 clout <<
"Error: could not open " << fullName << std::endl;
678 ffout <<
"\n</DataArray>\n";
void encode(const T *data, size_t length)
Base class for all LoadBalancer.
void closeVTI(const std::string &fullNamePiece)
performes </ImageData> and </VTKFile>
void dataArraySingleFunctor(const std::string &fullName, FUNCTOR &f, Vector< int, sourceDim > extent1, int iC=0)
writes functors stored at pointerVec
void closeVTU(const std::string &fullNamePiece)
performes </ImageData> and </VTKFile>
void preambleVTM(const std::string &fullNameVTM)
performes <VTKFile ...> and <Collection>
VTKwriter(const std::string &name, bool binary=true, bool compress=true)
constructor
void preamblePVD(const std::string &fullNamePVD)
performes <VTKFile ...> and <Collection>
void createMasterFile()
have to be called before calling write(int iT=0), since it creates
void closeVTM(const std::string &fullNameVTM)
performes </Collection> and </VTKFile>
void dataVTM(int iC, const std::string &fullNameVTM, const std::string &namePiece)
performes <DataSet timestep= ... file=namePiece > used for linking vti into pvd files
void dataArrayPoints(const std::string &fullName, Vector< int, sourceDim > extent1, int iC=0)
writes points necessary for VTU
void write(int iT, std::string nameCollection, F applyFunctors)
to clear stored functors, not yet used due to lack of necessity
void closePiece(const std::string &fullNamePiece)
performes </PointData> and </Piece>
void preambleVTU(const std::string &fullName, Vector< int, 1 > extent1)
performes <VTKFile ...>, <ImageData ...> and <PieceExtent ...>
void preambleVTI(const std::string &fullName, const LatticeR< D > extent0, const LatticeR< D > extent1, PhysR< T, D > origin, T delta)
performes <VTKFile ...>, <ImageData ...>, <PieceExtent ...> and <PointData ...>
void closePVD(const std::string &fullNamePVD)
performes </Collection> and </VTKFile>
void cellDataVTU(const std::string &fullName, Vector< int, 1 > extent1)
TODO: add description: connectivity, offsete, type of unscructured nodes.
void addFunctor(FUNCTOR &f)
put functor to _pointerVec to simplify writing process of several functors
void writeVTM(int iT, int rankSize, std::string fileExtension, std::string nameCollection)
wrapper for VTM file creation
void dataPVD(int iT, const std::string &fullNamePVD, const std::string &namePiece)
performes <DataSet timestep= ... file=namePiece >
std::string getVtkOutDir() const
int getRank() const
Returns the process ID.
Directories & directories()
Top level namespace for all of OpenLB.
std::string createFileName(std::string name)
for .pvd masterFile
std::conditional_t< D==2, SuperF2D< T, U >, SuperF3D< T, U > > SuperF