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) {}
66 bool hasKey(
const std::string& key)
const
67 {
return params_.hasKey(key); }
85 const std::string& groupPrefix)
const
87 if (groupPrefix.empty())
93 auto compoundKey = groupPrefix +
"." + key;
94 if (params_.hasKey(compoundKey))
98 if (compoundKey !=
"")
128 std::string groupPrefix)
const
130 std::vector<std::string> groupNames;
132 if (!groupPrefix.empty())
134 auto compoundGroup = groupPrefix +
"." + subGroupName;
135 for (std::string::size_type dotPos = 0; dotPos != std::string::npos; dotPos = groupPrefix.rfind(
"."))
137 if (params_.hasSub(compoundGroup) || defaultParams_.hasSub(compoundGroup))
138 groupNames.push_back(compoundGroup);
140 groupPrefix = groupPrefix.substr(0, dotPos);
141 compoundGroup = groupPrefix +
"." + subGroupName;
145 if (params_.hasSub(subGroupName) || defaultParams_.hasSub(subGroupName))
146 groupNames.push_back(subGroupName);
155 void report(std::ostream& stream = std::cout)
const
156 { params_.report(stream); }
166 stream <<
"\n# Runtime-specified parameters used:" << std::endl;
167 usedRuntimeParams_.report(stream);
169 stream <<
"\n# Global default parameters used:" << std::endl;
170 usedDefaultParams_.report(stream);
173 if (!unusedParams.empty())
175 stream <<
"\n# Unused parameters:" << std::endl;
176 for (
const auto& key : unusedParams)
177 stream << key <<
" = \"" << params_[key] <<
"\"" << std::endl;
193 const std::string& key,
194 const std::string& groupPrefix)
const
197 std::string prefix = groupPrefix;
198 auto dot = prefix.rfind(
".");
199 while (dot != std::string::npos)
201 prefix = prefix.substr(0, dot);
202 std::string compoundKey = prefix +
"." + key;
204 if (tree.hasKey(compoundKey))
208 dot = prefix.rfind(
".");
226 std::string
get(
const std::string& key,
const std::string& defaultValue)
const
228 if (params_.hasKey(key))
231 const auto returnValue = params_[key];
232 usedRuntimeParams_[key] = returnValue;
253 const std::string& key,
254 const std::string& defaultValue)
const
256 if (groupPrefix.empty())
257 return get(key, defaultValue);
260 std::string compoundKey = groupPrefix +
"." + key;
261 if (params_.hasKey(compoundKey))
264 const auto returnValue = params_[compoundKey];
265 usedRuntimeParams_[compoundKey] = returnValue;
271 if (compoundKey !=
"")
274 const auto returnValue = params_[compoundKey];
275 usedRuntimeParams_[compoundKey] = returnValue;
280 return get(key, defaultValue);
295 std::string
get(
const std::string& key,
const char* defaultValue)
const
297 const std::string dv = defaultValue;
315 const std::string& key,
316 const char* defaultValue)
const
318 const std::string dv = defaultValue;
335 T
get(
const std::string& key,
const T& defaultValue)
const
337 if (params_.hasKey(key))
340 usedRuntimeParams_[key] = params_[key];
341 return params_.template get<T>(key);
362 const std::string& key,
363 const T& defaultValue)
const
365 if (groupPrefix.empty())
366 return get<T>(key, defaultValue);
369 std::string compoundKey = groupPrefix +
"." + key;
370 if (params_.hasKey(compoundKey))
373 usedRuntimeParams_[compoundKey] = params_[compoundKey];
374 return params_.template get<T>(compoundKey);
379 if (compoundKey !=
"")
382 usedRuntimeParams_[compoundKey] = params_[compoundKey];
383 return params_.template get<T>(compoundKey);
387 return get<T>(key, defaultValue);
401 T
get(
const std::string& key)
const
403 if (params_.hasKey(key))
406 usedRuntimeParams_[key] = params_[key];
407 return params_.template get<T>(key);
410 else if(defaultParams_.hasKey(key))
413 usedDefaultParams_[key] = defaultParams_[key];
414 return defaultParams_.template get<T>(key);
434 const std::string& key)
const
436 if (groupPrefix.empty())
440 std::string compoundKey = groupPrefix +
"." + key;
441 if (params_.hasKey(compoundKey))
444 usedRuntimeParams_[compoundKey] = params_[compoundKey];
445 return params_.template get<T>(compoundKey);
450 if (compoundKey !=
"")
453 usedRuntimeParams_[compoundKey] = params_[compoundKey];
454 return params_.template get<T>(compoundKey);
458 compoundKey = groupPrefix +
"." + key;
461 if (params_.hasKey(key))
464 usedRuntimeParams_[key] = params_[key];
465 return params_.template get<T>(key);
469 else if(defaultParams_.hasKey(compoundKey))
472 usedDefaultParams_[compoundKey] = defaultParams_[compoundKey];
473 return defaultParams_.template get<T>(compoundKey);
480 if (compoundKey !=
"")
483 usedDefaultParams_[compoundKey] = defaultParams_[compoundKey];
484 return defaultParams_.template get<T>(compoundKey);
487 if(defaultParams_.hasKey(key))
490 usedDefaultParams_[key] = defaultParams_[key];
491 return defaultParams_.template get<T>(key);
505 std::vector<std::string> unusedParams;
506 findUnusedKeys_(params_, unusedParams);
517 void findUnusedKeys_(
const Dune::ParameterTree& tree,
518 std::vector<std::string>& unusedParams,
519 const std::string& prefix =
"")
const
523 const auto& keys = tree.getValueKeys();
524 for (
const auto& key : keys)
525 if (key !=
"ParameterFile" && !usedRuntimeParams_.hasKey(prefix + key))
526 unusedParams.push_back(prefix + key);
529 const auto& subTreeKeys = tree.getSubKeys();
530 for (
const auto& key : subTreeKeys)
531 findUnusedKeys_(tree.sub(key), unusedParams, prefix + key +
".");
534 const Dune::ParameterTree& params_;
535 const Dune::ParameterTree& defaultParams_;
538 mutable Dune::ParameterTree usedRuntimeParams_;
539 mutable Dune::ParameterTree usedDefaultParams_;
Some exceptions thrown in DuMux
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:252
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:361
void report(std::ostream &stream=std::cout) const
print the hierarchical parameter tree to stream
Definition: loggingparametertree.hh:155
std::string get(const std::string &key, const std::string &defaultValue) const
get value as string
Definition: loggingparametertree.hh:226
void reportAll(std::ostream &stream=std::cout) const
print distinct substructure to stream
Definition: loggingparametertree.hh:164
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:192
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:314
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
std::string get(const std::string &key, const char *defaultValue) const
get value as string
Definition: loggingparametertree.hh:295
T get(const std::string &key) const
Get value.
Definition: loggingparametertree.hh:401
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:503
T get(const std::string &key, const T &defaultValue) const
get value converted to a certain type
Definition: loggingparametertree.hh:335
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:433
bool hasKey(const std::string &key) const
test for key
Definition: loggingparametertree.hh:66