12#ifndef DUMUX_LOGGING_PARAMETER_TREE_HH
13#define DUMUX_LOGGING_PARAMETER_TREE_HH
29#include <dune/common/parametertree.hh>
52 , defaultParams_(defaultParams)
53 , usedRuntimeParams_(std::make_unique<
Dune::ParameterTree>())
54 , usedDefaultParams_(std::make_unique<
Dune::ParameterTree>())
55 , usedFallbackDefaultParams_(std::make_unique<
Dune::ParameterTree>())
56 , distinctDefaultValues_(std::make_unique<std::map<std::string, std::vector<std::string>>>())
70 bool hasKey(
const std::string& key)
const
71 {
return params_.hasKey(key); }
89 const std::string& groupPrefix)
const
91 if (groupPrefix.empty())
97 auto compoundKey = groupPrefix +
"." + key;
98 if (params_.hasKey(compoundKey))
102 if (compoundKey !=
"")
132 std::string groupPrefix)
const
134 std::vector<std::string> groupNames;
136 if (!groupPrefix.empty())
138 auto compoundGroup = groupPrefix +
"." + subGroupName;
139 for (std::string::size_type dotPos = 0; dotPos != std::string::npos; dotPos = groupPrefix.rfind(
"."))
141 if (params_.hasSub(compoundGroup) || defaultParams_.hasSub(compoundGroup))
142 groupNames.push_back(compoundGroup);
144 groupPrefix.resize(dotPos);
145 compoundGroup = groupPrefix +
"." + subGroupName;
149 if (params_.hasSub(subGroupName) || defaultParams_.hasSub(subGroupName))
150 groupNames.push_back(subGroupName);
159 void report(std::ostream& stream = std::cout)
const
160 { params_.report(stream); }
170 stream <<
"\n# Runtime-specified parameters used:" << std::endl;
171 usedRuntimeParams_->report(stream);
173 stream <<
"\n# Global default parameters used:" << std::endl;
174 usedDefaultParams_->report(stream);
176 if (logFallbackDefaults_)
178 stream <<
"\n# Fallback default parameters used (hardcoded at the call site in the source code):" << std::endl;
179 usedFallbackDefaultParams_->report(stream);
181 const auto& distinctDefaultValues = *distinctDefaultValues_;
182 if (std::ranges::any_of(distinctDefaultValues, [](
const auto& entry){
return entry.second.size() > 1; }))
184 stream <<
"\n# Parameters used with different default values (at call sites or as global default):" << std::endl;
185 for (
const auto& [key, values] : distinctDefaultValues)
187 if (values.size() > 1)
189 stream << key <<
" =";
190 for (
const auto& value : values)
191 stream <<
" \"" << value <<
"\"";
199 if (!unusedParams.empty())
201 stream <<
"\n# Unused parameters:" << std::endl;
202 for (
const auto& key : unusedParams)
203 stream << key <<
" = \"" << params_[key] <<
"\"" << std::endl;
219 const std::string& key,
220 const std::string& groupPrefix)
const
223 std::string prefix = groupPrefix;
224 auto dot = prefix.rfind(
".");
225 while (dot != std::string::npos)
228 std::string compoundKey = prefix +
"." + key;
230 if (tree.hasKey(compoundKey))
234 dot = prefix.rfind(
".");
252 std::string
get(
const std::string& key,
const std::string& defaultValue)
const
254 if (params_.hasKey(key))
257 const auto returnValue = params_[key];
258 logUsedRuntimeParam_(key, returnValue);
262 if (logFallbackDefaults_)
263 logUsedFallbackDefaultParam_(key, defaultValue);
282 const std::string& key,
283 const std::string& defaultValue)
const
285 if (groupPrefix.empty())
286 return get(key, defaultValue);
289 std::string compoundKey = groupPrefix +
"." + key;
290 if (params_.hasKey(compoundKey))
293 const auto returnValue = params_[compoundKey];
294 logUsedRuntimeParam_(compoundKey, returnValue);
300 if (compoundKey !=
"")
303 const auto returnValue = params_[compoundKey];
304 logUsedRuntimeParam_(compoundKey, returnValue);
309 return get(key, defaultValue);
324 std::string
get(
const std::string& key,
const char* defaultValue)
const
326 const std::string dv = defaultValue;
344 const std::string& key,
345 const char* defaultValue)
const
347 const std::string dv = defaultValue;
364 T
get(
const std::string& key,
const T& defaultValue)
const
366 if (params_.hasKey(key))
369 logUsedRuntimeParam_(key, params_[key]);
370 return params_.template
get<T>(key);
373 if (logFallbackDefaults_)
374 logUsedFallbackDefaultParam_(key, defaultValue);
394 const std::string& key,
395 const T& defaultValue)
const
397 if (groupPrefix.empty())
398 return get<T>(key, defaultValue);
401 std::string compoundKey = groupPrefix +
"." + key;
402 if (params_.hasKey(compoundKey))
405 logUsedRuntimeParam_(compoundKey, params_[compoundKey]);
406 return params_.template
get<T>(compoundKey);
411 if (compoundKey !=
"")
414 logUsedRuntimeParam_(compoundKey, params_[compoundKey]);
415 return params_.template
get<T>(compoundKey);
419 return get<T>(key, defaultValue);
433 T
get(
const std::string& key)
const
435 if (params_.hasKey(key))
438 logUsedRuntimeParam_(key, params_[key]);
439 return params_.template
get<T>(key);
442 else if(defaultParams_.hasKey(key))
445 logUsedDefaultParam_(key, defaultParams_[key]);
446 return defaultParams_.template
get<T>(key);
466 const std::string& key)
const
468 if (groupPrefix.empty())
472 std::string compoundKey = groupPrefix +
"." + key;
473 if (params_.hasKey(compoundKey))
476 logUsedRuntimeParam_(compoundKey, params_[compoundKey]);
477 return params_.template
get<T>(compoundKey);
482 if (compoundKey !=
"")
485 logUsedRuntimeParam_(compoundKey, params_[compoundKey]);
486 return params_.template
get<T>(compoundKey);
490 compoundKey = groupPrefix +
"." + key;
493 if (params_.hasKey(key))
496 logUsedRuntimeParam_(key, params_[key]);
497 return params_.template
get<T>(key);
501 else if(defaultParams_.hasKey(compoundKey))
504 logUsedDefaultParam_(compoundKey, defaultParams_[compoundKey]);
505 return defaultParams_.template
get<T>(compoundKey);
512 if (compoundKey !=
"")
515 logUsedDefaultParam_(compoundKey, defaultParams_[compoundKey]);
516 return defaultParams_.template
get<T>(compoundKey);
519 if(defaultParams_.hasKey(key))
522 logUsedDefaultParam_(key, defaultParams_[key]);
523 return defaultParams_.template
get<T>(key);
526 DUNE_THROW(
Dumux::ParameterException,
"Key " << key <<
" not found in the parameter tree with group prefix " << groupPrefix);
537 std::vector<std::string> unusedParams;
538 findUnusedKeys_(params_, unusedParams);
546 std::scoped_lock lock{ usedRuntimeMutex_, usedDefaultMutex_, usedFallbackDefaultMutex_ };
547 *usedRuntimeParams_ = Dune::ParameterTree{};
548 *usedDefaultParams_ = Dune::ParameterTree{};
549 *usedFallbackDefaultParams_ = Dune::ParameterTree{};
550 distinctDefaultValues_->clear();
559 { logFallbackDefaults_ = enable; }
568 void findUnusedKeys_(
const Dune::ParameterTree& tree,
569 std::vector<std::string>& unusedParams,
570 const std::string& prefix =
"")
const
574 const auto& keys = tree.getValueKeys();
575 for (
const auto& key : keys)
576 if (key !=
"ParameterFile" && !usedRuntimeParams_->hasKey(prefix + key))
577 unusedParams.push_back(prefix + key);
580 const auto& subTreeKeys = tree.getSubKeys();
581 for (
const auto& key : subTreeKeys)
582 findUnusedKeys_(tree.sub(key), unusedParams, prefix + key +
".");
587 void logUsedRuntimeParam_(
const std::string& key,
const std::string& value)
const
589 std::scoped_lock lock{ usedRuntimeMutex_ };
590 usedRuntimeParams_->operator[](key) = value;
595 void logUsedDefaultParam_(
const std::string& key,
const std::string& value)
const
597 std::scoped_lock lock{ usedDefaultMutex_ };
598 usedDefaultParams_->operator[](key) = value;
605 void logUsedFallbackDefaultParam_(
const std::string& key,
const T& defaultValue)
const
607 const auto value = toParameterString_(defaultValue);
611 std::optional<std::string> globalDefault;
612 if (defaultParams_.hasKey(key))
615 try { globalDefault = toParameterString_(defaultParams_.template
get<T>(key)); }
616 catch (
const Dune::RangeError&) { globalDefault = defaultParams_[key]; }
619 std::scoped_lock lock{ usedFallbackDefaultMutex_ };
620 usedFallbackDefaultParams_->operator[](key) = *value;
622 auto& values = (*distinctDefaultValues_)[key];
623 const auto addIfNew = [&](
const std::string& v){
624 if (std::ranges::find(values, v) == values.end())
629 addIfNew(*globalDefault);
635 static std::optional<std::string> toParameterString_(
const T& value)
637 if constexpr (std::is_same_v<T, std::string>)
639 else if constexpr (std::is_same_v<T, bool>)
640 return value ?
"true" :
"false";
641 else if constexpr (std::is_same_v<T, float> || std::is_same_v<T, double>)
644 std::array<char, 32> buffer;
645 const auto end = std::to_chars(buffer.data(), buffer.data() + buffer.size(), value).ptr;
646 return std::string(buffer.data(), end);
649 else if constexpr (
requires { std::end(value);
requires !std::is_same_v<std::decay_t<
decltype(*std::begin(value))>, T>; })
652 for (
bool first =
true;
const auto& entry : value)
654 const auto entryString = toParameterString_(entry);
658 result += (first ?
"" :
" ") + *entryString;
663 else if constexpr (
requires (std::ostream& stream) { stream << value; })
665 std::ostringstream stream;
673 const Dune::ParameterTree& params_;
674 const Dune::ParameterTree& defaultParams_;
677 std::unique_ptr<Dune::ParameterTree> usedRuntimeParams_;
678 std::unique_ptr<Dune::ParameterTree> usedDefaultParams_;
679 std::unique_ptr<Dune::ParameterTree> usedFallbackDefaultParams_;
680 std::unique_ptr<std::map<std::string, std::vector<std::string>>> distinctDefaultValues_;
683 mutable std::mutex usedRuntimeMutex_;
684 mutable std::mutex usedDefaultMutex_;
685 mutable std::mutex usedFallbackDefaultMutex_;
687 bool logFallbackDefaults_ =
false;
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:281
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:393
void report(std::ostream &stream=std::cout) const
print the hierarchical parameter tree to stream
Definition loggingparametertree.hh:159
std::string get(const std::string &key, const std::string &defaultValue) const
get value as string
Definition loggingparametertree.hh:252
void reportAll(std::ostream &stream=std::cout) const
print distinct substructure to stream
Definition loggingparametertree.hh:168
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:218
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:343
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:131
bool hasKeyInGroup(const std::string &key, const std::string &groupPrefix) const
test for key in group
Definition loggingparametertree.hh:88
std::string get(const std::string &key, const char *defaultValue) const
get value as string
Definition loggingparametertree.hh:324
T get(const std::string &key) const
Get value.
Definition loggingparametertree.hh:433
LoggingParameterTree(const Dune::ParameterTree ¶ms, const Dune::ParameterTree &defaultParams)
Definition loggingparametertree.hh:50
void clearUsageLog()
Forget which parameters have been used.
Definition loggingparametertree.hh:544
std::vector< std::string > getUnusedKeys() const
Find the keys that haven't been used yet.
Definition loggingparametertree.hh:535
T get(const std::string &key, const T &defaultValue) const
get value converted to a certain type
Definition loggingparametertree.hh:364
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:465
void setLogFallbackDefaults(bool enable)
Also log parameters that fall back to the default value given at the call site.
Definition loggingparametertree.hh:558
bool hasKey(const std::string &key) const
test for key
Definition loggingparametertree.hh:70
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