cleanup and compilation for python off

This commit is contained in:
Axel Kohlmeyer
2020-12-07 17:47:21 -05:00
parent 280c19cf32
commit 3eb22e2406
6 changed files with 25 additions and 28 deletions

View File

@ -505,7 +505,7 @@ class lammps(object):
self.FIX_EXTERNAL_CALLBACK_FUNC = CFUNCTYPE(None, py_object, self.c_bigint, c_int, POINTER(self.c_tagint), POINTER(POINTER(c_double)), POINTER(POINTER(c_double))) self.FIX_EXTERNAL_CALLBACK_FUNC = CFUNCTYPE(None, py_object, self.c_bigint, c_int, POINTER(self.c_tagint), POINTER(POINTER(c_double)), POINTER(POINTER(c_double)))
self.lib.lammps_set_fix_external_callback.argtypes = [c_void_p, c_char_p, self.FIX_EXTERNAL_CALLBACK_FUNC, py_object] self.lib.lammps_set_fix_external_callback.argtypes = [c_void_p, c_char_p, self.FIX_EXTERNAL_CALLBACK_FUNC, py_object]
self.lib.lammps_set_fix_external_callback.restype = None self.lib.lammps_set_fix_external_callback.restype = None
self.mliappy = MLIAPPY(self) self.mliappy = MLIAPPY(self)
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
@ -2931,12 +2931,12 @@ class MLIAPPY():
def __init__(self,lammps): def __init__(self,lammps):
self._module = None self._module = None
self.lammps = lammps self.lammps = lammps
@property @property
def module(self): def module(self):
if self._module: if self._module:
return self._module return self._module
try: try:
# Begin Importlib magic to find the embedded python module # Begin Importlib magic to find the embedded python module
# This is needed because the filename for liblammps does not # This is needed because the filename for liblammps does not
@ -2959,10 +2959,10 @@ class MLIAPPY():
# End Importlib magic to find the embedded python module # End Importlib magic to find the embedded python module
except: except:
raise ImportError("Could not load MLIAPPY coupling module") raise ImportError("Could not load MLIAPPY coupling module")
def activate(self): def activate(self):
self.module self.module
def load_model(self,model): def load_model(self,model):
self.module.load_from_python(model) self.module.load_from_python(model)

View File

@ -29,7 +29,7 @@ MLIAPData::MLIAPData(LAMMPS *lmp, int gradgradflag_in, int *map_in,
class MLIAPModel* model_in, class MLIAPModel* model_in,
class MLIAPDescriptor* descriptor_in, class MLIAPDescriptor* descriptor_in,
class PairMLIAP* pairmliap_in) : class PairMLIAP* pairmliap_in) :
Pointers(lmp), gradforce(nullptr), betas(nullptr), Pointers(lmp), gradforce(nullptr), betas(nullptr),
descriptors(nullptr), eatoms(nullptr), gamma(nullptr), descriptors(nullptr), eatoms(nullptr), gamma(nullptr),
gamma_row_index(nullptr), gamma_col_index(nullptr), egradient(nullptr), gamma_row_index(nullptr), gamma_col_index(nullptr), egradient(nullptr),
numneighs(nullptr), iatoms(nullptr), ielems(nullptr), jatoms(nullptr), jelems(nullptr), numneighs(nullptr), iatoms(nullptr), ielems(nullptr), jatoms(nullptr), jelems(nullptr),

View File

@ -15,6 +15,8 @@
Contributing author: Nicholas Lubbers (LANL) Contributing author: Nicholas Lubbers (LANL)
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
#ifdef MLIAP_PYTHON
#include <Python.h> #include <Python.h>
#include "mliap_model_python.h" #include "mliap_model_python.h"
#include "mliap_model_python_couple.h" #include "mliap_model_python_couple.h"
@ -43,9 +45,9 @@ MLIAPModelPython::MLIAPModelPython(LAMMPS* lmp, char* coefffilename) :
PyGILState_Release(gstate); PyGILState_Release(gstate);
error->all(FLERR,"Could not initialize embedded Python"); error->all(FLERR,"Could not initialize embedded Python");
} }
PyObject* coupling_module = PyImport_ImportModule("mliap_model_python_couple"); PyObject* coupling_module = PyImport_ImportModule("mliap_model_python_couple");
if (!coupling_module) { if (!coupling_module) {
PyErr_Print(); PyErr_Print();
PyErr_Clear(); PyErr_Clear();
@ -97,7 +99,7 @@ void MLIAPModelPython::read_coeffs(char * fname)
error->all(FLERR,"Loading python model failure."); error->all(FLERR,"Loading python model failure.");
} }
PyGILState_Release(gstate); PyGILState_Release(gstate);
if (loaded) { if (loaded) {
this->connect_param_counts(); this->connect_param_counts();
} }
@ -113,7 +115,7 @@ void MLIAPModelPython::connect_param_counts()
nelements = MLIAPPY_nelements(this); nelements = MLIAPPY_nelements(this);
nparams = MLIAPPY_nparams(this); nparams = MLIAPPY_nparams(this);
ndescriptors = MLIAPPY_ndescriptors(this); ndescriptors = MLIAPPY_ndescriptors(this);
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
PyErr_Print(); PyErr_Print();
PyErr_Clear(); PyErr_Clear();
@ -137,7 +139,7 @@ void MLIAPModelPython::compute_gradients(MLIAPData* data)
if (not model_loaded) { if (not model_loaded) {
error->all(FLERR,"Model not loaded."); error->all(FLERR,"Model not loaded.");
} }
PyGILState_STATE gstate = PyGILState_Ensure(); PyGILState_STATE gstate = PyGILState_Ensure();
MLIAPPY_compute_gradients(this, data); MLIAPPY_compute_gradients(this, data);
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
@ -196,3 +198,5 @@ double MLIAPModelPython::memory_usage()
// todo: get approximate memory usage in coupling code. // todo: get approximate memory usage in coupling code.
return 0; return 0;
} }
#endif

