Package PyFoam :: Package Applications :: Module ChangeBoundaryName
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Applications.ChangeBoundaryName

 1  """ 
 2  Application class that implements pyFoamChangeBoundaryName.py 
 3   
 4  Author: 
 5    Martin Beaudoin, Hydro-Quebec, 2010. 
 6   
 7  """ 
 8   
 9  from .PyFoamApplication import PyFoamApplication 
10   
11  from os import path 
12  from optparse import OptionGroup 
13   
14  from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile 
15  from PyFoam.RunDictionary.TimeDirectory import TimeDirectory 
16   
17  from PyFoam.ThirdParty.six import print_ 
18   
19 -class ChangeBoundaryName(PyFoamApplication):
20 - def __init__(self, 21 args=None, 22 **kwargs):
23 description="""\ 24 Changes the name of a boundary in the boundary-file. Also if a 25 time-step is specified 26 """ 27 PyFoamApplication.__init__(self,args=args, 28 description=description, 29 usage="%prog <caseDirectory> <boundaryName> <new name>", 30 changeVersion=False, 31 nr=3, 32 interspersed=True, 33 **kwargs)
34
35 - def addOptions(self):
36 change=OptionGroup(self.parser, 37 "Change", 38 "Change specific options") 39 self.parser.add_option_group(change) 40 change.add_option("--test", 41 action="store_true", 42 default=False, 43 dest="test", 44 help="Only print the new boundary file") 45 change.add_option("--time-step", 46 action="store", 47 default=None, 48 dest="timestep", 49 help="If specified all the field-files in that directory are updated")
50
51 - def run(self):
52 fName=self.parser.getArgs()[0] 53 bName=self.parser.getArgs()[1] 54 nName=self.parser.getArgs()[2] 55 56 boundary=ParsedParameterFile(path.join(".",fName,"constant","polyMesh","boundary"),debug=False,boundaryDict=True) 57 58 bnd=boundary.content 59 60 if type(bnd)!=list: 61 self.error("Problem with boundary file (not a list)") 62 63 found=False 64 65 for val in bnd: 66 if val==bName: 67 found=True 68 elif found: 69 bnd[bnd.index(bName)]=nName 70 break 71 72 if not found: 73 self.error("Boundary",bName,"not found in",bnd[::2]) 74 75 if self.opts.test: 76 print_(boundary) 77 else: 78 boundary.writeFile() 79 self.addToCaseLog(fName) 80 81 if self.opts.timestep: 82 print_("Updating the files of timestep",self.opts.timestep) 83 td=TimeDirectory(fName,self.opts.timestep, 84 yieldParsedFiles=True) 85 86 for f in td: 87 print_("Updating",f.name) 88 f["boundaryField"][nName]=f["boundaryField"][bName] 89 del f["boundaryField"][bName] 90 f.writeFile()
91 92 # Should work with Python3 and Python2 93