Changeset 7873


Ignore:
Timestamp:
Jun 25, 2010, 11:10:58 AM (14 years ago)
Author:
hudson
Message:

Added ungenerate loading functionality.

Location:
trunk/anuga_core/source/anuga
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/anuga_core/source/anuga/pmesh/mesh.py

    r7872 r7873  
    3636import anuga.utilities.log as log
    3737
     38from anuga.file.ungenerate import load_ungenerate
    3839
    3940try: 
     
    18931894        """
    18941895   
    1895         dict = importUngenerateFile(ofile)
     1896        dict = load_ungenerate(ofile)
    18961897        default_tag = Segment.get_default_tag()
    18971898        if tag is not None:
     
    21502151            #index +=1
    21512152            self.regions.append(Object)
    2152  
    2153 def importUngenerateFile(ofile):
    2154     """
    2155     import a file, ofile, with the format
    2156     [poly]
    2157     poly format:
    2158     First line:  <# of vertices> <x centroid> <y centroid>
    2159     Following lines: <x> <y>
    2160     last line:  "END"
    2161 
    2162     Note: These are clockwise.
    2163     """
    2164     fd = open(ofile,'r')
    2165     Dict = readUngenerateFile(fd)
    2166     fd.close()
    2167     return Dict
    2168 
    2169 def readUngenerateFile(fd):
    2170     """
    2171     import a file, ofile, with the format
    2172     [poly]
    2173     poly format:
    2174     First line:  <# of polynomial> <x centroid> <y centroid>
    2175     Following lines: <x> <y>
    2176     last line:  "END"
    2177     """
    2178    
    2179     END_DELIMITER = 'END'
    2180    
    2181     points = []
    2182     segments = []
    2183     polygons = []
    2184    
    2185     isEnd = False
    2186     line = fd.readline() #not used <# of polynomial> <x> <y>
    2187     while not isEnd:
    2188         poly = []
    2189         line = fd.readline()
    2190         fragments = line.split()
    2191         x = float(fragments.pop(0))
    2192         y = float(fragments.pop(0))
    2193         points.append([x,y])
    2194         poly.append([x,y])
    2195         PreviousVertIndex = len(points)-1
    2196         firstVertIndex = PreviousVertIndex
    2197        
    2198         line = fd.readline() #Read the next line
    2199         while not line.startswith(END_DELIMITER):
    2200             #print "line >" + line + "<"
    2201             fragments = line.split()
    2202             x = float(fragments.pop(0))
    2203             y = float(fragments.pop(0))
    2204             points.append([x,y])
    2205             poly.append([x,y])
    2206             thisVertIndex = len(points)-1
    2207             segment = [PreviousVertIndex,thisVertIndex]
    2208             segments.append(segment)
    2209             PreviousVertIndex = thisVertIndex
    2210             line = fd.readline() #Read the next line
    2211         # If the last and first segments are the same,
    2212         # Remove the last segment and the last vertex
    2213         # then add a segment from the second last vert to the 1st vert
    2214         thisVertIndex = len(points)-1
    2215         firstVert = points[firstVertIndex]
    2216         thisVert = points[thisVertIndex]
    2217         #print "firstVert",firstVert
    2218         #print "thisVert",thisVert
    2219         if (firstVert[0] == thisVert[0] and firstVert[1] == thisVert[1]):
    2220             points.pop()
    2221             segments.pop()
    2222             poly.pop()
    2223             thisVertIndex = len(points)-1
    2224         segments.append([thisVertIndex, firstVertIndex])
    2225        
    2226         line = fd.readline() # read <# of polynomial> <x> <y> OR END
    2227         #print "line >>" + line + "<<"
    2228         # do poly stuff here
    2229         polygons.append(poly)
    2230         if line.startswith(END_DELIMITER):
    2231             isEnd = True
    2232    
    2233     #print "points", points       
    2234     #print "segments", segments
    2235     ungenerated_dict = {}
    2236     ungenerated_dict['points'] = points
    2237     ungenerated_dict['segments'] = segments
    2238     ungenerated_dict['polygons'] = polygons
    2239     return ungenerated_dict
     2153
    22402154
    22412155def importMeshFromFile(ofile):
  • trunk/anuga_core/source/anuga/pmesh/test_mesh.py

    r7711 r7873  
    11941194
    11951195       
    1196     def test_ungenerateFileLoading(self):
    1197        
    1198         fileName = tempfile.mktemp(".txt")
    1199         file = open(fileName,"w")
    1200         file.write("         1       ??      ??\n\
    1201        0.0       0.0\n\
    1202        1.0       0.0\n\
    1203        1.0       1.0\n\
    1204        0.0       1.0\n\
    1205        0.0       0.0\n\
    1206 END\n\
    1207          2      ?? ??\n\
    1208        10.0       10.0\n\
    1209        10.0       20.0\n\
    1210        20.0       20.0\n\
    1211        10.0       10.0\n\
    1212 END\n\
    1213 END\n")
    1214         file.close()
    1215        
    1216        
    1217         a = Vertex (0.0, 0.0) #, attributes = [1.1])
    1218         b = Vertex (0.0, 40.0) #, attributes = [1.2])
    1219         c = Vertex (40.0,40.0) #, attributes = [1.3])
    1220         d = Vertex (40.0,0.0) #, attributes = [1.4])
    1221    
    1222         s1 = Segment(a,b)
    1223         s2 = Segment(b,c)
    1224         s3 = Segment(c,d)
    1225         s4 = Segment(d,a)
    1226      
    1227         m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4])
    1228         dict = importUngenerateFile(fileName)
    1229         #os.remove(fileName)
    1230 
    1231         tag = "DSG"
    1232         Segment.set_default_tag(tag)
    1233         m.addVertsSegs(dict)
    1234 
    1235         # have to reset this , since it's a class attribute
    1236         Segment.set_default_tag("")
    1237            
    1238         self.failUnless(len(m.userSegments) ==11,
    1239                         'Wrong segment list length.')
    1240         self.failUnless(len(m.userVertices) == 11,
    1241                         'Wrong vertex list length.')
    1242         self.failUnless(m.userSegments[10].vertices[0] == m.userVertices[10],
    1243                         'bad vertex on segment.')
    1244         self.failUnless(m.userSegments[10].vertices[1] == m.userVertices[8],
    1245                         'Bad segment.')
    1246         self.failUnless(m.userSegments[10].tag == tag,
    1247                         'wrong tag.')
    1248 
    1249         ## let's test the method
    1250         a = Vertex (0.0, 0.0) #, attributes = [1.1])
    1251         b = Vertex (0.0, 40.0) #, attributes = [1.2])
    1252         c = Vertex (40.0,40.0) #, attributes = [1.3])
    1253         d = Vertex (40.0,0.0) #, attributes = [1.4])
    1254    
    1255         s1 = Segment(a,b)
    1256         s2 = Segment(b,c)
    1257         s3 = Segment(c,d)
    1258         s4 = Segment(d,a)
    1259      
    1260         m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4])
    1261 
    1262         tag = "DSG"       
    1263         initial_tag = "PIG"
    1264         Segment.set_default_tag(initial_tag)
    1265         m.import_ungenerate_file(fileName, tag=tag)
    1266 
    1267         os.remove(fileName)
    1268 
    1269         self.failUnless(Segment.get_default_tag() == initial_tag,
    1270                         'Wrong segment list length.')
    1271        
    1272 
    1273         # have to reset this , since it's a class attribute
    1274         Segment.set_default_tag("")
    1275            
    1276         self.failUnless(len(m.userSegments) ==11,
    1277                         'Wrong segment list length.')
    1278         self.failUnless(len(m.userVertices) == 11,
    1279                         'Wrong vertex list length.')
    1280         self.failUnless(m.userSegments[10].vertices[0] == m.userVertices[10],
    1281                         'bad vertex on segment.')
    1282         self.failUnless(m.userSegments[10].vertices[1] == m.userVertices[8],
    1283                         'Bad segment.')
    1284         self.failUnless(m.userSegments[10].tag == tag,
    1285                         'wrong tag.')
    1286        
    1287     def test_import_ungenerate_file(self):
    1288        
    1289         fileName = tempfile.mktemp(".txt")
    1290         file = open(fileName,"w")
    1291         file.write("         1       ??      ??\n\
    1292        10.0       10.0\n\
    1293        11.0       10.0\n\
    1294        11.0       11.0\n\
    1295        10.0       11.0\n\
    1296        10.0       10.0\n\
    1297 END\n\
    1298          2      ?? ??\n\
    1299        20.0       20.0\n\
    1300        20.0       30.0\n\
    1301        30.0       30.0\n\
    1302 END\n\
    1303 END\n")
    1304         file.close()
    1305        
    1306        
    1307         a = Vertex (0.0, 0.0) #, attributes = [1.1])
    1308         b = Vertex (0.0, 40.0) #, attributes = [1.2])
    1309         c = Vertex (40.0,40.0) #, attributes = [1.3])
    1310         d = Vertex (40.0,0.0) #, attributes = [1.4])
    1311    
    1312         s1 = Segment(a,b)
    1313         s2 = Segment(b,c)
    1314         s3 = Segment(c,d)
    1315         s4 = Segment(d,a)
    1316      
    1317         m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4])
    1318         dict = importUngenerateFile(fileName)
    1319         #os.remove(fileName)
    1320 
    1321         tag = "DSG"
    1322         Segment.set_default_tag(tag)
    1323         m.addVertsSegs(dict)
    1324 
    1325         self.failUnless(len(m.userSegments) ==11,
    1326                         'Wrong segment list length.')
    1327         self.failUnless(len(m.userVertices) == 11,
    1328                         'Wrong vertex list length.')
    1329 
    1330         # Test the method
    1331         a = Vertex (0.0, 0.0) #, attributes = [1.1])
    1332         b = Vertex (0.0, 40.0) #, attributes = [1.2])
    1333         c = Vertex (40.0,40.0) #, attributes = [1.3])
    1334         d = Vertex (40.0,0.0) #, attributes = [1.4])
    1335    
    1336         s1 = Segment(a,b)
    1337         s2 = Segment(b,c)
    1338         s3 = Segment(c,d)
    1339         s4 = Segment(d,a)
    1340      
    1341         m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4])
    1342         tag = "DSG"       
    1343         initial_tag = "PIG"
    1344         Segment.set_default_tag(initial_tag)
    1345         m.import_ungenerate_file(fileName, tag=tag, region_tag="swamp")
    1346 
    1347         os.remove(fileName)
    1348 
    1349         self.failUnless(Segment.get_default_tag() == initial_tag,
    1350                         'Wrong segment list length.')
    1351         m.export_mesh_file("swamp.tsh")
    1352         #print "m.userSegments",m.userSegments
    1353         self.failUnless(len(m.userSegments) ==11,
    1354                         'Wrong segment list length.')
    1355         self.failUnless(len(m.userVertices) == 11,
    1356                         'Wrong vertex list length.')
    1357         self.failUnless(len(m.regions) == 2,
    1358                         'Wrong regions list length.')
    1359         self.failUnless(m.regions[0].getTag() == "swamp",
    1360                         'Wrong regions tag.')
    1361         self.failUnless(m.regions[1].getTag() == "swamp",
    1362                         'Wrong regions 1 tag.')
    1363        
    1364         # have to reset this , since it's a class attribute
    1365         Segment.set_default_tag("")
    1366        
    1367        
    1368     def test_import_ungenerate_file_different_region_tags(self):
    1369        
    1370         fileName = tempfile.mktemp(".txt")
    1371         file = open(fileName,"w")
    1372         file.write("         1       ??      ??\n\
    1373        10.0       10.0\n\
    1374        11.0       10.0\n\
    1375        11.0       11.0\n\
    1376        10.0       11.0\n\
    1377        10.0       10.0\n\
    1378 END\n\
    1379          2      ?? ??\n\
    1380        20.0       20.0\n\
    1381        20.0       30.0\n\
    1382        30.0       30.0\n\
    1383 END\n\
    1384 END\n")
    1385         file.close()
    1386        
    1387        
    1388         a = Vertex (0.0, 0.0) #, attributes = [1.1])
    1389         b = Vertex (0.0, 40.0) #, attributes = [1.2])
    1390         c = Vertex (40.0,40.0) #, attributes = [1.3])
    1391         d = Vertex (40.0,0.0) #, attributes = [1.4])
    1392    
    1393         s1 = Segment(a,b)
    1394         s2 = Segment(b,c)
    1395         s3 = Segment(c,d)
    1396         s4 = Segment(d,a)
    1397      
    1398         m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4])
    1399         dict = importUngenerateFile(fileName)
    1400         #os.remove(fileName)
    1401 
    1402         tag = "DSG"
    1403         Segment.set_default_tag(tag)
    1404         m.addVertsSegs(dict)
    1405 
    1406         self.failUnless(len(m.userSegments) ==11,
    1407                         'Wrong segment list length.')
    1408         self.failUnless(len(m.userVertices) == 11,
    1409                         'Wrong vertex list length.')
    1410 
    1411         # Test the method
    1412         a = Vertex (0.0, 0.0) #, attributes = [1.1])
    1413         b = Vertex (0.0, 40.0) #, attributes = [1.2])
    1414         c = Vertex (40.0,40.0) #, attributes = [1.3])
    1415         d = Vertex (40.0,0.0) #, attributes = [1.4])
    1416    
    1417         s1 = Segment(a,b)
    1418         s2 = Segment(b,c)
    1419         s3 = Segment(c,d)
    1420         s4 = Segment(d,a)
    1421      
    1422         m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4])
    1423         tag = "DSG"       
    1424         initial_tag = "PIG"
    1425         Segment.set_default_tag(initial_tag)
    1426         m.import_ungenerate_file(fileName, tag=tag, region_tag=["swamp","coastalp"])
    1427 
    1428         os.remove(fileName)
    1429 
    1430         self.failUnless(Segment.get_default_tag() == initial_tag,
    1431                         'Wrong segment list length.')
    1432         m.export_mesh_file("swamp.tsh")
    1433         #print "m.userSegments",m.userSegments
    1434         self.failUnless(len(m.userSegments) ==11,
    1435                         'Wrong segment list length.')
    1436         self.failUnless(len(m.userVertices) == 11,
    1437                         'Wrong vertex list length.')
    1438         self.failUnless(len(m.regions) == 2,
    1439                         'Wrong regions list length.')
    1440         self.failUnless(m.regions[0].getTag() == "swamp",
    1441                         'Wrong regions tag.')
    1442         self.failUnless(m.regions[1].getTag() == "coastalp",
    1443                         'Wrong regions 1 tag.')
    1444        
    1445        
    1446         # have to reset this , since it's a class attribute
    1447         Segment.set_default_tag("")
    1448        
    14491196    def test_addVertsSegs(self):
    14501197        m = Mesh()
Note: See TracChangeset for help on using the changeset viewer.