Several improvements to capabilities and build.

- cmake fixed, no longer needs numpy headers.
- models can be loaded from an external interepreter.
This commit is contained in:
Nicholas Lubbers
2020-11-26 12:40:28 -07:00
parent 7c1634e57f
commit 35f2c9bdf2
18 changed files with 369 additions and 89 deletions

26
examples/mliappy/README Normal file
View File

@ -0,0 +1,26 @@
README for MLIAPPY Example
These examples run the Ta06 example from the MLIAP package, but using the python coupling.
1: Running models using LAMMPS executable: in.mliap.snap.Ta06A.
To run this, first run convert_mliap_Ta06A.py, which will convert the Ta06 potential into a pytorch model.
It will be saved as "Ta06A.mliap.pytorch.model.pkl".
It will also copy the "mliappy_pytorch.py" file into the current working directory. mliappy_pytorch.py contains
class definitions suitable for wrapping an arbitrary energy model MLIAPPY. It must be available to python when
creating or unpicking a pytorch model for MLIAPPY.
From that point you can run the example lmp -in in.mliap.snap.Ta06A -echo both
2: Running models from python with LAMMPS in library mode: load_external.py
Before testing this, ensure that the first example (using LAMMPS executable) works.
Not all python installations support this mode of operation.
Too test if your interpreter supports this, run:
`python test_pylibs.py`
and examine the output.
If this succeeds, you should be able to run:
`python load_external.py`

View File

@ -1,11 +0,0 @@
README for MLIAPPY Example
This example runs the Ta06 example from the MLIAP example, but using the python coupling.
To run this, first run convert_mliap_Ta06A.py, which will convert the Ta06 potential into a pytorch model.
It will be saved as "Ta06A.mliap.pytorch.model.pkl".
It will also copy the "torchlink.py" file into the current working directory. torchlink.py contains
class definitions suitable for wrapping an arbitrary energy model MLIAPPY.
From that point you can run the example lmp -in in.mliap.snap.Ta06A -echo both

View File

@ -22,7 +22,7 @@ with torch.autograd.no_grad():
lin.bias.set_(torch.as_tensor(bias,dtype=torch.float64).unsqueeze(0))
# Wrap the pytorch model for usage with MLIAPPY
model = mliappy_pytorch.IgnoreTypes(lin)
model = mliappy_pytorch.IgnoreElems(lin)
n_descriptors = lin.weight.shape[1]
n_params = mliappy_pytorch.calc_n_params(model)
n_types = 1
@ -30,4 +30,4 @@ linked_model = mliappy_pytorch.TorchWrapper64(model,n_descriptors=n_descriptors,
# Save the result.
with open("Ta06A.mliap.pytorch.model.pkl",'wb') as pfile:
pickle.dump(linked_model,pfile)
pickle.dump(linked_model,pfile)

View File

@ -1,4 +1,4 @@
# Demonstrate MLIAP interface to kinear SNAP potential
# Demonstrate MLIAP interface to linear SNAP potential
# Initialize simulation

View File

@ -0,0 +1,104 @@
# Demonstrate how to load a model from the python side.
# This is essentially the same as in.mliap.pytorch.Ta06A--
# except that python is the driving program, and lammps
# is in library mode.
before_loading =\
"""
# Demonstrate MLIAP interface to linear SNAP potential
# Initialize simulation
variable nsteps index 100
variable nrep equal 4
variable a equal 3.316
units metal
# generate the box and atom positions using a BCC lattice
variable nx equal ${nrep}
variable ny equal ${nrep}
variable nz equal ${nrep}
boundary p p p
lattice bcc $a
region box block 0 ${nx} 0 ${ny} 0 ${nz}
create_box 1 box
create_atoms 1 box
mass 1 180.88
# choose potential
# DATE: 2014-09-05 UNITS: metal CONTRIBUTOR: Aidan Thompson athomps@sandia.gov CITATION: Thompson, Swiler, Trott, Foiles and Tucker, arxiv.org, 1409.3880 (2014)
# Definition of SNAP potential Ta_Cand06A
# Assumes 1 LAMMPS atom type
variable zblcutinner equal 4
variable zblcutouter equal 4.8
variable zblz equal 73
# Specify hybrid with SNAP, ZBL
pair_style hybrid/overlay &
zbl ${zblcutinner} ${zblcutouter} &
mliap model mliappy LATER &
descriptor sna Ta06A.mliap.descriptor
pair_coeff 1 1 zbl ${zblz} ${zblz}
pair_coeff * * mliap Ta
"""
after_loading =\
"""
# Setup output
compute eatom all pe/atom
compute energy all reduce sum c_eatom
compute satom all stress/atom NULL
compute str all reduce sum c_satom[1] c_satom[2] c_satom[3]
variable press equal (c_str[1]+c_str[2]+c_str[3])/(3*vol)
thermo_style custom step temp epair c_energy etotal press v_press
thermo 10
thermo_modify norm yes
# Set up NVE run
timestep 0.5e-3
neighbor 1.0 bin
neigh_modify once no every 1 delay 0 check yes
# Run MD
velocity all create 300.0 4928459 loop geom
fix 1 all nve
run ${nsteps}
"""
import lammps
lmp = lammps.lammps(cmdargs=['-echo','both'])
# This commmand must be run before the MLIAP object is declared in lammps.
lmp.mliappy.activate()
lmp.commands_string(before_loading)
# Now the model is declared, but empty -- because the model filename
# was given as "LATER".
# Load the python module, construct on the fly, do whatever, here:
import pickle
with open('Ta06A.mliap.pytorch.model.pkl','rb') as pfile:
model = pickle.load(pfile)
# Now that you have a model, connect it to the pairstyle
lmp.mliappy.load_model(model)
# Proceed with whatever calculations you like.
lmp.commands_string(after_loading)

View File

@ -0,0 +1,12 @@
import sysconfig, os,ctypes
library = sysconfig.get_config_vars('INSTSONAME')[0]
pylib = ctypes.CDLL(library)
connected = pylib.Py_IsInitialized()
if not connected:
print("FAILURE: This interpreter is not compatible with python-driven mliappy.")
else:
print("SUCCESS: This interpreter is compatible with python-driven MLIAPPY")