forked from flowkersma/swiftBlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockMeshBodyFit.py
237 lines (211 loc) · 9.17 KB
/
blockMeshBodyFit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import tempfile
import os
import numpy as np
import subprocess
import shutil
import itertools
import glob
from . import utils
class PreviewMesh():
def __init__(self, folder=None):
if shutil.which('blockMeshBodyFit'):
self.blockMeshbin = 'blockMeshBodyFit'
elif shutil.which('blockMeshBoyFit'):
self.blockMeshbin = 'blockMeshBoyFit'
else:
raise RuntimeError('ERROR: No BlockMeshBodyFit Found!')
if folder:
if not os.path.isdir(folder):
os.mkdir(folder)
if not os.path.isdir(folder+'/constant'):
os.mkdir(folder+'/constant')
if not os.path.isdir(folder+'/constant/triSurface'):
os.mkdir(folder+'/constant/triSurface')
if not os.path.isdir(folder+'/system'):
os.mkdir(folder+'/system')
self.blockMeshDictPath = folder+ '/system/blockMeshDict'
self.triSurfacePath = folder+'/constant/triSurface'
else:
self.tempdir = tempfile.mkdtemp()
self.blockMeshDictPath = self.tempdir+"/constant/polyMesh/blockMeshDict"
os.mkdir(self.tempdir+'/constant')
os.mkdir(self.tempdir+'/constant/polyMesh')
self.triSurfacePath = self.tempdir+'/constant/triSurface'
os.mkdir(self.triSurfacePath)
os.mkdir(self.tempdir+'/system')
os.mkdir(self.tempdir+'/0')
cd = open(self.tempdir+'/system/controlDict','w')
cd.write(self.header())
print('OpenFOAM temp directory: {}'.format(self.tempdir))
def writeBlockMeshDict(self, verts, convertToMeters, boundaries, polyLines, edgeInfo, blockNames, blocks, dependent_edges, projections, searchLength):
bmFile = open(self.blockMeshDictPath,'w')
bmFile.write(self.header())
bmFile.write("\nconvertToMeters " + str(convertToMeters) + ";\n")
bmFile.write("\nsearchLength {};\n\n\nvertices\n(\n".format(searchLength))
for v in verts:
bmFile.write(' ({} {} {})\n'.format(*v))
bmFile.write(");\nblocks\n(\n")
NoCells = 0
edge = lambda e0,e1: [min(e0,e1), max(e0,e1)]
for bid, (vl, blockName) in enumerate(zip(blocks, blockNames)):
edges = [(vl[e[0]],vl[e[1]]) for e in [(0,1),(3,2),(7,6),(4,5),(0,3),(1,2),(5,6),(4,7),(0,4),(1,5),(2,6),(3,7)]]
gradingStr = ""
for ei in edges:
e = edgeInfo[ei]
gradingStr+= '{:.6g} '.format(e["ratio"])
ires = edgeInfo[edges[0]]["N"]
jres = edgeInfo[edges[4]]["N"]
kres = edgeInfo[edges[8]]["N"]
NoCells += ires*jres*kres
bmFile.write('// block id {} \nhex ({} {} {} {} {} {} {} {}) '.format(bid,*vl) \
+ blockName + ' ({} {} {}) '.format(ires,jres,kres)\
+ 'edgeGrading (' + gradingStr + ')\n' )
snapFaces = dict()
for key,value in projections['face2surf'].items():
if value not in snapFaces:
snapFaces[value] = []
snapFaces[value].append(key)
bmFile.write(');\n\nsnapFaces\n{\n')
for key, value in snapFaces.items():
bmFile.write(' %s.stl\n {\n faces\n (\n'%key)
for v in value:
bmFile.write(' ({} {} {} {})\n'.format(*v))
bmFile.write(' );\n }\n')
bmFile.write('};\n\npatches\n(\n')
for b in boundaries:
bmFile.write(' {} {}\n (\n'.format(b['type'],b['name'] ))
for v in b['faceVerts']:
bmFile.write(' ({} {} {} {})\n'.format(*v))
bmFile.write(' )\n')
bmFile.write(');\n\nedges\n(\n')
for pl in polyLines:
bmFile.write(pl)
bmFile.write(');')
bmFile.close()
return NoCells
def readHeader(self,dicfile):
numberOfFields = 0
startLine = False
with open(dicfile) as fin:
for lidx,line in enumerate(fin):
if not numberOfFields:
try:
numberOfFields = int(line)
except ValueError:
pass
if '(' in line:
startLine = lidx + 1
break
return startLine, numberOfFields
def readBoundaries(self,files):
data = []
readingField = False
for line in files:
if not line.strip():
continue
if not readingField and line.strip() == '{':
readingField = True
elif not readingField:
temp = dict()
temp['name']= line.strip()
elif readingField and 'type' in line:
temp['type'] = line.strip().split()[1][:-1]
elif readingField and 'nFaces' in line:
temp['nFaces'] = int(line.strip().split()[1][:-1])
elif readingField and 'startFace' in line:
temp['startFace'] = int(line.strip().split()[1][:-1])
elif readingField and line.strip() == '}':
data.append(temp)
readingField = False
elif not readingField and line.strip() == ')':
break
return data
def getPoints(self,faces=None):
pointsFile = self.tempdir +'/constant/polyMesh/points'
startLine, numberofLines = self.readHeader(pointsFile)
convertfnc1 = lambda x: float(x[1:])
convertfnc2 = lambda x:float(x[:-1])
with open(pointsFile,'rb') as fin:
points = np.genfromtxt(itertools.islice(fin,startLine,startLine+numberofLines),\
converters={0:convertfnc1,2:convertfnc2},dtype=float)
if faces!=None:
pidxs = np.unique(np.ravel(faces))
points = points[pidxs]
points=points.tolist()
return points
def getFaces(self):
facesFile = self.tempdir +'/constant/polyMesh/faces'
startLine, numberofLines = self.readHeader(facesFile)
convertfnc1 = lambda x: int(x[2:])
convertfnc2 = lambda x: int(x[:-1])
with open(facesFile,'rb') as fin:
faces = np.genfromtxt(itertools.islice(fin,startLine,startLine+numberofLines),\
converters={0:convertfnc1,3:convertfnc2},dtype=int)
faces = faces.tolist()
return faces
def getBCFaces(self,internalCells):
faces = self.getFaces()
bcifaces = faces
bcfaces = faces
if not internalCells:
boundaryFile = self.tempdir + '/constant/polyMesh/boundary'
startLine, boundaries = self.readHeader(boundaryFile)
with open(boundaryFile) as fin:
fields = self.readBoundaries(itertools.islice(fin,startLine,None))
self.fields = fields
bcifaces = []
bcfaces=[]
for bc in sorted(fields, key=lambda k: k['startFace']):
bcfaces.extend(faces[bc['startFace']:bc['startFace']+bc['nFaces']])
bcifaces = np.array(bcfaces,dtype=int)
bcifaces = np.unique(bcifaces.ravel(),return_inverse=True)[1].reshape(bcifaces.shape)
bcifaces = bcifaces.astype(int).tolist()
return bcfaces,bcifaces
#this is faster
def getBCFaces2(self,internalCells):
facesFile = self.tempdir +'/constant/polyMesh/faces'
startLine, numberofLines = self.readHeader(facesFile)
faces = open(facesFile).readlines()
faces = faces[startLine:startLine+numberofLines]
subs = lambda s: list(map(int,s.__getitem__(slice(2,-2)).split()))
faces = list(map(subs, faces))
boundaryFile = self.tempdir + '/constant/polyMesh/boundary'
startLine, boundaries = self.readHeader(boundaryFile)
with open(boundaryFile) as fin:
fields = self.readBoundaries(itertools.islice(fin,startLine,None))
self.fields = fields
bcifaces = []
bcfaces=[]
for bc in sorted(fields, key=lambda k: k['startFace']):
bcfaces.extend(faces[bc['startFace']:bc['startFace']+bc['nFaces']])
bcifaces = np.array(bcfaces,dtype=int)
bcifaces = np.unique(bcifaces.ravel(),return_inverse=True)[1].reshape(bcifaces.shape)
bcifaces = bcifaces.astype(int).tolist()
return bcfaces,bcifaces
def runBlockMesh(self):
subprocess.call([self.blockMeshbin,'-case',self.tempdir],stdout=subprocess.PIPE)
def runMesh(self,runBlockMesh=True,internalCells=False):
print('running blockmesh')
if runBlockMesh:
self.runBlockMesh()
faces, bcifaces=self.getBCFaces2(internalCells)
points=self.getPoints(faces)
# shutil.rmtree(self.tempdir)
return points, bcifaces
def header(self):
return \
'''
/*--------------------------------*- C++ -*----------------------------------*/
// File was generated by SwiftBlock, a Blender 3D addon.
FoamFile
{
version 2.0;
format ascii;
class dictionary;
object blockMeshDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
deltaT 1;
writeInterval 1;
// ************************************************************************* //
'''