import os from math import sqrt, pi from shallow_water_domain import * from Numeric import allclose, array, zeros, ones, Float, take, sqrt from config import g, epsilon def analytical_sol(C,t): #t = 0.0 # time (s) g = 9.81 # gravity (m/s^2) h1 = 10.0 # depth upstream (m) h0 = 0.0 # depth downstream (m) L = 2000.0 # length of stream/domain (m) n = len(C) # number of cells u = zeros(n,Float) h = zeros(n,Float) x = C-3*L/4.0 for i in range(n): # Calculate Analytical Solution at time t > 0 u3 = 2.0/3.0*(sqrt(g*h1)+x[i]/t) h3 = 4.0/(9.0*g)*(sqrt(g*h1)-x[i]/(2.0*t))*(sqrt(g*h1)-x[i]/(2.0*t)) if ( x[i] <= -t*sqrt(g*h1) ): u[i] = 0.0 h[i] = h1 elif ( x[i] <= 2.0*t*sqrt(g*h1) ): u[i] = u3 h[i] = h3 else: u[i] = 0.0 h[i] = h0 return h , u*h #def newLinePlot(title='Simple Plot'): # import Gnuplot # gg = Gnuplot.Gnuplot(persist=0) # gg.terminal(postscript) # gg.title(title) # gg('set data style linespoints') # gg.xlabel('x') # gg.ylabel('y') # return gg #def linePlot(gg,x1,y1,x2,y2): # import Gnuplot # plot1 = Gnuplot.PlotItems.Data(x1.flat,y1.flat,with="linespoints") # plot2 = Gnuplot.PlotItems.Data(x2.flat,y2.flat, with="lines 3") # g.plot(plot1,plot2) print "TEST 1D-SOLUTION III -- DRY BED" L = 2000.0 # Length of channel (m) N = 100 # Number of compuational cells cell_len = L/N # Origin = 0.0 points = zeros(N+1,Float) for i in range(N+1): points[i] = i*cell_len domain = Domain(points) def stage(x): h1 = 10.0 h0 = 0.0 y = zeros(len(x),Float) for i in range(len(x)): if x[i]<=L/4.0: y[i] = h0 elif x[i]<=3*L/4.0: y[i] = h1 else: y[i] = h0 return y import time finaltime = 20.0 yieldstep = 1.0 L = 2000.0 # Length of channel (m) number_of_cells = [100]#,200,500,1000,2000,5000,10000,20000] h_error = zeros(len(number_of_cells),Float) uh_error = zeros(len(number_of_cells),Float) k = 0 for i in range(len(number_of_cells)): N = int(number_of_cells[i]) print "Evaluating domain with %d cells" %N cell_len = L/N # Origin = 0.0 points = zeros(N+1,Float) for j in range(N+1): points[j] = j*cell_len domain = Domain(points) domain.set_quantity('stage', stage) domain.set_boundary({'exterior': Reflective_boundary(domain)}) domain.default_order = 2 domain.default_time_order = 1 domain.cfl = 1.0 domain.limiter = "vanleer" t0 = time.time() for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): pass N = float(N) StageC = domain.quantities['stage'].centroid_values XmomC = domain.quantities['xmomentum'].centroid_values C = domain.centroids h, uh = analytical_sol(C,domain.time) h_error[k] = 1.0/(N)*sum(abs(h-StageC)) uh_error[k] = 1.0/(N)*sum(abs(uh-XmomC)) print "h_error %.10f" %(h_error[k]) print "uh_error %.10f"% (uh_error[k]) k = k+1 print 'That took %.2f seconds' %(time.time()-t0) X = domain.vertices StageQ = domain.quantities['stage'].vertex_values XmomQ = domain.quantities['xmomentum'].vertex_values h, uh = analytical_sol(X.flat,domain.time) x = X.flat from pylab import plot,title,xlabel,ylabel,legend,savefig,show,hold,subplot print 'test 2' hold(False) print 'test 3' plot1 = subplot(211) print 'test 4' plot(x,h,x,StageQ.flat) print 'test 5' plot1.set_ylim([-1,11]) xlabel('Position') ylabel('Stage') legend(('Analytical Solution', 'Numerical Solution'), 'upper right', shadow=True) plot2 = subplot(212) plot(x,uh,x,XmomQ.flat) plot2.set_ylim([-35,35]) xlabel('Position') ylabel('Xmomentum') file = "dry_bed_" file += str(number_of_cells[i]) file += ".eps" #savefig(file) show() print "Error in height", h_error print "Error in xmom", uh_error