source: inundation/ga/storm_surge/validation/LWRU1/lwru1.py @ 1627

Last change on this file since 1627 was 1627, checked in by ole, 20 years ago

Setting up for tank validation

File size: 4.9 KB
RevLine 
[1627]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
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
34#points, vertices, boundary = rectangular(400, 40,
35#                                         len1=55000, len2=5000,
36#                                         origin = (-5000, 0.0))
37
38points, vertices, boundary = rectangular(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
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]: w[i] = self.w[0]
104            elif xi > self.x[-1]: w[i] = self.w[-1]
105            else:
106                index = 0
107                while xi > self.x[index]: index += 1
108                while xi < self.x[index]: index -= 1
109
110                #print xi, index, self.x[index], self.w[index]                   
111
112                if xi == self.x[index]:
113                #if allclose(xi, self.x[index]):
114                    #Protect against case where x is the last value
115                    # - also works in general when x == self.x[i]
116                    ratio = 0
117                else:
118                    #x is now between index and index+1
119                    ratio = (xi - self.x[index])/\
120                            (self.x[index+1] - self.x[index])
121
122                #print xi, index, self.x[index], ratio
123
124                #Compute interpolated value
125                if ratio > 0:
126                    w[i] = self.w[index] +\
127                           ratio*(self.w[index+1] - self.w[index])
128                else:
129                    w[i] = self.w[index]
130
131        #print x, w           
132        return w   
133
134
135
136print 'Field values'
137domain.set_quantity('elevation', slope)
138domain.set_quantity('friction', 0.0)
139domain.set_quantity('stage', IC_x('lwru1_IC.txt'))
140
141#import sys; sys.exit()
142
143#print domain.quantities['stage'].centroid_values
144
145######################
146# Boundary conditions
147#
148print 'Boundaries'
149Br = Reflective_boundary(domain)
150Bt = Transmissive_boundary(domain)
151
152#Constant inflow
153Bd = Dirichlet_boundary([0.0, 0.0, 0.0])
154
155#Set boundary conditions
156domain.set_boundary({'left': Bd, 'right': Bd, 'bottom': Bd, 'top': Bd})
157
158
159#Evolve
160import time
161t0 = time.time()
162
163
164
165
166pt = []
167xes = []
168y = 2500 
169x0 = -5000
170step = 50
171for i in range(1000):
172    x = x0+i*step
173    xes.append(x)
174    pt.append( [x,y] )
175
176from pylab import *
177from pyvolution.least_squares import Interpolation
178I = Interpolation(domain.coordinates,
179                  domain.triangles,
180                  point_coordinates = pt,
181                  verbose = True)
182
183
184print 'xxxxx'
185
186hold(False)
187for t in domain.evolve(yieldstep = 1, finaltime = 220.0):
188
189    f = domain.quantities['stage'].get_vertex_values(smooth = True)
190    y = I.interpolate( f )
191
192    ion()
193    plot(xes, y)   
194    domain.write_time()
195
196
197print 'That took %.2f seconds' %(time.time()-t0)
198show()
Note: See TracBrowser for help on using the repository browser.