24#ifndef DUMUX_PARAMETERS_HH
25#define DUMUX_PARAMETERS_HH
30#include <unordered_map>
33#include <dune/common/parametertree.hh>
34#include <dune/common/parametertreeparser.hh>
35#include <dune/common/parallel/mpihelper.hh>
50 using DefaultParams = std::function<void (Dune::ParameterTree&)>;
51 using Usage = std::function<void (
const char *,
const std::string &)>;
56 static void init(
int argc,
char **argv,
const Usage&
usage)
58 init(argc, argv, [] (Dune::ParameterTree&) {},
"",
usage);
62 static void init(
int argc,
char **argv,
63 std::string parameterFileName,
64 const Usage&
usage = [](
const char *,
const std::string &){})
66 init(argc, argv, [] (Dune::ParameterTree&) {}, parameterFileName,
usage);
70 static void init(
int argc,
char **argv,
71 const DefaultParams& defaultParams,
74 init(argc, argv, defaultParams,
"",
usage);
93 static void init(
int argc,
char **argv,
94 const DefaultParams& defaultParams = [] (Dune::ParameterTree&) {},
95 std::string parameterFileName =
"",
96 const Usage&
usage = [](
const char *,
const std::string &){})
98 const auto& mpiHelper = Dune::MPIHelper::instance(argc, argv);
101 for (
int i = 1; i < argc; ++i)
103 if (std::string(
"--help") == argv[i] || std::string(
"-h") == argv[i])
106 if (mpiHelper.rank() == 0)
114 defaultParams(defaultParamTree_());
115 applyGlobalDefaults_(defaultParamTree_());
119 mergeTree_(paramTree_(), commandLineArgs);
122 parameterFileName = commandLineArgs.get<std::string>(
"ParameterFile", parameterFileName);
125 if (parameterFileName ==
"")
127 parameterFileName = [&](){
128 std::string defaultName = std::string(argv[0]) +
".input";
129 std::ifstream pFile(defaultName.c_str());
133 defaultName =
"params.input";
134 pFile = std::ifstream(defaultName.c_str());
138 return std::string(
"");
142 if (parameterFileName ==
"")
144 if (mpiHelper.size() > 1)
145 std::cout <<
"Rank " << mpiHelper.rank() <<
": ";
146 std::cout <<
"No parameter file found. Continuing without parameter file.\n";
152 if (mpiHelper.size() > 1)
153 std::cout <<
"Rank " << mpiHelper.rank() <<
": ";
154 std::cout <<
"No parameter file given. "
157 <<
"' for input file.\n";
161 if (mpiHelper.size() > 1)
162 std::cout <<
"Rank " << mpiHelper.rank() <<
": ";
163 std::cout <<
"Reading parameters from file " << parameterFileName <<
".\n";
168 Dune::ParameterTreeParser::readINITree(parameterFileName,
180 static void init(
const DefaultParams& params = [] (Dune::ParameterTree&) {},
181 const DefaultParams& defaultParams = [] (Dune::ParameterTree&) {})
184 params(paramTree_());
186 defaultParams(defaultParamTree_());
187 applyGlobalDefaults_(defaultParamTree_());
201 static void init(
const std::string& parameterFileName,
202 const DefaultParams& params = [] (Dune::ParameterTree&) {},
203 bool inputFileOverwritesParams =
true,
204 const DefaultParams& defaultParams = [] (Dune::ParameterTree&) {})
207 params(paramTree_());
210 Dune::ParameterTreeParser::readINITree(parameterFileName, paramTree_(), inputFileOverwritesParams);
213 defaultParams(defaultParamTree_());
214 applyGlobalDefaults_(defaultParamTree_());
226 Dune::ParameterTree commandLineArgs;
227 for (
int i = 1; i < argc; ++i)
229 if (argv[i][0] !=
'-' && i == 1)
232 commandLineArgs[
"ParameterFile"] = argv[1];
236 if (argv[i][0] !=
'-')
237 DUNE_THROW(
ParameterException,
"-> Command line argument " << i <<
" (='" << argv[i] <<
"') is invalid. <-");
240 DUNE_THROW(
ParameterException,
"-> No argument given for parameter '" << argv[i] <<
"'! <-");
243 if (argv[i]+1 == std::string(
"ParameterFile"))
245 commandLineArgs[
"ParameterFile"] = argv[i+1];
253 std::string paramName = argv[i]+1;
254 std::string paramValue = argv[i+1];
258 commandLineArgs[paramName] = paramValue;
261 return commandLineArgs;
264 [[deprecated(
"parseCommandLineArguments is deprecated and will be removed after 3.1")]]
266 std::string parameterFileName =
"")
268 for (
int i = 1; i < argc; ++i)
270 if (argv[i][0] !=
'-' && i == 1)
273 parameterFileName = argv[1];
277 if (argv[i][0] !=
'-')
278 DUNE_THROW(
ParameterException,
"-> Command line argument " << i <<
" (='" << argv[i] <<
"') is invalid. <-");
281 DUNE_THROW(
ParameterException,
"-> No argument given for parameter '" << argv[i] <<
"'! <-");
284 if (argv[i]+1 == std::string(
"ParameterFile"))
286 parameterFileName = argv[i+1];
294 std::string paramName = argv[i]+1;
295 std::string paramValue = argv[i+1];
299 paramTree_()[paramName] = paramValue;
302 return parameterFileName;
318 static Dune::ParameterTree& paramTree_()
320 static Dune::ParameterTree tree;
325 static Dune::ParameterTree& defaultParamTree_()
327 static Dune::ParameterTree tree;
333 static void applyGlobalDefaults_(Dune::ParameterTree& params)
336 Dune::ParameterTree defaultParams;
339 defaultParams[
"Flux.UpwindWeight"] =
"1.0";
340 defaultParams[
"Implicit.EnableJacobianRecycling"] =
"false";
343 defaultParams[
"Assembly.NumericDifferenceMethod"] =
"1";
346 defaultParams[
"LinearSolver.GMResRestart"] =
"10";
347 defaultParams[
"LinearSolver.MaxIterations"] =
"250";
348 defaultParams[
"LinearSolver.PreconditionerIterations"] =
"1";
349 defaultParams[
"LinearSolver.PreconditionerRelaxation"] =
"1.0";
350 defaultParams[
"LinearSolver.ResidualReduction"] =
"1e-13";
351 defaultParams[
"LinearSolver.Verbosity"] =
"0";
354 defaultParams[
"Problem.EnableGravity"] =
"true";
355 defaultParams[
"Problem.EnableInertiaTerms"] =
"true";
359 defaultParams[
"Newton.MinSteps"] =
"2";
360 defaultParams[
"Newton.MaxSteps"] =
"18";
361 defaultParams[
"Newton.TargetSteps"] =
"10";
362 defaultParams[
"Newton.UseLineSearch"] =
"false";
363 defaultParams[
"Newton.EnableChop"] =
"false";
364 defaultParams[
"Newton.EnableShiftCriterion"] =
"true";
365 defaultParams[
"Newton.MaxRelativeShift"] =
"1e-8";
366 defaultParams[
"Newton.EnableResidualCriterion"] =
"false";
367 defaultParams[
"Newton.ResidualReduction"] =
"1e-5";
368 defaultParams[
"Newton.EnableAbsoluteResidualCriterion"] =
"false";
369 defaultParams[
"Newton.MaxAbsoluteResidual"] =
"1e-5";
370 defaultParams[
"Newton.SatisfyResidualAndShiftCriterion"] =
"false";
371 defaultParams[
"Newton.EnablePartialReassembly"] =
"false";
374 defaultParams[
"TimeLoop.MaxTimeStepSize"] =
"1e300";
375 defaultParams[
"TimeLoop.MaxTimeStepDivisions"] =
"10";
378 defaultParams[
"Vtk.AddVelocity"] =
"false";
379 defaultParams[
"Vtk.AddProcessRank"] =
"true";
382 defaultParams[
"Mpfa.Q"] =
"0.0";
385 mergeTree_(params, defaultParams,
false);
389 static void mergeTree_(Dune::ParameterTree& target,
const Dune::ParameterTree& source,
bool overwrite =
true)
390 { mergeTreeImpl_(target, source, overwrite,
""); }
393 static void mergeTreeImpl_(Dune::ParameterTree& target,
const Dune::ParameterTree& source,
bool overwrite,
const std::string& group)
395 const auto prefix = group ==
"" ?
"" : group +
".";
396 for (
const auto& key : source.getValueKeys())
397 if (overwrite || !target.hasKey(key))
398 target[prefix + key] = source[key];
400 for (
const auto& subKey : source.getSubKeys())
401 mergeTreeImpl_(target, source.sub(subKey), overwrite, prefix + subKey);
409[[deprecated(
"Setting parameters is deprecated and will be removed after 3.1")]]
411 const std::string& group,
412 const std::string& key,
413 const std::string& value)
418 params[group +
"." + key] = value;
427template<
typename T,
typename... Args>
437template<
typename T,
typename... Args>
Function printing a default usage message.
Some exceptions thrown in DuMux
A parameter tree that logs which parameters have been used.
void setParam(Dune::ParameterTree ¶ms, const std::string &group, const std::string &key, const std::string &value)
a free function to set model- or problem-specific default parameters
Definition: parameters.hh:410
T getParamFromGroup(Args &&... args)
A free function to get a parameter from the parameter tree singleton with a model group.
Definition: parameters.hh:438
bool hasParamInGroup(const std::string ¶mGroup, const std::string ¶m)
Check whether a key exists in the parameter tree with a model group prefix.
Definition: parameters.hh:454
bool hasParam(const std::string ¶m)
Check whether a key exists in the parameter tree.
Definition: parameters.hh:446
std::string defaultUsageMessage(const std::string &programName)
Provides a general text block, that is part of error/ help messages.
Definition: defaultusagemessage.hh:37
T getParam(Args &&... args)
A free function to get a parameter from the parameter tree singleton.
Definition: parameters.hh:428
make the local view function available whenever we use the grid geometry
Definition: adapt.hh:29
Exception thrown if a run-time parameter is not specified correctly.
Definition: exceptions.hh:60
A parameter tree that logs which parameters have been used.
Definition: loggingparametertree.hh:41
void reportAll(std::ostream &stream=std::cout) const
print distinct substructure to stream
Definition: loggingparametertree.hh:109
bool hasKeyInGroup(const std::string &key, const std::string &groupPrefix) const
test for key in group
Definition: loggingparametertree.hh:76
bool hasKey(const std::string &key) const
test for key
Definition: loggingparametertree.hh:62
Parameter class managing runtime input parameters.
Definition: parameters.hh:48
static void init(int argc, char **argv, const DefaultParams &defaultParams=[](Dune::ParameterTree &) {}, std::string parameterFileName="", const Usage &usage=[](const char *, const std::string &){})
Initialize the parameter tree.
Definition: parameters.hh:93
static std::string parseCommandLineArguments(int argc, char **argv, std::string parameterFileName="")
Definition: parameters.hh:265
static void init(const std::string ¶meterFileName, const DefaultParams ¶ms=[](Dune::ParameterTree &) {}, bool inputFileOverwritesParams=true, const DefaultParams &defaultParams=[](Dune::ParameterTree &) {})
Initialize the parameter tree.
Definition: parameters.hh:201
static void init(int argc, char **argv, const DefaultParams &defaultParams, const Usage &usage)
Initialize the parameter tree singletons.
Definition: parameters.hh:70
static void init(const DefaultParams ¶ms=[](Dune::ParameterTree &) {}, const DefaultParams &defaultParams=[](Dune::ParameterTree &) {})
Initialize the parameter tree.
Definition: parameters.hh:180
static const LoggingParameterTree & getTree()
Get the parameter tree.
Definition: parameters.hh:310
static void init(int argc, char **argv, std::string parameterFileName, const Usage &usage=[](const char *, const std::string &){})
Initialize the parameter tree singletons.
Definition: parameters.hh:62
static void init(int argc, char **argv, const Usage &usage)
Initialize the parameter tree singletons.
Definition: parameters.hh:56
static void print()
prints all used and unused parameters
Definition: parameters.hh:218
static Dune::ParameterTree parseCommandLine(int argc, char **argv)
Parse command line arguments into a parameter tree.
Definition: parameters.hh:224
void usage(const char *progName, const std::string &errorMsg)
Provides an interface for customizing error messages associated with reading in parameters.
Definition: freeflow/rans/main.cc:60