1 | """ |
---|
2 | |
---|
3 | this test compares the gauge data from a boundary sww file which is precomputed and |
---|
4 | stored in this directory, and the same gauge location from a simple model that uses |
---|
5 | the same boundary sww file for its boundary conditions |
---|
6 | |
---|
7 | Basically tests that file_boundary and evolve is working correctly |
---|
8 | """ |
---|
9 | |
---|
10 | #------------------------------------------------------------------------------ |
---|
11 | # Import necessary modules |
---|
12 | #------------------------------------------------------------------------------ |
---|
13 | |
---|
14 | # Standard modules |
---|
15 | from os import sep,getcwd, access, F_OK, mkdir, getenv |
---|
16 | from os.path import dirname, basename,abspath |
---|
17 | from shutil import copy |
---|
18 | import time, sys, os, tempfile |
---|
19 | #import sys |
---|
20 | #import os |
---|
21 | #import tempfile |
---|
22 | |
---|
23 | # Related major packages |
---|
24 | from anuga.shallow_water import Domain,Dirichlet_boundary,File_boundary,Transmissive_boundary, Field_boundary |
---|
25 | #from anuga.shallow_water.shallow_water_domain import Field_boundary |
---|
26 | from Numeric import allclose, array |
---|
27 | from anuga.pmesh.mesh_interface import create_mesh_from_regions |
---|
28 | from anuga.abstract_2d_finite_volumes.util import start_screen_catcher, copy_code_files, sww2timeseries, get_data_from_file |
---|
29 | #from anuga.caching import myhash |
---|
30 | # Application specific imports |
---|
31 | |
---|
32 | #------------------------------------------------------------------------------ |
---|
33 | # Copy scripts to time stamped output directory and capture screen |
---|
34 | # output to file |
---|
35 | #------------------------------------------------------------------------------ |
---|
36 | out_dir = dirname(abspath(__file__))+sep |
---|
37 | #print'temp_name' |
---|
38 | fileName="temp" |
---|
39 | #import sys |
---|
40 | #sys.exit() |
---|
41 | meshes_dir_name = 'small.tsh' # this will be local |
---|
42 | |
---|
43 | tide = 2.4 |
---|
44 | |
---|
45 | |
---|
46 | #EVOLVED MODEL |
---|
47 | |
---|
48 | #-------------------------------------------------------------------------- |
---|
49 | # Create the triangular mesh based on overall clipping polygon with a |
---|
50 | # tagged |
---|
51 | # boundary and interior regions defined in project.py along with |
---|
52 | # resolutions (maximal area of per triangle) for each polygon |
---|
53 | #-------------------------------------------------------------------------- |
---|
54 | |
---|
55 | #all = [[469000,7760000],[470000,7758000],[468000,7758000]] |
---|
56 | #all = [[470000,7760000],[469000,7758000],[468000,7760000]] |
---|
57 | |
---|
58 | #all_large = [[474000,7764000],[474000,7756000],[466000,7756000],[466000,7764000]] |
---|
59 | #all = [[470000,7764000],[470000,7756000],[466000,7756000],[466000,7764000]] |
---|
60 | all = [[465184,7764500],[470397,7764510],[470407,7758988],[465195,7758979]] |
---|
61 | # N, W, S, E |
---|
62 | |
---|
63 | create_mesh_from_regions(all, |
---|
64 | # boundary_tags={'ocean': [0,1,2]}, |
---|
65 | # boundary_tags={'ocean': [ 2],'side': [0, 1]}, |
---|
66 | boundary_tags={'ocean': [ 0],'side': [1, 3], 'back': [2]}, |
---|
67 | maximum_triangle_area=50000, |
---|
68 | filename=meshes_dir_name, |
---|
69 | use_cache=False, |
---|
70 | # verbose=True |
---|
71 | ) |
---|
72 | |
---|
73 | #------------------------------------------------------------------------- |
---|
74 | # Setup computational domain |
---|
75 | #------------------------------------------------------------------------- |
---|
76 | #print 'Setup computational domain' |
---|
77 | domain = Domain( meshes_dir_name, verbose=True) |
---|
78 | |
---|
79 | from anuga.shallow_water.data_manager import urs2sww |
---|
80 | boundaries_dir_name = 'o_test' |
---|
81 | |
---|
82 | # convert MUX urs files to an SWW file output |
---|
83 | #print 'boundary file is: ',boundaries_dir_name |
---|
84 | from caching import cache |
---|
85 | cache(urs2sww, |
---|
86 | (boundaries_dir_name, |
---|
87 | boundaries_dir_name), |
---|
88 | {'verbose': False, |
---|
89 | 'mint': 9200, 'maxt': 11200, |
---|
90 | # 'origin': domain.geo_reference.get_origin(), |
---|
91 | 'fail_on_NaN': False}, |
---|
92 | verbose = False, |
---|
93 | ) |
---|
94 | |
---|
95 | #------------------------------------------------------------------------- |
---|
96 | # Setup initial conditions |
---|
97 | #------------------------------------------------------------------------- |
---|
98 | |
---|
99 | #print 'Start Set quantity' |
---|
100 | |
---|
101 | domain.set_quantity('elevation', -42.3) |
---|
102 | |
---|
103 | #print 'Finished Set quantity' |
---|
104 | |
---|
105 | #------------------------------------------------------ |
---|
106 | # Set domain parameters |
---|
107 | #------------------------------------------------------ |
---|
108 | |
---|
109 | domain.set_quantity('stage', tide) |
---|
110 | domain.set_quantity('friction', 0.01) |
---|
111 | domain.set_name(fileName) |
---|
112 | domain.set_datadir(out_dir) |
---|
113 | |
---|
114 | #------------------------------------------------------------------------- |
---|
115 | # Setup boundary conditions |
---|
116 | #------------------------------------------------------------------------- |
---|
117 | #Bf = File_boundary(out_dir + 'o_test_8500_12000.sww', |
---|
118 | # domain, time_thinning=24, use_cache=True, verbose=True) |
---|
119 | Bf = Field_boundary(out_dir + 'o_test.sww', |
---|
120 | domain, mean_stage=tide, use_cache=True, verbose=False) |
---|
121 | |
---|
122 | #print 'finished reading boundary file' |
---|
123 | |
---|
124 | #Br = Reflective_boundary(domain) |
---|
125 | Bt = Transmissive_boundary(domain) |
---|
126 | Bd = Dirichlet_boundary([tide,0,0]) |
---|
127 | |
---|
128 | #print'set_boundary' |
---|
129 | |
---|
130 | domain.set_boundary({'back': Bt, |
---|
131 | 'side': Bd, |
---|
132 | 'ocean': Bf |
---|
133 | }) |
---|
134 | #print'finish set boundary' |
---|
135 | |
---|
136 | #---------------------------------------------------------------------------- |
---|
137 | # Evolve system through time |
---|
138 | #---------------------------------------------------------------------------- |
---|
139 | |
---|
140 | #remove mesh file!!! and o_test.sww |
---|
141 | t0 = time.time() |
---|
142 | |
---|
143 | for t in domain.evolve(yieldstep = 60, finaltime = 1920): |
---|
144 | domain.write_time() |
---|
145 | # domain.write_boundary_statistics() |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | #Gets timeseries from boundary sww and evolved sww |
---|
150 | home = getenv('INUNDATIONHOME') #Sandpit's parent dir |
---|
151 | #user = get_user_name() |
---|
152 | data = 'data' |
---|
153 | state = 'western_australia' |
---|
154 | scenario_name = 'dampier.sww' |
---|
155 | |
---|
156 | scenario = 'dampier_tsunami_scenario_2006' |
---|
157 | #scenario = 'test_dampier' |
---|
158 | an = 'anuga' |
---|
159 | bo = 'boundaries' |
---|
160 | |
---|
161 | run_time = 'blank' |
---|
162 | #run_time = project.run_time |
---|
163 | production_dirs = {run_time: 'URS evolved data'#, |
---|
164 | #'boundaries': 'URS boundary condition' |
---|
165 | } |
---|
166 | |
---|
167 | topo = 'topographies' |
---|
168 | out = 'outputs' |
---|
169 | urs = 'urs' |
---|
170 | gridded = '1_10000' |
---|
171 | |
---|
172 | |
---|
173 | #gauge_boundary_filename = 'boundary_gauge_near_top.csv' |
---|
174 | gauge_boundary_filename = 'gauges_time_series_b_near_top.csv' |
---|
175 | gauge_evolved_filename = 'gauges_time_series_near_top.csv' |
---|
176 | |
---|
177 | boundary_dir_filename = os.path.join(out_dir,gauge_boundary_filename) |
---|
178 | #print'boundary_dir_filename',boundary_dir_filename |
---|
179 | |
---|
180 | evolved_dir_filename= os.path.join(out_dir,gauge_evolved_filename) |
---|
181 | |
---|
182 | #print'boundary_dir_filename',boundary_dir_filename |
---|
183 | #print'evolved_dir_filename',evolved_dir_filename |
---|
184 | |
---|
185 | swwfiles = {} |
---|
186 | swwfile = out_dir + fileName + '.sww' |
---|
187 | swwfiles[swwfile] = run_time |
---|
188 | #print"swwfiles",swwfiles,"shallow_water" |
---|
189 | |
---|
190 | texname, elev_output = sww2timeseries(swwfiles, |
---|
191 | out_dir+sep+"gauges.csv", |
---|
192 | production_dirs, |
---|
193 | report = False, |
---|
194 | plot_quantity = ['stage', 'xmomentum', 'ymomentum'], |
---|
195 | surface = False, |
---|
196 | time_min = None, |
---|
197 | time_max = None, |
---|
198 | title_on = False, |
---|
199 | use_cache = True, |
---|
200 | verbose = False) |
---|
201 | |
---|
202 | swwfiles = {} |
---|
203 | swwfile = out_dir + boundaries_dir_name + '.sww' |
---|
204 | swwfiles[swwfile] = run_time |
---|
205 | #print"swwfiles",swwfiles,"shallow_water" |
---|
206 | |
---|
207 | texname, elev_output = sww2timeseries(swwfiles, |
---|
208 | out_dir+sep+"boundary_gauges.csv", |
---|
209 | production_dirs, |
---|
210 | report = False, |
---|
211 | plot_quantity = ['stage', 'xmomentum', 'ymomentum'], |
---|
212 | surface = False, |
---|
213 | time_min = None, |
---|
214 | time_max = None, |
---|
215 | title_on = False, |
---|
216 | use_cache = True, |
---|
217 | verbose = False) |
---|
218 | |
---|
219 | #makes the csv files from the evolved model |
---|
220 | e_header, e_data = get_data_from_file(evolved_dir_filename) |
---|
221 | |
---|
222 | e_time = e_data[:,0] |
---|
223 | e_stage = e_data[:,1] |
---|
224 | e_momentum = e_data[:,2] |
---|
225 | e_speed = e_data[:,3] |
---|
226 | e_elevation = e_data[:,4] |
---|
227 | |
---|
228 | #print'boundary_dir_filename',boundary_dir_filename |
---|
229 | b_header, b_data = get_data_from_file(boundary_dir_filename) |
---|
230 | |
---|
231 | b_time = b_data[:,0] |
---|
232 | b_stage = b_data[:,1] |
---|
233 | b_momentum = b_data[:,2] |
---|
234 | b_speed = b_data[:,3] |
---|
235 | b_elevation = b_data[:,4] |
---|
236 | |
---|
237 | |
---|
238 | # compares the 2 models |
---|
239 | j=0 |
---|
240 | k=0 |
---|
241 | b_sample = [] |
---|
242 | e_sample = [] |
---|
243 | for i in range(len(b_time)): |
---|
244 | # if j<(len(e_time)) and b_time[i] == e_time[j]: |
---|
245 | if j<(len(e_time)) and k<(len(e_time)) and b_time[i] == e_time[j]: |
---|
246 | b_sample.append(float(tide+b_stage[i])) |
---|
247 | e_sample.append(float(e_stage[k])) |
---|
248 | # if k <len(e_time): print 'time e equal b:', b_time[i],i, j, b_stage[i], b_sample[i], e_stage[k],(len(e_time)-1) |
---|
249 | j = j +1 |
---|
250 | k = k +1 |
---|
251 | |
---|
252 | |
---|
253 | #print len(b_sample), len(e_stage),e_stage,type(e_stage) |
---|
254 | e_stage = e_stage.tolist() |
---|
255 | e_stage.pop() |
---|
256 | #e_stage.pop() |
---|
257 | |
---|
258 | #print len(b_sample), len(e_stage),e_stage |
---|
259 | |
---|
260 | |
---|
261 | #assert allclose (b_sample, e_stage, 0.2, 0.2) |
---|
262 | print "test successful" |
---|
263 | os.remove(fileName+'.sww') |
---|
264 | #print 'evolved_dir_filename',evolved_dir_filename |
---|
265 | os.remove('gauges_time_series_near_top.csv') |
---|
266 | os.remove('gauges_time_series_b_near_top.csv') |
---|
267 | os.remove('gauges_t0.csv') |
---|
268 | os.remove('gauges_maxmins.csv') |
---|
269 | os.remove(meshes_dir_name) |
---|
270 | |
---|
271 | |
---|
272 | assert allclose (b_sample, e_sample, 0.5, 0.5) |
---|
273 | |
---|
274 | |
---|
275 | |
---|
276 | |
---|
277 | |
---|
278 | |
---|
279 | |
---|
280 | |
---|
281 | |
---|
282 | |
---|
283 | |
---|
284 | |
---|
285 | |
---|
286 | |
---|
287 | |
---|