Update fix_python_move.cpp
This commit is contained in:
@ -21,8 +21,9 @@
|
||||
#include "error.h"
|
||||
#include "lmppython.h"
|
||||
#include "python_compat.h"
|
||||
#include "python_utils.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <Python.h> // IWYU pragma: export
|
||||
|
||||
using namespace LAMMPS_NS;
|
||||
@ -40,7 +41,7 @@ FixPythonMove::FixPythonMove(LAMMPS *lmp, int narg, char **arg) :
|
||||
|
||||
py_move = nullptr;
|
||||
|
||||
PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
PyUtils::GIL lock;
|
||||
|
||||
// add current directory to PYTHONPATH
|
||||
PyObject *py_path = PySys_GetObject((char *)"path");
|
||||
@ -48,70 +49,50 @@ FixPythonMove::FixPythonMove(LAMMPS *lmp, int narg, char **arg) :
|
||||
|
||||
|
||||
// create integrator instance
|
||||
char *full_cls_name = arg[3];
|
||||
char *lastpos = strrchr(full_cls_name, '.');
|
||||
std::string full_cls_name = arg[3];
|
||||
size_t lastpos = full_cls_name.rfind(".");
|
||||
|
||||
if (lastpos == nullptr) {
|
||||
if (lastpos == std::string::npos) {
|
||||
error->all(FLERR,"Fix python/integrate requires fully qualified class name");
|
||||
}
|
||||
|
||||
size_t module_name_length = strlen(full_cls_name) - strlen(lastpos);
|
||||
size_t cls_name_length = strlen(lastpos)-1;
|
||||
std::string module_name = full_cls_name.substr(0, lastpos);
|
||||
std::string cls_name = full_cls_name.substr(lastpos+1);
|
||||
|
||||
char *module_name = new char[module_name_length+1];
|
||||
char *cls_name = new char[cls_name_length+1];
|
||||
strncpy(module_name, full_cls_name, module_name_length);
|
||||
module_name[module_name_length] = 0;
|
||||
|
||||
strcpy(cls_name, lastpos+1);
|
||||
|
||||
PyObject *pModule = PyImport_ImportModule(module_name);
|
||||
PyObject *pModule = PyImport_ImportModule(module_name.c_str());
|
||||
if (!pModule) {
|
||||
PyErr_Print();
|
||||
PyErr_Clear();
|
||||
PyGILState_Release(gstate);
|
||||
PyUtils::Print_Errors();
|
||||
error->all(FLERR,"Loading python integrator module failure");
|
||||
}
|
||||
|
||||
// create LAMMPS atom type to potential file type mapping in python class
|
||||
// by calling 'lammps_pair_style.map_coeff(name,type)'
|
||||
|
||||
PyObject *py_move_type = PyObject_GetAttrString(pModule, cls_name);
|
||||
PyObject *py_move_type = PyObject_GetAttrString(pModule, cls_name.c_str());
|
||||
if (!py_move_type) {
|
||||
PyErr_Print();
|
||||
PyErr_Clear();
|
||||
PyGILState_Release(gstate);
|
||||
PyUtils::Print_Errors();
|
||||
error->all(FLERR,"Could not find integrator class in module'");
|
||||
}
|
||||
|
||||
delete [] module_name;
|
||||
delete [] cls_name;
|
||||
|
||||
PyObject *ptr = PY_VOID_POINTER(lmp);
|
||||
PyObject *arglist = Py_BuildValue("(O)", ptr);
|
||||
PyObject *py_move_obj = PyObject_CallObject(py_move_type, arglist);
|
||||
Py_DECREF(arglist);
|
||||
PyObject *py_move_obj = PyObject_CallFunction(py_move_type, "O", ptr);
|
||||
Py_CLEAR(ptr);
|
||||
|
||||
if (!py_move_obj) {
|
||||
PyErr_Print();
|
||||
PyErr_Clear();
|
||||
PyGILState_Release(gstate);
|
||||
PyUtils::Print_Errors();
|
||||
error->all(FLERR,"Could not instantiate instance of integrator class'");
|
||||
}
|
||||
|
||||
// check object interface
|
||||
py_move = (void *) py_move_obj;
|
||||
|
||||
PyGILState_Release(gstate);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
FixPythonMove::~FixPythonMove()
|
||||
{
|
||||
PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
if (py_move) Py_DECREF((PyObject*) py_move);
|
||||
PyGILState_Release(gstate);
|
||||
PyUtils::GIL lock;
|
||||
Py_CLEAR(py_move);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
@ -130,122 +111,82 @@ int FixPythonMove::setmask()
|
||||
|
||||
void FixPythonMove::init()
|
||||
{
|
||||
PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
PyObject *py_move_obj = (PyObject *) py_move;
|
||||
PyObject *py_init = PyObject_GetAttrString(py_move_obj,(char *)"init");
|
||||
if (!py_init) {
|
||||
PyErr_Print();
|
||||
PyErr_Clear();
|
||||
PyGILState_Release(gstate);
|
||||
error->all(FLERR,"Could not find 'init()' method'");
|
||||
PyUtils::GIL lock;
|
||||
PyObject * result = PyObject_CallMethod((PyObject *)py_move, "init", nullptr);
|
||||
|
||||
if (!result) {
|
||||
PyUtils::Print_Errors();
|
||||
error->all(FLERR,"Fix python/move init() method failed");
|
||||
}
|
||||
PyObject *result = PyEval_CallObject(py_init, nullptr);
|
||||
if (!result && (comm->me == 0)) PyErr_Print();
|
||||
PyGILState_Release(gstate);
|
||||
if (!result) error->all(FLERR,"Fix python/move init() method failed");
|
||||
Py_CLEAR(result);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
void FixPythonMove::initial_integrate(int vflag)
|
||||
{
|
||||
PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
PyObject *py_move_obj = (PyObject *) py_move;
|
||||
PyObject *py_initial_integrate = PyObject_GetAttrString(py_move_obj,"initial_integrate");
|
||||
if (!py_initial_integrate) {
|
||||
PyErr_Print();
|
||||
PyErr_Clear();
|
||||
PyGILState_Release(gstate);
|
||||
error->all(FLERR,"Could not find 'initial_integrate' method'");
|
||||
PyUtils::GIL lock;
|
||||
PyObject * result = PyObject_CallMethod((PyObject*)py_move, "initial_integrate", "i", vflag);
|
||||
|
||||
if (!result) {
|
||||
PyUtils::Print_Errors();
|
||||
error->all(FLERR,"Fix python/move initial_integrate() method failed");
|
||||
}
|
||||
PyObject *arglist = Py_BuildValue("(i)", vflag);
|
||||
PyObject *result = PyEval_CallObject(py_initial_integrate, arglist);
|
||||
Py_DECREF(arglist);
|
||||
if (!result && (comm->me == 0)) PyErr_Print();
|
||||
PyGILState_Release(gstate);
|
||||
if (!result) error->all(FLERR,"Fix python/move initial_integrate() "
|
||||
"method failed");
|
||||
Py_CLEAR(result);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
void FixPythonMove::final_integrate()
|
||||
{
|
||||
PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
PyObject *py_move_obj = (PyObject *) py_move;
|
||||
PyObject *py_final_integrate = PyObject_GetAttrString(py_move_obj,"final_integrate");
|
||||
if (!py_final_integrate) {
|
||||
PyErr_Print();
|
||||
PyErr_Clear();
|
||||
PyGILState_Release(gstate);
|
||||
error->all(FLERR,"Could not find 'final_integrate' method'");
|
||||
PyUtils::GIL lock;
|
||||
PyObject * result = PyObject_CallMethod((PyObject*)py_move, "final_integrate", nullptr);
|
||||
|
||||
if (!result) {
|
||||
PyUtils::Print_Errors();
|
||||
error->all(FLERR,"Fix python/move final_integrate() method failed");
|
||||
}
|
||||
PyObject *result = PyEval_CallObject(py_final_integrate, nullptr);
|
||||
if (!result && (comm->me == 0)) PyErr_Print();
|
||||
PyGILState_Release(gstate);
|
||||
if (!result) error->all(FLERR,"Fix python/move final_integrate() method "
|
||||
"failed");
|
||||
Py_CLEAR(result);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
void FixPythonMove::initial_integrate_respa(int vflag, int ilevel, int iloop)
|
||||
{
|
||||
PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
PyObject *py_move_obj = (PyObject *) py_move;
|
||||
PyObject *py_initial_integrate_respa = PyObject_GetAttrString(py_move_obj,"initial_integrate_respa");
|
||||
if (!py_initial_integrate_respa) {
|
||||
PyErr_Print();
|
||||
PyErr_Clear();
|
||||
PyGILState_Release(gstate);
|
||||
error->all(FLERR,"Could not find 'initial_integrate_respa' method'");
|
||||
PyUtils::GIL lock;
|
||||
PyObject * result = PyObject_CallMethod((PyObject*)py_move, "initial_integrate_respa", "iii", vflag, ilevel, iloop);
|
||||
|
||||
if (!result) {
|
||||
PyUtils::Print_Errors();
|
||||
error->all(FLERR,"Fix python/move initial_integrate_respa() method failed");
|
||||
}
|
||||
PyObject *arglist = Py_BuildValue("(iii)", vflag, ilevel, iloop);
|
||||
PyObject *result = PyEval_CallObject(py_initial_integrate_respa, arglist);
|
||||
Py_DECREF(arglist);
|
||||
if (!result && (comm->me == 0)) PyErr_Print();
|
||||
PyGILState_Release(gstate);
|
||||
if (!result) error->all(FLERR,"Fix python/move initial_integrate_respa() "
|
||||
"method failed");
|
||||
Py_CLEAR(result);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
void FixPythonMove::final_integrate_respa(int ilevel, int iloop)
|
||||
{
|
||||
PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
PyObject *py_move_obj = (PyObject *) py_move;
|
||||
PyObject *py_final_integrate_respa = PyObject_GetAttrString(py_move_obj,"final_integrate_respa");
|
||||
if (!py_final_integrate_respa) {
|
||||
PyErr_Print();
|
||||
PyErr_Clear();
|
||||
PyGILState_Release(gstate);
|
||||
error->all(FLERR,"Could not find 'final_integrate_respa' method'");
|
||||
PyUtils::GIL lock;
|
||||
PyObject * result = PyObject_CallMethod((PyObject*)py_move, "final_integrate_respa", "ii", ilevel, iloop);
|
||||
|
||||
if (!result) {
|
||||
PyUtils::Print_Errors();
|
||||
error->all(FLERR,"Fix python/move final_integrate_respa() method failed");
|
||||
}
|
||||
PyObject *arglist = Py_BuildValue("(ii)", ilevel, iloop);
|
||||
PyObject *result = PyEval_CallObject(py_final_integrate_respa, arglist);
|
||||
Py_DECREF(arglist);
|
||||
if (!result && (comm->me == 0)) PyErr_Print();
|
||||
PyGILState_Release(gstate);
|
||||
if (!result) error->all(FLERR,"Fix python/move final_integrate_respa() "
|
||||
"method failed");
|
||||
Py_CLEAR(result);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
void FixPythonMove::reset_dt()
|
||||
{
|
||||
PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
PyObject *py_move_obj = (PyObject *) py_move;
|
||||
PyObject *py_reset_dt = PyObject_GetAttrString(py_move_obj,"reset_dt");
|
||||
if (!py_reset_dt) {
|
||||
PyErr_Print();
|
||||
PyErr_Clear();
|
||||
PyGILState_Release(gstate);
|
||||
error->all(FLERR,"Could not find 'reset_dt' method'");
|
||||
PyUtils::GIL lock;
|
||||
PyObject * result = PyObject_CallMethod((PyObject*)py_move, "reset_dt", nullptr);
|
||||
|
||||
if (!result) {
|
||||
PyUtils::Print_Errors();
|
||||
error->all(FLERR,"Fix python/move reset_dt() method failed");
|
||||
}
|
||||
PyObject *result = PyEval_CallObject(py_reset_dt, nullptr);
|
||||
if (!result && (comm->me == 0)) PyErr_Print();
|
||||
PyGILState_Release(gstate);
|
||||
if (!result) error->all(FLERR,"Fix python/move reset_dt() method failed");
|
||||
Py_CLEAR(result);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user