mirror of
https://github.com/OPM/opm-simulators.git
synced 2026-09-05 04:40:19 -05:00
Merge pull request #916 from akva2/typetag_free_param_system
Add a typetag-free parameter system
This commit is contained in:
@@ -56,11 +56,13 @@ namespace Opm::Properties {
|
||||
|
||||
// Create new type tags
|
||||
namespace TTag {
|
||||
|
||||
//! Type tag for all models.
|
||||
struct NumericModel { using InheritsFrom = std::tuple<ParameterSystem>; };
|
||||
struct NumericModel {};
|
||||
|
||||
//! Type tag for all fully coupled models.
|
||||
struct ImplicitModel { using InheritsFrom = std::tuple<NumericModel>; };
|
||||
|
||||
} // end namespace TTag
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
+327
-137
@@ -26,8 +26,8 @@
|
||||
* \brief This file provides the infrastructure to retrieve run-time parameters
|
||||
*
|
||||
* Internally, runtime parameters are implemented using
|
||||
* Dune::ParameterTree with the default value taken from the property
|
||||
* system.
|
||||
* Dune::ParameterTree with the default value taken from the parameter
|
||||
* definition.
|
||||
*/
|
||||
#ifndef OPM_PARAMETER_SYSTEM_HH
|
||||
#define OPM_PARAMETER_SYSTEM_HH
|
||||
@@ -41,6 +41,7 @@
|
||||
#include <dune/common/classname.hh>
|
||||
#include <dune/common/parametertree.hh>
|
||||
|
||||
#include <charconv>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
@@ -55,8 +56,35 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
namespace Opm {
|
||||
namespace Parameters {
|
||||
namespace Opm::Parameters {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename, class = void>
|
||||
struct has_name : public std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct has_name<T, std::void_t<decltype(std::declval<T>().name)>>
|
||||
: public std::true_type {};
|
||||
|
||||
//! get the name data member of a parameter
|
||||
template<class Parameter>
|
||||
auto getParamName()
|
||||
{
|
||||
if constexpr (has_name<Parameter>::value) {
|
||||
return Parameter::name;
|
||||
} else {
|
||||
std::string paramName = Dune::className<Parameter>();
|
||||
paramName.replace(0, std::strlen("Opm::Parameters::"), "");
|
||||
const auto pos = paramName.find_first_of('<');
|
||||
if (pos != std::string::npos) {
|
||||
paramName.erase(pos);
|
||||
}
|
||||
return paramName;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct ParamInfo
|
||||
{
|
||||
@@ -95,6 +123,41 @@ struct ParamInfo
|
||||
template <class TypeTag, template<class,class> class Property>
|
||||
auto get(bool errorIfNotRegistered = true);
|
||||
|
||||
/*!
|
||||
* \ingroup Parameter
|
||||
*
|
||||
* \brief Retrieve a runtime parameter.
|
||||
*
|
||||
* The default value is specified in the parameter struct.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* \code
|
||||
* // Retrieves value UpwindWeight, default
|
||||
* // is taken from the property UpwindWeight
|
||||
* ::Opm::Parameters::get<::Opm::Parameters::UpwindWeight>();
|
||||
* \endcode
|
||||
*/
|
||||
template <class Param>
|
||||
auto Get(bool errorIfNotRegistered = true);
|
||||
|
||||
/*!
|
||||
* \ingroup Parameter
|
||||
*
|
||||
* \brief Set a runtime parameter.
|
||||
*
|
||||
* Override the default value specified.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* \code
|
||||
* // Set the value UpwindWeight
|
||||
* ::Opm::Parameters::Set<::Opm::Parameters::UpwindWeight>(3.0);
|
||||
* \endcode
|
||||
*/
|
||||
template <class Param>
|
||||
auto SetDefault(decltype(Param::value) new_value);
|
||||
|
||||
class ParamRegFinalizerBase_
|
||||
{
|
||||
public:
|
||||
@@ -104,7 +167,7 @@ public:
|
||||
};
|
||||
|
||||
template <class TypeTag, template<class,class> class Property>
|
||||
class ParamRegFinalizer_ : public ParamRegFinalizerBase_
|
||||
class ParamRegFinalizerTT_ : public ParamRegFinalizerBase_
|
||||
{
|
||||
public:
|
||||
void retrieve() override
|
||||
@@ -114,40 +177,33 @@ public:
|
||||
std::ignore = get<TypeTag, Property>(/*errorIfNotRegistered=*/true);
|
||||
}
|
||||
};
|
||||
} // namespace Parameters
|
||||
|
||||
} // namespace Opm
|
||||
template <class Param>
|
||||
class ParamRegFinalizer_ : public ParamRegFinalizerBase_
|
||||
{
|
||||
public:
|
||||
void retrieve() override
|
||||
{
|
||||
// retrieve the parameter once to make sure that its value does
|
||||
// not contain a syntax error.
|
||||
std::ignore = Get<Param>(/*errorIfNotRegistered=*/true);
|
||||
}
|
||||
};
|
||||
|
||||
namespace Opm::Properties {
|
||||
|
||||
namespace TTag {
|
||||
|
||||
// type tag which is supposed to spliced in or inherited from if the
|
||||
// parameter system is to be used
|
||||
struct ParameterSystem {};
|
||||
|
||||
} // namespace TTag
|
||||
|
||||
template<class TypeTag, class MyTypeTag>
|
||||
struct ParameterMetaData { using type = UndefinedProperty; };
|
||||
|
||||
|
||||
//! Set the ParameterMetaData property
|
||||
template<class TypeTag>
|
||||
struct ParameterMetaData<TypeTag, TTag::ParameterSystem>
|
||||
struct MetaData
|
||||
{
|
||||
using type = Dune::ParameterTree;
|
||||
|
||||
static Dune::ParameterTree& tree()
|
||||
{ return *storage_().tree; }
|
||||
|
||||
static std::map<std::string, ::Opm::Parameters::ParamInfo>& mutableRegistry()
|
||||
static std::map<std::string, ParamInfo>& mutableRegistry()
|
||||
{ return storage_().registry; }
|
||||
|
||||
static const std::map<std::string, ::Opm::Parameters::ParamInfo>& registry()
|
||||
static const std::map<std::string, ParamInfo>& registry()
|
||||
{ return storage_().registry; }
|
||||
|
||||
static std::list<std::unique_ptr<::Opm::Parameters::ParamRegFinalizerBase_> > ®istrationFinalizers()
|
||||
static std::list<std::unique_ptr<ParamRegFinalizerBase_>> ®istrationFinalizers()
|
||||
{ return storage_().finalizers; }
|
||||
|
||||
static bool& registrationOpen()
|
||||
@@ -155,7 +211,7 @@ struct ParameterMetaData<TypeTag, TTag::ParameterSystem>
|
||||
|
||||
static void clear()
|
||||
{
|
||||
storage_().tree.reset(new Dune::ParameterTree());
|
||||
storage_().tree = std::make_unique<Dune::ParameterTree>();
|
||||
storage_().finalizers.clear();
|
||||
storage_().registrationOpen = true;
|
||||
storage_().registry.clear();
|
||||
@@ -166,28 +222,27 @@ private:
|
||||
// member functions of the ParameterMetaData property class triggers a bug in clang
|
||||
// 3.5's address sanitizer which causes these variables to be initialized multiple
|
||||
// times...
|
||||
struct Storage_ {
|
||||
struct Storage_
|
||||
{
|
||||
Storage_()
|
||||
{
|
||||
tree.reset(new Dune::ParameterTree());
|
||||
tree = std::make_unique<Dune::ParameterTree>();
|
||||
registrationOpen = true;
|
||||
}
|
||||
|
||||
std::unique_ptr<Dune::ParameterTree> tree;
|
||||
std::map<std::string, ::Opm::Parameters::ParamInfo> registry;
|
||||
std::list<std::unique_ptr<::Opm::Parameters::ParamRegFinalizerBase_> > finalizers;
|
||||
std::map<std::string, ParamInfo> registry;
|
||||
std::list<std::unique_ptr<ParamRegFinalizerBase_>> finalizers;
|
||||
bool registrationOpen;
|
||||
};
|
||||
static Storage_& storage_() {
|
||||
|
||||
static Storage_& storage_()
|
||||
{
|
||||
static Storage_ obj;
|
||||
return obj;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Opm::Properties
|
||||
|
||||
namespace Opm::Parameters {
|
||||
|
||||
// function prototype declarations
|
||||
void printParamUsage_(std::ostream& os, const ParamInfo& paramInfo);
|
||||
void getFlattenedKeyList_(std::list<std::string>& dest,
|
||||
@@ -365,15 +420,14 @@ inline void getFlattenedKeyList_(std::list<std::string>& dest,
|
||||
}
|
||||
|
||||
// print the values of a list of parameters
|
||||
template <class TypeTag>
|
||||
void printParamList_(std::ostream& os, const std::list<std::string>& keyList, bool printDefaults = false)
|
||||
inline void printParamList_(std::ostream& os,
|
||||
const std::list<std::string>& keyList,
|
||||
bool printDefaults = false)
|
||||
{
|
||||
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
|
||||
|
||||
const Dune::ParameterTree& tree = ParamsMeta::tree();
|
||||
const Dune::ParameterTree& tree = MetaData::tree();
|
||||
|
||||
for (const auto& key : keyList) {
|
||||
const auto& paramInfo = ParamsMeta::registry().at(key);
|
||||
const auto& paramInfo = MetaData::registry().at(key);
|
||||
const std::string& defaultValue = paramInfo.compileTimeValue;
|
||||
std::string value = defaultValue;
|
||||
if (tree.hasKey(key))
|
||||
@@ -396,17 +450,13 @@ void printParamList_(std::ostream& os, const std::list<std::string>& keyList, bo
|
||||
* \param errorMsg The error message to be printed, if any
|
||||
* \param os The \c std::ostream which should be used.
|
||||
*/
|
||||
template <class TypeTag>
|
||||
void printUsage(const std::string& helpPreamble,
|
||||
const std::string& errorMsg = "",
|
||||
std::ostream& os = std::cerr,
|
||||
const bool showAll = false)
|
||||
inline void printUsage(const std::string& helpPreamble,
|
||||
const std::string& errorMsg = "",
|
||||
std::ostream& os = std::cerr,
|
||||
const bool showAll = false)
|
||||
{
|
||||
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
|
||||
|
||||
if (errorMsg != "") {
|
||||
os << errorMsg << "\n"
|
||||
<< "\n";
|
||||
if (!errorMsg.empty()) {
|
||||
os << errorMsg << "\n\n";
|
||||
}
|
||||
|
||||
os << breakLines_(helpPreamble, /*indent=*/2, /*maxWidth=*/getTtyWidth_());
|
||||
@@ -424,11 +474,9 @@ void printUsage(const std::string& helpPreamble,
|
||||
printParamUsage_(os, pInfo);
|
||||
}
|
||||
|
||||
auto paramIt = ParamsMeta::registry().begin();
|
||||
const auto& paramEndIt = ParamsMeta::registry().end();
|
||||
for (; paramIt != paramEndIt; ++paramIt) {
|
||||
if (showAll || !paramIt->second.isHidden)
|
||||
printParamUsage_(os, paramIt->second);
|
||||
for (const auto& param : MetaData::registry()) {
|
||||
if (showAll || !param.second.isHidden)
|
||||
printParamUsage_(os, param.second);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -567,24 +615,22 @@ inline std::string parseUnquotedValue_(std::string& s, const std::string&)
|
||||
* \return Empty string if everything worked out. Otherwise the thing that could
|
||||
* not be read.
|
||||
*/
|
||||
template <class TypeTag, class PositionalArgumentCallback>
|
||||
template <class PositionalArgumentCallback>
|
||||
std::string parseCommandLineOptions(int argc,
|
||||
const char **argv,
|
||||
const std::string& helpPreamble = "",
|
||||
const PositionalArgumentCallback& posArgCallback = noPositionalParameters_)
|
||||
{
|
||||
Dune::ParameterTree& paramTree = GetProp<TypeTag, Properties::ParameterMetaData>::tree();
|
||||
|
||||
// handle the "--help" parameter
|
||||
if (!helpPreamble.empty()) {
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (std::string("-h") == argv[i]
|
||||
|| std::string("--help") == argv[i]) {
|
||||
printUsage<TypeTag>(helpPreamble, /*errorMsg=*/"", std::cout);
|
||||
printUsage(helpPreamble, /*errorMsg=*/"", std::cout);
|
||||
return "Help called";
|
||||
}
|
||||
if (std::string("--help-all") == argv[i]) {
|
||||
printUsage<TypeTag>(helpPreamble, /*errorMsg=*/"", std::cout, true);
|
||||
printUsage(helpPreamble, /*errorMsg=*/"", std::cout, true);
|
||||
return "Help called";
|
||||
}
|
||||
}
|
||||
@@ -599,13 +645,14 @@ std::string parseCommandLineOptions(int argc,
|
||||
|| argv[i][1] != '-')
|
||||
{
|
||||
std::string errorMsg;
|
||||
int numHandled = posArgCallback(seenKeys, errorMsg, argc, argv, i, numPositionalParams);
|
||||
int numHandled = posArgCallback(seenKeys, errorMsg, argc, argv,
|
||||
i, numPositionalParams);
|
||||
|
||||
if (numHandled < 1) {
|
||||
std::ostringstream oss;
|
||||
|
||||
if (!helpPreamble.empty())
|
||||
printUsage<TypeTag>(helpPreamble, errorMsg, std::cerr);
|
||||
printUsage(helpPreamble, errorMsg, std::cerr);
|
||||
|
||||
return errorMsg;
|
||||
}
|
||||
@@ -630,7 +677,7 @@ std::string parseCommandLineOptions(int argc,
|
||||
<< "is invalid because it does not start with a letter.";
|
||||
|
||||
if (!helpPreamble.empty())
|
||||
printUsage<TypeTag>(helpPreamble, oss.str(), std::cerr);
|
||||
printUsage(helpPreamble, oss.str(), std::cerr);
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
@@ -646,7 +693,7 @@ std::string parseCommandLineOptions(int argc,
|
||||
"command line parameter";
|
||||
|
||||
if (!helpPreamble.empty())
|
||||
printUsage<TypeTag>(helpPreamble, msg, std::cerr);
|
||||
printUsage(helpPreamble, msg, std::cerr);
|
||||
return msg;
|
||||
}
|
||||
seenKeys.insert(paramName);
|
||||
@@ -657,14 +704,14 @@ std::string parseCommandLineOptions(int argc,
|
||||
+" Please use "+argv[i]+"=value.";
|
||||
|
||||
if (!helpPreamble.empty())
|
||||
printUsage<TypeTag>(helpPreamble, msg, std::cerr);
|
||||
printUsage(helpPreamble, msg, std::cerr);
|
||||
return msg;
|
||||
}
|
||||
|
||||
paramValue = s.substr(1);
|
||||
|
||||
// Put the key=value pair into the parameter tree
|
||||
paramTree[paramName] = paramValue;
|
||||
MetaData::tree()[paramName] = paramValue;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
@@ -675,11 +722,8 @@ std::string parseCommandLineOptions(int argc,
|
||||
*
|
||||
* This function does some basic syntax checks.
|
||||
*/
|
||||
template <class TypeTag>
|
||||
void parseParameterFile(const std::string& fileName, bool overwrite = true)
|
||||
inline void parseParameterFile(const std::string& fileName, bool overwrite = true)
|
||||
{
|
||||
Dune::ParameterTree& paramTree = GetProp<TypeTag, Properties::ParameterMetaData>::tree();
|
||||
|
||||
std::set<std::string> seenKeys;
|
||||
std::ifstream ifs(fileName);
|
||||
unsigned curLineNum = 0;
|
||||
@@ -731,8 +775,9 @@ void parseParameterFile(const std::string& fileName, bool overwrite = true)
|
||||
std::runtime_error(errorPrefix+"Syntax error, expecting 'key=value'");
|
||||
|
||||
// all went well, add the parameter to the database object
|
||||
if (overwrite || !paramTree.hasKey(canonicalKey))
|
||||
paramTree[canonicalKey] = value;
|
||||
if (overwrite || !MetaData::tree().hasKey(canonicalKey)) {
|
||||
MetaData::tree()[canonicalKey] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -742,20 +787,15 @@ void parseParameterFile(const std::string& fileName, bool overwrite = true)
|
||||
*
|
||||
* \param os The \c std::ostream on which the message should be printed
|
||||
*/
|
||||
template <class TypeTag>
|
||||
void printValues(std::ostream& os = std::cout)
|
||||
inline void printValues(std::ostream& os = std::cout)
|
||||
{
|
||||
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
|
||||
|
||||
const Dune::ParameterTree& tree = ParamsMeta::tree();
|
||||
|
||||
std::list<std::string> runTimeAllKeyList;
|
||||
std::list<std::string> runTimeKeyList;
|
||||
std::list<std::string> unknownKeyList;
|
||||
|
||||
getFlattenedKeyList_(runTimeAllKeyList, tree);
|
||||
getFlattenedKeyList_(runTimeAllKeyList, MetaData::tree());
|
||||
for (const auto& key : runTimeAllKeyList) {
|
||||
if (ParamsMeta::registry().find(key) == ParamsMeta::registry().end()) {
|
||||
if (MetaData::registry().find(key) == MetaData::registry().end()) {
|
||||
// key was not registered by the program!
|
||||
unknownKeyList.push_back(key);
|
||||
}
|
||||
@@ -767,9 +807,9 @@ void printValues(std::ostream& os = std::cout)
|
||||
|
||||
// loop over all registered parameters
|
||||
std::list<std::string> compileTimeKeyList;
|
||||
for (const auto& reg : ParamsMeta::registry()) {
|
||||
for (const auto& reg : MetaData::registry()) {
|
||||
// check whether the key was specified at run-time
|
||||
if (tree.hasKey(reg.first)) {
|
||||
if (MetaData::tree().hasKey(reg.first)) {
|
||||
continue;
|
||||
} else {
|
||||
compileTimeKeyList.push_back(reg.first);
|
||||
@@ -780,18 +820,18 @@ void printValues(std::ostream& os = std::cout)
|
||||
// parameters
|
||||
if (runTimeKeyList.size() > 0) {
|
||||
os << "# [known parameters which were specified at run-time]\n";
|
||||
printParamList_<TypeTag>(os, runTimeKeyList, /*printDefaults=*/true);
|
||||
printParamList_(os, runTimeKeyList, /*printDefaults=*/true);
|
||||
}
|
||||
|
||||
if (compileTimeKeyList.size() > 0) {
|
||||
os << "# [parameters which were specified at compile-time]\n";
|
||||
printParamList_<TypeTag>(os, compileTimeKeyList, /*printDefaults=*/false);
|
||||
printParamList_(os, compileTimeKeyList, /*printDefaults=*/false);
|
||||
}
|
||||
|
||||
if (unknownKeyList.size() > 0) {
|
||||
os << "# [unused run-time specified parameters]\n";
|
||||
for (const auto& unused : unknownKeyList) {
|
||||
os << unused << "=\"" << tree.get(unused, "") << "\"\n" << std::flush;
|
||||
os << unused << "=\"" << MetaData::tree().get(unused, "") << "\"\n" << std::flush;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -804,18 +844,14 @@ void printValues(std::ostream& os = std::cout)
|
||||
*
|
||||
* \return true if something was printed
|
||||
*/
|
||||
template <class TypeTag>
|
||||
bool printUnused(std::ostream& os = std::cout)
|
||||
inline bool printUnused(std::ostream& os = std::cout)
|
||||
{
|
||||
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
|
||||
|
||||
const Dune::ParameterTree& tree = ParamsMeta::tree();
|
||||
std::list<std::string> runTimeAllKeyList;
|
||||
std::list<std::string> unknownKeyList;
|
||||
|
||||
getFlattenedKeyList_(runTimeAllKeyList, tree);
|
||||
getFlattenedKeyList_(runTimeAllKeyList, MetaData::tree());
|
||||
for (const auto& key : runTimeAllKeyList) {
|
||||
if (ParamsMeta::registry().find(key) == ParamsMeta::registry().end()) {
|
||||
if (MetaData::registry().find(key) == MetaData::registry().end()) {
|
||||
// key was not registered by the program!
|
||||
unknownKeyList.push_back(key);
|
||||
}
|
||||
@@ -824,7 +860,8 @@ bool printUnused(std::ostream& os = std::cout)
|
||||
if (unknownKeyList.size() > 0) {
|
||||
os << "# [unused run-time specified parameters]\n";
|
||||
for (const auto& unused : unknownKeyList) {
|
||||
os << unused << "=\"" << tree.get(unused, "") << "\"\n" << std::flush;
|
||||
os << unused << "=\""
|
||||
<< MetaData::tree().get(unused, "") << "\"\n" << std::flush;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -834,20 +871,20 @@ bool printUnused(std::ostream& os = std::cout)
|
||||
template <class TypeTag, template<class,class> class Param>
|
||||
auto get(bool errorIfNotRegistered)
|
||||
{
|
||||
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
|
||||
const std::string paramName = getPropName<TypeTag, Param>();
|
||||
const auto defaultValue = getPropValue<TypeTag, Param>();
|
||||
using ParamType = std::conditional_t<std::is_same_v<decltype(defaultValue),
|
||||
const char* const>, std::string,
|
||||
std::remove_const_t<decltype(defaultValue)>>;
|
||||
if (errorIfNotRegistered) {
|
||||
if (ParamsMeta::registrationOpen())
|
||||
if (MetaData::registrationOpen())
|
||||
throw std::runtime_error("Parameters can only retrieved after _all_ of them have "
|
||||
"been registered.");
|
||||
|
||||
if (ParamsMeta::registry().find(paramName) == ParamsMeta::registry().end())
|
||||
if (MetaData::registry().find(paramName) == MetaData::registry().end()) {
|
||||
throw std::runtime_error("Accessing parameter " + paramName
|
||||
+" without prior registration is not allowed.");
|
||||
}
|
||||
}
|
||||
|
||||
// prefix the parameter name by the model's GroupName. E.g. If
|
||||
@@ -857,7 +894,60 @@ auto get(bool errorIfNotRegistered)
|
||||
// [Stokes]
|
||||
// NewtonWriteConvergence = true
|
||||
// retrieve actual parameter from the parameter tree
|
||||
return ParamsMeta::tree().template get<ParamType>(paramName, defaultValue);
|
||||
return MetaData::tree().template get<ParamType>(paramName, defaultValue);
|
||||
}
|
||||
|
||||
template <class Param>
|
||||
auto Get(bool errorIfNotRegistered)
|
||||
{
|
||||
const std::string paramName = detail::getParamName<Param>();
|
||||
if (errorIfNotRegistered) {
|
||||
if (MetaData::registrationOpen())
|
||||
throw std::runtime_error("Parameters can only retrieved after _all_ of them have "
|
||||
"been registered.");
|
||||
|
||||
if (MetaData::registry().find(paramName) == MetaData::registry().end()) {
|
||||
throw std::runtime_error("Accessing parameter " + paramName
|
||||
+" without prior registration is not allowed.");
|
||||
}
|
||||
}
|
||||
|
||||
using ParamType = std::conditional_t<std::is_same_v<decltype(Param::value),
|
||||
const char* const>, std::string,
|
||||
std::remove_const_t<decltype(Param::value)>>;
|
||||
ParamType defaultValue = Param::value;
|
||||
const std::string& defVal = MetaData::mutableRegistry()[paramName].compileTimeValue;
|
||||
if constexpr (std::is_same_v<ParamType, std::string>) {
|
||||
defaultValue = defVal;
|
||||
}
|
||||
else if constexpr (std::is_same_v<ParamType, bool>) {
|
||||
defaultValue = defVal == "1";
|
||||
}
|
||||
else {
|
||||
std::from_chars(defVal.data(), defVal.data() + defVal.size(), defaultValue);
|
||||
}
|
||||
|
||||
// prefix the parameter name by the model's GroupName. E.g. If
|
||||
// the model specifies its group name to be 'Stokes', in an
|
||||
// INI file this would result in something like:
|
||||
//
|
||||
// [Stokes]
|
||||
// NewtonWriteConvergence = true
|
||||
// retrieve actual parameter from the parameter tree
|
||||
return MetaData::tree().template get<ParamType>(paramName, defaultValue);
|
||||
}
|
||||
|
||||
template <class Param>
|
||||
auto SetDefault(decltype(Param::value) new_value)
|
||||
{
|
||||
const std::string paramName = detail::getParamName<Param>();
|
||||
if (MetaData::registry().find(paramName) == MetaData::registry().end()) {
|
||||
throw std::runtime_error("Accessing parameter " + paramName +
|
||||
" without prior registration is not allowed.");
|
||||
}
|
||||
std::ostringstream oss;
|
||||
oss << new_value;
|
||||
MetaData::mutableRegistry()[paramName].compileTimeValue = oss.str();
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -866,39 +956,36 @@ auto get(bool errorIfNotRegistered)
|
||||
* The two arguments besides the TypeTag are assumed to be STL containers which store
|
||||
* std::pair<std::string, std::string>.
|
||||
*/
|
||||
template <class TypeTag, class Container>
|
||||
template <class Container>
|
||||
void getLists(Container& usedParams, Container& unusedParams)
|
||||
{
|
||||
usedParams.clear();
|
||||
unusedParams.clear();
|
||||
|
||||
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
|
||||
if (ParamsMeta::registrationOpen())
|
||||
throw std::runtime_error("Parameter lists can only retieved after _all_ of them have "
|
||||
if (MetaData::registrationOpen()) {
|
||||
throw std::runtime_error("Parameter lists can only retrieved after _all_ of them have "
|
||||
"been registered.");
|
||||
}
|
||||
|
||||
// get all parameter keys
|
||||
std::list<std::string> allKeysList;
|
||||
const auto& paramTree = ParamsMeta::tree();
|
||||
getFlattenedKeyList_(allKeysList, paramTree);
|
||||
getFlattenedKeyList_(allKeysList, MetaData::tree());
|
||||
|
||||
for (const auto& key : allKeysList) {
|
||||
if (ParamsMeta::registry().find(key) == ParamsMeta::registry().end()) {
|
||||
if (MetaData::registry().find(key) == MetaData::registry().end()) {
|
||||
// key was not registered
|
||||
unusedParams.emplace_back(key, paramTree[key]);
|
||||
unusedParams.emplace_back(key, MetaData::tree()[key]);
|
||||
}
|
||||
else {
|
||||
// key was registered
|
||||
usedParams.emplace_back(key, paramTree[key]);
|
||||
usedParams.emplace_back(key, MetaData::tree()[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <class TypeTag>
|
||||
void reset()
|
||||
inline void reset()
|
||||
{
|
||||
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
|
||||
ParamsMeta::clear();
|
||||
MetaData::clear();
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -910,21 +997,47 @@ void reset()
|
||||
template <class TypeTag, template<class, class> class Param>
|
||||
bool isSet(bool errorIfNotRegistered = true)
|
||||
{
|
||||
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
|
||||
const std::string paramName = getPropName<TypeTag,Param>();
|
||||
|
||||
if (errorIfNotRegistered) {
|
||||
if (ParamsMeta::registrationOpen())
|
||||
if (MetaData::registrationOpen()) {
|
||||
throw std::runtime_error("Parameters can only checked after _all_ of them have "
|
||||
"been registered.");
|
||||
}
|
||||
|
||||
if (ParamsMeta::registry().find(paramName) == ParamsMeta::registry().end())
|
||||
throw std::runtime_error("Accessing parameter "+std::string(paramName)
|
||||
+" without prior registration is not allowed.");
|
||||
if (MetaData::registry().find(paramName) == MetaData::registry().end())
|
||||
throw std::runtime_error("Accessing parameter " + std::string(paramName) +
|
||||
" without prior registration is not allowed.");
|
||||
}
|
||||
|
||||
// check whether the parameter is in the parameter tree
|
||||
return ParamsMeta::tree().hasKey(paramName);
|
||||
return MetaData::tree().hasKey(paramName);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Returns true if a parameter has been specified at runtime, false
|
||||
* otherwise.
|
||||
*
|
||||
* If the parameter in question has not been registered, this throws an exception.
|
||||
*/
|
||||
template <class Param>
|
||||
bool IsSet(bool errorIfNotRegistered = true)
|
||||
{
|
||||
const std::string paramName = detail::getParamName<Param>();
|
||||
|
||||
if (errorIfNotRegistered) {
|
||||
if (MetaData::registrationOpen()) {
|
||||
throw std::runtime_error("Parameters can only checked after _all_ of them have "
|
||||
"been registered.");
|
||||
}
|
||||
|
||||
if (MetaData::registry().find(paramName) == MetaData::registry().end())
|
||||
throw std::runtime_error("Accessing parameter " + std::string(paramName) +
|
||||
" without prior registration is not allowed.");
|
||||
}
|
||||
|
||||
// check whether the parameter is in the parameter tree
|
||||
return MetaData::tree().hasKey(paramName);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -946,9 +1059,8 @@ bool isSet(bool errorIfNotRegistered = true)
|
||||
template <class TypeTag, template<class,class> class Param>
|
||||
void registerParam(const char* usageString)
|
||||
{
|
||||
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
|
||||
const std::string paramName = getPropName<TypeTag,Param>();
|
||||
if (!ParamsMeta::registrationOpen()) {
|
||||
if (!MetaData::registrationOpen()) {
|
||||
throw std::logic_error("Parameter registration was already closed before "
|
||||
"the parameter '" + paramName + "' was registered.");
|
||||
}
|
||||
@@ -957,8 +1069,8 @@ void registerParam(const char* usageString)
|
||||
using ParamType = std::conditional_t<std::is_same_v<decltype(defaultValue),
|
||||
const char* const>, std::string,
|
||||
std::remove_const_t<decltype(defaultValue)>>;
|
||||
ParamsMeta::registrationFinalizers().push_back(
|
||||
std::make_unique<ParamRegFinalizer_<TypeTag, Param>>());
|
||||
MetaData::registrationFinalizers().push_back(
|
||||
std::make_unique<ParamRegFinalizerTT_<TypeTag, Param>>());
|
||||
|
||||
ParamInfo paramInfo;
|
||||
paramInfo.paramName = paramName;
|
||||
@@ -970,16 +1082,70 @@ void registerParam(const char* usageString)
|
||||
oss << defaultValue;
|
||||
paramInfo.compileTimeValue = oss.str();
|
||||
paramInfo.isHidden = false;
|
||||
if (ParamsMeta::registry().find(paramName) != ParamsMeta::registry().end()) {
|
||||
if (MetaData::registry().find(paramName) != MetaData::registry().end()) {
|
||||
// allow to register a parameter twice, but only if the
|
||||
// parameter name, type and usage string are exactly the same.
|
||||
if (ParamsMeta::registry().at(paramName) == paramInfo)
|
||||
if (MetaData::registry().at(paramName) == paramInfo) {
|
||||
return;
|
||||
}
|
||||
throw std::logic_error("Parameter " + paramName
|
||||
+" registered twice with non-matching characteristics.");
|
||||
}
|
||||
|
||||
ParamsMeta::mutableRegistry()[paramName] = paramInfo;
|
||||
MetaData::mutableRegistry()[paramName] = paramInfo;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup Parameter
|
||||
*
|
||||
* \brief Register a run-time parameter.
|
||||
*
|
||||
* In OPM, parameters can only be used after they have been
|
||||
* registered.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* \code
|
||||
* // Registers a run-time parameter "UpwindWeight"
|
||||
* and the description "Relative weight of the upwind node."
|
||||
* registerParam<TypeTag,UpwindWeight>("Relative weight of the upwind node.");
|
||||
* \endcode
|
||||
*/
|
||||
template <class Param>
|
||||
void Register(const char* usageString)
|
||||
{
|
||||
const std::string paramName = detail::getParamName<Param>();
|
||||
if (!MetaData::registrationOpen()) {
|
||||
throw std::logic_error("Parameter registration was already closed before "
|
||||
"the parameter '" + paramName + "' was registered.");
|
||||
}
|
||||
|
||||
const auto defaultValue = Param::value;
|
||||
using ParamType = std::conditional_t<std::is_same_v<decltype(defaultValue),
|
||||
const char* const>, std::string,
|
||||
std::remove_const_t<decltype(defaultValue)>>;
|
||||
MetaData::registrationFinalizers().push_back(
|
||||
std::make_unique<ParamRegFinalizer_<Param>>());
|
||||
|
||||
ParamInfo paramInfo;
|
||||
paramInfo.paramName = paramName;
|
||||
paramInfo.paramTypeName = Dune::className<ParamType>();
|
||||
paramInfo.usageString = usageString;
|
||||
std::ostringstream oss;
|
||||
oss << defaultValue;
|
||||
paramInfo.compileTimeValue = oss.str();
|
||||
paramInfo.isHidden = false;
|
||||
if (MetaData::registry().find(paramName) != MetaData::registry().end()) {
|
||||
// allow to register a parameter twice, but only if the
|
||||
// parameter name, type and usage string are exactly the same.
|
||||
if (MetaData::registry().at(paramName) == paramInfo) {
|
||||
return;
|
||||
}
|
||||
throw std::logic_error("Parameter " + paramName
|
||||
+" registered twice with non-matching characteristics.");
|
||||
}
|
||||
|
||||
MetaData::mutableRegistry()[paramName] = paramInfo;
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -991,15 +1157,40 @@ template <class TypeTag, template<class,class> class Param>
|
||||
void hideParam()
|
||||
{
|
||||
const std::string paramName = getPropName<TypeTag,Param>();
|
||||
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
|
||||
if (!ParamsMeta::registrationOpen())
|
||||
if (!MetaData::registrationOpen()) {
|
||||
throw std::logic_error("Parameter '" +paramName + "' declared as hidden"
|
||||
" when parameter registration was already closed.");
|
||||
}
|
||||
|
||||
auto paramInfoIt = ParamsMeta::mutableRegistry().find(paramName);
|
||||
if (paramInfoIt == ParamsMeta::mutableRegistry().end())
|
||||
auto paramInfoIt = MetaData::mutableRegistry().find(paramName);
|
||||
if (paramInfoIt == MetaData::mutableRegistry().end()) {
|
||||
throw std::logic_error("Tried to declare unknown parameter '"
|
||||
+ paramName + "' hidden.");
|
||||
}
|
||||
|
||||
auto& paramInfo = paramInfoIt->second;
|
||||
paramInfo.isHidden = true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Indicate that a given parameter should not be mentioned in the help message
|
||||
*
|
||||
* This allows to deal with unused parameters
|
||||
*/
|
||||
template <class Param>
|
||||
void Hide()
|
||||
{
|
||||
const std::string paramName = detail::getParamName<Param>();
|
||||
if (!MetaData::registrationOpen()) {
|
||||
throw std::logic_error("Parameter '" +paramName + "' declared as hidden"
|
||||
" when parameter registration was already closed.");
|
||||
}
|
||||
|
||||
auto paramInfoIt = MetaData::mutableRegistry().find(paramName);
|
||||
if (paramInfoIt == MetaData::mutableRegistry().end()) {
|
||||
throw std::logic_error("Tried to declare unknown parameter '"
|
||||
+ paramName + "' hidden.");
|
||||
}
|
||||
|
||||
auto& paramInfo = paramInfoIt->second;
|
||||
paramInfo.isHidden = true;
|
||||
@@ -1012,22 +1203,21 @@ void hideParam()
|
||||
* \c endParamRegistration, a <tt>std::logic_error</tt> exception
|
||||
* will be thrown.
|
||||
*/
|
||||
template <class TypeTag>
|
||||
void endParamRegistration()
|
||||
inline void endRegistration()
|
||||
{
|
||||
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
|
||||
if (!ParamsMeta::registrationOpen())
|
||||
if (!MetaData::registrationOpen()) {
|
||||
throw std::logic_error("Parameter registration was already closed. It is only possible "
|
||||
"to close it once.");
|
||||
}
|
||||
|
||||
ParamsMeta::registrationOpen() = false;
|
||||
MetaData::registrationOpen() = false;
|
||||
|
||||
// loop over all parameters and retrieve their values to make sure
|
||||
// that there is no syntax error
|
||||
for (const auto& param : ParamsMeta::registrationFinalizers()) {
|
||||
for (const auto& param : MetaData::registrationFinalizers()) {
|
||||
param->retrieve();
|
||||
}
|
||||
ParamsMeta::registrationFinalizers().clear();
|
||||
MetaData::registrationFinalizers().clear();
|
||||
}
|
||||
//! \endcond
|
||||
|
||||
|
||||
+16
-16
@@ -88,7 +88,7 @@ static inline void registerAllParameters_(bool finalizeRegistration = true)
|
||||
ThreadManager::registerParameters();
|
||||
|
||||
if (finalizeRegistration) {
|
||||
Parameters::endParamRegistration<TypeTag>();
|
||||
Parameters::endRegistration();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,10 +129,10 @@ static inline int setupParameters_(int argc,
|
||||
if (myRank == 0 && handleHelp)
|
||||
helpPreamble = Problem::helpPreamble(argc, argv);
|
||||
std::string s =
|
||||
Parameters::parseCommandLineOptions<TypeTag>(argc,
|
||||
argv,
|
||||
helpPreamble,
|
||||
positionalParamCallback);
|
||||
Parameters::parseCommandLineOptions(argc,
|
||||
argv,
|
||||
helpPreamble,
|
||||
positionalParamCallback);
|
||||
if (!s.empty())
|
||||
{
|
||||
int status = 1;
|
||||
@@ -161,13 +161,13 @@ static inline int setupParameters_(int argc,
|
||||
if (myRank == 0) {
|
||||
oss << "Parameter file \"" << paramFileName
|
||||
<< "\" does not exist or is not readable.";
|
||||
Parameters::printUsage<TypeTag>(argv[0], oss.str());
|
||||
Parameters::printUsage(argv[0], oss.str());
|
||||
}
|
||||
return /*status=*/1;
|
||||
}
|
||||
|
||||
// read the parameter file.
|
||||
Parameters::parseParameterFile<TypeTag>(paramFileName, /*overwrite=*/false);
|
||||
Parameters::parseParameterFile(paramFileName, /*overwrite=*/false);
|
||||
}
|
||||
|
||||
// make sure that no unknown parameters are encountered
|
||||
@@ -177,7 +177,7 @@ static inline int setupParameters_(int argc,
|
||||
ParamList usedParams;
|
||||
ParamList unusedParams;
|
||||
|
||||
Parameters::getLists<TypeTag>(usedParams, unusedParams);
|
||||
Parameters::getLists(usedParams, unusedParams);
|
||||
if (!allowUnused && !unusedParams.empty()) {
|
||||
if (myRank == 0) {
|
||||
if (unusedParams.size() == 1)
|
||||
@@ -318,17 +318,17 @@ static inline int start(int argc, char **argv, bool registerParams=true)
|
||||
Scalar endTime = Parameters::get<TypeTag, Parameters::EndTime>();
|
||||
if (endTime < -1e50) {
|
||||
if (myRank == 0)
|
||||
Parameters::printUsage<TypeTag>(argv[0],
|
||||
"Mandatory parameter '--end-time' not specified!");
|
||||
Parameters::printUsage(argv[0],
|
||||
"Mandatory parameter '--end-time' not specified!");
|
||||
return 1;
|
||||
}
|
||||
|
||||
Scalar initialTimeStepSize = Parameters::get<TypeTag, Parameters::InitialTimeStepSize>();
|
||||
if (initialTimeStepSize < -1e50) {
|
||||
if (myRank == 0)
|
||||
Parameters::printUsage<TypeTag>(argv[0],
|
||||
"Mandatory parameter '--initial-time-step-size' "
|
||||
"not specified!");
|
||||
Parameters::printUsage(argv[0],
|
||||
"Mandatory parameter '--initial-time-step-size' "
|
||||
"not specified!");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -357,20 +357,20 @@ static inline int start(int argc, char **argv, bool registerParams=true)
|
||||
if (printParams) {
|
||||
bool printSeparator = false;
|
||||
if (printParams == 1 || !isatty(fileno(stdout))) {
|
||||
Parameters::printValues<TypeTag>();
|
||||
Parameters::printValues();
|
||||
printSeparator = true;
|
||||
}
|
||||
else
|
||||
// always print the list of specified but unused parameters
|
||||
printSeparator =
|
||||
printSeparator ||
|
||||
Parameters::printUnused<TypeTag>();
|
||||
Parameters::printUnused();
|
||||
if (printSeparator)
|
||||
std::cout << endParametersSeparator;
|
||||
}
|
||||
else
|
||||
// always print the list of specified but unused parameters
|
||||
if (Parameters::printUnused<TypeTag>())
|
||||
if (Parameters::printUnused())
|
||||
std::cout << endParametersSeparator;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user