From 24a38646ece851fa04e1741fa6daa1fb6e9aef11 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jakub=20Klinkovsk=C3=BD?= <klinkjak@fjfi.cvut.cz>
Date: Sat, 21 Oct 2017 20:19:40 +0200
Subject: [PATCH] Refactoring MeshFunctionGnuplotWriter

---
 src/TNL/Functions/CMakeLists.txt              |   1 -
 src/TNL/Functions/MeshFunctionGnuplotWriter.h | 224 +++--------
 .../MeshFunctionGnuplotWriter_impl.h          | 363 ------------------
 3 files changed, 61 insertions(+), 527 deletions(-)
 delete mode 100644 src/TNL/Functions/MeshFunctionGnuplotWriter_impl.h

diff --git a/src/TNL/Functions/CMakeLists.txt b/src/TNL/Functions/CMakeLists.txt
index 806f716635..92359a15bf 100644
--- a/src/TNL/Functions/CMakeLists.txt
+++ b/src/TNL/Functions/CMakeLists.txt
@@ -8,7 +8,6 @@ SET( headers Domain.h
              MeshFunctionEvaluator.h
              MeshFunctionEvaluator_impl.h
              MeshFunctionGnuplotWriter.h
-             MeshFunctionGnuplotWriter_impl.h
              MeshFunctionNormGetter.h
              MeshFunctionVTKWriter.h
              OperatorFunction.h
diff --git a/src/TNL/Functions/MeshFunctionGnuplotWriter.h b/src/TNL/Functions/MeshFunctionGnuplotWriter.h
index 2c21a10328..4b9b6d085f 100644
--- a/src/TNL/Functions/MeshFunctionGnuplotWriter.h
+++ b/src/TNL/Functions/MeshFunctionGnuplotWriter.h
@@ -13,175 +13,73 @@
 #include <TNL/Meshes/Grid.h>
 
 namespace TNL {
-namespace Functions {
 
-template< typename, int, typename > class MeshFunction;
+namespace Meshes {
+   template< typename, typename, typename > class MeshEntity;
+}
+
+namespace Functions {
 
 template< typename MeshFunction >
 class MeshFunctionGnuplotWriter
 {
-   public:
-
-      static bool write( const MeshFunction& function,
-                         std::ostream& str,
-                         const double& scale );
-};
-
-/***
- * 1D grids cells
- */
-template< typename MeshReal,
-          typename Device,
-          typename MeshIndex,
-          typename Real >
-class MeshFunctionGnuplotWriter< MeshFunction< Meshes::Grid< 1, MeshReal, Device, MeshIndex >, 1, Real > >
-{
-   public:
-      typedef Meshes::Grid< 1, MeshReal, Device, MeshIndex > MeshType;
-      typedef Real RealType;
-      typedef Functions::MeshFunction< MeshType, 1, RealType > MeshFunctionType;
-
-      static bool write( const MeshFunctionType& function,
-                         std::ostream& str,
-                         const double& scale );
-};
-
-/***
- * 1D grids vertices
- */
-template< typename MeshReal,
-          typename Device,
-          typename MeshIndex,
-          typename Real >
-class MeshFunctionGnuplotWriter< MeshFunction< Meshes::Grid< 1, MeshReal, Device, MeshIndex >, 0, Real > >
-{
-   public:
-      typedef Meshes::Grid< 1, MeshReal, Device, MeshIndex > MeshType;
-      typedef Real RealType;
-      typedef Functions::MeshFunction< MeshType, 0, RealType > MeshFunctionType;
-
-      static bool write( const MeshFunctionType& function,
-                         std::ostream& str,
-                         const double& scale );
-};
-
-
-/***
- * 2D grids cells
- */
-template< typename MeshReal,
-          typename Device,
-          typename MeshIndex,
-          typename Real >
-class MeshFunctionGnuplotWriter< MeshFunction< Meshes::Grid< 2, MeshReal, Device, MeshIndex >, 2, Real > >
-{
-   public:
-      typedef Meshes::Grid< 2, MeshReal, Device, MeshIndex > MeshType;
-      typedef Real RealType;
-      typedef Functions::MeshFunction< MeshType, 2, RealType > MeshFunctionType;
-
-      static bool write( const MeshFunctionType& function,
-                         std::ostream& str,
-                         const double& scale );
-};
-
-/***
- * 2D grids faces
- */
-template< typename MeshReal,
-          typename Device,
-          typename MeshIndex,
-          typename Real >
-class MeshFunctionGnuplotWriter< MeshFunction< Meshes::Grid< 2, MeshReal, Device, MeshIndex >, 1, Real > >
-{
-   public:
-      typedef Meshes::Grid< 2, MeshReal, Device, MeshIndex > MeshType;
-      typedef Real RealType;
-      typedef Functions::MeshFunction< MeshType, 1, RealType > MeshFunctionType;
-
-      static bool write( const MeshFunctionType& function,
-                         std::ostream& str,
-                         const double& scale );
-};
-
-/***
- * 2D grids vertices
- */
-template< typename MeshReal,
-          typename Device,
-          typename MeshIndex,
-          typename Real >
-class MeshFunctionGnuplotWriter< MeshFunction< Meshes::Grid< 2, MeshReal, Device, MeshIndex >, 0, Real > >
-{
-   public:
-      typedef Meshes::Grid< 2, MeshReal, Device, MeshIndex > MeshType;
-      typedef Real RealType;
-      typedef Functions::MeshFunction< MeshType, 0, RealType > MeshFunctionType;
-
-      static bool write( const MeshFunctionType& function,
-                         std::ostream& str,
-                         const double& scale );
-};
-
-
-/***
- * 3D grids cells
- */
-template< typename MeshReal,
-          typename Device,
-          typename MeshIndex,
-          typename Real >
-class MeshFunctionGnuplotWriter< MeshFunction< Meshes::Grid< 3, MeshReal, Device, MeshIndex >, 3, Real > >
-{
-   public:
-      typedef Meshes::Grid< 3, MeshReal, Device, MeshIndex > MeshType;
-      typedef Real RealType;
-      typedef Functions::MeshFunction< MeshType, 3, RealType > MeshFunctionType;
-
-      static bool write( const MeshFunctionType& function,
-                         std::ostream& str,
-                         const double& scale );
-};
-
-/***
- * 3D grids faces
- */
-template< typename MeshReal,
-          typename Device,
-          typename MeshIndex,
-          typename Real >
-class MeshFunctionGnuplotWriter< MeshFunction< Meshes::Grid< 3, MeshReal, Device, MeshIndex >, 2, Real > >
-{
-   public:
-      typedef Meshes::Grid< 3, MeshReal, Device, MeshIndex > MeshType;
-      typedef Real RealType;
-      typedef Functions::MeshFunction< MeshType, 2, RealType > MeshFunctionType;
-
-      static bool write( const MeshFunctionType& function,
-                         std::ostream& str,
-                         const double& scale );
-};
-
-/***
- * 3D grids vertices
- */
-template< typename MeshReal,
-          typename Device,
-          typename MeshIndex,
-          typename Real >
-class MeshFunctionGnuplotWriter< MeshFunction< Meshes::Grid< 3, MeshReal, Device, MeshIndex >, 0, Real > >
-{
-   public:
-      typedef Meshes::Grid< 3, MeshReal, Device, MeshIndex > MeshType;
-      typedef Real RealType;
-      typedef Functions::MeshFunction< MeshType, 0, RealType > MeshFunctionType;
-
-      static bool write( const MeshFunctionType& function,
-                         std::ostream& str,
-                         const double& scale );
+   using MeshType = typename MeshFunction::MeshType;
+   using EntityType = typename MeshType::template EntityType< MeshFunction::getEntitiesDimension() >;
+   using GlobalIndex = typename MeshType::GlobalIndexType;
+
+   template< typename Entity, int dim = Entity::getEntityDimension() >
+   struct center
+   {
+      static auto get( const Entity& entity ) -> decltype(entity.getCenter())
+      {
+         return entity.getCenter();
+      }
+   };
+
+   template< typename Entity >
+   struct center< Entity, 0 >
+   {
+      static auto get( const Entity& entity ) -> decltype(entity.getPoint())
+      {
+         return entity.getPoint();
+      }
+   };
+
+   template< typename MeshConfig, typename Device, typename Topology, int dim >
+   struct center< TNL::Meshes::MeshEntity< MeshConfig, Device, Topology >, dim >
+   {
+      static int get( const TNL::Meshes::MeshEntity< MeshConfig, Device, Topology >& entity )
+      {
+         throw "not implemented";
+      }
+   };
+
+   template< typename MeshConfig, typename Device, typename Topology >
+   struct center< TNL::Meshes::MeshEntity< MeshConfig, Device, Topology >, 0 >
+   {
+      static int get( const TNL::Meshes::MeshEntity< MeshConfig, Device, Topology >& entity )
+      {
+         throw "not implemented";
+      }
+   };
+
+public:
+   static bool write( const MeshFunction& function,
+                      std::ostream& str,
+                      const double& scale = 1.0 )
+   {
+      const MeshType& mesh = function.getMesh();
+      const GlobalIndex entitiesCount = mesh.template getEntitiesCount< EntityType >();
+      for( GlobalIndex i = 0; i < entitiesCount; i++ ) {
+         const EntityType& entity = mesh.template getEntity< EntityType >( i );
+         typename MeshType::PointType v = center< EntityType >::get( entity );
+         for( int j = 0; j < v.getSize(); j++ )
+            str << v[ j ] << " ";
+         str << scale * function.getData().getElement( i ) << "\n";
+      }
+      return true;
+   }
 };
 
 } // namespace Functions
 } // namespace TNL
-
-#include <TNL/Functions/MeshFunctionGnuplotWriter_impl.h>
diff --git a/src/TNL/Functions/MeshFunctionGnuplotWriter_impl.h b/src/TNL/Functions/MeshFunctionGnuplotWriter_impl.h
deleted file mode 100644
index 13c8fa1cb0..0000000000
--- a/src/TNL/Functions/MeshFunctionGnuplotWriter_impl.h
+++ /dev/null
@@ -1,363 +0,0 @@
-/***************************************************************************
-                          MeshFunctionGnuplotWriter.h  -  description
-                             -------------------
-    begin                : Jan 28, 2016
-    copyright            : (C) 2016 by oberhuber
-    email                : tomas.oberhuber@fjfi.cvut.cz
- ***************************************************************************/
-
-/* See Copyright Notice in tnl/Copyright */
-
-#pragma once
-
-#include <TNL/Functions/MeshFunctionGnuplotWriter.h>
-
-namespace TNL {
-namespace Functions {
-
-template< typename MeshFunction >
-bool
-MeshFunctionGnuplotWriter< MeshFunction >::
-write( const MeshFunction& function,
-       std::ostream& str,
-       const double& scale )
-{
-   std::cerr << "Gnuplot writer for mesh functions defined on mesh type " << MeshFunction::MeshType::getType() << " is not (yet) implemented." << std::endl;
-   return false;
-}
-
-/****
- * 1D grid, cells
- */
-template< typename MeshReal,
-          typename Device,
-          typename MeshIndex,
-          typename Real >
-bool
-MeshFunctionGnuplotWriter< MeshFunction< Meshes::Grid< 1, MeshReal, Device, MeshIndex >, 1, Real > >::
-write( const MeshFunctionType& function,
-       std::ostream& str,
-       const double& scale )
-{
-   const MeshType& mesh = function.getMesh();
-   typename MeshType::Cell entity( mesh );
-   for( entity.getCoordinates().x() = 0;
-        entity.getCoordinates().x() < mesh.getDimensions().x();
-        entity.getCoordinates().x() ++ )
-   {
-      entity.refresh();
-      typename MeshType::PointType v = entity.getCenter();
-      str << v.x() << " "
-          << scale * function.getData().getElement( entity.getIndex() ) << std::endl;
-   }
-   return true;
-}
-
-/****
- * 1D grid, vertices
- */
-template< typename MeshReal,
-          typename Device,
-          typename MeshIndex,
-          typename Real >
-bool
-MeshFunctionGnuplotWriter< MeshFunction< Meshes::Grid< 1, MeshReal, Device, MeshIndex >, 0, Real > >::
-write( const MeshFunctionType& function,
-       std::ostream& str,
-       const double& scale )
-{
-   const MeshType& mesh = function.getMesh();
-   typename MeshType::Vertex entity( mesh );
-   for( entity.getCoordinates().x() = 0;
-        entity.getCoordinates().x() <= mesh.getDimensions().x();
-        entity.getCoordinates().x() ++ )
-   {
-      entity.refresh();
-      typename MeshType::PointType v = entity.getCenter();
-      str << v.x() << " "
-          << scale * function.getData().getElement( entity.getIndex() ) << std::endl;
-   }
-   return true;
-}
-
-
-/****
- * 2D grid, cells
- */
-template< typename MeshReal,
-          typename Device,
-          typename MeshIndex,
-          typename Real >
-bool
-MeshFunctionGnuplotWriter< MeshFunction< Meshes::Grid< 2, MeshReal, Device, MeshIndex >, 2, Real > >::
-write( const MeshFunctionType& function,
-       std::ostream& str,
-       const double& scale )
-{
-   const MeshType& mesh = function.getMesh();
-   typename MeshType::Cell entity( mesh );
-   for( entity.getCoordinates().y() = 0;
-        entity.getCoordinates().y() < mesh.getDimensions().y();
-        entity.getCoordinates().y() ++ )
-   {
-      for( entity.getCoordinates().x() = 0;
-           entity.getCoordinates().x() < mesh.getDimensions().x();
-           entity.getCoordinates().x() ++ )
-      {
-         entity.refresh();
-         typename MeshType::PointType v = entity.getCenter();
-         str << v.x() << " " << v.y() << " "
-             << scale * function.getData().getElement( entity.getIndex() ) << std::endl;
-      }
-      str << std::endl;
-   }
-   return true;
-}
-
-/****
- * 2D grid, faces
- */
-template< typename MeshReal,
-          typename Device,
-          typename MeshIndex,
-          typename Real >
-bool
-MeshFunctionGnuplotWriter< MeshFunction< Meshes::Grid< 2, MeshReal, Device, MeshIndex >, 1, Real > >::
-write( const MeshFunctionType& function,
-       std::ostream& str,
-       const double& scale )
-{
-   const MeshType& mesh = function.getMesh();
-   typedef typename MeshType::Face EntityType;
-   typedef typename EntityType::EntityOrientationType EntityOrientation;
-   EntityType entity( mesh );
-
-   entity.setOrientation( EntityOrientation( 1.0, 0.0 ) );
-   for( entity.getCoordinates().y() = 0;
-        entity.getCoordinates().y() < mesh.getDimensions().y();
-        entity.getCoordinates().y() ++ )
-   {
-      for( entity.getCoordinates().x() = 0;
-           entity.getCoordinates().x() <= mesh.getDimensions().x();
-           entity.getCoordinates().x() ++ )
-      {
-         entity.refresh();
-         typename MeshType::PointType v = entity.getCenter();
-         str << v.x() << " " << v.y() << " "
-             << scale * function.getData().getElement( entity.getIndex() ) << std::endl;
-      }
-      str << std::endl;
-   }
-
-   entity.setOrientation( EntityOrientation( 0.0, 1.0 ) );
-         for( entity.getCoordinates().x() = 0;
-           entity.getCoordinates().x() < mesh.getDimensions().x();
-           entity.getCoordinates().x() ++ )
-
-   {
-            for( entity.getCoordinates().y() = 0;
-        entity.getCoordinates().y() <= mesh.getDimensions().y();
-        entity.getCoordinates().y() ++ )
-
-      {
-         entity.refresh();
-         typename MeshType::PointType v = entity.getCenter();
-         str << v.x() << " " << v.y() << " "
-             << scale * function.getData().getElement( entity.getIndex() ) << std::endl;
-      }
-      str << std::endl;
-   }
-   return true;
-}
-
-
-/****
- * 2D grid, vertices
- */
-template< typename MeshReal,
-          typename Device,
-          typename MeshIndex,
-          typename Real >
-bool
-MeshFunctionGnuplotWriter< MeshFunction< Meshes::Grid< 2, MeshReal, Device, MeshIndex >, 0, Real > >::
-write( const MeshFunctionType& function,
-       std::ostream& str,
-       const double& scale )
-{
-   const MeshType& mesh = function.getMesh();
-   typename MeshType::Vertex entity( mesh );
-   for( entity.getCoordinates().y() = 0;
-        entity.getCoordinates().y() <= mesh.getDimensions().y();
-        entity.getCoordinates().y() ++ )
-   {
-      for( entity.getCoordinates().x() = 0;
-           entity.getCoordinates().x() <= mesh.getDimensions().x();
-           entity.getCoordinates().x() ++ )
-      {
-         entity.refresh();
-         typename MeshType::PointType v = entity.getCenter();
-         str << v.x() << " " << v.y() << " "
-             << scale * function.getData().getElement( entity.getIndex() ) << std::endl;
-      }
-      str << std::endl;
-   }
-   return true;
-}
-
-
-/****
- * 3D grid, cells
- */
-template< typename MeshReal,
-          typename Device,
-          typename MeshIndex,
-          typename Real >
-bool
-MeshFunctionGnuplotWriter< MeshFunction< Meshes::Grid< 3, MeshReal, Device, MeshIndex >, 3, Real > >::
-write( const MeshFunctionType& function,
-       std::ostream& str,
-       const double& scale )
-{
-   const MeshType& mesh = function.getMesh();
-   typename MeshType::Cell entity( mesh );
-   for( entity.getCoordinates().z() = 0;
-        entity.getCoordinates().z() < mesh.getDimensions().z();
-        entity.getCoordinates().z() ++ )
-      for( entity.getCoordinates().y() = 0;
-           entity.getCoordinates().y() < mesh.getDimensions().y();
-           entity.getCoordinates().y() ++ )
-      {
-         for( entity.getCoordinates().x() = 0;
-              entity.getCoordinates().x() < mesh.getDimensions().x();
-              entity.getCoordinates().x() ++ )
-         {
-            entity.refresh();
-            typename MeshType::PointType v = entity.getCenter();
-            str << v.x() << " " << v.y() << " " << v.z() << " "
-                << scale * function.getData().getElement( entity.getIndex() ) << std::endl;
-         }
-         str << std::endl;
-      }
-   return true;
-}
-
-/****
- * 3D grid, faces
- */
-template< typename MeshReal,
-          typename Device,
-          typename MeshIndex,
-          typename Real >
-bool
-MeshFunctionGnuplotWriter< MeshFunction< Meshes::Grid< 3, MeshReal, Device, MeshIndex >, 2, Real > >::
-write( const MeshFunctionType& function,
-       std::ostream& str,
-       const double& scale )
-{
-   const MeshType& mesh = function.getMesh();
-   typedef typename MeshType::Face EntityType;
-   typedef typename EntityType::EntityOrientationType EntityOrientation;
-   EntityType entity( mesh );
-
-   entity.setOrientation( EntityOrientation( 1.0, 0.0, 0.0 ) );
-   for( entity.getCoordinates().z() = 0;
-        entity.getCoordinates().z() < mesh.getDimensions().z();
-        entity.getCoordinates().z() ++ )
-      for( entity.getCoordinates().y() = 0;
-           entity.getCoordinates().y() < mesh.getDimensions().y();
-           entity.getCoordinates().y() ++ )
-      {
-         for( entity.getCoordinates().x() = 0;
-              entity.getCoordinates().x() <= mesh.getDimensions().x();
-              entity.getCoordinates().x() ++ )
-         {
-            entity.refresh();
-            typename MeshType::PointType v = entity.getCenter();
-            str << v.x() << " " << v.y() << " " << v.z() << " "
-                << scale * function.getData().getElement( entity.getIndex() ) << std::endl;
-         }
-         str << std::endl;
-      }
-
-   entity.setOrientation( EntityOrientation( 0.0, 1.0, 0.0 ) );
-   for( entity.getCoordinates().z() = 0;
-        entity.getCoordinates().z() < mesh.getDimensions().z();
-        entity.getCoordinates().z() ++ )
-      for( entity.getCoordinates().x() = 0;
-           entity.getCoordinates().x() < mesh.getDimensions().x();
-           entity.getCoordinates().x() ++ )
-      {
-         for( entity.getCoordinates().y() = 0;
-              entity.getCoordinates().y() <= mesh.getDimensions().y();
-              entity.getCoordinates().y() ++ )
-         {
-            entity.refresh();
-            typename MeshType::PointType v = entity.getCenter();
-            str << v.x() << " " << v.y() << " " << v.z() << " "
-                << scale * function.getData().getElement( entity.getIndex() ) << std::endl;
-         }
-         str << std::endl;
-      }
-
-   entity.setOrientation( EntityOrientation( 0.0, 0.0, 1.0 ) );
-   for( entity.getCoordinates().x() = 0;
-        entity.getCoordinates().x() < mesh.getDimensions().x();
-        entity.getCoordinates().x() ++ )
-      for( entity.getCoordinates().y() = 0;
-           entity.getCoordinates().y() <= mesh.getDimensions().y();
-           entity.getCoordinates().y() ++ )
-      {
-         for( entity.getCoordinates().z() = 0;
-              entity.getCoordinates().z() < mesh.getDimensions().z();
-              entity.getCoordinates().z() ++ )
-         {
-            entity.refresh();
-            typename MeshType::PointType v = entity.getCenter();
-            str << v.x() << " " << v.y() << " " << v.z() << " "
-                << scale * function.getData().getElement( entity.getIndex() ) << std::endl;
-         }
-         str << std::endl;
-      }
-   return true;
-}
-
-
-/****
- * 3D grid, vertices
- */
-template< typename MeshReal,
-          typename Device,
-          typename MeshIndex,
-          typename Real >
-bool
-MeshFunctionGnuplotWriter< MeshFunction< Meshes::Grid< 3, MeshReal, Device, MeshIndex >, 0, Real > >::
-write( const MeshFunctionType& function,
-       std::ostream& str,
-       const double& scale )
-{
-   const MeshType& mesh = function.getMesh();
-   typename MeshType::Vertex entity( mesh );
-   for( entity.getCoordinates().z() = 0;
-        entity.getCoordinates().z() <= mesh.getDimensions().z();
-        entity.getCoordinates().z() ++ )
-      for( entity.getCoordinates().y() = 0;
-           entity.getCoordinates().y() <= mesh.getDimensions().y();
-           entity.getCoordinates().y() ++ )
-      {
-         for( entity.getCoordinates().x() = 0;
-              entity.getCoordinates().x() <= mesh.getDimensions().x();
-              entity.getCoordinates().x() ++ )
-         {
-            entity.refresh();
-            typename MeshType::PointType v = entity.getCenter();
-            str << v.x() << " " << v.y() << " " << v.z() << " "
-                << scale * function.getData().getElement( entity.getIndex() ) << std::endl;
-         }
-         str << std::endl;
-      }
-   return true;
-}
-
-} // namespace Functions
-} // namespace TNL
-
-- 
GitLab