Skip to content

Commit cff6a09

Browse files
committed
Update lab10 so examples work better and boundary conditions work nicely
1 parent a60a24b commit cff6a09

2 files changed

Lines changed: 61 additions & 22 deletions

File tree

notebooks/lab10/01-lab10.ipynb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
"import context\n",
3939
"from IPython.display import Image\n",
4040
"import matplotlib.pyplot as plt\n",
41-
"# make the plots happen inline\n",
42-
"%matplotlib inline\n",
4341
"# import the numpy array handling library\n",
4442
"import numpy as np\n",
4543
"# import the advection code from the numlabs directory\n",
@@ -566,7 +564,7 @@
566564
"metadata": {},
567565
"outputs": [],
568566
"source": [
569-
"afs.advection3(60, 4, lab_example=True)"
567+
"cmatrix = afs.advection3(60, 4, lab_example=True)"
570568
]
571569
},
572570
{
@@ -633,9 +631,7 @@
633631
{
634632
"cell_type": "code",
635633
"execution_count": null,
636-
"metadata": {
637-
"collapsed": true
638-
},
634+
"metadata": {},
639635
"outputs": [],
640636
"source": []
641637
}
@@ -661,7 +657,7 @@
661657
"name": "python",
662658
"nbconvert_exporter": "python",
663659
"pygments_lexer": "ipython3",
664-
"version": "3.12.1"
660+
"version": "3.13.11"
665661
},
666662
"nbsphinx": {
667663
"execute": "never"
@@ -678,5 +674,5 @@
678674
}
679675
},
680676
"nbformat": 4,
681-
"nbformat_minor": 2
677+
"nbformat_minor": 4
682678
}

numlabs/lab10/advection_funs.py

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,33 @@
55
import matplotlib.colors as colors
66
import matplotlib.cm as cmx
77
import matplotlib.colorbar as colorbar
8+
from matplotlib.patches import Rectangle
89
import numpy as np
910

1011
def initialize(timesteps, lab_example=False):
1112
''' initialize the physical system, horizontal grid size, etc
1213
'''
1314
# below are the parameters that can be varied
1415
u = 20.
15-
domain_length = 86400*20 # so it goes around the domain once in a day
16+
domain_length = 86400*u # so it goes around the domain once in a day
1617
effective_points = 500
1718
dx = domain_length/effective_points
1819

1920
dt = 0.4 * dx/ u
20-
Numpoints = effective_points + 1
21+
Numpoints = effective_points - 3
2122
shift = Numpoints * dx / 2
2223
c_0 = 1
2324
alpha = 1 / (150e3)**2
2425
epsilon = 0.0001
2526

2627
if lab_example:
28+
domain_length = 20 # m
29+
u = 0.1 # m/s
2730
Numpoints = 77
28-
shift = 7
29-
alpha = 0.1
30-
dt = 0.9
31+
dx = domain_length/(Numpoints + 3)
32+
shift = Numpoints * dx / 3
33+
alpha = 1.5
34+
dt = 0.4 * dx/ u
3135

3236
# create the concentration matrix and initialize it
3337
cmatrix = np.zeros((timesteps+1, Numpoints+4))
@@ -41,10 +45,10 @@ def initialize(timesteps, lab_example=False):
4145
def boundary_conditions(cmatrix, time, Numpoints):
4246
'''Set boundary conditions (double thick so it work for Bott Scheme as well as central and upstream
4347
'''
44-
cmatrix[time, 0] = cmatrix[time, Numpoints-1]
45-
cmatrix[time, 1] = cmatrix[time, Numpoints]
46-
cmatrix[time, Numpoints+2] = cmatrix[time, 3]
47-
cmatrix[time, Numpoints+3] = cmatrix[time, 4]
48+
cmatrix[time, 0] = cmatrix[time, Numpoints]
49+
cmatrix[time, 1] = cmatrix[time, Numpoints+1]
50+
cmatrix[time, Numpoints+2] = cmatrix[time, 2]
51+
cmatrix[time, Numpoints+3] = cmatrix[time, 3]
4852

4953
return cmatrix
5054

