1
2 """Manipulate a C{blockMeshDict}"""
3
4 import re,os
5
6 from PyFoam.Basics.LineReader import LineReader
7 from .FileBasis import FileBasisBackup
8
9 from math import ceil
10
11 from PyFoam.Error import error
12
14 """Represents a C{blockMeshDict}-file"""
15
17 """@param name: The name of the parameter file
18 @param backup: create a backup-copy of the file"""
19
20 FileBasisBackup.__init__(self,name,backup=backup)
21
23 """Get a dictionary with the 3 components of each vertex as keys
24 and the 'raw' line as the value"""
25 try:
26 from collections import OrderedDict
27 result=OrderedDict()
28 except ImportError:
29 error("This python-version doesn't have OrderedDict in library collections. Can't go on''")
30
31 startPattern=re.compile("^\s*vertices")
32 endPattern=re.compile("^\s*\);")
33 vertexPattern=re.compile("^\s*\(\s*(\S+)\s+(\S+)\s+(\S+)\s*\).*$")
34
35 inVertex=False
36 l=self.__startProcess()
37
38 cnt=0
39 while l.read(self.fh):
40 if not inVertex:
41 if startPattern.match(l.line):
42 inVertex=True
43 elif endPattern.match(l.line):
44 inVertex=False
45 else:
46 m=vertexPattern.match(l.line)
47 if m!=None:
48 result[m.groups()]=(cnt,l.line)
49 cnt+=1
50
51 return result
52
54 """Merge in the vertexes from another mesh after our own vertexes"""
55
56 otherVert=BlockMesh(other)._getVertexes()
57
58 startPattern=re.compile("^\s*vertices")
59 endPattern=re.compile("^\s*\);")
60 vertexPattern=re.compile("^\s*\(\s*(\S+)\s+(\S+)\s+(\S+)\s*\).*$")
61
62 inVertex=False
63 newMesh=""
64 l=self.__startProcess()
65
66 while l.read(self.fh):
67 toPrint=l.line
68 if not inVertex:
69 if startPattern.match(l.line):
70 inVertex=True
71 elif endPattern.match(l.line):
72 inVertex=False
73 tmp=toPrint
74 toPrint=""
75 for k in otherVert:
76 toPrint+=otherVert[k][1]+"\n"
77 toPrint+=tmp
78 else:
79 m=vertexPattern.match(l.line)
80 if m!=None:
81 if m.groups() in otherVert:
82 del otherVert[m.groups()]
83 newMesh+=toPrint+"\n"
84
85 return self.__endProcess(newMesh)
86
88 """Renumber the vertices in the current mesh according to another
89 mesh"""
90
91 otherVert=BlockMesh(other)._getVertexes()
92
93 startPattern=re.compile("^\s*vertices")
94 endPattern=re.compile("^\s*\);")
95 vertexPattern=re.compile("^\s*\(\s*(\S+)\s+(\S+)\s+(\S+)\s*\).*$")
96 patchPattern=re.compile("^(\s*\(\s*)(\S+)\s+(\S+)\s+(\S+)\s+(\S+)(\s*\).*)$")
97 blockPattern=re.compile("^(\s*hex\s*\(\s*)(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)(\s*\).+)$")
98
99 inVertex=False
100 newMesh=""
101 l=self.__startProcess()
102
103 replaceVert=""
104 for k in otherVert:
105 replaceVert+=otherVert[k][1]+"\n"
106
107 cnt=0
108 replace={}
109
110 def transformNr(orig):
111 return " ".join(
112 [str(replace[int(f)]) for f in orig]
113 )
114
115 while l.read(self.fh):
116 toPrint=l.line+"\n"
117 if not inVertex:
118 if startPattern.match(l.line):
119 inVertex=True
120 elif patchPattern.match(l.line):
121 g=patchPattern.match(l.line).groups()
122 toPrint=g[0]+transformNr(g[1:-1])+g[-1]+"\n"
123 elif blockPattern.match(l.line):
124 g=blockPattern.match(l.line).groups()
125 toPrint=g[0]+transformNr(g[1:-1])+g[-1]+"\n"
126 elif endPattern.match(l.line):
127 inVertex=False
128 toPrint="(\n"+replaceVert+toPrint
129 else:
130 toPrint=""
131 m=vertexPattern.match(l.line)
132 if m!=None:
133 if m.groups() in otherVert:
134 replace[cnt]=otherVert[m.groups()][0]
135 cnt+=1
136 else:
137 error("Vertex",m.groups(),"not found in other mesh")
138
139 newMesh+=toPrint
140
141 return self.__endProcess(newMesh)
142
144 """Rotate patches so that they start with the lowest number vertex"""
145
146 patchPattern=re.compile("^(\s*\(\s*)(\S+)\s+(\S+)\s+(\S+)\s+(\S+)(\s*\).*)$")
147
148 newMesh=""
149 l=self.__startProcess()
150
151 def rotate(orig):
152 tmp=[int(e) for e in orig]
153 ind=tmp.index(min(tmp))
154 return " ".join(
155 [str(e) for e in tmp[ind:]+tmp[:ind]]
156 )
157
158 while l.read(self.fh):
159 toPrint=l.line+"\n"
160 if patchPattern.match(l.line):
161 g=patchPattern.match(l.line).groups()
162 toPrint=g[0]+rotate(g[1:-1])+g[-1]+"\n"
163
164 newMesh+=toPrint
165
166 return self.__endProcess(newMesh)
167
169 """Remove comments after vertices"""
170
171 startPattern=re.compile("^\s*vertices")
172 endPattern=re.compile("^\s*\);")
173 vertexPattern=re.compile("^(\s*\(\s*\S+\s+\S+\s+\S+\s*\)).*$")
174
175 inVertex=False
176 newMesh=""
177 l=self.__startProcess()
178
179 while l.read(self.fh):
180 toPrint=l.line
181 if not inVertex:
182 if startPattern.match(l.line):
183 inVertex=True
184 elif endPattern.match(l.line):
185 inVertex=False
186 else:
187 m=vertexPattern.match(l.line)
188 if m!=None:
189 toPrint=m.group(1)
190 newMesh+=toPrint+"\n"
191
192 return self.__endProcess(newMesh)
193
195 """Add comments with the number of the vertex after them
196 @param prefix: a string to add before the number"""
197
198 startPattern=re.compile("^\s*vertices")
199 endPattern=re.compile("^\s*\);")
200 vertexPattern=re.compile("^\s*\(\s*\S+\s+\S+\s+\S+\s*\).*$")
201
202 inVertex=False
203 newMesh=""
204 l=self.__startProcess()
205
206 cnt=0
207 while l.read(self.fh):
208 toPrint=l.line
209 if not inVertex:
210 if startPattern.match(l.line):
211 inVertex=True
212 elif endPattern.match(l.line):
213 inVertex=False
214 else:
215 m=vertexPattern.match(l.line)
216 if m!=None:
217 toPrint+=" \t // "+prefix+" "+str(cnt)
218 cnt+=1
219 newMesh+=toPrint+"\n"
220
221 return self.__endProcess(newMesh)
222
227
228 - def refineMesh(self,factors,offset=(0,0,0),getContent=False,addOld=True):
229 """Refine the Mesh by multiplying the number of cells in the blocks
230 @param factors: either a scalar to scale in all directions or a
231 tuple with the value for each direction
232 @param offset: an optional tuple for an additionnal offset value
233 for each direction
234 @param getContent: Return the contents instead of writing a fil. Main purpose
235 of this parameter is not to break compatibility with old versions"""
236
237 if type(factors)!=tuple:
238 f=(factors,factors,factors)
239 else:
240 f=factors
241
242 startPattern=re.compile("^\s*blocks")
243 endPattern=re.compile("^\s*\);")
244 hexPattern=re.compile("^(\s*hex\s*\(.+\)\s+\(\s*)(\d+)\s+(\d+)\s+(\d+)(\s*\).*)$")
245
246 inBlock=False
247 l=self.__startProcess()
248 newMesh=""
249
250 while l.read(self.fh):
251 toPrint=l.line
252
253 if not inBlock:
254 if startPattern.match(l.line):
255 inBlock=True
256 else:
257 if endPattern.match(l.line):
258 inBlock=False
259 else:
260 m=hexPattern.match(l.line)
261 if m!=None:
262 g=m.groups()
263 if addOld:
264 toPrint =self.removedString+l.line+"\n"
265 else:
266 toPrint=""
267
268 toPrint+="%s%d %d %d%s" % (
269 g[0],
270 ceil(int(g[1])*f[0]+offset[0]),
271 ceil(int(g[2])*f[1]+offset[1]),
272 ceil(int(g[3])*f[2]+offset[2]),
273 g[4])
274 if addOld:
275 toPrint+=" "+self.addedString
276
277 newMesh+=toPrint+"\n"
278
279 return self.__endProcess(newMesh,getContent)
280
292
293
294