24#ifndef DUMUX_LOGGING_PARAMETER_TREE_HH
25#define DUMUX_LOGGING_PARAMETER_TREE_HH
31#include <dune/common/parametertree.hh>
53 : params_(params), defaultParams_(defaultParams) {}
62 bool hasKey(
const std::string& key)
const
63 {
return params_.hasKey(key); }
77 const std::string& groupPrefix)
const
79 if (groupPrefix ==
"")
85 auto compoundKey = groupPrefix +
"." + key;
86 if (params_.hasKey(compoundKey) || defaultParams_.hasKey(compoundKey))
90 if (compoundKey !=
"")
100 void report(std::ostream& stream = std::cout)
const
101 { params_.report(stream); }
111 stream <<
"\n# Runtime-specified parameters used:" << std::endl;
112 usedRuntimeParams_.report(stream);
114 stream <<
"\n# Global default parameters used:" << std::endl;
115 usedDefaultParams_.report(stream);
118 if (!unusedParams.empty())
120 stream <<
"\n# Unused parameters:" << std::endl;
121 for (
const auto& key : unusedParams)
122 stream << key <<
" = \"" << params_[key] <<
"\"" << std::endl;
138 const std::string& key,
139 const std::string& groupPrefix)
const
142 std::string prefix = groupPrefix;
143 auto dot = prefix.rfind(
".");
144 while (dot != std::string::npos)
146 prefix = prefix.substr(0, dot);
147 std::string compoundKey = prefix +
"." + key;
149 if (tree.hasKey(compoundKey))
153 dot = prefix.rfind(
".");
171 std::string
get(
const std::string& key,
const std::string& defaultValue)
const
173 if (params_.hasKey(key))
176 const auto returnValue = params_[key];
177 usedRuntimeParams_[key] = returnValue;
198 const std::string& key,
199 const std::string& defaultValue)
const
201 if (groupPrefix ==
"")
202 return get(key, defaultValue);
205 std::string compoundKey = groupPrefix +
"." + key;
206 if (params_.hasKey(compoundKey))
209 const auto returnValue = params_[compoundKey];
210 usedRuntimeParams_[compoundKey] = returnValue;
216 if (compoundKey !=
"")
219 const auto returnValue = params_[compoundKey];
220 usedRuntimeParams_[compoundKey] = returnValue;
225 return get(key, defaultValue);
240 std::string
get(
const std::string& key,
const char* defaultValue)
const
242 const std::string dv = defaultValue;
260 const std::string& key,
261 const char* defaultValue)
const
263 const std::string dv = defaultValue;
280 T
get(
const std::string& key,
const T& defaultValue)
const
282 if (params_.hasKey(key))
285 usedRuntimeParams_[key] = params_[key];
286 return params_.template get<T>(key);
307 const std::string& key,
308 const T& defaultValue)
const
310 if (groupPrefix ==
"")
311 return get<T>(key, defaultValue);
314 std::string compoundKey = groupPrefix +
"." + key;
315 if (params_.hasKey(compoundKey))
318 usedRuntimeParams_[compoundKey] = params_[compoundKey];
319 return params_.template get<T>(compoundKey);
324 if (compoundKey !=
"")
327 usedRuntimeParams_[compoundKey] = params_[compoundKey];
328 return params_.template get<T>(compoundKey);
332 return get<T>(key, defaultValue);
346 T
get(
const std::string& key)
const
348 if (params_.hasKey(key))
351 usedRuntimeParams_[key] = params_[key];
352 return params_.template get<T>(key);
355 else if(defaultParams_.hasKey(key))
358 usedDefaultParams_[key] = defaultParams_[key];
359 return defaultParams_.template get<T>(key);
379 const std::string& key)
const
381 if (groupPrefix ==
"")
385 std::string compoundKey = groupPrefix +
"." + key;
386 if (params_.hasKey(compoundKey))
389 usedRuntimeParams_[compoundKey] = params_[compoundKey];
390 return params_.template get<T>(compoundKey);
395 if (compoundKey !=
"")
398 usedRuntimeParams_[compoundKey] = params_[compoundKey];
399 return params_.template get<T>(compoundKey);
403 compoundKey = groupPrefix +
"." + key;
406 if (params_.hasKey(key))
409 usedRuntimeParams_[key] = params_[key];
410 return params_.template get<T>(key);
414 else if(defaultParams_.hasKey(compoundKey))
417 usedDefaultParams_[compoundKey] = defaultParams_[compoundKey];
418 return defaultParams_.template get<T>(compoundKey);
425 if (compoundKey !=
"")
428 usedDefaultParams_[compoundKey] = defaultParams_[compoundKey];
429 return defaultParams_.template get<T>(compoundKey);
432 if(defaultParams_.hasKey(key))
435 usedDefaultParams_[key] = defaultParams_[key];
436 return defaultParams_.template get<T>(key);
450 std::vector<std::string> unusedParams;
451 findUnusedKeys(params_, unusedParams);
462 void findUnusedKeys(
const Dune::ParameterTree& tree,
463 std::vector<std::string>& unusedParams,
464 const std::string& prefix =
"")
const
468 const auto& keys = tree.getValueKeys();
469 for (
const auto& key : keys)
470 if (key !=
"ParameterFile" && !usedRuntimeParams_.hasKey(prefix + key))
471 unusedParams.push_back(prefix + key);
474 const auto& subTreeKeys = tree.getSubKeys();
475 for (
const auto& key : subTreeKeys)
476 findUnusedKeys(tree.sub(key), unusedParams, prefix + key +
".");
479 const Dune::ParameterTree& params_;
480 const Dune::ParameterTree& defaultParams_;
483 mutable Dune::ParameterTree usedRuntimeParams_;
484 mutable Dune::ParameterTree usedDefaultParams_;
Some exceptions thrown in DuMux
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
std::string getFromGroup(const std::string &groupPrefix, const std::string &key, const std::string &defaultValue) const
get value as string, preferably from the sub-tree corresponding to a given prefix....
Definition: loggingparametertree.hh:197
T getFromGroup(const std::string &groupPrefix, const std::string &key, const T &defaultValue) const
get value as string, preferably from the sub-tree corresponding to a given prefix....
Definition: loggingparametertree.hh:306
void report(std::ostream &stream=std::cout) const
print the hierarchical parameter tree to stream
Definition: loggingparametertree.hh:100
std::string get(const std::string &key, const std::string &defaultValue) const
get value as string
Definition: loggingparametertree.hh:171
void reportAll(std::ostream &stream=std::cout) const
print distinct substructure to stream
Definition: loggingparametertree.hh:109
std::string findKeyInGroup(const Dune::ParameterTree &tree, const std::string &key, const std::string &groupPrefix) const
Do a backwards hierarchical search for a key in a group.
Definition: loggingparametertree.hh:137
std::string getFromGroup(const std::string &groupPrefix, const std::string &key, const char *defaultValue) const
get value as string, preferably from the sub-tree corresponding to a given prefix....
Definition: loggingparametertree.hh:259
bool hasKeyInGroup(const std::string &key, const std::string &groupPrefix) const
test for key in group
Definition: loggingparametertree.hh:76
std::string get(const std::string &key, const char *defaultValue) const
get value as string
Definition: loggingparametertree.hh:240
T get(const std::string &key) const
Get value.
Definition: loggingparametertree.hh:346
LoggingParameterTree(const Dune::ParameterTree ¶ms, const Dune::ParameterTree &defaultParams)
Definition: loggingparametertree.hh:52
std::vector< std::string > getUnusedKeys() const
Find the keys that haven't been used yet.
Definition: loggingparametertree.hh:448
T get(const std::string &key, const T &defaultValue) const
get value converted to a certain type
Definition: loggingparametertree.hh:280
LoggingParameterTree()=delete
T getFromGroup(const std::string &groupPrefix, const std::string &key) const
get value as string, preferably from the sub-tree corresponding to a given prefix....
Definition: loggingparametertree.hh:378
bool hasKey(const std::string &key) const
test for key
Definition: loggingparametertree.hh:62