Skip to content

Commit 0cccc8b

Browse files
committed
pep8 updates and update colormaps and style
1 parent f28e7e2 commit 0cccc8b

10 files changed

+755
-535
lines changed

lessons/06_Array_Operations_with_NumPy.ipynb

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"cell_type": "markdown",
2828
"metadata": {},
2929
"source": [
30-
"This lesson complements the first interactive module of the online [CFD Python](https://bitbucket.org/cfdpython/cfd-python-class) class, by Prof. Lorena A. Barba, called **12 Steps to Navier-Stokes.** It was written with BU graduate student Gilbert Forsyth."
30+
"This lesson complements the first interactive module of the online [CFD Python](https://github.com/barbagroup/CFDPython) class, by Prof. Lorena A. Barba, called **12 Steps to Navier-Stokes.** It was written with BU graduate student Gilbert Forsyth."
3131
]
3232
},
3333
{
@@ -77,8 +77,8 @@
7777
"source": [
7878
"u = numpy.array((0, 1, 2, 3, 4, 5))\n",
7979
"\n",
80-
"for i in range(1,len(u)):\n",
81-
" print(u[i]-u[i-1])"
80+
"for i in range(1, len(u)):\n",
81+
" print(u[i] - u[i-1])"
8282
]
8383
},
8484
{
@@ -107,7 +107,7 @@
107107
}
108108
],
109109
"source": [
110-
"u[1:]-u[0:-1]"
110+
"u[1:] - u[0:-1]"
111111
]
112112
},
113113
{
@@ -116,7 +116,7 @@
116116
"source": [
117117
"What this command says is subtract the 0th, 1st, 2nd, 3rd, 4th and 5th elements of $u$ from the 1st, 2nd, 3rd, 4th, 5th and 6th elements of $u$. \n",
118118
"\n",
119-
"###Speed Increases\n",
119+
"### Speed Increases\n",
120120
"\n",
121121
"For a 6 element array, the benefits of array operations are pretty slim. There will be no appreciable difference in execution time because there are so few operations taking place. But if we revisit 2D linear convection, we can see some substantial speed increases. \n"
122122
]
@@ -133,20 +133,20 @@
133133
"ny = 81\n",
134134
"nt = 100\n",
135135
"c = 1\n",
136-
"dx = 2/(nx-1)\n",
137-
"dy = 2/(ny-1)\n",
136+
"dx = 2 / (nx - 1)\n",
137+
"dy = 2 / (ny - 1)\n",
138138
"sigma = .2\n",
139-
"dt = sigma*dx\n",
139+
"dt = sigma * dx\n",
140140
"\n",
141-
"x = numpy.linspace(0,2,nx)\n",
142-
"y = numpy.linspace(0,2,ny)\n",
141+
"x = numpy.linspace(0, 2, nx)\n",
142+
"y = numpy.linspace(0, 2, ny)\n",
143143
"\n",
144-
"u = numpy.ones((ny,nx)) ##create a 1xn vector of 1's\n",
145-
"un = numpy.ones((ny,nx)) ##\n",
144+
"u = numpy.ones((ny, nx)) ##create a 1xn vector of 1's\n",
145+
"un = numpy.ones((ny, nx)) \n",
146146
"\n",
147147
"###Assign initial conditions\n",
148148
"\n",
149-
"u[.5/dy:1/dy+1,.5/dx:1/dx+1]=2"
149+
"u[int(.5 / dy): int(1 / dy + 1), int(.5 / dx):int(1 / dx + 1)] = 2"
150150
]
151151
},
152152
{
@@ -169,32 +169,35 @@
169169
"name": "stdout",
170170
"output_type": "stream",
171171
"text": [
172-
"1 loops, best of 3: 1.83 s per loop\n"
172+
"1 loop, best of 3: 1.94 s per loop\n"
173173
]
174174
}
175175
],
176176
"source": [
177177
"%%timeit\n",
178-
"u = numpy.ones((ny,nx))\n",
179-
"u[.5/dy:1/dy+1,.5/dx:1/dx+1]=2\n",
178+
"u = numpy.ones((ny, nx))\n",
179+
"u[int(.5 / dy): int(1 / dy + 1), int(.5 / dx):int(1 / dx + 1)] = 2\n",
180180
"\n",
181-
"for n in range(nt+1): ##loop across number of time steps\n",
181+
"for n in range(nt + 1): ##loop across number of time steps\n",
182182
" un = u.copy()\n",
183183
" row, col = u.shape\n",
184184
" for j in range(1, row):\n",
185185
" for i in range(1, col):\n",
186-
" u[j,i] = un[j, i] - (c*dt/dx*(un[j,i] - un[j,i-1]))-(c*dt/dy*(un[j,i]-un[j-1,i]))\n",
187-
" u[0,:] = 1\n",
188-
" u[-1,:] = 1\n",
189-
" u[:,0] = 1\n",
190-
" u[:,-1] = 1"
186+
" u[j, i] = (un[j, i] - (c * dt / dx * \n",
187+
" (un[j, i] - un[j, i - 1])) - \n",
188+
" (c * dt / dy * \n",
189+
" (un[j, i] - un[j - 1, i])))\n",
190+
" u[0, :] = 1\n",
191+
" u[-1, :] = 1\n",
192+
" u[:, 0] = 1\n",
193+
" u[:, -1] = 1"
191194
]
192195
},
193196
{
194197
"cell_type": "markdown",
195198
"metadata": {},
196199
"source": [
197-
"With the \"raw\" Python code above, the best execution time achieved was 1.83 seconds. Keep in mind that with these three nested loops, that the statements inside the **j** loop are being evaluated more than 650,000 times. Let's compare that with the performance of the same code implemented with array operations:"
200+
"With the \"raw\" Python code above, the best execution time achieved was 1.94 seconds. Keep in mind that with these three nested loops, that the statements inside the **j** loop are being evaluated more than 650,000 times. Let's compare that with the performance of the same code implemented with array operations:"
198201
]
199202
},
200203
{
@@ -208,29 +211,30 @@
208211
"name": "stdout",
209212
"output_type": "stream",
210213
"text": [
211-
"100 loops, best of 3: 4.92 ms per loop\n"
214+
"100 loops, best of 3: 5.09 ms per loop\n"
212215
]
213216
}
214217
],
215218
"source": [
216219
"%%timeit\n",
217-
"u = numpy.ones((ny,nx))\n",
218-
"u[.5/dy:1/dy+1,.5/dx:1/dx+1]=2\n",
220+
"u = numpy.ones((ny, nx))\n",
221+
"u[int(.5 / dy): int(1 / dy + 1), int(.5 / dx):int(1 / dx + 1)] = 2\n",
219222
"\n",
220-
"for n in range(nt+1): ##loop across number of time steps\n",
223+
"for n in range(nt + 1): ##loop across number of time steps\n",
221224
" un = u.copy()\n",
222-
" u[1:,1:]=un[1:,1:]-(c*dt/dx*(un[1:,1:]-un[1:, 0:-1]))-(c*dt/dy*(un[1:,1:]-un[0:-1,1:]))\n",
223-
" u[0,:] = 1\n",
224-
" u[-1,:] = 1\n",
225-
" u[:,0] = 1\n",
226-
" u[:,-1] = 1"
225+
" u[1:, 1:] = un[1:, 1:] - ((c * dt / dx * (un[1:, 1:] - un[1:, 0:-1])) -\n",
226+
" (c * dt / dy * (un[1:, 1:] - un[0:-1, 1:])))\n",
227+
" u[0, :] = 1\n",
228+
" u[-1, :] = 1\n",
229+
" u[:, 0] = 1\n",
230+
" u[:, -1] = 1"
227231
]
228232
},
229233
{
230234
"cell_type": "markdown",
231235
"metadata": {},
232236
"source": [
233-
"As you can see, the speed increase is substantial. The same calculation goes from 1.83 seconds to 4.92 milliseconds. 3 seconds isn't a huge amount of time to wait, but these speed gains will increase exponentially with the size and complexity of the problem being evaluated. "
237+
"As you can see, the speed increase is substantial. The same calculation goes from 1.94 seconds to 5.09 milliseconds. 2 seconds isn't a huge amount of time to wait, but these speed gains will increase exponentially with the size and complexity of the problem being evaluated. "
234238
]
235239
},
236240
{

lessons/07_Step_5.ipynb

Lines changed: 56 additions & 50 deletions
Large diffs are not rendered by default.

lessons/08_Step_6.ipynb

Lines changed: 101 additions & 48 deletions
Large diffs are not rendered by default.

lessons/09_Step_7.ipynb

Lines changed: 94 additions & 61 deletions
Large diffs are not rendered by default.

lessons/10_Step_8.ipynb

Lines changed: 100 additions & 70 deletions
Large diffs are not rendered by default.

lessons/11_Defining_Function_in_Python.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"cell_type": "markdown",
2828
"metadata": {},
2929
"source": [
30-
"This lesson complements the first interactive module of the online [CFD Python](https://bitbucket.org/cfdpython/cfd-python-class) class, by Prof. Lorena A. Barba, called **12 Steps to Navier-Stokes.** The interactive module starts with simple exercises in 1D that at first use little of the power of Python. We now present some new ways of doing the same things that are more efficient and produce prettier code.\n",
30+
"This lesson complements the first interactive module of the online [CFD Python](https://github.com/barbagroup/CFDPython) class, by Prof. Lorena A. Barba, called **12 Steps to Navier-Stokes.** The interactive module starts with simple exercises in 1D that at first use little of the power of Python. We now present some new ways of doing the same things that are more efficient and produce prettier code.\n",
3131
"\n",
3232
"This lesson was written with BU graduate student Gilbert Forsyth.\n"
3333
]
@@ -357,7 +357,7 @@
357357
"name": "python",
358358
"nbconvert_exporter": "python",
359359
"pygments_lexer": "ipython3",
360-
"version": "3.4.3"
360+
"version": "3.5.2"
361361
}
362362
},
363363
"nbformat": 4,

lessons/12_Step_9.ipynb

Lines changed: 41 additions & 36 deletions
Large diffs are not rendered by default.

lessons/13_Step_10.ipynb

Lines changed: 50 additions & 40 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)