modernize and simplify

This commit is contained in:
Axel Kohlmeyer
2023-03-20 01:02:22 -04:00
parent 57e86346a6
commit b0e7d9702b
4 changed files with 42 additions and 65 deletions

View File

@ -312,12 +312,8 @@ void KimInit::do_init(char *model_name, char *user_units, char *model_units, KIM
{
// create storage proxy fix. delete existing fix, if needed.
int ifix = modify->find_fix("KIM_MODEL_STORE");
if (ifix >= 0) modify->delete_fix(ifix);
modify->add_fix("KIM_MODEL_STORE all STORE/KIM");
ifix = modify->find_fix("KIM_MODEL_STORE");
auto fix_store = dynamic_cast<FixStoreKIM *>(modify->fix[ifix]);
if (modify->get_fix_by_id("KIM_MODEL_STORE")) modify->delete_fix("KIM_MODEL_STORE");
auto fix_store = dynamic_cast<FixStoreKIM *>(modify->add_fix("KIM_MODEL_STORE all STORE/KIM"));
fix_store->setptr("model_name", (void *) model_name);
fix_store->setptr("user_units", (void *) user_units);
fix_store->setptr("model_units", (void *) model_units);

View File

@ -103,11 +103,9 @@ void KimInteractions::do_setup(int narg, char **arg)
if ((narg == 1) && (arg_str == "fixed_types")) {
fixed_types = true;
} else if (narg != atom->ntypes) {
error->all(FLERR, "Illegal 'kim interactions' command.\nThe "
"LAMMPS simulation has {} atom type(s), but "
"{} chemical species passed to the "
"'kim interactions' command",
atom->ntypes, narg);
error->all(FLERR, "Illegal 'kim interactions' command.\nThe LAMMPS simulation has {} atom "
"type(s), but {} chemical species passed to the 'kim interactions' command",
atom->ntypes, narg);
} else {
fixed_types = false;
}
@ -119,16 +117,14 @@ void KimInteractions::do_setup(int narg, char **arg)
// retrieve model name and pointer to simulator model class instance.
// validate model name if not given as null pointer.
int ifix = modify->find_fix("KIM_MODEL_STORE");
if (ifix >= 0) {
auto fix_store = dynamic_cast<FixStoreKIM *>(modify->fix[ifix]);
auto fix_store = dynamic_cast<FixStoreKIM *>(modify->get_fix_by_id("KIM_MODEL_STORE"));
if (fix_store) {
model_name = (char *)fix_store->getptr("model_name");
simulatorModel = (KIM_SimulatorModel *)fix_store->getptr("simulator_model");
} else error->all(FLERR, "Must use 'kim init' before 'kim interactions'");
// Begin output to log file
input->write_echo("#=== BEGIN kim interactions ==========================="
"=======\n");
input->write_echo("#=== BEGIN kim interactions ==================================\n");
if (simulatorModel) {
auto first_visit = input->variable->find("kim_update");
@ -165,8 +161,8 @@ void KimInteractions::do_setup(int narg, char **arg)
if (atom_type_sym == sim_species) species_is_supported = true;
}
if (!species_is_supported) {
error->all(FLERR, "Species '{}' is not supported by this "
"KIM Simulator Model", atom_type_sym);
error->all(FLERR, "Species '{}' is not supported by this KIM Simulator Model",
atom_type_sym);
}
}
} else {
@ -179,18 +175,15 @@ void KimInteractions::do_setup(int narg, char **arg)
const char *sim_field, *sim_value;
KIM_SimulatorModel_GetNumberOfSimulatorFields(simulatorModel, &sim_fields);
for (int i = 0; i < sim_fields; ++i) {
KIM_SimulatorModel_GetSimulatorFieldMetadata(
simulatorModel, i, &sim_lines, &sim_field);
KIM_SimulatorModel_GetSimulatorFieldMetadata(simulatorModel, i, &sim_lines, &sim_field);
const std::string sim_field_str(sim_field);
if (sim_field_str == "units") {
KIM_SimulatorModel_GetSimulatorFieldLine(
simulatorModel, i, 0, &sim_value);
KIM_SimulatorModel_GetSimulatorFieldLine(simulatorModel, i, 0, &sim_value);
const std::string sim_value_str(sim_value);
const std::string unit_style_str(update->unit_style);
if (sim_value_str != unit_style_str)
error->all(FLERR, "Incompatible units for KIM Simulator Model");
if (strcmp(sim_value, update->unit_style) != 0)
error->all(FLERR, "Incompatible units for KIM Simulator Model: {} vs {}",
sim_value, update->unit_style);
}
}
@ -217,8 +210,7 @@ void KimInteractions::do_setup(int narg, char **arg)
no_model_definition = false;
for (int j = 0; j < sim_lines; ++j) {
KIM_SimulatorModel_GetSimulatorFieldLine(
simulatorModel, i, j, &sim_value);
KIM_SimulatorModel_GetSimulatorFieldLine(simulatorModel, i, j, &sim_value);
if (utils::strmatch(sim_value, "^KIM_SET_TYPE_PARAMETERS")) {
// Notes regarding the KIM_SET_TYPE_PARAMETERS command
// * This is an INTERNAL command.
@ -264,8 +256,7 @@ void KimInteractions::do_setup(int narg, char **arg)
}
// End output to log file
input->write_echo("#=== END kim interactions ============================="
"=======\n\n");
input->write_echo("#=== END kim interactions ====================================\n\n");
}
/* ---------------------------------------------------------------------- */

View File

@ -159,20 +159,17 @@ void KimParam::command(int narg, char **arg)
error->all(FLERR, "Incorrect arguments in 'kim param' command.\n"
"'kim param get/set' is mandatory");
int const ifix = modify->find_fix("KIM_MODEL_STORE");
if (ifix >= 0) {
auto fix_store = reinterpret_cast<FixStoreKIM *>(modify->fix[ifix]);
KIM_SimulatorModel *simulatorModel =
reinterpret_cast<KIM_SimulatorModel *>(
fix_store->getptr("simulator_model"));
auto fix_store = dynamic_cast<FixStoreKIM *>(modify->get_fix_by_id("KIM_MODEL_STORE"));
if (fix_store) {
auto *simulatorModel = reinterpret_cast<KIM_SimulatorModel *>(
fix_store->getptr("simulator_model"));
if (simulatorModel)
error->all(FLERR, "'kim param' can only be used with a KIM Portable Model");
}
input->write_echo(fmt::format("#=== BEGIN kim param {} ==================="
"==================\n", kim_param_get_set));
input->write_echo(fmt::format("#=== BEGIN kim param {} =====================================\n",
kim_param_get_set));
KIM_Model *pkim = nullptr;
@ -431,6 +428,6 @@ void KimParam::command(int narg, char **arg)
} else
error->all(FLERR, "This model has No mutable parameters");
input->write_echo(fmt::format("#=== END kim param {} ====================="
"==================\n", kim_param_get_set));
input->write_echo(fmt::format("#=== END kim param {} =======================================\n",
kim_param_get_set));
}

View File

@ -136,11 +136,9 @@ void KimQuery::command(int narg, char **arg)
// check the query_args format (a series of keyword=value pairs)
for (int i = 2; i < narg; ++i) {
if (!utils::strmatch(arg[i], "[=][\\[].*[\\]]"))
error->all(FLERR, "Illegal query format.\nInput argument "
"of `{}` to 'kim query' is wrong. The "
"query format is the keyword=[value], "
"where value is always an array of one or "
"more comma-separated items", arg[i]);
error->all(FLERR, "Illegal query format.\nInput argument of `{}` to 'kim query' is wrong. "
"The query format is the keyword=[value], where value is always an array of one "
"or more comma-separated items", arg[i]);
}
if (query_function != "get_available_models") {
@ -156,15 +154,13 @@ void KimQuery::command(int narg, char **arg)
// if the model name is not provided by the user
if (model_name.empty()) {
// check if we had a kim init command by finding fix STORE/KIM
const int ifix = modify->find_fix("KIM_MODEL_STORE");
if (ifix >= 0) {
auto fix_store = dynamic_cast<FixStoreKIM *>(modify->fix[ifix]);
auto fix_store = dynamic_cast<FixStoreKIM *>(modify->get_fix_by_id("KIM_MODEL_STORE"));
if (fix_store) {
char *model_name_c = (char *) fix_store->getptr("model_name");
model_name = model_name_c;
} else {
error->all(FLERR, "Illegal query format.\nMust use 'kim init' "
"before 'kim query' or must provide the model name "
"after query function with the format of "
error->all(FLERR, "Illegal query format.\nMust use 'kim init' before 'kim query' "
"or must provide the model name after query function with the format of "
"'model=[model_name]'");
}
}
@ -179,16 +175,14 @@ void KimQuery::command(int narg, char **arg)
// and then the error message that was returned by the web server
if (strlen(value) == 0) {
auto msg = fmt::format("OpenKIM query failed: {}", value + 1);
delete [] value;
error->all(FLERR, msg);
error->all(FLERR, "OpenKIM query failed: {}", value + 1);
delete[] value;
} else if (strcmp(value, "EMPTY") == 0) {
delete [] value;
delete[] value;
error->all(FLERR, "OpenKIM query returned no results");
}
input->write_echo("#=== BEGIN kim-query =================================="
"=======\n");
input->write_echo("#=== BEGIN kim-query =========================================\n");
// trim list of models to those that are installed on the system
if (query_function == "get_available_models") {
Tokenizer vals(value, ", \"");
@ -199,7 +193,7 @@ void KimQuery::command(int narg, char **arg)
KIM_CollectionItemType typ;
if (KIM_Collections_Create(&collections)) {
delete [] value;
delete[] value;
error->all(FLERR, "Unable to access KIM Collections to find Model");
}
@ -219,7 +213,7 @@ void KimQuery::command(int narg, char **arg)
fmt::format("# Missing OpenKIM models: {}\n\n", missing));
if (available.empty()) {
delete [] value;
delete[] value;
error->all(FLERR,"There are no matching OpenKIM models installed on the system");
}
@ -262,13 +256,12 @@ void KimQuery::command(int narg, char **arg)
input->variable->set(setcmd);
input->write_echo(fmt::format("variable {}\n", setcmd));
}
input->write_echo("#=== END kim-query ===================================="
"=======\n\n");
input->write_echo("#=== END kim-query ===========================================\n\n");
delete [] value;
delete[] value;
#else
error->all(FLERR, "Cannot use 'kim query' command when KIM package "
"is compiled without support for libcurl");
error->all(FLERR, "Cannot use 'kim query' command when KIM package is compiled without "
"support for libcurl");
#endif
}