Skip to content

Commit 9f32d06

Browse files
committed
nastran: better handling of large-fixed
1 parent afcdfe0 commit 9f32d06

File tree

1 file changed

+51
-27
lines changed

1 file changed

+51
-27
lines changed

src/meshio/nastran/_nastran.py

+51-27
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,6 @@ def add_cell(nastran_type, cell, cell_ref):
116116
else:
117117
break
118118

119-
print()
120-
print()
121-
print(repr(next_line))
122119
while True:
123120
# End loop when ENDDATA detected
124121
if next_line.startswith("ENDDATA"):
@@ -131,7 +128,6 @@ def add_cell(nastran_type, cell, cell_ref):
131128
chunks.append(c)
132129
while True:
133130
next_line = f.readline()
134-
print(repr(next_line))
135131

136132
if not next_line:
137133
raise ReadError("Premature EOF")
@@ -147,56 +143,85 @@ def add_cell(nastran_type, cell, cell_ref):
147143
# continuation identifier. A continuation identifier is a
148144
# special character (+ or *) that indicates that the data
149145
# continues on another line.
146+
assert len(chunks[-1]) <= 10
147+
if len(chunks[-1]) == 10:
148+
# This is a continuation line, so the 10th chunk of the
149+
# previous line must also be a continuation indicator.
150+
# Sometimes its first character is a `+`, but it's not
151+
# always present. Anyway, cut it off.
152+
chunks[-1][-1] = None
150153
c, _ = _chunk_line(next_line)
151-
chunks.append(c[1:])
154+
c[0] = None
155+
chunks.append(c)
152156

153157
elif len(chunks[-1]) == 10 and chunks[-1][-1] == " ":
154158
# automatic continuation: last chunk of previous line and first
155159
# chunk of current line are spaces
156160
c, _ = _chunk_line(next_line)
157161
if c[0] == " ":
158-
chunks[-1] = chunks[-1][:9]
159-
chunks.append(c[1:])
162+
chunks[-1][9] = None
163+
c[0] = None
164+
chunks.append(c)
160165
else:
161166
# not a continuation
162167
break
163168
else:
164169
break
165170

171+
# merge chunks according to large field format
172+
# large field format: 8 + 16 + 16 + 16 + 16 + 8
173+
if chunks[0][0].startswith("GRID*"):
174+
new_chunks = []
175+
for c in chunks:
176+
d = [c[0]]
177+
178+
if len(c) > 1:
179+
d.append(c[1])
180+
if len(c) > 2:
181+
d[-1] += c[2]
182+
183+
if len(c) > 3:
184+
d.append(c[3])
185+
if len(c) > 4:
186+
d[-1] += c[4]
187+
188+
if len(c) > 5:
189+
d.append(c[5])
190+
if len(c) > 6:
191+
d[-1] += c[6]
192+
193+
if len(c) > 7:
194+
d.append(c[7])
195+
if len(c) > 8:
196+
d[-1] += c[8]
197+
198+
if len(c) > 9:
199+
d.append(c[9])
200+
201+
new_chunks.append(d)
202+
203+
chunks = new_chunks
204+
166205
# flatten
167206
chunks = [item for sublist in chunks for item in sublist]
168207

208+
# remove None (continuation blocks)
209+
chunks = [chunk for chunk in chunks if chunk is not None]
210+
169211
# strip chunks
170212
chunks = [chunk.strip() for chunk in chunks]
171213

172214
keyword = chunks[0]
173215

174-
print(chunks)
175-
176216
# Points
177-
if keyword == "GRID":
178-
# remove empty chunks
179-
chunks = [c for c in chunks if c != ""]
217+
if keyword in ["GRID", "GRID*"]:
180218
point_id = int(chunks[1])
181219
pref = chunks[2].strip()
182220
if len(pref) > 0:
183221
point_refs.append(int(pref))
184222
points_id.append(point_id)
185223
points.append([_nastran_string_to_float(i) for i in chunks[3:6]])
186224

187-
elif keyword == "GRID*": # large field format: 8 + 16*4 + 8
188-
point_id = int(chunks[1] + chunks[2])
189-
pref = (chunks[3] + chunks[4]).strip()
190-
if len(pref) > 0:
191-
point_refs.append(int(pref))
192-
points_id.append(point_id)
193-
points.append(
194-
[
195-
_nastran_string_to_float(i + j)
196-
for i, j in [chunks[5:7], chunks[7:9], chunks[9:11]]
197-
]
198-
)
199-
200225
# CellBlock
201226
elif keyword in nastran_to_meshio_type:
202227
cell_id = int(chunks[1])
@@ -313,8 +338,6 @@ def write(filename, mesh, point_format="fixed-large", cell_format="fixed-small")
313338
fx = [float_fmt(k) for k in x]
314339
pref = str(point_refs[point_id]) if point_refs is not None else ""
315340
string = grid_fmt.format(point_id + 1, pref, fx[0], fx[1], fx[2])
316-
print(point_id + 1, repr(pref), x, fx)
317-
print("s", repr(string))
318341
f.write(string)
319342

320343
# CellBlock
@@ -338,6 +361,7 @@ def write(filename, mesh, point_format="fixed-large", cell_format="fixed-small")
338361
cell1 = cell + 1
339362
cell1 = _convert_to_nastran_ordering(cell1, nastran_type)
340363
conn = sjoin.join(int_fmt.format(nid) for nid in cell1[:nipl1])
364+
341365
if len(cell1) > nipl1:
342366
if cell_format == "free":
343367
cflag1 = cflag3 = ""

0 commit comments

Comments
 (0)