1 | import os |
---|
2 | from math import sqrt, sin, cos, pi, exp |
---|
3 | from shallow_water_domain_h_nonwellbalanced import * |
---|
4 | from Numeric import zeros, Float |
---|
5 | |
---|
6 | def analytic_dry_dam(C,t): |
---|
7 | #t = 0.0 # time (s) |
---|
8 | g = 9.81 # gravity (m/s^2) |
---|
9 | h1 = 10.0 # depth upstream (m) |
---|
10 | h0 = 0.0 # depth downstream (m) |
---|
11 | L = 2000.0 # length of stream/domain (m) |
---|
12 | n = len(C) # number of cells |
---|
13 | |
---|
14 | u = zeros(n,Float) |
---|
15 | h = zeros(n,Float) |
---|
16 | z = zeros(n,Float) |
---|
17 | x = C-L/2.0 |
---|
18 | |
---|
19 | for i in range(n): |
---|
20 | # Calculate Analytical Solution at time t > 0 |
---|
21 | u3 = 2.0/3.0*(sqrt(g*h1)+x[i]/t) |
---|
22 | h3 = 4.0/(9.0*g)*(sqrt(g*h1)-x[i]/(2.0*t))*(sqrt(g*h1)-x[i]/(2.0*t)) |
---|
23 | |
---|
24 | if ( x[i] <= -t*sqrt(g*h1) ): |
---|
25 | u[i] = 0.0 |
---|
26 | h[i] = h1 |
---|
27 | elif ( x[i] <= 2.0*t*sqrt(g*h1) ): |
---|
28 | u[i] = u3 |
---|
29 | h[i] = h3 |
---|
30 | else: |
---|
31 | u[i] = 0.0 |
---|
32 | h[i] = h0 |
---|
33 | |
---|
34 | return h+z , u*(h+z) |
---|
35 | |
---|
36 | |
---|
37 | def stage_dam(x): |
---|
38 | h1 = 10.0 |
---|
39 | h0 = 0.0 |
---|
40 | y=zeros(len(x), Float) |
---|
41 | for i in range(len(x)): |
---|
42 | if x[i] <= 1000.0: |
---|
43 | y[i] = h1 |
---|
44 | else: |
---|
45 | y[i] = h0 |
---|
46 | return y |
---|
47 | |
---|
48 | |
---|
49 | |
---|
50 | ''' |
---|
51 | def newLinePlot(title='Simple Plot'): |
---|
52 | import Gnuplot |
---|
53 | gg=Gnuplot.Gnuplot(persist=0) |
---|
54 | gg.title(title) |
---|
55 | gg('set data style linespoints') |
---|
56 | gg.xlabel('x') |
---|
57 | gg.ylabel('y') |
---|
58 | return gg |
---|
59 | |
---|
60 | def linePlot(gg, x1, y1, x2, y2): |
---|
61 | import Gnuplot |
---|
62 | plot1=Gnuplot.PlotItems.Data(x1.flat, y1.flat, with="linespoints") |
---|
63 | plot2=Gnuplot.PlotItems.Data(x2.flat, y2.flat, with="lines 3") |
---|
64 | gg.plot(plot1, plot2) |
---|
65 | ''' |
---|
66 | |
---|
67 | |
---|
68 | print "TEST 1D-SOLUTION I" |
---|
69 | |
---|
70 | L=2000.0 |
---|
71 | N=400 |
---|
72 | cell_len=L/N |
---|
73 | points=zeros(N+1, Float) |
---|
74 | for i in range(N+1): |
---|
75 | points[i]=i*cell_len |
---|
76 | domain=Domain(points) |
---|
77 | domain.order = 2 |
---|
78 | domain.set_timestepping_method('rk2') |
---|
79 | domain.cfl = 1.0 |
---|
80 | domain.limiter = "vanleer" |
---|
81 | domain.set_quantity('stage',stage_dam) |
---|
82 | domain.set_boundary({'exterior':Reflective_boundary(domain)}) |
---|
83 | |
---|
84 | X=domain.vertices |
---|
85 | C=domain.centroids |
---|
86 | |
---|
87 | #plot1x=newLinePlot("Stage") |
---|
88 | #plot2x=newLinePlot("Momentum") |
---|
89 | |
---|
90 | print 'THE DOMAIN ORDER is', domain.order |
---|
91 | print 'The Time Stepping Method is', domain.set_timestepping_method |
---|
92 | print 'THE DOMAIN LIMITER is', domain.limiter |
---|
93 | import time |
---|
94 | t0=time.time() |
---|
95 | yieldstep=20.0 |
---|
96 | finaltime=20.0 |
---|
97 | print "integral", domain.quantities['stage'].get_integral() |
---|
98 | for t in domain.evolve(yieldstep=yieldstep, finaltime=finaltime): |
---|
99 | domain.write_time() |
---|
100 | print "integral", domain.quantities['stage'].get_integral() |
---|
101 | if t>0.0: |
---|
102 | StageQ=domain.quantities['stage'].vertex_values |
---|
103 | MomentumQ=domain.quantities['xmomentum'].vertex_values |
---|
104 | ElevationQ=domain.quantities['elevation'].vertex_values |
---|
105 | |
---|
106 | #h, uh=analytical_sol(X.flat, domain.time) |
---|
107 | #linePlot(plot1x, X, StageQ, X, ElevationQ) |
---|
108 | #linePlot(plot2x, X, MomentumQ, X, MomentumQ)#, X, uh) #(plot2x, X, MomentumQ, X, uh) |
---|
109 | #print "press return" |
---|
110 | #pass |
---|
111 | |
---|
112 | |
---|
113 | #This is only for dry dam |
---|
114 | N = float(N) |
---|
115 | StageC = domain.quantities['stage'].centroid_values |
---|
116 | XmomC = domain.quantities['xmomentum'].centroid_values |
---|
117 | ElevationC = domain.quantities['elevation'].centroid_values |
---|
118 | C = domain.centroids |
---|
119 | h, uh = analytic_dry_dam(C,domain.time) |
---|
120 | h_error = 1.0/(N)*sum(abs(h-StageC)) |
---|
121 | uh_error = 1.0/(N)*sum(abs(uh-XmomC)) |
---|
122 | print "h_error %.10f" %(h_error) |
---|
123 | print "uh_error %.10f"% (uh_error) |
---|
124 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
125 | X = domain.vertices |
---|
126 | StageQ = domain.quantities['stage'].vertex_values |
---|
127 | XmomQ = domain.quantities['xmomentum'].vertex_values |
---|
128 | h, uh = analytic_dry_dam(X.flat,domain.time) |
---|
129 | #End for dry dam |
---|
130 | |
---|
131 | |
---|
132 | from pylab import clf,plot,title,xlabel,ylabel,legend,savefig,show,hold,subplot,ion |
---|
133 | #ion() |
---|
134 | hold(False) |
---|
135 | clf() |
---|
136 | |
---|
137 | plot1 = subplot(211) |
---|
138 | plot(X,StageQ, X,h, X,ElevationQ) #(X,ElevationQ,X,StageQ, X,h) |
---|
139 | plot1.set_ylim([-1.0,11.0]) #([9.999,10.006]) #([9.9999,10.0006]) #([-1.0,11.0]) |
---|
140 | xlabel('Position') |
---|
141 | ylabel('Stage') |
---|
142 | legend( ('Numerical Solution', 'Analytical Solution', 'Bed Elevation'), 'upper right', shadow=False) |
---|
143 | |
---|
144 | plot2 = subplot(212) |
---|
145 | plot(X,MomentumQ, X,uh) |
---|
146 | plot2.set_ylim([-5.0,30.0]) |
---|
147 | legend( ('Numerical Solution', 'for momentum'), 'upper right', shadow=False) |
---|
148 | xlabel('Position') |
---|
149 | ylabel('Xmomentum') |
---|
150 | |
---|
151 | #file = "dam_h_" |
---|
152 | #file += str(number_of_cells[i]) |
---|
153 | #file += ".eps" |
---|
154 | #savefig(file) |
---|
155 | show() |
---|
156 | |
---|
157 | print 'That took %.2f seconds'%(time.time()-t0) |
---|
158 | raw_input("Press return key") |
---|
159 | |
---|