1 | import os |
---|
2 | from math import sqrt, pi |
---|
3 | from shallow_water_domain import * |
---|
4 | from Numeric import allclose, array, zeros, ones, Float, take, sqrt |
---|
5 | from config import g, epsilon |
---|
6 | |
---|
7 | def analytical_sol(C,t): |
---|
8 | |
---|
9 | #t = 0.0 # time (s) |
---|
10 | g = 9.81 # gravity (m/s^2) |
---|
11 | h1 = 10.0 # depth upstream (m) |
---|
12 | h0 = 0.0 # depth downstream (m) |
---|
13 | L = 2000.0 # length of stream/domain (m) |
---|
14 | n = len(C) # number of cells |
---|
15 | |
---|
16 | u = zeros(n,Float) |
---|
17 | h = zeros(n,Float) |
---|
18 | x = C-3*L/4.0 |
---|
19 | |
---|
20 | |
---|
21 | for i in range(n): |
---|
22 | # Calculate Analytical Solution at time t > 0 |
---|
23 | u3 = 2.0/3.0*(sqrt(g*h1)+x[i]/t) |
---|
24 | h3 = 4.0/(9.0*g)*(sqrt(g*h1)-x[i]/(2.0*t))*(sqrt(g*h1)-x[i]/(2.0*t)) |
---|
25 | u3_ = 2.0/3.0*((x[i]+L/2.0)/t-sqrt(g*h1)) |
---|
26 | h3_ = 1.0/(9.0*g)*((x[i]+L/2.0)/t+2*sqrt(g*h1))*((x[i]+L/2.0)/t+2*sqrt(g*h1)) |
---|
27 | |
---|
28 | if ( x[i] <= -1*L/2.0+2*(-sqrt(g*h1)*t)): |
---|
29 | u[i] = 0.0 |
---|
30 | h[i] = h0 |
---|
31 | elif ( x[i] <= -1*L/2.0-(-sqrt(g*h1)*t)): |
---|
32 | u[i] = u3_ |
---|
33 | h[i] = h3_ |
---|
34 | |
---|
35 | elif ( x[i] <= -t*sqrt(g*h1) ): |
---|
36 | u[i] = 0.0 |
---|
37 | h[i] = h1 |
---|
38 | elif ( x[i] <= 2.0*t*sqrt(g*h1) ): |
---|
39 | u[i] = u3 |
---|
40 | h[i] = h3 |
---|
41 | else: |
---|
42 | u[i] = 0.0 |
---|
43 | h[i] = h0 |
---|
44 | |
---|
45 | return h , u*h |
---|
46 | |
---|
47 | #def newLinePlot(title='Simple Plot'): |
---|
48 | # import Gnuplot |
---|
49 | # gg = Gnuplot.Gnuplot(persist=0) |
---|
50 | # gg.terminal(postscript) |
---|
51 | # gg.title(title) |
---|
52 | # gg('set data style linespoints') |
---|
53 | # gg.xlabel('x') |
---|
54 | # gg.ylabel('y') |
---|
55 | # return gg |
---|
56 | |
---|
57 | #def linePlot(gg,x1,y1,x2,y2): |
---|
58 | # import Gnuplot |
---|
59 | # plot1 = Gnuplot.PlotItems.Data(x1.flat,y1.flat,with="linespoints") |
---|
60 | # plot2 = Gnuplot.PlotItems.Data(x2.flat,y2.flat, with="lines 3") |
---|
61 | # g.plot(plot1,plot2) |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | print "TEST 1D-SOLUTION III -- DRY BED" |
---|
66 | |
---|
67 | L = 2000.0 # Length of channel (m) |
---|
68 | N = 800 # Number of computational cells |
---|
69 | cell_len = L/N # Origin = 0.0 |
---|
70 | |
---|
71 | points = zeros(N+1,Float) |
---|
72 | for i in range(N+1): |
---|
73 | points[i] = i*cell_len |
---|
74 | |
---|
75 | domain = Domain(points) |
---|
76 | |
---|
77 | def stage(x): |
---|
78 | h1 = 10.0 |
---|
79 | h0 = 0.0 |
---|
80 | y = zeros(len(x),Float) |
---|
81 | for i in range(len(x)): |
---|
82 | if x[i]<=L/4.0: |
---|
83 | y[i] = h0 |
---|
84 | elif x[i]<=3*L/4.0: |
---|
85 | y[i] = h1 |
---|
86 | else: |
---|
87 | y[i] = h0 |
---|
88 | return y |
---|
89 | |
---|
90 | |
---|
91 | import time |
---|
92 | |
---|
93 | finaltime = 20.0 |
---|
94 | yieldstep = 1.0 |
---|
95 | L = 2000.0 # Length of channel (m) |
---|
96 | number_of_cells = [200]#,200,500,1000,2000,5000,10000,20000] |
---|
97 | h_error = zeros(len(number_of_cells),Float) |
---|
98 | uh_error = zeros(len(number_of_cells),Float) |
---|
99 | k = 0 |
---|
100 | for i in range(len(number_of_cells)): |
---|
101 | N = int(number_of_cells[i]) |
---|
102 | print "Evaluating domain with %d cells" %N |
---|
103 | cell_len = L/N # Origin = 0.0 |
---|
104 | points = zeros(N+1,Float) |
---|
105 | for j in range(N+1): |
---|
106 | points[j] = j*cell_len |
---|
107 | |
---|
108 | domain = Domain(points) |
---|
109 | |
---|
110 | domain.set_quantity('stage', stage) |
---|
111 | domain.set_boundary({'exterior': Reflective_boundary(domain)}) |
---|
112 | domain.default_order = 2 |
---|
113 | domain.default_time_order = 2 |
---|
114 | domain.cfl = 1.0 |
---|
115 | domain.limiter = "minmod" |
---|
116 | |
---|
117 | t0 = time.time() |
---|
118 | |
---|
119 | for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): |
---|
120 | domain.write_time() |
---|
121 | |
---|
122 | N = float(N) |
---|
123 | StageC = domain.quantities['stage'].centroid_values |
---|
124 | XmomC = domain.quantities['xmomentum'].centroid_values |
---|
125 | C = domain.centroids |
---|
126 | h, uh = analytical_sol(C,domain.time) |
---|
127 | h_error[k] = 1.0/(N)*sum(abs(h-StageC)) |
---|
128 | uh_error[k] = 1.0/(N)*sum(abs(uh-XmomC)) |
---|
129 | print "h_error %.10f" %(h_error[k]) |
---|
130 | print "uh_error %.10f"% (uh_error[k]) |
---|
131 | k = k+1 |
---|
132 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
133 | X = domain.vertices |
---|
134 | StageQ = domain.quantities['stage'].vertex_values |
---|
135 | XmomQ = domain.quantities['xmomentum'].vertex_values |
---|
136 | h, uh = analytical_sol(X.flat,domain.time) |
---|
137 | x = X.flat |
---|
138 | |
---|
139 | from pylab import plot,title,xlabel,ylabel,legend,savefig,show,hold,subplot |
---|
140 | print 'test 2' |
---|
141 | hold(False) |
---|
142 | print 'test 3' |
---|
143 | plot1 = subplot(211) |
---|
144 | print 'test 4' |
---|
145 | plot(x,h,x,StageQ.flat) |
---|
146 | print 'test 5' |
---|
147 | plot1.set_ylim([-1,11]) |
---|
148 | xlabel('Position') |
---|
149 | ylabel('Stage') |
---|
150 | legend(('Analytical Solution', 'Numerical Solution'), |
---|
151 | 'upper right', shadow=True) |
---|
152 | plot2 = subplot(212) |
---|
153 | plot(x,uh,x,XmomQ.flat) |
---|
154 | plot2.set_ylim([-35,35]) |
---|
155 | |
---|
156 | xlabel('Position') |
---|
157 | ylabel('Xmomentum') |
---|
158 | |
---|
159 | file = "dry_bed_" |
---|
160 | file += str(number_of_cells[i]) |
---|
161 | file += ".eps" |
---|
162 | #savefig(file) |
---|
163 | show() |
---|
164 | |
---|
165 | print "Error in height", h_error |
---|
166 | print "Error in xmom", uh_error |
---|