1
2 """
3 Application class that implements pyFoamClearBoundaryValue.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> <patchnames>",
28 changeVersion=False,
29 nr=2,
30 interspersed=True,
31 exactNr=False)
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