Advertisement
Guest User

Untitled

a guest
Apr 13th, 2025
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.63 KB | None | 0 0
  1. import sys as s
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4.  
  5.  
  6. #import torch # Getting some weird memory errors on my PC that are fixed with this import
  7.               # not actually used in code.
  8.  
  9.  
  10. #============================================================================#
  11. # Variables
  12. #============================================================================#
  13. diameter = 10.0
  14. number_windows = 39
  15.  
  16. #============================================================================#
  17. # Function to read output from wham
  18. #============================================================================#
  19.  
  20. def wham_read(filename, number_windows, diameter):
  21.     distance = np.zeros(number_windows)
  22.     PMF = np.zeros(number_windows)
  23.    
  24.     # Read in PMF from wham file
  25.     file = open(filename,'r')
  26.     line = file.readline()
  27.     for idx in range(number_windows):
  28.         line = file.readline()
  29.         spl = line.split()
  30.         distance[idx] = float(spl[0])
  31.         PMF[idx] = float(spl[1])   
  32.     file.close()
  33.     # Zero PMF at large distances
  34.     PMF = PMF - PMF[-1]
  35.    
  36.     # Scale distance by diameter
  37.     distance = distance/diameter
  38.  
  39.     # Compute force, -grad PMF, note that here Delta D is always constant
  40.     force = -np.gradient(PMF,distance[1]-distance[0])
  41.  
  42.     return distance, PMF, force
  43.  
  44. #============================================================================#
  45. # Function to extrapolate small distances
  46. #============================================================================#
  47.  
  48. def polynomial_extra(n_degree, n_points, min_distance, extra_points
  49.                              ,distance, PMF, force):
  50.    
  51.     # Use first n_points to compute a polynomial of n_degree
  52.  
  53.     constant = np.polyfit(distance[1:n_points], PMF[1:n_points], n_degree)
  54.  
  55.     new_dist = np.linspace(min_distance,distance[2],
  56.                              num=extra_points,endpoint=False)
  57.  
  58.     new_PMF = np.zeros(extra_points)
  59.     new_force = np.zeros(extra_points)
  60.    
  61.     for idx in range(n_degree + 1):
  62.         new_PMF = new_PMF + constant[idx]*new_dist**(n_degree-idx)
  63.         if idx != n_degree:
  64.             new_force = (new_force -
  65.                 (n_degree - idx)*constant[idx]*new_dist**(n_degree-idx-1))
  66.    
  67.     # Add extrapolation to initial data
  68.     extra_distance = np.concatenate((new_dist, distance[2:]))
  69.     extra_PMF = np.concatenate((new_PMF, PMF[2:]))
  70.     extra_force = np.concatenate((new_force, force[2:]))
  71.  
  72.     return extra_distance, extra_PMF, extra_force
  73.  
  74. #============================================================================#
  75. # Function to output potential file for LAMMPS
  76. #============================================================================#
  77.  
  78. def output_potential(name, type, distance, PMF, force):
  79.     file = open(name,'w')
  80.    
  81.     file.write('# DATE: 2021-11-17 UNTIS: lj CONTRIBUTOR: Luis Nieves\n')
  82.     file.write('# PMF Potential \n')
  83.     file.write('\n')
  84.     file.write(type + '\n')
  85.     file.write('N ' + str(len(distance)) + '\n')
  86.     file.write('\n')
  87.     for idx in range(len(distance)):
  88.         file.write(str(idx + 1) + ' ' + str(distance[idx]) + ' '
  89.                    + str(PMF[idx]) + ' ' + str(force[idx]) + '\n') 
  90.     file.close()    
  91.  
  92. #============================================================================#
  93. # Process AA, AB and BB data
  94. #============================================================================#
  95.  
  96. # Create figure of PMF data, add plots as data is processes
  97. plt.figure()
  98.  
  99. for type in ['AA','AB','BB']:
  100.     filename = type + '.out'
  101.    
  102.     # Read data from wham results    
  103.     distance, PMF, force = wham_read(filename, number_windows, diameter)
  104.  
  105.     # Add polynomial extrapolation to results
  106.     # n_degree = 2 best from experimentation
  107.     extra_distance, extra_PMF, extra_force = \
  108.             polynomial_extra(2, 10, 0.00001, 10, distance, PMF, force)
  109.                                      
  110.    
  111.  
  112.     # Output potential to file, both original and extrapolated 
  113.     output_potential(type + '.pot', type, distance, PMF, force)
  114.     output_potential(type + 'P.pot', type, extra_distance,
  115.                      extra_PMF, extra_force)
  116.  
  117.     # Add plot to figure
  118.     if type == 'AA':
  119.         plot_color = 'r'   
  120.     elif type == 'AB':
  121.         plot_color = 'g'       
  122.     elif type == 'BB':
  123.         plot_color = 'b'       
  124.        
  125.     plt.plot(distance, PMF, label = type, linewidth = 3,
  126.             color=plot_color, marker = 'o')
  127.  
  128.  
  129.  
  130. # Finish plot and output
  131. plt.axhline(y=0.0, linestyle='--', color='k')
  132. plt.axvline(x=1.0, linestyle='--', color='k')
  133. plt.ylim([-5,10])
  134. plt.legend()
  135. plt.ylabel('Potential of Mean Force')
  136. plt.xlabel('R/Diameter')
  137. plt.savefig('PMF.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement