Skip to content

Commit 567f551

Browse files
committed
Copied over to Python files and loaded into notebook.
1 parent df3fea2 commit 567f551

2 files changed

Lines changed: 163 additions & 89 deletions

File tree

fdm-devito-notebooks/03_diffu/diffu_rw.ipynb

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -243,26 +243,37 @@
243243
},
244244
{
245245
"cell_type": "code",
246-
"execution_count": 303,
246+
"execution_count": 1,
247247
"metadata": {},
248248
"outputs": [],
249249
"source": [
250-
"import random, numpy as np\n",
251-
"\n",
250+
"import random, numpy as np"
251+
]
252+
},
253+
{
254+
"cell_type": "code",
255+
"execution_count": 21,
256+
"metadata": {},
257+
"outputs": [],
258+
"source": [
259+
"# %load -s random_walk1D, src-diffu/random_walk.py\n",
252260
"def random_walk1D(x0, N, p, random=random):\n",
253-
" \"\"\"1D random walk with 1 particle.\"\"\"\n",
261+
" \"\"\"1D random walk with 1 particle and N moves.\"\"\"\n",
262+
" # random is argument so we can use np.random instead\n",
263+
" # and use it for testing\n",
264+
"\n",
254265
" # Store position in step k in position[k]\n",
255-
" position = np.zeros(N)\n",
266+
" position = np.zeros(N+1)\n",
256267
" position[0] = x0\n",
257268
" current_pos = x0\n",
258-
" for k in range(N-1):\n",
269+
" for k in range(N):\n",
259270
" r = random.uniform(0, 1)\n",
260271
" if r <= p:\n",
261272
" current_pos -= 1\n",
262273
" else:\n",
263274
" current_pos += 1\n",
264275
" position[k+1] = current_pos\n",
265-
" return position"
276+
" return position\n"
266277
]
267278
},
268279
{
@@ -287,7 +298,7 @@
287298
},
288299
{
289300
"cell_type": "code",
290-
"execution_count": 295,
301+
"execution_count": 22,
291302
"metadata": {},
292303
"outputs": [],
293304
"source": [
@@ -306,7 +317,7 @@
306317
},
307318
{
308319
"cell_type": "code",
309-
"execution_count": 296,
320+
"execution_count": 23,
310321
"metadata": {},
311322
"outputs": [],
312323
"source": [
@@ -315,12 +326,13 @@
315326
},
316327
{
317328
"cell_type": "code",
318-
"execution_count": 304,
329+
"execution_count": 24,
319330
"metadata": {},
320331
"outputs": [],
321332
"source": [
333+
"# %load -s random_walk1D_vec, src-diffu/random_walk.py\n",
322334
"def random_walk1D_vec(x0, N, p, random=random):\n",
323-
" \"\"\"1D random walk with 1 particle.\"\"\"\n",
335+
" \"\"\"Vectorized version of random_walk1D.\"\"\"\n",
324336
" # Create and initialise r with dimension x_d\n",
325337
" x_d = Dimension(name='x_d', spacing=Constant('h_x'))\n",
326338
" r = TimeFunction(name='r', dimensions=(x_d,), shape=(N+1,))\n",
@@ -334,7 +346,7 @@
334346
" eq = Eq(r.forward, r + steps)\n",
335347
" op = Operator(eq)\n",
336348
" op.apply()\n",
337-
" return r.data"
349+
" return r.data\n"
338350
]
339351
},
340352
{
@@ -671,15 +683,14 @@
671683
},
672684
{
673685
"cell_type": "code",
674-
"execution_count": 283,
686+
"execution_count": 25,
675687
"metadata": {},
676688
"outputs": [],
677689
"source": [
678-
"from devito import Grid\n",
679-
"\n",
680-
"def random_walks1D(x0, N, p, num_walks=1, num_times=1, random=random):\n",
690+
"# %load -s random_walks1D_vec, src-diffu/random_walk.py\n",
691+
"def random_walks1D_vec(x0, N, p, num_walks=1, num_times=1, random=random):\n",
681692
" \"\"\"Vectorized version of random_walks1D.\"\"\"\n",
682-
" position = np.zeros(N+1) # Accumulated positions\n",
693+
" position = np.zeros(N+1) # Accumulated positions\n",
683694
" position2 = np.zeros(N+1) # Accumulated positions**2\n",
684695
" # Histogram at num_times selected time points\n",
685696
" pos_hist = np.zeros((num_walks, num_times))\n",
@@ -711,7 +722,7 @@
711722
" \n",
712723
" pos_hist[:,:] = np.transpose(r.data[pos_hist_times,:])\n",
713724
" \n",
714-
" return position, position2, pos_hist, np.array(pos_hist_times)"
725+
" return position, position2, pos_hist, np.array(pos_hist_times)\n"
715726
]
716727
},
717728
{
@@ -754,7 +765,7 @@
754765
},
755766
{
756767
"cell_type": "code",
757-
"execution_count": 297,
768+
"execution_count": 26,
758769
"metadata": {},
759770
"outputs": [],
760771
"source": [
@@ -779,7 +790,7 @@
779790
},
780791
{
781792
"cell_type": "code",
782-
"execution_count": 298,
793+
"execution_count": 27,
783794
"metadata": {},
784795
"outputs": [
785796
{
@@ -814,6 +825,8 @@
814825
"run, and it can be very difficult to spot bugs through averaged statistical\n",
815826
"quantities.\n",
816827
"\n",
828+
"A full test suite for this notebook, along with the rest of the functions for random walks, can be found in [`random_walk.py`](src-diffu/random_walk.py).\n",
829+
"\n",
817830
"## Demonstration of multiple walks\n",
818831
"\n",
819832
"Assuming now that the code works, we can just scale up the number of\n",
@@ -1084,16 +1097,17 @@
10841097
},
10851098
{
10861099
"cell_type": "code",
1087-
"execution_count": 299,
1100+
"execution_count": null,
10881101
"metadata": {},
10891102
"outputs": [],
10901103
"source": [
1104+
"# %load -s random_walk2D, src-diffu/random_walk.py\n",
10911105
"def random_walk2D(x0, N, p, random=random):\n",
10921106
" \"\"\"2D random walk with 1 particle and N moves: N, E, W, S.\"\"\"\n",
10931107
" # Store position in step k in position[k]\n",
10941108
" d = len(x0)\n",
10951109
" position = np.zeros((N+1, d))\n",
1096-
" position[0,:] = x0\n",
1110+
" position[0, :] = x0\n",
10971111
" current_pos = np.array(x0, dtype=float)\n",
10981112
" for k in range(N):\n",
10991113
" r = random.uniform(0, 1)\n",
@@ -1105,8 +1119,8 @@
11051119
" current_pos += np.array([0, -1]) # Move south\n",
11061120
" else:\n",
11071121
" current_pos += np.array([-1, 0]) # Move west\n",
1108-
" position[k+1,:] = current_pos\n",
1109-
" return position"
1122+
" position[k+1, :] = current_pos\n",
1123+
" return position\n"
11101124
]
11111125
},
11121126
{
@@ -1144,16 +1158,17 @@
11441158
},
11451159
{
11461160
"cell_type": "code",
1147-
"execution_count": 301,
1161+
"execution_count": null,
11481162
"metadata": {},
11491163
"outputs": [],
11501164
"source": [
1165+
"# %load -s random_walkdD, src-diffu/random_walk.py\n",
11511166
"def random_walkdD(x0, N, p, random=random):\n",
11521167
" \"\"\"Any-D (diagonal) random walk with 1 particle and N moves.\"\"\"\n",
11531168
" # Store position in step k in position[k]\n",
11541169
" d = len(x0)\n",
11551170
" position = np.zeros((N+1, d))\n",
1156-
" position[0,:] = x0\n",
1171+
" position[0, :] = x0\n",
11571172
" current_pos = np.array(x0, dtype=float)\n",
11581173
" for k in range(N):\n",
11591174
" for i in range(d):\n",
@@ -1162,8 +1177,8 @@
11621177
" current_pos[i] -= 1\n",
11631178
" else:\n",
11641179
" current_pos[i] += 1\n",
1165-
" position[k+1,:] = current_pos\n",
1166-
" return position"
1180+
" position[k+1, :] = current_pos\n",
1181+
" return position\n"
11671182
]
11681183
},
11691184
{
@@ -1175,11 +1190,14 @@
11751190
},
11761191
{
11771192
"cell_type": "code",
1178-
"execution_count": 305,
1193+
"execution_count": null,
11791194
"metadata": {},
11801195
"outputs": [],
11811196
"source": [
1197+
"# %load -s random_walkdD_vec, src-diffu/random_walk.py\n",
11821198
"def random_walkdD_vec(x0, N, p, random=random):\n",
1199+
" \"\"\"Vectorized version of random_walkdD.\"\"\"\n",
1200+
"\n",
11831201
" # Here, x0 is an array of initial values in each spatial dimension\n",
11841202
" d = len(x0)\n",
11851203
" \n",
@@ -1197,7 +1215,7 @@
11971215
" eq = Eq(r.forward, r + steps)\n",
11981216
" op = Operator(eq)\n",
11991217
" op.apply()\n",
1200-
" return r.data"
1218+
" return r.data\n"
12011219
]
12021220
},
12031221
{
@@ -1221,11 +1239,12 @@
12211239
},
12221240
{
12231241
"cell_type": "code",
1224-
"execution_count": 225,
1242+
"execution_count": null,
12251243
"metadata": {},
12261244
"outputs": [],
12271245
"source": [
1228-
"def random_walksdD_vec(x0, N, p, num_walks=1, num_times=1, random=random):\n",
1246+
"# %load -s random_walksdD_vec, src-diffu/random_walk.py\n",
1247+
"def random_walksdD_vec(x0, N, p, num_walks=1, num_times=1, random=np.random):\n",
12291248
" \"\"\"Vectorized version of random_walksdD.\"\"\"\n",
12301249
" d = len(x0)\n",
12311250
" position = np.zeros((N+1, d)) # Accumulated positions\n",
@@ -1261,7 +1280,7 @@
12611280
" for k in range(num_walks):\n",
12621281
" pos_hist[k,:] = r.data[pos_hist_times,k]\n",
12631282
" \n",
1264-
" return position, position2, pos_hist, np.array(pos_hist_times)"
1283+
" return position, position2, pos_hist, np.array(pos_hist_times)\n"
12651284
]
12661285
},
12671286
{

0 commit comments

Comments
 (0)