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 bFile=BoundaryDict(case,region=region)
107
108 if self.opts.clear:
109 for b in boundaries.keys():
110 if b not in bFile.patches():
111 if self.opts.verbose:
112 print "Deleting patch",b
113 del boundaries[b]
114
115 if not self.opts.nocheck:
116 for p in bFile.patches():
117 if boundaries.has_key(p):
118 typ=boundaries[p]["type"]
119 pTyp=bFile[p]["type"]
120 if pTyp!="patch" and pTyp!="wall" and pTyp!=typ:
121 if self.opts.fixtypes:
122 if self.opts.verbose:
123 print "Fixing wall/patch patch",p
124 del boundaries[p]
125 continue
126 else:
127 self.error("Inconsistent type for ",p,": Is",typ,"but should be",pTyp)
128 if typ in ["symmetryPlane","empty","wedge","cyclic","processor"] and pTyp!=typ:
129 if self.opts.fixtypes:
130 if self.opts.verbose:
131 print "Fixing special patch",p
132 del boundaries[p]
133 continue
134 else:
135 self.error("Inconsistent type for ",p,": Is",typ,"but should be some kind of patch type")
136
137 for p in bFile.patches():
138 if (not boundaries.has_key(p) or self.opts.overwrite) and flter.match(p):
139 pTyp=bFile[p]["type"]
140 if pTyp!="patch" and pTyp!="wall":
141 tmp={"type":pTyp}
142 else:
143 tmp=eval(self.opts.default)
144 if self.opts.verbose:
145 print "Writing",tmp,"to patch",p
146 boundaries[p]=tmp;
147
148 if self.opts.test:
149 print str(dictFile)
150 else:
151 dictFile.writeFile()
152 self.addToCaseLog(case)
153