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