source: inundation/validation/Incomplete/LWRU1/lwru1.py @ 2116

Last change on this file since 2116 was 2116, checked in by steve, 19 years ago
File size: 5.7 KB
Line 
1"""Example of shallow water wave equation.
2
3This script sets up a 2D version of the 1D LWRU1 benchmark with initial condition stated in the file benchmark_1.txt.
4
5See also
6
7http://www.cee.cornell.edu/longwave/index.cfm?page=benchmark&problem=1
8
9
10"""
11
12######################
13# Module imports
14
15from pyvolution.shallow_water import Domain, Reflective_boundary,\
16     Dirichlet_boundary,Transmissive_boundary, Constant_height, Constant_stage
17
18from pyvolution.mesh_factory import rectangular_cross
19from Numeric import array, zeros, Float, allclose
20
21
22#######################
23# Domain
24#
25
26
27print '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
34points, 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
44domain = Domain(points, vertices, boundary)
45
46domain.check_integrity()
47domain.default_order = 2
48
49#Output params
50domain.smooth = True
51domain.reduction = min  #Looks a lot better on top of steep slopes
52print "Number of triangles = ", len(domain)
53
54domain.visualise = False
55domain.store = True    #Store for visualisation purposes
56domain.format = 'sww'   #Native netcdf visualisation format
57
58import sys, os
59base = os.path.basename(sys.argv[0])
60domain.filename, _ = os.path.splitext(base)
61
62
63#Set initial values
64def slope(x, y):
65    return -x/10
66
67
68class 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
138print 'Field values'
139domain.set_quantity('elevation', slope)
140domain.set_quantity('friction', 0.0)
141domain.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#
150print 'Boundaries'
151Br = Reflective_boundary(domain)
152Bt = Transmissive_boundary(domain)
153
154#Constant inflow
155Bd = Dirichlet_boundary([0.0, 0.0, 0.0])
156
157#Set boundary conditions
158domain.set_boundary({'left': Br, 'right': Br, 'bottom': Br, 'top': Br})
159
160
161#Evolve
162import time
163t0 = time.time()
164
165
166
167
168pt = []
169xes = []
170y = 2500
171x0 = -500
172step = 5
173for i in range(1000):
174    x = x0+i*step
175    xes.append(x)
176    pt.append( [x,y] )
177
178from pylab import *
179from pyvolution.least_squares import Interpolation
180
181
182V = domain.get_vertex_coordinates(obj=True) #Why?
183T = domain.get_triangles(obj=True)
184
185
186I = Interpolation(V,
187                  T,
188                  point_coordinates = pt,
189                  verbose = True)
190
191
192f = domain.quantities['elevation'].vertex_values.flat
193z = I.interpolate( f )
194
195print 'xxxxx'
196
197
198f = domain.quantities['stage'].vertex_values.flat
199y = I.interpolate( f )
200
201#ion()
202#plot(xes, y, '-b', xes, z, '-k', [-500, 50000], [0.0, 0.0], '-k')
203ion()
204clf()
205hold(True)
206plot(xes, y, '-b')
207plot(xes, z, '-k')
208plot([-500, 50000], [0.0, 0.0], '-k')
209set( gca(), Ylim=(-100,100) )
210set( gca(), Xlim=(-500,2000) )
211draw()
212ioff()
213
214#raw_input('go')
215for 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
240print 'That took %.2f seconds' %(time.time()-t0)
241show()
Note: See TracBrowser for help on using the repository browser.