OpenLB 1.8.1
Loading...
Searching...
No Matches
vtuPointWriter.hh
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2024 Christoph Gaul
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24
25#ifndef VTU_POINT_WRITER_HH
26#define VTU_POINT_WRITER_HH
27
28#include "vtuPointWriter.h"
29
30
31namespace olb{
32
33
34template<typename T, typename W>
35VTUpointWriter<T,W,2>::VTUpointWriter( std::string const name, bool binary )
36 : _name( name ), _binary( binary ),
37 clout(std::cout, "VTUpointWriter2D")
38{}
39
40template<typename T, typename W>
41void VTUpointWriter<T,W,2>::addFunctor( AnalyticalF2D<T,W>& f, const std::string& name ){
42 f.getName() = name;
43 functorsA.push_back(&f);
44}
45
46template<typename T, typename W>
48 functorsA.push_back(&f);
49}
50
51template<typename T, typename W>
52void VTUpointWriter<T,W,2>::addFunctor( SuperF2D<T,W>& f, const std::string& name ){
53 f.getName() = name;
54 functors.push_back(&f);
55}
56
57template<typename T, typename W>
59 functors.push_back(&f);
60}
61
62template<typename T, typename W>
63void VTUpointWriter<T,W,2>::addPoints( std::vector<Vector<T,2>>& new_positions ){
64 for( unsigned long i=0; i<new_positions.size(); i++ ){
65 pos.push_back(new_positions[i]);
66 }
67}
68
69template<typename T, typename W>
71 pos.push_back(new_position);
72}
73
75template<typename T, typename W>
76void VTUpointWriter<T,W,2>::write( std::size_t iT )
77{
78 int rank = 0;
79#ifdef PARALLEL_MODE_MPI
80 rank = singleton::mpi().getRank();
81#endif
82
83
84for (SuperF2D<T,W>* f : functors) {
85 f->getSuperStructure().communicate();
86}
87
88
89 if (rank == 0) {
90 std::string fullNamePVDmaster = singleton::directories().getVtkOutDir()
91 + createFileName(_name) + "_master.pvd";
92 std::string fullNamePVD = singleton::directories().getVtkOutDir() + "data/"
93 + createFileName(_name, iT) + ".pvd";
94 preamblePVD( fullNamePVD ); // timestep
95 std::string namePiece = "data/" + createFileName(_name, iT, 0) + ".vtu";
96 // puts name of .vti piece to a .pvd file [fullNamePVD]
97
98 dataPVD(iT, 1, fullNamePVD, namePiece);
99 // adds a namePiece to master.pvd file.
100 // To do so we overwrite closePVD() and add new entry.
101 dataPVDmaster(iT, 1, fullNamePVDmaster, namePiece);
102 closePVD(fullNamePVD); // timestep
103 } // master only
104 if ( rank == 0){ //master only or different systems each
105 std::string fullNameVTU = singleton::directories().getVtkOutDir()
106 + "data/" + createFileName(_name, iT, rank) + ".vtu";
107 preambleVTU(fullNameVTU, pos.size());
108 //writes data arrays into vtu file
109
110 this->dataArray( fullNameVTU );
111 closeVTU(fullNameVTU);
112}
113}
114
116template<typename T, typename W>
117void VTUpointWriter<T,W,2>::write( std::size_t iT, std::vector<Vector<T,2>>& new_positions )
118{
119 addPoints(new_positions);
120 write(iT);
121}
122
123template<typename T, typename W>
125{
126 std::string fullNamePVDmaster = singleton::directories().getVtkOutDir()
127 + createFileName(_name) + "_master.pvd";
128 preamblePVD(fullNamePVDmaster);
129 closePVD(fullNamePVDmaster);
130
131}
132
133template<typename T, typename W>
135 const std::string& fullNamePVD)
136{
137 std::ofstream fout(fullNamePVD.c_str(), std::ios::trunc);
138 if (!fout) {
139 clout << "Error: could not open " << fullNamePVD << std::endl;
140 }
141 fout << "<?xml version=\"1.0\"?>\n";
142 fout << "<VTKFile type=\"Collection\" version=\"0.1\" "
143 << "byte_order=\"LittleEndian\">\n" << "<Collection>\n";
144 fout.close();
145}
146
147
148template<typename T, typename W>
150 const std::string& fullNamePVD)
151{
152 std::ofstream fout(fullNamePVD.c_str(), std::ios::app);
153 if (!fout) {
154 clout << "Error: could not open " << fullNamePVD << std::endl;
155 }
156 fout << "</Collection>\n";
157 fout << "</VTKFile>\n";
158 fout.close();
159}
160
161
162template<typename T, typename W>
164 const std::string& fullName, int num)
165{
166 std::ofstream fout(fullName.c_str(), std::ios::trunc);
167 if (!fout) {
168 clout << "Error: could not open " << fullName << std::endl;
169 }
170 fout << "<?xml version=\"1.0\"?>" << std::endl << std::flush;
171 fout
172 << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">"
173 << std::endl;
174 fout << "<UnstructuredGrid>" << std::endl;
175 fout << "<Piece NumberOfPoints=\"" << num
176 << "\" NumberOfCells=\"" << num << "\">"
177 << std::endl;
178 fout << "<PointData Vectors=\"Particles\">" << std::endl;
179 fout.close();
180}
181
182template<typename T, typename W>
184 const std::string& fullNamePiece)
185{
186 std::ofstream fout(fullNamePiece.c_str(), std::ios::app);
187 if (!fout) {
188 clout << "Error: could not open " << fullNamePiece << std::endl;
189 }
190 fout << "</UnstructuredGrid>\n";
191 fout << "</VTKFile>\n";
192 fout.close();
193}
194
195
196template<typename T, typename W>
198 const std::string& fullNamePVD, const std::string& namePiece)
199{
200 std::ofstream fout(fullNamePVD.c_str(), std::ios::app);
201 if (!fout) {
202 clout << "Error: could not open " << fullNamePVD << std::endl;
203 }
204 fout << "<DataSet timestep=\"" << iT << "\" " << "group=\"\" part=\" " << i
205 << "\" " << "file=\"" << namePiece << "\"/>\n";
206 fout.close();
207}
208
209template<typename T, typename W>
211 const std::string& fullNamePVDMaster, const std::string& namePiece)
212{
213 std::ofstream fout(fullNamePVDMaster.c_str(),
214 std::ios::in | std::ios::out | std::ios::ate);
215 if (fout) {
216 fout.seekp(-25, std::ios::end); // jump -25 form the end of file to overwrite closePVD
217 fout << "<DataSet timestep=\"" << iT << "\" " << "group=\"\" part=\" "
218 << i << "\" " << "file=\"" << namePiece << "\"/>\n";
219 fout.close();
220 closePVD(fullNamePVDMaster);
221 } else {
222 clout << "Error: could not open " << fullNamePVDMaster << std::endl;
223 }
224}
225
226
227template<typename T, typename W>
229 const std::string& fullName )
230{
231 std::ofstream fout(fullName.c_str(), std::ios::app);
232 if (!fout) {
233 clout << "Error: could not open " << fullName << std::endl;
234 }
235
236 for(unsigned long i=0; i<functors.size(); i++){
237 writeFunctor( fullName, fout, *functors[i]);
238
239 }
240 for(unsigned long i=0; i<functorsA.size(); i++){
241 writeAnalyticalFunctor( fullName, fout, *functorsA[i]);
242 }
243
244 fout << "</PointData>" << std::endl;
245 fout << "<CellData /> " << std::endl;
246 fout << "<Cells>" << std::endl;
247 fout << "<DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">"
248 << std::endl;
249 for ( int i=0; i < 1; ++i){
250 for ( unsigned long j=0; j<pos.size(); ++j){
251 fout << j << " ";
252 }
253 }
254 fout << "</DataArray>" << std::endl;
255 fout << "<DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">"
256 << std::endl;
257 for ( int i=0; i < 1; ++i){
258 for ( unsigned long j=1; j <= pos.size(); ++j){
259 fout << j << " ";
260 }
261 }
262 fout << "</DataArray>" << std::endl;
263 fout << "<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">"
264 << std::endl;
265 for ( unsigned long j=0; j < pos.size(); ++j){
266 fout << 1 << " ";
267 }
268 fout << "</DataArray>" << std::endl;
269 fout << "</Cells>" << std::endl;
270 fout << "<Points>" << std::endl;
271 writePosition( fout );
272 fout << "</Points>" << std::endl;
273 fout << "</Piece>" << std::endl;
274
275 fout.close();
276}
277
278
279template<typename T, typename W>
280void VTUpointWriter<T,W,2>::writePosition( std::ofstream& fout ){
281 fout << "<DataArray type=\"Float32\" Name=\"Point\" NumberOfComponents=\""<< 3
282 << "\">" << std::endl;
283
284 int num = pos.size();
285 for ( int j=0; j < num; ++j){
286 fout << pos[j][0] << " "<<pos[j][1] << " 0.0 ";
287 }
288 fout << "</DataArray>" << std::endl;
289}
290
291
292template<typename T, typename W>
293void VTUpointWriter<T,W,2>::writeFunctor( const std::string& fullName, std::ofstream& fout , SuperF2D<T,W>& f ){
294
295 AnalyticalFfromSuperF2D<T> interpolateF( f, true );
296 writeAnalyticalFunctor( fullName, fout , interpolateF );
297
298}
299
300template<typename T, typename W>
301void VTUpointWriter<T,W,2>::writeAnalyticalFunctor( const std::string& fullName, std::ofstream& fout , AnalyticalF2D<T,W>& f ){
302 int dimF = f.getTargetDim();
303 if(!_binary){
304 fout << "<DataArray type=\"Float32\" Name=\""<< f.getName()<<"\" NumberOfComponents=\""<< dimF
305 << "\">" << std::endl;
306 }else if(_binary){
307 fout << "<DataArray type=\"Float32\" Name=\""<< f.getName()<<"\" format=\"binary\" encoding=\"base64\" NumberOfComponents=\""<< dimF
308 << "\">" << std::endl;
309 }
310
311 if(_binary){
312 fout.close();
313 }
314 size_t num = pos.size();
315
316 std::ofstream ofstr(fullName.c_str(),
317 std::ios::out | std::ios::app | std::ios::binary);
318 size_t binarySize = size_t( num*dimF * sizeof(float));
319 Base64Encoder<unsigned int> sizeEncoder(ofstr, 1);
320 unsigned int uintBinarySize = (unsigned int) binarySize;
321 if(_binary){
322 sizeEncoder.encode(&uintBinarySize, 1);
323 }
324 Base64Encoder<float> dataEncoder(ofstr, num);
325 T* val = new T[dimF]();
326 for ( size_t j=0; j < num; ++j){
327 T point[2]= {pos[j][0],pos[j][1]};
328 for (int i = 0; i < dimF; i++){
329 f(val,point);
330 const float helper=float(val[i]);
331 if(!_binary){
332 fout << helper << " ";
333 }
334 else{
335 dataEncoder.encode(&helper, 1);
336 }
337 }
338 }
339 delete[] val;
340
341 ofstr.close();
342 if(_binary){
343 fout.open(fullName.c_str(), std::ios::out | std::ios::app);
344 }
345 fout << "</DataArray>" << std::endl;
346
347}
348
349
350
351
352//same thing as specialization for 3D
353template<typename T, typename W>
355 bool binary )
356 : _name( name ), _binary( binary ),
357 clout(std::cout, "VTUpointWriter3D")
358{}
359
360template<typename T, typename W>
361void VTUpointWriter<T,W,3>::addFunctor( AnalyticalF3D<T,W>& f, const std::string& name ){
362 f.getName() = name;
363 functorsA.push_back(&f);
364}
365
366template<typename T, typename W>
368 functorsA.push_back(&f);
369}
370
371template<typename T, typename W>
372void VTUpointWriter<T,W,3>::addFunctor( SuperF3D<T,W>& f, const std::string& name ){
373 f.getName() = name;
374 functors.push_back(&f);
375}
376
377template<typename T, typename W>
379 functors.push_back(&f);
380}
381
382template<typename T, typename W>
383void VTUpointWriter<T,W,3>::addPoints( std::vector<Vector<T,3>>& new_positions ){
384 for( unsigned long i=0; i<new_positions.size(); i++ ){
385 pos.push_back(new_positions[i]);
386 }
387}
388
389template<typename T, typename W>
391 pos.push_back(new_position);
392}
393
395template<typename T, typename W>
396void VTUpointWriter<T,W,3>::write( std::size_t iT )
397{
398 int rank = 0;
399#ifdef PARALLEL_MODE_MPI
400 rank = singleton::mpi().getRank();
401#endif
402
403 for (SuperF3D<T,W>* f : functors) {
404 f->getSuperStructure().communicate();
405 }
406
407
408 if (rank == 0) {
409 std::string fullNamePVDmaster = singleton::directories().getVtkOutDir()
410 + createFileName(_name) + "_master.pvd";
411 std::string fullNamePVD = singleton::directories().getVtkOutDir() + "data/"
412 + createFileName(_name, iT) + ".pvd";
413 preamblePVD( fullNamePVD ); // timestep
414 std::string namePiece = "data/" + createFileName(_name, iT, 0) + ".vtu";
415 // puts name of .vti piece to a .pvd file [fullNamePVD]
416
417 dataPVD(iT, 1, fullNamePVD, namePiece);
418 // adds a namePiece to master.pvd file.
419 // To do so we overwrite closePVD() and add new entry.
420 dataPVDmaster(iT, 1, fullNamePVDmaster, namePiece);
421 closePVD(fullNamePVD); // timestep
422 } // master only
423 if ( rank == 0){ //master only or different systems each
424 std::string fullNameVTU = singleton::directories().getVtkOutDir()
425 + "data/" + createFileName(_name, iT, rank) + ".vtu";
426 preambleVTU(fullNameVTU, pos.size());
427 //writes data arrays into vtu file
428 this->dataArray( fullNameVTU );
429
430 closeVTU(fullNameVTU);
431}
432}
433
435template<typename T, typename W>
436void VTUpointWriter<T,W,3>::write( std::size_t iT, std::vector<Vector<T,3>>& new_positions )
437{
438 addPoints(new_positions);
439 write(iT);
440}
441
442
443template<typename T, typename W>
445 const std::string& fullName, int num)
446{
447 std::ofstream fout(fullName.c_str(), std::ios::trunc);
448 if (!fout) {
449 clout << "Error: could not open " << fullName << std::endl;
450 }
451 fout << "<?xml version=\"1.0\"?>" << std::endl << std::flush;
452 fout
453 << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">"
454 << std::endl;
455 fout << "<UnstructuredGrid>" << std::endl;
456 fout << "<Piece NumberOfPoints=\"" << num
457 << "\" NumberOfCells=\"" << num << "\">"
458 << std::endl;
459 fout << "<PointData Vectors=\"Particles\">" << std::endl;
460 fout.close();
461}
462
463template<typename T, typename W>
465 const std::string& fullNamePiece)
466{
467 std::ofstream fout(fullNamePiece.c_str(), std::ios::app);
468 if (!fout) {
469 clout << "Error: could not open " << fullNamePiece << std::endl;
470 }
471 fout << "</UnstructuredGrid>\n";
472 fout << "</VTKFile>\n";
473 fout.close();
474}
475
476
477template<typename T, typename W>
479 const std::string& fullNamePVD, const std::string& namePiece)
480{
481 std::ofstream fout(fullNamePVD.c_str(), std::ios::app);
482 if (!fout) {
483 clout << "Error: could not open " << fullNamePVD << std::endl;
484 }
485 fout << "<DataSet timestep=\"" << iT << "\" " << "group=\"\" part=\" " << i
486 << "\" " << "file=\"" << namePiece << "\"/>\n";
487 fout.close();
488}
489
490template<typename T, typename W>
492 const std::string& fullNamePVDMaster, const std::string& namePiece)
493{
494 std::ofstream fout(fullNamePVDMaster.c_str(),
495 std::ios::in | std::ios::out | std::ios::ate);
496 if (fout) {
497 fout.seekp(-25, std::ios::end); // jump -25 form the end of file to overwrite closePVD
498 fout << "<DataSet timestep=\"" << iT << "\" " << "group=\"\" part=\" "
499 << i << "\" " << "file=\"" << namePiece << "\"/>\n";
500 fout.close();
501 closePVD(fullNamePVDMaster);
502 } else {
503 clout << "Error: could not open " << fullNamePVDMaster << std::endl;
504 }
505}
506
507template<typename T, typename W>
509{
510 std::string fullNamePVDmaster = singleton::directories().getVtkOutDir()
511 + createFileName(_name) + "_master.pvd";
512 preamblePVD(fullNamePVDmaster);
513 closePVD(fullNamePVDmaster);
514
515}
516
517
518template<typename T, typename W>
520 const std::string& fullNamePVD)
521{
522 std::ofstream fout(fullNamePVD.c_str(), std::ios::trunc);
523 if (!fout) {
524 clout << "Error: could not open " << fullNamePVD << std::endl;
525 }
526 fout << "<?xml version=\"1.0\"?>\n";
527 fout << "<VTKFile type=\"Collection\" version=\"0.1\" "
528 << "byte_order=\"LittleEndian\">\n" << "<Collection>\n";
529 fout.close();
530}
531
532template<typename T, typename W>
534 const std::string& fullNamePVD)
535{
536 std::ofstream fout(fullNamePVD.c_str(), std::ios::app);
537 if (!fout) {
538 clout << "Error: could not open " << fullNamePVD << std::endl;
539 }
540 fout << "</Collection>\n";
541 fout << "</VTKFile>\n";
542 fout.close();
543}
544
545template<typename T, typename W>
547 const std::string& fullName )
548{
549 std::ofstream fout(fullName.c_str(), std::ios::app);
550 if (!fout) {
551 clout << "Error: could not open " << fullName << std::endl;
552 }
553
554 for(unsigned long i=0; i<functors.size(); i++){
555 writeFunctor( fullName, fout, *functors[i]);
556 }
557 for(unsigned long i=0; i<functorsA.size(); i++){
558 writeAnalyticalFunctor( fullName, fout, *functorsA[i]);
559 }
560 fout << "</PointData>" << std::endl;
561 fout << "<CellData /> " << std::endl;
562 fout << "<Cells>" << std::endl;
563 fout << "<DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">"
564 << std::endl;
565 for ( size_t i=0; i < 1; ++i){
566 for ( unsigned long j=0; j<pos.size(); ++j){
567 fout << j << " ";
568 }
569 }
570 fout << "</DataArray>" << std::endl;
571 fout << "<DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">"
572 << std::endl;
573 for ( size_t i=0; i < 1; ++i){
574 for ( unsigned long j=1; j <= pos.size(); ++j){
575 fout << j << " ";
576 }
577 }
578 fout << "</DataArray>" << std::endl;
579 fout << "<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">"
580 << std::endl;
581 for ( unsigned long j=0; j < pos.size(); ++j){
582 fout << 1 << " ";
583 }
584 fout << "</DataArray>" << std::endl;
585 fout << "</Cells>" << std::endl;
586 fout << "<Points>" << std::endl;
587 writePosition( fout );
588 fout << "</Points>" << std::endl;
589 fout << "</Piece>" << std::endl;
590
591 fout.close();
592}
593
594template<typename T, typename W>
595void VTUpointWriter<T,W,3>::writePosition( std::ofstream& fout ){
596 fout << "<DataArray type=\"Float32\" Name=\"Point\" NumberOfComponents=\""<< 3
597 << "\">" << std::endl;
598
599 int num = pos.size();
600 for ( int j=0; j < num; ++j){
601 fout << pos[j][0] << " "<< pos[j][1] << " " << pos[j][2] << " ";
602 }
603 fout << "</DataArray>" << std::endl;
604}
605
606
607template<typename T, typename W>
608void VTUpointWriter<T,W,3>::writeFunctor( const std::string& fullName, std::ofstream& fout , SuperF3D<T,W>& f ){
609 AnalyticalFfromSuperF3D<T> interpolateF( f, true );
610 writeAnalyticalFunctor( fullName, fout , interpolateF );
611}
612
613
614template<typename T, typename W>
615void VTUpointWriter<T,W,3>::writeAnalyticalFunctor( const std::string& fullName, std::ofstream& fout , AnalyticalF3D<T,W>& f ){
616 int dimF = f.getTargetDim();
617 if(!_binary){
618 fout << "<DataArray type=\"Float32\" Name=\""<< f.getName()<<"\" NumberOfComponents=\""<< dimF
619 << "\">" << std::endl;
620 }else if(_binary){
621 fout << "<DataArray type=\"Float32\" Name=\""<< f.getName()<<"\" format=\"binary\" encoding=\"base64\" NumberOfComponents=\""<< dimF
622 << "\">" << std::endl;
623 }
624
625 if(_binary){
626 fout.close();
627 }
628 size_t num = pos.size();
629 std::ofstream ofstr(fullName.c_str(),
630 std::ios::out | std::ios::app | std::ios::binary);
631 size_t binarySize = size_t( num*dimF * sizeof(float));
632 Base64Encoder<unsigned int> sizeEncoder(ofstr, 1);
633 unsigned int uintBinarySize = (unsigned int) binarySize;
634 if(_binary){
635 sizeEncoder.encode(&uintBinarySize, 1);
636 }
637
638 Base64Encoder<float> dataEncoder(ofstr, num);
639 T* val = new T[dimF]();
640 for ( size_t j=0; j < num; ++j){
641 T point[3]= {pos[j][0],pos[j][1],pos[j][2]};
642 for (int i = 0; i < dimF; i++){
643 f(val,point);
644 if(!_binary){
645 fout << val[i] << " ";
646 }
647 else{
648 const float helper=float(val[i]);
649 dataEncoder.encode(&helper, 1);
650 }
651 }
652 }
653 delete[] val;
654 ofstr.close();
655 if(_binary){
656 fout.open(fullName.c_str(), std::ios::out | std::ios::app);
657 }
658 fout << "</DataArray>" << std::endl;
659
660}
661
662
663}
664
665#endif
AnalyticalF are applications from DD to XD, where X is set by the constructor.
Converts super functions to analytical functions.
Converts super functors to analytical functors.
void encode(const T *data, size_t length)
Definition base64.hh:59
int getTargetDim() const
read only access to member variable _n
Definition genericF.hh:45
std::string & getName()
read and write access to name
Definition genericF.hh:51
represents all functors that operate on a SuperStructure<T,2> in general
Definition aliases.h:183
represents all functors that operate on a SuperStructure<T,3> in general
Definition aliases.h:184
void write(std::size_t iT, std::vector< Vector< T, 2 > > &new_positions)
write function to call during runtime, also accepts additional points during call
void write(std::size_t iT, std::vector< Vector< T, 3 > > &new_positions)
write function to call during runtime, also accepts additional points during call
Plain old scalar vector.
std::string getVtkOutDir() const
Definition singleton.h:103
int getRank() const
Returns the process ID.
void write(BlockReduction3D2D< T > &blockReduction, int iT, const plotParam< T > param={}, const std::vector< T > &valueArea=std::vector< T >{})
This function is used to plot heat maps as jpeg files.
MpiManager & mpi()
Directories & directories()
Definition singleton.h:162
Top level namespace for all of OpenLB.
std::string createFileName(std::string name)
for .pvd masterFile
Definition fileName.hh:34