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