Commit 14bb4359 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Added VTUWriter (for now ASCII format only)

parent 8f492406
Loading
Loading
Loading
Loading
+99 −0
Original line number Diff line number Diff line
/***************************************************************************
                          VTUWriter.h  -  description
                             -------------------
    begin                : Mar 18, 2020
    copyright            : (C) 2020 by Tomas Oberhuber et al.
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

// Implemented by: Jakub Klinkovský

#pragma once

#include <TNL/Meshes/Grid.h>
#include <TNL/Meshes/Mesh.h>
#include <TNL/Meshes/VTKTraits.h>

namespace TNL {
namespace Meshes {
namespace Writers {

namespace details {

template< typename Mesh, int EntityDimension > struct MeshEntitiesVTUCollector;

} // namespace details

template< typename Mesh >
class VTUWriter
{
   static_assert( Mesh::getMeshDimension() <= 3, "The VTK format supports only 1D, 2D and 3D meshes." );
   // TODO: check also world dimension when grids allow it
//   static_assert( Mesh::getWorldDimension() <= 3, "The VTK format supports only 1D, 2D and 3D meshes." );

   template< int EntityDimension >
   using EntitiesCollector = details::MeshEntitiesVTUCollector< Mesh, EntityDimension >;

   using HeaderType = std::uint64_t;
public:
   using MeshRealType = typename Mesh::RealType;
   using IndexType = typename Mesh::GlobalIndexType;

   VTUWriter() = delete;

   VTUWriter( std::ostream& str, VTK::FileFormat format = VTK::FileFormat::ascii )
   : str(str), format(format)
   {}

   template< int EntityDimension = Mesh::getMeshDimension() >
   void writeEntities( const Mesh& mesh );

   // If desired, cycle and time of the simulation can put into the file. This follows the instructions at
   // http://www.visitusers.org/index.php?title=Time_and_Cycle_in_VTK_files
   void writeMetadata( std::int32_t cycle = -1, double time = -1 );

   template< typename Array >
   void writeDataArray( const Array& array,
                        const String& name,
                        const int numberOfComponents = 1 );

   ~VTUWriter();

protected:
   void writeHeader( const Mesh& mesh );

   void writeFooter();

   void writePoints( const Mesh& mesh );

   std::ostream& str;

   VTK::FileFormat format;

   // number of points written to the file
   IndexType pointsCount = 0;

   // number of cells (in the VTK sense) written to the file
   IndexType cellsCount = 0;

   // indicator if the <VTKFile> tag is open
   bool vtkfileOpen = false;

   // indicator if a <Piece> tag is open
   bool pieceOpen = false;

   // number of data arrays written in each section
   int cellDataArrays = 0;
   int pointDataArrays = 0;

   // indicator of the current section
   VTK::DataType currentSection = VTK::DataType::CellData;
};

} // namespace Writers
} // namespace Meshes
} // namespace TNL

#include <TNL/Meshes/Writers/VTUWriter.hpp>
+518 −0

File added.

Preview size limit exceeded, changes collapsed.

+8 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@
#include <TNL/Config/parseCommandLine.h>
#include <TNL/Meshes/TypeResolver/TypeResolver.h>
#include <TNL/Meshes/Writers/VTKWriter.h>
#include <TNL/Meshes/Writers/VTUWriter.h>
#include <TNL/Meshes/Writers/NetgenWriter.h>

using namespace TNL;
@@ -92,6 +93,12 @@ struct MeshConverter
         VTKWriter writer( file );
         writer.template writeEntities< Mesh::getMeshDimension() >( mesh );
      }
      else if( outputFormat == "vtu" ) {
         using VTKWriter = Meshes::Writers::VTUWriter< Mesh >;
         std::ofstream file( outputFileName.getString() );
         VTKWriter writer( file );
         writer.template writeEntities< Mesh::getMeshDimension() >( mesh );
      }
      // FIXME: NetgenWriter is not specialized for grids
//      else if( outputFormat == "netgen" ) {
//         using NetgenWriter = Meshes::Writers::NetgenWriter< Mesh >;
@@ -111,6 +118,7 @@ void configSetup( Config::ConfigDescription& config )
   config.addEntry< String >( "output-format", "Output mesh file format." );
   config.addEntryEnum( "tnl" );
   config.addEntryEnum( "vtk" );
   config.addEntryEnum( "vtu" );
//   config.addEntryEnum( "netgen" );
}