Changeset 5517
- Timestamp:
- Jul 17, 2008, 7:48:55 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/abstract_2d_finite_volumes/test_quantity.py
r5349 r5517 14 14 from anuga.geospatial_data.geospatial_data import Geospatial_data 15 15 from anuga.coordinate_transforms.geo_reference import Geo_reference 16 from anuga.utilities.polygon import * 16 17 17 18 #Aux for fit_interpolate.fit example … … 417 418 quantity.set_values(3.14, indices=[0,2], location='vertices') 418 419 419 # Indices refer to triangle numbers here - not vertices (why?)420 # Indices refer to triangle numbers 420 421 assert allclose(quantity.vertex_values, 421 422 [[3.14,3.14,3.14], [0,0,0], … … 434 435 435 436 436 # Another polygon (pick triangle 1 and 2 (rightmost triangles) 437 # Another polygon (pick triangle 1 and 2 (rightmost triangles) using centroids 437 438 polygon = [[2.1, 0.0], [3.5,0.1], [2,2.2], [0.2,2]] 438 439 quantity.set_values(0.0) 439 quantity.set_values(3.14, polygon=polygon) 440 440 quantity.set_values(3.14, location='centroids', polygon=polygon) 441 441 assert allclose(quantity.vertex_values, 442 442 [[0,0,0], … … 446 446 447 447 448 # Same polygon now use vertices (default) 449 polygon = [[2.1, 0.0], [3.5,0.1], [2,2.2], [0.2,2]] 450 quantity.set_values(0.0) 451 #print 'Here 2' 452 quantity.set_values(3.14, polygon=polygon) 453 assert allclose(quantity.vertex_values, 454 [[0,0,0], 455 [3.14,3.14,3.14], 456 [3.14,3.14,3.14], 457 [0,0,0]]) 458 459 #print 'done' 448 460 449 461 # Test input checking … … 488 500 489 501 quantity.set_values(0.0) 490 quantity.set_values(3.14, polygon=polygon )502 quantity.set_values(3.14, polygon=polygon, location='centroids') 491 503 492 504 assert allclose(quantity.vertex_values, … … 680 692 os.remove(ptsfile) 681 693 682 def Cache_cache_test_set_values_from_file(self): 694 695 696 def XXXXtest_set_values_from_file_using_polygon(self): 683 697 quantity = Quantity(self.mesh4) 684 698 … … 715 729 file.close() 716 730 731 # Create restricting polygon (containing vertex #4 (2,2) or centroid of triangle #1 (bce) 732 polygon = [[1.0, 1.0], [4.0, 1.0], 733 [4.0, 4.0], [1.0, 4.0]] 734 735 #print self.mesh4.nodes 736 #print inside_polygon(self.mesh4.nodes, polygon) 737 assert allclose(inside_polygon(self.mesh4.nodes, polygon), 4) 738 739 #print quantity.domain.get_vertex_coordinates() 740 #print quantity.domain.get_nodes() 741 742 # Check that values can be set from file 743 quantity.set_values(filename=ptsfile, 744 polygon=polygon, 745 location='unique vertices', 746 alpha=0) 747 748 # Get indices for vertex coordinates in polygon 749 indices = inside_polygon(quantity.domain.get_vertex_coordinates(), polygon) 750 points = take(quantity.domain.get_vertex_coordinates(), indices) 751 752 answer = linear_function(points) 753 754 #print quantity.vertex_values.flat 755 #print answer 756 757 # Check vertices in polygon have been set 758 assert allclose(take(quantity.vertex_values.flat, indices), 759 answer) 760 761 # Check vertices outside polygon are zero 762 indices = outside_polygon(quantity.domain.get_vertex_coordinates(), polygon) 763 assert allclose(take(quantity.vertex_values.flat, indices), 764 0.0) 765 766 #Cleanup 767 import os 768 os.remove(ptsfile) 769 770 771 772 773 def Cache_cache_test_set_values_from_file(self): 774 quantity = Quantity(self.mesh4) 775 776 #Get (enough) datapoints 777 data_points = [[ 0.66666667, 0.66666667], 778 [ 1.33333333, 1.33333333], 779 [ 2.66666667, 0.66666667], 780 [ 0.66666667, 2.66666667], 781 [ 0.0, 1.0], 782 [ 0.0, 3.0], 783 [ 1.0, 0.0], 784 [ 1.0, 1.0], 785 [ 1.0, 2.0], 786 [ 1.0, 3.0], 787 [ 2.0, 1.0], 788 [ 3.0, 0.0], 789 [ 3.0, 1.0]] 790 791 data_geo_spatial = Geospatial_data(data_points, 792 geo_reference = Geo_reference(56, 0, 0)) 793 data_points_absolute = data_geo_spatial.get_data_points(absolute=True) 794 attributes = linear_function(data_points_absolute) 795 att = 'spam_and_eggs' 796 797 #Create .txt file 798 ptsfile = tempfile.mktemp(".txt") 799 file = open(ptsfile,"w") 800 file.write(" x,y," + att + " \n") 801 for data_point, attribute in map(None, data_points_absolute 802 ,attributes): 803 row = str(data_point[0]) + ',' + str(data_point[1]) \ 804 + ',' + str(attribute) 805 file.write(row + "\n") 806 file.close() 807 717 808 718 809 #Check that values can be set from file … … 884 975 os.remove(pts_file) 885 976 886 def test_set_values_from_UTM_pts(self):977 def verbose_test_set_values_from_UTM_pts(self): 887 978 quantity = Quantity(self.mesh_onslow) 888 979 889 VERBOSE = False # Change this to True to see output890 980 #Get (enough) datapoints 891 981 data_points = [[-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], … … 942 1032 #Check that values can be set from file 943 1033 quantity.set_values_from_file(pts_file, att, 0, 944 'vertices', None, verbose = VERBOSE,1034 'vertices', None, verbose = True, 945 1035 max_read_lines=2) 946 1036 answer = linear_function(quantity.domain.get_vertex_coordinates()) … … 950 1040 951 1041 #Check that values can be set from file 952 quantity.set_values(filename = pts_file, verbose = VERBOSE,1042 quantity.set_values(filename = pts_file, 953 1043 attribute_name = att, alpha = 0) 954 1044 answer = linear_function(quantity.domain.get_vertex_coordinates()) … … 962 1052 assert allclose(quantity.vertex_values.flat, answer) 963 1053 964 #Check that values can be block read from a text file965 quantity.set_values_from_file(txt_file, att, 0,966 'vertices', None, verbose = VERBOSE,967 max_read_lines=2)968 969 1054 #Cleanup 970 1055 import os … … 1184 1269 1185 1270 1186 # Negation1271 # Negation 1187 1272 Q = -quantity1 1188 1273 assert allclose(Q.vertex_values, -quantity1.vertex_values) … … 1190 1275 assert allclose(Q.edge_values, -quantity1.edge_values) 1191 1276 1192 # Addition1277 # Addition 1193 1278 Q = quantity1 + 7 1194 1279 assert allclose(Q.vertex_values, quantity1.vertex_values + 7) … … 2349 2434 #------------------------------------------------------------- 2350 2435 if __name__ == "__main__": 2351 suite = unittest.makeSuite(Test_Quantity, 'test') 2352 2353 #suite = unittest.makeSuite(Test_Quantity, 'test_set_values_from_UTM') 2436 suite = unittest.makeSuite(Test_Quantity, 'test') 2437 #suite = unittest.makeSuite(Test_Quantity, 'test_set_values_from_file_using_polygon') 2438 2439 #suite = unittest.makeSuite(Test_Quantity, 'test_set_vertex_values_using_general_interface_with_subset') 2354 2440 #print "restricted test" 2355 2441 #suite = unittest.makeSuite(Test_Quantity,'verbose_test_set_values_from_UTM_pts') 2356 runner = unittest.TextTestRunner() #verbosity=2)2442 runner = unittest.TextTestRunner() 2357 2443 runner.run(suite)
Note: See TracChangeset
for help on using the changeset viewer.