source: anuga_work/development/anuga_1d/anuga_1d_suggestion1_2_3/steady_flow.py @ 7576

Last change on this file since 7576 was 7576, checked in by sudi, 14 years ago
File size: 4.6 KB
Line 
1import os
2from math import sqrt, pi
3from shallow_water_domain_suggestion3 import *
4from Numeric import allclose, array, zeros, ones, Float, take, sqrt
5from config import g, epsilon
6
7h_0 = 0.5
8u_0 = 0.6
9h_1 = 0.5
10u_1 = 0.6
11
12
13q = 0.3
14g = 9.81
15
16b = 4.0
17xmax = 25.0
18z_bmax = 0.2
19Fr_0 = u_0/sqrt(g*h_0)
20
21def stage(x):
22    y,u,h = analytical_sol(x)
23    return y
24   
25
26def elevation(x):
27    b = 4.0
28    xmax = 25.0
29    z_bmax = 0.2
30    z_b = zeros(len(x),Float)
31    for i in range(len(x)):
32        if (x[i] >= xmax-b) & (x[i] <= xmax+b):
33            z_b[i] = z_bmax*(1.0-(x[i]-xmax)**2/b**2)
34        else:
35            z_b[i] = 0.0 
36    return z_b
37    #return 0.0
38
39def xmomentum(x):
40    return u_0*h_0
41
42
43def bisection(func,xR,xL,H):
44    while ((xR - xL) > epsilon):
45 
46        #Calculate midpoint of domain
47        xM = xL + (xR - xL) / 2.0
48 
49        #Find f(xM)
50        if ((f(xL,H) * f(xM,H)) > 0):
51            #Throw away left half
52            xL = xM
53        else:
54            #Throw away right half
55            xR = xM
56    return xR
57
58def f(D,H):
59    return D**3+D**2*(H-1.0-Fr_0**2/2.0)+Fr_0**2/2.0
60
61def fprime(D,H):
62    Fr_0 = u_0/sqrt(g*h_0)
63    return 3*D**2+2*D*(H-1-Fr_0**2/2.0)
64
65def analytical_sol(x):
66    y = zeros(len(x),Float)
67    u = zeros(len(x),Float)
68    height = zeros(len(x),Float)
69    for i in range(len(x)):
70        if (x[i] >= xmax-b) & (x[i] <= xmax+b):
71            zb = z_bmax*(1.0-(x[i]-xmax)**2/b**2)
72        else:
73            zb = 0.0 
74
75        H = zb/h_0
76        y1 = bisection(f,1.0,0.2/0.5,H)
77        height[i] = y1*h_0
78        y[i] = height[i]+zb
79        u[i] = q/(height[i])
80       
81
82    return y,u,height
83
84Fr_0 = u_0/sqrt(g*h_0)
85zb=0.2
86H = zb/h_0
87D = bisection(f,1,0.2/0.5,H)
88
89L = 50.0
90N = 400
91cell_len = L/N
92points = zeros(N+1,Float)
93for j in range(N+1):
94    points[j] = j*cell_len
95boundary = { (0,0): 'left',(N-1,1): 'right'}
96domain = Domain(points,boundary)
97D1 = Dirichlet_boundary([h_0,u_0*h_0,0.0,h_0,u_0])
98D2 = Dirichlet_boundary([h_1,u_1*h_1,0.0,h_1,u_1])
99
100domain.set_boundary({'left':D1,'right':D2})
101domain.set_quantity('elevation',elevation)
102domain.set_quantity('stage',stage)
103domain.set_quantity('xmomentum',xmomentum)
104X = domain.vertices
105C = domain.centroids
106
107Stage = domain.quantities['stage']
108Xmom = domain.quantities['xmomentum']
109Velocity = domain.quantities['velocity']
110
111StageV = Stage.vertex_values
112StageC = Stage.centroid_values
113XmomV = Xmom.vertex_values
114XmomC = Xmom.centroid_values
115VelC = Velocity.centroid_values
116VelV = Velocity.vertex_values
117ElevationV = domain.quantities['elevation'].vertex_values
118
119stage_bdry = Stage.boundary_values
120xmom_bdry =  Xmom.boundary_values
121
122w,u,h = analytical_sol(X.flat)
123wc, uc, hc = analytical_sol(C)
124
125h_error = zeros(1,Float)
126uh_error = zeros(1,Float)
127u_error = zeros(1,Float)
128k = 0
129
130domain.limiter = "minmod"
131domain.order = 2
132domain.set_timestepping_method('rk2')
133domain.cfl = 1.0
134print 'THE DOMAIN LIMITER is', domain.limiter
135import time
136yieldstep = finaltime = 0.2/71
137t0=time.time()
138while finaltime < 0.20001:
139    for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime):
140        domain.write_time()
141        print "integral", domain.quantities['stage'].get_integral()
142        if t>= 0.0:     
143            h_error[k] = cell_len*sum(abs(wc-StageC))
144            uh_error[k] = cell_len*sum(abs(uc*hc-XmomC))
145            u_error[k] = cell_len*sum(abs(uc-VelC))
146            print "h_error %.10f" %(h_error[k])
147            print "uh_error %.10f"% (uh_error[k])
148            print "u_error %.10f"% (u_error[k])
149            print "h_max %.10f"%max(abs(wc-StageC))
150            print "uh max  %.10f"%max(abs(uc*hc-XmomC))
151            print "u_max %.10f"%max(abs(uc-VelC))
152
153            from pylab import clf,plot,title,xlabel,ylabel,legend,savefig,show,hold,subplot,ion
154            hold(False)
155            clf()
156       
157            plot1 = subplot(311)
158            plot(X,w,X,StageV,X,ElevationV)
159            #xlabel('Position')
160            ylabel('Stage')
161            plot1.set_ylim([-0.1,1.2])
162            legend(('Analytical Solution', 'Numerical Solution', 'Channel Bed'),
163                   'upper right', shadow=False)
164       
165            plot2 = subplot(312)
166            plot(X,u*h,X,XmomV)
167            #xlabel('x (m)')
168            ylabel('Momentum')
169            #plot2.set_ylim([0.299,0.301])
170       
171
172            plot3 = subplot(313)
173            plot(X,u, X,VelV)
174            xlabel('Position')
175            ylabel('Velocity')
176
177            #filename = "steady_"
178            #filename += str(t)
179            #filename += ".png"
180            #savefig(filename)
181            #show()
182    print 'That took %.2f seconds'%(time.time()-t0)
183    # #raw_input("Press return key!")
184    finaltime = finaltime + 0.2/71
Note: See TracBrowser for help on using the repository browser.