Commit 3e9920c2 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Updated tnl-diff and tnl-init to use MeshFunctionIO functions

parent d1a7cafa
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ endif()

find_package( ZLIB )
if( ZLIB_FOUND )
   foreach( target IN ITEMS tnl-view tnl-grid-to-mesh tnl-mesh-converter tnl-game-of-life tnl-test-distributed-mesh )
   foreach( target IN ITEMS tnl-init tnl-diff tnl-view tnl-grid-to-mesh tnl-mesh-converter tnl-game-of-life tnl-test-distributed-mesh )
      target_compile_definitions(${target} PUBLIC "-DHAVE_ZLIB")
      target_include_directories(${target} PUBLIC ${ZLIB_INCLUDE_DIRS})
      target_link_libraries(${target} ${ZLIB_LIBRARIES})
@@ -38,7 +38,7 @@ endif()

find_package( tinyxml2 QUIET )
if( tinyxml2_FOUND )
   foreach( target IN ITEMS tnl-mesh-converter tnl-game-of-life tnl-test-distributed-mesh )
   foreach( target IN ITEMS tnl-init tnl-diff tnl-grid-to-mesh tnl-mesh-converter tnl-game-of-life tnl-test-distributed-mesh )
      target_compile_definitions(${target} PUBLIC "-DHAVE_TINYXML2")
      target_link_libraries(${target} tinyxml2::tinyxml2)
   endforeach()
+35 −27
Original line number Diff line number Diff line
@@ -11,13 +11,38 @@
#include "tnl-diff.h"
#include <TNL/Config/parseCommandLine.h>
#include <TNL/Meshes/Grid.h>
#include <TNL/Meshes/TypeResolver/resolveMeshType.h>

struct TNLDiffBuildConfigTag {};

namespace TNL {
namespace Meshes {
namespace BuildConfigTags {

/****
 * Turn off support for float and long double.
 */
//template<> struct GridRealTag< TNLDiffBuildConfigTag, float > { enum { enabled = false }; };
template<> struct GridRealTag< TNLDiffBuildConfigTag, long double > { enum { enabled = false }; };

/****
 * Turn off support for short int and long int indexing.
 */
template<> struct GridIndexTag< TNLDiffBuildConfigTag, short int >{ enum { enabled = false }; };
template<> struct GridIndexTag< TNLDiffBuildConfigTag, long int >{ enum { enabled = false }; };

} // namespace BuildConfigTags
} // namespace Meshes
} // namespace TNL

void setupConfig( Config::ConfigDescription& config )
{
   config.addEntry< String >( "mesh", "Input mesh file.", "mesh.tnl" );
   config.addRequiredList< String >( "input-files", "The first set of the input files." );
   config.addEntry< String >( "mesh", "Input mesh file.", "mesh.vti" );
   config.addEntry< String >( "mesh-format", "Mesh file format.", "auto" );
   config.addRequiredList< String >( "input-files", "Input files containing the mesh functions to be compared." );
   config.addEntry< String >( "mesh-function-name", "Name of the mesh function in the input files.", "f" );
   config.addEntry< String >( "output-file", "File for the output data.", "tnl-diff.log" );
   config.addEntry< String >( "mode", "Mode 'couples' compares two subsequent files. Mode 'sequence' compares the input files against the first one. 'halves' compares the files from the and the second half of the intput files.", "couples" );
   config.addEntry< String >( "mode", "Mode 'couples' compares two subsequent files. Mode 'sequence' compares the input files against the first one. 'halves' compares the files from the first and the second half of the intput files.", "couples" );
      config.addEntryEnum< String >( "couples" );
      config.addEntryEnum< String >( "sequence" );
      config.addEntryEnum< String >( "halves" );
@@ -39,29 +64,12 @@ int main( int argc, char* argv[] )
   if( ! parseCommandLine( argc, argv, conf_desc, parameters ) )
      return EXIT_FAILURE;

   String meshFile = parameters.getParameter< String >( "mesh" );
   const String meshType = getObjectType( meshFile );
   std::cout << meshType << " detected in " << meshFile << " file." << std::endl;
   const std::vector< String > parsedMeshType = parseObjectType( meshType );
   if( ! parsedMeshType.size() )
   const String meshFile = parameters.getParameter< String >( "mesh" );
   const String meshFileFormat = parameters.getParameter< String >( "mesh-format" );
   auto wrapper = [&] ( const auto& reader, auto&& mesh )
   {
      std::cerr << "Unable to parse the mesh type " << meshType << "." << std::endl;
      return EXIT_FAILURE;
   }
   if( parsedMeshType[ 0 ] == "Meshes::Grid" ||
       parsedMeshType[ 0 ] == "tnlGrid" )        // TODO: remove deprecated type name
   {
      const int dimensions = atoi( parsedMeshType[ 1 ].getString() );
      if( dimensions == 1 )
         if( ! resolveGridRealType< 1 >( parsedMeshType, parameters ) )
            return EXIT_FAILURE;
      if( dimensions == 2 )
         if( ! resolveGridRealType< 2 >( parsedMeshType, parameters ) )
            return EXIT_FAILURE;
      if( dimensions == 3 )
         if( ! resolveGridRealType< 3 >( parsedMeshType, parameters ) )
            return EXIT_FAILURE;
      return EXIT_SUCCESS;
   }
   return EXIT_FAILURE;
      using MeshType = std::decay_t< decltype(mesh) >;
      return processFiles< MeshType >( parameters );
   };
   return ! TNL::Meshes::resolveMeshType< TNLDiffBuildConfigTag, Devices::Host >( wrapper, meshFile, meshFileFormat );
}
+23 −63
Original line number Diff line number Diff line
@@ -8,14 +8,14 @@

