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