View File

@ -38,7 +38,7 @@ protected:
virtual void read_coeffs(char *); virtual void read_coeffs(char *);
private: private:
}; };
} }

View File

@ -1,12 +1,5 @@
# cython: language_level=3 # cython: language_level=3
# distutils: language = c++ # distutils: language = c++
# distutils: define_macros="LMP_MLIAPPY"
# distutils: extra_compile_args= -stdlib=libc++ -std=c++11
# distutils: include_dirs = ../STUBS .. ../MLIAP
# distutils: extra_link_args= -stdlib=libc++
# Note: only the language_level and language commands are needed, the rest pertain
# to building mliap_model_python_couple as a standalone python extension, which
# is experimental.
cimport cython cimport cython
@ -88,11 +81,11 @@ def load_from_python(model):
LOADED_MODELS[c_id]=model LOADED_MODELS[c_id]=model
lmp_model = <MLIAPModelPython *> <uintptr_t> c_id lmp_model = <MLIAPModelPython *> <uintptr_t> c_id
lmp_model.connect_param_counts() lmp_model.connect_param_counts()
cdef public void MLIAPPY_unload_model(MLIAPModelPython * c_model) with gil: cdef public void MLIAPPY_unload_model(MLIAPModelPython * c_model) with gil:
del LOADED_MODELS[c_id(c_model)] del LOADED_MODELS[c_id(c_model)]
cdef public int MLIAPPY_nparams(MLIAPModelPython * c_model) with gil: cdef public int MLIAPPY_nparams(MLIAPModelPython * c_model) with gil:
return int(retrieve(c_model).n_params) return int(retrieve(c_model).n_params)
@ -107,7 +100,7 @@ cdef public void MLIAPPY_compute_gradients(MLIAPModelPython * c_model, MLIAPData
n_d = data.ndescriptors n_d = data.ndescriptors
n_a = data.natoms n_a = data.natoms
# Make numpy arrays from pointers # Make numpy arrays from pointers
beta_np = np.asarray(<double[:n_a,:n_d] > &data.betas[0][0]) beta_np = np.asarray(<double[:n_a,:n_d] > &data.betas[0][0])
desc_np = np.asarray(<double[:n_a,:n_d]> &data.descriptors[0][0]) desc_np = np.asarray(<double[:n_a,:n_d]> &data.descriptors[0][0])
@ -116,7 +109,7 @@ cdef public void MLIAPPY_compute_gradients(MLIAPModelPython * c_model, MLIAPData
# Invoke python model on numpy arrays. # Invoke python model on numpy arrays.
model(elem_np,desc_np,beta_np,en_np) model(elem_np,desc_np,beta_np,en_np)
# Get the total energy from the atom energy. # Get the total energy from the atom energy.
energy = np.sum(en_np) energy = np.sum(en_np)
data.energy = <double> energy data.energy = <double> energy

View File

@ -73,17 +73,17 @@ PairMLIAP::~PairMLIAP()
void PairMLIAP::compute(int eflag, int vflag) void PairMLIAP::compute(int eflag, int vflag)
{ {
// consistency checks // consistency checks
if (data->ndescriptors != model->ndescriptors) { if (data->ndescriptors != model->ndescriptors) {
error->all(FLERR,"Incompatible model and descriptor descriptor count"); error->all(FLERR,"Incompatible model and descriptor descriptor count");
}; };
if (data->nelements != model->nelements) { if (data->nelements != model->nelements) {
error->all(FLERR,"Incompatible model and descriptor element count"); error->all(FLERR,"Incompatible model and descriptor element count");
}; };
ev_init(eflag,vflag); ev_init(eflag,vflag);
data->generate_neighdata(list, eflag, vflag); data->generate_neighdata(list, eflag, vflag);
@ -249,7 +249,7 @@ void PairMLIAP::coeff(int narg, char **arg)
void PairMLIAP::e_tally(MLIAPData* data) void PairMLIAP::e_tally(MLIAPData* data)
{ {
if (eflag_global) eng_vdwl += data->energy; if (eflag_global) eng_vdwl += data->energy;
if (eflag_atom) if (eflag_atom)
for (int ii = 0; ii < data->natoms; ii++) { for (int ii = 0; ii < data->natoms; ii++) {
const int i = data->iatoms[ii]; const int i = data->iatoms[ii];
eatom[i] += data->eatoms[ii]; eatom[i] += data->eatoms[ii];