def read_from_file(self, filename):
self.clear()<EOL>with open(filename) as f:<EOL><INDENT>line = next(f)<EOL>if not line.startswith("<STR_LIT>"):<EOL><INDENT>raise FileFormatError("<STR_LIT>")<EOL><DEDENT>current_section = None<EOL>sections = {}<EOL>for line in f:<EOL><INDENT>line = line.strip()<EOL>if line == "<STR_LIT>":<EOL><INDENT>continue<EOL><DEDENT>elif "<STR_LIT>" in line:<EOL><INDENT>words = line.split()<EOL>current_section = []<EOL>section_name = words[<NUM_LIT:1>][<NUM_LIT:2>:]<EOL>if section_name.endswith("<STR_LIT::>"):<EOL><INDENT>section_name = section_name[:-<NUM_LIT:1>]<EOL><DEDENT>sections[section_name] = current_section<EOL><DEDENT>else:<EOL><INDENT>current_section.append(line)<EOL><DEDENT><DEDENT><DEDENT>self.title = sections['<STR_LIT>'][<NUM_LIT:0>]<EOL>molecules = []<EOL>numbers = []<EOL>for line in sections['<STR_LIT>']:<EOL><INDENT>words = line.split()<EOL>self.atom_types.append(words[<NUM_LIT:5>])<EOL>self.charges.append(float(words[<NUM_LIT:6>]))<EOL>self.names.append(words[<NUM_LIT:3>])<EOL>molecules.append(int(words[<NUM_LIT:2>]))<EOL>atom = periodic[words[<NUM_LIT:4>]]<EOL>if atom is None:<EOL><INDENT>numbers.append(<NUM_LIT:0>)<EOL><DEDENT>else:<EOL><INDENT>numbers.append(periodic[words[<NUM_LIT:4>]].number)<EOL><DEDENT><DEDENT>self.molecules = np.array(molecules)-<NUM_LIT:1><EOL>self.numbers = np.array(numbers)<EOL>self.charges = np.array(self.charges)<EOL>tmp = []<EOL>for line in sections['<STR_LIT>']:<EOL><INDENT>tmp.extend(int(word) for word in line.split())<EOL><DEDENT>self.bonds = np.reshape(np.array(tmp), (-<NUM_LIT:1>, <NUM_LIT:2>))-<NUM_LIT:1><EOL>tmp = []<EOL>for line in sections['<STR_LIT>']:<EOL><INDENT>tmp.extend(int(word) for word in line.split())<EOL><DEDENT>self.bends = np.reshape(np.array(tmp), (-<NUM_LIT:1>, <NUM_LIT:3>))-<NUM_LIT:1><EOL>tmp = []<EOL>for line in sections['<STR_LIT>']:<EOL><INDENT>tmp.extend(int(word) for word in line.split())<EOL><DEDENT>self.dihedrals = np.reshape(np.array(tmp), (-<NUM_LIT:1>, <NUM_LIT:4>))-<NUM_LIT:1><EOL>tmp = []<EOL>for line in sections['<STR_LIT>']:<EOL><INDENT>tmp.extend(int(word) for word in line.split())<EOL><DEDENT>self.impropers = np.reshape(np.array(tmp), (-<NUM_LIT:1>, <NUM_LIT:4>))-<NUM_LIT:1><EOL>
Load a PSF file