-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyse_tool.py
174 lines (137 loc) · 6.57 KB
/
analyse_tool.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
__author__ = 'janekg89'
import simulation
import numpy as np # module for scientific computing
import matplotlib.pyplot as plt # module for plotting "a la" matlab
from scipy.stats import norm
from scipy.stats import moment
class Analyse(simulation.Felix_Method):
"""
Beinhaltet mehrere Methoden zur Analyse der Trajectorie. Erbt von der Klasse Simulation.Felix_Method .
"""
def __init__(self, D , particles,length, alpha,dt,x=2, version="python"):
simulation.Felix_Method.__init__(self,D=D, particles=particles,length=length,alpha=alpha,dt=dt,x=x,version=version)
self.trajectory=simulation.Felix_Method(D,particles,length,alpha,dt=dt,x=x,version=version).compute_trajectory()
def msdanalyt(self):
"""
:return: returns the analytical result for MSD
.. math::
\\delta r^{2}(t)=2 D t^{ \\alpha}
"""
return 2*((self.t*self.dt)**self.alpha)*self.K_alpha
def msd_ensemble(self):
"""
:return: returns the ensemble average of the simulated trajectories of all particles.
.. math::
\\delta r^{2}(t)=\\frac{\\sum_{j=0}^N {\\eta_i}}{N}
N= number of particles
i= cumulant
"""
r_t_allparticles=self.trajectory
r_t_allparticles_squared=abs(r_t_allparticles)**2
msd=r_t_allparticles_squared.mean(axis=0)
std=r_t_allparticles.std(axis=0)
return msd, std
def distribution(self,t=0,histpoints=70):
"""
:param t: point in time
:param histpoints: amount of timepoints
:return: Distribution of particles at time t
"""
r_t_allparticles=self.trajectory
r_tt=r_t_allparticles.T
hist, hist_p = np.histogram(r_tt[t], histpoints, normed=True)
hist_p=hist_p[1:]
return hist_p*self.dt,hist
def distribution_abs(self,t=0,histpoints=70):
"""
:param t: point in time
:param histpoints: amount of timepoints
:return: Distribution of particles at time t
"""
r_t_allparticles=abs(self.trajectory)
r_tt=r_t_allparticles.T
hist, hist_p = np.histogram(r_tt[t], histpoints, normed=True)
hist_p=hist_p[1:]
return hist_p,hist
def rescaled_function(self,t=0,histpoints=70):
r_t_allparticles=abs(self.trajectory)
r_tt=r_t_allparticles.T
hist, hist_p = np.histogram(r_tt[t], histpoints, normed=True)
hist_p=hist_p[1:]
#r,dist=Analyse(self.D,self.particles,self.n,self.alpha).distribution_abs(t,histpoints)
#res_r=hist_p*(2*self.D*t*self.dt)**(-self.alpha/2.)
res_r=hist_p*(2*self.D*(t*self.dt)**(self.alpha))**(-1./2.)
res_dis=hist*hist_p/2
return res_r, res_dis
def analytical_distribution_of_particles(self,t, r_dis=50):
r=np.arange(-r_dis, r_dis, 0.01)
#r=abs(r)
distrib=np.exp(-(r**2)/(2*2*self.D*(t*self.dt)**self.alpha))/np.sqrt(np.pi*2*2*self.D*(t*self.dt)**self.alpha)
distrib1=norm.pdf(r,0,np.sqrt(2*self.D*(t*self.dt)**self.alpha))
return r, distrib, distrib1
def rescaled_analytical_distribution(self, t, r_dis= 4):
"""
:param t: point in time (return should be independed of t, but i left possiblitiy to also demonstrate that this is true)
:param r_dis: the distance which want to be sampled. (not recommended to change)
:return: analytical rescaled function ( fill in math)
"""
r=np.arange(0.07, r_dis, 0.01)
#res_r=r*(2*self.D*t*self.dt)**(-self.alpha/2.)
res_dis=r*(np.exp(-r**2./2.))/(np.sqrt(2.*np.pi))
#res_r, res_dis = Analyse(self.D,self.particles,self.n,self.alpha).analytical_distribution_of_particles(t,r_dis=r_res)
#res_r = np.arange(-50, 50, 0.01)
#res_dis= norm.pdf(res_r,0,np.sqrt(2*self.D))
return r, res_dis
def msd_time(self,i=0):
"""
:param i: selected Particle trajectory of the which the time averaged MSD should be calculated. Cannot be bigger than total partical number
:return: The time averaged mean square displacement ans the sample standard deviation
"""
sinfgletrajectory=self.trajectory[i]
totalsize=len(sinfgletrajectory)
msd=[None]*len(sinfgletrajectory)
std=[None]*len(sinfgletrajectory)
for j in range(0,totalsize):
msdink=[]
for i in range(0,totalsize-j):
msdink.append(((sinfgletrajectory[i]-sinfgletrajectory[j+i])**2))
msd[j]=np.array(msdink).mean(axis=0)
std[j]=np.array(msdink).std(axis=0)/np.sqrt(totalsize-j)
return np.array(msd),np.array(std)
def nongaussian_parameter(self):
moment2poten2=(moment(self.trajectory,moment=2,axis=0))**2
moment4=moment(self.trajectory,moment=4,axis=0)
nongaussianparamter=(1/3.)*moment4/moment2poten2-1
return nongaussianparamter
def invert_time(self):
"""
Dreht die Richtung der Zeit
:return:
"""
traject=np.fliplr(self.trajectory)
traject=np.subtract(traject.T,traject[:,0].T)
self.trajectory=traject.T
def plot_freq(self):
plt.plot(self.frq)
plt.show()
def plotting(self, msdtype="ensemble", particlemsdtime=0,error=0, showlegend=None,scale="loglog"):
"""
:param error: Number of standard deviations from mean, which is shown in the figure
:return: A figure with plotting of the Ensemble MSD
"""
if msdtype=="ensemble":
msd,std=self.msd_ensemble()
if msdtype=="time":
msd,std=self.msd_time(particlemsdtime)
colors=['r','b','g','k','c','w','b','r','g','b','k','c','w','b','r','g','b','k','c','w','bo','ro','go','bo','ko','co','wo','bo']
#fig=plt.plot(range(msd_ensemble.size), msd_ensemble ,colors[2], label="ensemble msd")
if scale == "lin":
plt.plot(self.t*self.dt,self.msdanalyt(),":",color=colors[1], label="analytisch D=%f,particles=%d,length=%d,alpha=%f" %(self.D,self.particles,self.n,self.alpha))
if scale == "loglog":
plt.loglog(self.t*self.dt,self.msdanalyt(),":",color=colors[1], label="analytisch D=%f,particles=%d,length=%d,alpha=%f" %(self.D,self.particles,self.n,self.alpha))
fig=plt.errorbar(self.t*self.dt, msd, yerr=error*std,label="Spektrale Methode mit D=%f,particles=%d, length=%d ,alpha=%f, Std=%f" %(self.D,self.particles,self.n,self.alpha,error))
if showlegend is not None:
plt.legend(loc=2)
plt.xlabel('Steps', fontsize=14)
plt.ylabel('MSD', fontsize=14)
return fig