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