1 | """Example of shallow water wave equation. |
---|
2 | |
---|
3 | This script sets up a 2D version of the 1D LWRU1 benchmark with initial condition stated in the file benchmark_1.txt. |
---|
4 | |
---|
5 | See also |
---|
6 | |
---|
7 | http://www.cee.cornell.edu/longwave/index.cfm?page=benchmark&problem=1 |
---|
8 | |
---|
9 | |
---|
10 | """ |
---|
11 | |
---|
12 | ###################### |
---|
13 | # Module imports |
---|
14 | |
---|
15 | from pyvolution.shallow_water import Domain, Reflective_boundary,\ |
---|
16 | Dirichlet_boundary,Transmissive_boundary, Constant_height, Constant_stage |
---|
17 | |
---|
18 | from pyvolution.mesh_factory import rectangular_cross |
---|
19 | from Numeric import array, zeros, Float, allclose |
---|
20 | |
---|
21 | |
---|
22 | ####################### |
---|
23 | # Domain |
---|
24 | # |
---|
25 | |
---|
26 | |
---|
27 | print 'Creating domain' |
---|
28 | #Create basic mesh |
---|
29 | # |
---|
30 | #The initial condition extends 50km off shore |
---|
31 | #and 5,000m is allowed on shore for wetting |
---|
32 | #(only about 200m is expected, though) |
---|
33 | |
---|
34 | points, vertices, boundary = rectangular_cross(150, 15, |
---|
35 | len1=55000, len2=5000, |
---|
36 | origin = (-5000, 0.0)) |
---|
37 | |
---|
38 | #points, vertices, boundary = rectangular_cross(100, 10, |
---|
39 | # len1=55000, len2=5000, |
---|
40 | # origin = (-5000, 0.0)) |
---|
41 | |
---|
42 | |
---|
43 | #Create shallow water domain |
---|
44 | domain = Domain(points, vertices, boundary) |
---|
45 | |
---|
46 | domain.check_integrity() |
---|
47 | domain.default_order = 2 |
---|
48 | |
---|
49 | #Output params |
---|
50 | domain.smooth = True |
---|
51 | domain.reduction = min #Looks a lot better on top of steep slopes |
---|
52 | print "Number of triangles = ", len(domain) |
---|
53 | |
---|
54 | domain.visualise = False |
---|
55 | domain.store = True #Store for visualisation purposes |
---|
56 | domain.format = 'sww' #Native netcdf visualisation format |
---|
57 | |
---|
58 | import sys, os |
---|
59 | base = os.path.basename(sys.argv[0]) |
---|
60 | domain.filename, _ = os.path.splitext(base) |
---|
61 | |
---|
62 | |
---|
63 | #Set initial values |
---|
64 | def slope(x, y): |
---|
65 | return -x/10 |
---|
66 | |
---|
67 | |
---|
68 | class IC_x: |
---|
69 | """ |
---|
70 | Read 1D initial condition and provide values at any x, y |
---|
71 | |
---|
72 | File is assumed to list x values in the first column and |
---|
73 | stage in the second. |
---|
74 | """ |
---|
75 | |
---|
76 | def __init__(self, filename): |
---|
77 | |
---|
78 | self.x = [] |
---|
79 | self.w = [] |
---|
80 | fid = open(filename) |
---|
81 | for line in fid.readlines(): |
---|
82 | fields = line.split() |
---|
83 | assert len(fields) == 2, '%s' %fields |
---|
84 | self.x.append( float(fields[0]) ) |
---|
85 | self.w.append( float(fields[1]) ) |
---|
86 | |
---|
87 | #print 'X', self.x, len(self.x) |
---|
88 | #print 'W', self.w, len(self.w) |
---|
89 | #from pylab import plot, show |
---|
90 | #plot(self.x, self.w) |
---|
91 | #show() |
---|
92 | #import sys; sys.exit() |
---|
93 | |
---|
94 | def __call__(self, x, y): |
---|
95 | |
---|
96 | w = zeros( len(x), Float ) |
---|
97 | for i in range(len(x)): |
---|
98 | xi = x[i] |
---|
99 | |
---|
100 | |
---|
101 | #Find slot |
---|
102 | |
---|
103 | if xi < self.x[0]: |
---|
104 | w[i] = self.w[0] |
---|
105 | elif xi > self.x[-1]: |
---|
106 | w[i] = self.w[-1] |
---|
107 | else: |
---|
108 | index = 0 |
---|
109 | while xi > self.x[index]: index += 1 |
---|
110 | while xi < self.x[index]: index -= 1 |
---|
111 | |
---|
112 | #print xi, index, self.x[index], self.w[index] |
---|
113 | |
---|
114 | if xi == self.x[index]: |
---|
115 | #if allclose(xi, self.x[index]): |
---|
116 | #Protect against case where x is the last value |
---|
117 | # - also works in general when x == self.x[i] |
---|
118 | ratio = 0 |
---|
119 | else: |
---|
120 | #x is now between index and index+1 |
---|
121 | ratio = (xi - self.x[index])/\ |
---|
122 | (self.x[index+1] - self.x[index]) |
---|
123 | |
---|
124 | #print xi, index, self.x[index], ratio |
---|
125 | |
---|
126 | #Compute interpolated value |
---|
127 | if ratio > 0: |
---|
128 | w[i] = self.w[index] +\ |
---|
129 | ratio*(self.w[index+1] - self.w[index]) |
---|
130 | else: |
---|
131 | w[i] = self.w[index] |
---|
132 | |
---|
133 | #print x, w |
---|
134 | return w |
---|
135 | |
---|
136 | |
---|
137 | |
---|
138 | print 'Field values' |
---|
139 | domain.set_quantity('elevation', slope) |
---|
140 | domain.set_quantity('friction', 0.0) |
---|
141 | domain.set_quantity('stage', IC_x('lwru1_IC.txt')) |
---|
142 | |
---|
143 | #import sys; sys.exit() |
---|
144 | |
---|
145 | #print domain.quantities['stage'].centroid_values |
---|
146 | |
---|
147 | ###################### |
---|
148 | # Boundary conditions |
---|
149 | # |
---|
150 | print 'Boundaries' |
---|
151 | Br = Reflective_boundary(domain) |
---|
152 | Bt = Transmissive_boundary(domain) |
---|
153 | |
---|
154 | #Constant inflow |
---|
155 | Bd = Dirichlet_boundary([0.0, 0.0, 0.0]) |
---|
156 | |
---|
157 | #Set boundary conditions |
---|
158 | domain.set_boundary({'left': Br, 'right': Br, 'bottom': Br, 'top': Br}) |
---|
159 | |
---|
160 | |
---|
161 | #Evolve |
---|
162 | import time |
---|
163 | t0 = time.time() |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | |
---|
168 | pt = [] |
---|
169 | xes = [] |
---|
170 | y = 2500 |
---|
171 | x0 = -500 |
---|
172 | step = 5 |
---|
173 | for i in range(1000): |
---|
174 | x = x0+i*step |
---|
175 | xes.append(x) |
---|
176 | pt.append( [x,y] ) |
---|
177 | |
---|
178 | from pylab import * |
---|
179 | from pyvolution.least_squares import Interpolation |
---|
180 | |
---|
181 | |
---|
182 | V = domain.get_vertex_coordinates(obj=True) #Why? |
---|
183 | T = domain.get_triangles(obj=True) |
---|
184 | |
---|
185 | |
---|
186 | I = Interpolation(V, |
---|
187 | T, |
---|
188 | point_coordinates = pt, |
---|
189 | verbose = True) |
---|
190 | |
---|
191 | |
---|
192 | f = domain.quantities['elevation'].vertex_values.flat |
---|
193 | z = I.interpolate( f ) |
---|
194 | |
---|
195 | print 'xxxxx' |
---|
196 | |
---|
197 | |
---|
198 | f = domain.quantities['stage'].vertex_values.flat |
---|
199 | y = I.interpolate( f ) |
---|
200 | |
---|
201 | #ion() |
---|
202 | #plot(xes, y, '-b', xes, z, '-k', [-500, 50000], [0.0, 0.0], '-k') |
---|
203 | ion() |
---|
204 | clf() |
---|
205 | hold(True) |
---|
206 | plot(xes, y, '-b') |
---|
207 | plot(xes, z, '-k') |
---|
208 | plot([-500, 50000], [0.0, 0.0], '-k') |
---|
209 | set( gca(), Ylim=(-100,100) ) |
---|
210 | set( gca(), Xlim=(-500,2000) ) |
---|
211 | draw() |
---|
212 | ioff() |
---|
213 | |
---|
214 | #raw_input('go') |
---|
215 | for t in domain.evolve(yieldstep = 10, finaltime = 300.0): |
---|
216 | domain.write_time() |
---|
217 | |
---|
218 | |
---|
219 | f = domain.quantities['stage'].vertex_values.flat |
---|
220 | y = I.interpolate( f ) |
---|
221 | |
---|
222 | clf() |
---|
223 | hold(True) |
---|
224 | plot(xes, y, '-b') |
---|
225 | plot(xes, z, '-k') |
---|
226 | plot([-500, 50000], [0.0, 0.0], '-k') |
---|
227 | set( gca(), Ylim=(-100,100) ) |
---|
228 | set( gca(), Xlim=(-500,2000) ) |
---|
229 | draw() |
---|
230 | |
---|
231 | |
---|
232 | #raw_input('go') |
---|
233 | |
---|
234 | #print y[:], y.shape |
---|
235 | |
---|
236 | |
---|
237 | |
---|
238 | |
---|
239 | |
---|
240 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
241 | show() |
---|