Changeset 4742


Ignore:
Timestamp:
Sep 28, 2007, 5:15:52 PM (17 years ago)
Author:
duncan
Message:

ticket#196

Location:
anuga_core/source/anuga/damage_modelling
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/damage_modelling/inundation_damage.py

    r4585 r4742  
    99from Numeric import array, ravel, Float, zeros
    1010from random import choice
     11from types import StringType
    1112
    1213try: 
     
    4142CONT_VALUE_LABEL = 'CONT_VALUE'
    4243
    43 def inundation_damage(sww_base_name, exposure_file_in,
    44                       exposure_file_out=None,
     44def inundation_damage(sww_base_name, exposure_files_in,
     45                      exposure_file_out_marker=None,
    4546                      ground_floor_height=0.3,
    4647                      overwrite=False, verbose=True,
     
    5960    These calculations are done over all the sww files with the sww_base_name
    6061    in the specified directory.
    61     """
    62 
    63     csv = Exposure_csv(exposure_file_in,
    64                        title_check_list=[SHORE_DIST_LABEL,WALL_TYPE_LABEL,
    65                                          STR_VALUE_LABEL,CONT_VALUE_LABEL])
    66     geospatial = csv.get_location()
    67     geospatial = ensure_absolute(geospatial)
    68     max_depths, max_momentums = calc_max_depth_and_momentum(sww_base_name,
    69                                     geospatial,
    70                                     ground_floor_height=ground_floor_height,
    71                                     verbose=verbose,
    72                                     use_cache=use_cache)
    73     edm = EventDamageModel(max_depths,
    74                            csv.get_column(SHORE_DIST_LABEL),
    75                            csv.get_column(WALL_TYPE_LABEL),
    76                            csv.get_column(STR_VALUE_LABEL),
    77                            csv.get_column(CONT_VALUE_LABEL)
    78                            )
    79     results_dic = edm.calc_damage_and_costs(verbose_csv=True, verbose=verbose)
    80     for title, value in results_dic.iteritems():
    81         csv.set_column(title, value, overwrite=overwrite)
    82    
    83     # Save info back to csv file
    84     if exposure_file_out == None:
    85         exposure_file_out = exposure_file_in
    86     csv.save(exposure_file_out)
    87     if verbose: print '\n Augmented building file written to %s \n' %exposure_file_out
     62
     63    exposure_files_in - a file or a list of files to input from
     64    exposure_file_out_marker -  this string will be added to the input file
     65                                name to get the output file name
     66    """
     67    if type(exposure_files_in) == StringType:
     68        exposure_files_in = [exposure_files_in]
     69
     70
     71    for exposure_file_in in exposure_files_in:
     72        csv = Exposure_csv(exposure_file_in,
     73                           title_check_list=[SHORE_DIST_LABEL,WALL_TYPE_LABEL,
     74                                             STR_VALUE_LABEL,CONT_VALUE_LABEL])
     75        geospatial = csv.get_location()
     76        geospatial = ensure_absolute(geospatial)
     77        max_depths, max_momentums = calc_max_depth_and_momentum(sww_base_name,
     78                        geospatial,
     79                        ground_floor_height=ground_floor_height,
     80                        verbose=verbose,
     81                        use_cache=use_cache)
     82        edm = EventDamageModel(max_depths,
     83                               csv.get_column(SHORE_DIST_LABEL),
     84                               csv.get_column(WALL_TYPE_LABEL),
     85                               csv.get_column(STR_VALUE_LABEL),
     86                               csv.get_column(CONT_VALUE_LABEL)
     87                               )
     88        results_dic = edm.calc_damage_and_costs(verbose_csv=True,
     89                                                verbose=verbose)
     90        for title, value in results_dic.iteritems():
     91            csv.set_column(title, value, overwrite=overwrite)
     92   
     93        # Save info back to csv file
     94        if exposure_file_out_marker == None:
     95            exposure_file_out = exposure_file_in
     96        else:
     97            name, extension = exposure_file_in.split('.')
     98            exposure_file_out = name + exposure_file_out_marker + \
     99                                '.' + extension
     100        csv.save(exposure_file_out)
     101        if verbose: print '\n Augmented building file written to %s \n' %exposure_file_out
    88102   
    89103def add_depth_and_momentum2csv(sww_base_name, exposure_file_in,
  • anuga_core/source/anuga/damage_modelling/test_inundation_damage.py

    r4503 r4742  
    204204        #Create a csv file
    205205        self.csv_fileII = tempfile.mktemp(".csv")
    206         fd = open(self.csv_file,'wb')
     206        fd = open(self.csv_fileII,'wb')
    207207        writer = csv.writer(fd)
    208208        writer.writerow(['LONGITUDE','LATITUDE',STR_VALUE_LABEL,CONT_VALUE_LABEL,'ROOF_TYPE',WALL_TYPE_LABEL, SHORE_DIST_LABEL])
     
    210210        writer.writerow(['151','-34.5','150000','76000','Metal','Double Brick',200.])
    211211        writer.writerow(['151','-34.25','150000','76000','Metal','Brick Veneer',200.])
    212         writer.writerow(['151.5','-35.5','199770','130000','Metal','Timber',20.])
    213212        fd.close()
    214213       
     
    226225            pass
    227226        os.remove(self.csv_file)
     227        os.remove(self.csv_fileII)
    228228
    229229   
     
    237237
    238238   
     239    def test_inundation_damage_list_as_input(self):
     240
     241        # Note, this isn't testing the results,
     242        # just that is all runs
     243        sww_file = self.domain.get_name() + "." + self.domain.format
     244        #print "sww_file",sww_file
     245        inundation_damage(sww_file,
     246                          [self.csv_file, self.csv_fileII], verbose=False)
     247
    239248    def test_inundation_damage2(self):
    240249
     
    309318        os.remove(sww.filename)
    310319        os.remove(csv_file)
     320         
     321    def test_inundation_damage_list(self):
     322
     323        # create mesh
     324        mesh_file = tempfile.mktemp(".tsh")   
     325        points = [[0.0,0.0],[6.0,0.0],[6.0,6.0],[0.0,6.0]]
     326        m = Mesh()
     327        m.add_vertices(points)
     328        m.auto_segment()
     329        m.generate_mesh(verbose=False)
     330        m.export_mesh_file(mesh_file)
     331       
     332        #Create shallow water domain
     333        domain = Domain(mesh_file)
     334        os.remove(mesh_file)
     335       
     336        domain.default_order=2
     337        domain.beta_h = 0
     338
     339        #Set some field values
     340        domain.set_quantity('elevation', elevation_function)
     341        domain.set_quantity('friction', 0.03)
     342        domain.set_quantity('xmomentum', 22.0)
     343        domain.set_quantity('ymomentum', 55.0)
     344
     345        ######################
     346        # Boundary conditions
     347        B = Transmissive_boundary(domain)
     348        domain.set_boundary( {'exterior': B})
     349
     350        # This call mangles the stage values.
     351        domain.distribute_to_vertices_and_edges()
     352        domain.set_quantity('stage', 0.3)
     353
     354        #sww_file = tempfile.mktemp("")
     355        domain.set_name('datatest' + str(time.time()))
     356        domain.format = 'sww'
     357        domain.smooth = True
     358        domain.reduction = mean
     359
     360        sww = get_dataobject(domain)
     361        sww.store_connectivity()
     362        sww.store_timestep(['stage', 'xmomentum', 'ymomentum'])
     363        domain.set_quantity('stage', -0.3)
     364        domain.time = 2.
     365        sww.store_timestep(['stage', 'xmomentum', 'ymomentum'])
     366       
     367        #Create a csv file
     368        csv_file = tempfile.mktemp(".csv")
     369        fd = open(csv_file,'wb')
     370        writer = csv.writer(fd)
     371        writer.writerow(['x','y',STR_VALUE_LABEL,CONT_VALUE_LABEL,'ROOF_TYPE',WALL_TYPE_LABEL, SHORE_DIST_LABEL])
     372        writer.writerow([5.5,0.5,'10','130000','Metal','Timber',20])
     373        writer.writerow([4.5,1.0,'150','76000','Metal','Double Brick',20])
     374        writer.writerow([0.1,1.5,'100','76000','Metal','Brick Veneer',300])
     375        writer.writerow([6.1,1.5,'100','76000','Metal','Brick Veneer',300])
     376        fd.close()
     377       
     378        extension = ".csv"
     379        csv_fileII = tempfile.mktemp(extension)
     380        fd = open(csv_fileII,'wb')
     381        writer = csv.writer(fd)
     382        writer.writerow(['x','y',STR_VALUE_LABEL,CONT_VALUE_LABEL,'ROOF_TYPE',WALL_TYPE_LABEL, SHORE_DIST_LABEL])
     383        writer.writerow([5.5,0.5,'10','130000','Metal','Timber',20])
     384        writer.writerow([4.5,1.0,'150','76000','Metal','Double Brick',20])
     385        writer.writerow([0.1,1.5,'100','76000','Metal','Brick Veneer',300])
     386        writer.writerow([6.1,1.5,'100','76000','Metal','Brick Veneer',300])
     387        fd.close()
     388       
     389        sww_file = domain.get_name() + "." + domain.format
     390        #print "sww_file",sww_file
     391        marker='_gosh'
     392        inundation_damage(sww_file, [csv_file, csv_fileII],
     393                          exposure_file_out_marker=marker,
     394                          verbose=False)
     395
     396        # Test one file
     397        csv_handle = Exposure_csv(csv_file[:-4]+marker+extension)
     398        struct_loss = csv_handle.get_column(EventDamageModel.STRUCT_LOSS_TITLE)
     399        #print "struct_loss",struct_loss
     400        struct_loss = [float(x) for x in struct_loss]
     401        assert allclose(struct_loss,[10,150,16.9,0])       
     402        depth = csv_handle.get_column(EventDamageModel.MAX_DEPTH_TITLE)
     403        #print "depth",depth
     404        depth = [float(x) for x in depth]
     405        assert allclose(depth,[5.5,4.5,0.1,-0.3])
     406       
     407        # Test another file
     408        csv_handle = Exposure_csv(csv_fileII[:-4]+marker+extension)
     409        struct_loss = csv_handle.get_column(EventDamageModel.STRUCT_LOSS_TITLE)
     410        #print "struct_loss",struct_loss
     411        struct_loss = [float(x) for x in struct_loss]
     412        assert allclose(struct_loss,[10,150,16.9,0])       
     413        depth = csv_handle.get_column(EventDamageModel.MAX_DEPTH_TITLE)
     414        #print "depth",depth
     415        depth = [float(x) for x in depth]
     416        assert allclose(depth,[5.5,4.5,0.1,-0.3])
     417        os.remove(sww.filename)
     418        os.remove(csv_file)
     419        os.remove(csv_fileII)
    311420       
    312421    def ztest_add_depth_and_momentum2csv(self):
     
    491600    else:
    492601        pass
    493     #suite = unittest.makeSuite(Test_inundation_damage,'test_calc_max_depth_and_momentum')
    494602    suite = unittest.makeSuite(Test_inundation_damage,'test')
     603    #suite = unittest.makeSuite(Test_inundation_damage,'test_inundation_damage_list_as_input')
    495604    runner = unittest.TextTestRunner()
    496605    runner.run(suite)
Note: See TracChangeset for help on using the changeset viewer.