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

Source Code for Module PyFoam.RunDictionary.BlockMesh

 1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/RunDictionary/BlockMesh.py 4667 2009-02-28T22:46:13.716267Z bgschaid  $  
 2  """Manipulate a C{blockMeshDict}""" 
 3   
 4  import re,os 
 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,offset=(0,0,0)):
19 """Refine the Mesh by multiplying the number of cells in the blocks 20 @param factors: either a scalar to scale in all directions or a 21 tuple with the value for each direction 22 @param offset: an optional tuple for an additionnal offset value 23 for each direction""" 24 25 if type(factors)!=tuple: 26 f=(factors,factors,factors) 27 else: 28 f=factors 29 30 startPattern=re.compile("^\s*blocks") 31 endPattern=re.compile("^\s\);") 32 hexPattern=re.compile("^(\s*hex\s*\(.+\)\s+\(\s*)(\d+)\s+(\d+)\s+(\d+)(\s*\).*)$") 33 34 inBlock=False 35 36 l=LineReader() 37 self.openFile() 38 39 (fh,fn)=self.makeTemp() 40 41 while l.read(self.fh): 42 toPrint=l.line 43 44 if not inBlock: 45 if startPattern.match(l.line): 46 inBlock=True 47 else: 48 if endPattern.match(l.line): 49 inBlock=False 50 else: 51 m=hexPattern.match(l.line) 52 if m!=None: 53 g=m.groups() 54 toPrint =self.removedString+l.line+"\n" 55 toPrint+="%s%d %d %d%s" % ( 56 g[0], 57 int(g[1])*f[0]+offset[0], 58 int(g[2])*f[1]+offset[1], 59 int(g[3])*f[2]+offset[2], 60 g[4]) 61 toPrint+=" "+self.addedString 62 63 fh.write(toPrint+"\n") 64 65 self.closeFile() 66 fh.close() 67 os.rename(fn,self.name)
68