Skip to content

Commit 324339c

Browse files
committed
initial version of code
0 parents  commit 324339c

12 files changed

+823
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+

README

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Running code:
2+
* MATLAB: type >>shallow_water_model at the command prompt
3+
* To do the plots in the MATLAB version type >>animate after running
4+
the model
5+
* Python: can be run from ipython, by typing run -i shallow_water_model.py
6+
* To do the plots in the python version type run -i animate.py after
7+
running the model
8+
Contributing development:
9+
* at the moment the code is under public repo. Contact
10+
the code owner to contribute ideas.
11+

matlab/animate.m

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
% This script animates the height field and the vorticity produced by
2+
% a shallow water model. It should be called only after shallow_water_model
3+
% has been run.
4+
5+
% Set the size of the figure
6+
set(gcf,'units','inches');
7+
pos=get(gcf,'position');
8+
pos([3 4]) = [10.5 5];
9+
set(gcf,'position',pos)
10+
11+
% Set other figure properties and draw now
12+
set(gcf,'defaultaxesfontsize',12,...
13+
'paperpositionmode','auto','color','w');
14+
drawnow
15+
16+
% Axis units are thousands of kilometers (x and y are in metres)
17+
x_1000km = x.*1e-6;
18+
y_1000km = y.*1e-6;
19+
20+
% Set colormap to have 64 entries
21+
ncol=64;
22+
colormap(jet(ncol));
23+
% colormap(hclmultseq01);
24+
% colormap(flipud(cbrewer('div','RdBu',32)))
25+
26+
% Interval between arrows in the velocity vector plot
27+
interval = 6;
28+
29+
% Set this to "true" to save each frame as a png file
30+
plot_frames = false;
31+
32+
% Decide whether to show height in metres or km
33+
if mean(plot_height_range) > 1000
34+
height_scale = 0.001;
35+
height_title = 'Height (km)';
36+
else
37+
height_scale = 1;
38+
height_title = 'Height (m)';
39+
end
40+
41+
disp(['Maximum orography height = ' num2str(max(H(:))) ' m']);
42+
43+
% Loop through the frames of the animation
44+
for it = 1:noutput
45+
clf
46+
47+
% Extract the height and velocity components for this frame
48+
h = squeeze(h_save(:,:,it));
49+
u = squeeze(u_save(:,:,it));
50+
v = squeeze(v_save(:,:,it));
51+
52+
% First plot the height field
53+
subplot(2,1,1);
54+
55+
% Plot the height field
56+
handle = image(x_1000km, y_1000km, (h'+H').*height_scale);
57+
set(handle,'CDataMapping','scaled');
58+
set(gca,'ydir','normal');
59+
caxis(plot_height_range.*height_scale);
60+
61+
% Plot the orography as black contours every 1000 m
62+
hold on
63+
warning off
64+
contour(x_1000km, y_1000km, H',[1:1000:8001],'k');
65+
warning on
66+
67+
% Plot the velocity vectors
68+
quiver(x_1000km(3:interval:end), y_1000km(3:interval:end), ...
69+
u(3:interval:end, 3:interval:end)',...
70+
v(3:interval:end, 3:interval:end)','k','linewidth',0.2);
71+
72+
% Write the axis labels, title and time of the frame
73+
xlabel('X distance (1000s of km)');
74+
ylabel('Y distance (1000s of km)');
75+
title(['\bf' height_title]);
76+
text(0, max(y_1000km), ['Time = ' num2str(t_save(it)./3600) ' hours'],...
77+
'verticalalignment','bottom','fontsize',12);
78+
79+
% Set other axes properties and plot a colorbar
80+
daspect([1 1 1]);
81+
axis([0 max(x_1000km) 0 max(y_1000km)]);
82+
colorbar
83+
84+
% Compute the vorticity
85+
vorticity = zeros(size(u));
86+
vorticity(2:end-1,2:end-1) = (1/dy).*(u(2:end-1,1:end-2)-u(2:end-1,3:end)) ...
87+
+ (1/dx).*(v(3:end,2:end-1)-v(1:end-2,2:end-1));
88+
89+
% Now plot the vorticity
90+
subplot(2,1,2);
91+
handle = image(x_1000km, y_1000km, vorticity');
92+
set(handle,'CDataMapping','scaled');
93+
set(gca,'ydir','normal');
94+
caxis([-3 3].*1e-4);
95+
96+
% Axis labels and title
97+
xlabel('X distance (1000s of km)');
98+
ylabel('Y distance (1000s of km)');
99+
title('\bfRelative vorticity (s^{-1})');
100+
101+
% Other axes properties and plot a colorbar
102+
daspect([1 1 1]);
103+
axis([0 max(x_1000km) 0 max(y_1000km)]);
104+
colorbar
105+
106+
% Now render this frame
107+
warning off
108+
drawnow
109+
warning on
110+
111+
% To make an animation we can save the frames as a
112+
% sequence of images
113+
if plot_frames
114+
eval(['print -dpng frame',num2str(it,'%03d'),'.png']);
115+
% imwrite(frame2im(getframe(gcf)),...
116+
% ['frame' num2str(it,'%03d') '.png']);
117+
end
118+
pause(0.05);
119+
end

matlab/digital_elevation_map.mat

52.2 KB
Binary file not shown.

matlab/lax_wendroff.m

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function [u_new, v_new, h_new] = lax_wendroff(dx, dy, dt, g, u, v, h, ...
2+
u_tendency, v_tendency);
3+
% This function performs one timestep of the Lax-Wendroff scheme
4+
% applied to the shallow water equations
5+
6+
% First work out mid-point values in time and space
7+
uh = u.*h;
8+
vh = v.*h;
9+
h_mid_xt = 0.5.*(h(2:end,:)+h(1:end-1,:)) ...
10+
-(0.5*dt/dx).*(uh(2:end,:)-uh(1:end-1,:));
11+
h_mid_yt = 0.5.*(h(:,2:end)+h(:,1:end-1)) ...
12+
-(0.5*dt/dy).*(vh(:,2:end)-vh(:,1:end-1));
13+
14+
Ux = uh.*u+0.5.*g.*h.^2;
15+
Uy = uh.*v;
16+
uh_mid_xt = 0.5.*(uh(2:end,:)+uh(1:end-1,:)) ...
17+
-(0.5*dt/dx).*(Ux(2:end,:)-Ux(1:end-1,:));
18+
uh_mid_yt = 0.5.*(uh(:,2:end)+uh(:,1:end-1)) ...
19+
-(0.5*dt/dy).*(Uy(:,2:end)-Uy(:,1:end-1));
20+
21+
Vx = Uy;
22+
Vy = vh.*v+0.5.*g.*h.^2;
23+
vh_mid_xt = 0.5.*(vh(2:end,:)+vh(1:end-1,:)) ...
24+
-(0.5*dt/dx).*(Vx(2:end,:)-Vx(1:end-1,:));
25+
vh_mid_yt = 0.5.*(vh(:,2:end)+vh(:,1:end-1)) ...
26+
-(0.5*dt/dy).*(Vy(:,2:end)-Vy(:,1:end-1));
27+
28+
% Now use the mid-point values to predict the values at the next
29+
% timestep
30+
h_new = h(2:end-1,2:end-1) ...
31+
- (dt/dx).*(uh_mid_xt(2:end,2:end-1)-uh_mid_xt(1:end-1,2:end-1)) ...
32+
- (dt/dy).*(vh_mid_yt(2:end-1,2:end)-vh_mid_yt(2:end-1,1:end-1));
33+
34+
Ux_mid_xt = uh_mid_xt.*uh_mid_xt./h_mid_xt + 0.5.*g.*h_mid_xt.^2;
35+
Uy_mid_yt = uh_mid_yt.*vh_mid_yt./h_mid_yt;
36+
uh_new = uh(2:end-1,2:end-1) ...
37+
- (dt/dx).*(Ux_mid_xt(2:end,2:end-1)-Ux_mid_xt(1:end-1,2:end-1)) ...
38+
- (dt/dy).*(Uy_mid_yt(2:end-1,2:end)-Uy_mid_yt(2:end-1,1:end-1)) ...
39+
+ dt.*u_tendency.*0.5.*(h(2:end-1,2:end-1)+h_new);
40+
41+
Vx_mid_xt = uh_mid_xt.*vh_mid_xt./h_mid_xt;
42+
Vy_mid_yt = vh_mid_yt.*vh_mid_yt./h_mid_yt + 0.5.*g.*h_mid_yt.^2;
43+
vh_new = vh(2:end-1,2:end-1) ...
44+
- (dt/dx).*(Vx_mid_xt(2:end,2:end-1)-Vx_mid_xt(1:end-1,2:end-1)) ...
45+
- (dt/dy).*(Vy_mid_yt(2:end-1,2:end)-Vy_mid_yt(2:end-1,1:end-1)) ...
46+
+ dt.*v_tendency.*0.5.*(h(2:end-1,2:end-1)+h_new);
47+
u_new = uh_new./h_new;
48+
v_new = vh_new./h_new;
49+

matlab/reanalysis.mat

84.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)