-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow.py
733 lines (615 loc) · 25.8 KB
/
flow.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
"""
Module flow provides classes to compute and analyse displacements, velocities,
and orientations in order to characterise the flow of systems of ABPs.
(see https://yketa.github.io/DAMTP_MSC_2019_Wiki/#ABP%20flow%20characteristics)
"""
import numpy as np
from collections import OrderedDict
from operator import itemgetter
from active_work.read import Dat
from active_work.maths import Distribution, wave_vectors_2D, g2Dto1D,\
g2Dto1Dgrid, mean_sterr, logspace
from active_work.rotors import nu_pdf_th
class Displacements(Dat):
"""
Compute and analyse displacements from simulation data.
(see https://yketa.github.io/DAMTP_MSC_2019_Wiki/#Active%20Brownian%20particles)
"""
def __init__(self, filename, skip=1):
"""
Loads file.
Parameters
----------
filename : string
Name of input data file.
skip : int
Skip the `skip' first computed frames in the following calculations.
(default: 1)
NOTE: This can be changed at any time by setting self.skip.
"""
super().__init__(filename, loadWork=False) # initialise with super class
self.skip = skip # skip the `skip' first frames in the analysis
def nDisplacements(self, dt, int_max=None, jump=1, norm=False):
"""
Returns array of displacements with lag time `dt'.
Parameters
----------
dt : int
Displacement lag time.
int_max : int or None
Maximum number of intervals to consider. (default: None)
NOTE: If int_max == None, then take the maximum number of intervals.
jump : int
Period in number of frames at which to check if particles have
crossed any boundary. (default: 1)
NOTE: `jump' must be chosen so that particles do not move a distance
greater than half the box size during this time.
norm : bool
Return norm of displacements rather than 2D displacement.
(default: False)
Returns
-------
displacements : [not(norm)] (*, self.N, 2) float numpy array
[norm] (*, self.N) float numpy array
Array of computed displacements.
"""
displacements = []
for time0 in self._time0(dt, int_max=int_max):
displacements += [
self.getDisplacements(time0, time0 + dt, jump=jump, norm=norm)]
displacements = np.array(displacements)
return displacements
def displacementsPDF(self, dt, int_max=None, jump=1):
"""
Returns probability density function of displacement norm over lag time
`dt'.
Parameters
----------
dt : int
Displacement lag time.
int_max : int or None
Maximum number of intervals to consider. (default: None)
NOTE: If int_max == None, then take the maximum number of intervals.
jump : int
Period in number of frames at which to check if particles have
crossed any boundary. (default: 1)
NOTE: `jump' must be chosen so that particles do not move a distance
greater than half the box size during this time.
Returns
-------
axes : numpy array
Values at which the probability density function is evaluated.
pdf : float numpy array
Values of the probability density function.
"""
return Distribution(self.nDisplacements(
dt, int_max=int_max, jump=jump, norm=True)).pdf()
def displacementsHist(self, dt, nBins, int_max=None, jump=1,
vmin=None, vmax=None, log=False, rescaled_to_max=False):
"""
Returns histogram with `nBins' bins of displacement norm over lag time
`dt'.
Parameters
----------
dt : int
Displacement lag time.
nBins : int
Number of bins of the histogram.
int_max : int or None
Maximum number of intervals to consider. (default: None)
NOTE: If int_max == None, then take the maximum number of intervals.
jump : int
Period in number of frames at which to check if particles have
crossed any boundary. (default: 1)
NOTE: `jump' must be chosen so that particles do not move a distance
greater than half the box size during this time.
vmin : float
Minimum value of the bins. (default: minimum computed displacement)
vmax : float
Maximum value of the bins. (default: maximum computed displacement)
log : bool
Consider the log of the occupancy of the bins. (default: False)
rescaled_to_max : bool
Rescale occupancy of the bins by its maximum over bins.
(default: False)
Returns
-------
bins : float numpy array
Values of the bins.
hist : float numpy array
Occupancy of the bins.
"""
return Distribution(self.nDisplacements(
dt, int_max=int_max, jump=jump, norm=True)).hist(
nBins, vmin=vmin, vmax=vmax, log=log,
rescaled_to_max=rescaled_to_max)
def dtDisplacements(self, dt, int_max=100, jump=1, norm=False):
"""
Returns array of displacements with lag times `dt'.
Parameters
----------
dt : int array-like
Displacement lag times.
int_max : int
Maximum number of intervals to consider. (default: 100)
jump : int
Period in number of frames at which to check if particles have
crossed any boundary. (default: 1)
NOTE: `jump' must be chosen so that particles do not move a distance
greater than half the box size during this time.
norm : bool
Return norm of displacements rather than 2D displacement.
(default: False)
Returns
-------
displacements : [not(norm)] (*, dt.size, self.N, 2) float numpy array
[norm] (*, dt.size, self.N) float numpy array
Array of computed displacements.
"""
dt = np.array(dt)
time0 = np.array(list(OrderedDict.fromkeys(
np.linspace(self.skip, self.frames - dt.max() - 1, int_max, # array of initial times
endpoint=False, dtype=int))))
displacements = np.empty((time0.size, dt.size, self.N, 2))
for j in range(dt.size):
if j > 0:
for i in range(time0.size):
displacements[i][j] = ( # displacements between time0[i] and time0[i] + dt[j]
displacements[i][j - 1] # displacements between time0[i] and time0[i] + dt[j - 1]
+ self.getDisplacements( # displacements between time0[i] + dt[j - 1] and time0[i] + dt[j]
time0[i] + dt[j - 1], time0[i] + dt[j],
jump=jump))
else:
for i in range(time0.size):
displacements[i][0] = self.getDisplacements( # displacements between time0[i] and time0[i] + dt[0]
time0[i], time0[i] + dt[0],
jump=jump)
if norm: return np.sqrt(np.sum(displacements**2, axis=-1))
return displacements
def msd(self, n_max=100, int_max=100, min=None, max=None, jump=1):
"""
Compute mean square displacement.
Parameters
----------
n_max : int
Maximum number of times at which to compute the mean square
displacement. (default: 100)
int_max : int
Maximum number of different intervals to consider when averaging
the mean square displacement. (default: 100)
min : int or None
Minimum time at which to compute the displacement. (default: None)
NOTE: if min == None, then min = 1.
max : int or None
Maximum time at which to compute the displacement. (default: None)
NOTE: if max == None, then max is taken to be the maximum according
to the choice of int_max.
jump : int
Compute displacements by treating frames in packs of `jump'. This
can speed up calculation but also give unaccuracies if
`jump'*self.dt is of the order of the system size. (default: 1)
Returns
-------
msd_sterr : (3, *) float numpy array
Array of:
(0) lag time at which the mean square displacement is computed,
(1) mean square displacement,
(2) standard error of the computed mean square displacement.
"""
min = 1 if min == None else int(min)
max = ((self.frames - self.skip - 1)//int_max if max == None
else int(max))
# COMPUTE RELEVANT DISPLACEMENTS FROM DATA
dt = logspace(min, max, n_max) # array of lag times
displacements = self.dtDisplacements(dt, # array of displacements for different initial times and lag times
int_max=int_max, jump=jump, norm=False)
# COMPUTE MEAN SQUARE DISPLACEMENTS
msd_sterr = []
for i in range(dt.size):
disp = displacements[:, i]
if self.N > 1:
disp -= disp.mean(axis=1).reshape(displacements.shape[0], 1, 2) # substract mean displacement of particles during each considered intervals
disp.reshape(displacements.shape[0]*self.N, 2)
msd_sterr += [[
dt[i],
*mean_sterr(np.sum(disp**2, axis=-1).flatten())]]
return np.array(msd_sterr)
def msd_th(self, dt):
"""
Returns value of theoretical mean squared displacement at lag time `dt'
for a single ABP.
(see https://yketa.github.io/DAMTP_MSC_2019_Wiki/#One%20ABP)
Parameters
----------
dt : float
Lag time at which to evaluate the theoretical mean squared
displacement.
Returns
-------
msd : float
Mean squared displacement.
"""
if self._isDat0: # general parameters
return 4*self.D*dt + (2*(self.v0**2)/self.Dr)*(
dt + (np.exp(-self.Dr*dt) - 1)/self.Dr)
else: # custom relations between parameters
return 4/(3*self.lp)*dt + 2*self.lp*(
dt + self.lp*(np.exp(-dt/self.lp) - 1))
def _time0(self, dt, int_max=None):
"""
Returns array of initial times to evaluate displacements over lag time
`dt'.
Parameters
----------
dt : int
Displacement lag time.
int_max : int or None
Maximum number of initial times to return. (default: None)
NOTE: if int_max == None, a maximum number of them are returned.
Returns
-------
time0 : (*,) float numpy array
Array of initial times.
"""
time0 = np.linspace(
self.skip, self.frames - 1, int((self.frames - 1 - self.skip)//dt),
endpoint=False, dtype=int)
if int_max == None: return time0
indexes = list(OrderedDict.fromkeys(
np.linspace(0, time0.size, int_max, endpoint=False, dtype=int)))
return np.array(itemgetter(*indexes)(time0), ndmin=1)
class Velocities(Dat):
"""
Compute and analyse velocities from simulation data.
(see https://yketa.github.io/DAMTP_MSC_2019_Wiki/#Active%20Brownian%20particles)
"""
def __init__(self, filename, skip=1):
"""
Loads file.
Parameters
----------
filename : string
Name of input data file.
skip : int
Skip the `skip' first computed frames in the following calculations.
(default: 1)
NOTE: This can be changed at any time by setting self.skip.
"""
super().__init__(filename, loadWork=False) # initialise with super class
self.skip = skip # skip the `skip' first frames in the analysis
def nVelocities(self, int_max=None, norm=False):
"""
Returns array of velocities.
Parameters
----------
int_max : int or None
Maximum number of frames to consider. (default: None)
NOTE: If int_max == None, then take the maximum number of frames.
WARNING: This can be very big.
norm : bool
Return norm of velocities rather than 2D velocities.
(default: False)
Returns
-------
velocities : [not(norm)] (*, self.N, 2) float numpy array
[norm] (*, self.N) float numpy array
Array of computed velocities.
"""
return np.array(list(map(
lambda time0: self.getVelocities(time0, norm=norm),
self._time0(int_max=int_max))))
def velocitiesPDF(self, int_max=None):
"""
Returns probability density function of velocity norm.
PARAMETERS
----------
int_max : int or None
Maximum number of frames to consider. (default: None)
NOTE: If int_max == None, then take the maximum number of frames.
WARNING: This can be very big.
Returns
-------
axes : numpy array
Values at which the probability density function is evaluated.
pdf : float numpy array
Values of the probability density function.
"""
return Distribution(self.nVelocities(int_max=int_max, norm=True)).pdf()
def velocitiesHist(self, nBins, int_max=None, vmin=None, vmax=None,
log=False, rescaled_to_max=False):
"""
Returns histogram with `nBins' bins of velocity norm.
Parameters
----------
nBins : int
Number of bins of the histogram.
int_max : int or None
Maximum number of frames to consider. (default: None)
NOTE: If int_max == None, then take the maximum number of frames.
WARNING: This can be very big.
vmin : float
Minimum value of the bins. (default: minimum computed velocity)
vmax : float
Maximum value of the bins. (default: maximum computed velocity)
log : bool
Consider the log of the occupancy of the bins. (default: False)
rescaled_to_max : bool
Rescale occupancy of the bins by its maximum over bins.
(default: False)
Returns
-------
bins : float numpy array
Values of the bins.
hist : float numpy array
Occupancy of the bins.
"""
return Distribution(self.nVelocities(int_max=int_max, norm=True)).hist(
nBins, vmin=vmin, vmax=vmax, log=log,
rescaled_to_max=rescaled_to_max)
def energySpectrum(self, int_max=None, nBoxes=None):
"""
Returns kinetic energy spectrum.
(see https://yketa.github.io/DAMTP_MSC_2019_Wiki/#ABP%20flow%20characteristics)
Parameters
----------
int_max : int or None
Maximum number of frames to consider. (default: None)
NOTE: If int_max == None, then take the maximum number of frames.
WARNING: This can be very big.
nBoxes : int
Number of boxes in each direction to compute the grid of velocities
which FFT will be computed. (default: None)
NOTE: If nBoxes == None, then nBoxes = int(sqrt(self.N)).
Returns
-------
E : (*, 2) float Numpy array
Array of (k, E(k)).
"""
if nBoxes == None: nBoxes = int(np.sqrt(self.N))
nBoxes = int(nBoxes)
wave_vectors = np.sqrt(np.sum(
wave_vectors_2D(nBoxes, nBoxes, self.L/nBoxes)**2, axis=-1)) # grid of wave vector norms
FFTsq = [] # list of squared velocity FFT
for time, velocity in zip(
self._time0(int_max=int_max),
self.nVelocities(int_max=int_max, norm=True)):
velocitiesFFT = np.fft.fft2( # FFT of displacement grid
self.toGrid(time, velocity, nBoxes=nBoxes),
axes=(0, 1))
FFTsq += [np.real(np.conj(velocitiesFFT)*velocitiesFFT)]
return g2Dto1Dgrid(
wave_vectors*np.mean(FFTsq, axis=0), wave_vectors,
average_grid=False)
def _time0(self, int_max=None):
"""
Returns array of frames at which to compute velocities.
Parameters
----------
int_max : int or None
Maximum number of frames to consider. (default: None)
NOTE: If int_max == None, then take the maximum number of frames.
WARNING: This can be very big.
Returns
-------
time0 : (*,) float numpy array
Array of frames.
"""
if int_max == None: return np.array(range(self.skip, self.frames - 1))
return np.linspace(
self.skip, self.frames - 1, int(int_max), endpoint=False, dtype=int)
class Orientations(Dat):
"""
Compute and analyse orientations from simulation data.
(see https://yketa.github.io/DAMTP_MSC_2019_Wiki/#Active%20Brownian%20particles)
"""
def __init__(self, filename, skip=1):
"""
Loads file.
Parameters
----------
filename : string
Name of input data file.
skip : int
Skip the `skip' first computed frames in the following calculations.
(default: 1)
NOTE: This can be changed at any time by setting self.skip.
"""
super().__init__(filename, loadWork=False) # initialise with super class
self.skip = skip # skip the `skip' first frames in the analysis
def nOrientations(self, int_max=None):
"""
Returns array of orientations.
Parameters
----------
int_max : int or None
Maximum number of frames to consider. (default: None)
NOTE: If int_max == None, then take the maximum number of frames.
WARNING: This can be very big.
Returns
-------
orientations : (*, self.N) float numpy array
Array of computed orientations.
"""
return np.array(list(map(
lambda time0: self.getOrientations(time0),
self._time0(int_max=int_max))))
def nDirections(self, int_max=None):
"""
Returns array of directions.
Parameters
----------
int_max : int or None
Maximum number of frames to consider. (default: None)
NOTE: If int_max == None, then take the maximum number of frames.
WARNING: This can be very big.
Returns
-------
directions : (*, self.N, 2) float numpy array
Array of computed directions.
"""
return np.array(list(map(
lambda time0: self.getDirections(time0),
self._time0(int_max=int_max))))
def nOrder(self, int_max=None, norm=False):
"""
Returns array of order parameter.
Parameters
----------
int_max : int or None
Maximum number of frames to consider. (default: None)
NOTE: If int_max == None, then take the maximum number of frames.
WARNING: This can be very big.
norm : bool
Return norm of order parameter rather than 2D order parameter.
(default: False)
Returns
-------
order : [not(norm)] (*, 2) float numpy array
[norm] (*,) float numpy array
Array of computed order parameters.
"""
return np.array(list(map(
lambda time0: self.getOrderParameter(time0, norm=norm),
self._time0(int_max=int_max))))
def orderPDF(self, int_max=None):
"""
Returns probability density function of order parameter norm.
PARAMETERS
----------
int_max : int or None
Maximum number of frames to consider. (default: None)
NOTE: If int_max == None, then take the maximum number of frames.
WARNING: This can be very big.
Returns
-------
axes : numpy array
Values at which the probability density function is evaluated.
pdf : float numpy array
Values of the probability density function.
"""
return Distribution(self.nOrder(int_max=int_max, norm=True)).pdf()
def orderHist(self, nBins, int_max=None, vmin=None, vmax=None,
log=False, rescaled_to_max=False):
"""
Returns histogram with `nBins' bins of order parameter norm.
Parameters
----------
nBins : int
Number of bins of the histogram.
int_max : int or None
Maximum number of frames to consider. (default: None)
NOTE: If int_max == None, then take the maximum number of frames.
WARNING: This can be very big.
vmin : float
Minimum value of the bins.
(default: minimum computed order parameter)
vmax : float
Maximum value of the bins.
(default: maximum computed order parameter)
log : bool
Consider the log of the occupancy of the bins. (default: False)
rescaled_to_max : bool
Rescale occupancy of the bins by its maximum over bins.
(default: False)
Returns
-------
bins : float numpy array
Values of the bins.
hist : float numpy array
Occupancy of the bins.
"""
return Distribution(self.nOrder(int_max=int_max, norm=True)).hist(
nBins, vmin=vmin, vmax=vmax, log=log,
rescaled_to_max=rescaled_to_max)
def orientationsCor(self, int_max=None, nBoxes=None, cylindrical=True):
"""
Compute spatial correlations of particles' orientations (and density).
NOTE: Correlations are computed with FFT.
(see https://yketa.github.io/DAMTP_MSC_2019_Wiki/#Fourier%20transform%20field%20correlation)
Parameters
----------
int_max : int or None
Maximum number of frames to consider. (default: None)
NOTE: If int_max == None, then take the maximum number of frames.
WARNING: This can be very big.
nBoxes : int
Number of grid boxes in each direction. (default: None)
NOTE: if nBoxes==None, then None is passed to self.toGrid.
cylindrical : bool
Return cylindrical average of correlations.
Returns
-------
[cylindrical]
Cuu : (*, 2) float Numpy array
Array of (r, Cuu(r)) with Cuu(r) the cylindrically averaged spatial
correlations of orientation.
Cdd : (*, *) float Numpy array
Array of (r, Cdd(r)) with Cdd(r) the cylindrically averaged spatial
correlations of density.
[not(cylindrical)]
Cuu : (*, *) float Numpy array
2D grid of spatial orientation correlation.
Cdd : (*, *) float Numpy array
2D grid of spatial density correlation.
"""
grids = list(map(
lambda time, orientations: list(map(
lambda grid:
self.toGrid(time, grid, nBoxes=nBoxes,
box_size=self.L, centre=None, average=False), # do not average but sum
(orientations, np.full((self.N,), fill_value=1)))),
*(self._time0(int_max=int_max), self.nDirections(int_max=int_max))))
orientationGrids = np.array([_[0] for _ in grids]) # grids of orientation at different times
densityGrids = np.array([_[1] for _ in grids]) # grids of density at different times
cor = (lambda grid: # returns correlation grid of a 2D grid
(lambda FFT: np.real(np.fft.ifft2(np.conj(FFT)*FFT)))
(np.fft.fft2(grid)))
Cuu2D = np.sum(
list(map(
lambda dim: # sum over dimensions
np.mean( # mean correlation over time
list(map(cor, orientationGrids[:, :, :, dim])),
axis=0),
range(2))),
axis=0)
Cdd2D = np.mean( # mean correlation over time
list(map(cor, densityGrids)),
axis=0)
return tuple(map(
lambda C2D:
g2Dto1D(C2D/C2D[0, 0], self.L) if cylindrical # cylindrical average
else C2D/C2D[0, 0], # 2D grid
(Cuu2D, Cdd2D)))
def nu_pdf_th(self, *nu):
"""
Returns value of theoretical probability density function of the order
parameter norm.
(see https://yketa.github.io/DAMTP_MSC_2019_Wiki/#N-interacting%20Brownian%20rotors)
Parameters
----------
nu : float
Values of the order parameter norm at which to evaluate the
probability density function.
Returns
-------
pdf : (*,) float numpy array
Probability density function.
"""
return nu_pdf_th(self.N, self.g, 1/self.lp, *nu)
def _time0(self, int_max=None):
"""
Returns array of frames at which to compute orientations.
Parameters
----------
int_max : int or None
Maximum number of frames to consider. (default: None)
NOTE: If int_max == None, then take the maximum number of frames.
WARNING: This can be very big.
Returns
-------
time0 : (*,) float numpy array
Array of frames.
"""
if int_max == None: return np.array(range(self.skip, self.frames - 1))
return np.linspace(
self.skip, self.frames - 1, int(int_max), endpoint=False, dtype=int)