Commit d2f10532 authored by Philipp Nuske's avatar Philipp Nuske
Browse files

- Changed all boxmodels to parameter file: no other argument required

- Everything and the kitchen sink can be specified in there. Example:
  the 2p2c test
- Minor changes to the parameter file syntax: parameters do not start
  with capital any more
- It is checked now whether the parameter file exists
- How to use the program / parameter file can be queried by ./progName
  --help
- Documentation: /dumux/common/start.hh and the parameterfiles themselves


git-svn-id: svn://svn.iws.uni-stuttgart.de/DUMUX/dumux/trunk@7607 2fb0f335-1f38-0410-981e-8018bf24f1b0
parent a5014f35
......@@ -53,7 +53,7 @@ public:
*/
static void makeGrid()
{
const std::string dgfFileName = GET_RUNTIME_PARAM(TypeTag, std::string, DgfFile);
const std::string dgfFileName = GET_RUNTIME_PARAM(TypeTag, std::string, gridFile);
gridPtr_ = GridPointer(dgfFileName.c_str());
};
......
......@@ -242,8 +242,19 @@ int startWithGrid(const typename GET_PROP_TYPE(TypeTag, Grid) &grid,
return 3;
}
/*!
* \ingroup Start
* \brief Read the command line arguments and write them into the parameter tree.
* Do some syntax checks.
*
* \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 paramTree The parameterTree. It can be filled from an input file or the command line.
* \return Empty string if everything worked out. Otherwise the thing that could not be read.
*/
std::string readOptions_(int argc, char **argv, Dune::ParameterTree &paramTree)
{
// All command line options need to start with '-'
for (int i = 1; i < argc; ++i) {
if (argv[i][0] != '-') {
std::ostringstream oss;
......@@ -253,11 +264,12 @@ std::string readOptions_(int argc, char **argv, Dune::ParameterTree &paramTree)
std::string paramName, paramValue;
// read a --my-opt=abc option. This gets transformed
// into the parameter "myOpt" with the value being
// "abc"
if (argv[i][1] == '-') {
// read a --my-opt=abc option. This gets transformed
// into the parameter "MyOpt" with the value being
// "abc"
std::string s(argv[i] + 2);
// There is nothing after the '='
if (s.size() == 0 || s[0] == '=')
{
std::ostringstream oss;
......@@ -266,10 +278,6 @@ std::string readOptions_(int argc, char **argv, Dune::ParameterTree &paramTree)
return oss.str();
}
// capitalize first letter
s[0] = std::toupper(s[0]);
// parse argument
int j = 0;
while (true) {
......@@ -307,14 +315,14 @@ std::string readOptions_(int argc, char **argv, Dune::ParameterTree &paramTree)
return oss.str();
}
s[j] = toupper(s[j]);
};
}
++j;
}
}
else {
// read a -MyOpt abc option
paramName = argv[i] + 2;
// read a -myOpt abc option
paramName = argv[i] + 1;
if (argc == i + 1 || argv[i+1][0] == '-') {
std::ostringstream oss;
......@@ -323,7 +331,7 @@ std::string readOptions_(int argc, char **argv, Dune::ParameterTree &paramTree)
}
paramValue = argv[i+1];
++i;
++i; // In the case of '-myOpt abc' each pair counts as two arguments
}
// Put the key=value pair into the parameter tree
......@@ -332,6 +340,18 @@ std::string readOptions_(int argc, char **argv, Dune::ParameterTree &paramTree)
return "";
}
/*!
* \ingroup Start
*
* \brief Provides a main function which reads in parameters from the
* command line and a parameter file.
*
* \tparam TypeTag The type tag of the problem which needs to be solved
*
* \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
*/
template <class TypeTag>
int startWithParameters_(int argc,
char **argv,
......@@ -339,7 +359,7 @@ int startWithParameters_(int argc,
{
typedef typename GET_PROP_TYPE(TypeTag, Scalar) Scalar;
typedef typename GET_PROP_TYPE(TypeTag, Grid) Grid;
typedef typename GET_PROP_TYPE(TypeTag, GridCreator) GridCreator;
typedef typename GET_PROP_TYPE(TypeTag, GridCreator) GridCreator; // Set by default (dumux/common/basicproperties.hh) to DgfGridCreator (dumux/common/dgfgridcreator.hh)
typedef typename GET_PROP_TYPE(TypeTag, Problem) Problem;
typedef typename GET_PROP_TYPE(TypeTag, TimeManager) TimeManager;
......@@ -353,8 +373,17 @@ int startWithParameters_(int argc,
// check whether the user did not specify any parameter. in this
// case print the usage message
if (argc == 1) {
usage(argv[0], "");
return 0;
std::cout<< "\nNo parameter file given. \n"
<< "Defaulting to './parameter.input' for input file.\n";
std::ifstream parameterFile;
// check whether the parameter file exists.
parameterFile.open("parameter.input");
if (not parameterFile.is_open()){
std::cout<< "\n Could not open file './parameter.input'.\n\n";
usage(argv[0], "");
return 0;
}
parameterFile.close();
}
......@@ -368,25 +397,42 @@ int startWithParameters_(int argc,
}
// fill the parameter tree with the options from the command line
typedef typename GET_PROP(TypeTag, ParameterTree) Params;
std::string s = readOptions_(argc, argv, Params::tree());
typedef typename GET_PROP(TypeTag, ParameterTree) ParameterTree;
std::string s = readOptions_(argc, argv, ParameterTree::tree());
if (!s.empty()) {
usage(argv[0], s);
return 1;
};
if (Params::tree().hasKey("OptsFile")) {
return 0;
}
if (ParameterTree::tree().hasKey("parameterFile") or argc==1) {
// read input file, but do not overwrite options specified
// on the command line, since the latter have preceedence.
std::string inputFileName = GET_RUNTIME_PARAM(TypeTag, std::string, OptsFile);
// on the command line, since the latter have precedence.
std::string inputFileName ;
if(argc==1) // if there are no arguments given (and there is a file ./parameter.input) we use it as input file
inputFileName="parameter.input";
else
inputFileName = GET_RUNTIME_PARAM(TypeTag, std::string, parameterFile); // otherwise we read from the command line
std::ifstream parameterFile;
// check whether the parameter file exists.
parameterFile.open(inputFileName);
if (not parameterFile.is_open()){
std::cout<< "\n Could not open file"
<< inputFileName
<< "\n\n";
usage(argv[0], "");
return 0;
}
parameterFile.close();
Dune::ParameterTreeParser::readINITree(inputFileName,
Params::tree(),
ParameterTree::tree(),
/*overwrite=*/false);
}
bool printProps = true;
if (Params::tree().hasKey("PrintProperties"))
if (ParameterTree::tree().hasKey("PrintProperties"))
printProps = GET_RUNTIME_PARAM(TypeTag, bool, PrintProperies);
if (printProps && mpiHelper.rank() == 0) {
......@@ -396,16 +442,17 @@ int startWithParameters_(int argc,
// deal with the restart stuff
bool restart = false;
Scalar restartTime = 0;
if (Params::tree().hasKey("Restart")) {
if (ParameterTree::tree().hasKey("restart")) {
restart = true;
restartTime = GET_RUNTIME_PARAM(TypeTag, Scalar, Restart);
restartTime = GET_RUNTIME_PARAM(TypeTag, Scalar, restart);
}
// read the PrintParams parameter
bool printParams = true;
if (Params::tree().hasKey("PrintParameters"))
if (ParameterTree::tree().hasKey("PrintParameters"))
printParams = GET_RUNTIME_PARAM(TypeTag, bool, PrintParameters);
// try to create a grid (from the given grid file)
try { GridCreator::makeGrid(); }
catch (...) { usage(argv[1], "Creation of the grid failed!"); throw; }
......@@ -413,25 +460,22 @@ int startWithParameters_(int argc,
double tEnd;
double dt;
try { tEnd = GET_RUNTIME_PARAM(TypeTag, Scalar, TEnd); }
try { tEnd = GET_RUNTIME_PARAM(TypeTag, Scalar, tEnd); }
catch (...) { usage(argv[1], "Mandatory parameter '--t-end' not specified!"); throw; }
try { dt = GET_RUNTIME_PARAM(TypeTag, Scalar, DtInitial); }
try { dt = GET_RUNTIME_PARAM(TypeTag, Scalar, dtInitial); }
catch (...) { usage(argv[1], "Mandatory parameter '--dt-initial' not specified!"); throw; }
// instantiate and run the concrete problem
TimeManager timeManager;
Problem problem(timeManager);
timeManager.init(problem, 0, dt, tEnd, restart);
if (restart)
problem.restart(restartTime);
Problem problem(timeManager, GridCreator::grid().leafView());
timeManager.init(problem, restartTime, dt, tEnd, restart);
timeManager.run();
if (printParams && mpiHelper.rank() == 0) {
Dumux::Parameters::print<TypeTag>();
}
return 0;
return 1;
}
/*!
......@@ -456,10 +500,13 @@ bool inDebugger()
* \brief Provides a main function which reads in parameters from the
* command line and a parameter file.
*
* In this function only the differentiation between debugger
* or not is made.
*
* \tparam TypeTag The type tag of the problem which needs to be solved
*
* \param argc The number of command line arguments of the program
* \param argv The contents of the command line arguments of the program
* \param argc The number of command line arguments of the program
* \param argv The contents of the command line arguments of the program
* \param usage Callback function for printing the usage message
*/
template <class TypeTag>
......@@ -488,6 +535,189 @@ int startWithParameters(int argc,
return startWithParameters_<TypeTag>(argc, argv, usage);
}
/*!
* \ingroup Start
*
* \brief Provides a main function which reads in parameters from the
* command line and a parameter file. The Grid however is created
* in the 'real' main function.
*
* \tparam TypeTag The type tag of the problem which needs to be solved
* \tparam Scalar The type for floating point values
*
* \param argc The number of command line arguments of the program
* \param argv The contents of the command line arguments of the program
* \param usage Callback function for printing the usage message
* \param tEnd End of simulation time
* \param dt First time step size
* \param restart Should we restart a simulation?
* \param restartTime At which time should the simulation be restarted?
*/
template <class TypeTag, class Scalar>
int startWithParametersProvideMyOwnGrid_(int argc,
char **argv,
void (*usage)(const char *, const std::string &),
Scalar & tEnd,
Scalar & dt,
bool & restart,
Scalar & restartTime)
{
typedef typename GET_PROP_TYPE(TypeTag, Grid) Grid;
typedef typename GET_PROP_TYPE(TypeTag, GridCreator) GridCreator; // Set by default (dumux/common/basicproperties.hh) to DgfGridCreator (dumux/common/dgfgridcreator.hh)
typedef typename GET_PROP_TYPE(TypeTag, Problem) Problem;
typedef typename GET_PROP_TYPE(TypeTag, TimeManager) TimeManager;
// initialize MPI, finalize is done automatically on exit
const Dune::MPIHelper &mpiHelper = Dune::MPIHelper::instance(argc, argv);
////////////////////////////////////////////////////////////
// parse the command line arguments
////////////////////////////////////////////////////////////
// check whether the user did not specify any parameter. in this
// case print the usage message
if (argc == 1) {
std::cout<< "\nNo parameter file given. \n"
<< "Defaulting to './parameter.input' for input file.\n";
std::ifstream parameterFile;
// check whether the parameter file exists.
parameterFile.open("parameter.input");
if (not parameterFile.is_open()){
std::cout<< "\n Could not open file './parameter.input'.\n\n";
usage(argv[0], "");
exit(1);
}
parameterFile.close();
}
// check whether the user wanted to see the help message
for (int i = 1; i < argc; ++i) {
if (std::string("--help") == argv[i] || std::string("-h") == argv[i])
{
usage(argv[0], "");
exit(0);
}
}
// fill the parameter tree with the options from the command line
typedef typename GET_PROP(TypeTag, ParameterTree) ParameterTree;
std::string s = readOptions_(argc, argv, ParameterTree::tree());
if (!s.empty()) {
usage(argv[0], s);
exit(1);
}
if (ParameterTree::tree().hasKey("parameterFile") or argc==1) {
// read input file, but do not overwrite options specified
// on the command line, since the latter have precedence.
std::string inputFileName ;
if(argc==1) // if there are no arguments given (and there is a file ./parameter.input) we use it as input file
inputFileName="parameter.input";
else
inputFileName = GET_RUNTIME_PARAM(TypeTag, std::string, parameterFile); // otherwise we try
std::ifstream parameterFile;
// check whether the parameter file exists.
parameterFile.open(inputFileName);
if (not parameterFile.is_open()){
std::cout<< "\n Could not open file"
<< inputFileName
<< "\n\n";
usage(argv[0], "");
exit(1);
}
parameterFile.close();
Dune::ParameterTreeParser::readINITree(inputFileName,
ParameterTree::tree(),
/*overwrite=*/false);
}
bool printProps = true;
if (ParameterTree::tree().hasKey("PrintProperties"))
printProps = GET_RUNTIME_PARAM(TypeTag, bool, PrintProperies);
if (printProps && mpiHelper.rank() == 0) {
Dumux::Properties::print<TypeTag>();
}
// deal with the restart stuff
if (ParameterTree::tree().hasKey("restart")) {
restart = true;
restartTime = GET_RUNTIME_PARAM(TypeTag, Scalar, restart);
}
// read the PrintParams parameter
bool printParams = true;
if (ParameterTree::tree().hasKey("PrintParameters"))
printParams = GET_RUNTIME_PARAM(TypeTag, bool, PrintParameters);
try { tEnd = GET_RUNTIME_PARAM(TypeTag, Scalar, tEnd); }
catch (...) { usage(argv[1], "Mandatory parameter '--t-end' not specified!"); throw; }
try { dt = GET_RUNTIME_PARAM(TypeTag, Scalar, dtInitial); }
catch (...) { usage(argv[1], "Mandatory parameter '--dt-initial' not specified!"); throw; }
if (printParams && mpiHelper.rank() == 0) {
Dumux::Parameters::print<TypeTag>();
}
return 0;
}
/*!
* \ingroup Start
*
* \brief Provides a main function which reads in parameters from the
* command line and a parameter file. The Grid however is created
* in the 'real' main function.
*
* In this function only the differentiation between debugger
* or not is made.
*
* \tparam TypeTag The type tag of the problem which needs to be solved
*
* \param argc The number of command line arguments of the program
* \param argv The contents of the command line arguments of the program
* \param usage Callback function for printing the usage message
* \param tEnd End of simulation time
* \param dt First time step size
* \param restart Should we restart a simulation?
* \param restartTime At which time should the simulation be restarted?
*/
template <class TypeTag, class Scalar>
int startWithParametersProvideMyOwnGrid(int argc,
char **argv,
void (*usage)(const char *, const std::string &),
Scalar & tEnd,
Scalar & dt,
bool & restart,
Scalar & restartTime)
{
if (not inDebugger()) {
try {
return startWithParametersProvideMyOwnGrid_<TypeTag, Scalar>(argc, argv, usage, tEnd, dt, restart, restartTime);
}
catch (Dumux::ParameterException &e) {
std::cerr << e << ". Abort!\n";
return 1;
}
catch (Dune::Exception &e) {
std::cerr << "Dune reported error: " << e << std::endl;
return 2;
}
catch (...) {
std::cerr << "Unknown exception thrown!\n";
return 3;
}
}
else
return startWithParametersProvideMyOwnGrid_<TypeTag, Scalar>(argc, argv, usage, tEnd, dt, restart, restartTime);
}
} // namespace Dumux
#endif
......@@ -26,12 +26,47 @@
* \brief test for the one-phase box model
*/
#include "config.h"
#include "1ptestproblem.hh"
#include <dumux/common/start.hh>
void usage(const char *progName, const std::string &errorMsg)
{
if (errorMsg.size() > 0) {
std::cout << errorMsg << "\n"
<< "\n";
}
std::cout
<< "Usage: " << progName << " [options]\n"
<< "Mandatory options are:\n"
<< "\t--t-end=ENDTIME The time of the end of the simlation [s]\n"
<< "\t--dt-initial=STEPSIZE The initial time step size [s]\n"
<< "\t--grid-file=FILENAME The file name of the file containing the grid \n"
<< "\t definition in DGF format\n"
<< "\n"
<< "Alternativ supported syntax:\n"
<< "\t-tEnd ENDTIME The time of the end of the simlation [s]\n"
<< "\t-dtInitial STEPSIZE The initial time step size [s]\n"
<< "\t-gridFile FILENAME The file name of the file containing the grid \n"
<< "\t definition in DGF format\n"
<< "\n"
<< "If --parameter-file is specified parameters can also be defined there. In this case,\n"
<< "camel case is used for the parameters (e.g.: --grid-file becomes gridFile). Parameters\n"
<< "specified on the command line have priority over those in the parameter file.\n"
<< "Important optional options include:\n"
<< "\t--help,-h Print this usage message and exit\n"
<< "\t--print-parameters[=true|false] Print the run-time modifiable parameters _after_ \n"
<< "\t the simulation [default: true]\n"
<< "\t--print-properties[=true|false] Print the compile-time parameters _before_ \n"
<< "\t the simulation [default: true]\n"
<< "\t--parameter-file=FILENAME File with parameter definitions\n"
<< "\t--restart=RESTARTTIME Restart simulation from a restart file\n"
<< "\n"
<< "For the case of no arguments given, the input parameter file is expected to be named './parameter.input' \n"
<< "\n";
}
int main(int argc, char** argv)
{
return Dumux::startFromDGF<TTAG(OnePTestProblem)>(argc, argv);
typedef TTAG(OnePTestProblem) ProblemTypeTag;
return Dumux::startWithParameters<ProblemTypeTag>(argc, argv, usage);
}
#############################################################
# Parameter file for test_1p #
# Everything behind a '#' is a comment #
# Groups can be ordered e.g. : [BoundaryConditions], #
# see ../2p2c/ for a more detailed example #
#############################################################
#############################################################
# Mandatory arguments #
#############################################################
dtInitial =1 # seconds
tEnd =1 # seconds
gridFile =./grids/test_1p_2d.dgf
#####################################################################
# Simulation restart #
# #
# DuMux simulations can be restarted from *.drs files #
# Set restart to the value of a specific file, e.g.: 'restart = 27184.1' #
# for the restart file #
# name_time=27184.1_rank=0.drs #
# Please comment in the below value, if restart is desired. #
#####################################################################
# restart= ...
\ No newline at end of file
......@@ -27,12 +27,47 @@
* \brief test for the 1p2c box model
*/
#include "config.h"
#include "tissue_tumor_problem.hh"
#include <dumux/common/start.hh>
void usage(const char *progName, const std::string &errorMsg)
{
if (errorMsg.size() > 0) {
std::cout << errorMsg << "\n"
<< "\n";
}
std::cout
<< "Usage: " << progName << " [options]\n"
<< "Mandatory options are:\n"
<< "\t--t-end=ENDTIME The time of the end of the simlation [s]\n"
<< "\t--dt-initial=STEPSIZE The initial time step size [s]\n"
<< "\t--grid-file=FILENAME The file name of the file containing the grid \n"
<< "\t definition in DGF format\n"
<< "\n"
<< "Alternativ supported syntax:\n"
<< "\t-tEnd ENDTIME The time of the end of the simlation [s]\n"
<< "\t-dtInitial STEPSIZE The initial time step size [s]\n"
<< "\t-gridFile FILENAME The file name of the file containing the grid \n"
<< "\t definition in DGF format\n"
<< "\n"
<< "If --parameter-file is specified parameters can also be defined there. In this case,\n"
<< "camel case is used for the parameters (e.g.: --grid-file becomes gridFile). Parameters\n"
<< "specified on the command line have priority over those in the parameter file.\n"
<< "Important optional options include:\n"
<< "\t--help,-h Print this usage message and exit\n"
<< "\t--print-parameters[=true|false] Print the run-time modifiable parameters _after_ \n"
<< "\t the simulation [default: true]\n"
<< "\t--print-properties[=true|false] Print the compile-time parameters _before_ \n"
<< "\t the simulation [default: true]\n"
<< "\t--parameter-file=FILENAME File with parameter definitions\n"
<< "\t--restart=RESTARTTIME Restart simulation from a restart file\n"
<< "\n"
<< "For the case of no arguments given, the input parameter file is expected to be named './parameter.input' \n"
<< "\n";
}
int main(int argc, char** argv)
{
return Dumux::startFromDGF<TTAG(TissueTumorProblem)>(argc, argv);
typedef TTAG(TissueTumorProblem) ProblemTypeTag;
return Dumux::startWithParameters<ProblemTypeTag>(argc, argv, usage);
}
#############################################################
# Parameter file for test_1p2c #
# Everything behind a '#' is a comment #
# Groups can be ordered e.g. : [BoundaryConditions], #
# see ../2p2c/ for a more detailed example #
#############################################################
#############################################################
# Mandatory arguments #
#############################################################
dtInitial =1e3 # seconds
tEnd =2e3 # seconds
gridFile =./grids/test_1p2c.dgf
#####################################################################
# Simulation restart #
# #
# DuMux simulations can be restarted from *.drs files #
# Set restart to the value of a specific file, e.g.: 'restart = 27184.1' #
# for the restart file #
# name_time=27184.1_rank=0.drs #
# Please comment in the below value, if restart is desired. #
#####################################################################
# restart= ...
\ No newline at end of file
......@@ -27,9 +27,8 @@
* \brief test for the two-phase box model
*/
#include "config.h"
#define CUBES 1
#include "lensproblem.hh"
#include <dumux/common/start.hh>
#include <dune/grid/common/gridinfo.hh>
......@@ -39,6 +38,39 @@
#include <iostream>
void usage(const char *progName, const std::string &errorMsg)
{
if (errorMsg.size() > 0) {
std::cout << errorMsg << "\n"
<< "\n";