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