def pRDF(traj_file,coor_file,index_file,solute_index,dmax=20,nbins=32): ''' A simple pRDF test here. ''' START_TIME =Time.time() HAS_TRAJFILE = False if os.path.isfile(traj_file): HAS_TRAJFILE = True atom_list =Simple_atom.Get_atom_list(coor_file) index_list =Index.Read_index_to_Inclass(index_file) solute_list =index_list[solute_index].group_list solvent_list =Get_solvent_list(atom_list) if HAS_TRAJFILE: U=MDAnalysis.Universe(coor_file,traj_file) else: U=MDAnalysis.Universe(coor_file) GR=numpy.zeros((100),dtype=numpy.float64) Data_file=open("datafile.xvg",'w') #step 1 # Get the center of the solute. for ts in U.trajectory: print "Checking frame number: %d" %(ts.frame) solute_atoms = dict() coor_x=0.0 coor_y=0.0 coor_z=0.0 X_min = ts._x[0] X_max = ts._x[0] Y_min = ts._y[0] Y_max = ts._y[0] Z_min = ts._z[0] Z_max = ts._z[0] R_solute =0.0 for i in solute_list: coor_x += ts._x[i-1] coor_y += ts._y[i-1] coor_z += ts._z[i-1] u_atom = MDPackage.Coor.unit_atom.unit_atom(atom_coor_x=ts._x[i-1],atom_coor_y=ts._y[i-1],atom_coor_z=ts._z[i-1]) solute_atoms[i]=u_atom if ts._x[i-1] < X_min: X_min = ts._x[i-1] elif ts._x[i-1] > X_max: X_max = ts._x[i-1] if ts._y[i-1] < Y_min: Y_min = ts._y[i-1] elif ts._y[i-1] > Y_max: Y_max = ts._y[i-1] if ts._z[i-1] < Z_min: Z_min = ts._z[i-1] elif ts._z[i-1] > Z_max: Z_max = ts._z[i-1] coor_x /= len(solute_list) coor_y /= len(solute_list) coor_z /= len(solute_list) for i in solute_list: _R_tmp = ( solute_atoms[i].atom_coor_x - coor_x ) **2 + \ ( solute_atoms[i].atom_coor_y - coor_y ) **2 +\ ( solute_atoms[i].atom_coor_z - coor_z ) **2 if _R_tmp > R_solute: R_solute = _R_tmp R_solute = math.sqrt(R_solute) + dmax R_solute = R_solute **2 # print R_solute # sys.exit() # print "Step 1 finished." #print "center %f\t%f\t%f" %(coor_x,coor_y,coor_z) # print X_min,X_max #step 2 #Get the range of the box. X_min = X_min - dmax Y_min = Y_min - dmax Z_min = Z_min - dmax X_max = X_max + dmax Y_max = Y_max + dmax Z_max = Z_max + dmax # print X_min,X_max # bin = dmax *2.0 / nbins bin = 1 x_bins = int((X_max - X_min) /bin) +1 y_bins = int((Y_max - Y_min) /bin) +1 z_bins = int((Z_max - Z_min) /bin) +1 #print "bin:",bin #step 4 #assign each grid to solute atoms. #grid_in_solute contains that each grid blongs to which solute atom. grids_in_which_solute =dict() solute_contain_grids =dict() # print x_bins,y_bins,z_bins _gauss_value = -1 for i in range(x_bins * y_bins * z_bins ): z_temp = i / ( x_bins * y_bins) y_temp = (i % (x_bins * y_bins)) / x_bins x_temp = i % x_bins grid_site=[X_min+(x_temp+0.5)*bin, Y_min+(y_temp+0.5)*bin, Z_min+(z_temp+0.5)*bin] #print grid_site min_dist, min_index= Min_dist(solute_atoms,solute_list,grid_site,[coor_x,coor_y,coor_z],R_solute) if min_index == 0: continue # _gauss_value = min_dist if i % 300 ==0: NOW_TIME=Time.time() BIN_TIME=NOW_TIME-START_TIME sys.stderr.write("grid ID %10d, time used: %6.2f s\r" %(i,BIN_TIME)) sys.stderr.flush() try: grids_in_which_solute[i]=[min_index,min_dist] except: print "hello to see you" try: solute_contain_grids[min_index].append(i) except: solute_contain_grids[min_index]=list() solute_contain_grids[min_index].append(i) #print solute_contain_grids # for i in solute_contain_grids: # print i,len(solute_contain_grids[i]) # sys.exit() # print "\nStep 4 finished." #step 5. #assign each solvent atom to grids. grid_in_solvent=[list() for i in range(x_bins * y_bins * z_bins)] for i in solvent_list: SV_x = ts._x[i-1] SV_y = ts._y[i-1] SV_z = ts._z[i-1] if SV_x > X_min and SV_x < X_max: x_temp = int( (SV_x - X_min) / bin ) else: continue if SV_y > Y_min and SV_y < Y_max: y_temp = int( (SV_y - Y_min) / bin ) else: continue if SV_z > Z_min and SV_z < Z_max: z_temp = int( (SV_z - Z_min) / bin ) else: continue grid_in_solvent[z_temp*x_bins*y_bins + y_temp*x_bins + x_temp].append(i) # print "append solvent %d" %i # print "Step 5 finished." # step 6. #calculating the g(r) for each solute atom. density = MDAnalysis.core.units.convert(1.0, 'water', 'Angstrom^{-3}')*math.pow(10,3) unit_conc = 1/ ((bin)**3 * density) #unit solvent atom density. temp1 =list() #A list used to contain grid_dist. temp2 =list() #A list used to contain sol number for each grad. TOTAL_ATOMS = 0 for i in solute_list: try: temp_grids=solute_contain_grids[i] #print "solute %d, grids number %d" %(i,len(temp_grids)) except: continue #rdf_atom=[0 for i in range(50)] # bin =float(dmax)/nbins for grid in temp_grids: sol_number=len(grid_in_solvent[grid]) # if sol_number ==0: # continue #print " %d" %sol_number, # try: blabla,dist=grids_in_which_solute[grid] # except: # continue temp1.append(dist) temp2.append(sol_number) # if len(temp1) == 0: # continue # print temp1 # print temp2 #if i == 10: # sys.exit() rdf_atom=hist(temp1, temp2, 0.0, dmax, 100) # print rdf_atom # print unit_conc #print rdf_atom if sum(rdf_atom) > 0: TOTAL_ATOMS += 1 rdf_atom=numpy.array(rdf_atom) / unit_conc # sys.exit() GR += rdf_atom # print GR plt.clf() ax = plt.gca() ax.set_xlabel("Distance (nm)") ax.set_ylabel("pRDF(r)") x_label=[i*dmax/1000. for i in range(100)] y_label=[GR[i]/ts.frame for i in range(100)] ax.plot(x_label,y_label,'b',) plt.draw() temp_filename="temp"+"%d"%(ts.frame)+".png" plt.savefig(temp_filename) for i in range(100): Data_file.write("%12.4f" %y_label[i]) Data_file.write("\n") GR = GR / TOTAL_ATOMS / U.trajectory.numframes Data_file.close() # print TOTAL_ATOMS # print len(solute_index) for i in range(32): print "%f\t%f" %(2.0/32*(i+0.5),GR[i])
def test_Index_Read_index_to_Inclass(): global idx_file in_list = Index.Read_index_to_Inclass(idx_file) Index.Print_Index(in_list)
elif a == "-f": traj_file = b elif a == "-n": ndx_file = b elif a == "-o": rmsd_file = b elif a == "-i": input_file = b elif a == "--skip": skip = int(b) elif a == "-h": Usage() sys.exit() try: index_list = Index.Read_index_to_Inclass(ndx_file) except: print "Error in reading index file." print "Using -h to see usage." sys.exit() Index.Print_Index(index_list) group_ID = raw_input("Choose a group: ") try: atom_list = index_list[int(group_ID)].group_list except: print "error" sys.exit() if os.path.isfile(input_file): coor_list = list() traj_list = list()
def pRDF(traj_file, coor_file, index_file, solute_index, dmax=30, nbins=32): ''' A simple pRDF test here. ''' START_TIME = Time.time() HAS_TRAJFILE = False if os.path.isfile(traj_file): HAS_TRAJFILE = True atom_list = Simple_atom.Get_atom_list(coor_file) index_list = Index.Read_index_to_Inclass(index_file) solute_list = index_list[solute_index].group_list solvent_list = Get_solvent_list(atom_list) if HAS_TRAJFILE: U = MDAnalysis.Universe(coor_file, traj_file) else: U = MDAnalysis.Universe(coor_file) GR = numpy.zeros((100), dtype=numpy.float64) Data_file = open("datafile.xvg", 'w') #step 1 # Get the center of the solute. for ts in U.trajectory: print "Checking frame number: %d" % (ts.frame) solute_atoms = dict() coor_x = 0.0 coor_y = 0.0 coor_z = 0.0 for i in solute_list: coor_x += ts._x[i - 1] coor_y += ts._y[i - 1] coor_z += ts._z[i - 1] u_atom = MDPackage.Coor.unit_atom.unit_atom( atom_coor_x=ts._x[i - 1], atom_coor_y=ts._y[i - 1], atom_coor_z=ts._z[i - 1]) solute_atoms[i] = u_atom coor_x /= len(solute_list) coor_y /= len(solute_list) coor_z /= len(solute_list) print "Step 1 finished." #print "center %f\t%f\t%f" %(coor_x,coor_y,coor_z) #step 2 #Get the range of the box. X_min = coor_x - dmax Y_min = coor_y - dmax Z_min = coor_z - dmax X_max = coor_x + dmax Y_max = coor_y + dmax Z_max = coor_z + dmax bin = dmax * 2.0 / nbins #print "bin:",bin print "Step 2 finished." #step 3 # Get the grids #grid_number=z*nbins*nbins + y*nbins + x print "Step 3 finished." #step 4 #assign each grid to solute atoms. #grid_in_solute contains that each grid blongs to which solute atom. ###############begin this new algorithm################################# # grid_map = numpy.zeros([nbins**3,2],dtype=numpy.int) # occpiad_dict=dict() # for so in solute_list: # x_temp = int((solute_atoms[so].atom_coor_x - X_min)/bin+0.5) # y_temp = int((solute_atoms[so].atom_coor_y - Y_min)/bin+0.5) # z_temp = int((solute_atoms[so].atom_coor_z - Z_min)/bin+0.5) # ind= z_temp*nbins*nbins + y_temp*nbins + x_temp # if grid_map[ind,0] !=0: # # print "conflict. %d" %so # pass # else: # grid_map[ind,0] =so # occpiad_dict[ind] =1 # #print "map %d to grid id %d" %(so,ind) # cycle = 0 # while True: # occpiad_dict=dict() # for i in range(nbins**3): # if grid_map[i,0] != 0: # occpiad_dict[i]=1 # #new_dict = list() # print "cycle %d, len(occ) = %d" %(cycle,len(occpiad_dict)) # if len(occpiad_dict) > nbins **3 -1: # break # for i in occpiad_dict: # z_temp = i / nbins**2 # y_temp = (i % nbins**2) / nbins # x_temp = i % nbins # for step_x in range(-1,2): # for step_y in range(-1,2): # for step_z in range(-1,2): # if abs(step_x)+abs(step_y)+abs(step_z)==0: # continue # if (step_x+x_temp > nbins-1) or (step_x+x_temp <0): # continue # if (step_y+y_temp > nbins-1) or (step_y+y_temp <0): # continue # if (step_z+z_temp > nbins-1) or (step_z+z_temp <0): # continue # new_value=(z_temp+step_z)*nbins*nbins + (y_temp+step_y)*nbins + (x_temp+step_x) # if grid_map[new_value,0]== 0: # grid_map[new_value,0] = grid_map[i,0] # # new_dict.append(new_value) # else: # grid_site=[X_min+(x_temp+step_x+0.5)*bin,\ # Y_min+(y_temp+ step_y + 0.5)*bin, \ # Z_min+(z_temp+ step_z + 0.5)*bin] # old_atom =solute_atoms[grid_map[i,0]] # new_atom =solute_atoms[grid_map[new_value,0]] # old_vect =[old_atom.atom_coor_x,old_atom.atom_coor_y,old_atom.atom_coor_z] # new_vect =[new_atom.atom_coor_x,new_atom.atom_coor_y,new_atom.atom_coor_z] # dist1 = Dist_Square(grid_site,old_vect) # dist2 = Dist_Square(grid_site,new_vect) # # print dist1, dist2 # if dist1 < dist2: # grid_map[new_value,0] = grid_map[i,0] # # new_dict.append(new_value) # # grid_map[i]=0 # if len(occpiad_dict) < nbins **3 : # # for i in occpiad_dict: # # grid_map[i,0]=0 # # grid_map[i,1]=0 # del occpiad_dict # cycle += 1 # # print "cycle %d, len(occ) = %d" %(cycle,len(new_dict)) # # print occpiad_dict # # # # sys.exit() # solute_contain_grids =dict() # for i in range(nbins**3): # try: # solute_contain_grids[grid_map[i,0]].append(i) # except: # solute_contain_grids[grid_map[i,0]]=list() # solute_contain_grids[grid_map[i,0]].append(i) # z_temp = i / nbins**2 # y_temp = (i % nbins**2) / nbins # x_temp = i % nbins # grid_site=[X_min+(x_temp+0.5)*bin,\ # Y_min+(y_temp+ 0.5)*bin, \ # Z_min+(z_temp+ 0.5)*bin] # old_atom =solute_atoms[grid_map[i,0]] # old_vect =[old_atom.atom_coor_x,old_atom.atom_coor_y,old_atom.atom_coor_z] # dist1 = Dist_Square(grid_site,old_vect) # grid_map[i,1]=math.sqrt(dist1) # grids_in_which_solute=grid_map # for i in solute_contain_grids: # print i,len(solute_contain_grids[i]) # sys.exit() ###############end this algorithm###################################### grids_in_which_solute = list() solute_contain_grids = dict() for i in range(nbins**3): z_temp = i / nbins**2 y_temp = (i % nbins**2) / nbins x_temp = i % nbins grid_site = [ X_min + (x_temp + 0.5) * bin, Y_min + (y_temp + 0.5) * bin, Z_min + (z_temp + 0.5) * bin ] #print grid_site min_dist, min_index = Min_dist(solute_atoms, solute_list, grid_site) if i % 3000 == 0: NOW_TIME = Time.time() BIN_TIME = NOW_TIME - START_TIME sys.stderr.write("grid ID %10d, time used: %6.2f s\r" % (i, BIN_TIME)) sys.stderr.flush() grids_in_which_solute.append([min_index, min_dist]) try: solute_contain_grids[min_index].append(i) except: solute_contain_grids[min_index] = list() solute_contain_grids[min_index].append(i) #print solute_contain_grids # for i in solute_contain_grids: # print i,len(solute_contain_grids[i]) # sys.exit() print "\nStep 4 finished." #step 5. #assign each solvent atom to grids. grid_in_solvent = [list() for i in range(nbins**3)] for i in solvent_list: SV_x = ts._x[i - 1] SV_y = ts._y[i - 1] SV_z = ts._z[i - 1] if SV_x > X_min and SV_x < X_max: x_temp = int((SV_x - X_min) / bin) else: continue if SV_y > Y_min and SV_y < Y_max: y_temp = int((SV_y - Y_min) / bin) else: continue if SV_z > Z_min and SV_z < Z_max: z_temp = int((SV_z - Z_min) / bin) else: continue grid_in_solvent[z_temp * nbins * nbins + y_temp * nbins + x_temp].append(i) # print "append solvent %d" %i print "Step 5 finished." # step 6. #calculating the g(r) for each solute atom. TOTAL_ATOMS = 0 for i in solute_list: try: temp_grids = solute_contain_grids[i] #print "solute %d, grids number %d" %(i,len(temp_grids)) except: continue #rdf_atom=[0 for i in range(50)] bin = float(dmax) / nbins density = MDAnalysis.core.units.convert( 1.0, 'water', 'Angstrom^{-3}') * math.pow(10, 3) unit_conc = 1 / ((float(dmax) * 2 / nbins)**3 * density ) #unit solvent atom density. temp1 = list() #A list used to contain grid_dist. temp2 = list() #A list used to contain sol number for each grad. for grid in temp_grids: sol_number = len(grid_in_solvent[grid]) #print " %d" %sol_number, blabla, dist = grids_in_which_solute[grid] temp1.append(dist) temp2.append(sol_number) #print temp1 #print temp2 #if i == 10: # sys.exit() rdf_atom = hist(temp1, temp2, 0.0, 12.0, 100) #print rdf_atom if sum(rdf_atom) > 0: TOTAL_ATOMS += 1 rdf_atom = numpy.array(rdf_atom) * unit_conc GR += rdf_atom # print GR plt.clf() ax = plt.gca() ax.set_xlabel("Distance (nm)") ax.set_ylabel("pRDF(r)") x_label = [i / 50.0 for i in range(100)] y_label = [GR[i] / ts.frame for i in range(100)] ax.plot( x_label, y_label, 'b', ) plt.draw() temp_filename = "temp" + "%d" % (ts.frame) + ".png" plt.savefig(temp_filename) for i in range(100): Data_file.write("%12.4f" % y_label[i]) Data_file.write("\n") GR = GR / TOTAL_ATOMS / U.trajectory.numframes Data_file.close() # print TOTAL_ATOMS # print len(solute_index) for i in range(32): print "%f\t%f" % (2.0 / 32 * (i + 0.5), GR[i])
def pRDF(top_file, trj_file, index_file, trjout_file, solute_index, WAT_NUMBER=500): ''' Save the WAT_NUMBER closest water molecules to a new trajectory file. ''' START_TIME = Time.time() Atoms = Simple_atom.Get_atom_list(top_file) atom_list = copy.deepcopy(Atoms) index_list = Index.Read_index_to_Inclass(index_file) solute_atom_list = index_list[solute_index].group_list solvent_list = Get_solvent_list(atom_list) if len(solvent_list) < WAT_NUMBER: print "Error: The number of water molecules (%d) is less than the critical number (%d)." % ( len(solvent_list), WAT_NUMBER) natoms = libxdrfile.read_xtc_natoms(trj_file) x = numpy.zeros((natoms, libxdrfile.DIM), dtype=numpy.float32) box = numpy.zeros((libxdrfile.DIM, libxdrfile.DIM), dtype=numpy.float32) XTC = libxdrfile.xdrfile_open(trj_file, 'r') OUTPUT_ATOMS = len(solute_atom_list) + 3 * WAT_NUMBER XTC_write = libxdrfile.xdrfile_open(trjout_file, 'w') x_out = numpy.zeros((OUTPUT_ATOMS, libxdrfile.DIM), dtype=numpy.float32) # loop through file until return status signifies end or a problem # (it should become exdrENDOFFILE on the last iteration) status = libxdrfile.exdrOK FIRST_FRAME = True solu_list = list() while status == libxdrfile.exdrOK: status, step, time, prec = libxdrfile.read_xtc(XTC, box, x) # do something with x sol_dict = dict() sol_dist = list() ############### for i in atom_list: u_atom = MDPackage.Coor.unit_atom.unit_atom(atom_coor_x=x[i - 1, 0], atom_coor_y=x[i - 1, 1], atom_coor_z=x[i - 1, 2]) atom_list[i] = u_atom ############### if FIRST_FRAME == True: # FIRST_FRAME = False for solvent in solvent_list: # usage.echo("Checking solvent atom: %d\r" %(solvent)) min_dist, solu_temp = Min_dist(atom_list, solute_atom_list, solvent) sol_dist.append(min_dist) sol_dict[min_dist] = solvent solu_list.append(solu_temp) solu_list = list(set(solu_list)) else: for solvent in solvent_list: min_dist, solu_temp = Min_dist(atom_list, solu_list, solvent) sol_dist.append(min_dist) sol_dict[min_dist] = solvent sorted_sol_dist = sorted(sol_dist) STANDARD = sorted_sol_dist[WAT_NUMBER + 1] for i in range(len(solute_atom_list)): x_out[i, 0] = x[solute_atom_list[i] - 1, 0] x_out[i, 1] = x[solute_atom_list[i] - 1, 1] x_out[i, 2] = x[solute_atom_list[i] - 1, 2] for i in range(WAT_NUMBER): for j in range(3): x_out[len(solute_atom_list) + 3 * i + j, 0] = x[sol_dict[sorted_sol_dist[i]] - 1 + j, 0] x_out[len(solute_atom_list) + 3 * i + j, 1] = x[sol_dict[sorted_sol_dist[i]] - 1 + j, 1] x_out[len(solute_atom_list) + 3 * i + j, 2] = x[sol_dict[sorted_sol_dist[i]] - 1 + j, 2] status2 = libxdrfile.write_xtc(XTC_write, step, time, box, x_out, prec) if FIRST_FRAME == True: new_list = [Atoms[i] for i in solute_atom_list] for i in range(WAT_NUMBER): new_list.append(Atoms[sol_dict[sorted_sol_dist[i]]]) new_list.append(Atoms[sol_dict[sorted_sol_dist[i]] + 1]) new_list.append(Atoms[sol_dict[sorted_sol_dist[i]] + 2]) out_file = string.split(trjout_file, '.')[0] + ".pdb" Simple_atom.Save_file(out_file, new_list) FIRST_FRAME = False NOW_TIME = Time.time() BIN_TIME = NOW_TIME - START_TIME if step % 1000 == 0: sys.stderr.write( "step %10d, time %10.2f ps, time used: %6.2f s\r" % (step, time, BIN_TIME)) sys.stderr.flush() # finally close file print "" libxdrfile.xdrfile_close(XTC) libxdrfile.xdrfile_close(XTC_write)
def QH_entro(top_file, traj_file, index_file, temperature=300, skip=1, begin=0, end=-1, WRITE_EIGENVECTOR=False): ''' Add some words here. ''' #step 1: reading the trajectory. BEGIN_TIME = Time.time() print "step 1: Reading trajectory" U = MDAnalysis.Universe(top_file, traj_file) index = Index.Read_index_to_Inclass(index_file) Index.Print_Index(index) while True: try: solute_index = int( raw_input("Choose the group for entropy calculation:")) break except: print "You should input a number." continue chose_group = index[solute_index].group_list if end == -1: nframes = U.trajectory.numframes - begin elif end > begin: nframes = end - begin else: print "The begin and the end of the frame seem not correct." sys.exit() natoms = len(chose_group) print "\t Reading %d frames from trajectory file: %s" % (nframes, traj_file) #step 2: put data to traj_data matrix. get eigenvalues and eigenvectors. traj_data = np.zeros((nframes, natoms, 3), dtype=np.float32) traj_data2 = np.zeros((nframes, 3 * natoms), dtype=np.float32) traj_ave = np.zeros((3 * natoms), dtype=np.float32) covar_mat = np.zeros((3 * natoms, 3 * natoms), dtype=np.float32) sqrt_mass = [ math.sqrt(U.atoms[i].mass) for i in range(U.trajectory.numatoms) ] POINT = 0 ## POINT used to replace the U.trajectory.frame in this part. for ts in U.trajectory: # temp_vect = np.zeros(3*natoms,dtype=np.float32) POINT += 1 if POINT > begin and (POINT - begin) < nframes + 1: pass elif POINT < begin: continue elif POINT > end and end > 0: break else: print "Note: reach here. begin=%6d,end=%6d,POINT=%6d" % ( begin, end, POINT) sys.exit() sys.stderr.write("\t Reading frame %8d\r" % POINT) sys.stderr.flush() for i, ai in list(enumerate(chose_group)): traj_data[POINT - begin - 1, i, 0] = ts._x[ai - 1] * sqrt_mass[ai - 1] traj_data[POINT - begin - 1, i, 1] = ts._y[ai - 1] * sqrt_mass[ai - 1] traj_data[POINT - begin - 1, i, 2] = ts._z[ai - 1] * sqrt_mass[ai - 1] ######################## #Add fitting code here. from matrix traj_data to a new matrix. print "\nstep 2: Fitting the trajectory." ref_coor = traj_data[0, :, :] for k in range(natoms): for l in range(3): traj_data2[0, 3 * k + l] = ref_coor[k, l] ref_com = np.array([ sum(ref_coor[:, 0]) / nframes, sum(ref_coor[:, 1]) / nframes, sum(ref_coor[:, 2]) / nframes ]) #ref_com means center of coordinate the reference. ref_coordinates = ref_coor - ref_com # traj_coordinates = traj_atoms.coordinates().copy() # nframes = len(frames) rmsd = np.zeros((nframes, )) rot = np.zeros(9, dtype=np.float64) # allocate space for calculation R = np.matrix(rot.reshape(3, 3)) for k in range(1, nframes): # shift coordinates for rotation fitting # selection is updated with the time frame sys.stderr.write("\t Fitting frame %8d\r" % (k + 1)) sys.stderr.flush() traj_coor = traj_data[k, :, :] x_com = np.array([ sum(traj_coor[:, 0]) / nframes, sum(traj_coor[:, 1]) / nframes, sum(traj_coor[:, 2]) / nframes ]) traj_coordinates = traj_coor - x_com rmsd[k] = qcp.CalcRMSDRotationalMatrix( ref_coordinates.T.astype(np.float64), traj_coordinates.T.astype(np.float64), natoms, rot, None) R[:, :] = rot.reshape(3, 3) # Transform each atom in the trajectory (use inplace ops to avoid copying arrays) # (Marginally (~3%) faster than "ts._pos[:] = (ts._pos - x_com) * R + ref_com".) # ts._pos -= x_com # ts._pos[:] = ts._pos * R # R acts to the left & is broadcasted N times. # ts._pos += ref_com new_coor = np.array( np.matrix(traj_coordinates) * np.matrix(R)) + ref_com for i in range(natoms): for j in range(3): traj_data2[k, 3 * i + j] = ref_coor[i, j] del traj_data ######################## print "\nstep 3: Creating covariance matrix" print "\t Generazing the (%d X %d) covariance matrix" % (3 * natoms, 3 * natoms) # traj_data[ts.frame-1]=temp_vect for i in range(nframes): traj_ave += traj_data2[i] traj_ave = traj_ave / nframes for i in range(nframes): delta_vect = [ traj_data2[i][j] - traj_ave[j] for j in range(3 * natoms) ] covar_mat += np.array(np.matrix(delta_vect).T * np.matrix(delta_vect)) # covar_mat = covar_mat / ( nframes -1 ) # be careful! covar_mat = covar_mat / nframes TIME1 = Time.time() print "\t Time used %6.2f s" % (TIME1 - BEGIN_TIME) print "step 4: Diagnoalizing the covariance matrix" eigenvalues, eigenvectors = np.linalg.eig(covar_mat) print eigenvalues TIME2 = Time.time() print "\t Time used %6.2f s" % (TIME2 - TIME1) # sys.exit() #step 3: get the number of classical mode eigenvalues. # eigenvalues=sorted(eigenvalues,reverse=True) # np.savetxt("eigen.xvg",eigenvalues) eigen_thr = 1e-6 truncate_num = 0 for i in range(3 * natoms): if eigenvalues[i] < eigen_thr: truncate_num = i break elif eigenvalues[-1] > eigen_thr: truncate_num = 3 * natoms print "\t Using %d eigenvalues to calculate entropy." % truncate_num print "step 5: Calculating the quasiharmonic entropy" global HBA global K global MP # global R alpha_const = HBA * (1e10) / math.sqrt(K * temperature * MP) eigval_class = eigenvalues[:truncate_num] eigvec_class = eigenvectors[:truncate_num] alpha = [ alpha_const / math.sqrt(eigval_class[i]) for i in range(truncate_num) ] s_quantum = [ alpha[i] / (math.exp(alpha[i]) - 1) - math.log(1 - math.exp(-alpha[i])) for i in range(truncate_num) ] print s_quantum total_entropy = sum(s_quantum) * R print "\t Entopy: %8.2f J/mol/K, %8.2f Kcal/mol." % ( total_entropy, total_entropy / 4186.8 * temperature)