Changeset 851
- Timestamp:
- Feb 8, 2005, 7:07:56 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/domain.py
r850 r851 270 270 raise msg 271 271 272 272 273 def set_region(self, functions): 273 274 # The order of functions in the list is used. -
inundation/ga/storm_surge/pyvolution/generic_boundary_conditions.py
r850 r851 187 187 #any tagged boundary later on. 188 188 189 midpoint_coordinates = zeros( (len(domain.boundary), 2), Float)189 self.midpoint_coordinates = zeros( (len(domain.boundary), 2), Float) 190 190 boundary_keys = domain.boundary.keys() 191 192 #Record ordering #FIXME: should this happen in domain.py 193 self.boundary_indices = {} 194 191 195 for i, (vol_id, edge_id) in enumerate(boundary_keys): 192 196 … … 199 203 if edge_id == 1: m = array([(x0 + x2)/2, (y0 + y2)/2]) 200 204 if edge_id == 2: m = array([(x1 + x0)/2, (y1 + y0)/2]) 201 midpoint_coordinates[i,:] = m 202 203 self.midpoint_coordinates = midpoint_coordinates 205 self.midpoint_coordinates[i,:] = m 206 207 self.boundary_indices[(vol_id, edge_id)] = i 208 204 209 205 self.F = file_function(filename, domain, interpolation_points=midpoint_coordinates) 210 self.F = file_function(filename, domain, 211 interpolation_points=self.midpoint_coordinates) 206 212 self.domain = domain 207 213 208 214 #Test 209 q = self.F(0, 0,0)215 q = self.F(0, point_id=0) 210 216 211 217 d = len(domain.conserved_quantities) 212 msg = 'Values specified in file %s must be a list or an array of length %d' %(filename, d) 218 msg = 'Values specified in file %s must be ' %filename 219 msg += ' a list or an array of length %d' %d 213 220 assert len(q) == d, msg 214 221 … … 226 233 227 234 if vol_id is not None and edge_id is not None: 228 x, y = self.midpoint_coordinates[ vol_id, edge_id ]229 return self.F(t, x, y)235 i = self.boundary_indices[ vol_id, edge_id ] 236 return self.F(t, point_id = i) 230 237 else: 231 return self.F(t) 238 return self.F(t) #What should the semantics be? 232 239 233 240 -
inundation/ga/storm_surge/pyvolution/shallow_water.py
r850 r851 941 941 except Exception, e: 942 942 msg = 'Function %s could not be executed:\n%s' %(f, e) 943 #FIXME: Reconsider this semantics 943 944 raise msg 944 945 -
inundation/ga/storm_surge/pyvolution/test_shallow_water.py
r850 r851 2415 2415 Bd = Dirichlet_boundary([0.3,0,0]) 2416 2416 Bt = Transmissive_boundary(domain1) 2417 domain1.set_boundary({'left': Bd, 'right': B t, 'top': Br, 'bottom': Br})2417 domain1.set_boundary({'left': Bd, 'right': Br, 'top': Br, 'bottom': Br}) 2418 2418 2419 2419 #Initial condition … … 2452 2452 domain2.reduction = domain1.reduction 2453 2453 domain2.smooth = False 2454 domain2.visualise = True2454 domain2.visualise = False 2455 2455 domain2.default_order = 2 2456 2456 … … 2465 2465 2466 2466 2467 #FIXME: Continue from here!!!!!!!2468 return2469 2470 2467 Bf = Spatio_temporal_boundary(domain1.filename + '.' + domain1.format, 2471 2468 domain2) 2472 domain2.set_boundary({'right':B t, 'bottom':Br, 'diagonal':Bf})2469 domain2.set_boundary({'right':Br, 'bottom':Br, 'diagonal':Bf}) 2473 2470 2474 2471 #Initial condition … … 2486 2483 cv2 = domain2.quantities['stage'].centroid_values 2487 2484 2488 print take(cv1, (12,14,16)) #Right 2489 print take(cv2, (4,6,8)) 2490 2485 #print take(cv1, (12,14,16)) #Right 2486 #print take(cv2, (4,6,8)) 2491 2487 #print take(cv1, (0,6,12)) #Bottom 2492 2493 assert allclose( take(cv1, (12,14,16)), take(cv2, (4,6,8))) #RHS 2494 assert allclose( take(cv1, (0,6,12)), take(cv2, (0,1,4))) #Bottom 2488 #print take(cv2, (0,1,4)) 2489 #print take(cv1, (0,8,16)) #Diag 2490 #print take(cv2, (0,3,8)) 2491 #assert allclose( take(cv1, (12,14,16)), take(cv2, (4,6,8))) #RHS 2492 #assert allclose( take(cv1, (0,6,12)), take(cv2, (0,1,4))) #Bottom 2495 2493 2496 2494 #Cleanup 2497 os.remove(domain1.filename + '.' + domain1.format) 2498 2495 try: 2496 os.remove(domain1.filename + '.' + domain1.format) 2497 except: 2498 pass 2499 2499 2500 #------------------------------------------------------------- 2500 2501 if __name__ == "__main__": 2501 #suite = unittest.makeSuite(TestCase,'test')2502 suite = unittest.makeSuite(TestCase,'test_spatio_temporal_boundary')2502 suite = unittest.makeSuite(TestCase,'test') 2503 #suite = unittest.makeSuite(TestCase,'test_spatio_temporal_boundary') 2503 2504 runner = unittest.TextTestRunner() 2504 2505 runner.run(suite) -
inundation/ga/storm_surge/pyvolution/util.py
r850 r851 123 123 quantities = ['stage', 'xmomentum', 'ymomentum'] 124 124 125 interpolation_points decides at which points interpolated quantities are to be computed 125 interpolation_points decides at which points interpolated 126 quantities are to be computed whenever object is called. 127 128 129 126 130 If None, return average value 127 131 """ … … 133 137 will be checked and possibly modified 134 138 135 136 137 139 All times are assumed to be in UTC 138 140 """ … … 213 215 msg = 'File %s must list time as a monotonuosly ' %self.filename 214 216 msg += 'increasing sequence' 215 assert alltrue( 217 assert alltrue(time[1:] - time[:-1] > 0 ), msg 216 218 217 219 … … 227 229 228 230 ####self.Q = zeros( (len(time), 229 230 for i, t in enumerate(time[:]): 231 self.T = time[:] 232 self.index = 0 #Initial index 233 234 self.values = {} 235 for name in self.quantities: 236 self.values[name] = zeros( (len(self.T), 237 len(interpolation_points)), 238 Float) 239 240 for i, t in enumerate(self.T): 231 241 232 242 #Interpolate quantities at this timestep … … 238 248 verbose = False) 239 249 240 for name in self.quantities: 241 print t, name, interpol.interpolate(fid.variables[name][i,:]) 250 for name in self.quantities: 251 self.values[name][i, :] =\ 252 interpol.interpolate(fid.variables[name][i,:]) 242 253 243 254 else: 244 255 pass 245 256 246 self.T = time[:] 247 248 return 249 250 fid = open(self.filename) 251 lines = fid.readlines() 252 fid.close() 253 254 N = len(lines) 255 d = self.number_of_values 256 257 T = zeros(N, Float) #Time 258 Q = zeros((N, d), Float) #Values 259 260 for i, line in enumerate(lines): 261 fields = line.split(',') 262 realtime = calendar.timegm(time.strptime(fields[0], time_format)) 263 264 T[i] = realtime - self.starttime 265 266 for j, value in enumerate(fields[1].split()): 267 Q[i, j] = float(value) 268 269 270 271 self.T = T #Time 272 self.Q = Q #Values 273 self.index = 0 #Initial index 274 275 257 def __repr__(self): 258 return 'File function' 259 260 def __call__(self, t, x=None, y=None, point_id = None): 261 """Evaluate f(t, point_id) 262 263 Inputs: 264 t: time - Model time (tau = domain.starttime-self.starttime+t) 265 must lie within existing timesteps 266 point_id: index of one of the preprocessed points. 267 If point_id is None all preprocessed points are computed 268 269 FIXME: point_id could also be a slice 270 FIXME: One could allow arbitrary x, y coordinates 271 (requires computation of a new interpolator) 272 FIXME: What if x and y are vectors? 273 """ 274 275 from math import pi, cos, sin, sqrt 276 from Numeric import zeros, Float 277 278 #Find time tau such that 279 # 280 # domain.starttime + t == self.starttime + tau 281 282 if self.domain is not None: 283 tau = self.domain.starttime-self.starttime+t 284 else: 285 tau = t 286 287 msg = 'Time interval derived from file %s (%s:%s) does not match model time: %s'\ 288 %(self.filename, self.T[0], self.T[1], tau) 289 if tau < self.T[0]: raise msg 290 if tau > self.T[-1]: raise msg 291 292 oldindex = self.index #Time index 293 294 #Find time slot 295 while tau > self.T[self.index]: self.index += 1 296 while tau < self.T[self.index]: self.index -= 1 297 298 #t is now between index and index+1 299 ratio = (tau - self.T[self.index])/\ 300 (self.T[self.index+1] - self.T[self.index]) 301 302 303 #Compute interpolated values 304 q = zeros( len(self.quantities), Float) 305 306 for i, name in enumerate(self.quantities): 307 Q = self.values[name] 308 309 q[i] = Q[self.index, point_id] +\ 310 ratio*(Q[self.index+1, point_id] - Q[self.index, point_id]) 311 312 313 #Return vector of interpolated values 314 return q 315 276 316 277 317 class File_function_ASCII: … … 430 470 return 'File function' 431 471 432 433 434 def __call__(self, t, x=None, y=None): 472 def __call__(self, t, x=None, y=None, point_id=None): 435 473 """Evaluate f(t,x,y) 436 474 … … 438 476 result is a vector of same length as x and y 439 477 FIXME: Naaaa 478 479 FIXME: Rethink semantics when x,y are vectors. 440 480 """ 441 481
Note: See TracChangeset
for help on using the changeset viewer.