-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl_p2.py
133 lines (113 loc) · 3.97 KB
/
l_p2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import numpy as np
import MDAnalysis as mda
from MDAnalysis.analysis import polymer
import matplotlib.pyplot as plt
import scipy
import statsmodels.api as sm
#from cmeutils.sampling import autocorr1D
def autocorr1D(array):
"""Takes in a linear np array, performs autocorrelation
function and returns normalized array with half the length
of the input.
Parameters
----------
data : numpy.typing.Arraylike, required
1-D series of data to perform autocorrelation on.
Returns
-------
1D np.array
"""
ft = np.fft.rfft(array - np.average(array))
acorr = np.fft.irfft(ft * np.conjugate(ft)) / (len(array) * np.var(array))
return acorr[0 : len(acorr) // 2] # noqa: E203
def get_decorr(acorr):
"""
Returns the decorrelation time of the autocorrelation, a 1D np array
"""
return np.argmin(acorr > 0)
def persistence_length(filepath,start,stop,interval):
"""
filepath needs to be a format in which you can
create an mdanalysis universe from, we mostly use gsd files
"""
u = mda.Universe(topology=filepath)
"""rewrite atom indices"""
bond_indices = []
particle_index = [0]
for i in range(len(u.bonds)):
a = u.bonds[i].atoms.indices
bond_indices.append(list(a))
if particle_index[-1] in bond_indices[i]:
atom1 = bond_indices[i][0]
atom2 = bond_indices[i][1]
if atom1 not in particle_index:
particle_index.append(atom1)
if atom2 not in particle_index:
particle_index.append(atom2)
# print(bond_indices)
"""create bonds list"""
autocorrelation = []
bond_len = []
for t in u.trajectory[start:stop:interval]:
particle_positions = []
bonds = []
unit_bonds = []
bond_lengths = []
angles = []
for i in particle_index:
pos = t.positions[i]
particle_positions.append(pos)
for i in range(len(u.bonds)):
b = particle_positions[i+1]-particle_positions[i]
bonds.append(b)
l2 = t.dimensions[0]/2
for i,b in enumerate(bonds):
for j,x in enumerate(b):
if x>l2:
bonds[i][j] = x-l2*2
if x<-l2:
bonds[i][j] = x+l2*2
a = b/np.linalg.norm(b)
unit_bonds.append(a)
length = np.linalg.norm(b)
bond_lengths.append(length)
#l_b = np.mean(bond_lengths)
bond_len.append(bond_lengths)
# angles.append(np.dot(unit_bonds,unit_bonds))
for i in range(len(unit_bonds)-1):
b1 = unit_bonds[0]
b2 = unit_bonds[0+i]
dot_product = np.dot(b1,b2)
angles.append(dot_product)
n=len(u.bonds)
n_frames = u.trajectory.n_frames
n_chains = 1
norm = np.linspace(1,n- 1, n - 1)
norm *= n_chains# * n_frames
#auto = autocorr1D(angles)
#auto = sm.tsa.acf(angles)
#autocorrelation.append(auto)
autocorrelation.append(angles)#/norm)
# print(angles)
'''average the data from trajectories together'''
auto_average = []
for j in range(len(autocorrelation[0])):
k = []
for i in range(len(autocorrelation)):
k.append(autocorrelation[i][j])
auto_average.append(np.mean(k))
# print(auto_average)
l_b = np.average(bond_len)
# result = [x/len(av) for x in sums]
x = [i for i in range(len(auto_average))]
'''set negative results to 0'''
for r in range(len(auto_average)):
if auto_average[r] < 0:
auto_average[r] = 0
def expfunc(x, a):
return np.exp(-x/a)
exp_coeff = scipy.optimize.curve_fit(expfunc,x,auto_average)[0][0]
l_p = exp_coeff * l_b
fit = np.exp(-(x/exp_coeff))
decorrelation = get_decorr(np.array(auto_average))
return l_p, l_b, x, auto_average, fit, exp_coeff, autocorrelation, decorrelation, unit_bonds