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

Source Code for Module PyFoam.Applications.ClearInternalField

 1  #  ICE Revision: $Id$ 
 2  """ 
 3  Application class that implements pyFoamClearInternalField.py 
 4  """ 
 5   
 6  from os import path 
 7   
 8  from .PyFoamApplication import PyFoamApplication 
 9   
10  from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile 
11   
12  from PyFoam.ThirdParty.six import print_ 
13   
14  import sys 
15   
16 -class ClearInternalField(PyFoamApplication):
17 - def __init__(self, 18 args=None, 19 **kwargs):
20 description="""\ 21 Takes a field-file and makes the whole internal field uniform. Either 22 taking the value from a patch or using a user-specified value 23 """ 24 25 PyFoamApplication.__init__(self, 26 args=args, 27 description=description, 28 usage="%prog [options] <fieldfile>", 29 changeVersion=False, 30 nr=1, 31 interspersed=True, 32 **kwargs)
33
34 - def addOptions(self):
35 self.parser.add_option("--patch", 36 action="store", 37 default=None, 38 dest="patch", 39 help="The name of the patch that should provide the value") 40 self.parser.add_option("--value", 41 action="store", 42 default=None, 43 dest="value", 44 help="The value that should be used for the internal field") 45 self.parser.add_option("--test", 46 action="store_true", 47 default=None, 48 dest="test", 49 help="Does not write the file but only prints it to the screen") 50 self.parser.add_option("--source-key", 51 action="store", 52 default="value", 53 dest="srckey", 54 help="The key that should be read from the source patch: %default")
55 56
57 - def run(self):
58 fName=self.parser.getArgs()[0] 59 60 if self.opts.patch==None and self.opts.value==None: 61 self.error("Either a patch or a value must be specified") 62 if self.opts.patch!=None and self.opts.value!=None: 63 self.error("Only a patch or a value can be specified") 64 65 try: 66 fieldFile=ParsedParameterFile(fName,backup=False) 67 except IOError: 68 e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e' 69 self.error("Problem with file",fName,":",e) 70 71 value="" 72 if self.opts.patch: 73 value=fieldFile["boundaryField"][self.opts.patch][self.opts.srckey] 74 else: 75 value="uniform "+self.opts.value 76 77 fieldFile["internalField"]=value 78 79 if self.opts.test: 80 print_(str(fieldFile)) 81 else: 82 fieldFile.writeFile() 83 self.addToCaseLog(path.dirname(path.dirname(path.abspath(fName))))
84 85 # Should work with Python3 and Python2 86