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

Source Code for Module PyFoam.Applications.ChangeBoundaryType

  1  #  ICE Revision: $Id$ 
  2  """ 
  3  Application class that implements pyFoamChangeBoundaryType.py 
  4  """ 
  5   
  6  from .PyFoamApplication import PyFoamApplication 
  7   
  8  from os import path 
  9  from optparse import OptionGroup 
 10   
 11  from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile 
 12   
 13  from PyFoam.ThirdParty.six import print_,string_types 
 14   
15 -class ChangeBoundaryType(PyFoamApplication):
16 - def __init__(self, 17 args=None, 18 **kwargs):
19 description="""\ 20 Changes the type of a boundary in the boundary-file 21 """ 22 PyFoamApplication.__init__(self,args=args, 23 description=description, 24 usage="%prog <caseDirectory> <boundaryName> <new type>", 25 changeVersion=False, 26 nr=3, 27 interspersed=True, 28 **kwargs)
29
30 - def addOptions(self):
31 change=OptionGroup(self.parser, 32 "Change", 33 "Change specific options") 34 self.parser.add_option_group(change) 35 36 change.add_option("--test", 37 action="store_true", 38 default=False, 39 dest="test", 40 help="Only print the new boundary file") 41 42 change.add_option("--region", 43 action="store", 44 default="", 45 dest="region", 46 help="Region to use. If unset the default mesh is used") 47 48 change.add_option("--time-directory", 49 action="store", 50 default="constant", 51 dest="time", 52 help="Time to use. If unset the mesh in 'constant'' is used") 53 54 change.add_option("--additional-values", 55 action="store", 56 default=None, 57 dest="additionalValues", 58 help="Dictionary in Python-format with additional values to add to the boundary")
59
60 - def run(self):
61 fName=self.parser.getArgs()[0] 62 bName=self.parser.getArgs()[1] 63 tName=self.parser.getArgs()[2] 64 65 boundaryPath=path.join(".",fName,self.opts.time,self.opts.region,"polyMesh","boundary") 66 try: 67 boundary=ParsedParameterFile(boundaryPath,debug=False,boundaryDict=True) 68 except IOError: 69 self.error("Problem opening boundary file",boundaryPath) 70 71 bnd=boundary.content 72 73 if type(bnd)!=list: 74 self.error("Problem with boundary file (not a list)") 75 76 found=False 77 78 for val in bnd: 79 if val==bName: 80 found=True 81 elif found: 82 val["type"]=tName 83 if self.opts.additionalValues: 84 vals=self.opts.additionalValues 85 if isinstance(vals,string_types): 86 # we're called from the command line. Convert string to usable format 87 vals=eval(vals) 88 for k in vals: 89 val[k]=vals[k] 90 break 91 92 if not found: 93 self.error("Boundary",bName,"not found in",bnd[::2]) 94 95 if self.opts.test: 96 print_(boundary) 97 else: 98 boundary.writeFile() 99 self.addToCaseLog(fName)
100 101 # Should work with Python3 and Python2 102