Skip to content

Commit c2443bc

Browse files
committed
Cleaned up notebook.
1 parent 0bc2014 commit c2443bc

1 file changed

Lines changed: 47 additions & 24 deletions

File tree

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

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,40 @@
236236
"cell_type": "markdown",
237237
"metadata": {},
238238
"source": [
239-
"This implementation is scalar, which is slow for large values of N. Thankfully, Devito gives us a simple way of implementing this using vectorization. Read on!"
239+
"A random walk with $N$ steps, starting at $x_0$, where we move\n",
240+
"to the left with probability $p$ and to the right\n",
241+
"with probability $1-p$ can now be implemented by"
242+
]
243+
},
244+
{
245+
"cell_type": "code",
246+
"execution_count": 303,
247+
"metadata": {},
248+
"outputs": [],
249+
"source": [
250+
"import random, numpy as np\n",
251+
"\n",
252+
"def random_walk1D(x0, N, p, random=random):\n",
253+
" \"\"\"1D random walk with 1 particle.\"\"\"\n",
254+
" # Store position in step k in position[k]\n",
255+
" position = np.zeros(N)\n",
256+
" position[0] = x0\n",
257+
" current_pos = x0\n",
258+
" for k in range(N-1):\n",
259+
" r = random.uniform(0, 1)\n",
260+
" if r <= p:\n",
261+
" current_pos -= 1\n",
262+
" else:\n",
263+
" current_pos += 1\n",
264+
" position[k+1] = current_pos\n",
265+
" return position"
240266
]
241267
},
242268
{
243269
"cell_type": "markdown",
244270
"metadata": {},
245271
"source": [
272+
"\n",
246273
"Since $N$ is supposed to be large and we want to repeat the process for\n",
247274
"many particles, we should speed up the code as much as possible.\n",
248275
"Vectorization is the obvious technique here: we draw all the random\n",
@@ -260,12 +287,10 @@
260287
},
261288
{
262289
"cell_type": "code",
263-
"execution_count": 4,
290+
"execution_count": 295,
264291
"metadata": {},
265292
"outputs": [],
266293
"source": [
267-
"import random, numpy as np\n",
268-
"\n",
269294
"N=200\n",
270295
"p=50\n",
271296
"r = np.random.uniform(0, 1, size=N)\n",
@@ -276,14 +301,12 @@
276301
"cell_type": "markdown",
277302
"metadata": {},
278303
"source": [
279-
"A random walk with $N$ steps, starting at $x_0$, where we move\n",
280-
"to the left with probability $p$ and to the right\n",
281-
"with probability $1-p$ can now be implemented by"
304+
"Our vectorized implementation using Devito is as follows:"
282305
]
283306
},
284307
{
285308
"cell_type": "code",
286-
"execution_count": 5,
309+
"execution_count": 296,
287310
"metadata": {},
288311
"outputs": [],
289312
"source": [
@@ -292,13 +315,13 @@
292315
},
293316
{
294317
"cell_type": "code",
295-
"execution_count": 64,
318+
"execution_count": 304,
296319
"metadata": {},
297320
"outputs": [],
298321
"source": [
299-
"def random_walk1D(x0, N, p, random=random):\n",
322+
"def random_walk1D_vec(x0, N, p, random=random):\n",
300323
" \"\"\"1D random walk with 1 particle.\"\"\"\n",
301-
" # Create and initialise r\n",
324+
" # Create and initialise r with dimension x_d\n",
302325
" x_d = Dimension(name='x_d', spacing=Constant('h_x'))\n",
303326
" r = TimeFunction(name='r', dimensions=(x_d,), shape=(N+1,))\n",
304327
" r.data[0] = x0\n",
@@ -329,7 +352,7 @@
329352
},
330353
{
331354
"cell_type": "code",
332-
"execution_count": 5,
355+
"execution_count": 259,
333356
"metadata": {},
334357
"outputs": [],
335358
"source": [
@@ -648,7 +671,7 @@
648671
},
649672
{
650673
"cell_type": "code",
651-
"execution_count": 216,
674+
"execution_count": 283,
652675
"metadata": {},
653676
"outputs": [],
654677
"source": [
@@ -731,7 +754,7 @@
731754
},
732755
{
733756
"cell_type": "code",
734-
"execution_count": 39,
757+
"execution_count": 297,
735758
"metadata": {},
736759
"outputs": [],
737760
"source": [
@@ -740,14 +763,14 @@
740763
" # produce the same result\n",
741764
" x0 = 0; N = 4; p = 0.5\n",
742765
"\n",
743-
" # First, check that random_walks1D for 1 walk reproduces\n",
766+
" # Check that random_walks1D for 1 walk reproduces\n",
744767
" # the walk in random_walk1D\n",
745768
" num_walks = 1\n",
746769
" np.random.seed(10)\n",
747770
" computed = random_walks1D(\n",
748771
" x0, N, p, num_walks, random=np.random)\n",
749772
" np.random.seed(10)\n",
750-
" expected = random_walk1D(\n",
773+
" expected = random_walk1D_vec(\n",
751774
" x0, N, p, random=np.random)\n",
752775
" print(\"Computed: \", computed[0])\n",
753776
" print(\"Expected: \", expected)\n",
@@ -756,7 +779,7 @@
756779
},
757780
{
758781
"cell_type": "code",
759-
"execution_count": 40,
782+
"execution_count": 298,
760783
"metadata": {},
761784
"outputs": [
762785
{
@@ -857,7 +880,7 @@
857880
},
858881
{
859882
"cell_type": "code",
860-
"execution_count": 43,
883+
"execution_count": 239,
861884
"metadata": {},
862885
"outputs": [],
863886
"source": [
@@ -1061,7 +1084,7 @@
10611084
},
10621085
{
10631086
"cell_type": "code",
1064-
"execution_count": 211,
1087+
"execution_count": 299,
10651088
"metadata": {},
10661089
"outputs": [],
10671090
"source": [
@@ -1121,7 +1144,7 @@
11211144
},
11221145
{
11231146
"cell_type": "code",
1124-
"execution_count": 212,
1147+
"execution_count": 301,
11251148
"metadata": {},
11261149
"outputs": [],
11271150
"source": [
@@ -1152,7 +1175,7 @@
11521175
},
11531176
{
11541177
"cell_type": "code",
1155-
"execution_count": 219,
1178+
"execution_count": 305,
11561179
"metadata": {},
11571180
"outputs": [],
11581181
"source": [
@@ -1163,11 +1186,11 @@
11631186
" x_d = Dimension(name='x_d', spacing=Constant('h_x'))\n",
11641187
" # Add space dimensions\n",
11651188
" d_d = Dimension(name='d', spacing=Constant('h_d'))\n",
1166-
" r = TimeFunction(name='r', dimensions=(x_d,d_d), shape=(N+1,d))\n",
1189+
" r = TimeFunction(name='r', dimensions=(x_d, d_d), shape=(N+1, d))\n",
11671190
" r.data[0] = x0\n",
11681191
" \n",
11691192
" rs = random.uniform(0, 1, size=N*d).reshape(N,d)\n",
1170-
" steps = Function(name='steps', dimensions=(x_d,d_d), shape=(N+1,d))\n",
1193+
" steps = Function(name='steps', dimensions=(x_d, d_d), shape=(N+1, d))\n",
11711194
" steps.data[:N] = np.where(rs <= p, -1, 1)\n",
11721195
" \n",
11731196
" # The next value is computed using the previous value and the steps function at that point\n",
@@ -1198,7 +1221,7 @@
11981221
},
11991222
{
12001223
"cell_type": "code",
1201-
"execution_count": 218,
1224+
"execution_count": 225,
12021225
"metadata": {},
12031226
"outputs": [],
12041227
"source": [

0 commit comments

Comments
 (0)