Package PyFoam :: Package RunDictionary :: Module ParsedBlockMeshDict
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.RunDictionary.ParsedBlockMeshDict

 1  #  ICE Revision: $Id$ 
 2  """A parsed blockMeshDict""" 
 3   
 4  from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile 
 5   
6 -class ParsedBlockMeshDict(ParsedParameterFile):
7 """ A parsed version of a blockMeshDict-file. Adds some 8 convenience-methods to access parts of the file""" 9
10 - def __init__(self, 11 name, 12 backup=False, 13 debug=False, 14 doMacroExpansion=False):
15 ParsedParameterFile.__init__(self, 16 name, 17 backup=backup, 18 debug=debug, 19 longListOutputThreshold=None, 20 doMacroExpansion=doMacroExpansion)
21
22 - def convertToMeters(self):
23 try: 24 return float(self["convertToMeters"]) 25 except KeyError: 26 return 1
27
28 - def vertices(self):
29 factor=self.convertToMeters() 30 return [[float(y)*factor for y in x] for x in self["vertices"]]
31
32 - def blocks(self):
33 """Returns a list with the 8 vertices that define each block""" 34 result=[] 35 i=1 36 while i<len(self["blocks"]): 37 result.append(list(map(int,self["blocks"][i]))) 38 if type(self["blocks"][i+1])==str: 39 i+=6 40 else: 41 i+=5 42 43 return result
44
45 - def patches(self):
46 """Returns a dictionary with lists of 4-tuples that define each patch""" 47 result={} 48 if "boundary" in self: 49 # New format in 2.0 50 for k,d in zip(self["boundary"][0::2],self["boundary"][1::2]): 51 result[k]=[list(map(int,x)) for x in d["faces"]] 52 else: 53 for i in range(1,len(self["patches"]),3): 54 result[self["patches"][i]]=[list(map(int,x)) for x in self["patches"][i+1]] 55 56 return result
57
58 - def arcs(self):
59 factor=self.convertToMeters() 60 result=[] 61 try: 62 for i in range(len(self["edges"])): 63 if str(self["edges"][i])=='arc': 64 result.append((int(self["edges"][i+1]), 65 [float(y)*factor for y in self["edges"][i+3]], 66 int(self["edges"][i+2]))) 67 except KeyError: 68 pass 69 70 return result
71
72 - def getBounds(self):
73 v=self.vertices() 74 mi=[ 1e10, 1e10, 1e10] 75 ma=[-1e10,-1e10,-1e10] 76 for p in v: 77 for i in range(3): 78 mi[i]=min(p[i],mi[i]) 79 ma[i]=max(p[i],ma[i]) 80 return mi,ma
81
82 - def typicalLength(self):
83 mi,ma=self.getBounds() 84 85 biggest=max(ma[0]-mi[0],ma[1]-mi[1],ma[2]-mi[2]) 86 smallest=min(ma[0]-mi[0],ma[1]-mi[1],ma[2]-mi[2]) 87 88 # return 2*biggest*smallest/(biggest+smallest) 89 return (biggest+smallest)/2
90 91 # Should work with Python3 and Python2 92