1 | import os |
---|
2 | from math import sqrt, pi |
---|
3 | import numpy |
---|
4 | import time |
---|
5 | #from Numeric import allclose, array, zeros, ones, Float, take, sqrt |
---|
6 | |
---|
7 | |
---|
8 | |
---|
9 | from anuga_1d.sww.sww_domain import * |
---|
10 | from anuga_1d.config import g, epsilon |
---|
11 | from anuga_1d.base.generic_mesh import uniform_mesh |
---|
12 | |
---|
13 | h1 = 10.0 |
---|
14 | h0 = 0.0 |
---|
15 | |
---|
16 | def analytical_sol(C,t): |
---|
17 | |
---|
18 | #t = 0.0 # time (s) |
---|
19 | # gravity (m/s^2) |
---|
20 | #h1 = 10.0 # depth upstream (m) |
---|
21 | #h0 = 0.0 # depth downstream (m) |
---|
22 | L = 2000.0 # length of stream/domain (m) |
---|
23 | n = len(C) # number of cells |
---|
24 | |
---|
25 | u = zeros(n,Float) |
---|
26 | h = zeros(n,Float) |
---|
27 | x = C-3*L/4.0 |
---|
28 | |
---|
29 | |
---|
30 | for i in range(n): |
---|
31 | # Calculate Analytical Solution at time t > 0 |
---|
32 | u3 = 2.0/3.0*(sqrt(g*h1)+x[i]/t) |
---|
33 | h3 = 4.0/(9.0*g)*(sqrt(g*h1)-x[i]/(2.0*t))*(sqrt(g*h1)-x[i]/(2.0*t)) |
---|
34 | u3_ = 2.0/3.0*((x[i]+L/2.0)/t-sqrt(g*h1)) |
---|
35 | h3_ = 1.0/(9.0*g)*((x[i]+L/2.0)/t+2*sqrt(g*h1))*((x[i]+L/2.0)/t+2*sqrt(g*h1)) |
---|
36 | |
---|
37 | if ( x[i] <= -1*L/2.0+2*(-sqrt(g*h1)*t)): |
---|
38 | u[i] = 0.0 |
---|
39 | h[i] = h0 |
---|
40 | elif ( x[i] <= -1*L/2.0-(-sqrt(g*h1)*t)): |
---|
41 | u[i] = u3_ |
---|
42 | h[i] = h3_ |
---|
43 | |
---|
44 | elif ( x[i] <= -t*sqrt(g*h1) ): |
---|
45 | u[i] = 0.0 |
---|
46 | h[i] = h1 |
---|
47 | elif ( x[i] <= 2.0*t*sqrt(g*h1) ): |
---|
48 | u[i] = u3 |
---|
49 | h[i] = h3 |
---|
50 | else: |
---|
51 | u[i] = 0.0 |
---|
52 | h[i] = h0 |
---|
53 | |
---|
54 | return h , u*h, u |
---|
55 | |
---|
56 | |
---|
57 | |
---|
58 | def stage(x): |
---|
59 | import numpy |
---|
60 | |
---|
61 | y = numpy.where( (x>=L/4.0) & (x<=3*L/4.0), h1 , h0) |
---|
62 | |
---|
63 | # for i in range(len(x)): |
---|
64 | # if x[i]<=L/4.0: |
---|
65 | # y[i] = h0 |
---|
66 | # elif x[i]<=3*L/4.0: |
---|
67 | # y[i] = h1 |
---|
68 | # else: |
---|
69 | # y[i] = h0 |
---|
70 | return y |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | print "TEST 1D-SOLUTION III -- DRY BED" |
---|
75 | |
---|
76 | |
---|
77 | finaltime = 20.0 |
---|
78 | yieldstep = 1.0 |
---|
79 | L = 2000.0 # Length of channel (m) |
---|
80 | |
---|
81 | k = 0 |
---|
82 | |
---|
83 | N = 2000 |
---|
84 | print "Evaluating domain with %d cells" %N |
---|
85 | domain = Domain(*uniform_mesh(N,x_1=L)) |
---|
86 | |
---|
87 | domain.set_quantity('stage', stage) |
---|
88 | |
---|
89 | Br = Reflective_boundary(domain) |
---|
90 | |
---|
91 | domain.set_boundary({'left': Br, 'right' : Br}) |
---|
92 | domain.set_spatial_order(2) |
---|
93 | domain.set_timestepping_method('rk2') |
---|
94 | domain.set_CFL(1.0) |
---|
95 | domain.set_limiter("minmod_kurganov") |
---|
96 | domain.set_beta(1.5) |
---|
97 | #domain.h0=0.0001 |
---|
98 | |
---|
99 | t0 = time.time() |
---|
100 | |
---|
101 | for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): |
---|
102 | domain.write_time() |
---|
103 | |
---|
104 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
105 | |
---|
106 | |
---|
107 | N = float(N) |
---|
108 | HeightC = domain.quantities['height'].centroid_values |
---|
109 | StageC = domain.quantities['stage'].centroid_values |
---|
110 | BedC = domain.quantities['elevation'].centroid_values |
---|
111 | C = domain.centroids |
---|
112 | |
---|
113 | HeightV = domain.quantities['height'].vertex_values |
---|
114 | StageV = domain.quantities['stage'].vertex_values |
---|
115 | BedV = domain.quantities['elevation'].vertex_values |
---|
116 | VelocityV = domain.quantities['velocity'].vertex_values |
---|
117 | X = domain.vertices |
---|
118 | |
---|
119 | |
---|
120 | import pylab |
---|
121 | |
---|
122 | pylab.hold(False) |
---|
123 | |
---|
124 | plot1 = pylab.subplot(211) |
---|
125 | |
---|
126 | pylab.plot(X.flat,BedV.flat,X.flat,StageV.flat) |
---|
127 | |
---|
128 | plot1.set_ylim([-1,11]) |
---|
129 | |
---|
130 | pylab.xlabel('Position') |
---|
131 | pylab.ylabel('Stage') |
---|
132 | pylab.legend(('Analytical Solution', 'Numerical Solution'), |
---|
133 | 'upper right', shadow=True) |
---|
134 | |
---|
135 | |
---|
136 | plot2 = pylab.subplot(212) |
---|
137 | |
---|
138 | pylab.plot(X.flat,VelocityV.flat) |
---|
139 | plot2.set_ylim([-20,20]) |
---|
140 | |
---|
141 | pylab.xlabel('Position') |
---|
142 | pylab.ylabel('Velocity') |
---|
143 | |
---|
144 | pylab.show() |
---|
145 | |
---|