1 | import os |
---|
2 | from math import sqrt, pi, sin, cos |
---|
3 | from shallow_water_domain_suggestion1 import * |
---|
4 | from Numeric import allclose, array, zeros, ones, Float, take, sqrt |
---|
5 | from config import g, epsilon |
---|
6 | |
---|
7 | def analytic_cannal(C,t): |
---|
8 | N = len(C) |
---|
9 | u = zeros(N,Float) ## water velocity |
---|
10 | h = zeros(N,Float) ## water depth |
---|
11 | x = C |
---|
12 | g = 9.81 |
---|
13 | ## Define Basin Bathymetry |
---|
14 | z_b = zeros(N,Float) ## elevation of basin |
---|
15 | z = zeros(N,Float) ## elevation of water surface |
---|
16 | z_infty = 10.0 ## max equilibrium water depth at lowest point. |
---|
17 | L_x = 2500.0 ## width of channel |
---|
18 | A0 = 0.5*L_x ## determines amplitudes of oscillations |
---|
19 | omega = sqrt(2*g*z_infty)/L_x ## angular frequency of osccilation |
---|
20 | for i in range(N): |
---|
21 | z_b[i] = z_infty*(x[i]**2/L_x**2) |
---|
22 | u[i] = -A0*omega*sin(omega*t) |
---|
23 | z[i] = z_infty+2*A0*z_infty/L_x*cos(omega*t)*(x[i]/L_x-0.5*A0/(L_x)*cos(omega*t)) |
---|
24 | h = z-z_b |
---|
25 | T = 2.0*pi/omega |
---|
26 | return u,h,z,z_b, T |
---|
27 | |
---|
28 | |
---|
29 | L_x = 2500.0 # Length of channel (m) |
---|
30 | N = 400 # Number of compuational cells |
---|
31 | cell_len = 4*L_x/N # Origin = 0.0 |
---|
32 | |
---|
33 | points = zeros(N+1,Float) |
---|
34 | for i in range(N+1): |
---|
35 | points[i] = -2*L_x +i*cell_len |
---|
36 | |
---|
37 | domain = Domain(points) |
---|
38 | domain.order = 2 |
---|
39 | domain.set_timestepping_method('rk2') |
---|
40 | domain.cfl = 1.0 |
---|
41 | domain.beta = 1.0 |
---|
42 | domain.limiter = "vanleer" |
---|
43 | |
---|
44 | def stage(x): |
---|
45 | z_infty = 10.0 ## max equilibrium water depth at lowest point. |
---|
46 | L_x = 2500.0 ## width of channel |
---|
47 | A0 = 0.5*L_x ## determines amplitudes of oscillations |
---|
48 | omega = sqrt(2*g*z_infty)/L_x ## angular frequency of osccilation |
---|
49 | t=0.0 |
---|
50 | y = zeros(len(x),Float) |
---|
51 | for i in range(len(x)): |
---|
52 | y[i] = z_infty+2*A0*z_infty/L_x*cos(omega*t)*(x[i]/L_x-0.5*A0/(L_x)*cos(omega*t)) |
---|
53 | #y[i] = 12.0 |
---|
54 | return y |
---|
55 | |
---|
56 | def elevation(x): |
---|
57 | N = len(x) |
---|
58 | z_infty = 10.0 |
---|
59 | z = zeros(N,Float) |
---|
60 | L_x = 2500.0 |
---|
61 | A0 = 0.5*L_x |
---|
62 | omega = sqrt(2*g*z_infty)/L_x |
---|
63 | for i in range(N): |
---|
64 | z[i] = z_infty*(x[i]**2/L_x**2) |
---|
65 | return z |
---|
66 | |
---|
67 | def height(x): |
---|
68 | z_infty = 10.0 ## max equilibrium water depth at lowest point. |
---|
69 | L_x = 2500.0 ## width of channel |
---|
70 | A0 = 0.5*L_x ## determines amplitudes of oscillations |
---|
71 | omega = sqrt(2*g*z_infty)/L_x ## angular frequency of osccilation |
---|
72 | t=0.0 |
---|
73 | y = zeros(len(x),Float) |
---|
74 | for i in range(len(x)): |
---|
75 | y[i] = max(z_infty+2*A0*z_infty/L_x*cos(omega*t)*(x[i]/L_x-0.5*A0/(L_x)*cos(omega*t))-z_infty*(x[i]**2/L_x**2),0.0) |
---|
76 | return y |
---|
77 | |
---|
78 | |
---|
79 | domain.set_quantity('stage', stage) |
---|
80 | domain.set_quantity('elevation',elevation) |
---|
81 | domain.set_boundary({'exterior': Reflective_boundary(domain)}) |
---|
82 | |
---|
83 | X = domain.vertices |
---|
84 | u,h,z,z_b,T = analytic_cannal(X.flat,domain.time) |
---|
85 | print 'T = ',T |
---|
86 | |
---|
87 | yieldstep = finaltime = T/2.0 |
---|
88 | StageQ = domain.quantities['stage'].vertex_values |
---|
89 | XmomQ = domain.quantities['xmomentum'].vertex_values |
---|
90 | |
---|
91 | import time |
---|
92 | t0 = time.time() |
---|
93 | for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): |
---|
94 | domain.write_time() |
---|
95 | print "integral", domain.quantities['stage'].get_integral() |
---|
96 | if t>0.0: |
---|
97 | |
---|
98 | print 'X=',X |
---|
99 | print 'domain.time=',domain.time |
---|
100 | |
---|
101 | StageQ = domain.quantities['stage'].vertex_values |
---|
102 | XmomQ = domain.quantities['xmomentum'].vertex_values |
---|
103 | |
---|
104 | u,h,z,z_b,T = analytic_cannal(X.flat,domain.time) |
---|
105 | w = z |
---|
106 | for k in range(len(h)): |
---|
107 | if h[k] < 0.0: |
---|
108 | h[k] = 0.0 |
---|
109 | w[k] = z_b[k] |
---|
110 | |
---|
111 | from pylab import plot,title,xlabel,ylabel,legend,savefig,show,hold,subplot |
---|
112 | hold(False) |
---|
113 | plot1 = subplot(211) |
---|
114 | plot(X,w, X,StageQ, X,z_b) |
---|
115 | #plot1.set_ylim([4,11]) |
---|
116 | xlabel('Position') |
---|
117 | ylabel('Stage') |
---|
118 | legend(('Analytic Solution', 'Numerical Solution', 'Bed'), |
---|
119 | 'upper center', shadow=True) |
---|
120 | plot2 = subplot(212) |
---|
121 | plot(X,u*h,X,XmomQ) |
---|
122 | #plot2.set_ylim([-1,25]) |
---|
123 | xlabel('Position') |
---|
124 | ylabel('Xmomentum') |
---|
125 | show() |
---|
126 | raw_input("Press the return key!") |
---|