-
Notifications
You must be signed in to change notification settings - Fork 18
/
bin2vtu.py
229 lines (191 loc) · 6.31 KB
/
bin2vtu.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
#!/usr/bin/python
# ux*iHat+uy*jHat+uz*kHat
import sys
import struct as st #
#cvw = custom vtu writer
import makevtu as cvw
# Import subprocess library, so system calls can be made
# in this specific case we want to use it to delete old .vtu files
# in case partition size changes etc
import subprocess
import binascii
#"Global constants":
FIN = "output_00000.dat" #Std. input file format
FOUT = "output" #Std. output file format
def main(itr):
print("iter: " + str(itr))
# Delete all vtu files of same name
subprocess.call("rm " + FOUT + "*.vtu", shell=True);
# Try to open the file
try:
fin = open(FIN,'rb')
except:
exit("Could not open file.. exiting")
# The file always starts with a user defined string
# Here it is discarded, but if you have some information in it,
# it can be saved.
readInString(fin)
print("Reading in mesh information")
#Load information from header
nDom,nPointsT,nCellsT,nPFields,nCFields,nodesPerElement=readHeader(fin)
# The cell and point field names:
pointFieldNames = readInString(fin)
cellFieldNames = readInString(fin)
# Convert to tuples with, assume names are comma separated. Also strip for whitespaces/trailing characters
pointFieldNames = [x.strip() for x in pointFieldNames.split(',')]
cellFieldNames = [x.strip() for x in cellFieldNames.split(',')]
print("Read/write in mesh data: nodes")
# Open output stream
try:
fout = open(FOUT+ "_" + str(itr).zfill(5) + ".vtu",'wb')
except:
exit("Cannot create output file... exiting")
# Node list - simple take and put
rawP = ""
for i in range(nDom):
#Multiply by 3 because we have 3D
#Multiply by 4 == length of float32
rawP += fin.read(3*4*nPointsT[i])
cvw.writeHeader(fout,sum(nPointsT),sum(nCellsT))
cvw.writeRawPoints(fout,rawP)
rawP = None # delete rawP from memory
print("Read/write in mesh data: element connectivity")
#Cell data needs to be processed and cannot be read in raw
rawP = ""
for i in range(nDom):
#Multiply by 8 because we have 8 nodes per element
#Multiply by 8 == length of unsigned long int
rawP += fin.read(4*8*nCellsT[i]) # Note: for 2D use "4*8", 3D use "8*8" because 2D has 4 nodes.
cvw.writeRawCellsConn(fout,rawP)
#print st.unpack('Q'*128*8,rawP[0:8*128*8])
rawP = ""
for i in range(nDom):
#Multiply by 8 == length of unsigned long int
rawP += fin.read(8*nCellsT[i])
cvw.writeRawCellsOffset(fout,rawP)
# Convert from binary to e.g. floats and return in a tuple:
#print st.unpack('Q'*128,rawP[0:8*128])
rawP = ""
for i in range(nDom):
#Multiply by 8 == length of unsigned long int
rawP += fin.read(8*nCellsT[i])
cvw.writeRawCellsType(fout,rawP)
#print st.unpack('Q'*128,rawP[0:8*128])
rawP=None
print("Done writing in mesh")
#Write out a vtu file for each time step
dataset = 0
foundRequestedDataset = False
while(1):
try:
iteration = readdata(fin,'Q')
iteration = iteration[0]
print("Optimization iter. " + str(iteration) + " = dataset " + str(dataset) + ", you requested dataset " + str(itr))
except:
break #break loop
if int(dataset)==int(itr):
foundRequestedDataset = True
print("Processing dataset " + str(dataset))
lPFieldNames = []
lCFieldNames = []
lrawPFields = []
lrawCFields = []
for j in range(nPFields[i]):
lrawPFields.append("")
for j in range(nCFields[i]):
lrawCFields.append("")
for i in range(nDom):
for j in range(nPFields[i]):
if(i==0):
try:
lPFieldNames.append(pointFieldNames[j])
except:
lPFieldNames.append("Point Field " + str(j))
#Multiply by 4 == length of float32
lrawPFields[j] += fin.read(4*nPointsT[i])
for j in range(nCFields[i]):
if(i==0):
try:
lCFieldNames.append(cellFieldNames[j])
except:
lCFieldNames.append("Cell Field " + str(j))
#Multiply by 4 == length of float32
lrawCFields[j] += fin.read(4*nCellsT[i])
cvw.writeRawScalarPointData(fout,lrawPFields,lPFieldNames)
cvw.writeRawScalarCellData(fout,lrawCFields,lCFieldNames)
cvw.writeFooter(fout)
fout.close()
else:
tmp1 = 0
for i in range(nDom):
for j in range(nPFields[i]):
#fin.read(4*nPointsT[i])
tmp1 += 4*nPointsT[i]
for j in range(nCFields[i]):
#fin.read(4*nCellsT[i])
tmp1 += 4*nCellsT[i]
fin.seek(tmp1,1)
dataset += 1
fin.close()
if foundRequestedDataset:
print("Done")
else:
print("!! The requested dataset was NOT found!! ")
subprocess.call("rm " + FOUT + "*.vtu", shell=True);
def getNoNodes(i):
if(i==10):
return 4
if(i==12):
return 8
if(i==1000):
return 8
exit("Sorry, but the element type " + str(i) + " is not defined. You may add it to getNoNodes() yourself... exiting")
#If the input file has specified a custom cell number format they can be added here
#and in the getNoNodes()
def convertToVtkCell(i):
if(i==1000):
return 12
return i
def readdata(fin,inpformat):
# fin = file input
# inpformat = type of character/string/number to read - see python manual.
# sequence of datatypes
# How many bytes should we read when format is inpformat:
bytecount = st.calcsize(inpformat)
# Read the bytes into tmp
tmp = fin.read(bytecount)
# Convert from binary to e.g. floats and return in a tuple:
return st.unpack(inpformat,tmp)
def readHeader(fin):
#Should be called right after fin.open()
try:
nDom = readdata(fin,'Q')[0]
tmp = readdata(fin,'Q'*nDom*4)
nPointsT = list(tmp[0:nDom])
nCellsT = list(tmp[nDom:2*nDom])
nPFields = list(tmp[2*nDom:3*nDom])
nCFields = list(tmp[3*nDom:4*nDom])
nodesPerElement = readdata(fin,'Q')[0]
except:
exit("Could not read header format... exiting")
return nDom,nPointsT,nCellsT,nPFields,nCFields,nodesPerElement
def readInString(fin):
# Reads in a string until an end line symbol is detected
string = ''
while(1):
try:
tmp = readdata(fin,'c')[0] # The c means data of character type (length 1)
string += tmp
except:
exit("File ended while scanning for string. String not present or properly terminated?... exiting")
# Break if end character detected
if(tmp == '\x01'):
string = string[0:-1]; # Dont want to save last character
break
return string
# Make sure main is only called when the file is executed
if __name__ == "__main__":
itr = 0
if len(sys.argv) > 1:
itr = sys.argv[1]
main(itr)