refactor KIM package code for using {fmt} and std::string

This commit is contained in:
Axel Kohlmeyer 2020-06-11 13:25:41 -04:00
parent 7ace10238d
commit 505cc1036f
No known key found for this signature in database
GPG Key ID: D9B44E93BF0C375A
10 changed files with 61 additions and 170 deletions

View File

@ -74,6 +74,7 @@
#include "variable.h"
#include "citeme.h"
#include "utils.h"
#include "fmt/format.h"
extern "C" {
#include "KIM_SimulatorHeaders.h"
@ -303,7 +304,7 @@ void KimInit::do_init(char *model_name, char *user_units, char *model_units, KIM
fix_store->setptr("model_units", (void *) model_units);
// Begin output to log file
kim_init_log_delimiter("begin");
input->write_echo("#=== BEGIN kim-init ==========================================\n");
int kimerror;
KIM_SimulatorModel * simulatorModel;
@ -423,37 +424,14 @@ void KimInit::do_init(char *model_name, char *user_units, char *model_units, KIM
mesg += "\n";
}
}
else
mesg += "No mutable parameters. \n";
else mesg += "No mutable parameters. \n";
KIM_Model_Destroy(&pkim);
if (comm->me == 0)
{
input->write_echo(mesg.c_str());
}
input->write_echo(mesg);
}
// End output to log file
kim_init_log_delimiter("end");
}
/* ---------------------------------------------------------------------- */
void KimInit::kim_init_log_delimiter(std::string const &begin_end) const
{
if (comm->me == 0) {
std::string mesg;
if (begin_end == "begin")
mesg =
"#=== BEGIN kim-init ==========================================\n";
else if (begin_end == "end")
mesg =
"#=== END kim-init ============================================\n\n";
input->write_echo(mesg.c_str());
}
input->write_echo("#=== END kim-init ============================================\n\n");
}
/* ---------------------------------------------------------------------- */
@ -493,14 +471,8 @@ void KimInit::do_variables(char *user_units, char *model_units)
(char *)"efield",
(char *)"density"};
if (comm->me == 0) {
std::string mesg("# Conversion factors from ");
mesg += from;
mesg += " to ";
mesg += to;
mesg += ":\n";
input->write_echo(mesg.c_str());
}
input->write_echo(fmt::format("# Conversion factors from {} to {}:\n",
from,to));
for (int i = 0; i < nunits; i++) {
var_str = std::string("_u_") + std::string(units[i]);
@ -514,58 +486,43 @@ void KimInit::do_variables(char *user_units, char *model_units)
from,
to,
conversion_factor);
if (ier != 0) {
std::string err = std::string("Unable to obtain conversion factor: ") +
"unit = " + units[i] + "; "
"from = " + from + "; "
"to = " + to + ".";
error->all(FLERR,err);
}
if (ier != 0)
error->all(FLERR,fmt::format("Unable to obtain conversion factor: "
"unit = {}; from = {}; to = {}.",
units[i], from, to));
variable->internal_set(v_unit,conversion_factor);
if (comm->me == 0) {
std::stringstream mesg;
mesg << "variable " << std::setw(15) << std::left << var_str
<< " internal "
<< std::setprecision(12) << std::scientific << conversion_factor
<< std::endl;
input->write_echo(mesg.str().c_str());
}
input->write_echo(fmt::format("variable {:<15s} internal {:<15.12e}\n",
var_str, conversion_factor));
}
if (comm->me == 0) input->write_echo("#\n");
input->write_echo("#\n");
}
/* ---------------------------------------------------------------------- */
void KimInit::write_log_cite(char * model_name)
void KimInit::write_log_cite(const std::string &model_name)
{
KIM_Collections * coll;
int err = KIM_Collections_Create(&coll);
if (err) return;
int extent;
if (model_type == MO)
{
if (model_type == MO) {
err = KIM_Collections_CacheListOfItemMetadataFiles(
coll,KIM_COLLECTION_ITEM_TYPE_portableModel,model_name,&extent);
}
else if (model_type == SM)
{
coll,KIM_COLLECTION_ITEM_TYPE_portableModel,model_name.c_str(),&extent);
} else if (model_type == SM) {
err = KIM_Collections_CacheListOfItemMetadataFiles(
coll,KIM_COLLECTION_ITEM_TYPE_simulatorModel,model_name,&extent);
}
else
{
coll,KIM_COLLECTION_ITEM_TYPE_simulatorModel,model_name.c_str(),&extent);
} else {
error->all(FLERR,"Unknown model type.");
}
if (err)
{
if (err) {
KIM_Collections_Destroy(&coll);
return;
}
for (int i = 0; i < extent;++i)
{
for (int i = 0; i < extent;++i) {
char const * fileName;
int availableAsString;
char const * fileString;
@ -573,8 +530,7 @@ void KimInit::write_log_cite(char * model_name)
coll,i,&fileName,NULL,NULL,&availableAsString,&fileString);
if (err) continue;
if (0 == strncmp("kimcite",fileName,7))
{
if (0 == strncmp("kimcite",fileName,7)) {
if ((lmp->citeme) && (availableAsString)) lmp->citeme->add(fileString);
}
}

View File

@ -83,10 +83,9 @@ class KimInit : protected Pointers {
bool unit_conversion_mode;
void determine_model_type_and_units(char *, char *, char **, KIM_Model *&);
void write_log_cite(char *);
void write_log_cite(const std::string &);
void do_init(char *, char *, char *, KIM_Model *&);
void do_variables(char*, char*);
void kim_init_log_delimiter(std::string const &begin_end) const;
};
}

View File

@ -99,24 +99,6 @@ void KimInteractions::command(int narg, char **arg)
/* ---------------------------------------------------------------------- */
void KimInteractions::kim_interactions_log_delimiter(
std::string const begin_end) const
{
if (comm->me == 0) {
std::string mesg;
if (begin_end == "begin")
mesg =
"#=== BEGIN kim_interactions ==================================\n";
else if (begin_end == "end")
mesg =
"#=== END kim_interactions ====================================\n\n";
input->write_echo(mesg.c_str());
}
}
/* ---------------------------------------------------------------------- */
void KimInteractions::do_setup(int narg, char **arg)
{
bool fixed_types;
@ -145,7 +127,7 @@ void KimInteractions::do_setup(int narg, char **arg)
} else error->all(FLERR,"Must use 'kim_init' before 'kim_interactions'");
// Begin output to log file
kim_interactions_log_delimiter("begin");
input->write_echo("#=== BEGIN kim_interactions ==================================\n");
if (simulatorModel) {
@ -167,7 +149,7 @@ void KimInteractions::do_setup(int narg, char **arg)
simulatorModel,"atom-type-num-list",atom_type_num_list.c_str());
KIM_SimulatorModel_CloseTemplateMap(simulatorModel);
int len = strlen(atom_type_sym_list.c_str())+1;
int len = atom_type_sym_list.size()+1;
char *strbuf = new char[len];
char *strword;
@ -281,8 +263,7 @@ void KimInteractions::do_setup(int narg, char **arg)
}
// End output to log file
kim_interactions_log_delimiter("end");
input->write_echo("#=== END kim_interactions ====================================\n\n");
}
/* ---------------------------------------------------------------------- */
@ -377,7 +358,7 @@ void KimInteractions::KIM_SET_TYPE_PARAMETERS(char const *const input_line) cons
/* ---------------------------------------------------------------------- */
int KimInteractions::species_to_atomic_no(std::string const species) const
int KimInteractions::species_to_atomic_no(const std::string &species) const
{
if (species == "H") return 1;
else if (species == "He") return 2;

View File

@ -76,9 +76,8 @@ class KimInteractions : protected Pointers {
void command(int, char **);
private:
void do_setup(int, char **);
int species_to_atomic_no(std::string const species) const;
int species_to_atomic_no(const std::string &species) const;
void KIM_SET_TYPE_PARAMETERS(char const *const input_line) const;
void kim_interactions_log_delimiter(std::string const begin_end) const;
};
}

