Changeset 3337


Ignore:
Timestamp:
Jul 14, 2006, 6:05:58 PM (19 years ago)
Author:
duncan
Message:

updating. still work in progress.

Location:
inundation/damage
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/damage/inundation_damage.py

    r3334 r3337  
    4848                                                          verbose=verbose,
    4949                                                          use_cache=use_cache)
    50     #Do the two damage curves
    51     # Ole probably knows if ther is good code for this lying around
    52     #Do the prob of struct collapse
    53     #Do economics
     50    edm = EventDamageModel(max_depths,
     51                           csv.get_column('SHORE_DIST'),
     52                           csv.get_column('WALLS'),
     53                           csv.get_column('STR_VALUE'),
     54                           csv.get_column('C_VALUE')
     55                           )
     56    results_dic = edm.calc_damage_and_costs(verbose_csv=True)
     57    for title, value in results_dic.iteritems():
     58        csv.set_colum(title, value)
     59   
    5460    # Save info back to csv file
    55 
     61    csv.save(exposure_file_out)
    5662   
    5763def add_depth_and_momentum2csv(sww_file, exposure_file_in,
     
    120126
    121127    """
     128    STRUCT_LOSS_TITLE = "Structure Loss ($)"
     129    CONTENTS_LOSS_TITLE = "Contents Loss ($)"
     130    CONTENTS_DAMAGE_TITLE = "Contents damaged (fraction)"
     131    STRUCT_DAMAGE_TITLE = "Structure damaged (fraction)"
     132    COLLAPSE_CSV_INFO_TITLE = "Calculation notes"
    122133    double_brick_damage_array = array([[-kinds.default_float_kind.MAX, 0.0],
    123134                                       [0.0-depth_epsilon, 0.0],
    124                                        [0.0,1.6],
    125                                        [0.1,15.0],
    126                                        [0.3,42.5],
    127                                        [0.5,44.9],
    128                                        [1.0,57.2],
    129                                        [1.5,58.2],
    130                                        [2.0,58.7],
    131                                        [2.5,64.7],
     135                                       [0.0,0.016],
     136                                       [0.1,0.150],
     137                                       [0.3,0.425],
     138                                       [0.5,0.449],
     139                                       [1.0,0.572],
     140                                       [1.5,0.582],
     141                                       [2.0,0.587],
     142                                       [2.5,0.647],
    132143                                       [kinds.default_float_kind.MAX,64.7]])
    133144    double_brick_damage_curve = InterpolatingFunction( \
     
    137148    brick_veeer_damage_array = array([[-kinds.default_float_kind.MAX, 0.0],
    138149                                       [0.0-depth_epsilon, 0.0],
    139                                        [0.0,1.6],
    140                                        [0.1,16.9],
    141                                        [0.3,44.5],
    142                                        [0.5,47.2],
    143                                        [1.0,61.8],
    144                                        [1.5,62.9],
    145                                        [2.0,63.3],
    146                                        [2.5,69.4],
     150                                       [0.0,0.016],
     151                                       [0.1,0.169],
     152                                       [0.3,0.445],
     153                                       [0.5,0.472],
     154                                       [1.0,0.618],
     155                                       [1.5,0.629],
     156                                       [2.0,0.633],
     157                                       [2.5,0.694],
    147158                                       [kinds.default_float_kind.MAX,69.4]])
    148159    brick_veeer_damage_curve = InterpolatingFunction( \
     
    155166    contents_damage_array = array([[-kinds.default_float_kind.MAX, 0.0],
    156167                                       [0.0-depth_epsilon, 0.0],
    157                                        [0.0,1.3],
    158                                        [0.1,10.2],
    159                                        [0.3,38.1],
    160                                        [0.5,50.0],
    161                                        [1.0,97.0],
    162                                        [1.5,97.6],
    163                                        [2.0,98.6],
     168                                       [0.0,0.013],
     169                                       [0.1,0.102],
     170                                       [0.3,0.381],
     171                                       [0.5,0.500],
     172                                       [1.0,0.970],
     173                                       [1.5,0.976],
     174                                       [2.0,0.986],
    164175                                       [kinds.default_float_kind.MAX,98.6]])
    165176    contents_damage_curve = InterpolatingFunction( \
    166177        (ravel(contents_damage_array[:,0:1]),),
    167178        ravel(contents_damage_array[:,1:]))
    168    
    169     #Do contents stuff
    170     #Do the big one, collapsing
    171     #Do the testing of this stuff.
    172    
     179
     180    #building collapse probability
     181    # inundation depth above ground floor, m
     182    depth_upper_limits = [depth_epsilon, 1.0, 2.0, 3.0, 5.0, kinds.default_float_kind.MAX]
     183    # shore mistance, m
     184    shore_upper_limits = [125,200,250, kinds.default_float_kind.MAX]
     185    # Building collapse probability
     186    collapse_probability = [[0.0, 0.0, 0.0, 0.0], #Code below assumes 0.0
     187                            [0.05, 0.02, 0.01, 0.0],
     188                            [0.6, 0.3, 0.1, 0.05],
     189                            [0.8, 0.4, 0.25, 0.15],
     190                            [0.95, 0.7, 0.5, 0.3],
     191                            [0.99, 0.9, 0.65, 0.45]]
    173192   
    174193    def __init__(self,max_depths, shore_distances, walls,
     
    188207        #assert  self.structure_count == len(self.)
    189208       
    190 
     209    def calc_damage_and_costs(self, verbose_csv=False):
     210        self.calc_damage_percentages()
     211        collapse_probability = self.calc_collapse_probability()
     212        self._calc_collapse_structures(collapse_probability,
     213                                  verbose_csv=verbose_csv)
     214        self.calc_cost()
     215        results_dict = {self.STRUCT_LOSS_TITLE:self.struct_loss
     216                        ,self.STRUCT_DAMAGE_TITLE:self.struct_damage
     217                        ,self.CONTENTS_LOSS_TITLE:self.contents_loss
     218                        ,self.CONTENTS_DAMAGE_TITLE:self.contents_damage
     219                        #,self.:self.
     220                        #,self.:self.
     221                        #,self.:self.
     222                        }
     223        if verbose_csv:
     224           results_dict[self.COLLAPSE_CSV_INFO_TITLE] = self.collapse_csv_info
     225        return results_dict
     226           
    191227    def calc_damage_percentages(self):
    192228
     
    213249            struct_damage[i] = damage_curve(max_depth)
    214250            contents_damage[i] = self.contents_damage_curve(max_depth)
    215             #build collapse_probability
    216         self.collapse_probability = {0.4:[0],
    217                                 0.6:[1],
    218                                 0.5:[2],
    219                                 0.25:[3,4],
    220                                 0.1:[5,6,7,8],
    221                                 0.2:[9,10,11,12,13,14,15,16]}
     251           
    222252        self.struct_damage = struct_damage
    223253        self.contents_damage = contents_damage
     
    229259        self.contents_loss = self.contents_damage * \
    230260                           ensure_numeric(self.content_costs)
    231     def BAD_calc_collapse_probability(self, depth, shore_distance):
    232         # do the table thing.
    233         collapse_probability = {0.4:[0],
    234                                 0.6:[1],
    235                                 0.5:[2],
    236                                 0.25:[3,4],
    237                                 0.1:[5,6,7,8],
    238                                 0.2:[9,10,11,12,13,14,15,16]}
    239         return collapse_probability
     261       
     262    def calc_collapse_probability(self):
     263        """
     264        return a dict of which structures have x probability of collapse.
     265             key is collapse probability
     266             value is list of struct indexes with key probability of collapse
     267        """
     268        # I could've done this is the calc_damage_percentages and
     269        # Just had one loop.
     270        # But for ease of testing and bug finding I'm seperating the loops.
     271        # I'm make the outer loop for both of them the same though,
     272        # so this loop can easily be folded into the other loop.
     273       
     274        # dict of which structures have x probability of collapse.
     275        # key of collapse probability
     276        # value of list of struct indexes
     277        struct_coll_prob = {}
     278       
     279        for i,max_depth,shore_distance,wall in map(None,
     280                                                   range(self.structure_count),
     281                                                   self.max_depths,
     282                                                   self.shore_distances,
     283                                                   self.walls):
     284            #print "i",i
     285            #print "max_depth",max_depth
     286            #print "shore_distance",shore_distance
     287            #print "wall",wall
     288            # WARNING ASSUMING THE FIRST BIN OF DEPTHS GIVE A ZERO PROBABILITY
     289            depth_upper_limits = self.depth_upper_limits
     290            shore_upper_limits = self.shore_upper_limits
     291            collapse_probability = self.collapse_probability
     292            if max_depth <= depth_upper_limits[0]:
     293                continue
     294            start = 1
     295            for i_depth, depth_limit in enumerate(depth_upper_limits[start:]):
     296                #Have to change i_depth so it indexes into the lists correctly
     297                i_depth += start
     298                if max_depth <= depth_limit:
     299                    for i_shore, shore_limit in enumerate(shore_upper_limits):
     300                        if shore_distance <= shore_limit:
     301                            coll_prob = collapse_probability[i_depth][i_shore]
     302                            if 0.0 == collapse_probability[i_depth][i_shore]:
     303                                break
     304                            struct_coll_prob.setdefault(coll_prob,[]).append(i)
     305                            break
     306                    break
     307                           
     308        return struct_coll_prob
    240309   
    241310    def _calc_collapse_structures(self, collapse_probability,
     
    254323                for i in house_indexes:
    255324                    # This could be sped up I think
    256                     self.collapse_csv_info[i] = str(probability) + ' prob., ' \
     325                    self.collapse_csv_info[i] = str(probability) + ' prob.( ' \
    257326                           + str(int(collapse_count)) + ' collapsed out of ' \
    258                            + str(len(house_indexes))
     327                           + str(len(house_indexes)) + ')'
    259328            for _ in range(int(collapse_count)):
    260329                house_index = choice(house_indexes)
    261                 self.struct_loss[house_index] = 1.0
    262                 self.contents_loss[house_index] = 1.0
     330                self.struct_damage[house_index] = 1.0
     331                self.contents_damage[house_index] = 1.0
    263332                house_indexes.remove(house_index)
    264333            # Warning, the collapse_probability list now lists
     
    289358    print dict.get(100,dict[2])
    290359
    291        
     360    inundation_damage('source.sww', 'augmented_buildings_high.csv',
     361                      'augmented_buildings_high_dsg.csv')       
  • inundation/damage/test_inundation_damage.py

    r3334 r3337  
    141141
    142142   
    143     def test_inundation_damage(self):
     143    def ztest_inundation_damage(self):
    144144        sww_file = self.domain.filename + "." + self.domain.format
    145145        #print "sww_file",sww_file
     
    189189                               struct_costs, content_costs)
    190190        edm.calc_damage_percentages()
    191         assert allclose(edm.struct_damage,[0.0,1.6,57.2,
    192                                             0.0,1.6,61.8,
    193                                             0.0,1.6,61.8])
    194         assert allclose(edm.contents_damage,[0.0,1.3,97.0,
    195                                              0.0,1.3,97.0,
    196                                              0.0,1.3,97.0])
     191        assert allclose(edm.struct_damage,[0.0,0.016,0.572,
     192                                            0.0,0.016,0.618,
     193                                            0.0,0.016,0.618])
     194        assert allclose(edm.contents_damage,[0.0,0.013,0.970,
     195                                             0.0,0.013,0.970,
     196                                             0.0,0.013,0.970])
    197197        edm.calc_cost()
    198         assert allclose(edm.struct_loss,[0.0,16,572,
    199                                             0.0,16,618,
    200                                             0.0,1.6,61.8])
    201         assert allclose(edm.contents_loss,[0.0,130,9700,
    202                                              0.0,130,9700,
    203                                              0.0,13,970])
     198        assert allclose(edm.struct_loss,[0.0,.16,5.72,
     199                                            0.0,.16,6.18,
     200                                            0.0,0.016,0.618])
     201        assert allclose(edm.contents_loss,[0.0,1.3,97,
     202                                             0.0,1.3,97,
     203                                             0.0,0.13,9.7])
    204204       
    205205       
     
    207207        edm = EventDamageModel([0.0]*17, [0.0]*17, [0.0]*17,
    208208                               [0.0]*17, [0.0]*17)
    209         edm.struct_loss = zeros(17,Float)
    210         edm.contents_loss = zeros(17,Float)
     209        edm.struct_damage = zeros(17,Float)
     210        edm.contents_damage = zeros(17,Float)
    211211        collapse_probability = {0.4:[0], #0
    212212                                0.6:[1], #1
     
    217217        edm._calc_collapse_structures(collapse_probability, verbose_csv=True)
    218218
    219         self.failUnless( edm.struct_loss[0]  == 0.0 and
    220                          edm.contents_loss[0]  == 0.0,
    221                         'Error!')
    222         self.failUnless( edm.struct_loss[1]  == 1.0 and
    223                          edm.contents_loss[1]  == 1.0,
    224                         'Error!')
    225         self.failUnless( edm.struct_loss[2]  == 1.0 and
    226                          edm.contents_loss[2]  == 1.0,
    227                         'Error!')
    228         self.failUnless( edm.struct_loss[3]+ edm.struct_loss[4] == 1.0 and
    229                          edm.contents_loss[3] + edm.contents_loss[4] == 1.0,
    230                         'Error!')
    231         # use list comprehension to do the next two tests
    232        
     219        self.failUnless( edm.struct_damage[0]  == 0.0 and
     220                         edm.contents_damage[0]  == 0.0,
     221                        'Error!')
     222        self.failUnless( edm.struct_damage[1]  == 1.0 and
     223                         edm.contents_damage[1]  == 1.0,
     224                        'Error!')
     225        self.failUnless( edm.struct_damage[2]  == 1.0 and
     226                         edm.contents_damage[2]  == 1.0,
     227                        'Error!')
     228        self.failUnless( edm.struct_damage[3]+ edm.struct_damage[4] == 1.0 and
     229                         edm.contents_damage[3] + edm.contents_damage[4] ==1.0,
     230                        'Error!')
     231        # FIXME use list comprehension to do the next two tests
     232
     233       
     234    def test_calc_collapse_probability(self):
     235        depth =          [0.0, 0.5, 0.5  , 1.5, 2.5, 4.5, 10000, 2.0]
     236        shore_distance = [0.0, 125, 250.1, 0.0, 150, 225, 10000, 251]
     237        dummy = depth
     238        edm = EventDamageModel(depth, shore_distance, dummy, dummy, dummy)
     239        struct_coll_prob = edm.calc_collapse_probability()
     240        answer = {0.05:[1,7],
     241                  0.6:[3],
     242                  0.4:[4],
     243                  0.5:[5],
     244                  0.45:[6]}
     245        #print "struct_coll_prob",struct_coll_prob
     246        #print "answer",answer
     247
     248        self.failUnless( struct_coll_prob ==  answer,
     249                        'Error!')
     250       
     251       
     252    def test_calc_damage_and_costs(self):
     253                             
     254        max_depths = [-0.3, 0.0, 1.0,-0.3, 0.0, 1.0,-0.3, 0.0, 10.0]
     255        shore_distances = [100, 100, 100, 100, 100, 100, 100, 100, 100]
     256        walls = ['Double Brick',
     257                 'Double Brick',
     258                 'Double Brick',
     259                 'Timber',
     260                 'Timber',
     261                 'Timber',
     262                 'Brick Veneer',
     263                 'Brick Veneer',
     264                 'Brick Veneer']
     265        struct_costs = [10,
     266                        10,
     267                        10,
     268                        10,
     269                        10,
     270                        10,
     271                        1,
     272                        1,
     273                        1]
     274        content_costs = [100,
     275                        100,
     276                        100,
     277                        100,
     278                        100,
     279                        100,
     280                        10,
     281                        10,
     282                        10]
     283
     284        edm = EventDamageModel(max_depths, shore_distances, walls,
     285                               struct_costs, content_costs)
     286        results_dic = edm.calc_damage_and_costs(verbose_csv=True)
     287        #print "results_dic",results_dic
    233288#-------------------------------------------------------------
    234289if __name__ == "__main__":
    235290    #suite = unittest.makeSuite(Test_inundation_damage,'test_add_depth_and_momentum2csv')
    236     #suite = unittest.makeSuite(Test_inundation_damage,'test_inundation_damage')
     291    #suite = unittest.makeSuite(Test_inundation_damage,'test_calc_damage_and_costs')
    237292    suite = unittest.makeSuite(Test_inundation_damage,'test')
    238293    runner = unittest.TextTestRunner()
Note: See TracChangeset for help on using the changeset viewer.