From eb61fb7deb443505a8e4c0339fa00d99ecc5f44d Mon Sep 17 00:00:00 2001
From: Tomas Oberhuber <tomas.oberhuber@fjfi.cvut.cz>
Date: Wed, 3 Apr 2019 16:13:41 +0200
Subject: [PATCH] Adding support for adaptive grid.

---
 CMakeLists.txt                                | 13 ++-
 build                                         |  8 ++
 .../DistributedGridSynchronizer.h             |  2 +-
 src/TNL/Meshes/Readers/TNLReader.h            | 13 ++-
 .../TypeResolver/GridTypeResolver_impl.h      | 22 ++++-
 .../Meshes/TypeResolver/TypeResolver_impl.h   |  8 ++
 src/Tools/tnl-grid-setup.cpp                  |  1 +
 src/Tools/tnl-grid-setup.h                    | 83 ++++++++++++++-----
 8 files changed, 122 insertions(+), 28 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3642572635..e863dce830 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -122,7 +122,7 @@ if( DEFINED ENV{CI_JOB_NAME} OR ${CMAKE_GENERATOR} STREQUAL "Ninja" )
 endif()
 
 #####
-# Check for MPI -- poznej podle vraperu compileru -- da se testovat preklad bez MPI
+# Check for MPI
 #
 get_filename_component( CXX_COMPILER_NAME ${CMAKE_CXX_COMPILER} NAME )
 if( ${CXX_COMPILER_NAME} STREQUAL "mpicxx" )
@@ -267,6 +267,17 @@ if( JPEG_FOUND )
    include_directories( ${JPEG_INCLUDE_DIRS} )
 endif()
 
+
+####
+# Adaptive grid - temporary
+#
+if( ${WITH_ADAPTIVE_GRID} )
+    set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAVE_ADAPTIVE_GRID" )
+    if( NOT ${ADAPTIVE_GRID_PATH} EQUAL "" )
+        set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I ${ADAPTIVE_GRID_PATH}" )
+    endif()
+endif()
+
 ####
 # Test for GMP 
 #
diff --git a/build b/build
index c009a26084..899fa724dc 100755
--- a/build
+++ b/build
@@ -28,6 +28,9 @@ WITH_EXAMPLES="yes"
 WITH_PYTHON="yes"
 WITH_TOOLS="yes"
 WITH_BENCHMARKS="yes"
+# This is temporary
+WITH_ADAPTIVE_GRID="no"
+ADAPTIVE_GRID_PATH=""
 
 WITH_TEMPLATE_INSTANTIATION="no"
 INSTANTIATE_LONG_INT="no"
@@ -65,6 +68,8 @@ do
         --with-benchmarks=*              ) WITH_BENCHMARKS="${option#*=}" ;;
         --with-python=*                  ) WITH_PYTHON="${option#*=}" ;;
         --with-templates-instantiation=* ) WITH_TEMPLATE_INSTANTIATION="${option#*=}" ;;
+        --with-adaptive-grid=*           ) WITH_ADAPTIVE_GRID="${option#*=}" ;;
+        --adaptive-grid-path=*           ) ADAPTIVE_GRID_PATH="${option#*=}" ;;
         --instantiate-long-int=*         ) INSTANTIATE_LONG_INT="${option#*=}" ;;
         --instantiate-int=*              ) INSTANTIATE_INT="${option#*=}" ;;
         --instantiate-long-double=*      ) INSTANTIATE_LONG_DOUBLE="${option#*=}" ;;
@@ -101,6 +106,7 @@ if [[ ${HELP} == "yes" ]]; then
     echo "   --with-coverage=yes/no                Enables code coverage reports for unit tests. 'no' by default (lcov is required)."
     echo "   --with-examples=yes/no                Compile the 'examples' directory. 'yes' by default."
     echo "   --with-tools=yes/no                   Compile the 'src/Tools' directory. 'yes' by default."
+    echo "   --with-benchmarks=yes/no              Compile the 'src/Benchmarks' directory. 'yes' by default."
     echo "   --with-python=yes/no                  Compile the Python bindings. 'yes' by default."
     echo "   --with-templates-instantiation=yes/no Precompiles some TNL templates during the build. 'no' by default."
     echo "   --cmake=CMAKE                         Path to cmake. 'cmake' by default."
