refactor code to make better use of std::string and simplify using changes to Variable class API
This commit is contained in:
@ -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
|
||||
|
||||
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");
|
||||
|
||||
// get index to internal style variables. create, if needed.
|
||||
// set conversion factors for newly created variables.
|
||||
double conversion_factor;
|
||||
int ier;
|
||||
char *args[3];
|
||||
std::string var_str;
|
||||
args[1] = (char *)"internal";
|
||||
args[2] = (char *)"1.0";
|
||||
int v_unit;
|
||||
int const nunits = 14;
|
||||
char *units[nunits] = {(char *)"mass",
|
||||
(char *)"distance",
|
||||
(char *)"time",
|
||||
(char *)"energy",
|
||||
(char *)"velocity",
|
||||
(char *)"force",
|
||||
(char *)"torque",
|
||||
(char *)"temperature",
|
||||
(char *)"pressure",
|
||||
(char *)"viscosity",
|
||||
(char *)"charge",
|
||||
(char *)"dipole",
|
||||
(char *)"efield",
|
||||
(char *)"density"};
|
||||
const char *units[] = {"mass",
|
||||
"distance",
|
||||
"time",
|
||||
"energy",
|
||||
"velocity",
|
||||
"force",
|
||||
"torque",
|
||||
"temperature",
|
||||
"pressure",
|
||||
"viscosity",
|
||||
"charge",
|
||||
"dipole",
|
||||
"efield",
|
||||
"density",
|
||||
nullptr};
|
||||
|
||||
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]);
|
||||
args[0] = (char *)var_str.c_str();
|
||||
v_unit = variable->find(args[0]);
|
||||
auto variable = input->variable;
|
||||
for (int i = 0; units[i] != nullptr; ++i) {
|
||||
var_str = std::string("_u_") + units[i];
|
||||
v_unit = variable->find(var_str.c_str());
|
||||
if (v_unit < 0) {
|
||||
variable->set(3,args);
|
||||
v_unit = variable->find(args[0]);
|
||||
variable->set(var_str + " internal 1.0");
|
||||
v_unit = variable->find(var_str.c_str());
|
||||
}
|
||||
ier = lammps_unit_conversion(units[i],
|
||||
from,
|
||||
|
||||
@ -85,7 +85,7 @@ class KimInit : protected Pointers {
|
||||
void determine_model_type_and_units(char *, char *, char **, KIM_Model *&);
|
||||
void write_log_cite(char *);
|
||||
void do_init(char *, char *, char *, KIM_Model *&);
|
||||
void do_variables(char*, char*);
|
||||
void do_variables(const std::string &, const std::string &);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@ -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
|
||||
// variable
|
||||
char *kim_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");
|
||||
|
||||
char *kim_str = input->variable->retrieve("kim_property_str");
|
||||
PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
|
||||
// kim_property module
|
||||
@ -203,15 +198,8 @@ void kimProperty::command(int narg, char **arg)
|
||||
|
||||
// Python function returned a string value
|
||||
const char *pystr = PyUnicode_AsUTF8(pValue);
|
||||
|
||||
kim_str_cmd[2] = const_cast<char *>(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);
|
||||
}
|
||||
if (kim_str) input->variable->set_string("kim_property_str", pystr);
|
||||
else input->variable->set(std::string("kim_property_str string ") + pystr);
|
||||
|
||||
Py_XDECREF(pArgs);
|
||||
Py_XDECREF(pFunc);
|
||||
@ -268,10 +256,7 @@ void kimProperty::command(int narg, char **arg)
|
||||
|
||||
// Python function returned a string value
|
||||
const char *pystr = PyUnicode_AsUTF8(pValue);
|
||||
|
||||
kim_str_cmd[2] = const_cast<char *>(pystr);
|
||||
|
||||
input->variable->set_string(kim_str_cmd[0], kim_str_cmd[2]);
|
||||
input->variable->set_string("kim_property_str", pystr);
|
||||
|
||||
Py_XDECREF(pArgs);
|
||||
Py_XDECREF(pFunc);
|
||||
@ -335,10 +320,7 @@ void kimProperty::command(int narg, char **arg)
|
||||
|
||||
// Python function returned a string value
|
||||
const char *pystr = PyUnicode_AsUTF8(pValue);
|
||||
|
||||
kim_str_cmd[2] = const_cast<char *>(pystr);
|
||||
|
||||
input->variable->set_string(kim_str_cmd[0], kim_str_cmd[2]);
|
||||
input->variable->set_string("kim_property_str", pystr);
|
||||
|
||||
Py_XDECREF(pArgs);
|
||||
Py_XDECREF(pFunc);
|
||||
@ -402,10 +384,7 @@ void kimProperty::command(int narg, char **arg)
|
||||
|
||||
// Python function returned a string value
|
||||
const char *pystr = PyUnicode_AsUTF8(pValue);
|
||||
|
||||
kim_str_cmd[2] = const_cast<char *>(pystr);
|
||||
|
||||
input->variable->set_string(kim_str_cmd[0], kim_str_cmd[2]);
|
||||
input->variable->set_string("kim_property_str", pystr);
|
||||
|
||||
Py_XDECREF(pArgs);
|
||||
Py_XDECREF(pFunc);
|
||||
@ -464,8 +443,7 @@ void kimProperty::command(int narg, char **arg)
|
||||
pValue = NULL;
|
||||
|
||||
// Destroy the variable
|
||||
kim_str_cmd[1] = const_cast<char *>("delete");
|
||||
input->variable->set(2, kim_str_cmd);
|
||||
input->variable->set("kim_property_str delete");
|
||||
|
||||
Py_XDECREF(pArgs);
|
||||
Py_XDECREF(pFunc);
|
||||
@ -474,7 +452,6 @@ void kimProperty::command(int narg, char **arg)
|
||||
|
||||
PyGILState_Release(gstate);
|
||||
|
||||
delete[] kim_str_cmd;
|
||||
#endif // PY_MAJOR_VERSION
|
||||
#endif // LMP_PYTHON
|
||||
}
|
||||
|
||||
@ -52,8 +52,13 @@
|
||||
/* ----------------------------------------------------------------------
|
||||
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,
|
||||
const std::string &from_system_str,
|
||||
const std::string &to_system_str,
|
||||
double &conversion_factor);
|
||||
#endif
|
||||
|
||||
@ -578,13 +578,14 @@ void Variable::set(char *name, int narg, char **arg)
|
||||
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);
|
||||
if (ivar < 0) return -1;
|
||||
if (style[ivar] != STRING) return -1;
|
||||
delete [] data[ivar][0];
|
||||
copy(1,&str,data[ivar]);
|
||||
data[ivar][0] = new char[strlen(str)+1];
|
||||
strcpy(data[ivar][0],str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@ -24,10 +24,10 @@ class Variable : protected Pointers {
|
||||
public:
|
||||
Variable(class LAMMPS *);
|
||||
~Variable();
|
||||
void set(const std::string &);
|
||||
void set(int, char **);
|
||||
void set(const std::string &);
|
||||
void set(char *, int, char **);
|
||||
int set_string(char *, char *);
|
||||
int set_string(const char *, const char *);
|
||||
int next(int, char **);
|
||||
|
||||
int find(const char *);
|
||||
|
||||
Reference in New Issue
Block a user