1 | import os |
---|
2 | from math import sqrt, pi |
---|
3 | from shallow_water_domain 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-3*L/4.0 |
---|
19 | |
---|
20 | |
---|
21 | for i in range(n): |
---|
22 | # Calculate Analytical Solution at time t > 0 |
---|
23 | u3 = 2.0/3.0*(sqrt(g*h1)+x[i]/t) |
---|
24 | h3 = 4.0/(9.0*g)*(sqrt(g*h1)-x[i]/(2.0*t))*(sqrt(g*h1)-x[i]/(2.0*t)) |
---|
25 | |
---|
26 | if ( x[i] <= -t*sqrt(g*h1) ): |
---|
27 | u[i] = 0.0 |
---|
28 | h[i] = h1 |
---|
29 | elif ( x[i] <= 2.0*t*sqrt(g*h1) ): |
---|
30 | u[i] = u3 |
---|
31 | h[i] = h3 |
---|
32 | else: |
---|
33 | u[i] = 0.0 |
---|
34 | h[i] = h0 |
---|
35 | |
---|
36 | return h , u*h |
---|
37 | |
---|
38 | #def newLinePlot(title='Simple Plot'): |
---|
39 | # import Gnuplot |
---|
40 | # gg = Gnuplot.Gnuplot(persist=0) |
---|
41 | # gg.terminal(postscript) |
---|
42 | # gg.title(title) |
---|
43 | # gg('set data style linespoints') |
---|
44 | # gg.xlabel('x') |
---|
45 | # gg.ylabel('y') |
---|
46 | # return gg |
---|
47 | |
---|
48 | #def linePlot(gg,x1,y1,x2,y2): |
---|
49 | # import Gnuplot |
---|
50 | # plot1 = Gnuplot.PlotItems.Data(x1.flat,y1.flat,with="linespoints") |
---|
51 | # plot2 = Gnuplot.PlotItems.Data(x2.flat,y2.flat, with="lines 3") |
---|
52 | # g.plot(plot1,plot2) |
---|
53 | |
---|
54 | |
---|
55 | |
---|
56 | print "TEST 1D-SOLUTION III -- DRY BED" |
---|
57 | |
---|
58 | L = 2000.0 # Length of channel (m) |
---|
59 | N = 800 # Number of compuational cells |
---|
60 | cell_len = L/N # Origin = 0.0 |
---|
61 | |
---|
62 | points = zeros(N+1,Float) |
---|
63 | for i in range(N+1): |
---|
64 | points[i] = i*cell_len |
---|
65 | |
---|
66 | domain = Domain(points) |
---|
67 | |
---|
68 | def stage(x): |
---|
69 | h1 = 10.0 |
---|
70 | h0 = 0.0 |
---|
71 | y = zeros(len(x),Float) |
---|
72 | for i in range(len(x)): |
---|
73 | if x[i]<=L/4.0: |
---|
74 | y[i] = h0 |
---|
75 | elif x[i]<=3*L/4.0: |
---|
76 | y[i] = h1 |
---|
77 | else: |
---|
78 | y[i] = h0 |
---|
79 | return y |
---|
80 | |
---|
81 | |
---|
82 | import time |
---|
83 | |
---|
84 | finaltime = 20.0 |
---|
85 | yieldstep = 1.0 |
---|
86 | L = 2000.0 # Length of channel (m) |
---|
87 | k = 0 |
---|
88 | |
---|
89 | print "Evaluating domain with %d cells" %N |
---|
90 | cell_len = L/N # Origin = 0.0 |
---|
91 | points = zeros(N+1,Float) |
---|
92 | for j in range(N+1): |
---|
93 | points[j] = j*cell_len |
---|
94 | |
---|
95 | domain = Domain(points) |
---|
96 | |
---|
97 | domain.set_quantity('stage', stage) |
---|
98 | domain.set_boundary({'exterior': Reflective_boundary(domain)}) |
---|
99 | domain.default_order = 2 |
---|
100 | domain.default_time_order = 1 |
---|
101 | domain.cfl = 1.0 |
---|
102 | domain.limiter = "vanleer" |
---|
103 | |
---|
104 | t0 = time.time() |
---|
105 | |
---|
106 | s = 'for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): domain.write_time()' |
---|
107 | |
---|
108 | import profile, pstats |
---|
109 | FN = 'profile.dat' |
---|
110 | |
---|
111 | profile.run(s, FN) |
---|
112 | |
---|
113 | #print 'That took %.2f seconds' %(time.time()-t0) |
---|
114 | |
---|
115 | S = pstats.Stats(FN) |
---|
116 | s = S.sort_stats('cumulative').print_stats(30) |
---|
117 | |
---|
118 | print s |
---|
119 | |
---|
120 | N = float(N) |
---|
121 | StageC = domain.quantities['stage'].centroid_values |
---|
122 | XmomC = domain.quantities['xmomentum'].centroid_values |
---|
123 | C = domain.centroids |
---|
124 | h, uh = analytical_sol(C,domain.time) |
---|
125 | h_error = 1.0/(N)*sum(abs(h-StageC)) |
---|
126 | uh_error = 1.0/(N)*sum(abs(uh-XmomC)) |
---|
127 | print "h_error %.10f" %(h_error) |
---|
128 | print "uh_error %.10f"% (uh_error) |
---|
129 | |
---|
130 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
131 | |
---|