Changeset 3334


Ignore:
Timestamp:
Jul 13, 2006, 10:56:19 PM (19 years ago)
Author:
duncan
Message:

adding damage curve and collapsing structures.

Location:
inundation/damage
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/damage/inundation_damage.py

    r3318 r3334  
    77from math import sqrt
    88from Scientific.Functions.Interpolation import InterpolatingFunction
    9 from Numeric import array
    10 
     9from Numeric import array, ravel, Float, zeros
     10from random import choice
    1111
    1212try: 
     
    2727   
    2828
     29from utilities.numerical_tools import ensure_numeric
    2930from pyvolution.data_manager import Exposure_csv
    3031from pyvolution.util import file_function
     
    3233from utilities.numerical_tools import INF
    3334from anuga_config import epsilon
     35depth_epsilon = epsilon
    3436
    3537def inundation_damage(sww_file, exposure_file_in,
     
    101103            vh = quantities[3]
    102104
    103             # This can easily give -ve values,
    104             # but the array is initialised to 0.0,
    105             # so 0.0 will be the minimum value.
     105            #  -ground_floor_height is the minimum value.
    106106            depth = w - z - ground_floor_height
    107107             
     
    117117class EventDamageModel:
    118118    """
    119     Initially I'm doing this as a class to group all of these functions/methods.
    120 
    121 
    122     """
    123     double_brick_damage_curve = array([[-kinds.default_float_kind.MAX, 0.0],
    124                                     [0.0-epsilon, 0.0],
    125                                     [0.0,1.6],
    126                                     [2.5,70.0],
    127                                     [kinds.default_float_kind.MAX,70]])
    128      
    129     def __init__(self,max_depths, shore_distances, walls):
     119
     120
     121    """
     122    double_brick_damage_array = array([[-kinds.default_float_kind.MAX, 0.0],
     123                                       [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],
     132                                       [kinds.default_float_kind.MAX,64.7]])
     133    double_brick_damage_curve = InterpolatingFunction( \
     134        (ravel(double_brick_damage_array[:,0:1]),),
     135        ravel(double_brick_damage_array[:,1:]))
     136   
     137    brick_veeer_damage_array = array([[-kinds.default_float_kind.MAX, 0.0],
     138                                       [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],
     147                                       [kinds.default_float_kind.MAX,69.4]])
     148    brick_veeer_damage_curve = InterpolatingFunction( \
     149        (ravel(brick_veeer_damage_array[:,0:1]),),
     150        ravel(brick_veeer_damage_array[:,1:]))
     151    struct_damage_curve = {'Double Brick':double_brick_damage_curve,
     152                           'Brick Veneer':brick_veeer_damage_curve}
     153    default_struct_damage_curve = brick_veeer_damage_curve
     154
     155    contents_damage_array = array([[-kinds.default_float_kind.MAX, 0.0],
     156                                       [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],
     164                                       [kinds.default_float_kind.MAX,98.6]])
     165    contents_damage_curve = InterpolatingFunction( \
     166        (ravel(contents_damage_array[:,0:1]),),
     167        ravel(contents_damage_array[:,1:]))
     168   
     169    #Do contents stuff
     170    #Do the big one, collapsing
     171    #Do the testing of this stuff.
     172   
     173   
     174    def __init__(self,max_depths, shore_distances, walls,
     175                 struct_costs, content_costs):
    130176        self.max_depths = max_depths
    131177        self.shore_distances = shore_distances
    132178        self.walls = walls
    133         assert len(self.max_depths) == len(self.shore_distances)
     179        self.struct_costs = struct_costs
     180        self.content_costs = content_costs
     181
    134182        self.structure_count = len(self.max_depths)
    135         structure_damage_curve = InterpolatingFunction((array([0,1]),),array([0,10]))
     183        #Fixme expand
     184        assert self.structure_count == len(self.shore_distances)
     185        assert  self.structure_count == len(self.walls)
     186        assert  self.structure_count == len(self.struct_costs)
     187        assert  self.structure_count == len(self.content_costs)
     188        #assert  self.structure_count == len(self.)
     189       
    136190
    137191    def calc_damage_percentages(self):
    138192
    139193        # the data being created
    140         percent_struct_damage = [0.0]*self.structure_count
    141         percent_contents_damage = [0.0]*self.structure_count
     194        struct_damage = zeros(self.structure_count,Float)
     195        contents_damage = zeros(self.structure_count,Float)
    142196        #for i_building,
    143197
    144         for i,max_height,shore_distance,wall in map(None,
    145                                                     range(self.structure_count),
    146                                                     self.max_depths,
    147                                                     self.shore_distances,
    148                                                     self.walls):
    149             print "i",i
    150             print "max_height",max_height
    151             print "shore_distance",shore_distance
     198        for i,max_depth,shore_distance,wall in map(None,
     199                                                   range(self.structure_count),
     200                                                   self.max_depths,
     201                                                   self.shore_distances,
     202                                                   self.walls):
     203            #print "i",i
     204            #print "max_depth",max_depth
     205            #print "shore_distance",shore_distance
     206            #print "wall",wall
     207            ## WARNING SKIP IF DEPTH < 0.0
     208            if 0.0 > max_depth:
     209                continue
     210            #calc structural damage %
     211            damage_curve = self.struct_damage_curve.get(wall,
     212                                              self.default_struct_damage_curve)
     213            struct_damage[i] = damage_curve(max_depth)
     214            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]}
     222        self.struct_damage = struct_damage
     223        self.contents_damage = contents_damage
    152224           
    153225       
     226    def calc_cost(self):
     227        self.struct_loss = self.struct_damage * \
     228                           ensure_numeric(self.struct_costs)
     229        self.contents_loss = self.contents_damage * \
     230                           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
     240   
     241    def _calc_collapse_structures(self, collapse_probability,
     242                                  verbose_csv=False):
     243        """
     244        Given the collapse probabilities, throw the dice
     245        and collapse some houses
     246        """
     247        if verbose_csv:
     248            self.collapse_csv_info = ['']* self.structure_count
     249        #for a given 'bin', work out how many houses will collapse
     250        for probability, house_indexes in collapse_probability.iteritems():
     251            collapse_count = round(len(house_indexes) *probability)
     252           
     253            if verbose_csv:
     254                for i in house_indexes:
     255                    # This could be sped up I think
     256                    self.collapse_csv_info[i] = str(probability) + ' prob., ' \
     257                           + str(int(collapse_count)) + ' collapsed out of ' \
     258                           + str(len(house_indexes))
     259            for _ in range(int(collapse_count)):
     260                house_index = choice(house_indexes)
     261                self.struct_loss[house_index] = 1.0
     262                self.contents_loss[house_index] = 1.0
     263                house_indexes.remove(house_index)
     264            # Warning, the collapse_probability list now lists
     265            # houses that did not collapse, (though not all of them)
     266            #print "",self.collapse_csv_info
    154267#############################################################################
    155268if __name__ == "__main__":
     
    173286    print "value",i(0.5)
    174287
     288    dict = {1:10, 2:20}
     289    print dict.get(100,dict[2])
     290
    175291       
  • inundation/damage/test_inundation_damage.py

    r3318 r3334  
    88import csv
    99
     10#from damage.inundation_damage import _calc_collapse_structures
    1011from damage.inundation_damage import *
    1112from geospatial_data.geospatial_data import Geospatial_data
     
    1617from data_manager import get_dataobject
    1718
    18 from Numeric import zeros, Float
     19from Numeric import zeros, Float, allclose
    1920       
    2021class Test_inundation_damage(unittest.TestCase):
     
    131132        writer.writerow(['151.5','-34','199770','130000','Metal','Timber',20])
    132133        writer.writerow(['151','-34.5','150000','76000','Metal','Double Brick',200])
     134        writer.writerow(['151','-34.25','150000','76000','Metal','Brick Veneer',200])
    133135
    134136       
     
    154156       
    155157    def test_calc_damage_percentages(self):
    156         max_depths = [-0.3, 0.0, 1.0]
    157         shore_distances = [100, 200, 300]
    158         walls = ['Double Brick','Timber','Brick Veneer']
    159 
    160         edm = EventDamageModel(max_depths, shore_distances, walls)
     158        max_depths = [-0.3, 0.0, 1.0,-0.3, 0.0, 1.0,-0.3, 0.0, 1.0]
     159        shore_distances = [100, 100, 100, 100, 100, 100, 100, 100, 100]
     160        walls = ['Double Brick',
     161                 'Double Brick',
     162                 'Double Brick',
     163                 'Timber',
     164                 'Timber',
     165                 'Timber',
     166                 'Brick Veneer',
     167                 'Brick Veneer',
     168                 'Brick Veneer']
     169        struct_costs = [10,
     170                        10,
     171                        10,
     172                        10,
     173                        10,
     174                        10,
     175                        1,
     176                        1,
     177                        1]
     178        content_costs = [100,
     179                        100,
     180                        100,
     181                        100,
     182                        100,
     183                        100,
     184                        10,
     185                        10,
     186                        10]
     187
     188        edm = EventDamageModel(max_depths, shore_distances, walls,
     189                               struct_costs, content_costs)
    161190        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])
     197        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])
     204       
     205       
     206    def test_calc_collapse_structures(self):
     207        edm = EventDamageModel([0.0]*17, [0.0]*17, [0.0]*17,
     208                               [0.0]*17, [0.0]*17)
     209        edm.struct_loss = zeros(17,Float)
     210        edm.contents_loss = zeros(17,Float)
     211        collapse_probability = {0.4:[0], #0
     212                                0.6:[1], #1
     213                                0.5:[2], #1
     214                                0.25:[3,4], #1
     215                                0.1:[5,6,7,8], #0
     216                                0.2:[9,10,11,12,13,14,15,16]} #2
     217        edm._calc_collapse_structures(collapse_probability, verbose_csv=True)
     218
     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
    162232       
    163233#-------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.