source: inundation/validation/LWRU2/lwru2.py @ 1744

Last change on this file since 1744 was 1744, checked in by ole, 19 years ago

lwru2.py and future_directions.txt

File size: 5.0 KB
Line 
1"""Example of shallow water wave equation.
2
3This script sets up LWRU2 benchmark with initial condition stated
4
5See also
6
7http://www.cee.cornell.edu/longwave/index.cfm?page=benchmark&problem=2
8
9Depth at western boundary is d = 13.5 cm
10"""
11
12
13read_mesh = False
14
15import sys
16from os import sep
17sys.path.append('..'+sep+'..'+sep)
18
19def prepare_timeboundary(filename):
20    """Converting benchmark 2 time series to NetCDF sww file.
21    """
22
23
24    print 'Preparing time boundary from %s' %filename
25    from Numeric import array
26
27    fid = open(filename)
28
29    #Skip first line
30    line = fid.readline()
31
32    lines = fid.readlines()
33    fid.close()
34
35    N = len(lines)
36
37    T = zeros(N, Float)  #Time
38    Q = zeros(N, Float)  #Values
39
40    for i, line in enumerate(lines):
41        fields = line.split()
42
43        T[i] = float(fields[0])
44        Q[i] = float(fields[1])
45
46    #Create sww file
47    from Scientific.IO.NetCDF import NetCDFFile
48
49    outfile = filename[:-4] + '.sww'
50    print 'Writing to', outfile
51    fid = NetCDFFile(outfile, 'w')
52
53    fid.institution = 'Geoscience Australia'
54    fid.description = 'Input wave for Benchmark 2'
55    fid.starttime = 0.0
56    fid.createDimension('number_of_timesteps', len(T))
57    fid.createVariable('time', Float, ('number_of_timesteps',))
58    fid.variables['time'][:] = T
59
60    fid.createVariable('stage', Float, ('number_of_timesteps',))
61    fid.variables['stage'][:] = Q[:]
62
63    fid.createVariable('xmomentum', Float, ('number_of_timesteps',))
64    fid.variables['xmomentum'][:] = 0.0
65
66    fid.createVariable('ymomentum', Float, ('number_of_timesteps',))
67    fid.variables['ymomentum'][:] = 0.0
68
69    fid.close()
70
71
72# Module imports
73from pyvolution.shallow_water import Domain, Reflective_boundary,\
74            File_boundary, Transmissive_Momentum_Set_Stage_boundary
75from pyvolution.mesh_factory import rectangular
76from pyvolution.pmesh2domain import pmesh_to_domain_instance
77from Numeric import array, zeros, Float, allclose
78import filenames
79from caching import cache
80
81#Preparing time boundary
82prepare_timeboundary(filenames.boundary_filename)
83
84#Preparing points
85from pyvolution.data_manager import xya2pts
86xya2pts(filenames.bathymetry_filename, verbose = True,
87        z_func = lambda z: -z)
88
89
90#######################
91# Domain
92   
93if read_mesh is True:
94    print 'Creating domain from', filenames.mesh_filename
95    domain = pmesh_to_domain_instance(filenames.mesh_filename, Domain)   
96else:   
97    print 'Creating regular mesh'
98    N = 100
99    points, vertices, boundary = rectangular(N, N/5*3,
100                                             len1=5.448, len2=3.402)
101    print 'Creating domain'
102   
103    #domain = Domain(points, vertices, boundary)
104    domain = cache(Domain, (points, vertices, boundary))
105
106
107
108print "Number of triangles = ", len(domain)
109print 'The extent is ', domain.get_extent()
110
111
112#domain.check_integrity()
113domain.default_order = 2
114
115print "Number of triangles = ", len(domain)
116domain.store = True    #Store for visualisation purposes
117
118import sys, os
119base = os.path.basename(sys.argv[0])
120domain.filename, _ = os.path.splitext(base)
121
122#LS code to be included in set_quantity
123print 'Reading points'
124from pyvolution import util, least_squares
125points, attributes = util.read_xya(filenames.bathymetry_filename[:-4] + '.pts')
126elevation = attributes['elevation']
127
128#Fit attributes to mesh
129#vertex_attributes = least_squares.fit_to_mesh(domain.coordinates,
130#                                              domain.triangles,
131#                                              points,
132#                                              attributes['elevation'],
133#                                              alpha = 0.0001,
134#                                              verbose=True)
135
136vertex_attributes = cache(least_squares.fit_to_mesh,
137                          (domain.coordinates, domain.triangles,
138                           points, elevation) ,
139                          {'alpha': 0.0001, 'verbose': True})
140
141print 'Initial values'
142domain.set_quantity('elevation', vertex_attributes)
143#domain.set_quantity('elevation', points=points, attributes=attributes['elevation'], alpha=0.0001)
144domain.set_quantity('friction', 0.0)
145domain.set_quantity('stage', 0.0)
146
147
148
149######################
150# Boundary conditions
151#
152print 'Boundaries'
153Br = Reflective_boundary(domain)
154
155#####################
156# Error with File Boundary. We need to only set
157# stage and let the scheme set the momentum
158from pyvolution.util import file_function
159function = file_function(filenames.boundary_filename[:-4] + '.sww', domain,
160                         verbose = True)
161Bts = Transmissive_Momentum_Set_Stage_boundary(domain, function)
162
163#Set boundary conditions
164if read_mesh is True:
165    domain.set_boundary({'wave': Bts, 'wall': Br})     
166else:   
167    domain.set_boundary({'left': Bts, 'right': Br, 'bottom': Br, 'top': Br})
168
169
170#Evolve
171import time
172t0 = time.time()
173#domain.visualise = True
174
175for t in domain.evolve(yieldstep = 0.05, finaltime = 22.5):
176    domain.write_time()
177    #Stage_cv = domain.quantities['stage'].centroid_values
178    #print Stage_cv[0]
179    #print 'Function value',function(t)
180
181print 'That took %.2f seconds' %(time.time()-t0)
Note: See TracBrowser for help on using the repository browser.