From 32b0015afe4440f9169d58c7f8f327e539cab8ed Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Fri, 5 Jul 2024 14:12:32 +0200 Subject: [PATCH] changed: remove the typetag for using the parameter system also remove the typetag parameter for the metadata backing store. --- opm/models/utils/basicproperties.hh | 4 +- opm/models/utils/parametersystem.hh | 182 +++++++++++----------------- 2 files changed, 76 insertions(+), 110 deletions(-) diff --git a/opm/models/utils/basicproperties.hh b/opm/models/utils/basicproperties.hh index cb97c8cea..2451d4d95 100644 --- a/opm/models/utils/basicproperties.hh +++ b/opm/models/utils/basicproperties.hh @@ -56,11 +56,13 @@ namespace Opm::Properties { // Create new type tags namespace TTag { + //! Type tag for all models. -struct NumericModel { using InheritsFrom = std::tuple; }; +struct NumericModel {}; //! Type tag for all fully coupled models. struct ImplicitModel { using InheritsFrom = std::tuple; }; + } // end namespace TTag /////////////////////////////////// diff --git a/opm/models/utils/parametersystem.hh b/opm/models/utils/parametersystem.hh index bf9f5b0dd..7cbc37aef 100644 --- a/opm/models/utils/parametersystem.hh +++ b/opm/models/utils/parametersystem.hh @@ -55,8 +55,7 @@ #include #include -namespace Opm { -namespace Parameters { +namespace Opm::Parameters { struct ParamInfo { @@ -114,40 +113,21 @@ public: std::ignore = get(/*errorIfNotRegistered=*/true); } }; -} // namespace Parameters -} // namespace Opm - -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 -struct ParameterMetaData { using type = UndefinedProperty; }; - - -//! Set the ParameterMetaData property -template -struct ParameterMetaData +struct MetaData { using type = Dune::ParameterTree; static Dune::ParameterTree& tree() { return *storage_().tree; } - static std::map& mutableRegistry() + static std::map& mutableRegistry() { return storage_().registry; } - static const std::map& registry() + static const std::map& registry() { return storage_().registry; } - static std::list > ®istrationFinalizers() + static std::list> ®istrationFinalizers() { return storage_().finalizers; } static bool& registrationOpen() @@ -155,7 +135,7 @@ struct ParameterMetaData static void clear() { - storage_().tree.reset(new Dune::ParameterTree()); + storage_().tree = std::make_unique(); storage_().finalizers.clear(); storage_().registrationOpen = true; storage_().registry.clear(); @@ -166,28 +146,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(); registrationOpen = true; } std::unique_ptr tree; - std::map registry; - std::list > finalizers; + std::map registry; + std::list> 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& dest, @@ -366,14 +345,14 @@ inline void getFlattenedKeyList_(std::list& dest, // print the values of a list of parameters template -void printParamList_(std::ostream& os, const std::list& keyList, bool printDefaults = false) +void printParamList_(std::ostream& os, + const std::list& keyList, + bool printDefaults = false) { - using ParamsMeta = GetProp; - - 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)) @@ -402,11 +381,8 @@ void printUsage(const std::string& helpPreamble, std::ostream& os = std::cerr, const bool showAll = false) { - using ParamsMeta = GetProp; - - if (errorMsg != "") { - os << errorMsg << "\n" - << "\n"; + if (!errorMsg.empty()) { + os << errorMsg << "\n\n"; } os << breakLines_(helpPreamble, /*indent=*/2, /*maxWidth=*/getTtyWidth_()); @@ -424,11 +400,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); } } @@ -573,8 +547,6 @@ std::string parseCommandLineOptions(int argc, const std::string& helpPreamble = "", const PositionalArgumentCallback& posArgCallback = noPositionalParameters_) { - Dune::ParameterTree& paramTree = GetProp::tree(); - // handle the "--help" parameter if (!helpPreamble.empty()) { for (int i = 1; i < argc; ++i) { @@ -599,7 +571,8 @@ 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; @@ -664,7 +637,7 @@ std::string parseCommandLineOptions(int argc, paramValue = s.substr(1); // Put the key=value pair into the parameter tree - paramTree[paramName] = paramValue; + MetaData::tree()[paramName] = paramValue; } return ""; } @@ -678,8 +651,6 @@ std::string parseCommandLineOptions(int argc, template void parseParameterFile(const std::string& fileName, bool overwrite = true) { - Dune::ParameterTree& paramTree = GetProp::tree(); - std::set seenKeys; std::ifstream ifs(fileName); 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'"); // 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; + } } } @@ -745,17 +717,13 @@ void parseParameterFile(const std::string& fileName, bool overwrite = true) template void printValues(std::ostream& os = std::cout) { - using ParamsMeta = GetProp; - - const Dune::ParameterTree& tree = ParamsMeta::tree(); - std::list runTimeAllKeyList; std::list runTimeKeyList; std::list 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 +735,9 @@ void printValues(std::ostream& os = std::cout) // loop over all registered parameters std::list 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); @@ -791,7 +759,7 @@ void printValues(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; } } } @@ -807,15 +775,12 @@ void printValues(std::ostream& os = std::cout) template bool printUnused(std::ostream& os = std::cout) { - using ParamsMeta = GetProp; - - const Dune::ParameterTree& tree = ParamsMeta::tree(); std::list runTimeAllKeyList; std::list 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 +789,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 +800,20 @@ bool printUnused(std::ostream& os = std::cout) template class Param> auto get(bool errorIfNotRegistered) { - using ParamsMeta = GetProp; const std::string paramName = getPropName(); const auto defaultValue = getPropValue(); using ParamType = std::conditional_t, std::string, std::remove_const_t>; 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 +823,7 @@ auto get(bool errorIfNotRegistered) // [Stokes] // NewtonWriteConvergence = true // retrieve actual parameter from the parameter tree - return ParamsMeta::tree().template get(paramName, defaultValue); + return MetaData::tree().template get(paramName, defaultValue); } /*! @@ -872,33 +838,30 @@ void getLists(Container& usedParams, Container& unusedParams) usedParams.clear(); unusedParams.clear(); - using ParamsMeta = GetProp; - if (ParamsMeta::registrationOpen()) + if (MetaData::registrationOpen()) { throw std::runtime_error("Parameter lists can only retieved after _all_ of them have " "been registered."); + } // get all parameter keys std::list 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 -void reset() +inline void reset() { - using ParamsMeta = GetProp; - ParamsMeta::clear(); + MetaData::clear(); } /*! @@ -910,21 +873,21 @@ void reset() template class Param> bool isSet(bool errorIfNotRegistered = true) { - using ParamsMeta = GetProp; const std::string paramName = getPropName(); 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); } /*! @@ -946,9 +909,8 @@ bool isSet(bool errorIfNotRegistered = true) template class Param> void registerParam(const char* usageString) { - using ParamsMeta = GetProp; const std::string paramName = getPropName(); - if (!ParamsMeta::registrationOpen()) { + if (!MetaData::registrationOpen()) { throw std::logic_error("Parameter registration was already closed before " "the parameter '" + paramName + "' was registered."); } @@ -957,7 +919,7 @@ void registerParam(const char* usageString) using ParamType = std::conditional_t, std::string, std::remove_const_t>; - ParamsMeta::registrationFinalizers().push_back( + MetaData::registrationFinalizers().push_back( std::make_unique>()); ParamInfo paramInfo; @@ -970,16 +932,17 @@ 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; } /*! @@ -991,15 +954,16 @@ template class Param> void hideParam() { const std::string paramName = getPropName(); - using ParamsMeta = GetProp; - 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; @@ -1015,19 +979,19 @@ void hideParam() template void endParamRegistration() { - using ParamsMeta = GetProp; - 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