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

Last change on this file since 3402 was 3401, checked in by steve, 19 years ago

Setting up analytical solution

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