refactor code to make better use of std::string and simplify using changes to Variable class API

This commit is contained in:
Axel Kohlmeyer
2020-07-17 01:23:17 -04:00
parent 63bebf58fd
commit 2fb6a61f2a
6 changed files with 41 additions and 64 deletions

View File

@ -420,51 +420,45 @@ void KimInit::do_init(char *model_name, char *user_units, char *model_units, KIM
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
void KimInit::do_variables(char *user_units, char *model_units) void KimInit::do_variables(const std::string &from, const std::string &to)
{ {
char *from = user_units, *to = model_units;
Variable *variable = input->variable;
// refuse conversion from or to reduced units // refuse conversion from or to reduced units
if ((strcmp(from,"lj") == 0) || (strcmp(to,"lj") == 0)) if ((from == "lj") || (to == "lj"))
error->all(FLERR,"Cannot set up conversion variables for 'lj' units"); error->all(FLERR,"Cannot set up conversion variables for 'lj' units");
// get index to internal style variables. create, if needed. // get index to internal style variables. create, if needed.
// set conversion factors for newly created variables. // set conversion factors for newly created variables.
double conversion_factor; double conversion_factor;
int ier; int ier;
char *args[3];
std::string var_str; std::string var_str;
args[1] = (char *)"internal";
args[2] = (char *)"1.0";
int v_unit; int v_unit;
int const nunits = 14; const char *units[] = {"mass",
char *units[nunits] = {(char *)"mass", "distance",
(char *)"distance", "time",
(char *)"time", "energy",
(char *)"energy", "velocity",
(char *)"velocity", "force",
(char *)"force", "torque",
(char *)"torque", "temperature",
(char *)"temperature", "pressure",
(char *)"pressure", "viscosity",
(char *)"viscosity", "charge",
(char *)"charge", "dipole",
(char *)"dipole", "efield",
(char *)"efield", "density",
(char *)"density"}; nullptr};
input->write_echo(fmt::format("# Conversion factors from {} to {}:\n", input->write_echo(fmt::format("# Conversion factors from {} to {}:\n",
from,to)); from,to));
for (int i = 0; i < nunits; ++i) { auto variable = input->variable;
var_str = std::string("_u_") + std::string(units[i]); for (int i = 0; units[i] != nullptr; ++i) {
args[0] = (char *)var_str.c_str(); var_str = std::string("_u_") + units[i];
v_unit = variable->find(args[0]); v_unit = variable->find(var_str.c_str());
if (v_unit < 0) { if (v_unit < 0) {
variable->set(3,args); variable->set(var_str + " internal 1.0");
v_unit = variable->find(args[0]); v_unit = variable->find(var_str.c_str());
} }
ier = lammps_unit_conversion(units[i], ier = lammps_unit_conversion(units[i],
from, from,

View File

@ -85,7 +85,7 @@ class KimInit : protected Pointers {
void determine_model_type_and_units(char *, char *, char **, KIM_Model *&); void determine_model_type_and_units(char *, char *, char **, KIM_Model *&);
void write_log_cite(char *); void write_log_cite(char *);
void do_init(char *, char *, char *, KIM_Model *&); void do_init(char *, char *, char *, KIM_Model *&);
void do_variables(char*, char*); void do_variables(const std::string &, const std::string &);
}; };
} }

View File

@ -111,12 +111,7 @@ void kimProperty::command(int narg, char **arg)
// Get the kim_str ptr to the data associated with a kim_property_str // Get the kim_str ptr to the data associated with a kim_property_str
// variable // variable
char *kim_str = char *kim_str = input->variable->retrieve("kim_property_str");
input->variable->retrieve(const_cast<char *>("kim_property_str"));
char **kim_str_cmd = new char *[3];
kim_str_cmd[0] = const_cast<char *>("kim_property_str");
PyGILState_STATE gstate = PyGILState_Ensure(); PyGILState_STATE gstate = PyGILState_Ensure();
// kim_property module // kim_property module
@ -203,15 +198,8 @@ void kimProperty::command(int narg, char **arg)
// Python function returned a string value // Python function returned a string value
const char *pystr = PyUnicode_AsUTF8(pValue); const char *pystr = PyUnicode_AsUTF8(pValue);
if (kim_str) input->variable->set_string("kim_property_str", pystr);
kim_str_cmd[2] = const_cast<char *>(pystr); else input->variable->set(std::string("kim_property_str string ") + pystr);
if (kim_str) {
input->variable->set_string(kim_str_cmd[0], kim_str_cmd[2]);
} else {
kim_str_cmd[1] = const_cast<char *>("string");
input->variable->set(3, kim_str_cmd);
}
Py_XDECREF(pArgs); Py_XDECREF(pArgs);
Py_XDECREF(pFunc); Py_XDECREF(pFunc);
@ -268,10 +256,7 @@ void kimProperty::command(int narg, char **arg)
// Python function returned a string value // Python function returned a string value
const char *pystr = PyUnicode_AsUTF8(pValue); const char *pystr = PyUnicode_AsUTF8(pValue);
input->variable->set_string("kim_property_str", pystr);
kim_str_cmd[2] = const_cast<char *>(pystr);
input->variable->set_string(kim_str_cmd[0], kim_str_cmd[2]);
Py_XDECREF(pArgs); Py_XDECREF(pArgs);
Py_XDECREF(pFunc); Py_XDECREF(pFunc);
@ -335,10 +320,7 @@ void kimProperty::command(int narg, char **arg)
// Python function returned a string value // Python function returned a string value
const char *pystr = PyUnicode_AsUTF8(pValue); const char *pystr = PyUnicode_AsUTF8(pValue);
input->variable->set_string("kim_property_str", pystr);
kim_str_cmd[2] = const_cast<char *>(pystr);
input->variable->set_string(kim_str_cmd[0], kim_str_cmd[2]);
Py_XDECREF(pArgs); Py_XDECREF(pArgs);
Py_XDECREF(pFunc); Py_XDECREF(pFunc);
@ -402,10 +384,7 @@ void kimProperty::command(int narg, char **arg)
// Python function returned a string value // Python function returned a string value
const char *pystr = PyUnicode_AsUTF8(pValue); const char *pystr = PyUnicode_AsUTF8(pValue);
input->variable->set_string("kim_property_str", pystr);
kim_str_cmd[2] = const_cast<char *>(pystr);
input->variable->set_string(kim_str_cmd[0], kim_str_cmd[2]);
Py_XDECREF(pArgs); Py_XDECREF(pArgs);
Py_XDECREF(pFunc); Py_XDECREF(pFunc);
@ -464,8 +443,7 @@ void kimProperty::command(int narg, char **arg)
pValue = NULL; pValue = NULL;
// Destroy the variable // Destroy the variable
kim_str_cmd[1] = const_cast<char *>("delete"); input->variable->set("kim_property_str delete");
input->variable->set(2, kim_str_cmd);
Py_XDECREF(pArgs); Py_XDECREF(pArgs);
Py_XDECREF(pFunc); Py_XDECREF(pFunc);
@ -474,7 +452,6 @@ void kimProperty::command(int narg, char **arg)
PyGILState_Release(gstate); PyGILState_Release(gstate);
delete[] kim_str_cmd;
#endif // PY_MAJOR_VERSION #endif // PY_MAJOR_VERSION
#endif // LMP_PYTHON #endif // LMP_PYTHON
} }

View File

@ -52,8 +52,13 @@
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
Designed for use with the kim-api-2.0.2 (and newer) package Designed for use with the kim-api-2.0.2 (and newer) package
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
#ifndef LMP_KIM_UNITS_H
#define LMP_KIM_UNITS_H
#include <string>
int lammps_unit_conversion(const std::string &unit_type_str, int lammps_unit_conversion(const std::string &unit_type_str,
const std::string &from_system_str, const std::string &from_system_str,
const std::string &to_system_str, const std::string &to_system_str,
double &conversion_factor); double &conversion_factor);
#endif

View File

@ -578,13 +578,14 @@ void Variable::set(char *name, int narg, char **arg)
called via library interface, so external programs can set variables called via library interface, so external programs can set variables
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
int Variable::set_string(char *name, char *str) int Variable::set_string(const char *name, const char *str)
{ {
int ivar = find(name); int ivar = find(name);
if (ivar < 0) return -1; if (ivar < 0) return -1;
if (style[ivar] != STRING) return -1; if (style[ivar] != STRING) return -1;
delete [] data[ivar][0]; delete [] data[ivar][0];
copy(1,&str,data[ivar]); data[ivar][0] = new char[strlen(str)+1];
strcpy(data[ivar][0],str);
return 0; return 0;
} }

View File

@ -24,10 +24,10 @@ class Variable : protected Pointers {
public: public:
Variable(class LAMMPS *); Variable(class LAMMPS *);
~Variable(); ~Variable();
void set(const std::string &);
void set(int, char **); void set(int, char **);
void set(const std::string &);
void set(char *, int, char **); void set(char *, int, char **);
int set_string(char *, char *); int set_string(const char *, const char *);
int next(int, char **); int next(int, char **);
int find(const char *); int find(const char *);