changed: remove the typetag for using the parameter system

also remove the typetag parameter for the metadata backing store.
This commit is contained in:
Arne Morten Kvarving 2024-07-05 14:12:32 +02:00
parent ad8dd1e4b8
commit 32b0015afe
2 changed files with 76 additions and 110 deletions

View File

@ -56,11 +56,13 @@ namespace Opm::Properties {
// Create new type tags // Create new type tags
namespace TTag { namespace TTag {
//! Type tag for all models. //! Type tag for all models.
struct NumericModel { using InheritsFrom = std::tuple<ParameterSystem>; }; struct NumericModel {};
//! Type tag for all fully coupled models. //! Type tag for all fully coupled models.
struct ImplicitModel { using InheritsFrom = std::tuple<NumericModel>; }; struct ImplicitModel { using InheritsFrom = std::tuple<NumericModel>; };
} // end namespace TTag } // end namespace TTag
/////////////////////////////////// ///////////////////////////////////

View File

@ -55,8 +55,7 @@
#include <unistd.h> #include <unistd.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
namespace Opm { namespace Opm::Parameters {
namespace Parameters {
struct ParamInfo struct ParamInfo
{ {
@ -114,40 +113,21 @@ public:
std::ignore = get<TypeTag, Property>(/*errorIfNotRegistered=*/true); std::ignore = get<TypeTag, Property>(/*errorIfNotRegistered=*/true);
} }
}; };
} // namespace Parameters
} // namespace Opm struct MetaData
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>
{ {
using type = Dune::ParameterTree; using type = Dune::ParameterTree;
static Dune::ParameterTree& tree() static Dune::ParameterTree& tree()
{ return *storage_().tree; } { return *storage_().tree; }
static std::map<std::string, ::Opm::Parameters::ParamInfo>& mutableRegistry() static std::map<std::string, ParamInfo>& mutableRegistry()
{ return storage_().registry; } { return storage_().registry; }
static const std::map<std::string, ::Opm::Parameters::ParamInfo>& registry() static const std::map<std::string, ParamInfo>& registry()
{ return storage_().registry; } { return storage_().registry; }
static std::list<std::unique_ptr<::Opm::Parameters::ParamRegFinalizerBase_> > &registrationFinalizers() static std::list<std::unique_ptr<ParamRegFinalizerBase_>> &registrationFinalizers()
{ return storage_().finalizers; } { return storage_().finalizers; }
static bool& registrationOpen() static bool& registrationOpen()
@ -155,7 +135,7 @@ struct ParameterMetaData<TypeTag, TTag::ParameterSystem>
static void clear() static void clear()
{ {
storage_().tree.reset(new Dune::ParameterTree()); storage_().tree = std::make_unique<Dune::ParameterTree>();
storage_().finalizers.clear(); storage_().finalizers.clear();
storage_().registrationOpen = true; storage_().registrationOpen = true;
storage_().registry.clear(); storage_().registry.clear();
@ -166,28 +146,27 @@ private:
// member functions of the ParameterMetaData property class triggers a bug in clang // 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 // 3.5's address sanitizer which causes these variables to be initialized multiple
// times... // times...
struct Storage_ { struct Storage_
{
Storage_() Storage_()
{ {
tree.reset(new Dune::ParameterTree()); tree = std::make_unique<Dune::ParameterTree>();
registrationOpen = true; registrationOpen = true;
} }
std::unique_ptr<Dune::ParameterTree> tree; std::unique_ptr<Dune::ParameterTree> tree;
std::map<std::string, ::Opm::Parameters::ParamInfo> registry; std::map<std::string, ParamInfo> registry;
std::list<std::unique_ptr<::Opm::Parameters::ParamRegFinalizerBase_> > finalizers; std::list<std::unique_ptr<ParamRegFinalizerBase_>> finalizers;
bool registrationOpen; bool registrationOpen;
}; };
static Storage_& storage_() {
static Storage_& storage_()
{
static Storage_ obj; static Storage_ obj;
return obj; return obj;
} }
}; };
} // namespace Opm::Properties
namespace Opm::Parameters {
// function prototype declarations // function prototype declarations
void printParamUsage_(std::ostream& os, const ParamInfo& paramInfo); void printParamUsage_(std::ostream& os, const ParamInfo& paramInfo);
void getFlattenedKeyList_(std::list<std::string>& dest, void getFlattenedKeyList_(std::list<std::string>& dest,
@ -366,14 +345,14 @@ inline void getFlattenedKeyList_(std::list<std::string>& dest,
// print the values of a list of parameters // print the values of a list of parameters
template <class TypeTag> template <class TypeTag>
void printParamList_(std::ostream& os, const std::list<std::string>& keyList, bool printDefaults = false) void printParamList_(std::ostream& os,
const std::list<std::string>& keyList,
bool printDefaults = false)
{ {
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>; const Dune::ParameterTree& tree = MetaData::tree();
const Dune::ParameterTree& tree = ParamsMeta::tree();
for (const auto& key : keyList) { 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; const std::string& defaultValue = paramInfo.compileTimeValue;
std::string value = defaultValue; std::string value = defaultValue;
if (tree.hasKey(key)) if (tree.hasKey(key))
@ -402,11 +381,8 @@ void printUsage(const std::string& helpPreamble,
std::ostream& os = std::cerr, std::ostream& os = std::cerr,
const bool showAll = false) const bool showAll = false)
{ {
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>; if (!errorMsg.empty()) {
os << errorMsg << "\n\n";
if (errorMsg != "") {
os << errorMsg << "\n"
<< "\n";
} }
os << breakLines_(helpPreamble, /*indent=*/2, /*maxWidth=*/getTtyWidth_()); os << breakLines_(helpPreamble, /*indent=*/2, /*maxWidth=*/getTtyWidth_());
@ -424,11 +400,9 @@ void printUsage(const std::string& helpPreamble,
printParamUsage_(os, pInfo); printParamUsage_(os, pInfo);
} }
auto paramIt = ParamsMeta::registry().begin(); for (const auto& param : MetaData::registry()) {
const auto& paramEndIt = ParamsMeta::registry().end(); if (showAll || !param.second.isHidden)
for (; paramIt != paramEndIt; ++paramIt) { printParamUsage_(os, param.second);
if (showAll || !paramIt->second.isHidden)
printParamUsage_(os, paramIt->second);
} }
} }
@ -573,8 +547,6 @@ std::string parseCommandLineOptions(int argc,
const std::string& helpPreamble = "", const std::string& helpPreamble = "",
const PositionalArgumentCallback& posArgCallback = noPositionalParameters_) const PositionalArgumentCallback& posArgCallback = noPositionalParameters_)
{ {
Dune::ParameterTree& paramTree = GetProp<TypeTag, Properties::ParameterMetaData>::tree();
// handle the "--help" parameter // handle the "--help" parameter
if (!helpPreamble.empty()) { if (!helpPreamble.empty()) {
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
@ -599,7 +571,8 @@ std::string parseCommandLineOptions(int argc,
|| argv[i][1] != '-') || argv[i][1] != '-')
{ {
std::string errorMsg; std::string errorMsg;
int numHandled = posArgCallback(seenKeys, errorMsg, argc, argv, i, numPositionalParams); int numHandled = posArgCallback(seenKeys, errorMsg, argc, argv,
i, numPositionalParams);
if (numHandled < 1) { if (numHandled < 1) {
std::ostringstream oss; std::ostringstream oss;
@ -664,7 +637,7 @@ std::string parseCommandLineOptions(int argc,
paramValue = s.substr(1); paramValue = s.substr(1);
// Put the key=value pair into the parameter tree // Put the key=value pair into the parameter tree
paramTree[paramName] = paramValue; MetaData::tree()[paramName] = paramValue;
} }
return ""; return "";
} }
@ -678,8 +651,6 @@ std::string parseCommandLineOptions(int argc,
template <class TypeTag> template <class TypeTag>
void parseParameterFile(const std::string& fileName, bool overwrite = true) void parseParameterFile(const std::string& fileName, bool overwrite = true)
{ {
Dune::ParameterTree& paramTree = GetProp<TypeTag, Properties::ParameterMetaData>::tree();
std::set<std::string> seenKeys; std::set<std::string> seenKeys;
std::ifstream ifs(fileName); std::ifstream ifs(fileName);
unsigned curLineNum = 0; unsigned curLineNum = 0;
@ -731,8 +702,9 @@ void parseParameterFile(const std::string& fileName, bool overwrite = true)
std::runtime_error(errorPrefix+"Syntax error, expecting 'key=value'"); std::runtime_error(errorPrefix+"Syntax error, expecting 'key=value'");
// all went well, add the parameter to the database object // all went well, add the parameter to the database object
if (overwrite || !paramTree.hasKey(canonicalKey)) if (overwrite || !MetaData::tree().hasKey(canonicalKey)) {
paramTree[canonicalKey] = value; MetaData::tree()[canonicalKey] = value;
}
} }
} }
@ -745,17 +717,13 @@ void parseParameterFile(const std::string& fileName, bool overwrite = true)
template <class TypeTag> template <class TypeTag>
void printValues(std::ostream& os = std::cout) 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> runTimeAllKeyList;
std::list<std::string> runTimeKeyList; std::list<std::string> runTimeKeyList;
std::list<std::string> unknownKeyList; std::list<std::string> unknownKeyList;
getFlattenedKeyList_(runTimeAllKeyList, tree); getFlattenedKeyList_(runTimeAllKeyList, MetaData::tree());
for (const auto& key : runTimeAllKeyList) { 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! // key was not registered by the program!
unknownKeyList.push_back(key); unknownKeyList.push_back(key);
} }
@ -767,9 +735,9 @@ void printValues(std::ostream& os = std::cout)
// loop over all registered parameters // loop over all registered parameters
std::list<std::string> compileTimeKeyList; 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 // check whether the key was specified at run-time
if (tree.hasKey(reg.first)) { if (MetaData::tree().hasKey(reg.first)) {
continue; continue;
} else { } else {
compileTimeKeyList.push_back(reg.first); compileTimeKeyList.push_back(reg.first);
@ -791,7 +759,7 @@ void printValues(std::ostream& os = std::cout)
if (unknownKeyList.size() > 0) { if (unknownKeyList.size() > 0) {
os << "# [unused run-time specified parameters]\n"; os << "# [unused run-time specified parameters]\n";
for (const auto& unused : unknownKeyList) { for (const auto& unused : unknownKeyList) {
os << unused << "=\"" << tree.get(unused, "") << "\"\n" << std::flush; os << unused << "=\"" << MetaData::tree().get(unused, "") << "\"\n" << std::flush;
} }
} }
} }
@ -807,15 +775,12 @@ void printValues(std::ostream& os = std::cout)
template <class TypeTag> template <class TypeTag>
bool printUnused(std::ostream& os = std::cout) 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> runTimeAllKeyList;
std::list<std::string> unknownKeyList; std::list<std::string> unknownKeyList;
getFlattenedKeyList_(runTimeAllKeyList, tree); getFlattenedKeyList_(runTimeAllKeyList, MetaData::tree());
for (const auto& key : runTimeAllKeyList) { 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! // key was not registered by the program!
unknownKeyList.push_back(key); unknownKeyList.push_back(key);
} }
@ -824,7 +789,8 @@ bool printUnused(std::ostream& os = std::cout)
if (unknownKeyList.size() > 0) { if (unknownKeyList.size() > 0) {
os << "# [unused run-time specified parameters]\n"; os << "# [unused run-time specified parameters]\n";
for (const auto& unused : unknownKeyList) { for (const auto& unused : unknownKeyList) {
os << unused << "=\"" << tree.get(unused, "") << "\"\n" << std::flush; os << unused << "=\""
<< MetaData::tree().get(unused, "") << "\"\n" << std::flush;
} }
return true; return true;
} }
@ -834,20 +800,20 @@ bool printUnused(std::ostream& os = std::cout)
template <class TypeTag, template<class,class> class Param> template <class TypeTag, template<class,class> class Param>
auto get(bool errorIfNotRegistered) auto get(bool errorIfNotRegistered)
{ {
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
const std::string paramName = getPropName<TypeTag, Param>(); const std::string paramName = getPropName<TypeTag, Param>();
const auto defaultValue = getPropValue<TypeTag, Param>(); const auto defaultValue = getPropValue<TypeTag, Param>();
using ParamType = std::conditional_t<std::is_same_v<decltype(defaultValue), using ParamType = std::conditional_t<std::is_same_v<decltype(defaultValue),
const char* const>, std::string, const char* const>, std::string,
std::remove_const_t<decltype(defaultValue)>>; std::remove_const_t<decltype(defaultValue)>>;
if (errorIfNotRegistered) { if (errorIfNotRegistered) {
if (ParamsMeta::registrationOpen()) if (MetaData::registrationOpen())
throw std::runtime_error("Parameters can only retrieved after _all_ of them have " throw std::runtime_error("Parameters can only retrieved after _all_ of them have "
"been registered."); "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 throw std::runtime_error("Accessing parameter " + paramName
+" without prior registration is not allowed."); +" without prior registration is not allowed.");
}
} }
// prefix the parameter name by the model's GroupName. E.g. If // prefix the parameter name by the model's GroupName. E.g. If
@ -857,7 +823,7 @@ auto get(bool errorIfNotRegistered)
// [Stokes] // [Stokes]
// NewtonWriteConvergence = true // NewtonWriteConvergence = true
// retrieve actual parameter from the parameter tree // retrieve actual parameter from the parameter tree
return ParamsMeta::tree().template get<ParamType>(paramName, defaultValue); return MetaData::tree().template get<ParamType>(paramName, defaultValue);
} }
/*! /*!
@ -872,33 +838,30 @@ void getLists(Container& usedParams, Container& unusedParams)
usedParams.clear(); usedParams.clear();
unusedParams.clear(); unusedParams.clear();
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>; if (MetaData::registrationOpen()) {
if (ParamsMeta::registrationOpen())
throw std::runtime_error("Parameter lists can only retieved after _all_ of them have " throw std::runtime_error("Parameter lists can only retieved after _all_ of them have "
"been registered."); "been registered.");
}
// get all parameter keys // get all parameter keys
std::list<std::string> allKeysList; std::list<std::string> allKeysList;
const auto& paramTree = ParamsMeta::tree(); getFlattenedKeyList_(allKeysList, MetaData::tree());
getFlattenedKeyList_(allKeysList, paramTree);
for (const auto& key : allKeysList) { 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 // key was not registered
unusedParams.emplace_back(key, paramTree[key]); unusedParams.emplace_back(key, MetaData::tree()[key]);
} }
else { else {
// key was registered // key was registered
usedParams.emplace_back(key, paramTree[key]); usedParams.emplace_back(key, MetaData::tree()[key]);
} }
} }
} }
template <class TypeTag> inline void reset()
void reset()
{ {
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>; MetaData::clear();
ParamsMeta::clear();
} }
/*! /*!
@ -910,21 +873,21 @@ void reset()
template <class TypeTag, template<class, class> class Param> template <class TypeTag, template<class, class> class Param>
bool isSet(bool errorIfNotRegistered = true) bool isSet(bool errorIfNotRegistered = true)
{ {
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
const std::string paramName = getPropName<TypeTag,Param>(); const std::string paramName = getPropName<TypeTag,Param>();
if (errorIfNotRegistered) { if (errorIfNotRegistered) {
if (ParamsMeta::registrationOpen()) if (MetaData::registrationOpen()) {
throw std::runtime_error("Parameters can only checked after _all_ of them have " throw std::runtime_error("Parameters can only checked after _all_ of them have "
"been registered."); "been registered.");
}
if (ParamsMeta::registry().find(paramName) == ParamsMeta::registry().end()) if (MetaData::registry().find(paramName) == MetaData::registry().end())
throw std::runtime_error("Accessing parameter "+std::string(paramName) throw std::runtime_error("Accessing parameter " + std::string(paramName) +
+" without prior registration is not allowed."); " without prior registration is not allowed.");
} }
// check whether the parameter is in the parameter tree // check whether the parameter is in the parameter tree
return ParamsMeta::tree().hasKey(paramName); return MetaData::tree().hasKey(paramName);
} }
/*! /*!
@ -946,9 +909,8 @@ bool isSet(bool errorIfNotRegistered = true)
template <class TypeTag, template<class,class> class Param> template <class TypeTag, template<class,class> class Param>
void registerParam(const char* usageString) void registerParam(const char* usageString)
{ {
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
const std::string paramName = getPropName<TypeTag,Param>(); const std::string paramName = getPropName<TypeTag,Param>();
if (!ParamsMeta::registrationOpen()) { if (!MetaData::registrationOpen()) {
throw std::logic_error("Parameter registration was already closed before " throw std::logic_error("Parameter registration was already closed before "
"the parameter '" + paramName + "' was registered."); "the parameter '" + paramName + "' was registered.");
} }
@ -957,7 +919,7 @@ void registerParam(const char* usageString)
using ParamType = std::conditional_t<std::is_same_v<decltype(defaultValue), using ParamType = std::conditional_t<std::is_same_v<decltype(defaultValue),
const char* const>, std::string, const char* const>, std::string,
std::remove_const_t<decltype(defaultValue)>>; std::remove_const_t<decltype(defaultValue)>>;
ParamsMeta::registrationFinalizers().push_back( MetaData::registrationFinalizers().push_back(
std::make_unique<ParamRegFinalizer_<TypeTag, Param>>()); std::make_unique<ParamRegFinalizer_<TypeTag, Param>>());
ParamInfo paramInfo; ParamInfo paramInfo;
@ -970,16 +932,17 @@ void registerParam(const char* usageString)
oss << defaultValue; oss << defaultValue;
paramInfo.compileTimeValue = oss.str(); paramInfo.compileTimeValue = oss.str();
paramInfo.isHidden = false; 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 // allow to register a parameter twice, but only if the
// parameter name, type and usage string are exactly the same. // parameter name, type and usage string are exactly the same.
if (ParamsMeta::registry().at(paramName) == paramInfo) if (MetaData::registry().at(paramName) == paramInfo) {
return; return;
}
throw std::logic_error("Parameter " + paramName throw std::logic_error("Parameter " + paramName
+" registered twice with non-matching characteristics."); +" registered twice with non-matching characteristics.");
} }
ParamsMeta::mutableRegistry()[paramName] = paramInfo; MetaData::mutableRegistry()[paramName] = paramInfo;
} }
/*! /*!
@ -991,15 +954,16 @@ template <class TypeTag, template<class,class> class Param>
void hideParam() void hideParam()
{ {
const std::string paramName = getPropName<TypeTag,Param>(); const std::string paramName = getPropName<TypeTag,Param>();
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>; if (!MetaData::registrationOpen()) {
if (!ParamsMeta::registrationOpen())
throw std::logic_error("Parameter '" +paramName + "' declared as hidden" throw std::logic_error("Parameter '" +paramName + "' declared as hidden"
" when parameter registration was already closed."); " when parameter registration was already closed.");
}
auto paramInfoIt = ParamsMeta::mutableRegistry().find(paramName); auto paramInfoIt = MetaData::mutableRegistry().find(paramName);
if (paramInfoIt == ParamsMeta::mutableRegistry().end()) if (paramInfoIt == MetaData::mutableRegistry().end()) {
throw std::logic_error("Tried to declare unknown parameter '" throw std::logic_error("Tried to declare unknown parameter '"
+ paramName + "' hidden."); + paramName + "' hidden.");
}
auto& paramInfo = paramInfoIt->second; auto& paramInfo = paramInfoIt->second;
paramInfo.isHidden = true; paramInfo.isHidden = true;
@ -1015,19 +979,19 @@ void hideParam()
template <class TypeTag> template <class TypeTag>
void endParamRegistration() void endParamRegistration()
{ {
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>; if (!MetaData::registrationOpen()) {
if (!ParamsMeta::registrationOpen())
throw std::logic_error("Parameter registration was already closed. It is only possible " throw std::logic_error("Parameter registration was already closed. It is only possible "
"to close it once."); "to close it once.");
}
ParamsMeta::registrationOpen() = false; MetaData::registrationOpen() = false;
// loop over all parameters and retrieve their values to make sure // loop over all parameters and retrieve their values to make sure
// that there is no syntax error // that there is no syntax error
for (const auto& param : ParamsMeta::registrationFinalizers()) { for (const auto& param : MetaData::registrationFinalizers()) {
param->retrieve(); param->retrieve();
} }
ParamsMeta::registrationFinalizers().clear(); MetaData::registrationFinalizers().clear();
} }
//! \endcond //! \endcond