-
Notifications
You must be signed in to change notification settings - Fork 1
/
filconverter.py
284 lines (237 loc) · 8.82 KB
/
filconverter.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""
Copyright (C) 2019 Matthias Neuner <matthias.neuner@uibk.ac.at>
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.
"""
import argparse
import sys
import os
import numpy as np
import math
from src.exportengine import ExportEngine, filInt
from src.inputfileparser import parseInputFile, printKeywords
import time
import textwrap
# a word in a .fil file has a size of 8 bytes
FIL_WORDSIZE = 8
# a chunk in a .fil file consists of 513 words
FIL_CHUNKSIZE = 513 * FIL_WORDSIZE
# .fil files may become huge. We do not load them at once, but
# but rather we split them into multiple batch sizes
FIL_BATCHSIZE = FIL_CHUNKSIZE * 4096 * 32 # = ~ 538 MByte ... size in BYTES
def fileSizeHumanReadable(num: int, suffix: str = "B"):
"""Pretty format bytes to a human readable form.
Parameters
----------
int
The number.
suffix
The suffix.
Returns
-------
type
The pretty formatted string.
"""
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
if abs(num) < 1000:
return f"{num:3.1f} {unit}{suffix}"
num /= 1000
return f"{num:.1f} Y{suffix}"
def getCurrentFileSize(
fn: str,
):
"""Determine the size of the .fil file.
Parameters
----------
fn
The .fil file name.
Returns
-------
type
The file size.
"""
fileStat = os.stat(fn)
fileSize = fileStat.st_size
return fileSize
def getCurrentMaxIdxEnd(fn: str, fileIdx: str):
"""Determine the maximum index in the .fil file depending on the current position.
It may be a complete chunk, or less if we are already near the end of the .fil file.
Parameters
----------
fn
The .fil file name.
fileIdx
The current file index.
Returns
-------
type
The maximum allowed index in the file.
"""
fileRemainder = fileSize - fileIdx # remaining file size in BYTES
idxEnd = fileIdx + (
FIL_BATCHSIZE if fileRemainder >= FIL_BATCHSIZE else fileRemainder
) # get end index
# in case we are operating on an unfinished file and 'catch' an unfinished chunk
idxEnd -= idxEnd % FIL_CHUNKSIZE
return idxEnd
def getWords(fn, fileIdx, idxEnd):
"""Get readable words between the fileIdx and idxEnd.
Parameters
----------
fileIdx
The current file index.
idxEnd
The end index.
Returns
-------
type
The words.
"""
fnMap = np.memmap(
fn,
dtype="b",
mode="r",
)
batchChunk = np.copy(fnMap[fileIdx:idxEnd]) # get chunk of file
words = batchChunk.reshape(-1, FIL_CHUNKSIZE) # get words
# strip unused bytes. Probably they contain checksums,
# so we may leverage that feature in a future version.
words = words[:, 4:-4]
words = words.reshape(-1, 8)
return words
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="A translator for Abaqus .fil files.")
parser.add_argument(
"fil",
metavar="FILFILE.fil",
help="The Abaqus .fil file",
type=str,
)
parser.add_argument(
"expDef",
metavar="EXPORTDEFINITION.inp",
help="The .inp export definition file",
type=str,
)
parser.add_argument(
"--keywords", dest="kw", action="store_true", help="print keywords"
)
args = parser.parse_args()
if args.kw:
printKeywords()
exit(0)
fn = args.fil
jobFile = args.expDef
exportJobs = parseInputFile(jobFile)
exportName = "".join(fn.split("/")[-1].split(".")[-2])
lockFile = fn.split(".")[0] + ".lck"
print("{:<20}{:>60}".format("opening file", fn))
print("*" * 80)
exportEngine = ExportEngine(exportJobs, exportName)
fileSize = getCurrentFileSize(fn)
numberOfBatchSteps = math.ceil(fileSize / FIL_BATCHSIZE)
print("file has a size of {:}".format(fileSizeHumanReadable(fileSize)))
print("file will be processed in {:} batch(es)".format(numberOfBatchSteps))
fileIdx = 0
wordIdx = 0
parseFile = True
while parseFile:
try:
fileSize = getCurrentFileSize(
fn,
)
if fileIdx < fileSize:
idxEnd = getCurrentMaxIdxEnd(fn, fileIdx)
words = getWords(fn, fileIdx, idxEnd)
while wordIdx < len(words):
recordLength = filInt(words[wordIdx])[0]
if recordLength <= 2:
print(
"found a record with 0 length content, possible an aborted Abaqus analysis"
)
if os.path.exists(lockFile):
print("found .lck file, waiting for new result .fil data")
time.sleep(5)
fileSize = getCurrentFileSize(
fn,
)
idxEnd = getCurrentMaxIdxEnd(fn, fileIdx)
words = getWords(fn, fileIdx, idxEnd)
continue
else:
parseFile = False
break
# the next record exceeds our batchChunk, so we do some trick:
# - set the wordIdx to the end of the so far progressed frame
# - move the frame to the wordIDx
if wordIdx + recordLength > len(words):
bytesProgressedInCurrentBatch = (
int(math.floor(wordIdx / 512)) * 513 * 8
)
if (
bytesProgressedInCurrentBatch == 0
): # indicator for an aborted analysis
print(
"terminated file, possible an aborted Abaqus analysis"
)
if os.path.exists(lockFile):
print(
"found .lck file, waiting for new result .fil data"
)
time.sleep(5)
fileSize = getCurrentFileSize(
fn,
)
idxEnd = getCurrentMaxIdxEnd(fn, fileIdx)
words = getWords(fn, fileIdx, idxEnd)
continue
else:
parseFile = False
break
fileIdx += bytesProgressedInCurrentBatch # move to beginning of the current 512 word block in the batchChunk and restart with a new bathChunk
wordIdx = (
wordIdx % 512
) # of course, restart at the present index
break
recordType = filInt(words[wordIdx + 1])[0]
recordContent = words[wordIdx + 2 : wordIdx + recordLength]
success = exportEngine.computeRecord(
recordLength, recordType, recordContent
)
wordIdx += recordLength
# clean finish of a batchChunk
if wordIdx == len(words):
wordIdx = 0
fileIdx = idxEnd
del words
else:
if os.path.exists(lockFile):
print(
"found .lck file, waiting for new result .fil data or CTRL-C to finish..."
)
time.sleep(10)
else:
break
except KeyboardInterrupt:
print("Interrupted by user")
break
exportEngine.finalize()
print("*" * 80)
print("Summary of .fil file:")
print("{:<60}{:>20}".format("nodes:", len(exportEngine.nodes)))
print("{:<60}{:>20}".format("elements:", len(exportEngine.elements)))
print("{:<60}{:>20}".format("element sets:", len(exportEngine.elSets)))
for setName, elSet in exportEngine.elSets.items():
for i, (elType, elements) in enumerate(elSet.elementsByShape.items()):
print(
"{:<4}{:<46}{:10}{:>11} elements".format(
" ", setName if not i else "", elType, len(elements)
)
)
print("{:<60}{:>20}".format("node sets:", len(exportEngine.nSets)))
for setName, nSet in exportEngine.nSets.items():
print(
"{:<4}{:<46}{:10}{:>11} nodes".format(" ", setName, "", len(nSet.nodes))
)
print("{:<60}{:>20}".format("increments:", exportEngine.nIncrements))