Changeset 623 for inundation/ga/storm_surge/pyvolution/test_util.py
- Timestamp:
- Nov 24, 2004, 4:43:40 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/test_util.py
r258 r623 138 138 139 139 140 141 def test_file_function_time(self): 142 """Test that File function interpolates correctly 143 between given times. No x,y dependency here. 144 """ 145 146 #Write file 147 import os, time 148 from config import time_format 149 from math import sin, pi 150 151 finaltime = 1200 152 filename = 'test_file_function.txt' 153 fid = open(filename, 'w') 154 start = time.mktime(time.strptime('2000', '%Y')) 155 dt = 60 #One minute intervals 156 t = 0.0 157 while t <= finaltime: 158 t_string = time.strftime(time_format, time.gmtime(t+start)) 159 fid.write('%s, %f %f %f\n' %(t_string, 2*t, t**2, sin(t*pi/600))) 160 t += dt 161 162 fid.close() 163 164 F = File_function(filename) 165 166 #Now try interpolation 167 for i in range(20): 168 t = i*10 169 q = F(t) 170 171 #Exact linear intpolation 172 assert allclose(q[0], 2*t) 173 if i%6 == 0: 174 assert allclose(q[1], t**2) 175 assert allclose(q[2], sin(t*pi/600)) 176 177 #Check non-exact 178 179 t = 90 #Halfway between 60 and 120 180 q = F(t) 181 assert allclose( (120**2 + 60**2)/2, q[1] ) 182 assert allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] ) 183 184 185 t = 100 #Two thirds of the way between between 60 and 120 186 q = F(t) 187 assert allclose( 2*120**2/3 + 60**2/3, q[1] ) 188 assert allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] ) 189 190 191 192 193 def test_file_function_time_with_domain(self): 194 """Test that File function interpolates correctly 195 between given times. No x,y dependency here. 196 Use domain with starttime 197 """ 198 199 #Write file 200 import os, time, calendar 201 from config import time_format 202 from math import sin, pi 203 from domain import Domain 204 205 finaltime = 1200 206 filename = 'test_file_function.txt' 207 fid = open(filename, 'w') 208 start = time.mktime(time.strptime('2000', '%Y')) 209 dt = 60 #One minute intervals 210 t = 0.0 211 while t <= finaltime: 212 t_string = time.strftime(time_format, time.gmtime(t+start)) 213 fid.write('%s, %f %f %f\n' %(t_string, 2*t, t**2, sin(t*pi/600))) 214 t += dt 215 216 fid.close() 217 218 a = [0.0, 0.0] 219 b = [4.0, 0.0] 220 c = [0.0, 3.0] 221 222 points = [a, b, c] 223 vertices = [[0,1,2]] 224 domain = Domain(points, vertices) 225 226 #Check that domain.starttime is updated if non-existing 227 F = File_function(filename, domain) 228 assert allclose(domain.starttime, start) 229 230 #Check that domain.starttime is updated if too early 231 domain.starttime = start - 1 232 F = File_function(filename, domain) 233 assert allclose(domain.starttime, start) 234 235 #Check that domain.starttime isn't updated if later 236 domain.starttime = start + 1 237 F = File_function(filename, domain) 238 assert allclose(domain.starttime, start+1) 239 240 241 242 #Now try interpolation 243 for i in range(20): 244 t = i*10 245 q = F(t) 246 247 #Exact linear intpolation 248 assert allclose(q[0], 2*t) 249 if i%6 == 0: 250 assert allclose(q[1], t**2) 251 assert allclose(q[2], sin(t*pi/600)) 252 253 #Check non-exact 254 255 t = 90 #Halfway between 60 and 120 256 q = F(t) 257 assert allclose( (120**2 + 60**2)/2, q[1] ) 258 assert allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] ) 259 260 261 t = 100 #Two thirds of the way between between 60 and 120 262 q = F(t) 263 assert allclose( 2*120**2/3 + 60**2/3, q[1] ) 264 assert allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] ) 265 140 266 141 267 #-------------------------------------------------------------
Note: See TracChangeset
for help on using the changeset viewer.