From eee0c9ef7f8d4f85824c750f1a40b239117196e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Klinkovsk=C3=BD?= <klinkovsky@mmg.fjfi.cvut.cz> Date: Wed, 5 Feb 2020 10:16:27 +0100 Subject: [PATCH] Fixed handling of parameters in several tools Fixes #12 --- src/TNL/Config/ParameterContainer.h | 15 ++++++- src/Tools/tnl-diff.cpp | 6 +-- src/Tools/tnl-grid-setup.cpp | 31 +++++++------- src/Tools/tnl-grid-setup.h | 64 +++++++++++++++++------------ src/Tools/tnl-view.cpp | 33 ++++++++------- src/Tools/tnl-view.h | 2 +- 6 files changed, 88 insertions(+), 63 deletions(-) diff --git a/src/TNL/Config/ParameterContainer.h b/src/TNL/Config/ParameterContainer.h index 734db27f51..79a50e1510 100644 --- a/src/TNL/Config/ParameterContainer.h +++ b/src/TNL/Config/ParameterContainer.h @@ -82,7 +82,7 @@ public: } /** - * \brief Checks whether the parameter \e name already exists in ParameterContainer. + * \brief Checks if the ParameterContainer contains a parameter specified by its name. * * \param name Name of the parameter. */ @@ -95,6 +95,19 @@ public: return false; } + /** + * \brief Checks whether the ParameterContainer contains all specified parameter names. + * + * \param name Name of the parameter. + */ + bool checkParameters( std::initializer_list< String > names ) const + { + for( auto name : names ) + if( ! checkParameter( name ) ) + return false; + return true; + } + /** * \brief Assigns new \e value to the parameter \e name. * diff --git a/src/Tools/tnl-diff.cpp b/src/Tools/tnl-diff.cpp index 4614738154..7e442c982f 100644 --- a/src/Tools/tnl-diff.cpp +++ b/src/Tools/tnl-diff.cpp @@ -23,10 +23,10 @@ void setupConfig( Config::ConfigDescription& config ) config.addEntryEnum< String >( "halves" ); config.addEntry< bool >( "exact-match", "Check if the data are exactly the same.", false ); config.addEntry< bool >( "write-difference", "Write difference grid function.", false ); - config.addEntry< bool >( "write-exact-curve", "Write exact curve with given radius.", false ); +// config.addEntry< bool >( "write-exact-curve", "Write exact curve with given radius.", false ); config.addEntry< int >( "edges-skip", "Width of the edges that will be skipped - not included into the error norms.", 0 ); - config.addEntry< bool >( "write-graph", "Draws a graph in the Gnuplot format of the dependence of the error norm on t.", true ); - config.addEntry< bool >( "write-log-graph", "Draws a logarithmic graph in the Gnuplot format of the dependence of the error norm on t.", true ); +// config.addEntry< bool >( "write-graph", "Draws a graph in the Gnuplot format of the dependence of the error norm on t.", true ); +// config.addEntry< bool >( "write-log-graph", "Draws a logarithmic graph in the Gnuplot format of the dependence of the error norm on t.", true ); config.addEntry< double >( "snapshot-period", "The period between consecutive snapshots.", 0.0 ); config.addEntry< bool >( "verbose", "Sets verbosity.", true ); } diff --git a/src/Tools/tnl-grid-setup.cpp b/src/Tools/tnl-grid-setup.cpp index 1846dc0935..261f1cf716 100644 --- a/src/Tools/tnl-grid-setup.cpp +++ b/src/Tools/tnl-grid-setup.cpp @@ -15,11 +15,14 @@ void configSetup( Config::ConfigDescription& config ) { config.addDelimiter( "General parameters" ); config.addEntry < String >( "output-file", "Output file.", "mesh.tnl" ); - config.addEntry < int > ( "verbose", "Set the verbosity of the program.", 1 ); + config.addEntry < int > ( "verbose", "Set the verbosity of the program.", 1 ); - config.addDelimiter ( "Grid parameters" ); + config.addDelimiter ( "Grid parameters" ); config.addEntry < String >( "grid-name", "The grid name.", "tnl-grid" ); - config.addRequiredEntry< int > ( "dimensions", "The grid dimensions." ); + config.addRequiredEntry< int > ( "dimension", "The grid dimension." ); + config.addEntryEnum< int >( 1 ); + config.addEntryEnum< int >( 2 ); + config.addEntryEnum< int >( 3 ); config.addEntry < String >( "real-type", "Precision of the real type describing the grid.", "double" ); config.addEntryEnum < String >( "float" ); config.addEntryEnum < String >( "double" ); @@ -27,16 +30,16 @@ void configSetup( Config::ConfigDescription& config ) config.addEntry < String >( "index-type", "Type for the indexing of the grid elements." ,"int" ); config.addEntryEnum < String >( "int" ); config.addEntryEnum < String >( "long-int" ); - config.addEntry < double > ( "origin-x", "The x-coordinate of the origin.", 0.0 ); - config.addEntry < double > ( "origin-y", "The y-coordinate of the origin.", 0.0 ); - config.addEntry < double > ( "origin-z", "The z-coordinate of the origin.", 0.0 ); - config.addEntry < double > ( "proportions-x", "The proportions of the grid along the x axis.", 1.0 ); - config.addEntry < double > ( "proportions-y", "The proportions of the grid along the y axis.", 1.0 ); - config.addEntry < double > ( "proportions-z", "The proportions of the grid along the z axis.", 1.0 ); - config.addEntry < int > ( "size-x", "Number of elements along the x axis." ); - 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 < double >( "origin-x", "The x-coordinate of the origin.", 0.0 ); + config.addEntry < double >( "origin-y", "The y-coordinate of the origin.", 0.0 ); + config.addEntry < double >( "origin-z", "The z-coordinate of the origin.", 0.0 ); + config.addEntry < double >( "proportions-x", "The proportions of the grid along the x axis.", 1.0 ); + config.addEntry < double >( "proportions-y", "The proportions of the grid along the y axis.", 1.0 ); + config.addEntry < double >( "proportions-z", "The proportions of the grid along the z axis.", 1.0 ); + config.addEntry < int > ( "size-x", "Number of elements along the x axis." ); + 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 ); } int main( int argc, char* argv[] ) @@ -50,5 +53,3 @@ int main( int argc, char* argv[] ) return EXIT_FAILURE; return EXIT_SUCCESS; } - - diff --git a/src/Tools/tnl-grid-setup.h b/src/Tools/tnl-grid-setup.h index 896d3abb97..867cfdb73b 100644 --- a/src/Tools/tnl-grid-setup.h +++ b/src/Tools/tnl-grid-setup.h @@ -19,14 +19,18 @@ using namespace TNL; template< typename RealType, typename IndexType > bool setupGrid( const Config::ParameterContainer& parameters ) { - String gridName = parameters. getParameter< String >( "grid-name" ); - String outputFile = parameters. getParameter< String >( "output-file" ); - int dimensions = parameters. getParameter< int >( "dimensions" ); - if( dimensions == 1 ) + const String gridName = parameters.getParameter< String >( "grid-name" ); + const String outputFile = parameters.getParameter< String >( "output-file" ); + const int dimension = parameters.getParameter< int >( "dimension" ); + if( dimension == 1 ) { - RealType originX = parameters. getParameter< double >( "origin-x" ); - RealType proportionsX = parameters. getParameter< double >( "proportions-x" ); - IndexType sizeX = parameters. getParameter< int >( "size-x" ); + RealType originX = parameters.getParameter< double >( "origin-x" ); + RealType proportionsX = parameters.getParameter< double >( "proportions-x" ); + if( ! parameters.checkParameter( "size-x" ) ) { + std::cerr << "The parameter size-x is required when the grid dimension is 1." << std::endl; + return false; + } + IndexType sizeX = parameters.getParameter< int >( "size-x" ); typedef Meshes::Grid< 1, RealType, Devices::Host, IndexType > GridType; typedef typename GridType::PointType PointType; @@ -46,12 +50,16 @@ bool setupGrid( const Config::ParameterContainer& parameters ) return false; } } - if( dimensions == 2 ) + if( dimension == 2 ) { RealType originX = parameters.getParameter< double >( "origin-x" ); RealType originY = parameters.getParameter< double >( "origin-y" ); RealType proportionsX = parameters.getParameter< double >( "proportions-x" ); RealType proportionsY = parameters.getParameter< double >( "proportions-y" ); + if( ! parameters.checkParameters( {"size-x", "size-y"} ) ) { + std::cerr << "The parameters size-x and size-y are required when the grid dimension is 2." << std::endl; + return false; + } IndexType sizeX = parameters.getParameter< int >( "size-x" ); IndexType sizeY = parameters.getParameter< int >( "size-y" ); typedef Meshes::Grid< 2, RealType, Devices::Host, IndexType > GridType; @@ -84,17 +92,21 @@ bool setupGrid( const Config::ParameterContainer& parameters ) return false; } } - if( dimensions == 3 ) + if( dimension == 3 ) { - RealType originX = parameters. getParameter< double >( "origin-x" ); - RealType originY = parameters. getParameter< double >( "origin-y" ); - RealType originZ = parameters. getParameter< double >( "origin-z" ); - RealType proportionsX = parameters. getParameter< double >( "proportions-x" ); - RealType proportionsY = parameters. getParameter< double >( "proportions-y" ); - RealType proportionsZ = parameters. getParameter< double >( "proportions-z" ); - IndexType sizeX = parameters. getParameter< int >( "size-x" ); - IndexType sizeY = parameters. getParameter< int >( "size-y" ); - IndexType sizeZ = parameters. getParameter< int >( "size-z" ); + RealType originX = parameters.getParameter< double >( "origin-x" ); + RealType originY = parameters.getParameter< double >( "origin-y" ); + RealType originZ = parameters.getParameter< double >( "origin-z" ); + RealType proportionsX = parameters.getParameter< double >( "proportions-x" ); + RealType proportionsY = parameters.getParameter< double >( "proportions-y" ); + RealType proportionsZ = parameters.getParameter< double >( "proportions-z" ); + if( ! parameters.checkParameters( {"size-x", "size-y", "size-z"} ) ) { + std::cerr << "The parameters size-x, size-y and size-z are required when the grid dimension is 3." << std::endl; + return false; + } + IndexType sizeX = parameters.getParameter< int >( "size-x" ); + IndexType sizeY = parameters.getParameter< int >( "size-y" ); + IndexType sizeZ = parameters.getParameter< int >( "size-z" ); typedef Meshes::Grid< 3, RealType, Devices::Host, IndexType > GridType; typedef typename GridType::PointType PointType; @@ -115,7 +127,7 @@ bool setupGrid( const Config::ParameterContainer& parameters ) } std::cout << "Setting dimensions to ... " << grid.getDimensions() << std::endl; std::cout << "Writing the grid to the file " << outputFile << " .... "; - + try { grid.save( outputFile ); @@ -126,34 +138,34 @@ bool setupGrid( const Config::ParameterContainer& parameters ) return false; } } - std::cout << "[ OK ] " << std::endl; + std::cout << "[ OK ] " << std::endl; return true; } template< typename RealType > bool resolveIndexType( const Config::ParameterContainer& parameters ) { - const String& indexType = parameters. getParameter< String >( "index-type" ); - std::cout << "Setting index type to ... " << indexType << std::endl; + const String& indexType = parameters.getParameter< String >( "index-type" ); + std::cout << "Setting index type to ... " << indexType << std::endl; if( indexType == "int" ) return setupGrid< RealType, int >( parameters ); if( indexType == "long-int" ) return setupGrid< RealType, long int >( parameters ); - std::cerr << "The index type '" << indexType << "' is not defined. " << std::endl; + std::cerr << "The index type '" << indexType << "' is not defined." << std::endl; return false; } bool resolveRealType( const Config::ParameterContainer& parameters ) { - String realType = parameters. getParameter< String >( "real-type" ); - std::cout << "Setting real type to ... " << realType << std::endl; + String realType = parameters.getParameter< String >( "real-type" ); + std::cout << "Setting real type to ... " << realType << std::endl; if( realType == "float" ) return resolveIndexType< float >( parameters ); if( realType == "double" ) return resolveIndexType< double >( parameters ); if( realType == "long-double" ) return resolveIndexType< long double >( parameters ); - std::cerr << "The real type '" << realType << "' is not supported. " << std::endl; + std::cerr << "The real type '" << realType << "' is not supported." << std::endl; return false; } diff --git a/src/Tools/tnl-view.cpp b/src/Tools/tnl-view.cpp index f426b6fe0c..32fac87d96 100644 --- a/src/Tools/tnl-view.cpp +++ b/src/Tools/tnl-view.cpp @@ -64,22 +64,22 @@ template< typename GlobalIndex > struct MeshIdTag< TNLViewBuildConfigTag, Global void setupConfig( Config::ConfigDescription& config ) { - config.addDelimiter ( "General settings:" ); - config.addEntry < String > ( "mesh", "Mesh file.", "mesh.tnl" ); - config.addRequiredList < String > ( "input-files", "Input files." ); - config.addList < String > ( "output-files", "Output files." ); - config.addEntry < bool > ( "check-output-file", "If the output file already exists, do not recreate it.", false ); + config.addDelimiter( "General settings:" ); + config.addEntry < String >( "mesh", "Mesh file.", "mesh.tnl" ); + config.addRequiredList < String >( "input-files", "Input files." ); +// config.addList < String >( "output-files", "Output files." ); + config.addEntry < bool > ( "check-output-file", "If the output file already exists, do not recreate it.", false ); config.addDelimiter( "Grid settings:"); - config.addList < double > ( "level-lines", "List of level sets which will be drawn." ); - config.addEntry < int > ( "output-x-size", "X size of the output." ); - config.addEntry < int > ( "output-y-size", "Y size of the output." ); - config.addEntry < int > ( "output-z-size", "Z size of the output." ); - config.addEntry < double > ( "scale", "Multiply the function by given number.", 1.0 ); - config.addEntry < String > ( "output-format", "Output file format.", "gnuplot" ); - config.addEntryEnum < String > ( "gnuplot" ); - config.addEntryEnum < String > ( "vtk" ); - config.addEntry < int > ( "verbose", "Set the verbosity of the program.", 1 ); +// config.addList < double >( "level-lines", "List of level sets which will be drawn." ); +// config.addEntry < int > ( "output-x-size", "X size of the output." ); +// config.addEntry < int > ( "output-y-size", "Y size of the output." ); +// config.addEntry < int > ( "output-z-size", "Z size of the output." ); + config.addEntry < double >( "scale", "Multiply the function by given number.", 1.0 ); + config.addEntry < String >( "output-format", "Output file format.", "gnuplot" ); + config.addEntryEnum< String > ( "gnuplot" ); + config.addEntryEnum< String > ( "vtk" ); + config.addEntry < int > ( "verbose", "Set the verbosity of the program.", 1 ); } int main( int argc, char* argv[] ) @@ -90,10 +90,9 @@ int main( int argc, char* argv[] ) if( ! parseCommandLine( argc, argv, conf_desc, parameters ) ) return EXIT_FAILURE; - String meshFile = parameters.getParameter< String >( "mesh" ); + const String meshFile = parameters.getParameter< String >( "mesh" ); return ! TNL::Meshes::resolveMeshType< TNLViewBuildConfigTag, Devices::Host, FilesProcessor > - ( meshFile, - parameters ); + ( meshFile, parameters ); } diff --git a/src/Tools/tnl-view.h b/src/Tools/tnl-view.h index 7e7b82bbfc..fd7bdd8c47 100644 --- a/src/Tools/tnl-view.h +++ b/src/Tools/tnl-view.h @@ -415,7 +415,7 @@ struct FilesProcessor } bool checkOutputFile = parameters. getParameter< bool >( "check-output-file" ); - std::vector< String > inputFiles = parameters. getParameter< std::vector< String > >( "input-files" ); + std::vector< String > inputFiles = parameters.getParameter< std::vector< String > >( "input-files" ); bool error( false ); //#ifdef HAVE_OPENMP //#pragma omp parallel for -- GitLab