VTKMeshWriter.h 14.1 KB
Newer Older
Christian Godenschwager's avatar
Christian Godenschwager committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//======================================================================================================================
//
//  This file is part of waLBerla. waLBerla is free software: you can
//  redistribute it and/or modify it under the terms of the GNU General Public
//  License as published by the Free Software Foundation, either version 3 of
//  the License, or (at your option) any later version.
//
//  waLBerla is distributed in the hope that it will be useful, but WITHOUT
//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
//  for more details.
//
//  You should have received a copy of the GNU General Public License along
//  with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file VTKMeshWriter.h
//! \ingroup mesh
//! \author Christian Godenschwager <christian.godenschwager@fau.de>
//
//======================================================================================================================

#pragma once

24
#include "mesh_common/MeshOperations.h"
Christian Godenschwager's avatar
Christian Godenschwager committed
25
26
27
28
29
30
31

#include "core/math/Vector3.h"
#include "core/mpi/MPIManager.h"

#include "vtk/Base64Writer.h"
#include "vtk/UtilityFunctions.h"

Michael Kuron's avatar
Michael Kuron committed
32
#include "core/Filesystem.h"
Christian Godenschwager's avatar
Christian Godenschwager committed
33
34
35
36
37
38
39
40
41
42
43
44

#include <iostream>
#include <vector>
#include <set>
#include <string>

namespace walberla {
namespace mesh {

template< typename MeshType >
class VTKMeshWriter {
public:
Michael Kuron's avatar
Michael Kuron committed
45
   typedef std::function<bool ( const MeshType &, const typename MeshType::FaceHandle & )> FaceFilterFunction;
Christian Godenschwager's avatar
Christian Godenschwager committed
46
47
48
49
50
51
52
53
54

   typedef std::vector< typename MeshType::FaceHandle >   Faces;
   typedef std::vector< typename MeshType::VertexHandle > Vertices;

   template< typename T >
   class DataSource
   {
   public:
      DataSource( const std::string & _name ) : name_( _name ) { }
55
      virtual ~DataSource() = default;
Christian Godenschwager's avatar
Christian Godenschwager committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
      typedef T value_type;
      virtual uint_t      numComponents() = 0;
      const std::string & name() { return name_; }
   protected:
      std::string name_;
   };

   template< typename T >
   class VertexDataSource : public DataSource<T>
   {
   public:
      typedef typename DataSource<T>::value_type value_type;
      typedef typename VTKMeshWriter::Vertices Vertices;

      VertexDataSource( const std::string & _name ) : DataSource<T>( _name ) { }
71
      virtual ~VertexDataSource() = default;
Christian Godenschwager's avatar
Christian Godenschwager committed
72
73
74
75
76
77
78
79
80
81
82
      virtual void getData( const MeshType &, const Vertices & vertices, std::vector<T> & ) = 0;
   };

   template< typename T >
   class FaceDataSource : public DataSource<T>
   {
   public:
      typedef typename DataSource<T>::value_type value_type;
      typedef typename VTKMeshWriter::Faces Faces;

      FaceDataSource( const std::string & _name ) : DataSource<T>( _name ) { }
83
      virtual ~FaceDataSource() = default;
Christian Godenschwager's avatar
Christian Godenschwager committed
84
85
86
87
88
89
90
91
92
93
94
95
      virtual void getData( const MeshType &, const Faces & faces, std::vector<T> & ) = 0;
   };

   VTKMeshWriter( const shared_ptr<const MeshType> & mesh, const std::string & identifier,
                  const uint_t writeFrequency, const std::string & baseFolder = "vtk_out" );

   void operator()();

   inline void addDataSource( const shared_ptr< VertexDataSource<float>   > & dataSource ) { floatVertexDataSources_.push_back( dataSource );  }
   inline void addDataSource( const shared_ptr< VertexDataSource<double>  > & dataSource ) { doubleVertexDataSources_.push_back( dataSource ); }
   inline void addDataSource( const shared_ptr< VertexDataSource<int32_t> > & dataSource ) { int32VertexDataSources_.push_back( dataSource );  }
   inline void addDataSource( const shared_ptr< VertexDataSource<uint8_t> > & dataSource ) { uint8VertexDataSources_.push_back( dataSource );  }
96
   inline void addDataSource( const shared_ptr< VertexDataSource<uint64_t> > & dataSource ) { uint64VertexDataSources_.push_back( dataSource );  }
Christian Godenschwager's avatar
Christian Godenschwager committed
97
98
99
100
101

