Changeset 1753
- Timestamp:
- Aug 24, 2005, 1:49:47 PM (19 years ago)
- Location:
- inundation
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/pyvolution/data_manager.py
r1740 r1753 963 963 964 964 import os 965 from Scientific.IO.NetCDF import NetCDFFile965 #from Scientific.IO.NetCDF import NetCDFFile 966 966 from Numeric import Float, arrayrange, concatenate 967 967 … … 1010 1010 1011 1011 if verbose: print 'Store to NetCDF file %s' %ptsname 1012 write_ptsfile(ptsname, points, attribute, attribute_name) 1013 1014 1015 def write_ptsfile(ptsname, points, attribute, attribute_name = None): 1016 """Write points and associated attribute to pts (NetCDF) format 1017 """ 1018 1019 from Numeric import Float 1020 1021 if attribute_name is None: 1022 attribute_name = 'attribute' 1023 1024 1025 from Scientific.IO.NetCDF import NetCDFFile 1026 1012 1027 # NetCDF file definition 1013 1028 outfile = NetCDFFile(ptsname, 'w') 1029 1014 1030 1015 1031 #Create new file … … 1040 1056 nc_attribute[:] = attribute 1041 1057 1042 infile.close()1043 1058 outfile.close() 1044 1059 1060 1045 1061 def dem2pts(basename_in, basename_out=None, verbose=False, 1046 1062 easting_min=None, easting_max=None, … … 1063 1079 elevation: N Float array 1064 1080 """ 1081 1082 #FIXME: Can this be written feasibly using write_pts? 1065 1083 1066 1084 import os -
inundation/pyvolution/domain.py
r1752 r1753 16 16 def __init__(self, coordinates, vertices, boundary = None, 17 17 conserved_quantities = None, other_quantities = None, 18 tagged_elements = None, geo_reference = None, use_inscribed_circle=False): 18 tagged_elements = None, geo_reference = None, 19 use_inscribed_circle=False): 19 20 20 21 Mesh.__init__(self, coordinates, vertices, boundary, … … 141 142 142 143 143 def set_quantity(self, name, 144 X,145 location = 'vertices',146 indices = None):144 def set_quantity(self, name, *args, **kwargs): 145 #X, 146 #location = 'vertices', 147 #indices = None): 147 148 """Set values for named quantity 148 149 … … 196 197 #Set value 197 198 #FIXME: use **kwargs 198 self.quantities[name].set_values(X,199 location = location,200 indices = indices)201 199 #self.quantities[name].set_values(X, 200 # location = location, 201 # indices = indices) 202 self.quantities[name].set_values(*args, **kwargs) 202 203 203 204 -
inundation/pyvolution/quantity.py
r1752 r1753 91 91 location = 'vertices', 92 92 indices = None, 93 verbose = None): 93 verbose = None, 94 use_cache = False): 94 95 95 96 """Set values for quantity based on different sources. … … 155 156 will be left undefined. 156 157 158 verbose: True means that output to stdout is generated 159 160 use_cache: True means that caching of intermediate results is 161 attempted for least squares fit. 162 163 164 157 165 158 166 Exactly one of the arguments … … 211 219 assert values is not None, msg 212 220 self.set_values_from_points(points, values, alpha, 213 location, indices, verbose) 221 location, indices, verbose, 222 use_cache) 214 223 elif filename is not None: 215 224 self.set_values_from_file(filename, attribute_name, alpha, 216 location, indices, verbose) 225 location, indices, verbose, 226 use_cache) 217 227 else: 218 228 raise 'This can\'t happen :-)' … … 437 447 438 448 def set_values_from_points(self, points, values, alpha, 439 location, indices, verbose): 440 """ 441 """ 442 443 444 #FIXME: Needs unit test 449 location, indices, verbose, use_cache): 450 """Set quantity values from arbitray data points using least squares 451 """ 452 453 from Numeric import Float 445 454 from util import ensure_numeric 446 455 from least_squares import fit_to_mesh … … 457 466 triangles = self.domain.triangles 458 467 459 #FIXME Pass and use caching here 460 vertex_attributes = fit_to_mesh(coordinates, 461 triangles, 462 points, 463 values, 464 alpha = alpha, 465 verbose = verbose) 468 if use_cache is True: 469 try: 470 from caching import cache 471 except: 472 msg = 'Caching was requested, but caching module'+\ 473 'could not be imported' 474 raise msg 475 476 args = (coordinates, triangles, points, values) 477 kwargs = {'alpha': alpha, 'verbose': verbose} 478 vertex_attributes = cache(fit_to_mesh, 479 args, kwargs, 480 verbose = verbose) 481 else: 482 vertex_attributes = fit_to_mesh(coordinates, 483 triangles, 484 points, 485 values, 486 alpha = alpha, 487 verbose = verbose) 488 466 489 467 490 self.set_values_from_array(vertex_attributes, … … 473 496 474 497 def set_values_from_file(self, filename, attribute_name, alpha, 475 location, indices, verbose ):498 location, indices, verbose, use_cache): 476 499 """Set quantity based on arbitrary points in .pts file 477 500 using least_squares attribute_name selects name of attribute … … 481 504 482 505 483 #FIXME: Needs unit test484 506 from types import StringType 485 507 msg = 'Filename must be a text string' … … 513 535 #Call least squares method 514 536 self.set_values_from_points(points, z, alpha, 515 location, indices, verbose )537 location, indices, verbose, use_cache) 516 538 517 539 … … 523 545 location: Where values are to be stored. 524 546 Permissible options are: vertices, edges, centroid 525 Default is "vertices"547 and unique vertices. Default is 'vertices' 526 548 527 549 In case of location == 'centroids' the dimension values must -
inundation/pyvolution/test_advection.py
r1556 r1753 77 77 domain.check_integrity() 78 78 79 domain.set_quantity('stage', [1.0], 'centroids')79 domain.set_quantity('stage', [1.0], location='centroids') 80 80 81 81 domain.distribute_to_vertices_and_edges() … … 136 136 #Populate boundary array with dirichlet conditions. 137 137 domain.neighbours = array([[1,-1,-2], [0,-3,-4]]) 138 domain.set_quantity('stage', [1.0, 0.0], 'centroids')138 domain.set_quantity('stage', [1.0, 0.0], location='centroids') 139 139 domain.distribute_to_vertices_and_edges() 140 140 -
inundation/pyvolution/test_data_manager.py
r1740 r1753 492 492 #Cleanup 493 493 os.remove(sww.filename) 494 495 496 def test_write_pts(self): 497 #Get (enough) datapoints 498 499 from Numeric import array 500 points = array([[ 0.66666667, 0.66666667], 501 [ 1.33333333, 1.33333333], 502 [ 2.66666667, 0.66666667], 503 [ 0.66666667, 2.66666667], 504 [ 0.0, 1.0], 505 [ 0.0, 3.0], 506 [ 1.0, 0.0], 507 [ 1.0, 1.0], 508 [ 1.0, 2.0], 509 [ 1.0, 3.0], 510 [ 2.0, 1.0], 511 [ 3.0, 0.0], 512 [ 3.0, 1.0]]) 513 514 z = points[:,0] + 2*points[:,1] 515 516 ptsfile = 'testptsfile.pts' 517 write_ptsfile(ptsfile, points, z, 518 attribute_name = 'linear_combination') 519 520 #Check contents 521 #Get NetCDF 522 from Scientific.IO.NetCDF import NetCDFFile 523 fid = NetCDFFile(ptsfile, 'r') 524 525 # Get the variables 526 #print fid.variables.keys() 527 points1 = fid.variables['points'] 528 z1 = fid.variables['linear_combination'] 529 530 #Check values 531 532 #print points[:] 533 #print ref_points 534 assert allclose(points, points1) 535 536 #print attributes[:] 537 #print ref_elevation 538 assert allclose(z, z1) 539 540 #Cleanup 541 fid.close() 542 543 import os 544 os.remove(ptsfile) 545 494 546 495 547 -
inundation/pyvolution/test_domain.py
r1751 r1753 290 290 291 291 292 domain.set_quantity('stage', [1,2,3,4], 'centroids')293 domain.set_quantity('xmomentum', [1,2,3,4], 'centroids')294 domain.set_quantity('ymomentum', [1,2,3,4], 'centroids')292 domain.set_quantity('stage', [1,2,3,4], location='centroids') 293 domain.set_quantity('xmomentum', [1,2,3,4], location='centroids') 294 domain.set_quantity('ymomentum', [1,2,3,4], location='centroids') 295 295 296 296 -
inundation/pyvolution/test_quantity.py
r1752 r1753 276 276 277 277 278 278 279 279 280 def test_set_vertex_values_using_least_squares(self): 280 from least_squares import Interpolation, fit_to_mesh 281 281 282 282 283 quantity = Quantity(self.mesh4) … … 297 298 [ 3.0, 1.0]] 298 299 299 300 interp = Interpolation(quantity.domain.coordinates, quantity.domain.triangles,301 data_points, alpha=0.0)302 303 300 z = linear_function(data_points) 304 answer = linear_function(quantity.domain.coordinates) 305 306 f = interp.fit(z) 307 301 302 #Obsoleted bit 303 #interp = Interpolation(quantity.domain.coordinates, 304 # quantity.domain.triangles, 305 # data_points, alpha=0.0) 306 # 307 #answer = linear_function(quantity.domain.coordinates) 308 # 309 #f = interp.fit(z) 310 # 308 311 #print "f",f 309 312 #print "answer",answer 310 assert allclose(f, answer) 311 312 313 quantity.set_values(f) 314 315 313 #assert allclose(f, answer) 314 315 316 #Use built-in least squares fit 317 quantity.set_values(points = data_points, values = z, alpha = 0) 316 318 answer = linear_function(quantity.domain.get_vertex_coordinates(obj = True)) 317 319 #print quantity.vertex_values, answer 318 320 assert allclose(quantity.vertex_values.flat, answer) 319 321 320 #Now try using the general interface 321 322 323 #Now try by setting the same values directly 324 from least_squares import fit_to_mesh 322 325 vertex_attributes = fit_to_mesh(quantity.domain.coordinates, 323 326 quantity.domain.triangles, … … 329 332 #print vertex_attributes 330 333 quantity.set_values(vertex_attributes) 334 assert allclose(quantity.vertex_values.flat, answer) 335 336 337 def test_set_values_from_file(self): 338 quantity = Quantity(self.mesh4) 339 340 #Get (enough) datapoints 341 data_points = [[ 0.66666667, 0.66666667], 342 [ 1.33333333, 1.33333333], 343 [ 2.66666667, 0.66666667], 344 [ 0.66666667, 2.66666667], 345 [ 0.0, 1.0], 346 [ 0.0, 3.0], 347 [ 1.0, 0.0], 348 [ 1.0, 1.0], 349 [ 1.0, 2.0], 350 [ 1.0, 3.0], 351 [ 2.0, 1.0], 352 [ 3.0, 0.0], 353 [ 3.0, 1.0]] 354 355 z = linear_function(data_points) 356 357 358 #Create pts file 359 from data_manager import write_ptsfile 360 ptsfile = 'testptsfile.pts' 361 att = 'spam_and_eggs' 362 write_ptsfile(ptsfile, data_points, z, attribute_name = att) 363 364 #Check that values can be set from file 365 quantity.set_values(filename = ptsfile, 366 attribute_name = att, alpha = 0) 367 answer = linear_function(quantity.domain.get_vertex_coordinates(obj = True)) 368 #print quantity.vertex_values, answer 369 assert allclose(quantity.vertex_values.flat, answer) 370 371 372 #Check that values can be set from file using default attribute 373 quantity.set_values(filename = ptsfile, alpha = 0) 331 374 assert allclose(quantity.vertex_values.flat, answer) 332 375 376 #Cleanup 377 import os 378 os.remove(ptsfile) 379 333 380 334 381 … … 832 879 indices = [1] 833 880 quantity.set_values(value, 834 835 881 location = 'centroids', 882 indices = indices) 836 883 #print "quantity.centroid_values",quantity.centroid_values 837 884 assert allclose(quantity.centroid_values, [1,7,3,4,5,6]) -
inundation/pyvolution/test_shallow_water.py
r1671 r1753 1238 1238 val3 = 2. 1239 1239 1240 domain.set_quantity('stage', [val0, val1, val2, val3], 'centroids') 1240 domain.set_quantity('stage', [val0, val1, val2, val3], 1241 location='centroids') 1241 1242 L = domain.quantities['stage'].vertex_values 1242 1243 … … 1274 1275 return x**2 1275 1276 1276 domain.set_quantity('stage', stage, 'centroids')1277 domain.set_quantity('stage', stage, location='centroids') 1277 1278 1278 1279 a, b = domain.quantities['stage'].compute_gradients() … … 1311 1312 return x**4+y**2 1312 1313 1313 domain.set_quantity('stage', stage, 'centroids')1314 domain.set_quantity('stage', stage, location='centroids') 1314 1315 #print domain.quantities['stage'].centroid_values 1315 1316 … … 1355 1356 1356 1357 domain.set_quantity('elevation', slope) 1357 domain.set_quantity('stage', stage, 'centroids')1358 domain.set_quantity('stage', stage, location='centroids') 1358 1359 1359 1360 #print domain.quantities['elevation'].centroid_values … … 1443 1444 domain.set_quantity('elevation', slope) 1444 1445 domain.set_quantity('stage', 1445 [0.01298164, 0.00365611, 0.01440365, -0.0381856437096], 1446 'centroids') 1446 [0.01298164, 0.00365611, 1447 0.01440365, -0.0381856437096], 1448 location='centroids') 1447 1449 domain.set_quantity('xmomentum', 1448 [0.00670439, 0.01263789, 0.00647805, 0.0178180740668], 1449 'centroids') 1450 [0.00670439, 0.01263789, 1451 0.00647805, 0.0178180740668], 1452 location='centroids') 1450 1453 domain.set_quantity('ymomentum', 1451 [-7.23510980e-004, -6.30413883e-005, 6.30413883e-005, 0.000200907255866], 1452 'centroids') 1454 [-7.23510980e-004, -6.30413883e-005, 1455 6.30413883e-005, 0.000200907255866], 1456 location='centroids') 1453 1457 1454 1458 E = domain.quantities['elevation'].vertex_values … … 1466 1470 #print Y[1,:] 1467 1471 1468 assert allclose(L[1,:], [-0.00825735775384, -0.00801881482869, 0.0272445025825]) 1469 assert allclose(X[1,:], [0.0143507718962, 0.0142502147066, 0.00931268339717]) 1470 assert allclose(Y[1,:], [-0.000117062180693, 7.94434448109e-005, -0.000151505429018]) 1472 assert allclose(L[1,:], [-0.00825735775384, 1473 -0.00801881482869, 1474 0.0272445025825]) 1475 assert allclose(X[1,:], [0.0143507718962, 1476 0.0142502147066, 1477 0.00931268339717]) 1478 assert allclose(Y[1,:], [-0.000117062180693, 1479 7.94434448109e-005, 1480 -0.000151505429018]) 1471 1481 1472 1482 … … 2211 2221 2212 2222 2213 domain.set_quantity('stage', L, 'centroids')2214 domain.set_quantity('xmomentum', X, 'centroids')2215 domain.set_quantity('ymomentum', Y, 'centroids')2223 domain.set_quantity('stage', L, location='centroids') 2224 domain.set_quantity('xmomentum', X, location='centroids') 2225 domain.set_quantity('ymomentum', Y, location='centroids') 2216 2226 2217 2227 domain.check_integrity() -
inundation/validation/LWRU2/lwru2.py
r1747 r1753 11 11 12 12 13 read_mesh = True #Use large unstructured mesh generated from create_mesh.py14 #read_mesh = False #Use small structured mesh13 #read_mesh = True #Use large unstructured mesh generated from create_mesh.py 14 read_mesh = False #Use small structured mesh 15 15 16 16 import sys … … 128 128 domain.filename, _ = os.path.splitext(base) 129 129 130 #LS code to be included in set_quantity 131 print 'Reading points' 132 from pyvolution import util, least_squares 133 #points, attributes = util.read_xya(filenames.bathymetry_filename[:-4] + '.pts') 134 points, attributes = cache(util.read_xya, 135 filenames.bathymetry_filename[:-4] + '.pts') 130 print 'Initial values' 136 131 137 elevation = attributes['elevation'] 138 139 #Fit attributes to mesh 140 #vertex_attributes = least_squares.fit_to_mesh(domain.coordinates, 141 # domain.triangles, 142 # points, 143 # attributes['elevation'], 144 # alpha = 0.0001, 145 # verbose=True) 146 147 vertex_attributes = cache(least_squares.fit_to_mesh, 148 (domain.coordinates, domain.triangles, 149 points, elevation) , 150 {'alpha': 0.0001, 'verbose': True}) 151 152 print 'Initial values' 153 domain.set_quantity('elevation', vertex_attributes) 154 #domain.set_quantity('elevation', points=points, attributes=attributes['elevation'], alpha=0.0001) 132 domain.set_quantity('elevation', 133 filename=filenames.bathymetry_filename[:-4] + '.pts', 134 alpha = 0.0001, 135 verbose = True, 136 use_cache = True) 137 155 138 domain.set_quantity('friction', 0.0) 156 139 domain.set_quantity('stage', 0.0)
Note: See TracChangeset
for help on using the changeset viewer.