source: development/pyvolution-1d/dam2.py @ 3365

Last change on this file since 3365 was 3362, checked in by jakeman, 19 years ago

New files dam2.py and dry_dam.py further test numerical solution
against analytical solution of
Stoker 57. Limiting is still not working correctly.

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