View File

@ -56,6 +56,8 @@
------------------------------------------------------------------------- */
#include "kim_param.h"
#include "fix_store_kim.h"
#include "pair_kim.h"
#include <mpi.h>
#include <cstring>
#include <string>
@ -66,8 +68,7 @@
#include "modify.h"
#include "variable.h"
#include "force.h"
#include "fix_store_kim.h"
#include "pair_kim.h"
#include "fmt/format.h"
extern "C"
{
@ -157,10 +158,9 @@ void KimParam::command(int narg, char **arg)
if (narg < 4)
error->all(FLERR, "Illegal kim_param command");
kim_param_get = (strcmp(arg[0], "get") == 0);
kim_param_set = (strcmp(arg[0], "set") == 0);
std::string kim_param_get_set = arg[0];
if (!kim_param_get && !kim_param_set) {
if ((kim_param_get_set != "get") && (kim_param_get_set != "set")) {
std::string msg("Incorrect arguments in kim_param command.\n");
msg += "'kim_param get/set' is mandatory.";
error->all(FLERR, msg);
@ -193,7 +193,8 @@ void KimParam::command(int narg, char **arg)
else
error->all(FLERR, "Must use 'kim_init' before 'kim_param'");
kim_param_log_delimiter("begin");
input->write_echo(fmt::format("#=== BEGIN kim-param {} ==================="
"==================\n",kim_param_get_set));
KIM_Model *pkim = NULL;
@ -211,7 +212,7 @@ void KimParam::command(int narg, char **arg)
if (!pkim)
error->all(FLERR, "Unable to get the KIM Portable Model.");
if (kim_param_set) {
if (kim_param_get_set == "set") {
atom_type_list = pairKIM->get_atom_type_list();
if (atom_type_list.empty())
error->all(FLERR, "The requested atom type list is empty.");
@ -220,7 +221,7 @@ void KimParam::command(int narg, char **arg)
error->all(FLERR, "Pair style is defined,"
" but there is no match for kim style in lammps.");
} else {
if (kim_param_set) {
if (kim_param_get_set == "set") {
std::string msg("Wrong kim_param set command.\n");
msg += "To set the new parameter values, pair style must be assigned.\n";
msg += "Must use 'kim_interactions' or";
@ -259,7 +260,7 @@ void KimParam::command(int narg, char **arg)
KIM_Model_GetNumberOfParameters(pkim, &numberOfParameters);
if (numberOfParameters) {
// Get the parameters
if (kim_param_get) {
if (kim_param_get_set == "get") {
// Parameter name
char *paramname = NULL;
// Variable name
@ -532,38 +533,17 @@ void KimParam::command(int narg, char **arg)
if (!isPairStyleAssigned)
KIM_Model_Destroy(&pkim);
kim_param_log_delimiter("end");
input->write_echo(fmt::format("#=== END kim-param {} ====================="
"==================\n",kim_param_get_set));
}
/* ---------------------------------------------------------------------- */
void KimParam::kim_param_log_delimiter(std::string const &begin_end) const
void KimParam::echo_var_assign(const std::string &name,
const std::string &value) const
{
if (comm->me == 0) {
std::string msg;
if (begin_end == "begin") {
msg = "#=== BEGIN kim-param ";
msg += kim_param_get ? "get " : "set ";
msg += "=====================================\n";
} else if (begin_end == "end") {
msg = "#=== END kim-param ";
msg += kim_param_get ? "get " : "set ";
msg += "=======================================\n\n";
}
input->write_echo(msg.c_str());
}
}
/* ---------------------------------------------------------------------- */
void KimParam::echo_var_assign(std::string const &name,
std::string const &value) const
{
if (comm->me == 0) {
std::string msg;
msg += "variable " + name + " string " + value + "\n";
input->write_echo(msg.c_str());
}
input->write_echo(fmt::format("variable {} string {}\n",
name, value));
}
#undef SNUM

View File

@ -80,10 +80,7 @@ public:
void command(int, char **);
private:
void kim_param_log_delimiter(std::string const &begin_end) const;
void echo_var_assign(std::string const &name, std::string const &value)
const;
void echo_var_assign(const std::string &name, const std::string &value) const;
private:
bool kim_param_get;

View File

@ -56,6 +56,7 @@
------------------------------------------------------------------------- */
#include "kim_query.h"
#include "fix_store_kim.h"
#include <mpi.h>
#include <cstring>
#include <string>
@ -67,7 +68,7 @@
#include "variable.h"
#include "version.h"
#include "info.h"
#include "fix_store_kim.h"
#include "fmt/format.h"
#if defined(LMP_KIM_CURL)
#include <sys/types.h>
@ -153,7 +154,7 @@ void KimQuery::command(int narg, char **arg)
error->all(FLERR,errmsg);
}
kim_query_log_delimiter("begin");
input->write_echo("#=== BEGIN kim-query =========================================\n");
char **varcmd = new char*[3];
varcmd[1] = (char *) "string";
@ -186,7 +187,7 @@ void KimQuery::command(int narg, char **arg)
input->variable->set(3,varcmd);
echo_var_assign(varname, value_string);
}
kim_query_log_delimiter("end");
input->write_echo("#=== END kim-query ===========================================\n\n");
delete[] varcmd;
delete[] value;
@ -342,29 +343,8 @@ char *do_query(char *qfunction, char * model_name, int narg, char **arg,
/* ---------------------------------------------------------------------- */
void KimQuery::kim_query_log_delimiter(std::string const begin_end) const
void KimQuery::echo_var_assign(const std::string & name,
const std::string & value) const
{
if (comm->me == 0) {
std::string mesg;
if (begin_end == "begin")
mesg =
"#=== BEGIN kim-query =========================================\n";
else if (begin_end == "end")
mesg =
"#=== END kim-query ===========================================\n\n";
input->write_echo(mesg.c_str());
}
}
/* ---------------------------------------------------------------------- */
void KimQuery::echo_var_assign(std::string const & name,
std::string const & value) const
{
if (comm->me == 0) {
std::string mesg;
mesg += "variable " + name + " string " + value + "\n";
input->write_echo(mesg.c_str());
}
input->write_echo(fmt::format("variable {} string {}\n",name,value));
}

View File

@ -73,8 +73,7 @@ class KimQuery : protected Pointers {
KimQuery(class LAMMPS *lmp) : Pointers(lmp) {};
void command(int, char **);
private:
void kim_query_log_delimiter(std::string const begin_end) const;
void echo_var_assign(std::string const & name, std::string const & value)
void echo_var_assign(const std::string &name, const std::string &value)
const;
};

View File

@ -1370,9 +1370,9 @@ double get_unit_conversion_factor(unit_type &unit_type_enum,
// Wrapper to the routine that gets the unit conversion. Translates strings
// to enumerations and then call get_unit_conversion_factor()
int lammps_unit_conversion(string const &unit_type_str,
string const &from_system_str,
string const &to_system_str,
int lammps_unit_conversion(const string &unit_type_str,
const string &from_system_str,
const string &to_system_str,
double &conversion_factor)
{
// initialize

View File

@ -53,7 +53,7 @@
Designed for use with the kim-api-2.0.2 (and newer) package
------------------------------------------------------------------------- */
int lammps_unit_conversion(std::string const &unit_type_str,
std::string const &from_system_str,
std::string const &to_system_str,
int lammps_unit_conversion(const std::string &unit_type_str,
const std::string &from_system_str,
const std::string &to_system_str,
double &conversion_factor);