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