@@ -72,8 +76,9 @@ def step_advect(timesteps, cmatrix, Numpoints, u, dt, dx):
7276
'''Step algorithm for the Central Scheme'''
7377
for timecount in range(0, timesteps):
7478

75-
cmatrix[timecount+1, 1:Numpoints-1]= cmatrix[timecount, 1:Numpoints-1] - (
76-
u * dt/(2* dx) * (cmatrix[timecount, 2:Numpoints] - cmatrix[timecount, :Numpoints-2]))
79+
cmatrix[timecount+1, 2:Numpoints+2]= cmatrix[timecount, 2:Numpoints+2] - (
80+
u * dt/(2* dx) * (cmatrix[timecount, 3:Numpoints+3]
81+
- cmatrix[timecount, 1:Numpoints+1]))
7782

7883
cmatrix = boundary_conditions(cmatrix, timecount+1, Numpoints)
7984
return cmatrix
@@ -83,8 +88,9 @@ def step_advect2(timesteps, cmatrix, Numpoints, u, dt, dx):
8388

8489
for timecount in range(0, timesteps):
8590

86-
cmatrix[timecount+1, 1:Numpoints]= cmatrix[timecount, 1:Numpoints] - (
87-
u * dt/ dx * (cmatrix[timecount, 1:Numpoints] - cmatrix[timecount, :Numpoints-1]))
91+
cmatrix[timecount+1, 2:Numpoints+2]= cmatrix[timecount, 2:Numpoints+2] - (
92+
u * dt/ dx * (cmatrix[timecount, 2:Numpoints+2]
93+
- cmatrix[timecount, 1:Numpoints+1]))
8894

8995
cmatrix = boundary_conditions(cmatrix, timecount+1, Numpoints)
9096
return cmatrix
@@ -132,6 +138,38 @@ def step_advect3(timesteps, ltable, cmatrix, order, Numpoints, u, dt, dx, epsilo
132138

133139
return cmatrix
134140

141+
def grey_ghost_points(ax, Numpoints):
142+
'''Grey out the ghost points on the plot'''
143+
ax.set_ylim(-0.1, 1.1)
144+
145+
ybot, ytop = -0.1, 1.1
146+
ax.set_ylim(ybot, ytop)
147+
148+
width = 2
149+
height = ytop - ybot
150+
151+
xy_coords = (0, ybot)
152+
rect = Rectangle(
153+
xy_coords,
154+
width,
155+
height,
156+
linewidth=0,
157+
facecolor='grey',
158+
alpha=0.7,
159+
)
160+
ax.add_patch(rect)
161+
162+
xy_coords = (Numpoints-1+width, ybot)
163+
rect = Rectangle(
164+
xy_coords,
165+
width,
166+
height,
167+
linewidth=0,
168+
facecolor='grey',
169+
alpha=0.7,
170+
)
171+
ax.add_patch(rect)
172+
135173
def make_graph(cmatrix, timesteps, Numpoints, dt):
136174
"""Create graphs of the model results using matplotlib.
137175
"""
@@ -150,15 +188,20 @@ def make_graph(cmatrix, timesteps, Numpoints, dt):
150188
cNorm_inseconds = colors.Normalize(vmin=0, vmax=1.*timesteps*dt)
151189
scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cmap)
152190

191+
if timesteps > 20:
153192
# Only try to plot 20 lines, so choose an interval if more than that (i.e. plot every interval lines)
154-
plotsteps = (np.arange(0, timesteps, timesteps/20) + timesteps/20).astype(int)
193+
plotsteps = (np.arange(0, timesteps, timesteps/20) + timesteps/20).astype(int)
194+
else:
195+
plotsteps = range(timesteps)
155196

156197
ax.plot(cmatrix[0, :], color='r', linewidth=3)
157198
# Do the main plot
158199
for time in plotsteps:
159200
colorVal = scalarMap.to_rgba(time)
160201
ax.plot(cmatrix[time, :], color=colorVal)
161202

203+
grey_ghost_points(ax, Numpoints)
204+
162205
# Add the custom colorbar
163206
ax2 = fig.add_axes([0.95, 0.05, 0.05, 0.9])
164207
cb1 = colorbar.ColorbarBase(ax2, cmap=cmap, norm=cNorm_inseconds)

0 commit comments

Comments
 (0)