source: anuga_work/production/perth/run_perth.py @ 5607

Last change on this file since 5607 was 5607, checked in by kristy, 15 years ago
File size: 11.4 KB
Line 
1"""Script for running tsunami inundation scenario for Dampier, WA, Australia.
2
3Source data such as elevation and boundary data is assumed to be available in
4directories specified by project.py
5The output sww file is stored in project.output_run_time_dir
6
7The scenario is defined by a triangular mesh created from project.polygon,
8the elevation data and a simulated tsunami generated with URS code.
9
10Ole Nielsen and Duncan Gray, GA - 2005 and Jane Sexton, Nick Bartzis, GA - 2006
11"""
12
13#------------------------------------------------------------------------------
14# Import necessary modules
15#------------------------------------------------------------------------------
16
17# Standard modules
18from os import sep
19import os
20from os.path import dirname, basename
21from os import mkdir, access, F_OK
22from shutil import copy
23import time
24import sys
25
26# Related major packages
27from anuga.shallow_water import Domain
28from anuga.shallow_water import Dirichlet_boundary
29from anuga.shallow_water import File_boundary
30from anuga.shallow_water import Reflective_boundary
31from anuga.shallow_water import Field_boundary
32from Numeric import allclose
33from anuga.shallow_water.data_manager import export_grid, create_sts_boundary
34
35from anuga.pmesh.mesh_interface import create_mesh_from_regions
36from anuga.shallow_water.data_manager import start_screen_catcher, copy_code_files,store_parameters
37#from anuga_parallel.parallel_api import distribute, numprocs, myid, barrier
38from anuga_parallel.parallel_abstraction import get_processor_name
39from anuga.caching import myhash
40from anuga.damage_modelling.inundation_damage import add_depth_and_momentum2csv, inundation_damage
41from anuga.fit_interpolate.benchmark_least_squares import mem_usage
42from anuga.utilities.polygon import read_polygon, plot_polygons, polygon_area, is_inside_polygon
43from anuga.geospatial_data.geospatial_data import find_optimal_smoothing_parameter
44
45# Application specific imports
46import project                 # Definition of file names and polygons
47numprocs = 1
48myid = 0
49
50def run_model(**kwargs):
51   
52
53    #------------------------------------------------------------------------------
54    # Copy scripts to time stamped output directory and capture screen
55    # output to file
56    #------------------------------------------------------------------------------
57    print "Processor Name:",get_processor_name()
58
59    #copy script must be before screen_catcher
60    #print kwargs
61
62    print 'output_dir',kwargs['output_dir']
63    if myid == 0:
64        copy_code_files(kwargs['output_dir'],__file__, 
65                 dirname(project.__file__)+sep+ project.__name__+'.py' )
66
67        store_parameters(**kwargs)
68
69   # barrier()
70
71    start_screen_catcher(kwargs['output_dir'], myid, numprocs)
72
73    print "Processor Name:",get_processor_name()
74
75   
76    #-----------------------------------------------------------------------
77    # Domain definitions
78    #-----------------------------------------------------------------------
79
80    # Read in boundary from ordered sts file
81    urs_bounding_polygon=create_sts_boundary(os.path.join(project.boundaries_dir,project.scenario_name))
82
83    # Reading the landward defined points, this incorporates the original clipping
84    # polygon minus the 100m contour
85    landward_bounding_polygon = read_polygon(project.polygons_dir+'landward_bounding_polygon.txt')
86
87    # Combine sts polyline with landward points
88    bounding_polygon = urs_bounding_polygon + landward_bounding_polygon
89   
90    # counting segments
91    N = len(urs_bounding_polygon)-1
92    boundary_tags={'back': [N+1,N+2,N+3], 'side': [N,N+4],'ocean': range(N)}
93
94   
95    #--------------------------------------------------------------------------
96    # Create the triangular mesh based on overall clipping polygon with a
97    # tagged
98    # boundary and interior regions defined in project.py along with
99    # resolutions (maximal area of per triangle) for each polygon
100    #--------------------------------------------------------------------------
101
102    #IMPORTANT don't cache create_mesh_from_region and Domain(mesh....) as it
103    # causes problems with the ability to cache set quantity which takes alot of times
104    if myid == 0:
105   
106        print 'start create mesh from regions'
107
108        create_mesh_from_regions(bounding_polygon,
109                             boundary_tags=boundary_tags,
110                             maximum_triangle_area=project.res_poly_all,
111                             interior_regions=project.interior_regions,
112                             filename=project.meshes_dir_name+'.msh',
113                             use_cache=True,
114                             verbose=True)
115   # barrier()
116
117        covariance_value,alpha = find_optimal_smoothing_parameter (data_file= kwargs['elevation_file'],
118                                alpha_list=[0.001, 0.01, 0.1, 0.15, 0.2, 0.25, 0.3, 0.4, 0.5],
119                                mesh_file = project.meshes_dir_name+'.msh')
120        print 'optimal alpha', covariance_value,alpha       
121
122
123    #-------------------------------------------------------------------------
124    # Setup computational domain
125    #-------------------------------------------------------------------------
126    print 'Setup computational domain'
127
128    domain = Domain(project.meshes_dir_name+'.msh', use_cache=False, verbose=True)
129    print 'memory usage before del domain',mem_usage()
130       
131    print domain.statistics()
132    print 'triangles',len(domain)
133   
134    kwargs['act_num_trigs']=len(domain)
135
136
137    #-------------------------------------------------------------------------
138    # Setup initial conditions
139    #-------------------------------------------------------------------------
140    if myid == 0:
141
142        print 'Setup initial conditions'
143
144        from polygon import Polygon_function
145        #following sets the stage/water to be offcoast only
146        IC = Polygon_function( [(project.poly_mainland, 0)], default = kwargs['tide'],
147                                 geo_reference = domain.geo_reference)
148        domain.set_quantity('stage', IC)
149        #domain.set_quantity('stage',kwargs['tide'] )
150        domain.set_quantity('friction', kwargs['friction']) 
151       
152        print 'Start Set quantity',kwargs['elevation_file']
153
154        domain.set_quantity('elevation', 
155                            filename = kwargs['elevation_file'],
156                            use_cache = False,
157                            verbose = True,
158                            alpha = kwargs['alpha'])
159        print 'Finished Set quantity'
160    #barrier()
161
162##    #------------------------------------------------------
163##    # Create x,y,z file of mesh vertex!!!
164##    #------------------------------------------------------
165##        coord = domain.get_vertex_coordinates()
166##        depth = domain.get_quantity('elevation')
167##       
168##        # Write vertex coordinates to file
169##        filename=project.vertex_filename
170##        fid=open(filename,'w')
171##        fid.write('x (m), y (m), z(m)\n')
172##        for i in range(len(coord)):
173##            pt=coord[i]
174##            x=pt[0]
175##            y=pt[1]
176##            z=depth[i]
177##            fid.write('%.6f,%.6f,%.6f\n' %(x, y, z))
178##
179
180    #------------------------------------------------------
181    # Distribute domain to implement parallelism !!!
182    #------------------------------------------------------
183
184    if numprocs > 1:
185        domain=distribute(domain)
186
187    #------------------------------------------------------
188    # Set domain parameters
189    #------------------------------------------------------
190    print 'domain id', id(domain)
191    domain.set_name(kwargs['aa_scenario_name'])
192    domain.set_datadir(kwargs['output_dir'])
193    domain.set_default_order(2) # Apply second order scheme
194    domain.set_minimum_storable_height(0.01) # Don't store anything less than 1cm
195    domain.set_store_vertices_uniquely(False)
196    domain.set_quantities_to_be_stored(['stage', 'xmomentum', 'ymomentum'])
197    domain.tight_slope_limiters = 1
198    #domain.set_maximum_allowed_speed(0.1) # Allow a little runoff (0.1 is OK)
199    print 'domain id', id(domain)
200
201
202    #-------------------------------------------------------------------------
203    # Setup boundary conditions
204    #-------------------------------------------------------------------------
205    print 'Available boundary tags', domain.get_boundary_tags()
206    print 'domain id', id(domain)
207   
208    boundary_urs_out=project.boundaries_dir_name
209   
210    print 'Available boundary tags', domain.get_boundary_tags()
211    Bf = Field_boundary(boundary_urs_out+'.sts',  # Change from file_boundary
212                   domain, mean_stage= project.tide,
213                   time_thinning=1,
214                   use_cache=True,
215                   verbose = True)
216                  # boundary_polygon=bounding_polygon)
217
218    Br = Reflective_boundary(domain)
219    Bd = Dirichlet_boundary([kwargs['tide'],0,0])
220
221    print dir(Bf)
222    print 'start reading boundary file'
223   
224
225##    Bf = Field_boundary(kwargs['boundary_file'],
226##                domain, time_thinning=kwargs['time_thinning'], mean_stage=kwargs['tide'],
227##                use_cache=False, verbose=True)
228
229    domain.set_boundary({'back': Br,
230                         'side': Bd,
231                         'ocean': Bf}) 
232
233    kwargs['input_start_time']=domain.starttime
234
235    print'finish set boundary'
236
237    #----------------------------------------------------------------------------
238    # Evolve system through time
239    #--------------------------------------------------------------------
240    t0 = time.time()
241
242    using_sts_boundary = True 
243   
244    for t in domain.evolve(yieldstep = project.yieldstep, finaltime = kwargs['finaltime']
245                       ,skip_initial_step = False): 
246        domain.write_time()
247        domain.write_boundary_statistics(tags = 'ocean')
248        if using_sts_boundary is True:
249            if Bf.values >= 99:
250                domain.set_boundary({'ocean': Bd})
251                using_sts_boundary = False
252
253    x, y = domain.get_maximum_inundation_location()
254    q = domain.get_maximum_inundation_elevation()
255
256    print 'Maximum runup observed at (%.2f, %.2f) with elevation %.2f' %(x,y,q)
257
258    print 'That took %.2f seconds' %(time.time()-t0)
259
260    #kwargs 'completed' must be added to write the final parameters to file
261    kwargs['completed']=str(time.time()-t0)
262   
263    if myid==0:
264        store_parameters(**kwargs)
265   # barrier
266   
267    print 'memory usage before del domain1',mem_usage()
268   
269   
270#-------------------------------------------------------------
271if __name__ == "__main__":
272   
273    kwargs={}
274    kwargs['est_num_trigs']=project.trigs_min
275    kwargs['num_cpu']=numprocs
276    kwargs['host']=project.host
277    kwargs['res_factor']=project.res_factor
278    kwargs['starttime']=project.starttime
279    kwargs['yieldstep']=project.yieldstep
280    kwargs['finaltime']=project.finaltime
281   
282    kwargs['output_dir']=project.output_run_time_dir
283    kwargs['elevation_file']=project.combined_dir_name+'.pts'
284#    kwargs['elevation_file']=project.combined_small_dir_name + '.pts'
285    kwargs['boundary_file']=project.boundaries_in_dir_name + '.sww'
286    kwargs['file_name']=project.home+'detail.csv'
287    kwargs['aa_scenario_name']=project.scenario_name
288    kwargs['ab_time']=project.time
289    kwargs['res_factor']= project.res_factor
290    kwargs['tide']=project.tide
291    kwargs['user']=project.user
292    kwargs['alpha'] = project.alpha
293    kwargs['friction']=project.friction
294    kwargs['time_thinning'] = project.time_thinning
295    kwargs['dir_comment']=project.dir_comment
296    kwargs['export_cellsize']=project.export_cellsize
297   
298
299    run_model(**kwargs)
300     
301    if myid==0:
302        export_model(**kwargs)
303    #barrier
Note: See TracBrowser for help on using the repository browser.