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