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

Source Code for Module PyFoam.RunDictionary.BlockMesh

 1  """Manipulate a C{blockMeshDict}""" 
 2   
 3  import re,os 
 4  from os import path 
 5   
 6  from PyFoam.Basics.LineReader import LineReader 
 7  from FileBasis import FileBasisBackup 
 8   
9 -class BlockMesh(FileBasisBackup):
10 """Represents a C{blockMeshDict}-file""" 11
12 - def __init__(self,name,backup=False):
13 """@param name: The name of the parameter file 14 @param backup: create a backup-copy of the file""" 15 16 FileBasisBackup.__init__(self,name,backup=backup)
17
18 - def refineMesh(self,factors):
19 """Refine the Mesh by multiplying the number of cells in the blocks 20 @param: either a scalar to scale in all directions or a 21 tuple with the value for each direction""" 22 23 if type(factors)!=tuple: 24 f=(factors,factors,factors) 25 else: 26 f=factors 27 28 startPattern=re.compile("^\s*blocks") 29 endPattern=re.compile("^\s\);") 30 hexPattern=re.compile("^(\s*hex\s*\(.+\)\s+\(\s*)(\d+)\s+(\d+)\s+(\d+)(\s*\).*)$") 31 32 inBlock=False 33 34 l=LineReader() 35 self.openFile() 36 37 (fh,fn)=self.makeTemp() 38 39 while l.read(self.fh): 40 toPrint=l.line 41 42 if not inBlock: 43 if startPattern.match(l.line): 44 inBlock=True 45 else: 46 if endPattern.match(l.line): 47 inBlock=False 48 else: 49 m=hexPattern.match(l.line) 50 if m!=None: 51 g=m.groups() 52 toPrint =self.removedString+l.line+"\n" 53 toPrint+="%s%d %d %d%s" % (g[0],int(g[1])*f[0],int(g[2])*f[1],int(g[3])*f[2],g[4]) 54 toPrint+=" "+self.addedString 55 56 fh.write(toPrint+"\n") 57 58 self.closeFile() 59 fh.close() 60 os.rename(fn,self.name)
61