1 | import os |
---|
2 | from math import sqrt, pi |
---|
3 | from shallow_water_domain_suggestion3 import * |
---|
4 | from Numeric import allclose, array, zeros, ones, Float, take, sqrt |
---|
5 | from config import g, epsilon |
---|
6 | |
---|
7 | h_0 = 0.5 |
---|
8 | u_0 = 0.6 |
---|
9 | h_1 = 0.5 |
---|
10 | u_1 = 0.6 |
---|
11 | |
---|
12 | |
---|
13 | q = 0.3 |
---|
14 | g = 9.81 |
---|
15 | |
---|
16 | b = 4.0 |
---|
17 | xmax = 25.0 |
---|
18 | z_bmax = 0.2 |
---|
19 | Fr_0 = u_0/sqrt(g*h_0) |
---|
20 | |
---|
21 | def stage(x): |
---|
22 | y,u,h = analytical_sol(x) |
---|
23 | return y |
---|
24 | |
---|
25 | |
---|
26 | def elevation(x): |
---|
27 | b = 4.0 |
---|
28 | xmax = 25.0 |
---|
29 | z_bmax = 0.2 |
---|
30 | z_b = zeros(len(x),Float) |
---|
31 | for i in range(len(x)): |
---|
32 | if (x[i] >= xmax-b) & (x[i] <= xmax+b): |
---|
33 | z_b[i] = z_bmax*(1.0-(x[i]-xmax)**2/b**2) |
---|
34 | else: |
---|
35 | z_b[i] = 0.0 |
---|
36 | return z_b |
---|
37 | #return 0.0 |
---|
38 | |
---|
39 | def xmomentum(x): |
---|
40 | return u_0*h_0 |
---|
41 | |
---|
42 | |
---|
43 | def bisection(func,xR,xL,H): |
---|
44 | while ((xR - xL) > epsilon): |
---|
45 | |
---|
46 | #Calculate midpoint of domain |
---|
47 | xM = xL + (xR - xL) / 2.0 |
---|
48 | |
---|
49 | #Find f(xM) |
---|
50 | if ((f(xL,H) * f(xM,H)) > 0): |
---|
51 | #Throw away left half |
---|
52 | xL = xM |
---|
53 | else: |
---|
54 | #Throw away right half |
---|
55 | xR = xM |
---|
56 | return xR |
---|
57 | |
---|
58 | def f(D,H): |
---|
59 | return D**3+D**2*(H-1.0-Fr_0**2/2.0)+Fr_0**2/2.0 |
---|
60 | |
---|
61 | def fprime(D,H): |
---|
62 | Fr_0 = u_0/sqrt(g*h_0) |
---|
63 | return 3*D**2+2*D*(H-1-Fr_0**2/2.0) |
---|
64 | |
---|
65 | def analytical_sol(x): |
---|
66 | y = zeros(len(x),Float) |
---|
67 | u = zeros(len(x),Float) |
---|
68 | height = zeros(len(x),Float) |
---|
69 | for i in range(len(x)): |
---|
70 | if (x[i] >= xmax-b) & (x[i] <= xmax+b): |
---|
71 | zb = z_bmax*(1.0-(x[i]-xmax)**2/b**2) |
---|
72 | else: |
---|
73 | zb = 0.0 |
---|
74 | |
---|
75 | H = zb/h_0 |
---|
76 | y1 = bisection(f,1.0,0.2/0.5,H) |
---|
77 | height[i] = y1*h_0 |
---|
78 | y[i] = height[i]+zb |
---|
79 | u[i] = q/(height[i]) |
---|
80 | |
---|
81 | |
---|
82 | return y,u,height |
---|
83 | |
---|
84 | Fr_0 = u_0/sqrt(g*h_0) |
---|
85 | zb=0.2 |
---|
86 | H = zb/h_0 |
---|
87 | D = bisection(f,1,0.2/0.5,H) |
---|
88 | |
---|
89 | L = 50.0 |
---|
90 | N = 400 |
---|
91 | cell_len = L/N |
---|
92 | points = zeros(N+1,Float) |
---|
93 | for j in range(N+1): |
---|
94 | points[j] = j*cell_len |
---|
95 | boundary = { (0,0): 'left',(N-1,1): 'right'} |
---|
96 | domain = Domain(points,boundary) |
---|
97 | D1 = Dirichlet_boundary([h_0,u_0*h_0,0.0,h_0,u_0]) |
---|
98 | D2 = Dirichlet_boundary([h_1,u_1*h_1,0.0,h_1,u_1]) |
---|
99 | |
---|
100 | domain.set_boundary({'left':D1,'right':D2}) |
---|
101 | domain.set_quantity('elevation',elevation) |
---|
102 | domain.set_quantity('stage',stage) |
---|
103 | domain.set_quantity('xmomentum',xmomentum) |
---|
104 | X = domain.vertices |
---|
105 | C = domain.centroids |
---|
106 | |
---|
107 | Stage = domain.quantities['stage'] |
---|
108 | Xmom = domain.quantities['xmomentum'] |
---|
109 | Velocity = domain.quantities['velocity'] |
---|
110 | |
---|
111 | StageV = Stage.vertex_values |
---|
112 | StageC = Stage.centroid_values |
---|
113 | XmomV = Xmom.vertex_values |
---|
114 | XmomC = Xmom.centroid_values |
---|
115 | VelC = Velocity.centroid_values |
---|
116 | VelV = Velocity.vertex_values |
---|
117 | ElevationV = domain.quantities['elevation'].vertex_values |
---|
118 | |
---|
119 | stage_bdry = Stage.boundary_values |
---|
120 | xmom_bdry = Xmom.boundary_values |
---|
121 | |
---|
122 | w,u,h = analytical_sol(X.flat) |
---|
123 | wc, uc, hc = analytical_sol(C) |
---|
124 | |
---|
125 | h_error = zeros(1,Float) |
---|
126 | uh_error = zeros(1,Float) |
---|
127 | u_error = zeros(1,Float) |
---|
128 | k = 0 |
---|
129 | |
---|
130 | domain.limiter = "minmod" |
---|
131 | domain.order = 2 |
---|
132 | domain.set_timestepping_method('rk2') |
---|
133 | domain.cfl = 1.0 |
---|
134 | print 'THE DOMAIN LIMITER is', domain.limiter |
---|
135 | import time |
---|
136 | yieldstep = finaltime = 0.2/71 |
---|
137 | t0=time.time() |
---|
138 | while finaltime < 0.20001: |
---|
139 | for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): |
---|
140 | domain.write_time() |
---|
141 | print "integral", domain.quantities['stage'].get_integral() |
---|
142 | if t>= 0.0: |
---|
143 | h_error[k] = cell_len*sum(abs(wc-StageC)) |
---|
144 | uh_error[k] = cell_len*sum(abs(uc*hc-XmomC)) |
---|
145 | u_error[k] = cell_len*sum(abs(uc-VelC)) |
---|
146 | print "h_error %.10f" %(h_error[k]) |
---|
147 | print "uh_error %.10f"% (uh_error[k]) |
---|
148 | print "u_error %.10f"% (u_error[k]) |
---|
149 | print "h_max %.10f"%max(abs(wc-StageC)) |
---|
150 | print "uh max %.10f"%max(abs(uc*hc-XmomC)) |
---|
151 | print "u_max %.10f"%max(abs(uc-VelC)) |
---|
152 | |
---|
153 | from pylab import clf,plot,title,xlabel,ylabel,legend,savefig,show,hold,subplot,ion |
---|
154 | hold(False) |
---|
155 | clf() |
---|
156 | |
---|
157 | plot1 = subplot(311) |
---|
158 | plot(X,w,X,StageV,X,ElevationV) |
---|
159 | #xlabel('Position') |
---|
160 | ylabel('Stage') |
---|
161 | plot1.set_ylim([-0.1,1.2]) |
---|
162 | legend(('Analytical Solution', 'Numerical Solution', 'Channel Bed'), |
---|
163 | 'upper right', shadow=False) |
---|
164 | |
---|
165 | plot2 = subplot(312) |
---|
166 | plot(X,u*h,X,XmomV) |
---|
167 | #xlabel('x (m)') |
---|
168 | ylabel('Momentum') |
---|
169 | #plot2.set_ylim([0.299,0.301]) |
---|
170 | |
---|
171 | |
---|
172 | plot3 = subplot(313) |
---|
173 | plot(X,u, X,VelV) |
---|
174 | xlabel('Position') |
---|
175 | ylabel('Velocity') |
---|
176 | |
---|
177 | #filename = "steady_" |
---|
178 | #filename += str(t) |
---|
179 | #filename += ".png" |
---|
180 | #savefig(filename) |
---|
181 | #show() |
---|
182 | print 'That took %.2f seconds'%(time.time()-t0) |
---|
183 | # #raw_input("Press return key!") |
---|
184 | finaltime = finaltime + 0.2/71 |
---|