1 | import os |
---|
2 | from math import sqrt, pi |
---|
3 | from shallow_water_1d import * |
---|
4 | from Numeric import allclose, array, zeros, ones, Float, take, sqrt |
---|
5 | from config import g, epsilon |
---|
6 | |
---|
7 | def 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 | |
---|
55 | def 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 | |
---|
64 | def 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 | |
---|
72 | debug = False |
---|
73 | |
---|
74 | print "TEST 1D-SOLUTION II -- 0.1m Deep Downstream" |
---|
75 | |
---|
76 | L = 2000.0 # Length of channel (m) |
---|
77 | N = 100 # Number of compuational cells |
---|
78 | cell_len = L/N # Origin = 0.0 |
---|
79 | |
---|
80 | points = zeros(N+1,Float) |
---|
81 | for i in range(N+1): |
---|
82 | points[i] = i*cell_len |
---|
83 | |
---|
84 | domain = Domain(points) |
---|
85 | |
---|
86 | def 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 | |
---|
95 | domain.set_quantity('stage', stage) |
---|
96 | |
---|
97 | domain.order = 2 |
---|
98 | domain.default_order = 2 |
---|
99 | domain.cfl = 0.5 |
---|
100 | print "domain.order", domain.order |
---|
101 | |
---|
102 | if (debug == True): |
---|
103 | area = domain.areas |
---|
104 | for i in range(len(area)): |
---|
105 | if area != 20: |
---|
106 | print "Cell Areas are Wrong" |
---|
107 | |
---|
108 | L = domain.quantities['stage'].vertex_values |
---|
109 | print "Initial Stage" |
---|
110 | print L |
---|
111 | raw_input('press enter') |
---|
112 | |
---|
113 | domain.set_boundary({'exterior': Reflective_boundary(domain)}) |
---|
114 | |
---|
115 | X = domain.vertices |
---|
116 | C = domain.centroids |
---|
117 | plot1 = newLinePlot("Stage") |
---|
118 | plot2 = newLinePlot("Momentum") |
---|
119 | |
---|
120 | import time |
---|
121 | t0 = time.time() |
---|
122 | yieldstep = 1.0 |
---|
123 | finaltime = 50.0 |
---|
124 | for 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 | |
---|
135 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
136 | |
---|
137 | C = domain.quantities['stage'].centroid_values |
---|
138 | |
---|
139 | if (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 | |
---|