Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lessons/15_Step_12.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
"outputs": [],
"source": [
"def pressure_poisson_periodic(p, dx, dy):\n",
" pn = numpy.empty_like(p)\n",
" pn[:] = numpy.empty_like(p)\n",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pn doesn’t exist yet, so pn[:] = ... will raise an error

even if it did exist, you’d just be copying junk from a freshly allocated array into pn—no gain here

" \n",
" for q in range(nit):\n",
" pn = p.copy()\n",
Expand Down Expand Up @@ -313,10 +313,10 @@
"stepcount = 0\n",
"\n",
"while udiff > .001:\n",
" un = u.copy()\n",
" vn = v.copy()\n",
" un[:] = u.copy()\n",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you allocate a temporary copy and then copy it again into un/vn. Double work.

Would suggest this instead (fastest without allocation):

un[...] = u          # or: numpy.copyto(un, u)
vn[...] = v          # or: numpy.copyto(vn, v)

" vn[:] = v.copy()\n",
"\n",
" b = build_up_b(rho, dt, dx, dy, u, v)\n",
" b[:] = build_up_b(rho, dt, dx, dy, u, v)\n",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build_up_b still allocates a new array internally and returns it; then you copy that result into b. That’s an extra full-array copy every iteration

to improve the performance here you would need to refactor build_up_b to write in-place into a provided buffer, something like:

def build_up_b_out(b, rho, dt, dx, dy, u, v):
    b[1:-1, 1:-1] = ...
    b[1:-1, -1]   = ...
    b[1:-1, 0]    = ...

" p = pressure_poisson_periodic(p, dx, dy)\n",
"\n",
" u[1:-1, 1:-1] = (un[1:-1, 1:-1] -\n",
Expand Down