Source code for postmd.system.mdfile

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_cmd() def _set_path(self, path=None) -> 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_cmd(self, line_num:int=1) -> None: """get the command used to generate the file Args: line (int, optional): the line number (start from ``1``) including command. Defaults to 1. """ # line参数是为了以后覆写此方法 with open(self.path) as f: for i, line in enumerate(f, 1): # enumerate()从1开始计数 if i == line_num: info = line break out_flag=0 if "Chunk-averaged".lower() in info.lower(): out_flag = 'fix ave/chunk' elif "Time-averaged".lower() in info.lower(): out_flag = 'fix ave/time' if out_flag: print(f"'{self.path}' is generated by command {out_flag}") print(f"The command line is: {info.strip('#').strip()}") 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("-------------------------------------------------")