@@ -173,6 +179,8 @@ cmake_command=(
          -DWITH_EXAMPLES=${WITH_EXAMPLES}
          -DWITH_TOOLS=${WITH_TOOLS}
          -DWITH_BENCHMARKS=${WITH_BENCHMARKS}
+         -DWITH_ADAPTIVE_GRID=${WITH_ADAPTIVE_GRID}
+         -DADAPTIVE_GRID_PATH=${ADAPTIVE_GRID_PATH}
          -DWITH_PYTHON=${WITH_PYTHON}
          -DDCMTK_DIR=${DCMTK_DIR}
          -DWITH_TEMPLATE_INSTANTIATION=${WITH_TEMPLATE_INSTANTIATION}
diff --git a/src/TNL/Meshes/DistributedMeshes/DistributedGridSynchronizer.h b/src/TNL/Meshes/DistributedMeshes/DistributedGridSynchronizer.h
index e99e755fd0..6f058a8015 100644
--- a/src/TNL/Meshes/DistributedMeshes/DistributedGridSynchronizer.h
+++ b/src/TNL/Meshes/DistributedMeshes/DistributedGridSynchronizer.h
@@ -176,7 +176,7 @@ class DistributedMeshSynchronizer< Functions::MeshFunction< Grid< MeshDimension,
                requests.push_back( CommunicatorType::IRecv( recieveBuffers[ i ].getData(),  sendSizes[ i ], periodicNeighbors[ i ], 1, group ) );
             }
          }
-      };
+      }
       
       template< typename CommunicatorType,
                 typename MeshFunctionType,
diff --git a/src/TNL/Meshes/Readers/TNLReader.h b/src/TNL/Meshes/Readers/TNLReader.h
index 68f4d13a21..d75b9dec66 100644
--- a/src/TNL/Meshes/Readers/TNLReader.h
+++ b/src/TNL/Meshes/Readers/TNLReader.h
@@ -153,7 +153,17 @@ public:
    {
       return idType;
    }
- 
+   
+   void setAdaptivity( bool adaptive )
+   {
+      this->adaptivity = adaptive;
+   }
+   
+   bool getAdaptivity() const
+   {
+      return this->adaptivity;
+   }
+   
 protected:
    String fileName;
    String meshType;
@@ -164,6 +174,7 @@ protected:
    String globalIndexType;
    String localIndexType;
    String idType;
