"""
This module provides functionality to post-process a LAMMPS data file generated by Moltemplate.
Specifically, it finds the line indicating the number of bond types, changes it from '2' to '3',
and inserts a new line specifying extra bonds per atom.
Usage (command line):
python fix_datafiles.py <datafile>
Example:
python fix_datafiles.py b70_N200_L500.data
"""
import sys
[docs]
def fix_data_file(file_path: str) -> None:
"""
Read a LAMMPS data file and modify its bond type section.
The function looks for the line containing "2 bond types",
replaces that "2" with "3", and then inserts a line "50 extra bond per atom"
immediately after. All other lines are left unchanged.
Args:
file_path (str): Path to the LAMMPS data file to be modified.
Raises:
FileNotFoundError: If the given file does not exist.
"""
with open(file_path, 'r') as f:
lines = f.readlines()
modified_lines = []
for line in lines:
if '2 bond types' in line:
# Replace '2 bond types' with '3 bond types'
modified_line = line.replace('2', '3', 1)
modified_lines.append(modified_line)
# Insert the new line specifying extra bonds per atom
modified_lines.append('50 extra bond per atom\n')
else:
modified_lines.append(line)
with open(file_path, 'w') as f:
f.writelines(modified_lines)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python fix_datafiles.py <datafile>")
sys.exit(1)
datafile = sys.argv[1]
try:
fix_data_file(datafile)
print(f"Fixed data file: {datafile}")
except FileNotFoundError:
print(f"Error: File not found: {datafile}")
sys.exit(1)