1 | |
---|
2 | import os |
---|
3 | from math import sqrt, pi |
---|
4 | from shallow_water_1d import * |
---|
5 | from Numeric import allclose, array, zeros, ones, Float, take, sqrt |
---|
6 | from config import g, epsilon |
---|
7 | from analytic_dam import AnalyticDam |
---|
8 | |
---|
9 | h0 = 0.1 |
---|
10 | h1 = 10.0 |
---|
11 | |
---|
12 | analytical_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 | |
---|
61 | def 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 | |
---|
70 | def 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 | |
---|
78 | debug = False |
---|
79 | |
---|
80 | print "TEST 1D-SOLUTION I" |
---|
81 | |
---|
82 | L = 2000.0 # Length of channel (m) |
---|
83 | N = 100 # Number of compuational cells |
---|
84 | cell_len = L/N # Origin = 0.0 |
---|
85 | |
---|
86 | points = zeros(N+1,Float) |
---|
87 | for i in range(N+1): |
---|
88 | points[i] = i*cell_len |
---|
89 | |
---|
90 | domain = Domain(points) |
---|
91 | |
---|
92 | def 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 | |
---|
101 | domain.set_quantity('stage', stage) |
---|
102 | |
---|
103 | |
---|
104 | domain.default_order = 2 |
---|
105 | domain.cfl = 1.0 |
---|
106 | domain.beta = 0.85 |
---|
107 | print "domain.order", domain.default_order |
---|
108 | |
---|
109 | if (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 | |
---|
119 | domain.set_boundary({'exterior': Reflective_boundary(domain)}) |
---|
120 | |
---|
121 | X = domain.vertices |
---|
122 | C = domain.centroids |
---|
123 | plot1 = newLinePlot("Stage") |
---|
124 | plot2 = newLinePlot("Momentum") |
---|
125 | plot3 = newLinePlot("Velocity") |
---|
126 | |
---|
127 | import time |
---|
128 | t0 = time.time() |
---|
129 | yieldstep = 1.0 |
---|
130 | finaltime = 50.0 |
---|
131 | |
---|
132 | for 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 | |
---|
145 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
146 | |
---|
147 | C = domain.quantities['stage'].centroid_values |
---|
148 | |
---|
149 | if (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 | |
---|
156 | raw_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 |
---|