Skip to content

Commit

Permalink
examples: Start linting CFD notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
EdCaunt committed Nov 14, 2024
1 parent 8358a27 commit bcc2487
Show file tree
Hide file tree
Showing 4 changed files with 268,091 additions and 149 deletions.
38 changes: 20 additions & 18 deletions examples/cfd/01_convection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"# NBVAL_IGNORE_OUTPUT\n",
"\n",
"# Create field and assign initial conditions\n",
"u = np.empty((nx, ny))\n",
Expand Down Expand Up @@ -116,18 +116,19 @@
"for n in range(nt + 1):\n",
" # Copy previous result into a new buffer\n",
" un = u.copy()\n",
" \n",
"\n",
" # Update the new result with a 3-point stencil\n",
" u[1:, 1:] = (un[1:, 1:] - (c * dt / dy * (un[1:, 1:] - un[1:, :-1])) -\n",
" (c * dt / dx * (un[1:, 1:] - un[:-1, 1:])))\n",
"\n",
" # Apply boundary conditions. \n",
" # Apply boundary conditions.\n",
" u[0, :] = 1. # left\n",
" u[-1, :] = 1. # right\n",
" u[-1, :] = 1. # right\n",
" u[:, 0] = 1. # bottom\n",
" u[:, -1] = 1. # top\n",
" # Note that in the above expressions the NumPy index -1 corresponds to the final point of the array along the indexed dimension, \n",
" # i.e. here u[-1, :] is equivalent to u[80, :].\n"
" u[:, -1] = 1. # top\n",
" # Note that in the above expressions the NumPy index -1 corresponds to the\n",
" # final point of the array along the indexed dimension, i.e. here u[-1, :]\n",
" # is equivalent to u[80, :].\n"
]
},
{
Expand All @@ -147,7 +148,7 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"# NBVAL_IGNORE_OUTPUT\n",
"\n",
"# A small sanity check for auto-testing\n",
"assert (u[45:55, 45:55] > 1.8).all()\n",
Expand Down Expand Up @@ -197,8 +198,8 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"from devito import Grid, TimeFunction\n",
"# NBVAL_IGNORE_OUTPUT\n",
"from devito import Grid, TimeFunction # noqa\n",
"\n",
"grid = Grid(shape=(nx, ny), extent=(2., 2.))\n",
"u = TimeFunction(name='u', grid=grid)\n",
Expand Down Expand Up @@ -230,7 +231,7 @@
}
],
"source": [
"from devito import Eq\n",
"from devito import Eq # noqa\n",
"\n",
"# Specify the `interior` flag so that the stencil is only\n",
"# applied to the interior of the domain.\n",
Expand Down Expand Up @@ -264,8 +265,8 @@
}
],
"source": [
"from devito import solve\n",
"from sympy import nsimplify, pprint\n",
"from devito import solve # noqa\n",
"from sympy import nsimplify, pprint # noqa\n",
"\n",
"stencil = solve(eq, u.forward)\n",
"\n",
Expand Down Expand Up @@ -306,8 +307,8 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"from devito import Operator\n",
"# NBVAL_IGNORE_OUTPUT\n",
"from devito import Operator # noqa\n",
"\n",
"# Reset our initial condition in both buffers.\n",
"# This is required to avoid 0s propagating into\n",
Expand Down Expand Up @@ -368,7 +369,7 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"# NBVAL_IGNORE_OUTPUT\n",
"\n",
"# Reset our data field and ICs in both buffers\n",
"init_hat(field=u.data[0], dx=dx, dy=dy, value=2.)\n",
Expand All @@ -386,7 +387,8 @@
"# Now combine the BC expressions with the stencil to form operator\n",
"expressions = [Eq(u.forward, stencil)]\n",
"expressions += [bc_left, bc_right, bc_top, bc_bottom]\n",
"op = Operator(expressions=expressions, opt=None, openmp=False) # <-- Turn off performance optimisations\n",
"# `opt=None, openmp=False` turns off performance optimisations\n",
"op = Operator(expressions=expressions, opt=None, openmp=False)\n",
"op(time=nt, dt=dt)\n",
"\n",
"plot_field(u.data[0])\n",
Expand Down Expand Up @@ -507,7 +509,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
"version": "3.11.5"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
Expand Down
45 changes: 23 additions & 22 deletions examples/cfd/01_convection_revisited.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"outputs": [],
"source": [
"from examples.cfd import plot_field, init_hat, init_smooth\n",
"from examples.cfd import plot_field, init_smooth\n",
"import numpy as np\n",
"%matplotlib inline\n",
"\n",
Expand Down Expand Up @@ -79,7 +79,7 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"# NBVAL_IGNORE_OUTPUT\n",
"\n",
"# Create field and assign initial conditions\n",
"u = np.empty((nx, ny))\n",
Expand Down Expand Up @@ -110,17 +110,17 @@
"for n in range(nt + 1):\n",
" # Copy previous result into a new buffer\n",
" un = u.copy()\n",
" \n",
"\n",
" # Update the new result with a 3-point stencil\n",
" u[1:, 1:] = (un[1:, 1:] - (c * dt / dy * (un[1:, 1:] - un[1:, :-1])) -\n",
" (c * dt / dx * (un[1:, 1:] - un[:-1, 1:])))\n",
" \n",
" # Apply boundary conditions. \n",
"\n",
" # Apply boundary conditions.\n",
" # Note: -1 here is the last index in the array, not the one at x=-1 or y=-1.\n",
" u[0, :] = 1. # left\n",
" u[-1, :] = 1. # right\n",
" u[-1, :] = 1. # right\n",
" u[:, 0] = 1. # bottom\n",
" u[:, -1] = 1. # top"
" u[:, -1] = 1. # top"
]
},
{
Expand All @@ -140,7 +140,7 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"# NBVAL_IGNORE_OUTPUT\n",
"\n",
"# A small sanity check for auto-testing\n",
"assert (u[45:55, 45:55] > 1.8).all()\n",
Expand Down Expand Up @@ -181,8 +181,8 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"from devito import Grid, TimeFunction\n",
"# NBVAL_IGNORE_OUTPUT\n",
"from devito import Grid, TimeFunction # noqa\n",
"\n",
"grid = Grid(shape=(nx, ny), extent=(2., 2.))\n",
"u = TimeFunction(name='u', grid=grid)\n",
Expand Down Expand Up @@ -212,7 +212,7 @@
}
],
"source": [
"from devito import Eq\n",
"from devito import Eq # noqa\n",
"\n",
"eq = Eq(u.dt + c*u.dxl + c*u.dyl)\n",
"\n",
Expand Down Expand Up @@ -242,8 +242,8 @@
}
],
"source": [
"from devito import solve \n",
"from sympy import nsimplify, pprint\n",
"from devito import solve # noqa\n",
"from sympy import nsimplify, pprint # noqa\n",
"\n",
"stencil = solve(eq, u.forward)\n",
"\n",
Expand Down Expand Up @@ -281,8 +281,8 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"from devito import Operator\n",
"# NBVAL_IGNORE_OUTPUT\n",
"from devito import Operator # noqa\n",
"\n",
"# Reset our initial condition in both buffers.\n",
"# This is required to avoid 0s propagating into\n",
Expand All @@ -291,13 +291,13 @@
"init_smooth(field=u.data[1], dx=dx, dy=dy)\n",
"\n",
"# Apply boundary conditions.\n",
"# Note that as the u.data method is from numpy, we can use the \n",
"# -1 syntax to represent the last item in the array.\n",
"# Note that as the u.data method is from numpy, we can use the\n",
"# -1 syntax to represent the last item in the array.\n",
"u.data[:, 0, :] = 1.\n",
"u.data[:, -1, :] = 1.\n",
"u.data[:, :, 0] = 1.\n",
"u.data[:, :, -1] = 1.\n",
" \n",
"\n",
"# Create an Operator that updates the forward stencil\n",
"# point in the interior subdomain only.\n",
"op = Operator(Eq(u.forward, stencil, subdomain=grid.interior))\n",
Expand Down Expand Up @@ -343,15 +343,15 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"# NBVAL_IGNORE_OUTPUT\n",
"\n",
"# Reset our data field and ICs in both buffers\n",
"init_smooth(field=u.data[0], dx=dx, dy=dy)\n",
"init_smooth(field=u.data[1], dx=dx, dy=dy)\n",
"\n",
"# For defining BCs, we generally to explicitly set rows/columns\n",
"# in our field using an expression. We can use Devito's \"indexed\" \n",
"# notation to do this. A u in this instance is a sympy function \n",
"# in our field using an expression. We can use Devito's \"indexed\"\n",
"# notation to do this. A u in this instance is a sympy function\n",
"# we cannot use the numpy shortcut u[-1] to refer to the last location:\n",
"x, y = grid.dimensions\n",
"t = grid.stepping_dim\n",
Expand All @@ -363,7 +363,8 @@
"# Now combine the BC expressions with the stencil to form operator.\n",
"expressions = [Eq(u.forward, stencil, subdomain=grid.interior)]\n",
"expressions += [bc_left, bc_right, bc_top, bc_bottom]\n",
"op = Operator(expressions=expressions, opt=None, openmp=False) # <-- Turn off performance optimisations\n",
"# `opt=None, openmp=False` turns off performance optimisations\n",
"op = Operator(expressions=expressions, opt=None, openmp=False)\n",
"op(time=nt, dt=dt)\n",
"\n",
"plot_field(u.data[0])\n",
Expand Down
47 changes: 24 additions & 23 deletions examples/cfd/02_convection_nonlinear.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"source": [
"from examples.cfd import plot_field, init_hat\n",
"import numpy as np\n",
"import sympy\n",
"%matplotlib inline\n",
"\n",
"# Some variable declarations\n",
Expand Down Expand Up @@ -74,7 +73,7 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"# NBVAL_IGNORE_OUTPUT\n",
"\n",
"# Allocate fields and assign initial conditions\n",
"u = np.empty((nx, ny))\n",
Expand Down Expand Up @@ -110,27 +109,27 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"for n in range(nt + 1): ##loop across number of time steps\n",
"# NBVAL_IGNORE_OUTPUT\n",
"for n in range(nt + 1): # Loop across number of time steps\n",
" un = u.copy()\n",
" vn = v.copy()\n",
" u[1:, 1:] = (un[1:, 1:] - \n",
" (un[1:, 1:] * c * dt / dy * (un[1:, 1:] - un[1:, :-1])) -\n",
" vn[1:, 1:] * c * dt / dx * (un[1:, 1:] - un[:-1, 1:]))\n",
" v[1:, 1:] = (vn[1:, 1:] -\n",
" (un[1:, 1:] * c * dt / dy * (vn[1:, 1:] - vn[1:, :-1])) -\n",
" vn[1:, 1:] * c * dt / dx * (vn[1:, 1:] - vn[:-1, 1:]))\n",
" \n",
" u[1:, 1:] = (un[1:, 1:]\n",
" - (un[1:, 1:] * c * dt / dy * (un[1:, 1:] - un[1:, :-1]))\n",
" - vn[1:, 1:] * c * dt / dx * (un[1:, 1:] - un[:-1, 1:]))\n",
" v[1:, 1:] = (vn[1:, 1:]\n",
" - (un[1:, 1:] * c * dt / dy * (vn[1:, 1:] - vn[1:, :-1]))\n",
" - vn[1:, 1:] * c * dt / dx * (vn[1:, 1:] - vn[:-1, 1:]))\n",
"\n",
" u[0, :] = 1\n",
" u[-1, :] = 1\n",
" u[:, 0] = 1\n",
" u[:, -1] = 1\n",
" \n",
"\n",
" v[0, :] = 1\n",
" v[-1, :] = 1\n",
" v[:, 0] = 1\n",
" v[:, -1] = 1\n",
" \n",
"\n",
"plot_field(u)"
]
},
Expand Down Expand Up @@ -160,8 +159,8 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"from devito import Grid, TimeFunction\n",
"# NBVAL_IGNORE_OUTPUT\n",
"from devito import Grid, TimeFunction # noqa\n",
"\n",
"# First we need two time-dependent data fields, both initialized with the hat function\n",
"grid = Grid(shape=(nx, ny), extent=(2., 2.))\n",
Expand Down Expand Up @@ -200,7 +199,7 @@
}
],
"source": [
"from devito import Eq, solve\n",
"from devito import Eq, solve # noqa\n",
"\n",
"eq_u = Eq(u.dt + u*u.dxl + v*u.dyl)\n",
"eq_v = Eq(v.dt + u*v.dxl + v*v.dyl)\n",
Expand Down Expand Up @@ -231,6 +230,7 @@
"source": [
"x, y = grid.dimensions\n",
"t = grid.stepping_dim\n",
"\n",
"bc_u = [Eq(u[t+1, 0, y], 1.)] # left\n",
"bc_u += [Eq(u[t+1, nx-1, y], 1.)] # right\n",
"bc_u += [Eq(u[t+1, x, ny-1], 1.)] # top\n",
Expand Down Expand Up @@ -272,8 +272,8 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"from devito import Operator\n",
"# NBVAL_IGNORE_OUTPUT\n",
"from devito import Operator # noqa\n",
"\n",
"# Reset our data field and ICs\n",
"init_hat(field=u.data[0], dx=dx, dy=dy, value=2.)\n",
Expand Down Expand Up @@ -313,7 +313,7 @@
}
],
"source": [
"from devito import VectorTimeFunction, grad\n",
"from devito import VectorTimeFunction, grad # noqa\n",
"\n",
"U = VectorTimeFunction(name='U', grid=grid)\n",
"init_hat(field=U[0].data[0], dx=dx, dy=dy, value=2.)\n",
Expand Down Expand Up @@ -370,6 +370,7 @@
"source": [
"x, y = grid.dimensions\n",
"t = grid.stepping_dim\n",
"\n",
"bc_u = [Eq(U[0][t+1, 0, y], 1.)] # left\n",
"bc_u += [Eq(U[0][t+1, nx-1, y], 1.)] # right\n",
"bc_u += [Eq(U[0][t+1, x, ny-1], 1.)] # top\n",
Expand Down Expand Up @@ -453,7 +454,7 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"# NBVAL_IGNORE_OUTPUT\n",
"op = Operator([update_U] + bc_u + bc_v)\n",
"op(time=nt, dt=dt)\n",
"\n",
Expand All @@ -467,7 +468,7 @@
"metadata": {},
"outputs": [],
"source": [
"from devito import norm\n",
"from devito import norm # noqa\n",
"assert np.isclose(norm(u), norm(U[0]), rtol=1e-5, atol=0)"
]
}
Expand All @@ -479,7 +480,7 @@
},
"hide_input": false,
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -493,7 +494,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
"version": "3.11.5"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
Expand Down
Loading

0 comments on commit bcc2487

Please sign in to comment.