|
| 1 | +''' |
| 2 | +Author: Pranav Khade (pranavk@iastate.edu) |
| 3 | +''' |
| 4 | + |
| 5 | +import numpy |
| 6 | + |
| 7 | + |
| 8 | +''' |
| 9 | +################################################################################################## |
| 10 | +# ANM # |
| 11 | +################################################################################################## |
| 12 | +''' |
| 13 | + |
| 14 | +class ANM: |
| 15 | + def __init__(self, coords , gamma=1.0, dr=15.0, power=0, pf=None): |
| 16 | + """ |
| 17 | + Author: Pranav Khade |
| 18 | + Part Credits: Ambuj Kumar (ambuj@iastate.edu) |
| 19 | + """ |
| 20 | + self.gamma = gamma |
| 21 | + self.dr = dr |
| 22 | + self.power = power |
| 23 | + self.pf = pf |
| 24 | + self.coords = numpy.array(coords) |
| 25 | + if self.pf != None and self.pf <= 0: |
| 26 | + raise Exception("pf value cannot be zero or negative") |
| 27 | + if self.gamma <= 0: |
| 28 | + raise Exception("gamma value cannot be zero or negative") |
| 29 | + if self.dr <= 0: |
| 30 | + raise Exception("distance cutoff value cannot be zero or negative") |
| 31 | + |
| 32 | + self.fluctuations = None |
| 33 | + self.stiffness_map = None |
| 34 | + self.compliance_map = None |
| 35 | + self.stiffness_profile = None |
| 36 | + self.compliance_profile = None |
| 37 | + |
| 38 | + |
| 39 | + '''Get Functions''' |
| 40 | + def get_hessian(self): |
| 41 | + return self.hessian |
| 42 | + |
| 43 | + def get_eigenvalues(self): |
| 44 | + return self.eigen_values |
| 45 | + |
| 46 | + def get_eigenvectors(self): |
| 47 | + return self.eigen_vectors |
| 48 | + |
| 49 | + def get_fluctuations(self): |
| 50 | + return self.fluctuations |
| 51 | + |
| 52 | + def get_stiffness_map(self): |
| 53 | + return self.stiffness_map |
| 54 | + |
| 55 | + def get_compliance_map(self): |
| 56 | + return self.compliance_map |
| 57 | + |
| 58 | + def get_stiffness_profile(self): |
| 59 | + return self.stiffness_profile |
| 60 | + |
| 61 | + def get_compliance_profile(self): |
| 62 | + return self.compliance_profile |
| 63 | + |
| 64 | + |
| 65 | + '''Calculate Functions''' |
| 66 | + def calculate_hessian(self): |
| 67 | + n_atoms=len(self.coords) |
| 68 | + hessian=numpy.zeros((n_atoms*3, n_atoms*3), float) |
| 69 | + distance_mat=numpy.ones((n_atoms*3, n_atoms*3), float) |
| 70 | + for i in range(len(self.coords)): |
| 71 | + diff = self.coords[i+1:, :] - self.coords[i] |
| 72 | + squared_diff = diff**2 |
| 73 | + for j, s_ij in enumerate(squared_diff.sum(1)): |
| 74 | + if s_ij <= self.dr**2: |
| 75 | + diff_coords = diff[j] |
| 76 | + j = j + i + 1 |
| 77 | + derivative = numpy.outer(diff_coords, diff_coords)*(float(-self.gamma)/numpy.sqrt(s_ij)**(2+self.power)) |
| 78 | + hessian[i*3:i*3+3, j*3:j*3+3] = derivative |
| 79 | + hessian[j*3:j*3+3, i*3:i*3+3] = derivative |
| 80 | + hessian[i*3:i*3+3, i*3:i*3+3] = hessian[i*3:i*3+3, i*3:i*3+3] - derivative |
| 81 | + hessian[j*3:j*3+3, j*3:j*3+3] = hessian[j*3:j*3+3, j*3:j*3+3] - derivative |
| 82 | + #abs added to avoid negative numbers |
| 83 | + d = numpy.sqrt(s_ij) |
| 84 | + lobj = [[d,d,d],[d,d,d], [d,d,d]] |
| 85 | + dmat = numpy.array(lobj) |
| 86 | + distance_mat[i*3:i*3+3, j*3:j*3+3] = dmat |
| 87 | + |
| 88 | + if self.pf != None: |
| 89 | + hessian = numpy.divide(hessian, distance_mat) |
| 90 | + |
| 91 | + self.hessian=hessian |
| 92 | + return True |
| 93 | + |
| 94 | + def calculate_decomposition(self): |
| 95 | + ''' |
| 96 | + ''' |
| 97 | + self.eigen_values,self.eigen_vectors=numpy.linalg.eigh(self.hessian) |
| 98 | + return True |
| 99 | + |
| 100 | + def calculate_fluctuations(self,endmode=None): |
| 101 | + ''' |
| 102 | + ''' |
| 103 | + EVec=self.eigen_vectors.T |
| 104 | + mode_bfactors=[] |
| 105 | + for numi,i in enumerate(self.eigen_values[6:]): |
| 106 | + evec_row=EVec[numi+6] |
| 107 | + mode_bfactors.append([ float(evec_row[j]**2 + evec_row[j+1]**2 + evec_row[j+2]**2)/i for j in range(0,len(self.eigen_values),3)]) |
| 108 | + |
| 109 | + mode_bfactors=numpy.array(mode_bfactors) |
| 110 | + self.fluctuations=[numpy.sum(i) for i in mode_bfactors.T] |
| 111 | + return True |
| 112 | + |
| 113 | + |
| 114 | + def calculate_stiffness_compliance(self): |
| 115 | + ''' |
| 116 | + ''' |
| 117 | + EVec=self.eigen_vectors.T |
| 118 | + hessian_inv= numpy.matmul( numpy.matmul( EVec[6:].transpose() , numpy.diag(1/self.get_eigenvalues()[6:]) ) , EVec[6:] ) |
| 119 | + compliance_map=numpy.zeros((len(self.coords),len(self.coords))) |
| 120 | + stiffness_map=numpy.zeros((len(self.coords),len(self.coords))) |
| 121 | + |
| 122 | + for numi,i in enumerate(self.coords): |
| 123 | + for numj,j in enumerate(self.coords): |
| 124 | + if(numi!=numj): |
| 125 | + FMAT=numpy.zeros((len(self.coords),3)) |
| 126 | + Fij=(j-i)/numpy.linalg.norm(i-j) |
| 127 | + FMAT[numi]=-Fij |
| 128 | + FMAT[numj]=Fij |
| 129 | + FMAT=FMAT.flatten() |
| 130 | + |
| 131 | + d=numpy.matmul(hessian_inv,FMAT) |
| 132 | + |
| 133 | + compliance= (d[(numj*3)+0]-d[(numi*3)+0])*Fij[0] + (d[(numj*3)+1]-d[(numi*3)+1])*Fij[1] + (d[(numj*3)+2]-d[(numi*3)+2])*Fij[2] |
| 134 | + stiffness= 1/ compliance |
| 135 | + |
| 136 | + compliance_map[numi][numj]= compliance |
| 137 | + stiffness_map[numi][numj] = stiffness |
| 138 | + |
| 139 | + self.stiffness_map = stiffness_map |
| 140 | + self.compliance_map = compliance_map |
| 141 | + self.stiffness_profile = [numpy.nanmean(i) for i in stiffness_map] |
| 142 | + self.compliance_profile = [numpy.nanmean(i) for i in compliance_map] |
| 143 | + return True |
0 commit comments