def load(self): "Rewrite load method" PosCar.load(self) with open(self.filename, 'r') as f: for i in range(self.totline): f.readline() #get dimension of 3d array grid = f.readline().strip(whitespace) empty = not grid # empty row while empty: grid = f.readline().strip(whitespace) empty = not grid x, y, z = line2list(grid, dtype=int) #read electron localization function data elf_data = [] for line in f: datalist = line2list(line) elf_data.extend(datalist) ######################################### # # # !!! Notice !!! # # NGX is the length of the **0th** axis # # NGY is the length of the **1st** axis # # NGZ is the length of the **2nd** axis # # # ######################################### #reshape to 3d array elf_data = np.array(elf_data).reshape((x, y, z), order='F') #set attrs self.grid = x, y, z self.elf_data = elf_data return
def freq_iterator(self): """ 返回频率信息字典的迭代器。 Return frequency iterator to generating frequency related data. """ with open(self.filename, "r") as f: collecting = False for line in f: freq = self.freq_regex.match(line) title = self.title_regex.match(line) empty_line = (line.strip(whitespace) == "") if freq: freq_data = list(freq.groups()) # Collect start. if title and not collecting: collecting = True coords, deltas = [], [] # Collect stop. elif empty_line and collecting: collecting = False freq_data.append(coords) freq_data.append(deltas) freq_dict = dict(zip(self.freq_info, freq_data)) yield freq_dict # Collect data. elif collecting: x, y, z, dx, dy, dz = line2list(line) coord = (x, y, z) delta = (dx, dy, dz) coords.append(coord) deltas.append(delta)
def force_iterator(self): """ 返回每个离子步迭代的步数,坐标和每个原子受力信息。 Return a generator yield ionic_step, coordinates, forces on atoms. NOTE: ionic step starts from 1 **NOT 0**. """ with open(self.filename, "r") as f: ion_step = 0 # Force data collection flags. collect_begin = False collecting = False # Collect force data for each ionic step and yield. for line in f: if not collect_begin: if self.force_regex.match(line): collect_begin = True ion_step += 1 elif not collecting: if "-"*6 in line: collecting = True coordinates = [] forces = [] else: if "-"*6 in line: collecting = False collect_begin = False yield ion_step, coordinates, forces else: x, y, z, fx, fy, fz = line2list(line) coordinates.append([x, y, z]) forces.append([fx, fy, fz])
def force_iterator(self): """ 返回每个离子步迭代的步数,坐标和每个原子受力信息。 Return a generator yield ionic_step, coordinates, forces on atoms. NOTE: ionic step starts from 1 **NOT 0**. """ with open(self.filename, "r") as f: ion_step = 0 # Force data collection flags. collect_begin = False collecting = False # Collect force data for each ionic step and yield. for line in f: if not collect_begin: if self.force_regex.match(line): collect_begin = True ion_step += 1 elif not collecting: if "-" * 6 in line: collecting = True coordinates = [] forces = [] else: if "-" * 6 in line: collecting = False collect_begin = False yield ion_step, coordinates, forces else: x, y, z, fx, fy, fz = line2list(line) coordinates.append([x, y, z]) forces.append([fx, fy, fz])
def __read_info(info): ''' read lattice info ''' # read lattice info info['bases_const'] = float(f.readline().strip()) # lattice basis info['bases'] = [] for i in range(3): vector = line2list(f.readline()) info['bases'].append(vector)
def load(self): "Load all data in file into array." data = [] with open(self.filename, 'r') as f: for line in f: line = line.strip() if not line: # blank line continue if not line[0].isdigit(): # comment line or not if not line.startswith('-'): continue elif not line[1].isdigit() and line[1] != '.': continue linedata = line2list(line, field=self.field, dtype=self.dtype) data.append(linedata) self.data = np.array(data) return data
def iforces(self): """ 返回每个离子步迭代的步数,坐标和每个原子受力信息。 Return a generator yield ionic_step, coordinates, forces on atoms. NOTE: ionic step starts from 1 **NOT 0**. """ # Define namedtuple for item in force iteration. ForceItem = namedtuple('ForceItem', ['step', 'coordinates', 'forces']) with open(self.filename, "r") as f: ion_step = 0 # Force data collection flags. collect_begin = False collecting = False # Collect force data for each ionic step and yield. for line in f: if not collect_begin: if self.force_regex.match(line): collect_begin = True ion_step += 1 elif not collecting: if "-" * 6 in line: collecting = True coordinates = [] forces = [] else: if "-" * 6 in line: collecting = False collect_begin = False yield ForceItem._make([ion_step, coordinates, forces]) else: x, y, z, fx, fy, fz = line2list(line) coordinates.append([x, y, z]) forces.append([fx, fy, fz])
def iforces(self): """ 返回每个离子步迭代的步数,坐标和每个原子受力信息。 Return a generator yield ionic_step, coordinates, forces on atoms. NOTE: ionic step starts from 1 **NOT 0**. """ # Define namedtuple for item in force iteration. ForceItem = namedtuple('ForceItem', ['step', 'coordinates', 'forces']) with open(self.filename, "r") as f: ion_step = 0 # Force data collection flags. collect_begin = False collecting = False # Collect force data for each ionic step and yield. for line in f: if not collect_begin: if self.force_regex.match(line): collect_begin = True ion_step += 1 elif not collecting: if "-"*6 in line: collecting = True coordinates = [] forces = [] else: if "-"*6 in line: collecting = False collect_begin = False yield ForceItem._make([ion_step, coordinates, forces]) else: x, y, z, fx, fy, fz = line2list(line) coordinates.append([x, y, z]) forces.append([fx, fy, fz])
flush = f.readline().strip() if flush == system: __read_info(info) # update info # skip atom info f.readline() f.readline() step = f.readline().strip().split()[-1] else: step = flush.split()[-1] content += ' ' + str(info['natom']) + '\n' content += 'STEP = ' + str(step) + '\n' for i in range(info['natom']): content += '%2s' % info['atom_names'][i] relative_coord = np.array(line2list(f.readline()), dtype='float64') direct_coord = np.dot(info['bases'], relative_coord) * info['bases_const'] content += '%16.9f%16.9f%16.9f\n' %\ (direct_coord[0], direct_coord[1], direct_coord[2]) out_name = 'xdatcar' + str(step) + '.xyz' with open(out_name, 'w') as f1: f1.write(content) with open('xdatcar.xyz', 'a') as f2: f2.write(content) f2.close() except IndexError: break