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