1
2 """
3 Application class that implements pyFoamCreateBoundaryPatches.py
4 """
5
6 import re
7 from os import path
8
9 import sys
10
11 from .PyFoamApplication import PyFoamApplication
12
13 from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile
14 from PyFoam.RunDictionary.BoundaryDict import BoundaryDict
15 from PyFoam.RunDictionary.SolutionDirectory import SolutionDirectory
16
17 from PyFoam.ThirdParty.six import print_
18
21 description="""\
22 Takes a field-file. Looks up the polyMesh/boundary-file of the case
23 and adds the corresponding patches to the boundary field setting it to
24 zeroGradient for all patches and walls
25 """
26
27 PyFoamApplication.__init__(self,
28 args=args,
29 description=description,
30 usage="%prog [options] <fieldfile>",
31 changeVersion=False,
32 nr=1,
33 interspersed=True)
34
36 self.parser.add_option("--clear-unused",
37 action="store_true",
38 default=None,
39 dest="clear",
40 help="Removes all the boundaries that are not in the boundary-file")
41 self.parser.add_option("--no-check",
42 action="store_true",
43 default=None,
44 dest="nocheck",
45 help="Doesn't check whether the boundary tests are consistent")
46
47 self.parser.add_option("--test",
48 action="store_true",
49 default=None,
50 dest="test",
51 help="Does not write the file but only prints it to the screen")
52
53 self.parser.add_option("--verbose",
54 action="store_true",
55 default=None,
56 dest="verbose",
57 help="Writes to the screen what is being modified")
58
59 self.parser.add_option("--default",
60 action="store",
61 default="{'type':'zeroGradient'}",
62 dest="default",
63 help="The default value for newly created patches as a Python-dictionary (instead of '{ \"type\" : \"zeroGradient\" }')")
64
65 self.parser.add_option("--filter",
66 action="store",
67 default=None,
68 dest="filter",
69 help="A regular expression by which patch names are filtered before they are set")
70
71 self.parser.add_option("--overwrite",
72 action="store_true",
73 default=False,
74 dest="overwrite",
75 help="Overwrites existing boundary conditions")
76
77 self.parser.add_option("--fix-types",
78 action="store_true",
79 default=False,
80 dest="fixtypes",
81 help="Fix inconsistencies")
82
84 fName=self.parser.getArgs()[0]
85
86 try:
87 dictFile=ParsedParameterFile(fName,backup=False)
88 except IOError:
89 e = sys.exc_info()[1]
90 self.error("Problem with file",fName,":",e)
91
92 fName=path.abspath(fName)
93 case=path.dirname(path.dirname(fName))
94 region=None
95
96 if not SolutionDirectory(case,archive=None,paraviewLink=False).isValid():
97
98 case=path.dirname(case)
99 region=path.basename(path.dirname(fName))
100 print_(case,region)
101 if region not in SolutionDirectory(case,archive=None,paraviewLink=False).getRegions():
102 self.error(region,"is not a valid region in the case",case)
103
104 if self.opts.filter==None:
105 flter=re.compile(".+")
106 else:
107 flter=re.compile(self.opts.filter)
108
109 boundaries=dictFile["boundaryField"]
110
111 try:
112 bFile=BoundaryDict(case,region=region)
113 except IOError:
114 e = sys.exc_info()[1]
115 self.error("Problem reading the boundary file:",e)
116
117 if self.opts.clear:
118 for b in list(boundaries.keys()):
119 if b not in bFile.patches():
120 if self.opts.verbose:
121 print_("Deleting patch",b)
122 del boundaries[b]
123
124 if not self.opts.nocheck:
125 for p in bFile.patches():
126 if p in boundaries:
127 typ=boundaries[p]["type"]
128 pTyp=bFile[p]["type"]
129 if pTyp!="patch" and pTyp!="wall" and pTyp!=typ:
130 if self.opts.fixtypes:
131 if self.opts.verbose:
132 print_("Fixing wall/patch patch",p)
133 del boundaries[p]
134 continue
135 else:
136 self.error("Inconsistent type for ",p,": Is",typ,"but should be",pTyp)
137 if typ in ["symmetryPlane","empty","wedge","cyclic","processor"] and pTyp!=typ:
138 if self.opts.fixtypes:
139 if self.opts.verbose:
140 print_("Fixing special patch",p)
141 del boundaries[p]
142 continue
143 else:
144 self.error("Inconsistent type for ",p,": Is",typ,"but should be some kind of patch type")
145
146 for p in bFile.patches():
147 if (not p in boundaries or self.opts.overwrite) and flter.match(p):
148 pTyp=bFile[p]["type"]
149 if pTyp!="patch" and pTyp!="wall":
150 tmp={"type":pTyp}
151 else:
152 tmp=eval(self.opts.default)
153 if self.opts.verbose:
154 print_("Writing",tmp,"to patch",p)
155 boundaries[p]=tmp;
156
157 if self.opts.test:
158 print_(str(dictFile))
159 else:
160 dictFile.writeFile()
161 self.addToCaseLog(case)
162
163
164