24#ifndef DUMUX_LOGGING_PARAMETER_TREE_HH
25#define DUMUX_LOGGING_PARAMETER_TREE_HH
33#include <dune/common/parametertree.hh>
56 , defaultParams_(defaultParams)
57 , usedRuntimeParams_(std::make_unique<
Dune::ParameterTree>())
58 , usedDefaultParams_(std::make_unique<
Dune::ParameterTree>())
72 bool hasKey(
const std::string& key)
const
73 {
return params_.hasKey(key); }
91 const std::string& groupPrefix)
const
93 if (groupPrefix.empty())
99 auto compoundKey = groupPrefix +
"." + key;
100 if (params_.hasKey(compoundKey))
104 if (compoundKey !=
"")
134 std::string groupPrefix)
const
136 std::vector<std::string> groupNames;
138 if (!groupPrefix.empty())
140 auto compoundGroup = groupPrefix +
"." + subGroupName;
141 for (std::string::size_type dotPos = 0; dotPos != std::string::npos; dotPos = groupPrefix.rfind(
"."))
143 if (params_.hasSub(compoundGroup) || defaultParams_.hasSub(compoundGroup))
144 groupNames.push_back(compoundGroup);
146 groupPrefix = groupPrefix.substr(0, dotPos);
147 compoundGroup = groupPrefix +
"." + subGroupName;
151 if (params_.hasSub(subGroupName) || defaultParams_.hasSub(subGroupName))
152 groupNames.push_back(subGroupName);
161 void report(std::ostream& stream = std::cout)
const
162 { params_.report(stream); }
172 stream <<
"\n# Runtime-specified parameters used:" << std::endl;
173 usedRuntimeParams_->report(stream);
175 stream <<
"\n# Global default parameters used:" << std::endl;
176 usedDefaultParams_->report(stream);
179 if (!unusedParams.empty())
181 stream <<
"\n# Unused parameters:" << std::endl;
182 for (
const auto& key : unusedParams)
183 stream << key <<
" = \"" << params_[key] <<
"\"" << std::endl;
199 const std::string& key,
200 const std::string& groupPrefix)
const
203 std::string prefix = groupPrefix;
204 auto dot = prefix.rfind(
".");
205 while (dot != std::string::npos)
207 prefix = prefix.substr(0, dot);
208 std::string compoundKey = prefix +
"." + key;
210 if (tree.hasKey(compoundKey))
214 dot = prefix.rfind(
".");
232 std::string
get(
const std::string& key,
const std::string& defaultValue)
const
234 if (params_.hasKey(key))
237 const auto returnValue = params_[key];
238 logUsedRuntimeParam_(key, returnValue);
259 const std::string& key,
260 const std::string& defaultValue)
const
262 if (groupPrefix.empty())
263 return get(key, defaultValue);
266 std::string compoundKey = groupPrefix +
"." + key;
267 if (params_.hasKey(compoundKey))
270 const auto returnValue = params_[compoundKey];
271 logUsedRuntimeParam_(compoundKey, returnValue);
277 if (compoundKey !=
"")
280 const auto returnValue = params_[compoundKey];
281 logUsedRuntimeParam_(compoundKey, returnValue);
286 return get(key, defaultValue);
301 std::string
get(
const std::string& key,
const char* defaultValue)
const
303 const std::string dv = defaultValue;
321 const std::string& key,
322 const char* defaultValue)
const
324 const std::string dv = defaultValue;
341 T
get(
const std::string& key,
const T& defaultValue)
const
343 if (params_.hasKey(key))
346 logUsedRuntimeParam_(key, params_[key]);
347 return params_.template get<T>(key);
368 const std::string& key,
369 const T& defaultValue)
const
371 if (groupPrefix.empty())
372 return get<T>(key, defaultValue);
375 std::string compoundKey = groupPrefix +
"." + key;
376 if (params_.hasKey(compoundKey))
379 logUsedRuntimeParam_(compoundKey, params_[compoundKey]);
380 return params_.template get<T>(compoundKey);
385 if (compoundKey !=
"")
388 logUsedRuntimeParam_(compoundKey, params_[compoundKey]);
389 return params_.template get<T>(compoundKey);
393 return get<T>(key, defaultValue);
407 T
get(
const std::string& key)
const
409 if (params_.hasKey(key))
412 logUsedRuntimeParam_(key, params_[key]);
413 return params_.template get<T>(key);
416 else if(defaultParams_.hasKey(key))
419 logUsedDefaultParam_(key, defaultParams_[key]);
420 return defaultParams_.template get<T>(key);
440 const std::string& key)
const
442 if (groupPrefix.empty())
446 std::string compoundKey = groupPrefix +
"." + key;
447 if (params_.hasKey(compoundKey))
450 logUsedRuntimeParam_(compoundKey, params_[compoundKey]);
451 return params_.template get<T>(compoundKey);
456 if (compoundKey !=
"")
459 logUsedRuntimeParam_(compoundKey, params_[compoundKey]);
460 return params_.template get<T>(compoundKey);
464 compoundKey = groupPrefix +
"." + key;
467 if (params_.hasKey(key))
470 logUsedRuntimeParam_(key, params_[key]);
471 return params_.template get<T>(key);
475 else if(defaultParams_.hasKey(compoundKey))
478 logUsedDefaultParam_(compoundKey, defaultParams_[compoundKey]);
479 return defaultParams_.template get<T>(compoundKey);
486 if (compoundKey !=
"")
489 logUsedDefaultParam_(compoundKey, defaultParams_[compoundKey]);
490 return defaultParams_.template get<T>(compoundKey);
493 if(defaultParams_.hasKey(key))
496 logUsedDefaultParam_(key, defaultParams_[key]);
497 return defaultParams_.template get<T>(key);
511 std::vector<std::string> unusedParams;
512 findUnusedKeys_(params_, unusedParams);
523 void findUnusedKeys_(
const Dune::ParameterTree& tree,
524 std::vector<std::string>& unusedParams,
525 const std::string& prefix =
"")
const
529 const auto& keys = tree.getValueKeys();
530 for (
const auto& key : keys)
531 if (key !=
"ParameterFile" && !usedRuntimeParams_->hasKey(prefix + key))
532 unusedParams.push_back(prefix + key);
535 const auto& subTreeKeys = tree.getSubKeys();
536 for (
const auto& key : subTreeKeys)
537 findUnusedKeys_(tree.sub(key), unusedParams, prefix + key +
".");
542 void logUsedRuntimeParam_(
const std::string& key,
const std::string& value)
const
544 std::scoped_lock lock{ usedRuntimeMutex_ };
545 usedRuntimeParams_->operator[](key) = value;
550 void logUsedDefaultParam_(
const std::string& key,
const std::string& value)
const
552 std::scoped_lock lock{ usedDefaultMutex_ };
553 usedDefaultParams_->operator[](key) = value;
556 const Dune::ParameterTree& params_;
557 const Dune::ParameterTree& defaultParams_;
560 std::unique_ptr<Dune::ParameterTree> usedRuntimeParams_;
561 std::unique_ptr<Dune::ParameterTree> usedDefaultParams_;
564 mutable std::mutex usedRuntimeMutex_;
565 mutable std::mutex usedDefaultMutex_;
Some exceptions thrown in DuMux
Definition: common/pdesolver.hh:36
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:43
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:258
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:367
void report(std::ostream &stream=std::cout) const
print the hierarchical parameter tree to stream
Definition: loggingparametertree.hh:161
std::string get(const std::string &key, const std::string &defaultValue) const
get value as string
Definition: loggingparametertree.hh:232
void reportAll(std::ostream &stream=std::cout) const
print distinct substructure to stream
Definition: loggingparametertree.hh:170
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:198
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:320
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:133
bool hasKeyInGroup(const std::string &key, const std::string &groupPrefix) const
test for key in group
Definition: loggingparametertree.hh:90
std::string get(const std::string &key, const char *defaultValue) const
get value as string
Definition: loggingparametertree.hh:301
T get(const std::string &key) const
Get value.
Definition: loggingparametertree.hh:407
LoggingParameterTree(const Dune::ParameterTree ¶ms, const Dune::ParameterTree &defaultParams)
Definition: loggingparametertree.hh:54
std::vector< std::string > getUnusedKeys() const
Find the keys that haven't been used yet.
Definition: loggingparametertree.hh:509
T get(const std::string &key, const T &defaultValue) const
get value converted to a certain type
Definition: loggingparametertree.hh:341
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:439
bool hasKey(const std::string &key) const
test for key
Definition: loggingparametertree.hh:72