   inline void addDataSource( const shared_ptr< FaceDataSource<float>   > & dataSource ) { floatFaceDataSources_.push_back( dataSource );  }
   inline void addDataSource( const shared_ptr< FaceDataSource<double>  > & dataSource ) { doubleFaceDataSources_.push_back( dataSource ); }
   inline void addDataSource( const shared_ptr< FaceDataSource<int32_t> > & dataSource ) { int32FaceDataSources_.push_back( dataSource );  }
   inline void addDataSource( const shared_ptr< FaceDataSource<uint8_t> > & dataSource ) { uint8FaceDataSources_.push_back( dataSource );  }
102
   inline void addDataSource( const shared_ptr< FaceDataSource<uint64_t> > & dataSource ) { uint64FaceDataSources_.push_back( dataSource );  }
Christian Godenschwager's avatar
Christian Godenschwager committed
103
104
105

   inline void setFaceFilter  ( const FaceFilterFunction & f ) { faceFilter_ = f;                    }
   inline void clearFaceFilter()                               { faceFilter_ = FaceFilterFunction(); }
Michael Kuron's avatar
Michael Kuron committed
106
   inline bool isFaceFilterSet()                         const { return static_cast<bool>(faceFilter_);        }
Christian Godenschwager's avatar
Christian Godenschwager committed
107
108

   void incrementTimeStep()      { ++timestep_; }
109
   bool isWriteScheduled() const { return writeFrequency_ > 0 && timestep_ % writeFrequency_ == 0; }
Christian Godenschwager's avatar
Christian Godenschwager committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

protected:
   void write( std::ostream & os ) const;
   void writePrefix( std::ostream & os ) const;
   void writePostfix( std::ostream & os ) const;
   void writePiece( std::ostream & os ) const;

   template<typename T>
   inline void writeVertexData( const T & vertexDataSources, const Vertices & vertices, std::ostream & os, vtk::Base64Writer & b64 ) const;

   template<typename T>
   inline void writeFaceData( const T & faceDataSources, const Faces & faces, std::ostream & os, vtk::Base64Writer & b64 ) const;

   void writePVD( std::ostream & os ) const;

   shared_ptr<const MeshType> mesh_;

   uint_t      writeFrequency_;
   std::string identifier_;
   std::string baseFolder_;

   uint_t      timestep_;

   FaceFilterFunction faceFilter_;

135
136
137
138
139
140
141
142
143
144
145
   std::vector< shared_ptr< VertexDataSource<float  > > >  floatVertexDataSources_;
   std::vector< shared_ptr< VertexDataSource<double > > >  doubleVertexDataSources_;
   std::vector< shared_ptr< VertexDataSource<int32_t> > >  int32VertexDataSources_;
   std::vector< shared_ptr< VertexDataSource<uint8_t> > >  uint8VertexDataSources_;
   std::vector< shared_ptr< VertexDataSource<uint64_t> > > uint64VertexDataSources_;