+   bool adaptivity = false;
 
    void reset()
    {
diff --git a/src/TNL/Meshes/TypeResolver/GridTypeResolver_impl.h b/src/TNL/Meshes/TypeResolver/GridTypeResolver_impl.h
index 20f40c2682..81fc87f70d 100644
--- a/src/TNL/Meshes/TypeResolver/GridTypeResolver_impl.h
+++ b/src/TNL/Meshes/TypeResolver/GridTypeResolver_impl.h
@@ -16,6 +16,11 @@
 #include <TNL/Meshes/Grid.h>
 #include <TNL/Meshes/TypeResolver/GridTypeResolver.h>
 
+#ifdef HAVE_ADAPTIVE_GRID
+#include <TNL/Meshes/GridAdaptive.h>
+#endif
+
+
 namespace TNL {
 namespace Meshes {   
 
@@ -162,8 +167,21 @@ GridTypeResolver< Reader, ConfigTag, Device, ProblemSetter, ProblemSetterArgs...
 resolveGridType( const Reader& reader,
                  ProblemSetterArgs&&... problemSetterArgs )
 {
-   using GridType = Meshes::Grid< MeshDimension, Real, Device, Index >;
-   return resolveTerminate< GridType >( reader, std::forward<ProblemSetterArgs>(problemSetterArgs)... );
+   if( reader.getAdaptivity() )
+   {
+#ifdef HAVE_ADAPTIVE_GRID
+      using GridType = Meshes::GridA< MeshDimension, Real, Device, Index >;
+      return resolveTerminate< GridType >( reader, std::forward<ProblemSetterArgs>(problemSetterArgs)... );
+#else
+      std::cerr << "Adaptive grid is not supported." << std::endl;
+      return false;
+#endif
+   }
+   else
+   {
+      using GridType = Meshes::Grid< MeshDimension, Real, Device, Index >;
+      return resolveTerminate< GridType >( reader, std::forward<ProblemSetterArgs>(problemSetterArgs)... );
+   }
 }
 
 template< typename Reader,
diff --git a/src/TNL/Meshes/TypeResolver/TypeResolver_impl.h b/src/TNL/Meshes/TypeResolver/TypeResolver_impl.h
index b3fe7f1ba5..79e8e89f71 100644
--- a/src/TNL/Meshes/TypeResolver/TypeResolver_impl.h
+++ b/src/TNL/Meshes/TypeResolver/TypeResolver_impl.h
@@ -66,6 +66,14 @@ bool resolveMeshType( const String& fileName_,
       if( reader.getMeshType() == "Meshes::Grid" )
          return GridTypeResolver< decltype(reader), ConfigTag, Device, ProblemSetter, ProblemSetterArgs... >::
             run( reader, std::forward<ProblemSetterArgs>(problemSetterArgs)... );
+#ifdef HAVE_ADAPTIVE_GRID      
+      else if( reader.getMeshType() == "Meshes::AdaptiveGrid" )
+      {
+         reader.setAdaptivity( true );
+         return GridTypeResolver< decltype(reader), ConfigTag, Device, ProblemSetter, ProblemSetterArgs... >::
+            run( reader, std::forward<ProblemSetterArgs>(problemSetterArgs)... );
+      }
+#endif
       else if( reader.getMeshType() == "Meshes::Mesh" )
          return MeshTypeResolver< decltype(reader), ConfigTag, Device, ProblemSetter, ProblemSetterArgs... >::
             run( reader, std::forward<ProblemSetterArgs>(problemSetterArgs)... );
diff --git a/src/Tools/tnl-grid-setup.cpp b/src/Tools/tnl-grid-setup.cpp
index 1846dc0935..31c3276708 100644
--- a/src/Tools/tnl-grid-setup.cpp
+++ b/src/Tools/tnl-grid-setup.cpp
@@ -37,6 +37,7 @@ void configSetup( Config::ConfigDescription& config )
    config.addEntry        < int >      ( "size-y",            "Number of elements along the y axis." );
    config.addEntry        < int >      ( "size-z",            "Number of elements along the z axis." );
    config.addEntry        < bool >     ( "equal-space-steps", "All space steps will be equivalent.", false );
+   config.addEntry        < bool >     ( "adaptive-grid",     "Create adaptive grid.", false );
 }
 
 int main( int argc, char* argv[] )
diff --git a/src/Tools/tnl-grid-setup.h b/src/Tools/tnl-grid-setup.h
index 48da6a16dc..19c641dd4e 100644
--- a/src/Tools/tnl-grid-setup.h
+++ b/src/Tools/tnl-grid-setup.h
@@ -8,12 +8,14 @@
 
 /* See Copyright Notice in tnl/Copyright */
 
-#ifndef TNL_GRID_SETUP_H_
-#define TNL_GRID_SETUP_H_
-
+#pragma once
 #include <TNL/Config/ParameterContainer.h>
 #include <TNL/Meshes/Grid.h>
 
+#ifdef HAVE_ADAPTIVE_GRID
+#include <TNL/Meshes/GridAdaptive.h>
+#endif
+
 using namespace TNL;
 
 template< typename RealType, typename IndexType >
@@ -50,30 +52,67 @@ bool setupGrid( const Config::ParameterContainer& parameters )
       RealType proportionsY = parameters.getParameter< double >( "proportions-y" );
       IndexType sizeX = parameters.getParameter< int >( "size-x" );
       IndexType sizeY = parameters.getParameter< int >( "size-y" );
-      typedef Meshes::Grid< 2, RealType, Devices::Host, IndexType > GridType;
-      typedef typename GridType::PointType PointType;
-      typedef typename GridType::CoordinatesType CoordinatesType;
-      GridType grid;
-      grid.setDomain( PointType( originX, originY ), PointType( proportionsX, proportionsY ) );
-      grid.setDimensions( CoordinatesType( sizeX, sizeY ) );
-      if( parameters.getParameter< bool >( "equal-space-steps" ) )
+      if( parameters.getParameter< bool >( "adaptive-grid" ) )
       {
-         if( grid.getSpaceSteps().x() != grid.getSpaceSteps().y() )
+#ifdef HAVE_ADAPTIVE_GRID
+         typedef Meshes::GridA< 2, RealType, Devices::Host, IndexType > AdaptiveGridType;
+         typedef typename AdaptiveGridType::PointType PointType;
+         typedef typename AdaptiveGridType::CoordinatesType CoordinatesType;
+         
+         AdaptiveGridType grid;
+         grid.setDomain( PointType( originX, originY ), PointType( proportionsX, proportionsY ) );
+         grid.setDimensions( CoordinatesType( sizeX, sizeY ) );
+         if( parameters.getParameter< bool >( "equal-space-steps" ) )
          {
-            double h = min( grid.getSpaceSteps().x(), grid.getSpaceSteps().y() );
-            grid.setSpaceSteps( PointType( h, h ) );
-            std::cout << "Adjusting grid space steps to " << grid.getSpaceSteps() 
-                      << " and grid proportions to " << grid.getProportions() << "."  << std::endl;
+            if( grid.getSpaceSteps().x() != grid.getSpaceSteps().y() )
+            {
+               double h = min( grid.getSpaceSteps().x(), grid.getSpaceSteps().y() );
+               grid.setSpaceSteps( PointType( h, h ) );
+               std::cout << "Adjusting grid space steps to " << grid.getSpaceSteps() 
+                         << " and grid proportions to " << grid.getProportions() << "."  << std::endl;
 
+            }
          }
-      }
-      std::cout << "Setting dimensions to  ... " << grid.getDimensions() << std::endl;
-      std::cout << "Writing the grid to the file " << outputFile << " .... ";
+         std::cout << "Setting dimensions to  ... " << grid.getDimensions() << std::endl;
+         std::cout << "Writing the grid to the file " << outputFile << " .... ";
 
-      if( ! grid.save( outputFile ) )
+         if( ! grid.save( outputFile ) )
+         {
+            std::cerr << "[ FAILED ] " << std::endl;
+            return false;
+         }         
+#else
+         std::cerr << "Adaptive grid is not supported." << std::endl;
+#endif
+      }
+      else
       {
-         std::cerr << "[ FAILED ] " << std::endl;
-         return false;
+         typedef Meshes::Grid< 2, RealType, Devices::Host, IndexType > GridType;
+         typedef typename GridType::PointType PointType;
+         typedef typename GridType::CoordinatesType CoordinatesType;
+
+         GridType grid;
+         grid.setDomain( PointType( originX, originY ), PointType( proportionsX, proportionsY ) );
+         grid.setDimensions( CoordinatesType( sizeX, sizeY ) );
+         if( parameters.getParameter< bool >( "equal-space-steps" ) )
+         {
+            if( grid.getSpaceSteps().x() != grid.getSpaceSteps().y() )
+            {
+               double h = min( grid.getSpaceSteps().x(), grid.getSpaceSteps().y() );
+               grid.setSpaceSteps( PointType( h, h ) );
+               std::cout << "Adjusting grid space steps to " << grid.getSpaceSteps() 
+                         << " and grid proportions to " << grid.getProportions() << "."  << std::endl;
+
+            }
+         }
+         std::cout << "Setting dimensions to  ... " << grid.getDimensions() << std::endl;
+         std::cout << "Writing the grid to the file " << outputFile << " .... ";
+
+         if( ! grid.save( outputFile ) )
+         {
+            std::cerr << "[ FAILED ] " << std::endl;
+            return false;
+         }
       }
    }
    if( dimensions == 3 )
@@ -144,5 +183,3 @@ bool resolveRealType( const Config::ParameterContainer& parameters )
    std::cerr << "The real type '" << realType << "' is not supported. " << std::endl;
    return false;
 }
-
-#endif /* TNL_GRID_SETUP_H_ */
-- 
GitLab