Changeset 8970
- Timestamp:
- Sep 11, 2013, 7:43:43 AM (11 years ago)
- Location:
- trunk/anuga_core/source/anuga
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/anuga_core/source/anuga/abstract_2d_finite_volumes/quantity.py
r8961 r8970 16 16 17 17 import types 18 import os.path 18 19 19 20 from anuga.utilities.numerical_tools import ensure_numeric, is_scalar … … 349 350 import numpy as np 350 351 351 352 plt.clf() 352 353 plt.tripcolor(X, Y, V, A) 353 354 plt.colorbar() … … 1042 1043 msg = 'Filename must be a text string' 1043 1044 assert isinstance(filename, basestring), msg 1045 1046 msg = 'Extension should be .pts .dem, .csv, or txt' 1047 assert os.path.splitext(filename)[1] in ['.pts', '.dem', '.csv', '.txt'], msg 1048 1044 1049 1045 1050 if location != 'vertices': … … 1083 1088 indices, use_cache=use_cache, 1084 1089 verbose=verbose) 1085 1090 1091 1092 1093 def set_values_from_asc_file(self, 1094 filename, 1095 location='vertices', 1096 indices=None, 1097 verbose=False): 1098 1099 """Read Digital Elevation model from the following ASCII format (.asc or .grd) 1100 1101 Example: 1102 ncols 3121 1103 nrows 1800 1104 xllcorner 722000 1105 yllcorner 5893000 1106 cellsize 25 1107 NODATA_value -9999 1108 138.3698 137.4194 136.5062 135.5558 .......... 1109 1110 1111 An accompanying file with same basename but extension .prj must exist 1112 and is used to fix the UTM zone, datum, false northings and eastings. 1113 1114 The prj format is assumed to be as 1115 1116 Projection UTM 1117 Zone 56 1118 Datum WGS84 1119 Zunits NO 1120 Units METERS 1121 Spheroid WGS84 1122 Xshift 0.0000000000 1123 Yshift 10000000.0000000000 1124 Parameters 1125 """ 1126 1127 1128 1129 1130 msg = 'Filename must be a text string' 1131 assert isinstance(filename, basestring), msg 1132 1133 msg = 'Extension should be .grd or asc' 1134 assert os.path.splitext(filename)[1] in ['.grd', '.asc'], msg 1135 1136 1137 msg = "set_values_from_grd_file is only defined for location='vertices' or 'centroids'" 1138 assert location in ['vertices', 'centroids'], msg 1139 1140 1141 if location == 'centroids': 1142 points = self.domain.centroid_coordinates 1143 else: 1144 points = self.domain.vertex_coordinates 1145 1146 1147 root = filename[:-4] 1148 1149 # # Read Meta data 1150 # if verbose: log.critical('Reading METADATA from %s' % (root + '.prj')) 1151 # 1152 # metadatafile = open(root + '.prj') 1153 # metalines = metadatafile.readlines() 1154 # metadatafile.close() 1155 # 1156 # L = metalines[0].strip().split() 1157 # assert L[0].strip().lower() == 'projection' 1158 # projection = L[1].strip() #TEXT 1159 # 1160 # L = metalines[1].strip().split() 1161 # assert L[0].strip().lower() == 'zone' 1162 # zone = int(L[1].strip()) 1163 # 1164 # L = metalines[2].strip().split() 1165 # assert L[0].strip().lower() == 'datum' 1166 # datum = L[1].strip() #TEXT 1167 # 1168 # L = metalines[3].strip().split() 1169 # assert L[0].strip().lower() == 'zunits' #IGNORE 1170 # zunits = L[1].strip() #TEXT 1171 # 1172 # L = metalines[4].strip().split() 1173 # assert L[0].strip().lower() == 'units' 1174 # units = L[1].strip() #TEXT 1175 # 1176 # L = metalines[5].strip().split() 1177 # assert L[0].strip().lower() == 'spheroid' #IGNORE 1178 # spheroid = L[1].strip() #TEXT 1179 # 1180 # L = metalines[6].strip().split() 1181 # assert L[0].strip().lower() == 'xshift' 1182 # false_easting = float(L[1].strip()) 1183 # 1184 # L = metalines[7].strip().split() 1185 # assert L[0].strip().lower() == 'yshift' 1186 # false_northing = float(L[1].strip()) 1187 1188 1189 #Read DEM data 1190 datafile = open(filename) 1191 1192 if verbose: log.critical('Reading data from %s' % (filename)) 1193 1194 lines = datafile.readlines() 1195 datafile.close() 1196 1197 if verbose: log.critical('Got %d lines' % len(lines)) 1198 1199 1200 ncols = int(lines[0].split()[1].strip()) 1201 nrows = int(lines[1].split()[1].strip()) 1202 1203 1204 # Do cellsize (line 4) before line 2 and 3 1205 cellsize = float(lines[4].split()[1].strip()) 1206 1207 # Checks suggested by Joaquim Luis 1208 # Our internal representation of xllcorner 1209 # and yllcorner is non-standard. 1210 xref = lines[2].split() 1211 if xref[0].strip() == 'xllcorner': 1212 xllcorner = float(xref[1].strip()) # + 0.5*cellsize # Correct offset 1213 elif xref[0].strip() == 'xllcenter': 1214 xllcorner = float(xref[1].strip()) 1215 else: 1216 msg = 'Unknown keyword: %s' % xref[0].strip() 1217 raise Exception, msg 1218 1219 yref = lines[3].split() 1220 if yref[0].strip() == 'yllcorner': 1221 yllcorner = float(yref[1].strip()) # + 0.5*cellsize # Correct offset 1222 elif yref[0].strip() == 'yllcenter': 1223 yllcorner = float(yref[1].strip()) 1224 else: 1225 msg = 'Unknown keyword: %s' % yref[0].strip() 1226 raise Exception, msg 1227 1228 NODATA_value = int(float(lines[5].split()[1].strip())) 1229 1230 assert len(lines) == nrows + 6 1231 1232 1233 #Store data 1234 import numpy 1235 1236 datafile = open(filename) 1237 Z = numpy.loadtxt(datafile, skiprows=6) 1238 datafile.close() 1239 1240 #print Z.shape, Z 1241 1242 x = num.linspace(xllcorner, xllcorner+cellsize*(nrows-1), nrows) 1243 y = num.linspace(yllcorner, yllcorner+cellsize*(ncols-1), ncols) 1244 1245 #print x.shape, x 1246 #print y.shape, y 1247 1248 from anuga.fit_interpolate.interpolate2d import interpolate2d 1249 1250 #print points 1251 values = interpolate2d(x, y, Z, points, mode='linear', bounds_error=False) 1252 1253 #print values 1254 1255 # Call underlying method using array values 1256 if verbose: 1257 log.critical('Applying fitted data to quantity') 1258 1259 1260 if location == 'centroids': 1261 if indices is None: 1262 msg = 'Number of values must match number of elements' 1263 #assert values.shape[0] == N, msg 1264 1265 self.centroid_values[:] = values 1266 else: 1267 msg = 'Number of values must match number of indices' 1268 assert values.shape[0] == indices.shape[0], msg 1269 1270 # Brute force 1271 self.centroid_values[indices] = values 1272 else: 1273 if indices is None: 1274 msg = 'Number of values must match number of elements' 1275 #assert values.shape[0] == N, msg 1276 1277 self.vertex_values[:] = values 1278 else: 1279 msg = 'Number of values must match number of indices' 1280 assert values.shape[0] == indices.shape[0], msg 1281 1282 # Brute force 1283 self.vertex_values[indices] = values 1284 1086 1285 def get_extremum_index(self, mode=None, indices=None): 1087 1286 """Return index for maximum or minimum value of quantity (on centroids) -
trunk/anuga_core/source/anuga/abstract_2d_finite_volumes/test_quantity.py
r8945 r8970 22 22 def linear_function(point): 23 23 point = num.array(point) 24 return point[:,0]+point[:,1] 24 return point[:,0]+3*point[:,1] 25 #return point[:,1] 26 27 28 def axes2points(x, y): 29 """Generate all combinations of grid point coordinates from x and y axes 30 31 Args: 32 * x: x coordinates (array) 33 * y: y coordinates (array) 34 35 Returns: 36 * P: Nx2 array consisting of coordinates for all 37 grid points defined by x and y axes. The x coordinate 38 will vary the fastest to match the way 2D numpy 39 arrays are laid out by default ('C' order). That way, 40 the x and y coordinates will match a corresponding 41 2D array A when flattened (A.flat[:] or A.reshape(-1)) 42 43 Note: 44 Example 45 46 x = [1, 2, 3] 47 y = [10, 20] 48 49 P = [[1, 10], 50 [2, 10], 51 [3, 10], 52 [1, 20], 53 [2, 20], 54 [3, 20]] 55 """ 56 import numpy 57 58 # Reverse y coordinates to have them start at bottom of array 59 y = numpy.flipud(y) 60 61 # Repeat x coordinates for each y (fastest varying) 62 X = numpy.kron(numpy.ones(len(y)), x) 63 64 # Repeat y coordinates for each x (slowest varying) 65 Y = numpy.kron(y, numpy.ones(len(x))) 66 67 # Check 68 N = len(X) 69 assert len(Y) == N 70 71 # Create Nx2 array of x and y coordinates 72 X = numpy.reshape(X, (N, 1)) 73 Y = numpy.reshape(Y, (N, 1)) 74 P = numpy.concatenate((X, Y), axis=1) 75 76 # Return 77 return P 78 25 79 26 80 … … 1333 1387 1334 1388 1389 def test_set_values_from_asc_vertices(self): 1390 1391 quantity= Quantity(self.mesh_onslow) 1392 1393 1394 """ Format of asc file 1395 ncols 11 1396 nrows 12 1397 xllcorner 240000 1398 yllcorner 7620000 1399 cellsize 6000 1400 NODATA_value -9999 1401 """ 1402 1403 # UTM round Onslow 1404 1405 1406 1407 ncols = 11 # Nx 1408 nrows = 12 # Ny 1409 xllcorner = 240000 1410 yllcorner = 7620000 1411 cellsize = 6000 1412 NODATA_value = -9999 1413 1414 #xllcorner = 0 1415 #yllcorner = 100 1416 #cellsize = 10 1417 #NODATA_value = -9999 1418 1419 #Create .asc file 1420 #txt_file = tempfile.mktemp(".asc") 1421 txt_file = 'test_asc.asc' 1422 datafile = open(txt_file,"w") 1423 datafile.write('ncols '+str(ncols)+"\n") 1424 datafile.write('nrows '+str(nrows)+"\n") 1425 datafile.write('xllcorner '+str(xllcorner)+"\n") 1426 datafile.write('yllcorner '+str(yllcorner)+"\n") 1427 datafile.write('cellsize '+str(cellsize)+"\n") 1428 datafile.write('NODATA_value '+str(NODATA_value)+"\n") 1429 1430 x = num.linspace(xllcorner, xllcorner+(ncols-1)*cellsize, ncols) 1431 y = num.linspace(yllcorner, yllcorner+(nrows-1)*cellsize, nrows) 1432 points = axes2points(x, y) 1433 1434 #print points 1435 #print x.shape, x 1436 #print y.shape, y 1437 1438 datavalues = linear_function(points) 1439 #print datavalues 1440 1441 datavalues = datavalues.reshape(nrows,ncols) 1442 1443 #print datavalues 1444 #print datavalues.shape 1445 for row in datavalues: 1446 #print row 1447 datafile.write(" ".join(str(elem) for elem in row) + "\n") 1448 datafile.close() 1449 1450 #print quantity.vertex_values 1451 #print quantity.centroid_values 1452 1453 quantity.set_values_from_asc_file(txt_file, 1454 location='vertices', 1455 indices=None, 1456 verbose=False) 1457 1458 # check order of vertices 1459 answer = [ 23298000. , 23358000. , 23118000. ] 1460 answer = [ 23298000. , 23118000. , 23358000 ] 1461 print quantity.vertex_values 1462 assert num.allclose(quantity.vertex_values, answer) 1463 1464 1465 #print quantity.vertex_values 1466 #print quantity.centroid_values 1467 quantity.set_values(0.0) 1468 1469 1470 #print quantity.vertex_values 1471 #print quantity.centroid_values 1472 quantity.set_values_from_asc_file(txt_file, 1473 location='centroids', 1474 indices=None, 1475 verbose=False) 1476 1477 #print quantity.vertex_values 1478 #print quantity.centroid_values 1479 1480 answer = [ 23258000. ] 1481 assert num.allclose(quantity.centroid_values, answer) 1482 #Cleanup 1483 #import os 1484 #os.remove(txt_file) 1485 1335 1486 1336 1487 … … 2571 2722 2572 2723 if __name__ == "__main__": 2573 suite = unittest.makeSuite(Test_Quantity, 'test') 2724 suite = unittest.makeSuite(Test_Quantity, 'test') #_set_values_from_asc') 2574 2725 runner = unittest.TextTestRunner(verbosity=1) 2575 2726 runner.run(suite) -
trunk/anuga_core/source/anuga/anuga_exceptions.py
r7865 r8970 18 18 class ANUGAError(Exception): 19 19 """ Generic ANUGA error. """ 20 def __init__(self, args=None): 21 self.args = args 20 #def __init__(self, args=None): 21 #self.args = args 22 pass 22 23 23 24 class DataMissingValuesError(exceptions.Exception): -
trunk/anuga_core/source/anuga/config.py
r8823 r8970 178 178 179 179 # Water depth below which it is considered to be 0 in the model 180 minimum_allowed_height = 1.0e-0 3180 minimum_allowed_height = 1.0e-05 181 181 182 182 # Water depth below which it is *stored* as 0 183 minimum_storable_height = 1.0e-0 5183 minimum_storable_height = 1.0e-03 184 184 185 185 # FIXME (Ole): Redefine this parameter to control maximal speeds in general -
trunk/anuga_core/source/anuga/fit_interpolate/fit.py
r8808 r8970 67 67 Padarn Note 05/12/12: This documentation should probably 68 68 be updated to account for the fact that the fitting is now 69 done in C. I wasn't sure what details were nec cesary though.69 done in C. I wasn't sure what details were necessary though. 70 70 71 71 Fit data at points to the vertices of a mesh. -
trunk/anuga_core/source/anuga/fit_interpolate/interpolate2d.py
r8969 r8970 261 261 Nx = len(x) 262 262 Ny = len(y) 263 msg = ('Input array Z must have dimensions %i x %i corresponding to the ' 264 'lengths of the input coordinates x and y. However, ' 265 'Z has dimensions %i x %i.' % (Nx, Ny, m, n)) 263 264 msg = ('Input array Z must have dimensions %i x %i corresponding to the ' 265 'lengths of the input coordinates x and y. However, ' 266 'Z has dimensions %i x %i.' % (Nx, Ny, m, n)) 266 267 if not (Nx == m and Ny == n): 267 268 raise ANUGAError(msg) -
trunk/anuga_core/source/anuga/shallow_water/test_data_manager.py
r8780 r8970 475 475 476 476 A = stage[0,:] 477 #print A[0], (Q2[0,0] + Q1[1,0])/2 478 assert num.allclose(A[0], (Q2[0] + Q1[1])/2) 479 assert num.allclose(A[1], (Q0[1] + Q1[3] + Q2[2])/3) 477 478 # print stage[0,:] 479 # print A[0], (Q2[0] + Q1[1])/2 480 # print A[1], (Q0[1] + Q1[3] + Q2[2])/3 481 # print A[2], Q0[3] 482 # print A[3], (Q0[0] + Q1[5] + Q2[4])/3 483 assert num.allclose(A[0], (Q2[0] + Q1[1])/2, rtol=1.0e-5, atol=1.0e-5) 484 485 assert num.allclose(A[1], (Q0[1] + Q1[3] + Q2[2])/3, rtol=1.0e-5, atol=1.0e-5) 480 486 assert num.allclose(A[2], Q0[3]) 481 assert num.allclose(A[3], (Q0[0] + Q1[5] + Q2[4])/3 )487 assert num.allclose(A[3], (Q0[0] + Q1[5] + Q2[4])/3, rtol=1.0e-5, atol=1.0e-5) 482 488 483 489 #Center point 484 490 assert num.allclose(A[4], (Q1[0] + Q2[1] + Q0[2] +\ 485 Q0[5] + Q2[6] + Q1[7])/6 )491 Q0[5] + Q2[6] + Q1[7])/6, rtol=1.0e-5, atol=1.0e-5) 486 492 487 493 fid.close() … … 527 533 528 534 A = stage[1,:] 529 assert num.allclose(A[0], (Q2[0] + Q1[1])/2 )530 assert num.allclose(A[1], (Q0[1] + Q1[3] + Q2[2])/3 )531 assert num.allclose(A[2], Q0[3] )532 assert num.allclose(A[3], (Q0[0] + Q1[5] + Q2[4])/3 )535 assert num.allclose(A[0], (Q2[0] + Q1[1])/2, rtol=1.0e-5, atol=1.0e-5) 536 assert num.allclose(A[1], (Q0[1] + Q1[3] + Q2[2])/3, rtol=1.0e-5, atol=1.0e-5) 537 assert num.allclose(A[2], Q0[3], rtol=1.0e-5, atol=1.0e-5) 538 assert num.allclose(A[3], (Q0[0] + Q1[5] + Q2[4])/3, rtol=1.0e-5, atol=1.0e-5) 533 539 534 540 #Center point 535 541 assert num.allclose(A[4], (Q1[0] + Q2[1] + Q0[2] +\ 536 Q0[5] + Q2[6] + Q1[7])/6 )542 Q0[5] + Q2[6] + Q1[7])/6, rtol=1.0e-5, atol=1.0e-5) 537 543 538 544 … … 577 583 578 584 if t == 0.0: 579 assert num.allclose(stage, self.initial_stage )580 assert num.allclose(stage_file[:], stage.flatten() )585 assert num.allclose(stage, self.initial_stage, rtol=1.0e-5, atol=1.0e-5) 586 assert num.allclose(stage_file[:], stage.flatten(),rtol=1.0e-5, atol=1.0e-5) 581 587 else: 582 assert not num.allclose(stage, self.initial_stage )583 assert not num.allclose(stage_file[:], stage.flatten() )588 assert not num.allclose(stage, self.initial_stage, rtol=1.0e-5, atol=1.0e-5) 589 assert not num.allclose(stage_file[:], stage.flatten(), rtol=1.0e-5, atol=1.0e-5) 584 590 585 591 fid.close()
Note: See TracChangeset
for help on using the changeset viewer.