source: anuga_work/development/pyvolution-1d/dam.py @ 4297

Last change on this file since 4297 was 3425, checked in by steve, 19 years ago
File size: 4.0 KB
Line 
1
2import os
3from math import sqrt, pi
4from shallow_water_1d import *
5from Numeric import allclose, array, zeros, ones, Float, take, sqrt
6from config import g, epsilon
7from analytic_dam import AnalyticDam
8
9h0 = 0.1
10h1 = 10.0
11
12analytical_sol = AnalyticDam(h0, h1)
13
14## def analytical_sol(C,t):
15   
16##     #t  = 0.0     # time (s)
17##     g  = 9.81    # gravity (m/s^2)
18##     h1 = 10.0    # depth upstream (m)
19##     h0 = 5.0     # depth downstream (m)
20##     L = 2000.0   # length of stream/domain (m)
21##     n = len(C)    # number of cells
22
23##     u = zeros(n,Float)
24##     h = zeros(n,Float)
25##     x = C-L/2
26##     #x = zeros(n,Float)
27##     #for i in range(n):
28##     #    x[i] = C[i]-1000.0
29
30##     # Upstream and downstream boundary conditions are set to the intial water
31##     # depth for all time.
32
33##     # Calculate Shock Speed
34##     h2 = 7.2692044
35##     S2 = 2*h2/(h2-h0)*(sqrt(g*h1)-sqrt(g*h2))
36##     u2 = S2 - g*h0/(4*S2)*(1+sqrt(1+8*S2*S2/(g*h0)))
37   
38##     #t=50
39##     #x = (-L/2:L/2)
40##     for i in range(n):
41##         # Calculate Analytical Solution at time t > 0
42##         u3 = 2/3*(sqrt(g*h1)+x[i]/t)
43##         h3 = 4/(9*g)*(sqrt(g*h1)-x[i]/(2*t))*(sqrt(g*h1)-x[i]/(2*t))
44
45##         if ( x[i] <= -t*sqrt(g*h1) ):
46##             u[i] = 0.0
47##             h[i] = h1
48##         elif ( x[i] <= t*(u2-sqrt(g*h2)) ):
49##             u[i] = u3
50##             h[i] = h3
51##         elif ( x[i] < t*S2 ):
52##             u[i] = u2
53##             h[i] = h2
54##         else:
55##             u[i] = 0.0
56##             h[i] = h0
57
58##     return h , u*h
59
60
61def newLinePlot(title='Simple Plot'):
62    import Gnuplot
63    g = Gnuplot.Gnuplot()
64    g.title(title)
65    g('set style data lines') 
66    g.xlabel('x')
67    g.ylabel('y')
68    return g
69
70def linePlot(g,x1,y1,x2,y2):
71    import Gnuplot
72    plot1 = Gnuplot.PlotItems.Data(x1.flat,y1.flat,with="linespoints")
73    plot2 = Gnuplot.PlotItems.Data(x2.flat,y2.flat, with="lines 3")
74    g.plot(plot1,plot2)
75    #g.plot(Gnuplot.PlotItems.Data(x1.flat,y1.flat),with="lines")
76    #g.plot(Gnuplot.PlotItems.Data(x2.flat,y2.flat), with="lines")
77
78debug = False
79
80print "TEST 1D-SOLUTION I"
81
82L = 2000.0     # Length of channel (m)
83N = 100        # Number of compuational cells
84cell_len = L/N # Origin = 0.0
85
86points = zeros(N+1,Float)
87for i in range(N+1):
88    points[i] = i*cell_len
89
90domain = Domain(points)
91
92def stage(x):
93    y = zeros(len(x),Float)
94    for i in range(len(x)):
95        if x[i]<=1000.0:
96            y[i] = h1
97        else:
98            y[i] = h0
99    return y
100
101domain.set_quantity('stage', stage)
102
103
104domain.default_order = 2
105domain.cfl = 1.0
106domain.beta = 0.85
107print "domain.order", domain.default_order
108
109if (debug == True):
110    area = domain.areas
111    for i in range(len(area)):
112        if area != 20:
113            print "Cell Areas are Wrong"
114           
115    L = domain.quantities['stage'].vertex_values
116    print "Initial Stage"
117    print L
118
119domain.set_boundary({'exterior': Reflective_boundary(domain)})
120
121X = domain.vertices
122C = domain.centroids
123plot1 = newLinePlot("Stage")
124plot2 = newLinePlot("Momentum")
125plot3 = newLinePlot("Velocity")
126
127import time
128t0 = time.time()
129yieldstep = 1.0
130finaltime = 50.0
131
132for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime):
133    domain.write_time()
134    if t > 0.0:
135        StageQ = domain.quantities['stage'].vertex_values
136        MomentumQ = domain.quantities['xmomentum'].vertex_values
137        Velocity = MomentumQ/StageQ
138        y , my = analytical_sol(X.flat,domain.time)
139        linePlot(plot1,X,StageQ,X,y)
140        linePlot(plot2,X,MomentumQ,X,my)
141        linePlot(plot3,X,Velocity,X,my/y)
142    #raw_input('press_return')
143    #pass
144
145print 'That took %.2f seconds' %(time.time()-t0)
146
147C = domain.quantities['stage'].centroid_values
148
149if (debug == True):
150    L = domain.quantities['stage'].vertex_values
151    print "Final Stage Vertex Values"
152    print L
153    print "Final Stage Centroid Values"
154    print C
155
156raw_input('press return')
157#f = file('test_solution_I.out', 'w')
158#for i in range(N):
159#    f.write(str(C[i]))
160#    f.write("\n")
161#f.close
162
163#del plot1, plot2,plot3
Note: See TracBrowser for help on using the repository browser.