Skip to content

Commit

Permalink
Merge pull request #426 from matk86/master
Browse files Browse the repository at this point in the history
io.lammps updates
  • Loading branch information
shyuep committed Jun 27, 2016
2 parents 3080b5b + 1c9d2b3 commit c6bd09c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
29 changes: 18 additions & 11 deletions pymatgen/io/lammps/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
the ensemble type(NVT, NPT etc), max number of iterations etc.
"""

import six
import json
import os
from functools import partial
Expand Down Expand Up @@ -98,36 +99,42 @@ def write_input(self, filename, data_filename=None):
f.write(self.__str__())
# write the data file if present
if self.lammps_data:
print("Writing the data to {}".format(self.data_filename))
print("Data file: {}".format(self.data_filename))
self.lammps_data.write_data_file(filename=self.data_filename)

@staticmethod
def from_file(name, filename, data_filename=None, is_forcefield=False):
def from_file(name, filename, lammps_data=None, data_filename="in.data",
user_lammps_settings={}, is_forcefield=False):
"""
Read in the input settings from json file as ordereddict. Also
reads in the datafile if provided.
Read in the input settings from json file as ordereddict.
Note: with monty.serialization.loadfn the order of paramters in the
json file is not preserved
Args:
filename (string): name of the file with the lamps control
paramters
data_filename (string): path to the data file
lammps_data (string/LammpsData/LammpsForceFieldData): path to the
data file or an appropriate object
data_filename (string): name of the the lammps data file
user_lammps_settings (dict): User lammps settings
is_forcefield (bool): whether the data file has forcefield and
topology info in it.
topology info in it. This is required only if lammps_data is
a path to the data file instead of a data object
Returns:
DictLammpsInput
"""
with open(filename) as f:
config_dict = json.load(f, object_pairs_hook=OrderedDict)
lammps_data = None
if data_filename:
lammps_data = lammps_data
if isinstance(lammps_data, six.string_types):
if is_forcefield:
lammps_data = LammpsForceFieldData.from_file(data_filename)
lammps_data = LammpsForceFieldData.from_file(lammps_data)
else:
lammps_data = LammpsData.from_file(data_filename)
return DictLammpsInput(name, config_dict, lammps_data)
lammps_data = LammpsData.from_file(lammps_data)
return DictLammpsInput(name, config_dict, lammps_data=lammps_data,
data_filename=data_filename,
user_lammps_settings=user_lammps_settings)

def as_dict(self):
d = MSONable.as_dict(self)
Expand Down
35 changes: 35 additions & 0 deletions pymatgen/io/lammps/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,41 @@ def run(self, copy_to_current_on_exit=False):
return None


class LammpsRunner(object):
def __init__(self, dict_input, input_filename="lammps.in", bin="lammps"):
"""
LAMMPS wrapper
Args:
dict_input (DictLammpsInput): lammps input object
input_filename (string): input file name
bin (string): command to run, excluding the input file name
"""
self.lammps_bin = bin.split()
if not which(self.lammps_bin[-1]):
raise RuntimeError(
"LammpsRunner requires the executable {} to be in the path. "
"Please download and install LAMMPS from " \
"http://lammps.sandia.gov. "
"Don't forget to add the binary to your path".format(self.lammps_bin[-1]))
self.dict_input = dict_input
self.input_filename = input_filename

def run(self):
"""
Write the input/data files and run LAMMPS.
"""
self.dict_input.write_input(self.input_filename)
print("Input file: {}".format(self.input_filename))
lammps_cmd = self.lammps_bin + ['-in', self.input_filename]
print("Running: {}".format(" ".join(lammps_cmd)))
p = Popen(lammps_cmd, stdout=PIPE, stderr=PIPE)
p.wait()
(stdout, stderr) = p.communicate()
print("Done")
print(stdout, stderr)


if __name__ == '__main__':
ethanol_coords = [[0.00720, -0.56870, 0.00000],
[-1.28540, 0.24990, 0.00000],
Expand Down

0 comments on commit c6bd09c

Please sign in to comment.