-
Notifications
You must be signed in to change notification settings - Fork 9
/
VTKToFoam.py
223 lines (194 loc) · 6.88 KB
/
VTKToFoam.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
import numpy
import vtk
import sys
import os
file_header = """
/*--------------------------------*- C++ -*----------------------------------*\\
| ========= | |
| \\\\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\\\ / O peration | Version: 2.1.1 |
| \\\\ / A nd | Web: www.OpenFOAM.org |
| \\\\/ M anipulation | |
\*---------------------------------------------------------------------------*/
"""
top_separator = """
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
"""
bottom_separator = """
// ************************************************************************* //
"""
def write_FoamFile(ver, fmt, cls, obj):
return """
FoamFile
{
version %.1f;
format %s;
class %s;
object %s;
}
""" % (ver, fmt, cls, obj)
def get_midpoint(cell):
np = cell.GetNumberOfPoints()
pts = cell.GetPoints()
midpoint = numpy.array([0.0,0.0,0.0])
for i in range(np):
midpoint += pts.GetPoint(i)
midpoint /= np
return midpoint
volume_file = sys.argv[1]
patch_file = sys.argv[2]
output_dir = sys.argv[3]
print "Volume : ", volume_file
print "Patches : ", patch_file
print "Output dir: ", output_dir
# load in volume
reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName(volume_file)
reader.Update()
volume = reader.GetOutput()
# load in patches
reader2 = vtk.vtkXMLMultiBlockDataReader()
reader2.SetFileName(patch_file)
reader2.Update()
surfaces = reader2.GetOutput()
boundary_mid_points = vtk.vtkPoints()
boundary_id_array = vtk.vtkIntArray()
iter = surfaces.NewIterator()
iter.UnRegister(None)
iter.InitTraversal()
boundary_id = 0
while not iter.IsDoneWithTraversal():
polyData = iter.GetCurrentDataObject()
if not polyData.IsA("vtkPolyData"):
print "ERROR: unexpected input (not vtkPolyData)"
exit(1)
nc = polyData.GetNumberOfCells()
print "boundary",boundary_id,":",nc
for i in range(nc):
cell = polyData.GetCell(i)
midpoint = get_midpoint(cell)
boundary_mid_points.InsertNextPoint(midpoint)
boundary_id_array.InsertNextValue(boundary_id)
boundary_id += 1
iter.GoToNextItem()
num_boundaries = boundary_id
loc = vtk.vtkKdTreePointLocator()
boundary_dataset = vtk.vtkPolyData()
boundary_dataset.SetPoints(boundary_mid_points)
loc.SetDataSet(boundary_dataset)
loc.BuildLocator()
# map from boundaries to a list of tuples containing point ids and the cell id
boundary_faces = []
for i in range(num_boundaries):
boundary_faces.append([])
internal_faces = []
volume.BuildLinks()
nc = volume.GetNumberOfCells()
for cell_id in range(nc):
if (cell_id % 10000) == 0:
print "10000 cells"
cell = volume.GetCell(cell_id)
nf = cell.GetNumberOfFaces()
cell_internal_faces = {}
for face_id in range(nf):
face = cell.GetFace(face_id)
neighbour_cell_ids = vtk.vtkIdList()
face_point_ids = face.GetPointIds()
volume.GetCellNeighbors(cell_id, face.GetPointIds(), neighbour_cell_ids)
nn = neighbour_cell_ids.GetNumberOfIds()
if nn == 0:
# boundary
face_midpoint = get_midpoint(face)
boundary_id = boundary_id_array.GetValue(loc.FindClosestPoint(face_midpoint))
boundary_faces[boundary_id].append((
[face.GetPointId(p) for p in range(face.GetNumberOfPoints())],
cell_id))
elif nn == 1:
# internal
neighbour_cell_id = neighbour_cell_ids.GetId(0)
if cell_id < neighbour_cell_id:
cell_internal_faces[neighbour_cell_id] = (
[face.GetPointId(p) for p in range(face.GetNumberOfPoints())],
cell_id,
neighbour_cell_id)
else:
print "ERROR: face associated with more than 2 cells"
exit(1)
ids = cell_internal_faces.keys()
ids.sort()
for f in ids:
internal_faces.append(cell_internal_faces[f])
for i in range(num_boundaries):
print "boundary", i, ":", len(boundary_faces[i])
# write files
points_file = open(os.path.join(output_dir, "points"), "w")
points_file.write(file_header)
points_file.write(write_FoamFile(2.0, "ascii", "vectorField", "points"))
points_file.write(top_separator)
np = volume.GetNumberOfPoints()
pts = volume.GetPoints()
points_file.write("%d\n(\n" % np)
for i in range(np):
points_file.write("(%f %f %f)\n" % pts.GetPoint(i))
points_file.write(")\n")
points_file.write(bottom_separator)
points_file.close()
def write_face(face_points):
return "%d(%s)\n" % (len(face_points), " ".join([str(p) for p in face_points]))
faces_file = open(os.path.join(output_dir, "faces"), "w")
faces_file.write(file_header)
faces_file.write(write_FoamFile(2.0, "ascii", "faceList", "faces"))
faces_file.write(top_separator)
total_faces = len(internal_faces)
for i in range(num_boundaries):
total_faces += len(boundary_faces[i])
faces_file.write("%d\n(\n" % total_faces)
for i in range(len(internal_faces)):
faces_file.write(write_face(internal_faces[i][0]))
for b in boundary_faces:
for j in range(len(b)):
faces_file.write(write_face(b[j][0]))
faces_file.write(")\n")
faces_file.write(bottom_separator)
faces_file.close()
neighbour_file = open(os.path.join(output_dir, "neighbour"), "w")
neighbour_file.write(file_header)
neighbour_file.write(write_FoamFile(2.0, "ascii", "labelList", "neighbour"))
neighbour_file.write(top_separator)
neighbour_file.write("%d\n(\n" % len(internal_faces))
for i in range(len(internal_faces)):
neighbour_file.write("%d\n" % internal_faces[i][2])
neighbour_file.write(")\n")
neighbour_file.write(bottom_separator)
neighbour_file.close()
owner_file = open(os.path.join(output_dir, "owner"), "w")
owner_file.write(file_header)
owner_file.write(write_FoamFile(2.0, "ascii", "labelList", "owner"))
owner_file.write(top_separator)
owner_file.write("%d\n(\n" % total_faces)
for i in range(len(internal_faces)):
owner_file.write("%d\n" % internal_faces[i][1])
for b in boundary_faces:
for j in range(len(b)):
owner_file.write("%d\n" % b[j][1])
owner_file.write(")\n")
owner_file.write(bottom_separator)
owner_file.close()
boundary_file = open(os.path.join(output_dir, "boundary"), "w")
boundary_file.write(file_header)
boundary_file.write(write_FoamFile(2.0, "ascii", "polyBoundaryMesh", "boundary"))
boundary_file.write(top_separator)
start_face = len(internal_faces)
boundary_file.write("%d\n(\n" % num_boundaries)
for i in range(num_boundaries):
boundary_file.write("""boundary_%d
{
type patch;
nFaces %d;
startFace %d;
}
""" % (i, len(boundary_faces[i]), start_face))
start_face += len(boundary_faces[i])
boundary_file.write(")\n")
boundary_file.write(bottom_separator)
boundary_file.close()