   std::vector< shared_ptr< FaceDataSource<float  > > >  floatFaceDataSources_;
   std::vector< shared_ptr< FaceDataSource<double > > >  doubleFaceDataSources_;
   std::vector< shared_ptr< FaceDataSource<int32_t> > >  int32FaceDataSources_;
   std::vector< shared_ptr< FaceDataSource<uint8_t> > >  uint8FaceDataSources_;
   std::vector< shared_ptr< FaceDataSource<uint64_t> > > uint64FaceDataSources_;
Christian Godenschwager's avatar
Christian Godenschwager committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
};


template< typename MeshType >
template< typename T >
void VTKMeshWriter<MeshType>::writeVertexData( const T & vertexDataSources, const Vertices & vertices, std::ostream & os, vtk::Base64Writer & b64 ) const
{
   typedef typename T::value_type::element_type::value_type value_type;

   for( auto dsIt = vertexDataSources.begin(); dsIt != vertexDataSources.end(); ++dsIt )
   {
      std::vector< value_type > data;
      (*dsIt)->getData( *mesh_, vertices, data );

      WALBERLA_CHECK_EQUAL( vertices.size() * (*dsIt)->numComponents(), data.size(), "The vertex data source \"" << (*dsIt)->name() << "\" wrote the wrong amount of data!" );

      os << "        <DataArray type=\"" << vtk::typeToString<value_type>() << "\" Name=\"" << (*dsIt)->name() << "\" NumberOfComponents=\"" << (*dsIt)->numComponents() << "\" format=\"binary\">\n";
      os << "          ";
      for( auto it = data.begin(); it != data.end(); ++it )
         b64 << *it;
      b64.toStream( os );
      os << "        </DataArray>\n";
   }
}


template< typename MeshType >
template< typename T >
void VTKMeshWriter<MeshType>::writeFaceData( const T & faceDataSources, const Faces & faces, std::ostream & os, vtk::Base64Writer & b64 ) const
{
   typedef typename T::value_type::element_type::value_type value_type;

   for( auto dsIt = faceDataSources.begin(); dsIt != faceDataSources.end(); ++dsIt )
   {
      std::vector< value_type > data;
      (*dsIt)->getData( *mesh_, faces, data );

      WALBERLA_CHECK_EQUAL( faces.size() * (*dsIt)->numComponents(), data.size(), "The face data source \"" << (*dsIt)->name() << "\" wrote the wrong amount of data!" );

      os << "        <DataArray type=\"" << vtk::typeToString<value_type>() << "\" Name=\"" << (*dsIt)->name() << "\" NumberOfComponents=\"" << (*dsIt)->numComponents() << "\" format=\"binary\">\n";
      os << "          ";
      for( auto it = data.begin(); it != data.end(); ++it )
         b64 << *it;
      b64.toStream( os );
      os << "        </DataArray>\n";
   }
}


template< typename MeshType >
VTKMeshWriter<MeshType>::VTKMeshWriter( const shared_ptr<const MeshType> & mesh, const std::string & identifier, const uint_t writeFrequency,
   const std::string & baseFolder )
   : mesh_( mesh ), writeFrequency_( writeFrequency ), identifier_( identifier ), baseFolder_( baseFolder ), timestep_( 0 )
{
   WALBERLA_ROOT_SECTION()
   {
      std::ostringstream folder;
      folder << baseFolder_ << '/' << identifier_;
Michael Kuron's avatar
Michael Kuron committed
204
205
      if( filesystem::exists( folder.str() ) )
         filesystem::remove_all( folder.str() );
Christian Godenschwager's avatar
Christian Godenschwager committed
206
207
208

      std::ostringstream pvdFile;
      pvdFile << baseFolder_ << '/' << identifier_ << ".pvd";
Michael Kuron's avatar
Michael Kuron committed
209
210
      if( filesystem::exists( pvdFile.str() ) )
         filesystem::remove( pvdFile.str() );
Christian Godenschwager's avatar
Christian Godenschwager committed
211

Michael Kuron's avatar
Michael Kuron committed
212
      filesystem::create_directories( folder.str() );
Christian Godenschwager's avatar
Christian Godenschwager committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
   }
   WALBERLA_MPI_BARRIER();
}


template< typename MeshType >
void VTKMeshWriter<MeshType>::writePrefix( std::ostream & os ) const
{
   os << "<VTKFile type=\"PolyData\" version=\"0.1\">\n"
      << "  <PolyData>\n";
}

template< typename MeshType >
void VTKMeshWriter<MeshType>::writePostfix( std::ostream & os ) const
{
   os << "  </PolyData>\n"
      << "</VTKFile>\n";
}

template< typename MeshType >
void VTKMeshWriter<MeshType>::writePiece( std::ostream & os ) const
{
   vtk::Base64Writer b64;

   Faces faces;
   Vertices vertices;

   if( isFaceFilterSet() )
   {
      for( auto it = mesh_->faces_sbegin(); it != mesh_->faces_end(); ++it )
         if( faceFilter_( *mesh_, *it ) )
            faces.push_back( *it );

      std::vector< typename MeshType::VertexHandle > tmpVertices = findConnectedVertices( *mesh_, faces.begin(), faces.end() );
      vertices.assign( tmpVertices.begin(), tmpVertices.end() );
   }
   else
   {
      std::copy( mesh_->faces_sbegin()   , mesh_->faces_end()   , std::back_inserter( faces )    );
      std::copy( mesh_->vertices_sbegin(), mesh_->vertices_end(), std::back_inserter( vertices ) );
   }

   const uint_t numVertices  = vertices.size();
   const uint_t numFaces     = faces.size();

   os << "    <Piece NumberOfPoints=\"" << numVertices << "\" NumberOfPolys=\"" << numFaces << "\">\n"
      << "      <Points>\n"
      << "        <DataArray type=\"" << vtk::typeToString<typename MeshType::Point::value_type>() << "\" NumberOfComponents=\"3\" format=\"binary\">\n";

   os << "          ";
   std::map<typename MeshType::VertexHandle, int > vertexIndizes;
   int32_t vertexCounter = 0;
   for( auto it = vertices.begin(); it != vertices.end(); ++it )
   {
      const auto & p = mesh_->point( *it );
      b64 << p[0] << p[1] << p[2];
      vertexIndizes[ *it ] = vertexCounter++;
   }
   b64.toStream( os );

   os << "        </DataArray>\n"
      << "      </Points>\n";

   os << "      <Polys>\n";

   os << "        <DataArray type=\"Int32\" Name=\"connectivity\" format=\"binary\">\n";
   os << "          ";
   std::vector<int32_t> offsets;
   offsets.reserve( numFaces );
   int32_t currentOffset = 0;
   for( auto it = faces.begin(); it != faces.end(); ++it )
   {
      int32_t nv = 0;
      for( auto v_it = mesh_->cfv_ccwbegin( *it ); v_it != mesh_->cfv_ccwend( *it ); ++v_it, ++nv )
      {
         b64 << vertexIndizes[ *v_it ];
      }

      currentOffset += nv;
      offsets.push_back( currentOffset );

   }
   b64.toStream( os );

   os << "        </DataArray>\n";

   os << "        <DataArray type=\"Int32\" Name=\"offsets\" format=\"binary\">\n";
   os << "          ";
   for( auto it = offsets.begin(); it != offsets.end(); ++it )
   {
      b64 << *it;
   }
   b64.toStream( os );

   os << "        </DataArray>\n";

   os << "      </Polys>\n";

   os << "      <CellData>\n";

313
314
315
316
317
   writeFaceData( floatFaceDataSources_ ,  faces, os, b64 );
   writeFaceData( doubleFaceDataSources_,  faces, os, b64 );
   writeFaceData( int32FaceDataSources_ ,  faces, os, b64 );
   writeFaceData( uint8FaceDataSources_ ,  faces, os, b64 );
   writeFaceData( uint64FaceDataSources_ , faces, os, b64 );
Christian Godenschwager's avatar
Christian Godenschwager committed
318
319
320
321
322

   os << "      </CellData>\n";

   os << "      <PointData>\n";

323
324
325
326
327
   writeVertexData( floatVertexDataSources_ ,  vertices, os, b64 );
   writeVertexData( doubleVertexDataSources_,  vertices, os, b64 );
   writeVertexData( int32VertexDataSources_ ,  vertices, os, b64 );
   writeVertexData( uint8VertexDataSources_ ,  vertices, os, b64 );
   writeVertexData( uint64VertexDataSources_ , vertices, os, b64 );
Christian Godenschwager's avatar
Christian Godenschwager committed
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387

   os << "      </PointData>\n";


   os << "    </Piece>\n";
}

template< typename MeshType >
void VTKMeshWriter<MeshType>::write( std::ostream & os ) const
{
   writePrefix( os );
   writePiece( os );
   writePostfix( os );
}


template< typename MeshType >
inline void VTKMeshWriter<MeshType>::writePVD( std::ostream & os ) const
{
   os << "<VTKFile type=\"Collection\" version=\"0.1\">\n"
      << "  <Collection>\n";

   for( uint_t timestep = 0; timestep <= timestep_; timestep += writeFrequency_ )
   {
      os << "    <DataSet timestep=\"" << timestep << "\" "
         "file=\"" << identifier_ << '/' << identifier_ << '_' << timestep << ".vtp" << "\"/>\n";
   }

   os << "  </Collection>\n"
      << "</VTKFile>\n";
}


template< typename MeshType >
void VTKMeshWriter<MeshType>::operator()()
{
   if( isWriteScheduled() )
   {
      WALBERLA_ROOT_SECTION()
      {
         std::ostringstream filePathVtp;
         filePathVtp << baseFolder_ << '/' << identifier_ << '/' << identifier_ << '_' << timestep_ << ".vtp";
      
         std::ofstream ofsVtp;
         ofsVtp.open( filePathVtp.str().c_str() );
  
         write( ofsVtp );

         std::ostringstream filePathPvd;
         filePathPvd << baseFolder_ << '/' << identifier_ << ".pvd";
         std::ofstream ofsPvd( filePathPvd.str().c_str() );
         writePVD( ofsPvd );
      }
   }

   incrementTimeStep();
}


} // namespace mesh
388
} // namespace walberla