/* See Copyright Notice in tnl/Copyright */

#ifndef TNL_DIFF_H_
#define TNL_DIFF_H_
#pragma once

#include <iomanip>
#include <TNL/Config/ParameterContainer.h>
#include <TNL/FileName.h>
#include <TNL/Containers/Vector.h>
#include <TNL/Containers/StaticVector.h>
#include <TNL/Meshes/TypeResolver/resolveMeshType.h>
#include <TNL/Functions/MeshFunction.h>
#include <TNL/Exceptions/NotImplementedError.h>

@@ -183,6 +183,7 @@ bool computeDifferenceOfMeshFunctions( const MeshPointer& meshPointer, const Con
{
   bool verbose = parameters. getParameter< bool >( "verbose" );
   std::vector< String > inputFiles = parameters. getParameter< std::vector< String > >( "input-files" );
   const String meshFunctionName = parameters.getParameter< String >( "mesh-function-name" );
   String mode = parameters. getParameter< String >( "mode" );
   String outputFileName = parameters. getParameter< String >( "output-file" );
   double snapshotPeriod = parameters. getParameter< double >( "snapshot-period" );
@@ -230,8 +231,8 @@ bool computeDifferenceOfMeshFunctions( const MeshPointer& meshPointer, const Con
           std::cout << "Processing files " << inputFiles[ i ] << " and " << inputFiles[ i + 1 ] << "...           \r" << std::flush;
         try
         {
            v1.load( inputFiles[ i ] );
            v2.load( inputFiles[ i + 1 ] );
            Functions::readMeshFunction( v1, meshFunctionName, inputFiles[ i ], "auto" );
            Functions::readMeshFunction( v2, meshFunctionName, inputFiles[ i + 1 ], "auto" );
         }
         catch(...)
         {
@@ -251,12 +252,12 @@ bool computeDifferenceOfMeshFunctions( const MeshPointer& meshPointer, const Con
         {
            if( verbose )
              std::cout << "Reading the file " << inputFiles[ 0 ] << "...               \r" << std::flush;
            v1.load( inputFiles[ 0 ] );
            Functions::readMeshFunction( v1, meshFunctionName, inputFiles[ 0 ], "auto" );
            file1 = inputFiles[ 0 ];
         }
         if( verbose )
           std::cout << "Processing the files " << inputFiles[ 0 ] << " and " << inputFiles[ i ] << "...             \r" << std::flush;
         v2.load( inputFiles[ i ] );
         Functions::readMeshFunction( v2, meshFunctionName, inputFiles[ i ], "auto" );
         if( ! exactMatch )
            outputFile << std::setw( 6 ) << ( i - 1 ) * snapshotPeriod << " ";
         file2 = inputFiles[ i ];
@@ -268,8 +269,8 @@ bool computeDifferenceOfMeshFunctions( const MeshPointer& meshPointer, const Con
            i = half;
         if( verbose )
           std::cout << "Processing files " << inputFiles[ i - half ] << " and " << inputFiles[ i ] << "...                 \r" << std::flush;
         v1.load( inputFiles[ i - half ] );
         v2.load( inputFiles[ i ] );
         Functions::readMeshFunction( v1, meshFunctionName, inputFiles[ i - half ], "auto" );
         Functions::readMeshFunction( v2, meshFunctionName, inputFiles[ i ], "auto" );
         //if( snapshotPeriod != 0.0 )
         if( ! exactMatch )
            outputFile << std::setw( 6 ) << ( i - half ) * snapshotPeriod << " ";
@@ -305,11 +306,11 @@ bool computeDifferenceOfMeshFunctions( const MeshPointer& meshPointer, const Con

         if( writeDifference )
         {
            String differenceFileName = removeFileNameExtension( inputFiles[ i ] ) + ".diff.tnl";
            String differenceFileName = removeFileNameExtension( inputFiles[ i ] ) + ".diff.vti";
            //diff.setLike( v1 );
            diff = v1;
            diff -= v2;
            diff.save( differenceFileName );
            diff.write( "diff", differenceFileName );
         }
      }
   }
@@ -558,29 +559,16 @@ bool setValueType( const MeshPointer& meshPointer,
template< typename Mesh >
bool processFiles( const Config::ParameterContainer& parameters )
{
   int verbose = parameters. getParameter< bool >( "verbose");
   std::vector< String > inputFiles = parameters. getParameter< std::vector< String > >( "input-files" );

   /****
    * Reading the mesh
    */
   String meshFile = parameters. getParameter< String >( "mesh" );
   int verbose = parameters.getParameter< int >( "verbose");
   const String meshFile = parameters.getParameter< String >( "mesh" );
   const String meshFileFormat = parameters.getParameter< String >( "mesh-format" );

   typedef Pointers::SharedPointer< Mesh > MeshPointer;

   MeshPointer meshPointer;
   if( meshFile != "" )
   {
      try
      {
         meshPointer->load( meshFile );
      }
      catch(...)
      {
         std::cerr << "I am not able to load mesh from the file " << meshFile << "." << std::endl;
   if( ! Meshes::loadMesh( *meshPointer, meshFile, meshFileFormat ) )
      return false;
      }
   }

   std::vector< String > inputFiles = parameters.getParameter< std::vector< String > >( "input-files" );

   String objectType;
   try
@@ -604,31 +592,3 @@ bool processFiles( const Config::ParameterContainer& parameters )
   setValueType< MeshPointer >( meshPointer, inputFiles[ 0 ], parsedObjectType, parameters );
   return true;
}

template< int Dim, typename Real >
bool resolveGridIndexType( const std::vector< String >& parsedMeshType,
                           const Config::ParameterContainer& parameters )
{
   if( parsedMeshType[ 4 ] == "int" )
      return processFiles< Meshes::Grid< Dim, Real, Devices::Host, int > >( parameters );
   if( parsedMeshType[ 4 ] == "long int" )
      return processFiles< Meshes::Grid< Dim, Real, Devices::Host, long int > >( parameters );
   std::cerr << "Unknown index type " << parsedMeshType[ 4 ] << "." <<  std::endl;
   return false;
}

template< int Dim >
bool resolveGridRealType( const std::vector< String >& parsedMeshType,
                          const Config::ParameterContainer& parameters )
{
   if( parsedMeshType[ 2 ] == "float" )
      return resolveGridIndexType< Dim, float >( parsedMeshType, parameters );
   if( parsedMeshType[ 2 ] == "double" )
      return resolveGridIndexType< Dim, double >( parsedMeshType, parameters );
//   if( parsedMeshType[ 2 ] == "long double" )
//      return resolveGridIndexType< Dim, long double >( parsedMeshType, parameters );
   std::cerr << "Unknown real type " << parsedMeshType[ 2 ] << "." <<  std::endl;
   return false;
}

#endif /* TNL_DIFF_H_ */
+5 −6
Original line number Diff line number Diff line
@@ -24,7 +24,8 @@ using namespace TNL;
void setupConfig( Config::ConfigDescription& config )
{
   config.addDelimiter( "General settings:" );
   config.addEntry< String >( "mesh", "Mesh file. If none is given, a regular rectangular mesh is assumed.", "mesh.tnl" );
   config.addEntry< String >( "mesh", "Mesh file. If none is given, a regular rectangular mesh is assumed.", "mesh.vti" );
   config.addEntry< String >( "mesh-function-name", "Name of the mesh function in the VTI files.", "f" );
   config.addEntry< String >( "real-type", "Precision of the function evaluation.", "mesh-real-type" );
      config.addEntryEnum< String >( "mesh-real-type" );
      config.addEntryEnum< String >( "float" );
@@ -45,8 +46,6 @@ void setupConfig( Config::ConfigDescription& config )
   Functions::TestFunction< 1 >::configSetup( config );
}



int main( int argc, char* argv[] )
{
   Config::ParameterContainer parameters;
+34 −24
Original line number Diff line number Diff line
@@ -13,6 +13,8 @@
#include <TNL/MPI/Wrappers.h>
#include <TNL/Config/ParameterContainer.h>
#include <TNL/Meshes/Grid.h>
#include <TNL/Meshes/Readers/VTIReader.h>
#include <TNL/Meshes/Writers/VTIWriter.h>
#include <TNL/Functions/TestFunction.h>
#include <TNL/Operators/FiniteDifferences.h>
#include <TNL/FileName.h>
@@ -37,25 +39,27 @@ bool renderFunction( const Config::ParameterContainer& parameters )
   Pointers::SharedPointer< MeshType > meshPointer;
   MeshType globalMesh;

   if(TNL::MPI::GetSize() > 1)
   // TODO: PVTI reader is not implemented yet
//   if(TNL::MPI::GetSize() > 1)
//   {
//       //suppose global mesh loaded from single file
//       String meshFile = parameters.getParameter< String >( "mesh" );
//       std::cout << "+ -> Loading mesh from " << meshFile << " ... " << std::endl;
//       globalMesh.load( meshFile );
//
//       // TODO: This should work with no overlaps
//       distributedMesh.setGlobalGrid(globalMesh);
//       typename DistributedGridType::SubdomainOverlapsType lowerOverlap, upperOverlap;
//       SubdomainOverlapsGetter< MeshType >::getOverlaps( &distributedMesh, lowerOverlap, upperOverlap, 1 );
//       distributedMesh.setOverlaps( lowerOverlap, upperOverlap );
//       distributedMesh.setupGrid(*meshPointer);
//    }
//    else
    {
       //suppose global mesh loaded from single file
       String meshFile = parameters.getParameter< String >( "mesh" );
       std::cout << "+ -> Loading mesh from " << meshFile << " ... " << std::endl;
       globalMesh.load( meshFile );

       // TODO: This should work with no overlaps
       distributedMesh.setGlobalGrid(globalMesh);
       typename DistributedGridType::SubdomainOverlapsType lowerOverlap, upperOverlap;
       SubdomainOverlapsGetter< MeshType >::getOverlaps( &distributedMesh, lowerOverlap, upperOverlap, 1 );
       distributedMesh.setOverlaps( lowerOverlap, upperOverlap );
       distributedMesh.setupGrid(*meshPointer);
    }
    else
    {
       String meshFile = parameters.getParameter< String >( "mesh" );
       std::cout << "+ -> Loading mesh from " << meshFile << " ... " << std::endl;
       meshPointer->load( meshFile );
       Meshes::Readers::VTIReader reader( meshFile );
       reader.loadMesh( *meshPointer );
    }

   typedef Functions::TestFunction< MeshType::getMeshDimension(), RealType > FunctionType;
@@ -81,7 +85,6 @@ bool renderFunction( const Config::ParameterContainer& parameters )

   while( step <= steps )
   {

      if( numericalDifferentiation )
      {
        std::cout << "+ -> Computing the finite differences ... " << std::endl;
@@ -112,17 +115,24 @@ bool renderFunction( const Config::ParameterContainer& parameters )
      else
         std::cout << "+ -> Writing the function to " << outputFile << " ... " << std::endl;

      if(TNL::MPI::GetSize() > 1)
      const std::string meshFunctionName = parameters.getParameter< std::string >( "mesh-function-name" );

      // TODO: PVTI writer is not implemented yet
//      if(TNL::MPI::GetSize() > 1)
//      {
//         if( ! Meshes::DistributedMeshes::DistributedGridIO<MeshFunctionType> ::save(outputFile, *meshFunction ) )
//            return false;
//      }
//      else
      {
         if( ! Meshes::DistributedMeshes::DistributedGridIO<MeshFunctionType> ::save(outputFile, *meshFunction ) )
            return false;
         // TODO: write metadata: step and time
         meshFunction->write( meshFunctionName, outputFile, "auto" );
      }
      else
        meshFunction->save( outputFile);

      time += tau;
      step ++;
   }

   return true;
}