-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffsimviz.py
749 lines (589 loc) · 25.9 KB
/
diffsimviz.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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
#!/usr/bin/env python
# coding: utf-8
# # Global/Universal Initializations
# In[32]:
import random
import numpy
import numpy as np
import os
import imageio.v2 as imageio
from math import sqrt
from scipy.stats import norm
import matplotlib.pyplot as plt
import sys
import argparse
# Create the parser
parser = argparse.ArgumentParser(description='Diffusion Simulator Visualizer')
# Add arguments
parser.add_argument('-mol', '--numofmol', type=int, default=8, help='Number of molecules')
parser.add_argument('-steps', '--steps', type=int, default=30, help='Number of steps')
parser.add_argument('-rad', '--radius', type=float, default=0.2, help='Radius')
parser.add_argument('-vrad', '--vradius', type=float, default=0.2, help='VRadius')
parser.add_argument('-theta', type=float, default=45, help='Theta')
parser.add_argument('-delta', type=float, default=0.4, help='Delta')
parser.add_argument('-time', type=float, default=7, help='Total time')
parser.add_argument('-sticks', type=int, default=2, help="Number of 'sticks'")
# Parse the arguments
args = parser.parse_args()
# Assign values
numofmol = args.numofmol # formerly mol
steps = args.steps # formerly N
radius = args.radius
vradius = args.vradius
theta = args.theta
delta = args.delta
time = args.time # formerly T
sticks = args.sticks
# numofmol = 8
# steps = 30
# radius = 0.2
# vradius=0.2
# theta = 45
# delta = 0.4
# # Total time.
# time = 7
# Time step size
dt = time/steps
traj = np.zeros((2,steps,numofmol))
initialrot = np.zeros((numofmol,2))
where = np.zeros((numofmol))
ratiointra = 0.5
# In[33]:
# function is also used in the stick cross generation
def make_stick_line(theta,radius):
y = np.linspace(-2,2,100)
xlow = np.zeros((100,))
xlow[:] = -radius
xhigh = np.zeros((100,))
xhigh[:] = radius
line1 = np.c_[xlow,y]
line2 = np.c_[xhigh,y]
c, s = np.cos(theta), np.sin(theta)
rot = np.array(((c, -s), (s, c)))
line1rot = line1 @ rot
line2rot = line2 @ rot
return line1rot, line2rot
line1, line2 = make_stick_line(theta,radius)
# # Vertical Stick
# In[34]:
if sticks==1:
"""
brownian() implements one dimensional Brownian motion (i.e. the Wiener process).
"""
def brownian_singlestep(x0, n, dt, delta, radius, theta, out=None):
"""
Generate an instance of Brownian motion (i.e. the Wiener process):
X(t) = X(0) + N(0, delta**2 * t; 0, t)
where N(a,b; t0, t1) is a normally distributed random variable with mean a and
variance b. The parameters t0 and t1 make explicit the statistical
independence of N on different time intervals; that is, if [t0, t1) and
[t2, t3) are disjoint intervals, then N(a, b; t0, t1) and N(a, b; t2, t3)
are independent.
Written as an iteration scheme,
X(t + dt) = X(t) + N(0, delta**2 * dt; t, t+dt)
If `x0` is an array (or array-like), each value in `x0` is treated as
an initial condition, and the value returned is a numpy array with one
more dimension than `x0`.
Arguments
---------
x0 : float or numpy array (or something that can be converted to a numpy array
using numpy.asarray(x0)).
The initial condition(s) (i.e. position(s)) of the Brownian motion.
n : int
The number of steps to take.
dt : float
The time step.
delta : float
delta determines the "speed" of the Brownian motion. The random variable
of the position at time t, X(t), has a normal distribution whose mean is
the position at time t=0 and whose variance is delta**2*t.
out : numpy array or None
If `out` is not None, it specifies the array in which to put the
result. If `out` is None, a new numpy array is created and returned.
sticks : int
The number of sticks (pairs of wall) in the simulation.
Used for conditionals as more collisions will need to be checked.
Returns
-------
A numpy array of floats with shape `x0.shape + (n,)`.
Note that the initial value `x0` is not included in the returned array.
"""
x0 = np.asarray(x0)
# For each element of x0, generate a sample of n numbers from a
# normal distribution.
coordhold = np.copy(x0)
coordfinal = np.zeros((2,n))
#coordfinal = []
c, s = np.cos(theta), np.sin(theta)
rot = np.array(((c, -s), (s, c)))
if x0[0] < -radius:
where=0
for i in range(n):
if i == 0:
coordfinal[:,0] = x0
#print(i)
else:
r = norm.rvs(size=(2,),scale=delta*sqrt(dt))
if (r[0] + coordhold[0]) < -radius:
coordhold += r
coordfinal[:,i] = coordhold
else:
while (r[0] + coordhold[0]) > -radius:
r = norm.rvs(size=(2,),scale=delta*sqrt(dt))
coordhold += r
coordfinal[:,i] = coordhold
elif x0[0] > radius:
where=0
for i in range(n):
if i == 0:
coordfinal[:,0] = x0
#print(i)
else:
r = norm.rvs(size=(2,),scale=delta*sqrt(dt))
if (r[0] + coordhold[0]) > radius:
coordhold += r
coordfinal[:,i] = coordhold
else:
while (r[0] + coordhold[0]) < radius:
r = norm.rvs(size=(2,),scale=delta*sqrt(dt))
coordhold += r
coordfinal[:,i] = coordhold
else:
where = 1
for i in range(n):
if i == 0:
coordfinal[:,0] = x0
#print(i)
else:
r = norm.rvs(size=(2,),scale=delta*sqrt(dt))
if (r[0] + coordhold[0]) > -radius and (r[0] + coordhold[0]) < radius:
coordhold += r
coordfinal[:,i] = coordhold
else:
while (r[0] + coordhold[0]) < -radius or (r[0] + coordhold[0]) > radius:
r = norm.rvs(size=(2,),scale=delta*sqrt(dt))
coordhold += r
coordfinal[:,i] = coordhold
coordfinal = np.squeeze(np.array(coordfinal))
# Add the initial condition.
#coordfinal += np.expand_dims(x0, axis=-1)
coordfinal = np.transpose(rot) @ coordfinal
initialrot = np.transpose(rot) @ x0
return coordfinal,where,initialrot
# In[35]:
initialinx = numpy.random.uniform(low=-radius, high=radius, size=(round(numofmol*ratiointra),1))
initialiny = numpy.random.uniform(low=-1.5, high=1.5, size=(round(numofmol*ratiointra),1))
initialincomb = np.concatenate((initialinx,initialiny),axis=1)
initialleftx = numpy.random.uniform(low=-1.5, high=-radius, size=(round(numofmol*(1-ratiointra)*0.5),1))
initiallefty = numpy.random.uniform(low=-1.5, high=1.5, size=(round(numofmol*(1-ratiointra)*0.5),1))
initialleftcomb = np.concatenate((initialleftx,initiallefty),axis=1)
initialrightx = numpy.random.uniform(low=radius, high=1.5, size=(round(numofmol*(1-ratiointra)*0.5),1))
initialrighty = numpy.random.uniform(low=-1.5, high=1.5, size=(round(numofmol*(1-ratiointra)*0.5),1))
initialrightcomb = np.concatenate((initialrightx,initialrighty),axis=1)
initial = np.concatenate((initialincomb,initialleftcomb,initialrightcomb),axis=0)
# In[36]:
# get_ipython().run_line_magic('matplotlib', 'inline')
fig = plt.figure()
ax = fig.add_subplot(111)
filenames = []
for i in range(steps):
# print(i)
for j in range(numofmol):
new_y = traj[1,:i,j]
new_x = traj[0,:i,j]
if i == 0:
ax.scatter(traj[0,0,j],traj[1,0,j],color='black')
#elif i==steps-1:
#ax.scatter(traj[0,i,j],traj[1,i,j])
if where[j] == 1:
ax.plot(new_x,new_y,'deepskyblue')
else:
ax.plot(new_x,new_y,'lightskyblue')
ax.plot(line1[:,0],line1[:,1],color='black')
ax.plot(line2[:,0],line2[:,1],color='black')
plt.xlim((-1.5, 1.5)) # set the xlim to left, right
plt.ylim((-1.5, 1.5))
plt.savefig(f'test_{i}.png')
filenames.append(f'test_{i}.png')
#ax.clear()
#display(fig)
# clear_output(wait=True)
# # Stick Cross
# In[37]:
elif sticks==2:
def brownian_singlestep_cross(x0, n, dt, delta, radius, vradius, theta, out=None):
x0 = np.asarray(x0)
# For each element of x0, generate a sample of n numbers from a
# normal distribution.
coordhold = np.copy(x0)
coordfinal = np.zeros((2,n))
#coordfinal = []
c, s = np.cos(theta), np.sin(theta)
rot = np.array(((c, -s), (s, c)))
# Quadrant 1 check
if x0[0] > radius and x0[1] > vradius:
where=0
for i in range(n):
# print(i)
# print("Q1 For Loop")
if i == 0:
coordfinal[:,0] = x0
# print("Q1 If Statement")
else:
# print("Q1 Else Statement")
r = norm.rvs(size=(2,),scale=delta*sqrt(dt))
if (r[0] + coordhold[0]) > radius and (r[1] + coordhold[1]) > vradius:
# print("Q1 Else If Statement")
coordhold += r
coordfinal[:,i] = coordhold
else:
# print("Q1 Else Else Statement")
while (r[0] + coordhold[0]) < radius or (r[1] + coordhold[1]) < vradius:
# print("Q1 Else Else While Statement")
r = norm.rvs(size=(2,),scale=delta*sqrt(dt))
coordhold += r
coordfinal[:,i] = coordhold
# print("Q1 Else Else Post-While Statement")
# Quadrant 2 check
elif x0[0] < -radius and x0[1] > vradius:
where = 0
for i in range(n):
# print(i)
# print("Quadrant 2, Iteration:", i)
if i == 0:
coordfinal[:,0] = x0
else:
r = norm.rvs(size=(2,), scale=delta*sqrt(dt))
if (r[0] + coordhold[0]) < -radius and (r[1] + coordhold[1]) > vradius:
coordhold += r
coordfinal[:,i] = coordhold
else:
# print("Entering Quadrant 2 While Loop")
while (r[0] + coordhold[0]) > -radius or (r[1] + coordhold[1]) < vradius:
r = norm.rvs(size=(2,), scale=delta*sqrt(dt))
# print("Inside Quadrant 2 While Loop")
coordhold += r
coordfinal[:,i] = coordhold
# Quadrant 3 check
elif x0[0] < -radius and x0[1] < -vradius:
where=0
for i in range(n):
# print(i)
# print("Q3 For Loop")
if i == 0:
coordfinal[:,0] = x0
# print("Q3 If Statement")
else:
# print("Q3 Else Statement")
r = norm.rvs(size=(2,),scale=delta*sqrt(dt))
if (r[0] + coordhold[0]) < -radius and (r[1] + coordhold[1]) < -vradius:
# print("Q3 Else If Statement")
coordhold += r
coordfinal[:,i] = coordhold
else:
# print("Q3 Else Else Statement")
while (r[0] + coordhold[0]) > -radius or (r[1] + coordhold[1]) > -vradius:
# print("Q3 Else Else While Statement")
r = norm.rvs(size=(2,),scale=delta*sqrt(dt))
coordhold += r
coordfinal[:,i] = coordhold
# print("Q3 Else Else Post-While Statement")
# Quadrant 4 check
elif x0[0] > radius and x0[1] < -vradius:
where=0
for i in range(n):
# print("Q4 For Loop")
# print(i)
if i == 0:
coordfinal[:,0] = x0
# print("Q4 If Statement")
else:
# print("Q4 Else Statement")
r = norm.rvs(size=(2,),scale=delta*sqrt(dt))
if (r[0] + coordhold[0]) > radius and (r[1] + coordhold[1]) < -vradius:
# print("Q4 Else If Statement")
coordhold += r
coordfinal[:,i] = coordhold
else:
# print("Q4 Else Else Statement")
while (r[0] + coordhold[0]) < radius or (r[1] + coordhold[1]) > -vradius:
# print("Q4 Else Else While Statement")
r = norm.rvs(size=(2,),scale=delta*sqrt(dt))
coordhold += r
coordfinal[:,i] = coordhold
# print("Q4 Else Else Post-While Statement")
# horizontal stick check
# Does not need to check is x0 is above or below the stick as it is already checked in the quadrant checks
elif x0[1] < vradius and x0[1] > -vradius:
where=1
for i in range(n):
# print(i)
# print("Horizontal Stick For Loop")
if i == 0:
coordfinal[:,0] = x0
else:
r = norm.rvs(size=(2,),scale=delta*sqrt(dt))
if (r[1] + coordhold[1]) < vradius and (r[1] + coordhold[1]) > -vradius:
coordhold += r
coordfinal[:,i] = coordhold
else:
while (r[1] + coordhold[1]) > vradius or (r[1] + coordhold[1]) < -vradius:
r = norm.rvs(size=(2,),scale=delta*sqrt(dt))
coordhold += r
coordfinal[:,i] = coordhold
# else case should be the vertical stick check
elif x0[0] > -radius and x0[0] < radius:
where=0
where = 1
for i in range(n):
# print(i)
# print("vertical stick case")
if i == 0:
# print("vstick if")
coordfinal[:,0] = x0
else:
r = norm.rvs(size=(2,),scale=delta*sqrt(dt))
# print("vstick else")
if (r[0] + coordhold[0]) > -radius and (r[0] + coordhold[0]) < radius:
# print("vstick else > if")
coordhold += r
coordfinal[:,i] = coordhold
else:
# print("vstick else > else")
while (r[0] + coordhold[0]) < -radius or (r[0] + coordhold[0]) > radius:
# print("vstick else > else > while")
r = norm.rvs(size=(2,),scale=delta*sqrt(dt))
coordhold += r
coordfinal[:,i] = coordhold
else:
print("conditionals failed")
print(x0)
coordfinal = np.squeeze(np.array(coordfinal))
# Add the initial condition.
#coordfinal += np.expand_dims(x0, axis=-1)
coordfinal = np.transpose(rot) @ coordfinal
initialrot = np.transpose(rot) @ x0
return coordfinal,where,initialrot
# In[38]:
# What's probably more efficient is to have a counter of how many particles need to be in each area, and then reduce a counter as particles are placed in each area.
# Then, if the counter is 0, then the particles can't be placed in that area.
# Two Pairs (CROSS)
# In vertical stick, generate initial position/coordinate in top area only.
initialinx = numpy.random.uniform(low=-radius, high=radius, size=(round(numofmol*ratiointra*0.25)*2,1))
initialiny = numpy.random.uniform(low=vradius, high=1.5, size=(round(numofmol*ratiointra*0.25),1))
initialiny2= numpy.random.uniform(low=-1.5, high=-vradius, size=(round(numofmol*ratiointra*0.25),1))
initialiny = np.concatenate((initialiny,initialiny2),axis=0)
initialinVStick = np.concatenate((initialinx,initialiny),axis=1)
initialinx = numpy.random.uniform(low=-1.5,high=-radius,size=(round(numofmol*ratiointra*0.25),1))
initialinx2= numpy.random.uniform(low=radius,high=1.5,size=(round(numofmol*ratiointra*0.25),1))
initialinx = np.concatenate((initialinx,initialinx2),axis=0)
initialiny = numpy.random.uniform(low=-vradius,high=vradius,size=(round(numofmol*ratiointra*0.25)*2,1))
initialinHStick = np.concatenate((initialinx,initialiny),axis=1)
initialQ1x = numpy.random.uniform(low=radius,high=1.5,size=(round(numofmol*(1-ratiointra)*0.25),1))
initialQ1y = numpy.random.uniform(low=vradius,high=1.5,size=(round(numofmol*(1-ratiointra)*0.25),1))
initialQ1 = np.concatenate((initialQ1x,initialQ1y),axis=1)
initialQ2x = numpy.random.uniform(low=-1.5,high=-radius,size=(round(numofmol*(1-ratiointra)*0.25),1))
initialQ2y = numpy.random.uniform(low=vradius,high=1.5,size=(round(numofmol*(1-ratiointra)*0.25),1))
initialQ2 = np.concatenate((initialQ2x,initialQ2y),axis=1)
initialQ3x = numpy.random.uniform(low=-1.5,high=-radius,size=(round(numofmol*(1-ratiointra)*0.25),1))
initialQ3y = numpy.random.uniform(low=-1.5,high=-vradius,size=(round(numofmol*(1-ratiointra)*0.25),1))
initialQ3 = np.concatenate((initialQ3x,initialQ3y),axis=1)
initialQ4x = numpy.random.uniform(low=radius,high=1.5,size=(round(numofmol*(1-ratiointra)*0.25),1))
initialQ4y = numpy.random.uniform(low=-1.5,high=-vradius,size=(round(numofmol*(1-ratiointra)*0.25),1))
initialQ4 = np.concatenate((initialQ4x,initialQ4y),axis=1)
initialQcomb = np.concatenate((initialQ1,initialQ2,initialQ3,initialQ4),axis=0)
initial = np.concatenate((initialinVStick,initialinHStick,initialQcomb),axis=0)
print("Initial positions generated.")
# There appear to be no issues with the initial positions.
# In[39]:
where = np.zeros(numofmol) # initialize where to a default value
for i in range(numofmol):
# print("mol #",i)
# print(initial[i,:])
[traj[:,:,i],where[i],initialrot[i,:]] = brownian_singlestep_cross(initial[i,:], steps, dt, delta,radius,vradius,theta)
# In[40]:
def make_horiz_line(theta,vradius):
x = np.linspace(-2,2,100)
ylow = np.zeros((100,))
yhigh = np.zeros((100,))
ylow[:] = -vradius
yhigh[:] = vradius
line3=np.c_[x,ylow]
line4=np.c_[x,yhigh]
c, s = np.cos(theta), np.sin(theta)
rot = np.array(((c, -s), (s, c)))
line3rot = line3 @ rot
line4rot = line4 @ rot
return line3rot, line4rot
line3, line4 = make_horiz_line(theta,vradius)
# In[41]:
# get_ipython().run_line_magic('matplotlib', 'inline')
fig = plt.figure()
ax = fig.add_subplot(111)
filenames = []
print("Generating first visual...")
for i in range(steps):
# print(i)
for j in range(numofmol):
new_y = traj[1,:i,j]
new_x = traj[0,:i,j]
if i == 0:
ax.scatter(traj[0,0,j],traj[1,0,j],color='black')
#elif i==steps-1:
#ax.scatter(traj[0,i,j],traj[1,i,j])
if where[j] == 1:
ax.plot(new_x,new_y,'deepskyblue')
else:
ax.plot(new_x,new_y,'lightskyblue')
ax.plot(line1[:,0],line1[:,1],color='black')
ax.plot(line2[:,0],line2[:,1],color='black')
ax.plot(line3[:,0],line3[:,1],color='black')
ax.plot(line4[:,0],line4[:,1],color='black')
plt.xlim((-1.5, 1.5)) # set the xlim to left, right
plt.ylim((-1.5, 1.5))
plt.savefig(f'test_{i}.png')
filenames.append(f'test_{i}.png')
#ax.clear()
#display(fig)
# clear_output(wait=True)
else:
print("Invalid number of sticks. Please enter 1 or 2.")
# In[42]:
print("Outputting first visual...")
with imageio.get_writer(f'{numofmol}mol_{theta}deg.gif', mode='I') as writer:
for filename in filenames:
image = imageio.imread(filename)
writer.append_data(image)
os.remove(filename) # delete the file after appending its data
# # Now plot all points from the origin (universal)
# In[43]:
origtraj = np.zeros((steps,2,numofmol))
for i in range(numofmol):
origtraj[:,0,i] = traj[0,:,i] - initialrot[i,0]
origtraj[:,1,i] = traj[1,:,i] - initialrot[i,1]
# In[44]:
# get_ipython().run_line_magic('matplotlib', 'inline')
fig = plt.figure()
ax = fig.add_subplot(111)
filenames = []
print("Generating origin visual...")
for i in range(steps):
# print(i)
for j in range(numofmol):
new_y = origtraj[:i,1,j]
new_x = origtraj[:i,0,j]
#ax.plot(new_x,new_y,'b')
if where[j] == 1:
ax.plot(new_x,new_y,'deepskyblue')
else:
ax.plot(new_x,new_y,'lightskyblue')
plt.xlim((-1.5, 1.5)) # set the xlim to left, right
plt.ylim((-1.5, 1.5))
plt.savefig(f'test_{i}.png')
filenames.append(f'test_{i}.png')
#ax.clear()
#display(fig)
# clear_output(wait=True)
# In[45]:
print("Outputting origin visual...")
import imageio.v2 as imageio
with imageio.get_writer(f'{numofmol}mol_{theta}degstick_origin.gif', mode='I') as writer:
for filename in filenames:
image = imageio.imread(filename)
writer.append_data(image)
os.remove(filename)
# # Trying 3D
# In[46]:
"""
brownian() implements one dimensional Brownian motion (i.e. the Wiener process).
"""
from math import sqrt
from scipy.stats import norm
import numpy as np
def brownian_singlestep(x0, n, dt, delta, radius, theta, out=None):
"""
Generate an instance of Brownian motion (i.e. the Wiener process):
X(t) = X(0) + N(0, delta**2 * t; 0, t)
where N(a,b; t0, t1) is a normally distributed random variable with mean a and
variance b. The parameters t0 and t1 make explicit the statistical
independence of N on different time intervals; that is, if [t0, t1) and
[t2, t3) are disjoint intervals, then N(a, b; t0, t1) and N(a, b; t2, t3)
are independent.
Written as an iteration scheme,
X(t + dt) = X(t) + N(0, delta**2 * dt; t, t+dt)
If `x0` is an array (or array-like), each value in `x0` is treated as
an initial condition, and the value returned is a numpy array with one
more dimension than `x0`.
Arguments
---------
x0 : float or numpy array (or something that can be converted to a numpy array
using numpy.asarray(x0)).
The initial condition(s) (i.e. position(s)) of the Brownian motion.
n : int
The number of steps to take.
dt : float
The time step.
delta : float
delta determines the "speed" of the Brownian motion. The random variable
of the position at time t, X(t), has a normal distribution whose mean is
the position at time t=0 and whose variance is delta**2*t.
out : numpy array or None
If `out` is not None, it specifies the array in which to put the
result. If `out` is None, a new numpy array is created and returned.
Returns
-------
A numpy array of floats with shape `x0.shape + (n,)`.
Note that the initial value `x0` is not included in the returned array.
"""
x0 = np.asarray(x0)
# For each element of x0, generate a sample of n numbers from a
# normal distribution.
coordhold = np.copy(x0)
coordfinal = np.zeros((3,n))
#coordfinal = []
#c, s = np.cos(theta), np.sin(theta)
#rot = np.array(((c, -s), (s, c)))
if x0[0] < -radius:
print('less than -')
for i in range(n):
#print(i)
r = norm.rvs(size=(3,),scale=delta*sqrt(dt))
if (r[0] + coordhold[0]) < -radius:
coordhold += r
coordfinal[:,i] = coordhold
else:
while (r[0] + coordhold[0]) > -radius:
r = norm.rvs(size=(3,),scale=delta*sqrt(dt))
coordhold += r
coordfinal[:,i] = coordhold
elif x0[0] > radius:
print('greater than +')
for i in range(n):
#print(i)
r = norm.rvs(size=(3,),scale=delta*sqrt(dt))
if (r[0] + coordhold[0]) > radius:
coordhold += r
coordfinal[:,i] = coordhold
else:
while (r[0] + coordhold[0]) < radius:
r = norm.rvs(size=(3,),scale=delta*sqrt(dt))
coordhold += r
coordfinal[:,i] = coordhold
else:
print('inside')
for i in range(n):
#print(i)
r = norm.rvs(size=(3,),scale=delta*sqrt(dt))
if (r[0] + coordhold[0]) > -radius and (r[0] + coordhold[0]) < radius and (r[1] + coordhold[1]) > -radius and (r[1] + coordhold[1]) < radius:
coordhold += r
coordfinal[:,i] = coordhold
else:
while (r[0] + coordhold[0]) < -radius or (r[0] + coordhold[0]) > radius:
r = norm.rvs(size=(3,),scale=delta*sqrt(dt))
coordhold += r
coordfinal[:,i] = coordhold
coordfinal = np.squeeze(np.array(coordfinal))
# Add the initial condition.
#coordfinal += np.expand_dims(x0, axis=-1)
#coordfinal = rot @ coordfinal
return coordfinal