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