source: development/momentum_sink/scripts/CCS.py @ 3295

Last change on this file since 3295 was 2927, checked in by nicholas, 19 years ago

final update of all relavent data

File size: 7.7 KB
RevLine 
[2927]1"""Read in sww files, interpolate at specified locations and
2    Cross section at specified T. Cross sections are spatial drawing
3    at the specified time.
4
5    '2nd normal' is the same as relative RMS test
6    test is employed to find the best fit friction value
7    for the given building scenario => 'B_file'
8
9    the RMS result for each friction file is written to a csv file
10   
11
12"""
13from os import sep
14import os
15import Numeric
16import project
17import string
18from pyvolution.util import file_function
19from numerical_tools import norm, corr, err 
20
21# NAME FOLDER TO GET BUILDING SCENARIOS FROM
22#-------------------------------------------------------------------------
23folder_name = 'buildings_Rot(45)_10'   # <<<<<< place folder name here <<<<<<<
24#-------------------------------------------------------------------------
25wave_height = str(10)                    # <<<<<< place wave depth here <<<<<<<
26#-------------------------------------------------------------------------
27
28
29
30# FILE SORTER
31#-------------------------------------------------------------------------
32# Sorting function used to sort files in the specified folder #   
33B_list_unsorted=os.listdir(project.building_dir+ folder_name + sep) 
34B_strings=[]
35B_list=[]
36for B_L in B_list_unsorted:
37    A = B_L.split('=')[0] + '=' # first part of string
38    B = int(B_L.split('=')[1].split('_')[0]) # middle part of string
39    C = '_' + B_L.split('=')[1].split('_')[1] # last part of string
40    B_current = [A,B,C]; B_strings.append(B_current)
41
42B_strings.sort(lambda x, y: cmp(x[1],y[1]))
43
44for B_str in B_strings:
45    B_str[1]=str(B_str[1])
46    final=string.join(B_str).replace(' ','')
47    B_list.append(final)
48print B_list
49print "-----------------------------------------------------"
50print ">> ",B_list[0],"<< FIRST file"
51print ">> ",B_list[-1],"<< LAST file"
52print "-----------------------------------------------------"
53# End of sorting file
54
55#-------------------------------------------------------------------------
56print "-----------------------------------------------------"
57fd = open(folder_name + '_report.csv', 'w')
58fd.write( folder_name + '_report from CCS.py using 2nd normal test' + '\n' )
59
60#-------------------------------------------------------------------------
61# Specify correct building parameters of scenario. Only used in naming
62WidtH = 200
63BL = 25
64
65fd.write( 'Building scenario parameters: WidtH = ' + str(WidtH) + ', Block length = ' + str(BL) + ', Wave height = ' + str(wave_height) + '\n' )
66print " "
67print "-----------------------------------------------------"
68print 'Building scenario parameters: WidtH = ' + str(WidtH) + ', Block length = ' + str(BL) + ', Wave height = ' + str(wave_height)
69print "-----------------------------------------------------"
70#-------------------------------------------------------------------------
71
72best_fric=[]
73B_Density=[]
74best_norm=[]
75Breadth=[]
76prev_index=0
77u=1
78
79#-------------------------------------------------------------------------
80# Buildings scenario loops from sorting. loops through sww files in specifies folder name
81for B_file in B_list:
82    swwfile_B = project.building_dir + folder_name +sep+ B_file
83    bre = float(B_file.split('=')[1].split('_')[0]) # middle part of string
84    print bre, "breadth"
85   
86    # IMPORTANT, locates gauge points between buildings.
87    gauge_depth = list(Numeric.arange(0.2*WidtH + ((BL-bre)/2), 1.2*WidtH+0.1, 25))
88    gauge_depth.append((1.2*WidtH)+0.01)
89    gauge_breadth = 100 # identifies where the cross section will be cut
90    print gauge_depth, "gauge locations"
91    # raw_input('Check gauge locations >> are they in order?')
92   
93    gauge_locations = []
94    for GD in gauge_depth:
95        gauge_location = [GD,gauge_breadth]
96        gauge_locations.append(gauge_location)
97
98
99    #Read model output
100    quantities = ['stage', 'xmomentum', 'ymomentum']
101
102    f_B = file_function(swwfile_B,
103                      quantities = quantities,
104                      interpolation_points = gauge_locations,
105                      verbose = True,
106                      use_cache = False)
107
108    fd.write('_______________________________________________________' + '\n' )
109    fd.write( '<<<< Mannings n for [' + B_file + '] >>>>' + '\n' )
110    fd.write( '(n), 2nd norm ' + '\n')
111
112    fric_OK=[]
113    least_norm=[100]
114    Normal=[]
115
116    # creation of list of friction values to check through, make sure sww files exist before proceeding.
117    Friction_large = list(Numeric.arange(1,120,1))
118    Friction = list(Numeric.arange(0.025,1,0.025))
119    Friction_log = list([200, 500, 1000, 2000, 5000, 10000])
120    Friction.extend(Friction_large)
121    Friction.extend(Friction_log)
122    #Friction_log = [0.025, 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000]
123   
124    if prev_index < 6:
125        pri=0
126    else:
127        pri=prev_index-5
128       
129    #-------------------------------------------------------------------------
130   
131
132    for f_index, fric in enumerate(Friction):
133    #for f_index, fric in enumerate(Friction[pri:]):
134        swwfile_F = project.friction_dir +'friction_files_'+wave_height+ sep + 'friction_' +wave_height+ '_n=' + str(fric) + '_3125.sww'
135        f_F = file_function(swwfile_F,
136                          quantities = quantities,
137                          interpolation_points = gauge_locations,
138                          verbose = True,
139                          use_cache = False)
140       
141        t=990 # specifies times to look at cross sections
142        stages_B = []
143        stages_F = []
144        vel_B=[]
145        vel_F=[]
146        for i, GL in enumerate(gauge_depth):
147
148            # mines data from specified time values in sww file
149
150            # Building values
151            wB = f_B(t, point_id = i)[0]
152            uhB = f_B(t, point_id = i)[1]
153            vhB = f_B(t, point_id = i)[2]
154            # Friction values
155            wF = f_F(t, point_id = i)[0]       
156            uhF = f_B(t, point_id = i)[1]
157            vhF = f_B(t, point_id = i)[2]
158
159            #m = sqrt(uh*uh + vh*vh)   #Absolute momentum
160            velB = ((uhB*uhB + vhB*vhB)**0.5) / (wB + 1.e-30) #Absolute velocity
161            velF = ((uhF*uhF + vhF*vhF)**0.5) / (wF + 1.e-30) #Absolute velocity                   
162            stages_B.append(wB)
163            stages_F.append(wF)
164
165        vel_B.append(velB)
166        vel_F.append(velF)
167        Ar_B = Numeric.array(stages_B)
168        Ar_F = Numeric.array(stages_F)
169        diff = abs(Ar_B - Ar_F)
170        norm_F = err(Ar_F, Ar_B, 2, relative = True)  # 2nd norm (rel. RMS) test
171        Normal.append(norm_F)
172        fd.write(str(fric) + ', ' +  str(norm_F) + '\n')
173
174        # finds lowest RMS thus friction value
175        if norm_F < least_norm:
176            least_norm = norm_F
177            fric_OK = fric
178            prev_index = Friction.index(fric)
179           
180        else:
181            print "not OK"
182            #break
183
184    print fric_OK, " <- This Manning's n is best fit from 2nd norm test"
185    print least_norm, "2nd normal of Friction vs Buildings  "
186    best_fric.append(fric_OK)
187    B_Density.append((float(bre)**2)/625)
188    best_norm.append(least_norm)
189    Breadth.append(bre)
190     
191print best_fric, " << Best fit friction"
192print B_Density, " << Building footprint density"
193print best_norm, " << Matching normal densities"
194
195len_file=Numeric.arrayrange(0,len(B_list),1)
196fd.write('_______________________________________________________' + '\n' )
197fd.write( '<<<<< ' + folder_name + ' report Summary >>>>>' + '\n' )
198fd.write( 'width, footprint ratio, mannings n, 2nd norm ' + '\n')
199for i in len_file:
200    fd.write( str(Breadth[i]) + ', ' + str(B_Density[i]) + ', ' +  str(best_fric[i]) + ', ' +  str(best_norm[i]) + '\n')
201fd.close
202
Note: See TracBrowser for help on using the repository browser.