mirror of
https://github.com/OPM/opm-simulators.git
synced 2025-02-25 18:55:30 -06:00
Merge pull request #928 from akva2/remove_typetag_param_system
remove typetag based parameter system
This commit is contained in:
commit
8cc1a0e3f7
@ -32,8 +32,6 @@
|
|||||||
#ifndef OPM_PARAMETER_SYSTEM_HH
|
#ifndef OPM_PARAMETER_SYSTEM_HH
|
||||||
#define OPM_PARAMETER_SYSTEM_HH
|
#define OPM_PARAMETER_SYSTEM_HH
|
||||||
|
|
||||||
#include <opm/models/utils/propertysystem.hh>
|
|
||||||
|
|
||||||
#if HAVE_QUAD
|
#if HAVE_QUAD
|
||||||
#include <opm/material/common/quad.hpp>
|
#include <opm/material/common/quad.hpp>
|
||||||
#endif // HAVE_QUAD
|
#endif // HAVE_QUAD
|
||||||
@ -93,7 +91,7 @@ struct ParamInfo
|
|||||||
std::string paramTypeName;
|
std::string paramTypeName;
|
||||||
std::string typeTagName;
|
std::string typeTagName;
|
||||||
std::string usageString;
|
std::string usageString;
|
||||||
std::string compileTimeValue;
|
std::string defaultValue;
|
||||||
bool isHidden;
|
bool isHidden;
|
||||||
|
|
||||||
bool operator==(const ParamInfo& other) const
|
bool operator==(const ParamInfo& other) const
|
||||||
@ -105,24 +103,6 @@ struct ParamInfo
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
|
||||||
* \ingroup Parameter
|
|
||||||
*
|
|
||||||
* \brief Retrieve a runtime parameter.
|
|
||||||
*
|
|
||||||
* The default value is specified via the property system.
|
|
||||||
*
|
|
||||||
* Example:
|
|
||||||
*
|
|
||||||
* \code
|
|
||||||
* // Retrieves value UpwindWeight, default
|
|
||||||
* // is taken from the property UpwindWeight
|
|
||||||
* ::Opm::Parameters::get<TypeTag, Properties::UpwindWeight>();
|
|
||||||
* \endcode
|
|
||||||
*/
|
|
||||||
template <class TypeTag, template<class,class> class Property>
|
|
||||||
auto get(bool errorIfNotRegistered = true);
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \ingroup Parameter
|
* \ingroup Parameter
|
||||||
*
|
*
|
||||||
@ -166,18 +146,6 @@ public:
|
|||||||
virtual void retrieve() = 0;
|
virtual void retrieve() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class TypeTag, template<class,class> class Property>
|
|
||||||
class ParamRegFinalizerTT_ : 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<TypeTag, Property>(/*errorIfNotRegistered=*/true);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <class Param>
|
template <class Param>
|
||||||
class ParamRegFinalizer_ : public ParamRegFinalizerBase_
|
class ParamRegFinalizer_ : public ParamRegFinalizerBase_
|
||||||
{
|
{
|
||||||
@ -381,18 +349,18 @@ inline void printParamUsage_(std::ostream& os, const ParamInfo& paramInfo)
|
|||||||
paramMessage += '.';
|
paramMessage += '.';
|
||||||
paramMessage += " Default: ";
|
paramMessage += " Default: ";
|
||||||
if (paramInfo.paramTypeName == "bool") {
|
if (paramInfo.paramTypeName == "bool") {
|
||||||
if (paramInfo.compileTimeValue == "0")
|
if (paramInfo.defaultValue == "0")
|
||||||
paramMessage += "false";
|
paramMessage += "false";
|
||||||
else
|
else
|
||||||
paramMessage += "true";
|
paramMessage += "true";
|
||||||
}
|
}
|
||||||
else if (isString) {
|
else if (isString) {
|
||||||
paramMessage += "\"";
|
paramMessage += "\"";
|
||||||
paramMessage += paramInfo.compileTimeValue;
|
paramMessage += paramInfo.defaultValue;
|
||||||
paramMessage += "\"";
|
paramMessage += "\"";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
paramMessage += paramInfo.compileTimeValue;
|
paramMessage += paramInfo.defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
paramMessage = breakLines_(paramMessage, /*indent=*/52, ttyWidth);
|
paramMessage = breakLines_(paramMessage, /*indent=*/52, ttyWidth);
|
||||||
@ -428,7 +396,7 @@ inline void printParamList_(std::ostream& os,
|
|||||||
|
|
||||||
for (const auto& key : keyList) {
|
for (const auto& key : keyList) {
|
||||||
const auto& paramInfo = MetaData::registry().at(key);
|
const auto& paramInfo = MetaData::registry().at(key);
|
||||||
const std::string& defaultValue = paramInfo.compileTimeValue;
|
const std::string& defaultValue = paramInfo.defaultValue;
|
||||||
std::string value = defaultValue;
|
std::string value = defaultValue;
|
||||||
if (tree.hasKey(key))
|
if (tree.hasKey(key))
|
||||||
value = tree.get(key, "");
|
value = tree.get(key, "");
|
||||||
@ -868,35 +836,6 @@ inline bool printUnused(std::ostream& os = std::cout)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class TypeTag, template<class,class> class Param>
|
|
||||||
auto get(bool errorIfNotRegistered)
|
|
||||||
{
|
|
||||||
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 (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.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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>
|
template <class Param>
|
||||||
auto Get(bool errorIfNotRegistered)
|
auto Get(bool errorIfNotRegistered)
|
||||||
{
|
{
|
||||||
@ -917,7 +856,7 @@ auto Get(bool errorIfNotRegistered)
|
|||||||
std::remove_const_t<decltype(Param::value)>>;
|
std::remove_const_t<decltype(Param::value)>>;
|
||||||
ParamType defaultValue = Param::value;
|
ParamType defaultValue = Param::value;
|
||||||
|
|
||||||
const std::string& defVal = MetaData::mutableRegistry()[paramName].compileTimeValue;
|
const std::string& defVal = MetaData::mutableRegistry()[paramName].defaultValue;
|
||||||
if constexpr (std::is_same_v<ParamType, std::string>) {
|
if constexpr (std::is_same_v<ParamType, std::string>) {
|
||||||
defaultValue = defVal;
|
defaultValue = defVal;
|
||||||
}
|
}
|
||||||
@ -958,7 +897,7 @@ auto SetDefault(decltype(Param::value) new_value)
|
|||||||
}
|
}
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << new_value;
|
oss << new_value;
|
||||||
MetaData::mutableRegistry()[paramName].compileTimeValue = oss.str();
|
MetaData::mutableRegistry()[paramName].defaultValue = oss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -999,32 +938,6 @@ inline void reset()
|
|||||||
MetaData::clear();
|
MetaData::clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
* \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 TypeTag, template<class, class> class Param>
|
|
||||||
bool isSet(bool errorIfNotRegistered = true)
|
|
||||||
{
|
|
||||||
const std::string paramName = getPropName<TypeTag,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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Returns true if a parameter has been specified at runtime, false
|
* \brief Returns true if a parameter has been specified at runtime, false
|
||||||
* otherwise.
|
* otherwise.
|
||||||
@ -1064,62 +977,7 @@ bool IsSet(bool errorIfNotRegistered = true)
|
|||||||
* \code
|
* \code
|
||||||
* // Registers a run-time parameter "UpwindWeight"
|
* // Registers a run-time parameter "UpwindWeight"
|
||||||
* and the description "Relative weight of the upwind node."
|
* and the description "Relative weight of the upwind node."
|
||||||
* registerParam<TypeTag,UpwindWeight>("Relative weight of the upwind node.");
|
* Register<UpwindWeight>("Relative weight of the upwind node.");
|
||||||
* \endcode
|
|
||||||
*/
|
|
||||||
template <class TypeTag, template<class,class> class Param>
|
|
||||||
void registerParam(const char* usageString)
|
|
||||||
{
|
|
||||||
const std::string paramName = getPropName<TypeTag,Param>();
|
|
||||||
if (!MetaData::registrationOpen()) {
|
|
||||||
throw std::logic_error("Parameter registration was already closed before "
|
|
||||||
"the parameter '" + paramName + "' was registered.");
|
|
||||||
}
|
|
||||||
|
|
||||||
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)>>;
|
|
||||||
MetaData::registrationFinalizers().push_back(
|
|
||||||
std::make_unique<ParamRegFinalizerTT_<TypeTag, Param>>());
|
|
||||||
|
|
||||||
ParamInfo paramInfo;
|
|
||||||
paramInfo.paramName = paramName;
|
|
||||||
paramInfo.paramTypeName = Dune::className<ParamType>();
|
|
||||||
std::string tmp = Dune::className<TypeTag>();
|
|
||||||
tmp.replace(0, strlen("Opm::Properties::TTag::"), "");
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
* \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
|
* \endcode
|
||||||
*/
|
*/
|
||||||
template <class Param>
|
template <class Param>
|
||||||
@ -1144,7 +1002,7 @@ void Register(const char* usageString)
|
|||||||
paramInfo.usageString = usageString;
|
paramInfo.usageString = usageString;
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << defaultValue;
|
oss << defaultValue;
|
||||||
paramInfo.compileTimeValue = oss.str();
|
paramInfo.defaultValue = oss.str();
|
||||||
paramInfo.isHidden = false;
|
paramInfo.isHidden = false;
|
||||||
if (MetaData::registry().find(paramName) != MetaData::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
|
||||||
@ -1159,29 +1017,6 @@ void Register(const char* usageString)
|
|||||||
MetaData::mutableRegistry()[paramName] = paramInfo;
|
MetaData::mutableRegistry()[paramName] = paramInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
* \brief Indicate that a given parameter should not be mentioned in the help message
|
|
||||||
*
|
|
||||||
* This allows to deal with unused parameters
|
|
||||||
*/
|
|
||||||
template <class TypeTag, template<class,class> class Param>
|
|
||||||
void hideParam()
|
|
||||||
{
|
|
||||||
const std::string paramName = getPropName<TypeTag,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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Indicate that a given parameter should not be mentioned in the help message
|
* \brief Indicate that a given parameter should not be mentioned in the help message
|
||||||
|
@ -218,13 +218,6 @@ struct GetSplicePropImpl
|
|||||||
static_assert(!std::is_same<type, std::tuple<>>::value, "Splice is undefined!");
|
static_assert(!std::is_same<type, std::tuple<>>::value, "Splice is undefined!");
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename, class = void>
|
|
||||||
struct has_name : public std::false_type {};
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
struct has_name<T, decltype((void)T::name, void())>
|
|
||||||
: public std::true_type {};
|
|
||||||
|
|
||||||
} // end namespace Detail
|
} // end namespace Detail
|
||||||
} // end namespace Property
|
} // end namespace Property
|
||||||
|
|
||||||
@ -248,22 +241,6 @@ using GetSplicePropType = typename Properties::Detail::GetSplicePropImpl<TypeTag
|
|||||||
template<class TypeTag, template<class,class> class Property>
|
template<class TypeTag, template<class,class> class Property>
|
||||||
constexpr auto getPropValue() { return Properties::Detail::GetPropImpl<TypeTag, Property>::type::value; }
|
constexpr auto getPropValue() { return Properties::Detail::GetPropImpl<TypeTag, Property>::type::value; }
|
||||||
|
|
||||||
//! get the name data member of a property
|
|
||||||
template<class TypeTag, template<class,class> class Property>
|
|
||||||
auto getPropName()
|
|
||||||
{
|
|
||||||
using type = typename Properties::Detail::GetPropImpl<TypeTag,Property>::type;
|
|
||||||
if constexpr (Properties::Detail::has_name<type>::value) {
|
|
||||||
return Properties::Detail::GetPropImpl<TypeTag, Property>::type::name;
|
|
||||||
} else {
|
|
||||||
std::string paramName = Dune::className<type>();
|
|
||||||
paramName.replace(0, std::strlen("Opm::Properties::"), "");
|
|
||||||
const auto pos = paramName.find_first_of('<');
|
|
||||||
paramName.erase(pos);
|
|
||||||
return paramName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef __clang__
|
#ifdef __clang__
|
||||||
#pragma clang diagnostic pop
|
#pragma clang diagnostic pop
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user