From ed7775f083f19b406c174875c7ca0f517a4702d5 Mon Sep 17 00:00:00 2001 From: Aidan Thompson Date: Fri, 4 Dec 2020 14:48:02 -0700 Subject: [PATCH] Added blessed log files --- examples/mliap/README.pytorch | 25 ++++-- examples/mliap/mliap.pytorch.Ta06A.py | 106 -------------------------- examples/mliap/mliap_pytorch_Ta06A.py | 106 ++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 114 deletions(-) delete mode 100644 examples/mliap/mliap.pytorch.Ta06A.py create mode 100644 examples/mliap/mliap_pytorch_Ta06A.py diff --git a/examples/mliap/README.pytorch b/examples/mliap/README.pytorch index 642b39b842..7f511168a7 100644 --- a/examples/mliap/README.pytorch +++ b/examples/mliap/README.pytorch @@ -1,6 +1,7 @@ README for the PyTorch energy model example -These examples run the Ta06 example from the MLIAP package, but using the python coupling. +These two examples run the Ta06 example from the MLIAP package, but using the python coupling. +The differ only in how the coupling is constructed. 1: Running models using LAMMPS executable: in.mliap.pytorch.Ta06A @@ -13,14 +14,22 @@ creating or unpicking a PyTorch energy model. From that point you can run the example lmp -in in.mliap.pytorch.Ta06A -echo both -2: Running models from python with LAMMPS in library mode: load_external.py +2: Running models from python with LAMMPS in library mode: mliap_pytorch_Ta06A.py Before testing this, ensure that the first example (using LAMMPS executable) works. +Also, not all python installations support this mode of operation. +It requires that the Python interpreter be initialized. +To check this for your Python library, +run the Py_IsInitialized() method. +If the return value is True, you should be able to run the example, +as follows: -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. +`python mliap_pytorch_Ta06A.py` + +or + +`mpirun -np 4 python mliap_pytorch_Ta06A.py` + +The resultant log.lammps output should be identical to that generated +by in.mliap.snap.Ta06A and in.mliap.pytorch.Ta06A -If this succeeds, you should be able to run: -`python load_external.py` diff --git a/examples/mliap/mliap.pytorch.Ta06A.py b/examples/mliap/mliap.pytorch.Ta06A.py deleted file mode 100644 index 5f3ecaadc8..0000000000 --- a/examples/mliap/mliap.pytorch.Ta06A.py +++ /dev/null @@ -1,106 +0,0 @@ -# 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/PyTorch 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() - -# setup the simulation and declare an empty model -# by specifying model filename as "LATER" - -lmp.commands_string(before_loading) - -# define the PyTorch model by loading a pkl file. -# this could also be done in other ways. - -import pickle -with open('Ta06A.mliap.pytorch.model.pkl','rb') as pfile: - model = pickle.load(pfile) - -# connect the PyTorch model to the mliap pair style - -lmp.mliappy.load_model(model) - -# run the simulation with the mliap pair style - -lmp.commands_string(after_loading) diff --git a/examples/mliap/mliap_pytorch_Ta06A.py b/examples/mliap/mliap_pytorch_Ta06A.py new file mode 100644 index 0000000000..71c89f112e --- /dev/null +++ b/examples/mliap/mliap_pytorch_Ta06A.py @@ -0,0 +1,106 @@ +# 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/PyTorch 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() + +# setup the simulation and declare an empty model +# by specifying model filename as "LATER" + +lmp.commands_string(before_loading) + +# define the PyTorch model by loading a pkl file. +# this could also be done in other ways. + +import pickle +with open('Ta06A.mliap.pytorch.model.pkl','rb') as pfile: + model = pickle.load(pfile) + +# connect the PyTorch model to the mliap pair style + +lmp.mliappy.load_model(model) + +# run the simulation with the mliap pair style + +lmp.commands_string(after_loading)