def expand(self, ratio): if self.celldm: for i in range(3): self.celldm[i] = self.celldm[i] * ratio self.R = qtk.fractional2xyz(self.R_scale, self.celldm) else: self.R = self.R * ratio
def read_cif(self, name, **kwargs): xyz = open(name, 'r') content = xyz.readlines() xyz.close() l_list = filter(lambda x: '_cell_length_' in x, content) a_list = filter(lambda x: '_cell_angle_' in x, content) l = [float(filter(None, l_str.split(' '))[1]) for l_str in l_list] a = [float(filter(None, a_str.split(' '))[1]) for a_str in a_list] a = np.cos(np.array(a) * (np.pi / 180.)) fx, fy, fz = map(len, [ filter(lambda x: '_atom_site_fract_x' in x, content), filter(lambda x: '_atom_site_fract_y' in x, content), filter(lambda x: '_atom_site_fract_z' in x, content), ]) if (fx, fy, fz) != (1, 1, 1): qtk.exit("Failed! Only fractional coordingates are implemented") r_flag = filter(lambda x: '_atom_site_occupancy' in x, content) r_ind = content.index(r_flag[0]) + 1 atoms = np.array( [filter(None, r_str.split(' ')) for r_str in content[r_ind:]] ) self.periodic = True self.celldm = np.concatenate([l, a]) self.R_scale = atoms[:, 3:6].astype(float) self.R = qtk.fractional2xyz(self.R_scale, self.celldm) self.type_list = atoms[:, 0].tolist() self.Z = np.array(map(qtk.n2Z, atoms[:, 0])) self.N = len(self.Z) self.string = ['' for _ in range(self.N)] self.name = name
def read_xyz(self, name, **kwargs): xyz = open(name, 'r') content = xyz.readlines() xyz.close() content = [line.replace('\t', ' ') for line in content] prop_list = ['charge', 'celldm', 'scale', 'symmetry'] for prop in prop_list: try: prop_str = filter(lambda x: prop in x, content)[0] prop_str = re.sub('.*:', '', prop_str) prop_data = prop_str.split(' ') prop_data = filter(None, prop_data) if len(prop_data) == 1: try: setattr(self, prop, float(prop_data[0])) except Exception as exc: if prop == 'symmetry': setattr(self, prop, prop_data[0].strip()) elif len(prop_data) > 1: setattr(self, prop, [float(_) for _ in prop_data]) except ValueError as exc: setattr(self, prop, False) qtk.warning("setting attribute %s with error: %s" % \ (prop, exc)) except: setattr(self, prop, False) if not self.charge: self.charge = 0 if self.celldm or self.scale: self.periodic = True if self.celldm: assert len(self.celldm) == 6 self.N = int(content[0]) coord_list = content[2 : self.N + 2] coord = [filter(None,[a for a in entry.split(' ')]) for entry in coord_list] type_list = list(np.array(coord)[:,0]) self.type_list = [str(elem) for elem in type_list] self.Z = [qtk.n2Z(elem) for elem in self.type_list] self.Z = np.array(self.Z) self.R = np.array(coord)[:,1:4].astype(float) self.box = False if self.celldm: self.periodic = True angle = self.celldm[3:] angle_sum = sum([abs(entry) for entry in angle]) if angle_sum == 0: self.box = self.celldm[:3] self.R_scale = copy.deepcopy(self.R) self.R = qtk.fractional2xyz(self.R_scale, self.celldm)
def read_xyz(self, name, **kwargs): xyz = open(name, 'r') content = xyz.readlines() xyz.close() content = [line.replace('\t', ' ') for line in content] prop_list = ['charge', 'celldm', 'scale', 'symmetry', 'isolated'] for prop in prop_list: try: prop_str = filter(lambda x: prop in x, content)[0] prop_str = re.sub('.*:', '', prop_str) prop_data = prop_str.split(' ') prop_data = filter(None, prop_data) if len(prop_data) == 1: try: setattr(self, prop, float(prop_data[0])) except Exception as exc: if prop == 'symmetry' or prop == 'isolated': setattr(self, prop, prop_data[0].strip()) elif len(prop_data) > 1: setattr(self, prop, [float(_) for _ in prop_data]) except ValueError as exc: setattr(self, prop, False) qtk.warning("setting attribute %s with error: %s" % \ (prop, exc)) except: setattr(self, prop, False) # convert celldm acoording to proper symmetry is still not working # universally due to abinit and vasp k-point sampling # require explicity reciprocal coordinate #if hasattr(self, 'symmetry') and self.symmetry in ['bcc', 'fcc']: # new_dm = copy.deepcopy(self.celldm) # if np.linalg.norm(np.array(new_dm[3:])) < 1E-5: # lattice_cube = np.array(new_dm[:3]) * np.eye(3) # if self.symmetry == 'fcc': # vec1 = (lattice_cube[1] + lattice_cube[2])/2 # vec2 = (lattice_cube[0] + lattice_cube[2])/2 # vec3 = (lattice_cube[0] + lattice_cube[1])/2 # elif self.symmetry == 'bcc': # vec1 = (-lattice_cube[0] + lattice_cube[1] + lattice_cube[2])/2 # vec2 = ( lattice_cube[0] - lattice_cube[1] + lattice_cube[2])/2 # vec3 = ( lattice_cube[0] + lattice_cube[1] - lattice_cube[2])/2 # nv1 = vec1 / np.linalg.norm(vec1) # nv2 = vec2 / np.linalg.norm(vec2) # nv3 = vec3 / np.linalg.norm(vec3) # new_dm = [ # np.linalg.norm(vec1), # np.linalg.norm(vec2), # np.linalg.norm(vec3), # np.dot(nv2, nv3), # np.dot(nv1, nv3), # np.dot(nv1, nv2), # ] # qtk.warning( # "symmetry is %s but celldm is set to cubic, reset from %s to %s"\ # % (self.symmetry, str(self.celldm), str(new_dm)) # ) # self.celldm = new_dm if not self.charge: self.charge = 0 if self.celldm or self.scale: self.periodic = True if self.celldm: assert len(self.celldm) == 6 self.N = int(content[0]) prop_list = content[1] try: self.prop_list = np.array(prop_list.split(' ')).astype(float) except: self.prop_list = prop_list coord_list = content[2 : self.N + 2] coord = [filter(None,[a for a in entry.replace("\n", '').split(' ')]) for entry in coord_list] type_list = list(np.array(coord)[:,0]) self.type_list = [str(elem) for elem in type_list] self.Z = [qtk.n2Z(elem) for elem in self.type_list] self.Z = np.array(self.Z) self.R = np.array(coord)[:,1:4].astype(float) self.box = False if self.celldm: self.periodic = True angle = self.celldm[3:] angle_sum = sum([abs(entry) for entry in angle]) if angle_sum == 0: self.box = self.celldm[:3] if not self.isolated: self.R_scale = copy.deepcopy(self.R) self.R = qtk.fractional2xyz(self.R_scale, self.celldm)