diff --git a/dumux/common/parameterparser.hh b/dumux/common/parameterparser.hh index d1ef50723bddebfb96d7f5cc0bae17a0727d6877..1b26150d85407db701d105d4e6677dc5a2d3352f 100644 --- a/dumux/common/parameterparser.hh +++ b/dumux/common/parameterparser.hh @@ -42,7 +42,6 @@ namespace Dumux * \ingroup Start * \brief Parses parameters in the command line and input files */ -template<class TypeTag> class ParameterParser { @@ -83,32 +82,25 @@ public: } /*! - * \brief Parse the input file. If the user didn't specify anything in the parameter tree (that contains - * command line options if parseCommandLineArguments was called before) we default to the - * program name + input. When calling this function we consider it an error if no input file is found. + * \brief Parse the input file. + Throws an error if no input file is found. * - * \param argc The 'argc' argument of the main function: count of arguments (1 if there are no arguments) - * \param argv The 'argv' argument of the main function: array of pointers to the argument strings - * \param params A parameter tree. It can be filled from an input file or the command line. - * \param usage Callback function for printing the usage message + * \param argc The 'argc' argument of the main function: count of arguments (1 if there are no arguments) + * \param argv The 'argv' argument of the main function: array of pointers to the argument strings + * \param params A parameter tree. It can be filled from an input file or the command line. + * \param usage Optional callback function for printing the usage message + * \param parameterFileName Optional name of the input file. If empty we default to program name + ".input" */ static void parseInputFile(int argc, char **argv, Dune::ParameterTree ¶ms, + std::string parameterFileName = "", void (*usage)(const char *, const std::string &) = [](const char *, const std::string &){}) { const auto& mpiHelper = Dune::MPIHelper::instance(argc, argv); - std::string parameterFileName = ""; - - // check the parameter tree for a user specified input file - if (params.hasKey("ParameterFile")) - // this is the only reason why this class needs a TypeTag -- sigh - // if the parameter tree is used directly this appears in the unused properties list - parameterFileName = GET_RUNTIME_PARAM(TypeTag, std::string, ParameterFile); - - // otherwise use the default - else + // if no parameter file was specified use the default + if (parameterFileName == "") { if (mpiHelper.size() > 1) std::cout << "Rank " << mpiHelper.rank() << ": "; diff --git a/dumux/common/start.hh b/dumux/common/start.hh index e2e1e20bbeb14ec3233ee65eb233d600894880e1..9d70e83f9e4c8246f4f85555761e810f224fbd37 100644 --- a/dumux/common/start.hh +++ b/dumux/common/start.hh @@ -83,14 +83,15 @@ int start_(int argc, //////////////////////////////////////////////////////////// // if the user just wanted to see the help / usage message show usage and stop program - if(!ParameterParser<TypeTag>::parseCommandLineArguments(argc, argv, ParameterTree::tree(), usage)) + if(!ParameterParser::parseCommandLineArguments(argc, argv, ParameterTree::tree(), usage)) { usage(argv[0], defaultUsageMessage(argv[0])); return 0; } // parse the input file into the parameter tree - ParameterParser<TypeTag>::parseInputFile(argc, argv, ParameterTree::tree(), usage); - + // check first if the user provided an input file through the command line, if not use the default + const auto parameterFileName = ParameterTree::tree().hasKey("ParameterFile") ? GET_RUNTIME_PARAM(TypeTag, std::string, ParameterFile) : ""; + ParameterParser::parseInputFile(argc, argv, ParameterTree::tree(), parameterFileName, usage); //////////////////////////////////////////////////////////// // check for some user debugging parameters diff --git a/test/common/generalproblem/test_generalproblem2p.cc b/test/common/generalproblem/test_generalproblem2p.cc index db14ff80025704a41c94f4bc3a481b622f7d20ac..f69f4d6fbf8f5dc040df4ddc0d1911b7ec59435c 100644 --- a/test/common/generalproblem/test_generalproblem2p.cc +++ b/test/common/generalproblem/test_generalproblem2p.cc @@ -23,8 +23,11 @@ */ #include <config.h> -#include "generallensproblem.hh" #include <dumux/common/start.hh> +#include <dumux/common/defaultusagemessage.hh> +#include <dumux/common/parameterparser.hh> + +#include "generallensproblem.hh" //////////////////////// // the main function @@ -50,36 +53,59 @@ void usage(const char *progName, const std::string &errorMsg) int main(int argc, char** argv) { - Dune::ParameterTree paramTree; - std::string s(Dumux::readOptions_(argc, argv, paramTree)); - if (s.empty()) // everything was read correctly - { + using namespace Dumux; + + try { + + Dune::ParameterTree paramTree; + // if the user just wanted to see the help / usage message show usage and stop program + if(!ParameterParser::parseCommandLineArguments(argc, argv, paramTree, usage)) + { + usage(argv[0], defaultUsageMessage(argv[0])); + return 0; + } + // default model type is box const std::string modelType(paramTree.get<std::string>("ModelType", "box")); if (modelType == "box") { - typedef TTAG(BoxGeneralLensProblem) ProblemTypeTag; + using ProblemTypeTag = TTAG(BoxGeneralLensProblem); + // avoid unused parameter message GET_PROP(ProblemTypeTag, ParameterTree)::runTimeParams()["ModelType"] = "box"; - return Dumux::start<ProblemTypeTag>(argc, argv, usage); + return start<ProblemTypeTag>(argc, argv, usage); } else if (modelType == "cc") { - typedef TTAG(CCGeneralLensProblem) ProblemTypeTag; - return Dumux::start<ProblemTypeTag>(argc, argv, usage); + using ProblemTypeTag = TTAG(CCGeneralLensProblem); + // avoid unused parameter message + GET_PROP(ProblemTypeTag, ParameterTree)::runTimeParams()["ModelType"] = "cc"; + return start<ProblemTypeTag>(argc, argv, usage); } else if (modelType == "sequential") { - typedef TTAG(SequentialGeneralLensProblem) ProblemTypeTag; - return Dumux::start<ProblemTypeTag>(argc, argv, usage); + using ProblemTypeTag = TTAG(SequentialGeneralLensProblem); + // avoid unused parameter message + GET_PROP(ProblemTypeTag, ParameterTree)::runTimeParams()["ModelType"] = "sequential"; + return start<ProblemTypeTag>(argc, argv, usage); } else { - Dumux::ParameterException e("Unknown ModelType: " + modelType); - std::cerr << e << ". Abort!" << std::endl + std::cerr << ParameterException("Unknown ModelType: " + modelType) << ". Abort!" << std::endl << "ModelType can be: box (2p box model), cc (2p cc model), sequential (2p impes model)" << std::endl; - exit(1); + return 1; } } - else - DUNE_THROW(Dumux::ParameterException, "Unknown command line option " << s); -} + catch (ParameterException &e) { + std::cerr << std::endl << e << ". Abort!" << std::endl; + return 1; + } + catch (Dune::Exception &e) { + std::cerr << "Dune reported error: " << e << std::endl; + return 3; + } + catch (...) { + std::cerr << "Unknown exception thrown!\n"; + return 4; + } + +} // end main diff --git a/test/porousmediumflow/1p/sequential/test_diffusion3d.cc b/test/porousmediumflow/1p/sequential/test_diffusion3d.cc index 811f9191cfd7d0c99c3166cb04ddc9fc3f5e760f..0ae5d30ec37a7e279f8de14153bba03cc519504a 100644 --- a/test/porousmediumflow/1p/sequential/test_diffusion3d.cc +++ b/test/porousmediumflow/1p/sequential/test_diffusion3d.cc @@ -32,11 +32,209 @@ #include <dune/common/parametertreeparser.hh> #include <dune/grid/common/gridinfo.hh> -#include <dumux/common/start.hh> +#include <dumux/common/dumuxmessage.hh> +#include <dumux/common/defaultusagemessage.hh> +#include <dumux/common/parameterparser.hh> #include "test_diffusionproblem3d.hh" #include "resultevaluation3d.hh" +namespace Dumux +{ +// forward declaration of property tags +namespace Properties +{ +NEW_PROP_TAG(GridCreator); +NEW_PROP_TAG(Problem); +} + +/*! + * \ingroup Start + * + * \brief Provides a main function which reads in parameters from the + * command line and a parameter file. + * + * \param argc The 'argc' argument of the main function: count of arguments (1 if there are no arguments) + * \param argv The 'argv' argument of the main function: array of pointers to the argument strings + * \param usage Callback function for printing the usage message + */ +int start(int argc, + char **argv, + void (*usage)(const char *, const std::string &)) +{ + // initialize MPI, finalize is done automatically on exit + const auto& mpiHelper = Dune::MPIHelper::instance(argc, argv); + + // print dumux start message + if (mpiHelper.rank() == 0) + DumuxMessage::print(/*firstCall=*/true); + + //////////////////////////////////////////////////////////// + // parse the command line arguments and input file + //////////////////////////////////////////////////////////// + + using TypeTag = TTAG(DiffusionTestProblem); + using ParameterTree = typename GET_PROP(TypeTag, ParameterTree); + + // if the user just wanted to see the help / usage message show usage and stop program + if(!ParameterParser::parseCommandLineArguments(argc, argv, ParameterTree::tree(), usage)) + { + usage(argv[0], defaultUsageMessage(argv[0])); + return 0; + } + // parse the input file into the parameter tree + // check first if the user provided an input file through the command line, if not use the default + const auto parameterFileName = ParameterTree::tree().hasKey("ParameterFile") ? GET_RUNTIME_PARAM(TypeTag, std::string, ParameterFile) : ""; + ParameterParser::parseInputFile(argc, argv, ParameterTree::tree(), parameterFileName, usage); + + //////////////////////////////////////////////////////////// + // get some optional parameters + //////////////////////////////////////////////////////////// + const int numRefine = ParameterTree::tree().hasKey("Grid.Refinement") ? + GET_RUNTIME_PARAM(TypeTag, int, Grid.Refinement) : 0; + + std::string outputName(""); + if (ParameterTree::tree().hasKey("Problem.OutputName")) + { + outputName += "_"; + outputName += GET_RUNTIME_PARAM(TypeTag, std::string, OutputName); + } + + ////////////////////////////////////////////////////////////////////// + // try to create a grid (from the given grid file or the input file) + ///////////////////////////////////////////////////////////////////// + + using GridCreator = typename GET_PROP_TYPE(TypeTag, GridCreator); + try { GridCreator::makeGrid(); } + catch (...) { + std::string usageMessage = "\n\t -> Creation of the grid failed! <- \n\n"; + usageMessage += defaultUsageMessage(argv[0]); + usage(argv[0], usageMessage); + throw; + } + GridCreator::loadBalance(); + + // print grid info + auto& grid = GridCreator::grid(); + Dune::gridinfo(grid); + + ////////////////////////////////////////////////////////////////////// + // run the simulation + ///////////////////////////////////////////////////////////////////// + + Dune::Timer timer; + bool consecutiveNumbering = true; + + ////////////////////////////////////////////////////////////////////// + // finite volume TPFA test problem + ///////////////////////////////////////////////////////////////////// + + using FVTypeTag = TTAG(FVTestProblem); + using FVProblem = GET_PROP_TYPE(FVTypeTag, Problem); + using FVParameterTree = GET_PROP(FVTypeTag, ParameterTree); + ParameterParser::parseInputFile(argc, argv, FVParameterTree::tree(), parameterFileName, usage); + + std::shared_ptr<FVProblem> fvProblem = std::make_shared<FVProblem>(grid.leafGridView()); + // set output name + std::string fvOutput = "test_diffusion3d_fv" + outputName; + if (numRefine > 0) + fvOutput += "_numRefine" + std::to_string(numRefine); + fvProblem->setName(fvOutput.c_str()); + + timer.reset(); + + fvProblem->init(); + fvProblem->calculateFVVelocity(); + + auto fvTime = timer.elapsed(); + + fvProblem->writeOutput(); + + Dumux::ResultEvaluation fvResult; + fvResult.evaluate(grid.leafGridView(), *fvProblem, consecutiveNumbering); + + ////////////////////////////////////////////////////////////////////// + // finite volume MPFA-L test problem + ///////////////////////////////////////////////////////////////////// + + using MPFALTypeTag = TTAG(FVMPFAL3DTestProblem); + using MPFALProblem = GET_PROP_TYPE(MPFALTypeTag, Problem); + using MPFALParameterTree = GET_PROP(MPFALTypeTag, ParameterTree); + ParameterParser::parseInputFile(argc, argv, MPFALParameterTree::tree(), parameterFileName, usage); + + std::shared_ptr<MPFALProblem> mpfaProblem = std::make_shared<MPFALProblem>(grid.leafGridView()); + // set output name + std::string fvmpfaOutput = "test_diffusion3d_fvmpfal" + outputName; + if (numRefine > 0) + fvmpfaOutput += "_numRefine" + std::to_string(numRefine); + mpfaProblem->setName(fvmpfaOutput.c_str()); + + timer.reset(); + + mpfaProblem->init(); + mpfaProblem->calculateFVVelocity(); + + auto mpfaTime = timer.elapsed(); + + mpfaProblem->writeOutput(); + + Dumux::ResultEvaluation mpfaResult; + mpfaResult.evaluate(grid.leafGridView(), *mpfaProblem, consecutiveNumbering); + + ////////////////////////////////////////////////////////////////////// + // mimetic finite difference test problem + ///////////////////////////////////////////////////////////////////// + + using MimeticTypeTag = TTAG(MimeticTestProblem); + using MimeticProblem = GET_PROP_TYPE(MimeticTypeTag, Problem); + using MimeticParameterTree = GET_PROP(MimeticTypeTag, ParameterTree); + ParameterParser::parseInputFile(argc, argv, MimeticParameterTree::tree(), parameterFileName, usage); + + std::shared_ptr<MimeticProblem> mimeticProblem = std::make_shared<MimeticProblem>(grid.leafGridView()); + // set output name + std::string mimeticOutput = "test_diffusion3d_mimetic" + outputName; + if (numRefine > 0) + mimeticOutput += "_numRefine" + std::to_string(numRefine); + mimeticProblem->setName(mimeticOutput.c_str()); + + timer.reset(); + + mimeticProblem->init(); + mimeticProblem->calculateFVVelocity(); + + auto mimeticTime = timer.elapsed(); + + mimeticProblem->writeOutput(); + + Dumux::ResultEvaluation mimeticResult; + mimeticResult.evaluate(grid.leafGridView(), *mimeticProblem, consecutiveNumbering); + + ////////////////////////////////////////////////////////////////////// + // print results to command line + ///////////////////////////////////////////////////////////////////// + + std::cout.setf(std::ios_base::scientific, std::ios_base::floatfield); + std::cout.precision(2); + std::cout << "\t\t pErrorL2 \t pInnerErrorL2 \t vErrorL2 \t vInnerErrorL2 \t hMax \t\t pMin\t\t pMax\t\t pMinExact\t pMaxExact\t time" + << std::endl; + std::cout << "2pfa\t\t " << fvResult.relativeL2Error << "\t " << fvResult.relativeL2ErrorIn << "\t " + << fvResult.ervell2 << "\t " << fvResult.ervell2In << "\t " << fvResult.hMax << "\t " << fvResult.uMin + << "\t " << fvResult.uMax << "\t " << fvResult.uMinExact << "\t " << fvResult.uMaxExact << "\t " + << fvTime << std::endl; + std::cout << "mpfa-l\t " << mpfaResult.relativeL2Error << "\t " << mpfaResult.relativeL2ErrorIn << "\t " + << mpfaResult.ervell2 << "\t " << mpfaResult.ervell2In << "\t " << mpfaResult.hMax << "\t " + << mpfaResult.uMin << "\t " << mpfaResult.uMax << "\t " << mpfaResult.uMinExact << "\t " + << mpfaResult.uMaxExact << "\t " << mpfaTime << std::endl; + std::cout << "mimetic\t " << mimeticResult.relativeL2Error << "\t " << mimeticResult.relativeL2ErrorIn << "\t " + << mimeticResult.ervell2 << "\t " << mimeticResult.ervell2In << "\t " << mimeticResult.hMax << "\t " + << mimeticResult.uMin << "\t " << mimeticResult.uMax << "\t " << mimeticResult.uMinExact << "\t " + << mimeticResult.uMaxExact << "\t " << mimeticTime << std::endl; + + return 0; +} + +} // end namespace Dumux + /*! * \brief Provides an interface for customizing error messages associated with * reading in parameters. @@ -63,166 +261,28 @@ void usage(const char *progName, const std::string &errorMsg) int main(int argc, char** argv) { try { - typedef TTAG(DiffusionTestProblem) TypeTag; - typedef GET_PROP_TYPE(TypeTag, Grid) Grid; - typedef GET_PROP(TypeTag, ParameterTree) ParameterTree; - - // initialize MPI, finalize is done automatically on exit - Dune::MPIHelper::instance(argc, argv); - - // fill the parameter tree with the options from the command line - std::string s = Dumux::readOptions_(argc, argv, ParameterTree::tree()); - if (!s.empty()) { - usage(argv[0], s); - return 1; - } - - // obtain the name of the parameter file - std::string parameterFileName; - if (ParameterTree::tree().hasKey("ParameterFile")) - { - // set the name to the one provided by the user - parameterFileName = GET_RUNTIME_PARAM(TypeTag, std::string, ParameterFile); - } - else // otherwise we read from the command line - { - // set the name to the default ./<programname>.input - parameterFileName = argv[0]; - parameterFileName += ".input"; - } - - Dune::ParameterTreeParser::readINITree(parameterFileName, - ParameterTree::tree(), - /*overwrite=*/false); - - int numRefine = 0; - if (ParameterTree::tree().hasKey("Grid.RefinementRatio")) - numRefine = GET_RUNTIME_PARAM_FROM_GROUP(TypeTag, int, Grid, RefinementRatio); - - std::string outputName(""); - if (ParameterTree::tree().hasKey("Problem.OutputName")) - { - outputName += "_"; - outputName += GET_RUNTIME_PARAM(TypeTag, std::string, OutputName); - } - - //////////////////////////////////////////////////////////// - // create the grid - //////////////////////////////////////////////////////////// - std::string gridFileName = GET_RUNTIME_PARAM_FROM_GROUP(TypeTag, std::string, Grid, File); - Dune::GridPtr < Grid > grid(gridFileName); - grid->globalRefine(numRefine); - - Dune::gridinfo (*grid); - - //////////////////////////////////////////////////////////// - // instantiate and run the concrete problem - //////////////////////////////////////////////////////////// - Dune::Timer timer; - bool consecutiveNumbering = true; - - typedef TTAG (FVTestProblem) - FVTypeTag; - typedef GET_PROP_TYPE(FVTypeTag, Problem) FVProblem; - typedef GET_PROP(FVTypeTag, ParameterTree) FVParameterTree; - Dune::ParameterTreeParser::readINITree(parameterFileName, FVParameterTree::tree()); - FVProblem *fvProblem = new FVProblem(grid->leafGridView()); - - std::string fvOutput("test_diffusion3d_fv"); - fvOutput += outputName; - if (numRefine > 0) - { - char refine[128]; - sprintf(refine, "_numRefine%d", numRefine); - fvOutput += refine; - } - fvProblem->setName(fvOutput.c_str()); - timer.reset(); - fvProblem->init(); - fvProblem->calculateFVVelocity(); - double fvTime = timer.elapsed(); - fvProblem->writeOutput(); - Dumux::ResultEvaluation fvResult; - fvResult.evaluate(grid->leafGridView(), *fvProblem, consecutiveNumbering); - delete fvProblem; - - typedef TTAG (FVMPFAL3DTestProblem) - MPFALTypeTag; - typedef GET_PROP_TYPE(MPFALTypeTag, Problem) MPFALProblem; - typedef GET_PROP(MPFALTypeTag, ParameterTree) MPFALParameterTree; - Dune::ParameterTreeParser::readINITree(parameterFileName, MPFALParameterTree::tree()); - MPFALProblem *mpfaProblem = new MPFALProblem(grid->leafGridView()); - - std::string fvmpfaOutput("test_diffusion3d_fvmpfal"); - fvmpfaOutput += outputName; - if (numRefine > 0) - { - char refine[128]; - sprintf(refine, "_numRefine%d", numRefine); - fvmpfaOutput += refine; - } - mpfaProblem->setName(fvmpfaOutput.c_str()); - timer.reset(); - mpfaProblem->init(); - double mpfaTime = timer.elapsed(); - mpfaProblem->writeOutput(); - Dumux::ResultEvaluation mpfaResult; - mpfaResult.evaluate(grid->leafGridView(), *mpfaProblem, consecutiveNumbering); - delete mpfaProblem; - - typedef TTAG (MimeticTestProblem) - MimeticTypeTag; - typedef GET_PROP_TYPE(MimeticTypeTag, Problem) MimeticProblem; - typedef GET_PROP(MimeticTypeTag, ParameterTree) MimeticParameterTree; - Dune::ParameterTreeParser::readINITree(parameterFileName, MimeticParameterTree::tree()); - MimeticProblem *mimeticProblem = new MimeticProblem(grid->leafGridView()); - - std::string mimeticOutput("test_diffusion3d_mimetic"); - mimeticOutput += outputName; - if (numRefine > 0) - { - char refine[128]; - sprintf(refine, "_numRefine%d", numRefine); - mimeticOutput += refine; - } - mimeticProblem->setName(mimeticOutput.c_str()); - timer.reset(); - mimeticProblem->init(); - double mimeticTime = timer.elapsed(); - mimeticProblem->writeOutput(); - Dumux::ResultEvaluation mimeticResult; - mimeticResult.evaluate(grid->leafGridView(), *mimeticProblem, consecutiveNumbering); - delete mimeticProblem; - - std::cout.setf(std::ios_base::scientific, std::ios_base::floatfield); - std::cout.precision(2); - std::cout - << "\t pErrorL2 \t pInnerErrorL2 \t vErrorL2 \t vInnerErrorL2 \t hMax \t\t pMin\t\t pMax\t\t pMinExact\t pMaxExact\t time" - << std::endl; - std::cout << "2pfa\t " << fvResult.relativeL2Error << "\t " << fvResult.relativeL2ErrorIn << "\t " - << fvResult.ervell2 << "\t " << fvResult.ervell2In << "\t " << fvResult.hMax << "\t " << fvResult.uMin - << "\t " << fvResult.uMax << "\t " << fvResult.uMinExact << "\t " << fvResult.uMaxExact << "\t " - << fvTime << std::endl; - std::cout << "mpfa-l\t " << mpfaResult.relativeL2Error << "\t " << mpfaResult.relativeL2ErrorIn << "\t " - << mpfaResult.ervell2 << "\t " << mpfaResult.ervell2In << "\t " << mpfaResult.hMax << "\t " - << mpfaResult.uMin << "\t " << mpfaResult.uMax << "\t " << mpfaResult.uMinExact << "\t " - << mpfaResult.uMaxExact << "\t " << mpfaTime << std::endl; - std::cout << "mimetic\t " << mimeticResult.relativeL2Error << "\t " << mimeticResult.relativeL2ErrorIn << "\t " - << mimeticResult.ervell2 << "\t " << mimeticResult.ervell2In << "\t " << mimeticResult.hMax << "\t " - << mimeticResult.uMin << "\t " << mimeticResult.uMax << "\t " << mimeticResult.uMinExact << "\t " - << mimeticResult.uMaxExact << "\t " << mimeticTime << std::endl; - - return 0; - } catch (Dune::Exception &e) - { + return Dumux::start(argc, argv, usage); + } + catch (Dumux::ParameterException &e) { + std::cerr << std::endl << e << ". Abort!" << std::endl; + return 1; + } + catch (Dune::DGFException & e) { + std::cerr << "DGF exception thrown (" << e << + "). Most likely, the DGF file name is wrong " + "or the DGF file is corrupted, " + "e.g. missing hash at end of file or wrong number (dimensions) of entries." + << std::endl; + return 2; + } + catch (Dune::Exception &e) { std::cerr << "Dune reported error: " << e << std::endl; - } catch (...) - { + return 3; + } + catch (...) { std::cerr << "Unknown exception thrown!\n"; - throw; + return 4; } - - return 3; } #else int main() diff --git a/test/porousmediumflow/2p/sequential/test_3d2p.cc b/test/porousmediumflow/2p/sequential/test_3d2p.cc index 404d1f82d77abfb9b23e3434e5a6f270c4d87435..ba5e520b9374238e44e2681db25aba7480082f73 100644 --- a/test/porousmediumflow/2p/sequential/test_3d2p.cc +++ b/test/porousmediumflow/2p/sequential/test_3d2p.cc @@ -25,8 +25,11 @@ #if HAVE_DUNE_ALUGRID -#include "test_3d2pproblem.hh" #include <dumux/common/start.hh> +#include <dumux/common/defaultusagemessage.hh> +#include <dumux/common/parameterparser.hh> + +#include "test_3d2pproblem.hh" //////////////////////// // the main function @@ -62,110 +65,105 @@ void usage(const char *progName, const std::string &errorMsg) int main(int argc, char** argv) { - Dune::ParameterTree paramTree; - std::string s(Dumux::readOptions_(argc, argv, paramTree)); - if (s.empty()) - { - if (paramTree.hasKey("ModelType")) + using namespace Dumux; + + try { + + Dune::ParameterTree paramTree; + // if the user just wanted to see the help / usage message show usage and stop program + if(!ParameterParser::parseCommandLineArguments(argc, argv, paramTree, usage)) { - std::string modelType(paramTree.get<std::string>("ModelType")); + usage(argv[0], defaultUsageMessage(argv[0])); + return 0; + } + + const std::string modelType(paramTree.get<std::string>("ModelType", "MPFAL")); - if (modelType == "FV") - { - typedef TTAG(FVTwoPTestProblem) ProblemTypeTag; - typedef GET_PROP(ProblemTypeTag, ParameterTree) ParamTree; - Dune::ParameterTree &rt = ParamTree::runTimeParams(); - rt["ModelType"]=modelType; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<"Used standard finite volume model\n"; - return startReturn; - } - else if (modelType == "FVAdaptive") - { - typedef TTAG(FVAdaptiveTwoPTestProblem) ProblemTypeTag; - typedef GET_PROP(ProblemTypeTag, ParameterTree) ParamTree; - Dune::ParameterTree &rt = ParamTree::runTimeParams(); - rt["ModelType"]=modelType; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<"Used adaptive finite volume model\n"; - return startReturn; - } - else if (modelType == "MPFAL") - { - typedef TTAG(MPFALTwoPTestProblem) ProblemTypeTag; - typedef GET_PROP(ProblemTypeTag, ParameterTree) ParamTree; - Dune::ParameterTree &rt = ParamTree::runTimeParams(); - rt["ModelType"]=modelType; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<"Used finite volume MPFA l-method model\n"; - return startReturn; - } - else if (modelType == "MPFALAdaptive") - { - typedef TTAG(MPFALAdaptiveTwoPTestProblem) ProblemTypeTag; - typedef GET_PROP(ProblemTypeTag, ParameterTree) ParamTree; - Dune::ParameterTree &rt = ParamTree::runTimeParams(); - rt["ModelType"]=modelType; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<"Used adaptive finite volume MPFA l-method model\n"; - return startReturn; - } + if (modelType == "FV") + { + using ProblemTypeTag = TTAG(FVTwoPTestProblem); + // avoid unused parameter message + GET_PROP(ProblemTypeTag, ParameterTree)::runTimeParams()["ModelType"] = modelType; + std::cout<<"##########################################" << std::endl; + std::cout<<"Standard finite volume TPFA model" << std::endl; + std::cout<<"##########################################" << std::endl; + return start<ProblemTypeTag>(argc, argv, usage); + } + else if (modelType == "FVAdaptive") + { + using ProblemTypeTag = TTAG(FVAdaptiveTwoPTestProblem); + // avoid unused parameter message + GET_PROP(ProblemTypeTag, ParameterTree)::runTimeParams()["ModelType"] = modelType; + std::cout<<"##########################################" << std::endl; + std::cout<<"Adapative finite volume TPFA model" << std::endl; + std::cout<<"##########################################" << std::endl; + return start<ProblemTypeTag>(argc, argv, usage); + } + else if (modelType == "MPFAL") + { + using ProblemTypeTag = TTAG(MPFALTwoPTestProblem); + // avoid unused parameter message + GET_PROP(ProblemTypeTag, ParameterTree)::runTimeParams()["ModelType"] = modelType; + std::cout<<"##########################################" << std::endl; + std::cout<<"Standard finite volume MPFA-L model" << std::endl; + std::cout<<"##########################################" << std::endl; + return start<ProblemTypeTag>(argc, argv, usage); + } + else if (modelType == "MPFALAdaptive") + { + using ProblemTypeTag = TTAG(MPFALAdaptiveTwoPTestProblem); + // avoid unused parameter message + GET_PROP(ProblemTypeTag, ParameterTree)::runTimeParams()["ModelType"] = modelType; + std::cout<<"##########################################" << std::endl; + std::cout<<"Adapative finite volume MPFA-L model" << std::endl; + std::cout<<"##########################################" << std::endl; + return start<ProblemTypeTag>(argc, argv, usage); + } #if PROBLEM != 1 - else if (modelType == "Mimetic") - { - typedef TTAG(MimeticTwoPTestProblem) ProblemTypeTag; - typedef GET_PROP(ProblemTypeTag, ParameterTree) ParamTree; - Dune::ParameterTree &rt = ParamTree::runTimeParams(); - rt["ModelType"]=modelType; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<"Used mimetic finite difference model\n"; - return startReturn; - } - else if (modelType == "MimeticAdaptive") - { - typedef TTAG(MimeticAdaptiveTwoPTestProblem) ProblemTypeTag; - typedef GET_PROP(ProblemTypeTag, ParameterTree) ParamTree; - Dune::ParameterTree &rt = ParamTree::runTimeParams(); - rt["ModelType"]=modelType; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<"Used adaptive mimetic finite difference model\n"; - return startReturn; - } -#endif - else - { - typedef TTAG(MPFALTwoPTestProblem) ProblemTypeTag; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<"Unknwon model type "<<modelType<<" specified\n"; - std::cout<<"Default to finite volume MPFA l-method model\n"; - return startReturn; - } + else if (modelType == "Mimetic") + { + using ProblemTypeTag = TTAG(MimeticTwoPTestProblem); + // avoid unused parameter message + GET_PROP(ProblemTypeTag, ParameterTree)::runTimeParams()["ModelType"] = modelType; + std::cout<<"##########################################" << std::endl; + std::cout<<"Standard mimetic finite difference model" << std::endl; + std::cout<<"##########################################" << std::endl; + return start<ProblemTypeTag>(argc, argv, usage); } + else if (modelType == "MimeticAdaptive") + { + using ProblemTypeTag = TTAG(MimeticAdaptiveTwoPTestProblem); + // avoid unused parameter message + GET_PROP(ProblemTypeTag, ParameterTree)::runTimeParams()["ModelType"] = modelType; + std::cout<<"##########################################" << std::endl; + std::cout<<"Adaptive mimetic finite difference model" << std::endl; + std::cout<<"##########################################" << std::endl; + return start<ProblemTypeTag>(argc, argv, usage); + } +#endif else { - typedef TTAG(MPFALTwoPTestProblem) ProblemTypeTag; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<"No model type specified\n"; - std::cout<<"Default to finite volume MPFA l-method model\n"; - return startReturn; + using ProblemTypeTag = TTAG(MPFALTwoPTestProblem); + // avoid unused parameter message + GET_PROP(ProblemTypeTag, ParameterTree)::runTimeParams()["ModelType"] = modelType; + std::cout<<"##########################################" << std::endl; + std::cout<<"Unknown model type " << modelType << ", default to" << std::endl; + std::cout<<"Standard finite volume MPFA-L model" << std::endl; + std::cout<<"##########################################" << std::endl; + return start<ProblemTypeTag>(argc, argv, usage); } } - else - { - typedef TTAG(MPFALTwoPTestProblem) ProblemTypeTag; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<s<<" is not a valid model type specification!\n"; - std::cout<<"Default to finite volume MPFA l-method model\n"; - return startReturn; + catch (ParameterException &e) { + std::cerr << std::endl << e << ". Abort!" << std::endl; + return 1; + } + catch (Dune::Exception &e) { + std::cerr << "Dune reported error: " << e << std::endl; + return 3; + } + catch (...) { + std::cerr << "Unknown exception thrown!\n"; + return 4; } return 0; diff --git a/test/porousmediumflow/2p/sequential/test_mpfa2p.cc b/test/porousmediumflow/2p/sequential/test_mpfa2p.cc index 01b4cf25e99d29df01ba8c2f811537fd36f37ec4..6dea810ad3459e7a0ed5a575c01d1a9bb300d148 100644 --- a/test/porousmediumflow/2p/sequential/test_mpfa2p.cc +++ b/test/porousmediumflow/2p/sequential/test_mpfa2p.cc @@ -25,8 +25,11 @@ #if HAVE_UG -#include "test_mpfa2pproblem.hh" #include <dumux/common/start.hh> +#include <dumux/common/defaultusagemessage.hh> +#include <dumux/common/parameterparser.hh> + +#include "test_mpfa2pproblem.hh" //////////////////////// // the main function @@ -53,98 +56,97 @@ void usage(const char *progName, const std::string &errorMsg) int main(int argc, char** argv) { - Dune::ParameterTree paramTree; - std::string s(Dumux::readOptions_(argc, argv, paramTree)); - if (s.empty()) - { - if (paramTree.hasKey("ModelType")) + using namespace Dumux; + + try { + + Dune::ParameterTree paramTree; + // if the user just wanted to see the help / usage message show usage and stop program + if(!ParameterParser::parseCommandLineArguments(argc, argv, paramTree, usage)) { - std::string modelType(paramTree.get<std::string>("ModelType")); + usage(argv[0], defaultUsageMessage(argv[0])); + return 0; + } + + const std::string modelType(paramTree.get<std::string>("ModelType", "MPFAL")); - if (modelType == "FV") - { - typedef TTAG(FVTwoPTestProblem) ProblemTypeTag; - typedef GET_PROP(ProblemTypeTag, ParameterTree) ParamTree; - Dune::ParameterTree &rt = ParamTree::runTimeParams(); - rt["ModelType"]=modelType; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<"Used standard finite volume model\n"; - return startReturn; - } - else if (modelType == "FVAdaptive") - { - typedef TTAG(FVAdaptiveTwoPTestProblem) ProblemTypeTag; - typedef GET_PROP(ProblemTypeTag, ParameterTree) ParamTree; - Dune::ParameterTree &rt = ParamTree::runTimeParams(); - rt["ModelType"]=modelType; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<"Used adaptive finite volume model\n"; - return startReturn; - } - else if (modelType == "MPFAO") - { - typedef TTAG(MPFAOTwoPTestProblem) ProblemTypeTag; - typedef GET_PROP(ProblemTypeTag, ParameterTree) ParamTree; - Dune::ParameterTree &rt = ParamTree::runTimeParams(); - rt["ModelType"]=modelType; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<"Used finite volume MPFA o-method model\n"; - return startReturn; - } - else if (modelType == "MPFAL") - { - typedef TTAG(MPFALTwoPTestProblem) ProblemTypeTag; - typedef GET_PROP(ProblemTypeTag, ParameterTree) ParamTree; - Dune::ParameterTree &rt = ParamTree::runTimeParams(); - rt["ModelType"]=modelType; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<"Used finite volume MPFA l-method model\n"; - return startReturn; - } - else if (modelType == "MPFALAdaptive") - { - typedef TTAG(MPFALAdaptiveTwoPTestProblem) ProblemTypeTag; - typedef GET_PROP(ProblemTypeTag, ParameterTree) ParamTree; - Dune::ParameterTree &rt = ParamTree::runTimeParams(); - rt["ModelType"]=modelType; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<"Used adaptive finite volume MPFA l-method model\n"; - return startReturn; - } - else - { - typedef TTAG(MPFAOTwoPTestProblem) ProblemTypeTag; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<"Unknwon model type "<<modelType<<" specified\n"; - std::cout<<"Default to finite volume MPFA o-method model\n"; - return startReturn; - } + if (modelType == "FV") + { + using ProblemTypeTag = TTAG(FVTwoPTestProblem); + // avoid unused parameter message + GET_PROP(ProblemTypeTag, ParameterTree)::runTimeParams()["ModelType"] = modelType; + std::cout<<"##########################################" << std::endl; + std::cout<<"Standard finite volume TPFA model" << std::endl; + std::cout<<"##########################################" << std::endl; + return start<ProblemTypeTag>(argc, argv, usage); + } + else if (modelType == "FVAdaptive") + { + using ProblemTypeTag = TTAG(FVAdaptiveTwoPTestProblem); + // avoid unused parameter message + GET_PROP(ProblemTypeTag, ParameterTree)::runTimeParams()["ModelType"] = modelType; + std::cout<<"##########################################" << std::endl; + std::cout<<"Adaptive finite volume TPFA model" << std::endl; + std::cout<<"##########################################" << std::endl; + return start<ProblemTypeTag>(argc, argv, usage); + } + else if (modelType == "MPFAO") + { + using ProblemTypeTag = TTAG(MPFAOTwoPTestProblem); + // avoid unused parameter message + GET_PROP(ProblemTypeTag, ParameterTree)::runTimeParams()["ModelType"] = modelType; + std::cout<<"##########################################" << std::endl; + std::cout<<"Standard finite volume MPFA-O model" << std::endl; + std::cout<<"##########################################" << std::endl; + return start<ProblemTypeTag>(argc, argv, usage); + } + else if (modelType == "MPFAL") + { + using ProblemTypeTag = TTAG(MPFALTwoPTestProblem); + // avoid unused parameter message + GET_PROP(ProblemTypeTag, ParameterTree)::runTimeParams()["ModelType"] = modelType; + std::cout<<"##########################################" << std::endl; + std::cout<<"Unknown model type " << modelType << ", default to" << std::endl; + std::cout<<"Standard finite volume MPFA-L model" << std::endl; + std::cout<<"##########################################" << std::endl; + return start<ProblemTypeTag>(argc, argv, usage); + } + else if (modelType == "MPFALAdaptive") + { + using ProblemTypeTag = TTAG(MPFALAdaptiveTwoPTestProblem); + // avoid unused parameter message + GET_PROP(ProblemTypeTag, ParameterTree)::runTimeParams()["ModelType"] = modelType; + std::cout<<"##########################################" << std::endl; + std::cout<<"Adaptive finite volume MPFA-L model" << std::endl; + std::cout<<"##########################################" << std::endl; + return start<ProblemTypeTag>(argc, argv, usage); } else { - typedef TTAG(MPFAOTwoPTestProblem) ProblemTypeTag; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<"No model type specified\n"; - std::cout<<"Default to finite volume MPFA o-method model\n"; - return startReturn; + using ProblemTypeTag = TTAG(MPFAOTwoPTestProblem); + // avoid unused parameter message + GET_PROP(ProblemTypeTag, ParameterTree)::runTimeParams()["ModelType"] = modelType; + std::cout<<"##########################################" << std::endl; + std::cout<<"Unknown model type " << modelType << ", default to" << std::endl; + std::cout<<"Standard finite volume MPFA-O model" << std::endl; + std::cout<<"##########################################" << std::endl; + return start<ProblemTypeTag>(argc, argv, usage); } } - else - { - typedef TTAG(MPFAOTwoPTestProblem) ProblemTypeTag; - int startReturn = Dumux::start<ProblemTypeTag>(argc, argv, usage); - std::cout<<"######################################################\n"; - std::cout<<s<<" is not a valid model type specification!\n"; - std::cout<<"Default to finite volume MPFA o-method model\n"; - return startReturn; + catch (ParameterException &e) { + std::cerr << std::endl << e << ". Abort!" << std::endl; + return 1; + } + catch (Dune::Exception &e) { + std::cerr << "Dune reported error: " << e << std::endl; + return 3; } + catch (...) { + std::cerr << "Unknown exception thrown!\n"; + return 4; + } + + return 0; } #else