simplify code
- use Tokenizer instead of ValueTokenizer since no value conversions are needed - don't use fmt::format() when no string formatting is needed
This commit is contained in:
@ -119,38 +119,36 @@ void KimQuery::command(int narg, char **arg)
|
|||||||
}
|
}
|
||||||
++arg;
|
++arg;
|
||||||
--narg;
|
--narg;
|
||||||
// The “list” is the default setting
|
// The "list" is the default setting
|
||||||
// the result is returned as a space-separated list of values in a variable
|
// the result is returned as a space-separated list of values in a variable
|
||||||
} else format_arg = "list";
|
} else format_arg = "list";
|
||||||
|
|
||||||
std::string query_function{arg[1]};
|
std::string query_function(arg[1]);
|
||||||
if (query_function == "split" || query_function == "list" ||
|
if (query_function == "split" || query_function == "list" ||
|
||||||
query_function == "index") {
|
query_function == "index")
|
||||||
auto msg = fmt::format("Illegal 'kim query' command.\nThe '{}' keyword "
|
error->all(FLERR, fmt::format("Illegal 'kim query' command.\nThe '{}' "
|
||||||
"can not be used after '{}'", query_function, format_arg);
|
"keyword can not be used after '{}'",
|
||||||
error->all(FLERR, msg);
|
query_function, format_arg));
|
||||||
}
|
|
||||||
|
|
||||||
std::string model_name;
|
std::string model_name;
|
||||||
|
|
||||||
// check the query_args format (a series of keyword=value pairs)
|
// check the query_args format (a series of keyword=value pairs)
|
||||||
for (int i = 2; i < narg; ++i) {
|
for (int i = 2; i < narg; ++i) {
|
||||||
if (!utils::strmatch(arg[i], "[=][\\[].*[\\]]")) {
|
if (!utils::strmatch(arg[i], "[=][\\[].*[\\]]"))
|
||||||
auto msg = fmt::format("Illegal query format.\nInput argument "
|
error->all(FLERR, fmt::format("Illegal query format.\nInput argument "
|
||||||
"of `{}` to 'kim query' is wrong. The query format is the "
|
"of `{}` to 'kim query' is wrong. The "
|
||||||
"keyword=[value], where value is always an array of one or "
|
"query format is the keyword=[value], "
|
||||||
"more comma-separated items", arg[i]);
|
"where value is always an array of one or "
|
||||||
error->all(FLERR, msg);
|
"more comma-separated items", arg[i]));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (query_function != "get_available_models") {
|
if (query_function != "get_available_models") {
|
||||||
for (int i = 2; i < narg; ++i) {
|
for (int i = 2; i < narg; ++i) {
|
||||||
// check if the model is specified as an argument
|
// check if the model is specified as an argument
|
||||||
if (utils::strmatch(arg[i], "^model=")) {
|
if (utils::strmatch(arg[i], "^model=")) {
|
||||||
ValueTokenizer values(arg[i], "=[]");
|
Tokenizer values(arg[i], "=[]");
|
||||||
std::string key = values.next_string();
|
values.skip(1);
|
||||||
model_name = values.next_string();
|
model_name = values.next();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -161,12 +159,12 @@ void KimQuery::command(int narg, char **arg)
|
|||||||
if (ifix >= 0) {
|
if (ifix >= 0) {
|
||||||
FixStoreKIM *fix_store = (FixStoreKIM *) modify->fix[ifix];
|
FixStoreKIM *fix_store = (FixStoreKIM *) modify->fix[ifix];
|
||||||
char *model_name_c = (char *) fix_store->getptr("model_name");
|
char *model_name_c = (char *) fix_store->getptr("model_name");
|
||||||
model_name = fmt::format("{}", model_name_c);
|
model_name = model_name_c;
|
||||||
} else {
|
} else {
|
||||||
auto msg = fmt::format("Illegal query format.\nMust use 'kim init' "
|
error->all(FLERR, "Illegal query format.\nMust use 'kim init' "
|
||||||
"before 'kim query' or must provide the model name after query "
|
"before 'kim query' or must provide the model name "
|
||||||
"function with the format of 'model=[model_name]'");
|
"after query function with the format of "
|
||||||
error->all(FLERR, msg);
|
"'model=[model_name]'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -185,31 +183,30 @@ void KimQuery::command(int narg, char **arg)
|
|||||||
error->all(FLERR, msg);
|
error->all(FLERR, msg);
|
||||||
} else if (strcmp(value, "EMPTY") == 0) {
|
} else if (strcmp(value, "EMPTY") == 0) {
|
||||||
delete [] value;
|
delete [] value;
|
||||||
error->all(FLERR, fmt::format("OpenKIM query returned no results"));
|
error->all(FLERR, "OpenKIM query returned no results");
|
||||||
}
|
}
|
||||||
|
|
||||||
input->write_echo("#=== BEGIN kim-query =================================="
|
input->write_echo("#=== BEGIN kim-query =================================="
|
||||||
"=======\n");
|
"=======\n");
|
||||||
// trim list of models to those that are installed on the system
|
// trim list of models to those that are installed on the system
|
||||||
if (query_function == "get_available_models") {
|
if (query_function == "get_available_models") {
|
||||||
ValueTokenizer vals(value, ", \"");
|
Tokenizer vals(value, ", \"");
|
||||||
std::string available;
|
std::string available;
|
||||||
std::string missing;
|
std::string missing;
|
||||||
|
|
||||||
KIM_Collections * collections;
|
KIM_Collections *collections;
|
||||||
KIM_CollectionItemType typ;
|
KIM_CollectionItemType typ;
|
||||||
|
|
||||||
if (KIM_Collections_Create(&collections)) {
|
if (KIM_Collections_Create(&collections)) {
|
||||||
delete [] value;
|
delete [] value;
|
||||||
error->all(FLERR,
|
error->all(FLERR, "Unable to access KIM Collections to find Model");
|
||||||
fmt::format("Unable to access KIM Collections to find Model"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto logID = fmt::format("{}_Collections", comm->me);
|
auto logID = fmt::format("{}_Collections", comm->me);
|
||||||
KIM_Collections_SetLogID(collections, logID.c_str());
|
KIM_Collections_SetLogID(collections, logID.c_str());
|
||||||
|
|
||||||
while (vals.has_next()) {
|
while (vals.has_next()) {
|
||||||
auto svalue = vals.next_string();
|
auto svalue = vals.next();
|
||||||
if (KIM_Collections_GetItemType(collections, svalue.c_str(), &typ))
|
if (KIM_Collections_GetItemType(collections, svalue.c_str(), &typ))
|
||||||
missing += fmt::format("{}, ", svalue);
|
missing += fmt::format("{}, ", svalue);
|
||||||
else
|
else
|
||||||
@ -220,34 +217,34 @@ void KimQuery::command(int narg, char **arg)
|
|||||||
input->write_echo(
|
input->write_echo(
|
||||||
fmt::format("# Missing OpenKIM models: {}\n\n", missing));
|
fmt::format("# Missing OpenKIM models: {}\n\n", missing));
|
||||||
|
|
||||||
if (available.empty())
|
if (available.empty()) {
|
||||||
error->all(FLERR,
|
delete [] value;
|
||||||
fmt::format(
|
error->all(FLERR,"There are no matching OpenKIM models installed on the system");
|
||||||
"There are no matching OpenKIM models installed on the system"));
|
}
|
||||||
|
|
||||||
// replace results with available
|
// replace results with available
|
||||||
strcpy(value, available.c_str()); // available guaranteed to fit
|
strcpy(value, available.c_str()); // available guaranteed to fit
|
||||||
};
|
};
|
||||||
|
|
||||||
ValueTokenizer values(value, ",");
|
Tokenizer values(value, ",");
|
||||||
if (format_arg == "split") {
|
if (format_arg == "split") {
|
||||||
int counter = 1;
|
int counter = 1;
|
||||||
while (values.has_next()) {
|
while (values.has_next()) {
|
||||||
auto svalue = values.next_string();
|
auto svalue = values.next();
|
||||||
auto setcmd = fmt::format("{}_{} string {}", var_name, counter++, svalue);
|
auto setcmd = fmt::format("{}_{} string {}", var_name, counter++, svalue);
|
||||||
input->variable->set(setcmd);
|
input->variable->set(setcmd);
|
||||||
input->write_echo(fmt::format("variable {}\n", setcmd));
|
input->write_echo(fmt::format("variable {}\n", setcmd));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
std::string setcmd;
|
std::string setcmd;
|
||||||
auto svalue = utils::trim(values.next_string());
|
auto svalue = utils::trim(values.next());
|
||||||
if (format_arg == "list") {
|
if (format_arg == "list") {
|
||||||
setcmd = fmt::format("{} string \"", var_name);
|
setcmd = fmt::format("{} string \"", var_name);
|
||||||
setcmd += (svalue.front() == '"' && svalue.back() == '"')
|
setcmd += (svalue.front() == '"' && svalue.back() == '"')
|
||||||
? fmt::format("{}", svalue.substr(1, svalue.size() - 2))
|
? fmt::format("{}", svalue.substr(1, svalue.size() - 2))
|
||||||
: fmt::format("{}", svalue);
|
: fmt::format("{}", svalue);
|
||||||
while (values.has_next()) {
|
while (values.has_next()) {
|
||||||
svalue = utils::trim(values.next_string());
|
svalue = utils::trim(values.next());
|
||||||
setcmd += (svalue.front() == '"' && svalue.back() == '"')
|
setcmd += (svalue.front() == '"' && svalue.back() == '"')
|
||||||
? fmt::format(" {}", svalue.substr(1, svalue.size() - 2))
|
? fmt::format(" {}", svalue.substr(1, svalue.size() - 2))
|
||||||
: fmt::format(" {}", svalue);
|
: fmt::format(" {}", svalue);
|
||||||
@ -257,7 +254,7 @@ void KimQuery::command(int narg, char **arg)
|
|||||||
// format_arg == "index"
|
// format_arg == "index"
|
||||||
setcmd = fmt::format("{} index {}", var_name, svalue);
|
setcmd = fmt::format("{} index {}", var_name, svalue);
|
||||||
while (values.has_next()) {
|
while (values.has_next()) {
|
||||||
svalue = values.next_string();
|
svalue = values.next();
|
||||||
setcmd += fmt::format(" {}", svalue);
|
setcmd += fmt::format(" {}", svalue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user