|
27 | 27 | "cell_type": "markdown", |
28 | 28 | "metadata": {}, |
29 | 29 | "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." |
31 | 31 | ] |
32 | 32 | }, |
33 | 33 | { |
|
77 | 77 | "source": [ |
78 | 78 | "u = numpy.array((0, 1, 2, 3, 4, 5))\n", |
79 | 79 | "\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])" |
82 | 82 | ] |
83 | 83 | }, |
84 | 84 | { |
|
107 | 107 | } |
108 | 108 | ], |
109 | 109 | "source": [ |
110 | | - "u[1:]-u[0:-1]" |
| 110 | + "u[1:] - u[0:-1]" |
111 | 111 | ] |
112 | 112 | }, |
113 | 113 | { |
|
116 | 116 | "source": [ |
117 | 117 | "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", |
118 | 118 | "\n", |
119 | | - "###Speed Increases\n", |
| 119 | + "### Speed Increases\n", |
120 | 120 | "\n", |
121 | 121 | "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" |
122 | 122 | ] |
|
133 | 133 | "ny = 81\n", |
134 | 134 | "nt = 100\n", |
135 | 135 | "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", |
138 | 138 | "sigma = .2\n", |
139 | | - "dt = sigma*dx\n", |
| 139 | + "dt = sigma * dx\n", |
140 | 140 | "\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", |
143 | 143 | "\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", |
146 | 146 | "\n", |
147 | 147 | "###Assign initial conditions\n", |
148 | 148 | "\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" |
150 | 150 | ] |
151 | 151 | }, |
152 | 152 | { |
|
169 | 169 | "name": "stdout", |
170 | 170 | "output_type": "stream", |
171 | 171 | "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" |
173 | 173 | ] |
174 | 174 | } |
175 | 175 | ], |
176 | 176 | "source": [ |
177 | 177 | "%%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", |
180 | 180 | "\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", |
182 | 182 | " un = u.copy()\n", |
183 | 183 | " row, col = u.shape\n", |
184 | 184 | " for j in range(1, row):\n", |
185 | 185 | " 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" |
191 | 194 | ] |
192 | 195 | }, |
193 | 196 | { |
194 | 197 | "cell_type": "markdown", |
195 | 198 | "metadata": {}, |
196 | 199 | "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:" |
198 | 201 | ] |
199 | 202 | }, |
200 | 203 | { |
|
208 | 211 | "name": "stdout", |
209 | 212 | "output_type": "stream", |
210 | 213 | "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" |
212 | 215 | ] |
213 | 216 | } |
214 | 217 | ], |
215 | 218 | "source": [ |
216 | 219 | "%%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", |
219 | 222 | "\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", |
221 | 224 | " 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" |
227 | 231 | ] |
228 | 232 | }, |
229 | 233 | { |
230 | 234 | "cell_type": "markdown", |
231 | 235 | "metadata": {}, |
232 | 236 | "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. " |
234 | 238 | ] |
235 | 239 | }, |
236 | 240 | { |
|
0 commit comments