import re
import os
from ..utils import judge_file
[docs]
class MDFile:
def __init__(self, path=None):
"""Base class for reading files generated by LAMMPS.
Args:
path (str, optional): path to the file. Defaults to None.
"""
self._set_path(path)
self._get_info()
def _set_path(self, path=None):
if path is None:
raise ValueError("path is None! Please provide a path to LAMMPS-generated file.")
self.path = os.path.abspath(path)
judge_file(self.path)
print(f"You are processing the file: '{self.path}'")
def _get_info(self, line:int=1):
with open(self.path) as f:
info = f.readline()[2:]
print(f"'{self.path}' is generated by command ", end='')
if info.startswith("Chunk-averaged"):
print("'fix ave/chunk'")
elif info.startswith("Time-averaged"):
print("'fix ave/time'")
print(f"The first line is: {info}")
def _get_header(self, line_num:int) -> list:
"""extract header from the line contents.
Args:
line_num (int, optional): the line number (start from ``1``) including headers.
Returns:
list: a list of the property names
Notes:
In generated files from LAMMPS command, the line including headers usually have the following format:
``# header1 header2 header3 ...``
the return list : [header1, header2, header3, ...]
"""
with open(self.path) as f:
for i, line in enumerate(f.readlines(), 1): # enumerate(iterator,1) means counting from 1
if i == line_num:
return re.split('\s+', line.strip())[1:] # split the line with regex '\s+' to list, and discard the first element "#"
[docs]
def read_file(self):
"""You need to override this method in the child class."""
pass
# class System:
# def __init__(self, T=298.0, timestep=1.0) -> None:
# """initialize the properties of system
# Args:
# T (float, optional): temperature of system. Defaults to 298.0 [K].
# timestep (float, optional): temperature you set in LAMMPS input file. Defaults to 1.0 [fs].
# """
# self.T = T
# self.timestep = timestep
# print("---------------- System Properties --------------")
# print(f"Temperature:\t{self.T} K")
# print(f"Timestep:\t{self.timestep} fs")
# print("-------------------------------------------------")