Changeset 1155
- Timestamp:
- Mar 29, 2005, 1:09:46 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/data_manager.py
r1145 r1155 1835 1835 1836 1836 1837 def sww2domain(filename,t=None,fail_if_NaN=True,NaN_filler=0,verbose = True): 1837 def sww2domain(filename,boundary=None,t=None,\ 1838 fail_if_NaN=True,NaN_filler=0\ 1839 ,verbose = True,very_verbose = False): 1838 1840 """ 1839 1841 Usage: domain = sww2domain('file.sww',t=time (default = last time in file)) 1840 1842 1841 If the sww has stages, but not 1843 Boundary is not recommended if domian.smooth is not selected, as it 1844 uses unique coordinates, but not unique boundaries. This means that 1845 the boundary file will not be compatable with the coordiantes, and will 1846 give a different final boundary, or crash. 1842 1847 """ 1843 1848 NaN=9.969209968386869e+036 … … 1845 1850 1846 1851 from Scientific.IO.NetCDF import NetCDFFile 1847 from domainimport Domain1848 from Numeric import asarray, transpose 1852 from shallow_water import Domain 1853 from Numeric import asarray, transpose, resize 1849 1854 1850 1855 if verbose: print 'Reading from ', filename … … 1891 1896 1892 1897 if verbose: print ' building domain' 1893 domain = Domain(coordinates, volumes,\ 1894 conserved_quantities = conserved_quantities,\ 1895 other_quantities = other_quantities,zone=zone,\ 1896 xllcorner=xllcorner, yllcorner=yllcorner) 1897 domain.starttime=starttime 1898 domain.time=t 1898 # From domain.Domain: 1899 # domain = Domain(coordinates, volumes,\ 1900 # conserved_quantities = conserved_quantities,\ 1901 # other_quantities = other_quantities,zone=zone,\ 1902 # xllcorner=xllcorner, yllcorner=yllcorner) 1903 1904 # From shallow_water.Domain: 1905 coordinates=coordinates.tolist() 1906 volumes=volumes.tolist() 1907 #FIXME:should this be in mesh?(peter row) 1908 unique,coordinates,volumes,boundary=weed(coordinates,volumes,boundary) 1909 1910 domain = Domain(coordinates, volumes, boundary) 1911 1912 if not boundary is None: 1913 domain.boundary = boundary 1914 domain.zone=zone 1915 domain.xllcorner=float(xllcorner) 1916 domain.yllcorner=float(yllcorner) 1917 1918 domain.starttime=float(starttime)+float(t) 1919 domain.time=0.0 1920 1899 1921 for quantity in other_quantities: 1900 1922 try: … … 1918 1940 data = (X<>NaN) 1919 1941 X = (X*data)+(data==0)*NaN_filler 1942 if unique: 1943 X = resize(X,(len(X)/3,3)) 1920 1944 domain.set_quantity(quantity,X) 1921 1945 # … … 1941 1965 data = (X<>NaN) 1942 1966 X = (X*data)+(data==0)*NaN_filler 1967 if unique: 1968 X = resize(X,(X.shape[0]/3,3)) 1943 1969 domain.set_quantity(quantity,X) 1944 1970 fid.close() … … 1985 2011 1986 2012 2013 def weed(coordinates,volumes,boundary = None): 2014 if type(coordinates)=='array': 2015 coordinates = coordinates.tolist() 2016 if type(volumes)=='array': 2017 volumes = volumes.tolist() 2018 2019 unique = False 2020 point_dict = {} 2021 same_point = {} 2022 for i in range(len(coordinates)): 2023 point = tuple(coordinates[i]) 2024 if point_dict.has_key(point): 2025 unique = True 2026 same_point[i]=point 2027 #to change all point i references to point j 2028 else: 2029 point_dict[point]=i 2030 same_point[i]=point 2031 2032 coordinates = [] 2033 i = 0 2034 for point in point_dict.keys(): 2035 point = tuple(point) 2036 coordinates.append(list(point)) 2037 point_dict[point]=i 2038 i+=1 2039 2040 2041 for volume in volumes: 2042 for i in range(len(volume)): 2043 index = volume[i] 2044 if index>-1: 2045 volume[i]=point_dict[same_point[index]] 2046 2047 new_boundary = {} 2048 if not boundary is None: 2049 for segment in boundary.keys(): 2050 point0 = point_dict[same_point[segment[0]]] 2051 point1 = point_dict[same_point[segment[1]]] 2052 label = boundary[segment] 2053 #FIXME should the bounday attributes be concaterated 2054 #('exterior, pond') or replaced ('pond')(peter row) 2055 2056 if new_boundary.has_key((point0,point1)): 2057 new_boundary[(point0,point1)]=new_boundary[(point0,point1)]#\ 2058 #+','+label 2059 2060 elif new_boundary.has_key((point1,point0)): 2061 new_boundary[(point1,point0)]=new_boundary[(point1,point0)]#\ 2062 #+','+label 2063 else: new_boundary[(point0,point1)]=label 2064 2065 boundary = new_boundary 2066 2067 return unique,coordinates,volumes,boundary 2068 2069 1987 2070 1988 2071 #OBSOLETE STUFF -
inundation/ga/storm_surge/pyvolution/test_data_manager.py
r1151 r1155 1341 1341 1342 1342 #Create basic mesh 1343 points, vertices, boundary = rectangular(2,2) 1344 1343 1344 yiel=0.01 1345 points, vertices, boundary = rectangular(10,10) 1345 1346 #Create shallow water domain 1346 1347 domain = Domain(points, vertices, boundary) … … 1358 1359 Bt = Transmissive_boundary(domain) 1359 1360 Bd = Dirichlet_boundary([0.2,0.,0.]) 1360 Bw = Time_boundary(domain=domain,1361 Bw = Time_boundary(domain=domain, 1361 1362 f=lambda t: [(0.1*sin(t*2*pi)), 0.0, 0.0]) 1362 1363 1363 1364 domain.set_boundary({'left': Bd, 'right': Br, 'top': Br, 'bottom': Br}) 1365 1364 1366 domain.quantities_to_be_stored.extend(['xmomentum','ymomentum']) 1365 1367 #Initial condition … … 1367 1369 elevation = domain.quantities['elevation'].vertex_values 1368 1370 domain.set_quantity('stage', elevation + h) 1369 #elevation = domain.get_quantity('elevation' ,location='unique vertices')1370 #domain.set_quantity('stage', elevation + h ,location='unique vertices')1371 #elevation = domain.get_quantity('elevation') 1372 #domain.set_quantity('stage', elevation + h) 1371 1373 1372 1374 domain.check_integrity() 1373 dir(domain)1374 1375 #Evolution 1375 for t in domain.evolve(yieldstep = 1, finaltime = 2.0):1376 for t in domain.evolve(yieldstep = yiel, finaltime = 0.05): 1376 1377 # domain.write_time() 1377 1378 pass … … 1383 1384 from data_manager import sww2domain 1384 1385 from Numeric import allclose 1385 import os 1386 1387 filename = domain.datadir+os.sep+domain.filename+'.sww' 1388 1389 domain2 = sww2domain(filename,fail_if_NaN=False,verbose = False) 1386 1387 filename = domain.datadir+'\\'+domain.filename+'.sww' 1388 domain2 = sww2domain(filename,None,fail_if_NaN=False,verbose = False) 1389 #points, vertices, boundary = rectangular(15,15) 1390 #domain2.boundary = boundary 1391 ################### 1392 ##NOW TEST IT!!! 1393 ################### 1394 1395 bits = ['xllcorner','yllcorner','vertex_coordinates'] 1396 1397 for quantity in ['elevation']+domain.quantities_to_be_stored: 1398 bits.append('quantities["%s"].get_integral()'%quantity) 1399 bits.append('get_quantity("%s")'%quantity) 1400 1401 for bit in bits: 1402 # print 'testing that domain.'+bit+' has been restored' 1403 # print bit 1404 #print 'done' 1405 assert allclose(eval('domain.'+bit),eval('domain2.'+bit)) 1406 1407 ###################################### 1408 #Now evolve them both, just to be sure 1409 visualise = False 1410 #visualise = True 1411 domain.visualise = visualise 1412 domain.time = 0. 1413 from time import sleep 1414 1415 final = .1 1416 domain.set_quantity('friction', 0.1) 1417 domain.store = False 1418 domain.set_boundary({'left': Bd, 'right': Bd, 'top': Bd, 'bottom': Bd}) 1419 1420 1421 for t in domain.evolve(yieldstep = yiel, finaltime = final): 1422 if visualise: sleep(1.) 1423 #domain.write_time() 1424 pass 1425 1426 domain2.smooth = False 1427 domain2.visualise = visualise 1428 domain2.store = False 1429 domain2.default_order=2 1430 domain2.set_quantity('friction', 0.1) 1431 #Bed-slope and friction 1432 # Boundary conditions 1433 Bd2=Dirichlet_boundary([0.2,0.,0.]) 1434 domain2.boundary = domain.boundary 1435 #print 'domain2.boundary' 1436 #print domain2.boundary 1437 domain2.set_boundary({'left': Bd, 'right': Bd, 'top': Bd, 'bottom': Bd}) 1438 #domain2.set_boundary({'exterior': Bd}) 1439 1440 domain2.check_integrity() 1441 1442 for t in domain2.evolve(yieldstep = yiel, finaltime = final): 1443 if visualise: sleep(1.) 1444 #domain2.write_time() 1445 pass 1446 1390 1447 1391 1448 ################### … … 1393 1450 ################## 1394 1451 1395 bits = ['xllcorner','yllcorner','vertex_coordinates','time','starttime'] 1396 1397 for quantity in ['elevation']+domain.quantities_to_be_stored: 1452 bits = ['xllcorner','yllcorner','vertex_coordinates'] 1453 1454 for quantity in ['elevation','xmomentum','ymomentum']:#+domain.quantities_to_be_stored: 1455 bits.append('quantities["%s"].get_integral()'%quantity) 1398 1456 bits.append('get_quantity("%s")'%quantity) 1399 1457 1400 1458 for bit in bits: 1401 # print 'testing that domain.'+bit+' has been restored' 1459 #print bit 1402 1460 assert allclose(eval('domain.'+bit),eval('domain2.'+bit)) 1403 1461 1404 #print 'passed' 1405 1462 1406 1463 1407 1464 def test_sww2domain2(self): … … 1443 1500 elevation = domain.quantities['elevation'].vertex_values 1444 1501 domain.set_quantity('stage', elevation + h) 1445 #elevation = domain.get_quantity('elevation',location='unique vertices')1446 #domain.set_quantity('stage', elevation + h,location='unique vertices')1447 1502 1448 1503 domain.check_integrity() … … 1451 1506 pass 1452 1507 #domain.write_time() 1508 1509 1453 1510 1454 1511 ################################## … … 1459 1516 import os 1460 1517 1461 1462 1518 filename = domain.datadir+os.sep+domain.filename+'.sww' 1463 1519 1464 1520 #Fail because NaNs are present 1465 1521 try: 1466 domain2 = sww2domain(filename, fail_if_NaN=True,verbose=False)1522 domain2 = sww2domain(filename,boundary,fail_if_NaN=True,verbose=False) 1467 1523 assert True == False 1468 1524 except: 1469 1525 #Now import it, filling NaNs to be 0 1470 1526 filler = 0 1471 domain2 = sww2domain(filename, fail_if_NaN=False,NaN_filler = filler,verbose=False)1472 1473 bits = ['xllcorner','yllcorner','vertex_coordinates' ,'time','starttime']1527 domain2 = sww2domain(filename,None,fail_if_NaN=False,NaN_filler = filler,verbose=False) 1528 1529 bits = ['xllcorner','yllcorner','vertex_coordinates'] 1474 1530 1475 1531 for quantity in ['elevation']+domain.quantities_to_be_stored: 1532 bits.append('quantities["%s"].get_integral()'%quantity) 1476 1533 bits.append('get_quantity("%s")'%quantity) 1477 1534 … … 1479 1536 # print 'testing that domain.'+bit+' has been restored' 1480 1537 assert allclose(eval('domain.'+bit),eval('domain2.'+bit)) 1481 1482 #print max(max(domain2.get_quantity('xmomentum')))1483 #print min(min(domain2.get_quantity('xmomentum')))1484 #print max(max(domain2.get_quantity('ymomentum')))1485 #print min(min(domain2.get_quantity('ymomentum')))1486 1538 1487 1539 assert max(max(domain2.get_quantity('xmomentum')))==filler … … 1496 1548 #os.remove(domain.datadir+'/'+domain.filename+'.sww') 1497 1549 1550 1551 #def test_weed(self): 1552 #from data_manager import weed 1553 1554 #coordinates1 = [[0.,0.],[1.,0.],[1.,1.],[1.,0.],[2.,0.],[1.,1.]] 1555 #volumes1 = [[0,1,2],[3,4,5]] 1556 #boundary1= {(0,1): 'external',(1,2): 'not external',(2,0): 'external',(3,4): 'external',(4,5): 'external',(5,3): 'not external'} 1557 #unique2,coordinates2,volumes2,boundary2=weed(coordinates1,volumes1,boundary1) 1558 #print 'coordinates2' 1559 #print coordinates2 1560 #print 'volumes2' 1561 #print volumes2 1562 #print 'boundary2' 1563 #print boundary2 1564 1565 1498 1566 #------------------------------------------------------------- 1499 1567 if __name__ == "__main__": 1500 1568 suite = unittest.makeSuite(Test_Data_Manager,'test') 1501 #suite = unittest.makeSuite(Test_Data_Manager,'test_sww2asc_ mis')1569 #suite = unittest.makeSuite(Test_Data_Manager,'test_sww2asc_stage') 1502 1570 runner = unittest.TextTestRunner() 1503 1571 runner.run(suite)
Note: See TracChangeset
for help on using the changeset viewer.