Changeset 629
- Timestamp:
- Nov 24, 2004, 6:55:52 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/test_util.py
r623 r629 234 234 235 235 #Check that domain.starttime isn't updated if later 236 domain.starttime = start + 1237 F = File_function(filename, domain)238 assert allclose(domain.starttime, start+1)236 #domain.starttime = start + 1 237 #F = File_function(filename, domain) 238 #assert allclose(domain.starttime, start+1) 239 239 240 240 … … 264 264 assert allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] ) 265 265 266 266 267 268 def test_file_function_time_with_domain_different_start(self): 269 """Test that File function interpolates correctly 270 between given times. No x,y dependency here. 271 Use domain with a starttime later than that of file 272 """ 273 274 #Write file 275 import os, time, calendar 276 from config import time_format 277 from math import sin, pi 278 from domain import Domain 279 280 finaltime = 1200 281 filename = 'test_file_function.txt' 282 fid = open(filename, 'w') 283 start = time.mktime(time.strptime('2000', '%Y')) 284 dt = 60 #One minute intervals 285 t = 0.0 286 while t <= finaltime: 287 t_string = time.strftime(time_format, time.gmtime(t+start)) 288 fid.write('%s, %f %f %f\n' %(t_string, 2*t, t**2, sin(t*pi/600))) 289 t += dt 290 291 fid.close() 292 293 a = [0.0, 0.0] 294 b = [4.0, 0.0] 295 c = [0.0, 3.0] 296 297 points = [a, b, c] 298 vertices = [[0,1,2]] 299 domain = Domain(points, vertices) 300 301 #Check that domain.starttime isn't updated if later 302 delta = 23 303 domain.starttime = start + delta 304 F = File_function(filename, domain) 305 assert allclose(domain.starttime, start+delta) 306 307 308 309 310 #Now try interpolation with delta offset 311 for i in range(20): 312 t = i*10 313 q = F(t-delta) 314 315 #Exact linear intpolation 316 assert allclose(q[0], 2*t) 317 if i%6 == 0: 318 assert allclose(q[1], t**2) 319 assert allclose(q[2], sin(t*pi/600)) 320 321 #Check non-exact 322 323 t = 90 #Halfway between 60 and 120 324 q = F(t-delta) 325 assert allclose( (120**2 + 60**2)/2, q[1] ) 326 assert allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] ) 327 328 329 t = 100 #Two thirds of the way between between 60 and 120 330 q = F(t-delta) 331 assert allclose( 2*120**2/3 + 60**2/3, q[1] ) 332 assert allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] ) 333 334 335 267 336 #------------------------------------------------------------- 268 337 if __name__ == "__main__": -
inundation/ga/storm_surge/pyvolution/util.py
r624 r629 48 48 49 49 50 51 #FIXME: Maybe move to util as this is quite general52 50 class File_function: 53 51 """Read time series from file and return a callable object: … … 139 137 self.filename = filename 140 138 self.starttime = starttime 141 139 self.domain = domain 142 140 143 141 if domain is not None: 144 142 if domain.starttime is None: 145 domain.starttime = s tarttime143 domain.starttime = self.starttime 146 144 else: 147 145 msg = 'WARNING: Start time as specified in domain (%s)'\ … … 209 207 from math import pi, cos, sin, sqrt 210 208 211 209 210 #Find time tau such that 211 # 212 # domain.starttime + t == self.starttime + tau 213 214 #FIXME: This hasn't been unit tested yet 215 if self.domain is not None: 216 tau = self.domain.starttime-self.starttime+t 217 else: 218 tau = t 219 220 212 221 msg = 'Time interval derived from file %s (%s:%s) does not match model time: %s'\ 213 %(self.filename, self.T[0], self.T[1], t )214 if t < self.T[0]: raise msg215 if t > self.T[-1]: raise msg222 %(self.filename, self.T[0], self.T[1], tau) 223 if tau < self.T[0]: raise msg 224 if tau > self.T[-1]: raise msg 216 225 217 226 oldindex = self.index 218 227 219 228 #Find slot 220 while t > self.T[self.index]: self.index += 1221 while t < self.T[self.index]: self.index -= 1229 while tau > self.T[self.index]: self.index += 1 230 while tau < self.T[self.index]: self.index -= 1 222 231 223 232 #t is now between index and index+1 224 ratio = (t - self.T[self.index])/\233 ratio = (tau - self.T[self.index])/\ 225 234 (self.T[self.index+1] - self.T[self.index]) 226 235
Note: See TracChangeset
for help on using the changeset viewer.