mirror of
https://github.com/OPM/opm-simulators.git
synced 2025-02-25 18:55:30 -06:00
ParameterSystem: cosmetics
This commit is contained in:
parent
5b40229211
commit
462926a356
@ -143,8 +143,9 @@ std::string parseKey(std::string& s)
|
|||||||
|
|
||||||
std::string parseQuotedValue(std::string& s, const std::string& errorPrefix)
|
std::string parseQuotedValue(std::string& s, const std::string& errorPrefix)
|
||||||
{
|
{
|
||||||
if (s.empty() || s[0] != '"')
|
if (s.empty() || s[0] != '"') {
|
||||||
throw std::runtime_error(errorPrefix + "Expected quoted string");
|
throw std::runtime_error(errorPrefix + "Expected quoted string");
|
||||||
|
}
|
||||||
|
|
||||||
std::string result;
|
std::string result;
|
||||||
unsigned i = 1;
|
unsigned i = 1;
|
||||||
@ -152,8 +153,9 @@ std::string parseQuotedValue(std::string& s, const std::string& errorPrefix)
|
|||||||
// handle escape characters
|
// handle escape characters
|
||||||
if (s[i] == '\\') {
|
if (s[i] == '\\') {
|
||||||
++i;
|
++i;
|
||||||
if (s.size() <= i)
|
if (s.size() <= i) {
|
||||||
throw std::runtime_error(errorPrefix + "Unexpected end of quoted string");
|
throw std::runtime_error(errorPrefix + "Unexpected end of quoted string");
|
||||||
|
}
|
||||||
|
|
||||||
switch (s[i]) {
|
switch (s[i]) {
|
||||||
case 'n': result += '\n'; break;
|
case 'n': result += '\n'; break;
|
||||||
@ -165,11 +167,13 @@ std::string parseQuotedValue(std::string& s, const std::string& errorPrefix)
|
|||||||
"Unknown escape character '\\" + s[i] + "'");
|
"Unknown escape character '\\" + s[i] + "'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (s[i] == '"')
|
else if (s[i] == '"') {
|
||||||
break;
|
break;
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
result += s[i];
|
result += s[i];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
s = s.substr(i+1);
|
s = s.substr(i+1);
|
||||||
return result;
|
return result;
|
||||||
@ -193,8 +197,9 @@ void printParamList(std::ostream& os,
|
|||||||
const auto& paramInfo = MetaData::registry().at(key);
|
const auto& paramInfo = MetaData::registry().at(key);
|
||||||
const std::string& defaultValue = paramInfo.defaultValue;
|
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, "");
|
||||||
|
}
|
||||||
os << key << "=\"" << value << "\"";
|
os << key << "=\"" << value << "\"";
|
||||||
if (printDefaults)
|
if (printDefaults)
|
||||||
os << " # default: \"" << defaultValue << "\"";
|
os << " # default: \"" << defaultValue << "\"";
|
||||||
@ -213,8 +218,9 @@ void printParamUsage(std::ostream& os,
|
|||||||
std::string cmdLineName = "-";
|
std::string cmdLineName = "-";
|
||||||
const std::string camelCaseName = paramInfo.paramName;
|
const std::string camelCaseName = paramInfo.paramName;
|
||||||
for (unsigned i = 0; i < camelCaseName.size(); ++i) {
|
for (unsigned i = 0; i < camelCaseName.size(); ++i) {
|
||||||
if (isupper(camelCaseName[i]))
|
if (isupper(camelCaseName[i])) {
|
||||||
cmdLineName += "-";
|
cmdLineName += "-";
|
||||||
|
}
|
||||||
cmdLineName += static_cast<char>(std::tolower(camelCaseName[i]));
|
cmdLineName += static_cast<char>(std::tolower(camelCaseName[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,15 +242,18 @@ void printParamUsage(std::ostream& os,
|
|||||||
#if HAVE_QUAD
|
#if HAVE_QUAD
|
||||||
|| paramInfo.paramTypeName == Dune::className<quad>()
|
|| paramInfo.paramTypeName == Dune::className<quad>()
|
||||||
#endif // HAVE_QUAD
|
#endif // HAVE_QUAD
|
||||||
)
|
) {
|
||||||
paramMessage += "=SCALAR";
|
paramMessage += "=SCALAR";
|
||||||
|
}
|
||||||
else if (paramInfo.paramTypeName == Dune::className<int>()
|
else if (paramInfo.paramTypeName == Dune::className<int>()
|
||||||
|| paramInfo.paramTypeName == Dune::className<unsigned int>()
|
|| paramInfo.paramTypeName == Dune::className<unsigned int>()
|
||||||
|| paramInfo.paramTypeName == Dune::className<short>()
|
|| paramInfo.paramTypeName == Dune::className<short>()
|
||||||
|| paramInfo.paramTypeName == Dune::className<unsigned short>())
|
|| paramInfo.paramTypeName == Dune::className<unsigned short>()) {
|
||||||
paramMessage += "=INTEGER";
|
paramMessage += "=INTEGER";
|
||||||
else if (paramInfo.paramTypeName == Dune::className<bool>())
|
}
|
||||||
|
else if (paramInfo.paramTypeName == Dune::className<bool>()) {
|
||||||
paramMessage += "=BOOLEAN";
|
paramMessage += "=BOOLEAN";
|
||||||
|
}
|
||||||
else if (paramInfo.paramTypeName.empty()) {
|
else if (paramInfo.paramTypeName.empty()) {
|
||||||
// the parameter is a flag. Do nothing!
|
// the parameter is a flag. Do nothing!
|
||||||
}
|
}
|
||||||
@ -264,23 +273,27 @@ void printParamUsage(std::ostream& os,
|
|||||||
|
|
||||||
// add the default value
|
// add the default value
|
||||||
if (!paramInfo.paramTypeName.empty()) {
|
if (!paramInfo.paramTypeName.empty()) {
|
||||||
if (paramMessage.back() != '.')
|
if (paramMessage.back() != '.') {
|
||||||
paramMessage += '.';
|
paramMessage += '.';
|
||||||
|
}
|
||||||
paramMessage += " Default: ";
|
paramMessage += " Default: ";
|
||||||
if (paramInfo.paramTypeName == "bool") {
|
if (paramInfo.paramTypeName == "bool") {
|
||||||
if (paramInfo.defaultValue == "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.defaultValue;
|
paramMessage += paramInfo.defaultValue;
|
||||||
paramMessage += "\"";
|
paramMessage += "\"";
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
paramMessage += paramInfo.defaultValue;
|
paramMessage += paramInfo.defaultValue;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
paramMessage = Opm::Parameters::breakLines(paramMessage, /*indent=*/52, ttyWidth);
|
paramMessage = Opm::Parameters::breakLines(paramMessage, /*indent=*/52, ttyWidth);
|
||||||
paramMessage += "\n";
|
paramMessage += "\n";
|
||||||
@ -302,29 +315,37 @@ std::string transformKey(const std::string& s,
|
|||||||
{
|
{
|
||||||
std::string result;
|
std::string result;
|
||||||
|
|
||||||
if (s.empty())
|
if (s.empty()) {
|
||||||
throw std::runtime_error(errorPrefix + "Empty parameter names are invalid");
|
throw std::runtime_error(errorPrefix + "Empty parameter names are invalid");
|
||||||
|
}
|
||||||
|
|
||||||
if (!std::isalpha(s[0]))
|
if (!std::isalpha(s[0])) {
|
||||||
throw std::runtime_error(errorPrefix+"Parameter name '" + s + "' is invalid: First character must be a letter");
|
throw std::runtime_error(errorPrefix +" Parameter name '" + s +
|
||||||
|
"' is invalid: First character must be a letter");
|
||||||
|
}
|
||||||
|
|
||||||
if (capitalizeFirstLetter)
|
if (capitalizeFirstLetter) {
|
||||||
result += static_cast<char>(std::toupper(s[0]));
|
result += static_cast<char>(std::toupper(s[0]));
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
result += s[0];
|
result += s[0];
|
||||||
|
}
|
||||||
|
|
||||||
for (unsigned i = 1; i < s.size(); ++i) {
|
for (unsigned i = 1; i < s.size(); ++i) {
|
||||||
if (s[i] == '-') {
|
if (s[i] == '-') {
|
||||||
++i;
|
++i;
|
||||||
if (s.size() <= i || !std::isalpha(s[i]))
|
if (s.size() <= i || !std::isalpha(s[i])) {
|
||||||
throw std::runtime_error(errorPrefix + "Invalid parameter name '" + s + "'");
|
throw std::runtime_error(errorPrefix + "Invalid parameter name '" + s + "'");
|
||||||
|
}
|
||||||
result += static_cast<char>(std::toupper(s[i]));
|
result += static_cast<char>(std::toupper(s[i]));
|
||||||
}
|
}
|
||||||
else if (!std::isalnum(s[i]))
|
else if (!std::isalnum(s[i])) {
|
||||||
throw std::runtime_error(errorPrefix + "Invalid parameter name '" + s + "'");
|
throw std::runtime_error(errorPrefix + "Invalid parameter name '" + s + "'");
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
result += s[i];
|
result += s[i];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -340,13 +361,14 @@ ParamType Get_(const std::string& paramName, ParamType defaultValue,
|
|||||||
bool errorIfNotRegistered)
|
bool errorIfNotRegistered)
|
||||||
{
|
{
|
||||||
if (errorIfNotRegistered) {
|
if (errorIfNotRegistered) {
|
||||||
if (MetaData::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 (MetaData::registry().find(paramName) == MetaData::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.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -390,8 +412,8 @@ void Hide_(const std::string& paramName)
|
|||||||
|
|
||||||
auto paramInfoIt = MetaData::mutableRegistry().find(paramName);
|
auto paramInfoIt = MetaData::mutableRegistry().find(paramName);
|
||||||
if (paramInfoIt == MetaData::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;
|
||||||
@ -406,14 +428,14 @@ bool IsSet_(const std::string& paramName, bool errorIfNotRegistered)
|
|||||||
"been registered.");
|
"been registered.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MetaData::registry().find(paramName) == MetaData::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 MetaData::tree().hasKey(paramName);
|
return MetaData::tree().hasKey(paramName);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Register_(const std::string& paramName,
|
void Register_(const std::string& paramName,
|
||||||
@ -438,8 +460,8 @@ void Register_(const std::string& paramName,
|
|||||||
if (MetaData::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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
MetaData::mutableRegistry()[paramName] = paramInfo;
|
MetaData::mutableRegistry()[paramName] = paramInfo;
|
||||||
@ -455,7 +477,7 @@ void SetDefault_(const std::string& paramName,
|
|||||||
MetaData::mutableRegistry()[paramName].defaultValue = paramValue;
|
MetaData::mutableRegistry()[paramName].defaultValue = paramValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // namespace detail
|
||||||
|
|
||||||
std::string breakLines(const std::string& msg,
|
std::string breakLines(const std::string& msg,
|
||||||
int indentWidth,
|
int indentWidth,
|
||||||
@ -477,8 +499,9 @@ std::string breakLines(const std::string& msg,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (std::isspace(msg[inPos]))
|
if (std::isspace(msg[inPos])) {
|
||||||
lastBreakPos = inPos;
|
lastBreakPos = inPos;
|
||||||
|
}
|
||||||
|
|
||||||
if (ttyPos >= maxWidth) {
|
if (ttyPos >= maxWidth) {
|
||||||
if (lastBreakPos > startInPos) {
|
if (lastBreakPos > startInPos) {
|
||||||
@ -495,8 +518,9 @@ std::string breakLines(const std::string& msg,
|
|||||||
}
|
}
|
||||||
|
|
||||||
result += "\n";
|
result += "\n";
|
||||||
for (int i = 0; i < indentWidth; ++i)
|
for (int i = 0; i < indentWidth; ++i) {
|
||||||
result += " ";
|
result += " ";
|
||||||
|
}
|
||||||
ttyPos = indentWidth;
|
ttyPos = indentWidth;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -578,10 +602,11 @@ void printUsage(const std::string& helpPreamble,
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const auto& param : MetaData::registry()) {
|
for (const auto& param : MetaData::registry()) {
|
||||||
if (showAll || !param.second.isHidden)
|
if (showAll || !param.second.isHidden) {
|
||||||
printParamUsage(os, param.second);
|
printParamUsage(os, param.second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void parseParameterFile(const std::string& fileName, bool overwrite)
|
void parseParameterFile(const std::string& fileName, bool overwrite)
|
||||||
{
|
{
|
||||||
@ -599,8 +624,9 @@ void parseParameterFile(const std::string& fileName, bool overwrite)
|
|||||||
removeLeadingSpace(curLine);
|
removeLeadingSpace(curLine);
|
||||||
|
|
||||||
// ignore empty and comment lines
|
// ignore empty and comment lines
|
||||||
if (curLine.empty() || curLine[0] == '#' || curLine[0] == ';')
|
if (curLine.empty() || curLine[0] == '#' || curLine[0] == ';') {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO (?): support for parameter groups.
|
// TODO (?): support for parameter groups.
|
||||||
|
|
||||||
@ -608,32 +634,39 @@ void parseParameterFile(const std::string& fileName, bool overwrite)
|
|||||||
std::string key = parseKey(curLine);
|
std::string key = parseKey(curLine);
|
||||||
std::string canonicalKey = transformKey(key, /*capitalizeFirst=*/true, errorPrefix);
|
std::string canonicalKey = transformKey(key, /*capitalizeFirst=*/true, errorPrefix);
|
||||||
|
|
||||||
if (seenKeys.count(canonicalKey) > 0)
|
if (seenKeys.count(canonicalKey) > 0) {
|
||||||
throw std::runtime_error(errorPrefix+"Parameter '"+canonicalKey+"' seen multiple times in the same file");
|
throw std::runtime_error(errorPrefix + "Parameter '" + canonicalKey +
|
||||||
|
"' seen multiple times in the same file");
|
||||||
|
}
|
||||||
seenKeys.insert(canonicalKey);
|
seenKeys.insert(canonicalKey);
|
||||||
|
|
||||||
// deal with the equals sign
|
// deal with the equals sign
|
||||||
removeLeadingSpace(curLine);
|
removeLeadingSpace(curLine);
|
||||||
if (curLine.empty() || curLine[0] != '=')
|
if (curLine.empty() || curLine[0] != '=') {
|
||||||
std::runtime_error(errorPrefix+"Syntax error, expecting 'key=value'");
|
std::runtime_error(errorPrefix+"Syntax error, expecting 'key=value'");
|
||||||
|
}
|
||||||
|
|
||||||
curLine = curLine.substr(1);
|
curLine = curLine.substr(1);
|
||||||
removeLeadingSpace(curLine);
|
removeLeadingSpace(curLine);
|
||||||
|
|
||||||
if (curLine.empty() || curLine[0] == '#' || curLine[0] == ';')
|
if (curLine.empty() || curLine[0] == '#' || curLine[0] == ';') {
|
||||||
std::runtime_error(errorPrefix+"Syntax error, expecting 'key=value'");
|
std::runtime_error(errorPrefix+"Syntax error, expecting 'key=value'");
|
||||||
|
}
|
||||||
|
|
||||||
// get the value
|
// get the value
|
||||||
std::string value;
|
std::string value;
|
||||||
if (curLine[0] == '"')
|
if (curLine[0] == '"') {
|
||||||
value = parseQuotedValue(curLine, errorPrefix);
|
value = parseQuotedValue(curLine, errorPrefix);
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
value = parseUnquotedValue(curLine, errorPrefix);
|
value = parseUnquotedValue(curLine, errorPrefix);
|
||||||
|
}
|
||||||
|
|
||||||
// ignore trailing comments
|
// ignore trailing comments
|
||||||
removeLeadingSpace(curLine);
|
removeLeadingSpace(curLine);
|
||||||
if (!curLine.empty() && curLine[0] != '#' && curLine[0] != ';')
|
if (!curLine.empty() && curLine[0] != '#' && curLine[0] != ';') {
|
||||||
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 || !MetaData::tree().hasKey(canonicalKey)) {
|
if (overwrite || !MetaData::tree().hasKey(canonicalKey)) {
|
||||||
@ -650,8 +683,8 @@ std::string parseCommandLineOptions(int argc,
|
|||||||
// 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) {
|
||||||
if (std::string("-h") == argv[i]
|
if (std::string("-h") == argv[i] ||
|
||||||
|| std::string("--help") == argv[i]) {
|
std::string("--help") == argv[i]) {
|
||||||
printUsage(helpPreamble, std::cout, /*errorMsg=*/"");
|
printUsage(helpPreamble, std::cout, /*errorMsg=*/"");
|
||||||
return "Help called";
|
return "Help called";
|
||||||
}
|
}
|
||||||
@ -719,8 +752,8 @@ std::string parseCommandLineOptions(int argc,
|
|||||||
// parse argument
|
// parse argument
|
||||||
paramName = transformKey(parseKey(s), /*capitalizeFirst=*/true);
|
paramName = transformKey(parseKey(s), /*capitalizeFirst=*/true);
|
||||||
if (seenKeys.count(paramName) > 0) {
|
if (seenKeys.count(paramName) > 0) {
|
||||||
std::string msg =
|
const std::string msg = "Parameter '" + paramName +
|
||||||
std::string("Parameter '")+paramName+"' specified multiple times as a "
|
"' specified multiple times as a "
|
||||||
"command line parameter";
|
"command line parameter";
|
||||||
|
|
||||||
if (!helpPreamble.empty()) {
|
if (!helpPreamble.empty()) {
|
||||||
@ -731,9 +764,9 @@ std::string parseCommandLineOptions(int argc,
|
|||||||
seenKeys.insert(paramName);
|
seenKeys.insert(paramName);
|
||||||
|
|
||||||
if (s.empty() || s[0] != '=') {
|
if (s.empty() || s[0] != '=') {
|
||||||
std::string msg =
|
const std::string msg = "Parameter '" + paramName +
|
||||||
std::string("Parameter '")+paramName+"' is missing a value. "
|
"' is missing a value. "
|
||||||
+" Please use "+argv[i]+"=value.";
|
" Please use " + argv[i] + "=value.";
|
||||||
|
|
||||||
if (!helpPreamble.empty()) {
|
if (!helpPreamble.empty()) {
|
||||||
printUsage(helpPreamble, std::cerr, msg);
|
printUsage(helpPreamble, std::cerr, msg);
|
||||||
@ -854,6 +887,6 @@ template unsigned Get_(const std::string&, unsigned, bool);
|
|||||||
template quad Get_(const std::string&, quad, bool);
|
template quad Get_(const std::string&, quad, bool);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
} // namespace detail
|
||||||
|
|
||||||
} // namespace Opm::Parameters
|
} // namespace Opm::Parameters
|
||||||
|
Loading…
Reference in New Issue
Block a user