Initial commit of mliap unified work

This commit is contained in:
Steven Ray Anaya
2022-04-15 14:22:46 -06:00
parent cd8cdc711c
commit ebbace403a
19 changed files with 1046 additions and 34 deletions

View File

@ -17,27 +17,58 @@
import sys
import importlib
import importlib.util
import importlib.machinery
import importlib.abc
from ctypes import pythonapi, c_int, c_void_p, py_object
# This dynamic loader imports a python module embedded in a shared library.
# The default value of api_version is 1013 because it has been stable since 2006.
class DynamicLoader(importlib.abc.Loader):
def __init__(self,module_name,library,api_version=1013):
self.api_version = api_version
attr = "PyInit_"+module_name
initfunc = getattr(library,attr)
# c_void_p is standin for PyModuleDef *
initfunc.restype = c_void_p
initfunc.argtypes = ()
self.module_def = initfunc()
def create_module(self, spec):
createfunc = pythonapi.PyModule_FromDefAndSpec2
# c_void_p is standin for PyModuleDef *
createfunc.argtypes = c_void_p, py_object, c_int
createfunc.restype = py_object
module = createfunc(self.module_def, spec, self.api_version)
return module
def exec_module(self, module):
execfunc = pythonapi.PyModule_ExecDef
# c_void_p is standin for PyModuleDef *
execfunc.argtypes = py_object, c_void_p
execfunc.restype = c_int
result = execfunc(module, self.module_def)
if result<0:
raise ImportError()
def activate_mliappy(lmp):
try:
# Begin Importlib magic to find the embedded python module
# This is needed because the filename for liblammps does not
# match the spec for normal python modules, wherein
# file names match with PyInit function names.
# Also, python normally doesn't look for extensions besides '.so'
# We fix both of these problems by providing an explict
# path to the extension module 'mliap_model_python_couple' in
path = lmp.lib._name
loader = importlib.machinery.ExtensionFileLoader('mliap_model_python_couple', path)
spec = importlib.util.spec_from_loader('mliap_model_python_couple', loader)
module = importlib.util.module_from_spec(spec)
sys.modules['mliap_model_python_couple'] = module
spec.loader.exec_module(module)
# End Importlib magic to find the embedded python module
library = lmp.lib
module_names = ["mliap_model_python_couple", "mliap_unifiedpy"]
api_version = library.lammps_PYTHON_API_VERSION()
for module_name in module_names:
# Make Machinery
loader = DynamicLoader(module_name,library,api_version)
spec = importlib.util.spec_from_loader(module_name,loader)
# Do the import
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
except Exception as ee:
raise ImportError("Could not load ML-IAP python coupling module.") from ee
@ -49,4 +80,3 @@ def load_model(model):
"the pair style. Call lammps.mliap.activate_mliappy(lmp)."
) from ie
mliap_model_python_couple.load_from_python(model)

View File

@ -0,0 +1,23 @@
from abc import ABC, abstractmethod
class MLIAPUnified(ABC):
"""Abstract base class for MLIAPUnified."""
def __init__(self):
self.interface = None
self.element_types = None
self.ndescriptors = None
self.nparams = None
self.rcutfac = None
@abstractmethod
def compute_gradients(self, data):
"""Compute gradients."""
@abstractmethod
def compute_descriptors(self, data):
"""Compute descriptors."""
@abstractmethod
def compute_forces(self, data):
"""Compute forces."""

View File

@ -0,0 +1,35 @@
from .mliap_unified_abc import MLIAPUnified
import numpy as np
class MLIAPUnifiedLJ(MLIAPUnified):
"""Test implementation for MLIAPUnified."""
def __init__(self):
super().__init__()
def compute_gradients(self, data):
"""Test compute_gradients."""
def compute_descriptors(self, data):
"""Test compute_descriptors."""
def compute_forces(self, data):
"""Test compute_forces."""
eij, fij = self.compute_pair_ef(data)
data.update_pair_energy(eij)
data.update_pair_forces(fij)
def compute_pair_ef(self, data):
rij = data.rij
r2inv = 1.0 / np.sum(rij ** 2, axis=1)
r6inv = r2inv * r2inv * r2inv
lj1 = 4.0 * self.epsilon * self.sigma**12
lj2 = 4.0 * self.epsilon * self.sigma**6
eij = r6inv * (lj1 * r6inv - lj2)
fij = r6inv * (3.0 * lj2 - 6.0 * lj2 * r6inv) * r2inv
fij = fij[:, np.newaxis] * rij
return eij, fij