12#ifndef DUMUX_LOGGING_PARAMETER_TREE_HH
13#define DUMUX_LOGGING_PARAMETER_TREE_HH
21#include <dune/common/parametertree.hh>
44 , defaultParams_(defaultParams)
45 , usedRuntimeParams_(std::make_unique<
Dune::ParameterTree>())
46 , usedDefaultParams_(std::make_unique<
Dune::ParameterTree>())
60 bool hasKey(
const std::string& key)
const
61 {
return params_.hasKey(key); }
79 const std::string& groupPrefix)
const
81 if (groupPrefix.empty())
87 auto compoundKey = groupPrefix +
"." + key;
88 if (params_.hasKey(compoundKey))
92 if (compoundKey !=
"")
122 std::string groupPrefix)
const
124 std::vector<std::string> groupNames;
126 if (!groupPrefix.empty())
128 auto compoundGroup = groupPrefix +
"." + subGroupName;
129 for (std::string::size_type dotPos = 0; dotPos != std::string::npos; dotPos = groupPrefix.rfind(
"."))
131 if (params_.hasSub(compoundGroup) || defaultParams_.hasSub(compoundGroup))
132 groupNames.push_back(compoundGroup);
134 groupPrefix = groupPrefix.substr(0, dotPos);
135 compoundGroup = groupPrefix +
"." + subGroupName;
139 if (params_.hasSub(subGroupName) || defaultParams_.hasSub(subGroupName))
140 groupNames.push_back(subGroupName);
149 void report(std::ostream& stream = std::cout)
const
150 { params_.report(stream); }
160 stream <<
"\n# Runtime-specified parameters used:" << std::endl;
161 usedRuntimeParams_->report(stream);
163 stream <<
"\n# Global default parameters used:" << std::endl;
164 usedDefaultParams_->report(stream);
167 if (!unusedParams.empty())
169 stream <<
"\n# Unused parameters:" << std::endl;
170 for (
const auto& key : unusedParams)
171 stream << key <<
" = \"" << params_[key] <<
"\"" << std::endl;
187 const std::string& key,
188 const std::string& groupPrefix)
const
191 std::string prefix = groupPrefix;
192 auto dot = prefix.rfind(
".");
193 while (dot != std::string::npos)
195 prefix = prefix.substr(0, dot);
196 std::string compoundKey = prefix +
"." + key;
198 if (tree.hasKey(compoundKey))
202 dot = prefix.rfind(
".");
220 std::string
get(
const std::string& key,
const std::string& defaultValue)
const
222 if (params_.hasKey(key))
225 const auto returnValue = params_[key];
226 logUsedRuntimeParam_(key, returnValue);
247 const std::string& key,
248 const std::string& defaultValue)
const
250 if (groupPrefix.empty())
251 return get(key, defaultValue);
254 std::string compoundKey = groupPrefix +
"." + key;
255 if (params_.hasKey(compoundKey))
258 const auto returnValue = params_[compoundKey];
259 logUsedRuntimeParam_(compoundKey, returnValue);
265 if (compoundKey !=
"")
268 const auto returnValue = params_[compoundKey];
269 logUsedRuntimeParam_(compoundKey, returnValue);
274 return get(key, defaultValue);
289 std::string
get(
const std::string& key,
const char* defaultValue)
const
291 const std::string dv = defaultValue;
309 const std::string& key,
310 const char* defaultValue)
const
312 const std::string dv = defaultValue;
329 T
get(
const std::string& key,
const T& defaultValue)
const
331 if (params_.hasKey(key))
334 logUsedRuntimeParam_(key, params_[key]);
335 return params_.template get<T>(key);
356 const std::string& key,
357 const T& defaultValue)
const
359 if (groupPrefix.empty())
360 return get<T>(key, defaultValue);
363 std::string compoundKey = groupPrefix +
"." + key;
364 if (params_.hasKey(compoundKey))
367 logUsedRuntimeParam_(compoundKey, params_[compoundKey]);
368 return params_.template get<T>(compoundKey);
373 if (compoundKey !=
"")
376 logUsedRuntimeParam_(compoundKey, params_[compoundKey]);
377 return params_.template get<T>(compoundKey);
381 return get<T>(key, defaultValue);
395 T
get(
const std::string& key)
const
397 if (params_.hasKey(key))
400 logUsedRuntimeParam_(key, params_[key]);
401 return params_.template get<T>(key);
404 else if(defaultParams_.hasKey(key))
407 logUsedDefaultParam_(key, defaultParams_[key]);
408 return defaultParams_.template get<T>(key);
428 const std::string& key)
const
430 if (groupPrefix.empty())
434 std::string compoundKey = groupPrefix +
"." + key;
435 if (params_.hasKey(compoundKey))
438 logUsedRuntimeParam_(compoundKey, params_[compoundKey]);
439 return params_.template get<T>(compoundKey);
444 if (compoundKey !=
"")
447 logUsedRuntimeParam_(compoundKey, params_[compoundKey]);
448 return params_.template get<T>(compoundKey);
452 compoundKey = groupPrefix +
"." + key;
455 if (params_.hasKey(key))
458 logUsedRuntimeParam_(key, params_[key]);
459 return params_.template get<T>(key);
463 else if(defaultParams_.hasKey(compoundKey))
466 logUsedDefaultParam_(compoundKey, defaultParams_[compoundKey]);
467 return defaultParams_.template get<T>(compoundKey);
474 if (compoundKey !=
"")
477 logUsedDefaultParam_(compoundKey, defaultParams_[compoundKey]);
478 return defaultParams_.template get<T>(compoundKey);
481 if(defaultParams_.hasKey(key))
484 logUsedDefaultParam_(key, defaultParams_[key]);
485 return defaultParams_.template get<T>(key);
499 std::vector<std::string> unusedParams;
500 findUnusedKeys_(params_, unusedParams);
511 void findUnusedKeys_(
const Dune::ParameterTree& tree,
512 std::vector<std::string>& unusedParams,
513 const std::string& prefix =
"")
const
517 const auto& keys = tree.getValueKeys();
518 for (
const auto& key : keys)
519 if (key !=
"ParameterFile" && !usedRuntimeParams_->hasKey(prefix + key))
520 unusedParams.push_back(prefix + key);
523 const auto& subTreeKeys = tree.getSubKeys();
524 for (
const auto& key : subTreeKeys)
525 findUnusedKeys_(tree.sub(key), unusedParams, prefix + key +
".");
530 void logUsedRuntimeParam_(
const std::string& key,
const std::string& value)
const
532 std::scoped_lock lock{ usedRuntimeMutex_ };
533 usedRuntimeParams_->operator[](key) = value;
538 void logUsedDefaultParam_(
const std::string& key,
const std::string& value)
const
540 std::scoped_lock lock{ usedDefaultMutex_ };
541 usedDefaultParams_->operator[](key) = value;
544 const Dune::ParameterTree& params_;
545 const Dune::ParameterTree& defaultParams_;
548 std::unique_ptr<Dune::ParameterTree> usedRuntimeParams_;
549 std::unique_ptr<Dune::ParameterTree> usedDefaultParams_;
552 mutable std::mutex usedRuntimeMutex_;
553 mutable std::mutex usedDefaultMutex_;
A parameter tree that logs which parameters have been used.
Definition: loggingparametertree.hh:31
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:246
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:355
void report(std::ostream &stream=std::cout) const
print the hierarchical parameter tree to stream
Definition: loggingparametertree.hh:149
std::string get(const std::string &key, const std::string &defaultValue) const
get value as string
Definition: loggingparametertree.hh:220
void reportAll(std::ostream &stream=std::cout) const
print distinct substructure to stream
Definition: loggingparametertree.hh:158
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:186
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:308
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:121
bool hasKeyInGroup(const std::string &key, const std::string &groupPrefix) const
test for key in group
Definition: loggingparametertree.hh:78
std::string get(const std::string &key, const char *defaultValue) const
get value as string
Definition: loggingparametertree.hh:289
T get(const std::string &key) const
Get value.
Definition: loggingparametertree.hh:395
LoggingParameterTree(const Dune::ParameterTree ¶ms, const Dune::ParameterTree &defaultParams)
Definition: loggingparametertree.hh:42
std::vector< std::string > getUnusedKeys() const
Find the keys that haven't been used yet.
Definition: loggingparametertree.hh:497
T get(const std::string &key, const T &defaultValue) const
get value converted to a certain type
Definition: loggingparametertree.hh:329
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:427
bool hasKey(const std::string &key) const
test for key
Definition: loggingparametertree.hh:60
Exception thrown if a run-time parameter is not specified correctly.
Definition: exceptions.hh:48
Some exceptions thrown in DuMux
Definition: common/pdesolver.hh:24