source: anuga_work/development/anuga_1d/dry_dam_vel.py @ 5845

Last change on this file since 5845 was 5845, checked in by steve, 16 years ago

Adding shallow_water_vel code

File size: 4.0 KB
Line 
1import os
2from math import sqrt, pi
3from shallow_water_vel_domain import *
4from Numeric import allclose, array, zeros, ones, Float, take, sqrt
5from config import g, epsilon
6
7
8h1 = 10.0
9h0 = 0.0
10
11def 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
51print "TEST 1D-SOLUTION III -- DRY BED"
52
53def stage(x):
54    y = zeros(len(x),Float)
55    for i in range(len(x)):
56        if x[i]<=L/4.0:
57            y[i] = h0
58        elif x[i]<=3*L/4.0:
59            y[i] = h1
60        else:
61            y[i] = h0
62    return y
63
64
65import time
66
67finaltime = 10.0
68yieldstep = finaltime
69L = 2000.0     # Length of channel (m)
70number_of_cells = [800]#,200,500,1000,2000,5000,10000,20000]
71h_error = zeros(len(number_of_cells),Float)
72uh_error = zeros(len(number_of_cells),Float)
73u_error = zeros(len(number_of_cells),Float)
74k = 0
75for i in range(len(number_of_cells)):
76    N = int(number_of_cells[i])
77    print "Evaluating domain with %d cells" %N
78    cell_len = L/N # Origin = 0.0
79    points = zeros(N+1,Float)
80    for j in range(N+1):
81        points[j] = j*cell_len
82       
83    domain = Domain(points)
84   
85    domain.set_quantity('stage', stage)
86    domain.set_boundary({'exterior': Reflective_boundary(domain)})
87    domain.order = 2
88    domain.set_timestepping_method('rk2')
89    domain.set_CFL(1.0)
90    domain.set_limiter("vanleer")
91    #domain.h0=0.0001
92
93    t0 = time.time()
94
95    for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime):
96        domain.write_time()
97
98    N = float(N)
99    StageC = domain.quantities['stage'].centroid_values
100    XmomC = domain.quantities['xmomentum'].centroid_values
101    u_C = domain.quantities['velocity'].centroid_values
102    C = domain.centroids
103    h, uh, u = analytical_sol(C,domain.time)
104    h_error[k] = 1.0/(N)*sum(abs(h-StageC))
105    uh_error[k] = 1.0/(N)*sum(abs(uh-XmomC))
106    u_error[k] = 1.0/(N)*sum(abs(u-u_C))
107    print "h_error %.10f" %(h_error[k])
108    print "uh_error %.10f"% (uh_error[k])
109    print "u_error %.10f"% (u_error[k])
110    k = k+1
111    print 'That took %.2f seconds' %(time.time()-t0)
112    X = domain.vertices
113    StageQ = domain.quantities['stage'].vertex_values
114    XmomQ = domain.quantities['xmomentum'].vertex_values
115    velQ = domain.quantities['velocity'].vertex_values
116   
117    h, uh, u = analytical_sol(X.flat,domain.time)
118    x = X.flat
119   
120    from pylab import plot,title,xlabel,ylabel,legend,savefig,show,hold,subplot
121    print 'test 2'
122    hold(False)
123    print 'test 3'
124    plot1 = subplot(211)
125    print 'test 4'
126    plot(x,h,x,StageQ.flat)
127    print 'test 5'
128    plot1.set_ylim([-1,11])
129    xlabel('Position')
130    ylabel('Stage')
131    legend(('Analytical Solution', 'Numerical Solution'),
132           'upper right', shadow=True)
133    plot2 = subplot(212)
134    plot(x,u,x,velQ.flat)
135    plot2.set_ylim([-35,35])
136   
137    xlabel('Position')
138    ylabel('Velocity')
139   
140    file = "dry_bed_"
141    file += str(number_of_cells[i])
142    file += ".eps"
143    #savefig(file)
144    show()
145   
146print "Error in height", h_error
147print "Error in xmom", uh_error
Note: See TracBrowser for help on using the repository browser.