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