""" ----------------------------------------------------------------- model_run.py ANUGA RURAL FLOOD MODELLING TEMPLATE (Domain Construction & Execution) Script for building the model domain and setting initial conditions and (temporally varying) boundary conditions appropriate to the particular scenario-event before running (evolving) the simulation. Most model data for the run is read in or otherwise referenced in model_data.py and imported into model_run.py to create the model mesh and domain before running the simulation. Code Version : 1.00 June 2008 Initial release Author : E Rigby (0437 250 500) ted.rigby@rienco.com.au ------------------------------------------------------------------------------- """ #------------------------------------------------------------------------------ # Import necessary modules #------------------------------------------------------------------------------ # Standard python modules import os import time import sys # Related ANUGA modules from anuga.shallow_water import Domain from anuga.shallow_water import Reflective_boundary from anuga.shallow_water import Dirichlet_boundary from anuga.shallow_water import Time_boundary from anuga.shallow_water import Transmissive_Momentum_Set_Stage_boundary from anuga.shallow_water.shallow_water_domain import Inflow from anuga.abstract_2d_finite_volumes.util import file_function from anuga.abstract_2d_finite_volumes.quantity import Quantity from anuga.pmesh.mesh_interface import create_mesh_from_regions from anuga.utilities.polygon import read_polygon, plot_polygons, Polygon_function from anuga.alpha_shape.alpha_shape import alpha_shape_via_files # Model specific imports import model_data # Defines and provides the scenario-event specific data import get_tuflow_data # Provides the routines to read in tuflow specific data ##################################################################################### # # # S I M U L A T I O N M A I N L I N E # # # ##################################################################################### t0 = time.time() # record start time of this run (secs) print model_data.basename+' >>>> Comencing model_run.py code execution at t= %.2f hours' %(float(time.time()-t0)/3600.0) ##################################################################################### # # # CREATE THE TRIANGULAR MESH # # # ##################################################################################### # Note: The mesh is created based on overall clipping polygon with a tagged # boundary and interior regions as defined in model_data.py along with # resolutions (maximal area per triangle) for each interior polygon print model_data.basename+' >>>> Creating variable mesh within bounding_poly reflecting regional resolutions' # Create the mesh reflecting the bounding (default) and internal region mesh resolutions set in model_data.py # with boundary tags as defined in model_data.py create_mesh_from_regions(model_data.bounding_polygon, boundary_tags = model_data.boundary_strings, maximum_triangle_area = model_data.default_res, minimum_triangle_angle=30.0, interior_regions = model_data.interior_resregions, filename = model_data.mesh_filename, use_cache=True, verbose=model_data.anuga_verbose) domain domain.forcing_terms.append(hydrograph) print model_data.basename+' >>>> Completed assignment of %i inflow hydrographs at t= %.2f hours ' %(len(model_data.infl print model_data.basename+' >>>> Completed mesh creation at t= %.2f hours ' %(float(time.time()-t0)/3600.0) ###################################################################################### # # # CREATE THE COMPUTATIONAL DOMAIN # # # ###################################################################################### print model_data.basename+' >>>> Creating domain from mesh and setting domain parameters' # create domain from mesh meshname domain = Domain(model_data.mesh_filename, use_cache=True, verbose=model_data.anuga_verbose) print domain.statistics() # confirm what has been done # set base domain parameters domain.set_name(model_data.basename) # already created in model_data.py domain.set_datadir(model_data.results_dir) # already created and/or checked in model_data.py domain.set_quantities_to_be_stored(['stage', 'xmomentum', 'ymomentum']) domain.set_minimum_storable_height(0.01) # Remove very shallow water depths from sww domain.set_maximum_allowed_speed(8.0) # Allow Maximum velocity of 8m/s.... ###################################################################################### # # # ASSIGN THE DOMAIN INITIAL CONDITIONS # # # ###################################################################################### #------------------------------------------------------------------------------------- # This section assigns the initial (t=0) conditions for the domain # Domain initial conditions can be fixed, read from a file, or from a function # Note also that anuga permits almost all initial conditions to subsequently vary # with time, so while the following conditions apply to the whole domain at t=0 # all can be modified within the evolve time loop. In this way scour, deposition, # blockage, changes in roughness etc with time can be explicitly included in a simulation #------------------------------------------------------------------------------------- print model_data.basename+' >>>> Assigning initial conditions to %s domain' %(model_data.basename) # Elevation data may be assigned to the domain from a csv or (NetCDF) pts file. # For a smaller csv that will be read in repeatedly, best to use geospatial_data to readin and # export_point_file to save the points permanently in a NetCDF *.pts file. This runs faster # than the block read of a csv - but - is limited to about 6 million points. # Alternatively - by directly assigning elevation from a csv file can take advantage of the block # read in domain.set_quantity so the elevation dataset can be virtually unlimited in size. # In this simulation the size of the elevation dataset is to large for conversion to a (NetCDF) # pts format so data is read in from the raw csv file. domain.set_quantity('elevation', filename = model_data.elev_filename, use_cache=True, verbose=model_data.anuga_verbose, alpha=0.01) # Set the intial water level (stage) across the domain InitialWaterLevel = model_data.InitialWaterLevel # At t=0 water surface is in this model at initial lake level (0.3m) if domain.get_quantity('elevation') < InitialWaterLevel: # This can create ponds if land is below initial tailwater level domain.set_quantity('stage', InitialWaterLevel) # set initial stage at initial tailwater level (wet) else: domain.set_quantity('stage', expression='elevation') # else set initial stage at land level (dry) # Assign uniform initial friction value to domain mesh centroids as initial condition # Note need to re-assess initial friction value based on rough_polys read in from tuflow # files and computed depth during evolve time loop!!!! domain.set_quantity('friction', model_data.default_n, location = 'centroids') ####################################################################################### # # # ASSIGN THE (TEMPORAL) SUBAREAL INFLOWS TO THE DOMAIN # # # ####################################################################################### #-------------------------------------------------------------------------------------- # This section reads the local or total temporal inflows read in in model_data.py from # the Tuflow ts1 files and stored as NETCDF tms file in the tms_files subdirectory, # into the domain. # # Note: The current ANUGA inflow function 'pours' flow onto the domain within a circle # or polygonal area specified by the user at the specified temporal rate. # EHR -- the above inflows should eventually be augmented by inflows at the boundary # (total as distinct from local hydrographs) to better correspond with conventional # flood modelling. #-------------------------------------------------------------------------------------- print model_data.basename+" >>>> Assigning the inflow hydrographs from the (%s) tms files directory" %(model_data.tms_dir) inflow_fields=[] # read in each line of inflow_hydrographs[tms_filename,xcoord,ycoord] and assign as inflow hydrograph to domain for i in range(len(model_data.inflow_hydrographs)) : tms_filename = model_data.inflow_hydrographs[i][0] # extract the inflow hydro tms_filename xcoord = model_data.inflow_hydrographs[i][1] # extract the inflow hydro xcoord ycoord = model_data.inflow_hydrographs[i][2] # extract the inflow hydro ycoord print model_data.basename+' >>>> Reading inflow hydrograph from %s at %7.2f %7.2f and appending to forcing terms ' %(tms_filename,xcoord,ycoord) # convert time series to temporal function flowrate = file_function(tms_filename, quantities='value',boundary_polygon=None) hydrograph = Inflow(domain,center=(xcoord,ycoord),radius=10, rate=flowrate ) # append hydrograph to domain domain.forcing_terms.append(hydrograph) print model_data.basename+' >>>> Completed assignment of %i inflow hydrographs at t= %.2f hours ' %(len(model_data.inflow_hydrographs),float(time.time()-t0)/3600.0) ####################################################################################### # # # ASSIGN THE (TEMPORAL) DOWNSTREAM BOUNDARY CONDITIONS TO THE DOMAIN # # # ####################################################################################### print model_data.basename+' >>>> Assigning the DSBC from the (%s) tms files directory ' %(model_data.tms_dir) print model_data.basename+' >>>> Available boundary tags are ', domain.get_boundary_tags() # convert dsbc time series to function of time tms_filename=model_data.dsbc_hydrographs[0] lake_stage = file_function(tms_filename,quantities='value',boundary_polygon=None) # Note: will need to be modified if more than one dsbcis to be applied print model_data.basename + ' >>>> Applying dsbc %s to model at the Lake_bdry ' %(tms_filename) MaMt_bdry = Reflective_boundary(domain) # all reflective except lake as using inflow() Mriv_bdry = Reflective_boundary(domain) # Really redundant untill get boundary inflows Cbck_bdry = Reflective_boundary(domain) WFra_bdry = Reflective_boundary(domain) EFra_bdry = Reflective_boundary(domain) lake_bdry = Transmissive_Momentum_Set_Stage_boundary(domain=domain,function=lake_stage) closed_bdry = Reflective_boundary(domain) # boundary conditions for current scenario domain.set_boundary({'Lake': lake_bdry, 'MaMt': MaMt_bdry, 'Mriv': Mriv_bdry, 'Cbck': Cbck_bdry, 'WFra': WFra_bdry, 'EFra': EFra_bdry, 'exterior': closed_bdry }) # this relates to the residue of the bounding poly not explicitly asigned above print model_data.basename+' >>>> Completed assignment of %i dsbc boundary conditions to domain at t= %.2f hours ' %(len(model_data.dsbc_hydrographs),float(time.time()-t0)/3600.0) ####################################################################################### # # # READ IN THE (TEMPORALLY DEPTH DEPENDENT) SURFACE ROUGHNESS (FRICTION) PARAMETERS # # # ####################################################################################### #--------------------------------------------------------------------------------------- # This section creates a new domain quantity mat_RID and assigns surface roughness IDs (RID) # values to this quantity for later use in the evolve loop where RID is used with depth to # compute a depth dependent friction at each time step. # # Note that because the polys read in should but may not cover the entire bounding poly area # It is necessarry to declare a default material as in Tuflow applying the default to mat_RID # across the whole bounding poly area before applying (overwriting) with the actual RID polys # and their RID values obtained from the 2d_mat mid/mif files!!!! #-------------------------------------------------------------------------------------- print model_data.basename+' >>>> Creating and assigning mat_RID (surface Roughness ID)instance for use in evolve' # Create an instance of a new domain quantity called mat_RID to store the surface Roughness IDs - fill initially with zeros mat_RID = Quantity(domain) if model_data.model_verbose : print ' ++++ Created a zeroed domain quantity of %s and length %i called mat_RID' %( type(mat_RID),len(mat_RID) ) # Set all of the RID values initially to one fixed default value (makes sure all elements have a RID) mat_RID.set_values(numeric=model_data.default_RID,location='centroids',verbose=True) if model_data.model_verbose : print ' ++++ Initially filled mat_RID with the specified default RID value of %i ' %( model_data.default_RID ) # Assign model_data.mat_RID_list values to domain.from model_data.mat_poly_list for i in range(len(model_data.mat_RID_list)) : mat_RID.set_values(numeric=model_data.mat_RID_list[i],polygon=model_data.mat_poly_list[i]) if model_data.model_verbose : print ' ++++ Upgraded mat_RID spatially with the values set in the mif/mid files ' # Dump some RIDS to confirm assignment occured as intended if model_data.model_verbose : print ' ++++ Check printout of RID assignments to centroids of elements nos 100 200 and 300' print ' ++++ Element 100s RIDs ', mat_RID.get_values(location='centroids',indices=[100]) print ' ++++ Element 200s RIDs ', mat_RID.get_values(location='centroids',indices=[200]) print ' ++++ Element 300s RIDs ', mat_RID.get_values(location='centroids',indices=[300]) print model_data.basename+' >>>> Completed assignment of material (surface roughness) RIDs and default n to domain at t= %.2f hours ' %(float(time.time()-t0)/3600.0) print model_data.basename+' >>>> Completed domain construction at t= %.2f hours ' %(float(time.time()-t0)/3600.0) ######################################################################################### # # # RUN THE SIMULATION # # # ######################################################################################### #---------------------------------------------------------------------------------------- # This section starts the analysis of flow through the domain up to the specified finaltime # All time is in seconds # # Note: The internal computational timestep is set at each timestep to meet a CFL=1 limit. # It is therefore important to examine the mesh statistics to see that there are no # unintended small triangles, particularly in areas of deeper water as DT=DX/(g*H)**0.5 # Yieldstep is the interval at which results are saved to the output (sww) file #---------------------------------------------------------------------------------------- # Note: 'friction' is recomputed as a function of depth and location at each timestep( now each steptime!!) # EHR - could reduce recalc of friction to say every steptime??? this would markedly speedup?? Done?? # EHR - need to set a startime and use in domain.set_time(starttime) to restrict to area of data of interest # not clear how initial conditions etc fit in to a start not at t=0??? # EHR - could restrict update of friction to wet cells only as well print model_data.basename+' >>>> Starting the simulation for ',model_data.catchment,'-',model_data.simclass,'-',model_data.scenario,'-',model_data.event starttime = 0 # start simulation at t=0 EHR not yet implemented ??????? endtime = 9000 # end at 9000 secs (150min) - Increase to 40hrs when debugged!!!!!!!! steptime = 300 # write sww file results out every 300secs = 5min lastprintstep = 0 # initialise lastprintstep print model_data.basename+' >>>> Simulation is for %.2f hours with output to the sww file at %.0f minute intervals ' %(float(endtime/3600.0),float(steptime/60.0)) for t in domain.evolve(yieldstep = steptime, finaltime = endtime): domain.write_time() # confirm t each steptime domain.write_boundary_statistics(tags='Lake', quantities='stage') # indicate bdry stats each steptime depth = domain.create_quantity_from_expression('stage - elevation') # create depth instance for this timestep print ' ++++ Recomputing friction at t= ', t noelementsupdated = 0 # intialise update check # rebuild the 'friction' values adjusted for depth for i in domain.get_wet_elements(): # loop for each wet element in domain element_RID_value=mat_RID.get_values(location='centroids', indices=[i]) # get current elements centroidal RID value # print ' ++++ Element %i centroidal element_RID_value is %s' %( i,element_RID_value[0] ) for k in range(len(model_data.mat_roughness_data_list)): # loop for each matl in tmf list if int(element_RID_value[0]) == int( model_data.mat_roughness_data_list[k][0] ) : # got tmf line for that matl # print ' ++++ found tmf match for RID %i of %s ' %( int(element_RID_value[0]),model_data.mat_roughness_data_list[k][8].rstrip(' ') ) d1 = float(model_data.mat_roughness_data_list[k][4]) n1 = float(model_data.mat_roughness_data_list[k][5]) d2 = float(model_data.mat_roughness_data_list[k][6]) n2 = float(model_data.mat_roughness_data_list[k][7]) noelementsupdated = noelementsupdated + 1 # increment update counter start at zeroth element if depth.get_values(location='centroids', indices=[i]) <= d1 : # recompute friction values from depth for this element get_tuflow_data.py domain.set_quantity('friction', numeric=n1, location='centroids', indices=[i], verbose=model_data.anuga_verbose) elif depth.get_values(location='centroids', indices=[i]) >= d2 : domain.set_quantity('friction',numeric=n2,location='centroids', indices=[i],verbose=model_data.anuga_verbose) else : n3 = n1+(n2-n1)/(d2-d1)* ( depth.get_values(location='centroids',indices=[i])[0] ) domain.set_quantity('friction',numeric=n3,location='centroids', indices=[i],verbose=model_data.anuga_verbose) if model_data.model_verbose : friction = domain.get_quantity('friction') print " ++++ Check some RID,depth, friction values returned during this timestep " print " element 100 ", mat_RID.get_values(location='centroids',indices=[100]), depth.get_values(location='centroids',indices=[100]), friction.get_values(location='centroids',indices=[100]) print " element 200 ", mat_RID.get_values(location='centroids',indices=[200]), depth.get_values(location='centroids',indices=[200]), friction.get_values(location='centroids',indices=[200]) print " element 300 ", mat_RID.get_values(location='centroids',indices=[300]), depth.get_values(location='centroids',indices=[300]), friction.get_values(location='centroids',indices=[300] ) print model_data.basename+' >>>> ',model_data.catchment,'-',model_data.simclass,'-',model_data.scenario,'-',model_data.event,' -- Simulation completed succesfully' print model_data.basename+' >>>> Completed ', endtime/3600.0, 'hours of model simulation at t= %.2f hours' %(float(time.time()-t0)/3600.0)