-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiscellaneous.py
263 lines (199 loc) · 6.2 KB
/
miscellaneous.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
"""
Module miscellaneous contains miscellaneous functions and classes related to
this project.
"""
import numpy as np
import scipy.optimize as optimize
######################
### RTPs ON A RING ###
######################
class RTPring:
"""
Theoretical results for 2 RTPs on a ring.
(see https://github.com/yketa/DAMTP_MSC_2019_Wiki/tree/master/Summaries/RTPring)
"""
def __init__(self):
"""
Initialises computation parameters.
"""
self._zero = 1e-16
self.none = np.nan
self.lambdaLmin = -20
def s(self, L, psi):
"""
Biasing parameter rescaled by the product of persistence length and
propulsion velocity as a function of the cumulant generating function
(CGF) scaled by the persistence time.
Parameters
----------
L : float
Ring length rescaled by the persistence length.
psi : float
CGF rescaled by the persistence time.
Returns
-------
s : float
Associated biasing parameter rescaled by the product of persistence
length and propulsion velocity
"""
if psi == 0: return 0
k = self._k(psi)
try:
s = (psi*(psi + 4))/(2*psi + 4)
if psi > 0: tan = np.tanh
if psi < 0: tan = np.tan
den = psi*(psi + 2)*(psi + 4) + k*((psi + 2)**2)/tan(k*L/2.)
if den < 0: return self.none
s += (psi*(psi + 4))/den
if s < self.lambdaLmin: return self.none
if s*psi < 0 : return self.none
return s
except ZeroDivisionError:
return self.none
def nu(self, psi):
"""
Polarisation of the RTPs.
Parameters
----------
psi : float
CGF rescaled by the persistence time.
Returns
-------
nu : float
Polarisation.
"""
if psi == 0: return 1./2.
return 2./(psi + 4)
def nuAve(self, L, psi):
"""
Polarisation of the RTPs computed with the full-time distribution.
Parameters
----------
L : float
Ring length rescaled by the persistence length.
psi : float
CGF rescaled by the persistence time.
Returns
-------
nu : float
Polarisation.
"""
if psi == 0: return 1./2.
k = self._k(psi)
omega = 2*self._k(psi)/(psi + 2)
if psi < 0:
omegasq = -omega**2
sin = np.sin
cos = np.cos
if psi > 0:
omegasq = omega**2
sin = np.sinh
cos = np.cosh
nu = (
(2*(omega/k)*(1 + cos(k*L)) + 2*(omegasq/k)*sin(k*L)
+ (4/((psi + 2)**2))*(sin(k*L)/k + L))
/(2*(omega/k)*(1 + cos(k*L)) + (2 + (psi + 2)**2)*(omegasq/k)*sin(k*L)
+ (psi + 2)*(1 - omegasq) + (psi + 2)*(1 + omegasq)*cos(k*L)
+ (4/((psi + 2)**2) + (1 - omegasq))*sin(k*L)/k
+ (4/((psi + 2)**2) + (1 + omegasq))*L)
)
return nu
def Psi(self, Lambda):
"""
Rescaled cumulant generating function (CGF) in the scaling regime.
Parameters
----------
Lambda : float
Rescaled biasing parameter.
Returns
-------
Psi : float
Rescaled SCGF.
"""
if Lambda == 0: return 0
if Lambda > 0:
return optimize.brentq(
lambda _: self._scaling_function(_, Lambda),
np.abs(self._zero),
1e4)
if Lambda < 0:
return optimize.brentq(
lambda _: self._scaling_function(_, Lambda),
-((np.pi**2)/4 - np.abs(self._zero)),
-np.abs(self._zero))
def Gamma(self, Lambda):
"""
Rescaled sticking probability of parallel particles in the scaling
regime.
Parameters
----------
Lambda : float
Rescaled biasing parameter.
Returns
-------
Gamma : float
Rescaled sticking probability.
"""
if Lambda == 0: return 1 # wild guess
return self.Psi(Lambda)/Lambda
def LEpsilon(self, Lambda, *rL):
"""
Product of ring length and regular part of the probability density
function in the scaling regime.
Parameters
----------
Lambda : float
Resclaed biasing parameter.
rL : float
Ratio of position over ring length.
Returns
-------
Epsilon : float Numpy array
Probability density function.
"""
rL = np.array(rL)
prefactor = (1./4.)*self.Gamma(Lambda)
if Lambda == 0: return np.full(rL.shape, fill_value=prefactor)
psi = self.Psi(Lambda)
if Lambda > 0:
return (prefactor*np.cosh(np.sqrt(psi)*(1 - 2*rL))
/np.cosh(np.sqrt(psi)))
if Lambda < 0:
return (prefactor*np.cos(np.sqrt(-psi)*(1 - 2*rL))
/np.cos(np.sqrt(-psi)))
def _scaling_function(self, Psi, Lambda):
"""
Function which root for a given `Lambda` gives the corresponding `Psi`.
Parameters
----------
Psi : float
Rescaled cumulant generating function (CGF).
Lambda : float
Rescaled biasing parameter.
Returns
-------
scale : float
Scaling function.
"""
if Lambda == 0: return 0
if Lambda > 0:
return (
1./(np.tanh(np.sqrt(np.abs(Psi)))*np.sqrt(np.abs(Psi)))
- 1./Lambda)
if Lambda < 0:
return (
1./(np.tan(np.sqrt(np.abs(Psi)))*np.sqrt(np.abs(Psi)))
+ 1./Lambda)
def _k(self, psi):
"""
Modulus of the scaled exponential length scale.
Parameters
----------
psi : float
CGF rescaled by the persistence time.
Returns
-------
k : float
Scaled exponential length scale.
"""
return np.sqrt(np.abs(psi*(psi/4. + 1)))