24#ifndef DUMUX_PARAMETERS_HH
25#define DUMUX_PARAMETERS_HH
30#include <unordered_map>
34#include <dune/common/parametertree.hh>
35#include <dune/common/parametertreeparser.hh>
36#include <dune/common/parallel/mpihelper.hh>
51 using DefaultParams = std::function<void (Dune::ParameterTree&)>;
52 using Usage = std::function<void (
const char *,
const std::string &)>;
57 static void init(
int argc,
char **argv,
const Usage& usage)
59 init(argc, argv, [] (Dune::ParameterTree&) {},
"", usage);
63 static void init(
int argc,
char **argv,
64 std::string parameterFileName,
65 const Usage& usage = [](
const char *,
const std::string &){})
67 init(argc, argv, [] (Dune::ParameterTree&) {}, parameterFileName, usage);
71 static void init(
int argc,
char **argv,
72 const DefaultParams& defaultParams,
75 init(argc, argv, defaultParams,
"", usage);
94 static void init(
int argc,
char **argv,
95 const DefaultParams& defaultParams = [] (Dune::ParameterTree&) {},
96 std::string parameterFileName =
"",
97 const Usage& usage = [](
const char *,
const std::string &){})
99 const auto& mpiHelper = Dune::MPIHelper::instance(argc, argv);
102 for (
int i = 1; i < argc; ++i)
104 if (std::string(
"--help") == argv[i] || std::string(
"-h") == argv[i])
107 if (mpiHelper.rank() == 0)
115 defaultParams(defaultParamTree_());
116 applyGlobalDefaults_(defaultParamTree_());
120 mergeTree_(paramTree_(), commandLineArgs);
123 parameterFileName = commandLineArgs.get<std::string>(
"ParameterFile", parameterFileName);
126 if (parameterFileName.empty())
128 parameterFileName = [&](){
129 std::string defaultName = std::string(argv[0]) +
".input";
130 std::ifstream pFile(defaultName.c_str());
134 defaultName =
"params.input";
135 pFile = std::ifstream(defaultName.c_str());
139 return std::string(
"");
143 if (parameterFileName.empty())
145 if (mpiHelper.size() > 1)
146 std::cout <<
"Rank " << mpiHelper.rank() <<
": ";
147 std::cout <<
"No parameter file found. Continuing without parameter file.\n";
153 if (mpiHelper.size() > 1)
154 std::cout <<
"Rank " << mpiHelper.rank() <<
": ";
155 std::cout <<
"No parameter file given. "
158 <<
"' for input file.\n";
162 if (mpiHelper.size() > 1)
163 std::cout <<
"Rank " << mpiHelper.rank() <<
": ";
164 std::cout <<
"Reading parameters from file " << parameterFileName <<
".\n";
169 Dune::ParameterTreeParser::readINITree(parameterFileName,
181 static void init(
const DefaultParams& params = [] (Dune::ParameterTree&) {},
182 const DefaultParams& defaultParams = [] (Dune::ParameterTree&) {})
185 params(paramTree_());
187 defaultParams(defaultParamTree_());
188 applyGlobalDefaults_(defaultParamTree_());
202 static void init(
const std::string& parameterFileName,
203 const DefaultParams& params = [] (Dune::ParameterTree&) {},
204 bool inputFileOverwritesParams =
true,
205 const DefaultParams& defaultParams = [] (Dune::ParameterTree&) {})
208 params(paramTree_());
211 Dune::ParameterTreeParser::readINITree(parameterFileName, paramTree_(), inputFileOverwritesParams);
214 defaultParams(defaultParamTree_());
215 applyGlobalDefaults_(defaultParamTree_());
227 Dune::ParameterTree commandLineArgs;
228 for (
int i = 1; i < argc; ++i)
230 if (argv[i][0] !=
'-' && i == 1)
233 commandLineArgs[
"ParameterFile"] = argv[1];
237 if (argv[i][0] !=
'-')
238 DUNE_THROW(
ParameterException,
"-> Command line argument " << i <<
" (='" << argv[i] <<
"') is invalid. <-");
241 DUNE_THROW(
ParameterException,
"-> No argument given for parameter '" << argv[i] <<
"'! <-");
244 if (argv[i]+1 == std::string(
"ParameterFile"))
246 commandLineArgs[
"ParameterFile"] = argv[i+1];
254 std::string paramName = argv[i]+1;
255 std::string paramValue = argv[i+1];
259 commandLineArgs[paramName] = paramValue;
262 return commandLineArgs;
278 static Dune::ParameterTree& paramTree_()
280 static Dune::ParameterTree tree;
285 static Dune::ParameterTree& defaultParamTree_()
287 static Dune::ParameterTree tree;
293 static void applyGlobalDefaults_(Dune::ParameterTree& params)
296 Dune::ParameterTree defaultParams;
299 defaultParams[
"Flux.UpwindWeight"] =
"1.0";
300 defaultParams[
"Implicit.EnableJacobianRecycling"] =
"false";
303 defaultParams[
"Assembly.NumericDifferenceMethod"] =
"1";
306 defaultParams[
"Problem.EnableGravity"] =
"true";
307 defaultParams[
"Problem.EnableInertiaTerms"] =
"true";
311 defaultParams[
"Newton.MinSteps"] =
"2";
312 defaultParams[
"Newton.MaxSteps"] =
"18";
313 defaultParams[
"Newton.TargetSteps"] =
"10";
314 defaultParams[
"Newton.UseLineSearch"] =
"false";
315 defaultParams[
"Newton.EnableChop"] =
"false";
316 defaultParams[
"Newton.EnableShiftCriterion"] =
"true";
317 defaultParams[
"Newton.MaxRelativeShift"] =
"1e-8";
318 defaultParams[
"Newton.EnableResidualCriterion"] =
"false";
319 defaultParams[
"Newton.ResidualReduction"] =
"1e-5";
320 defaultParams[
"Newton.EnableAbsoluteResidualCriterion"] =
"false";
321 defaultParams[
"Newton.MaxAbsoluteResidual"] =
"1e-5";
322 defaultParams[
"Newton.SatisfyResidualAndShiftCriterion"] =
"false";
323 defaultParams[
"Newton.EnablePartialReassembly"] =
"false";
326 defaultParams[
"TimeLoop.MaxTimeStepSize"] =
"1e300";
327 defaultParams[
"TimeLoop.MaxTimeStepDivisions"] =
"10";
330 defaultParams[
"Vtk.AddVelocity"] =
"false";
331 defaultParams[
"Vtk.AddProcessRank"] =
"true";
334 defaultParams[
"Mpfa.Q"] =
"0.0";
335 defaultParams[
"MPFA.Q"] =
"0.0";
338 mergeTree_(params, defaultParams,
false);
342 static void mergeTree_(Dune::ParameterTree& target,
const Dune::ParameterTree& source,
bool overwrite =
true)
343 { mergeTreeImpl_(target, source, overwrite,
""); }
346 static void mergeTreeImpl_(Dune::ParameterTree& target,
const Dune::ParameterTree& source,
bool overwrite,
const std::string& group)
348 const auto prefix = group.empty() ?
"" : group +
".";
349 for (
const auto& key : source.getValueKeys())
350 if (overwrite || !target.hasKey(key))
351 target[prefix + key] = source[key];
353 for (
const auto& subKey : source.getSubKeys())
354 mergeTreeImpl_(target, source.sub(subKey), overwrite, prefix + subKey);
364template<
typename T,
typename... Args>
374template<
typename T,
typename... Args>
399inline std::vector<std::string>
getParamSubGroups(
const std::string& subGroupName,
const std::string& paramGroup)
Function printing a default usage message.
Some exceptions thrown in DuMux
A parameter tree that logs which parameters have been used.
std::vector< std::string > getParamSubGroups(const std::string &subGroupName, const std::string ¶mGroup)
Get a list of sub groups from the parameter tree sorted by relevance.
Definition: parameters.hh:399
T getParamFromGroup(Args &&... args)
A free function to get a parameter from the parameter tree singleton with a model group.
Definition: parameters.hh:375
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:391
bool hasParam(const std::string ¶m)
Check whether a key exists in the parameter tree.
Definition: parameters.hh:383
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:365
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:164
std::vector< std::string > getSubGroups(const std::string &subGroupName, std::string groupPrefix) const
obtain a vector of all full group names for a specified subgroup name
Definition: loggingparametertree.hh:127
bool hasKeyInGroup(const std::string &key, const std::string &groupPrefix) const
test for key in group
Definition: loggingparametertree.hh:84
bool hasKey(const std::string &key) const
test for key
Definition: loggingparametertree.hh:66
Parameter class managing runtime input parameters.
Definition: parameters.hh:49
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:94
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:202
static void init(int argc, char **argv, const DefaultParams &defaultParams, const Usage &usage)
Initialize the parameter tree singletons.
Definition: parameters.hh:71
static void init(const DefaultParams ¶ms=[](Dune::ParameterTree &) {}, const DefaultParams &defaultParams=[](Dune::ParameterTree &) {})
Initialize the parameter tree.
Definition: parameters.hh:181
static const LoggingParameterTree & getTree()
Get the parameter tree.
Definition: parameters.hh:270
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:63
static void init(int argc, char **argv, const Usage &usage)
Initialize the parameter tree singletons.
Definition: parameters.hh:57
static void print()
prints all used and unused parameters
Definition: parameters.hh:219
static Dune::ParameterTree parseCommandLine(int argc, char **argv)
Parse command line arguments into a parameter tree.
Definition: parameters.hh:225