Skip to content

Commit afcdfe0

Browse files
committed
more nastran fixes
1 parent 6ef8bee commit afcdfe0

File tree

1 file changed

+63
-30
lines changed

1 file changed

+63
-30
lines changed

src/meshio/nastran/_nastran.py

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ def add_cell(nastran_type, cell, cell_ref):
116116
else:
117117
break
118118

119+
print()
120+
print()
121+
print(repr(next_line))
119122
while True:
120123
# End loop when ENDDATA detected
121124
if next_line.startswith("ENDDATA"):
@@ -128,6 +131,7 @@ def add_cell(nastran_type, cell, cell_ref):
128131
chunks.append(c)
129132
while True:
130133
next_line = f.readline()
134+
print(repr(next_line))
131135

132136
if not next_line:
133137
raise ReadError("Premature EOF")
@@ -167,6 +171,8 @@ def add_cell(nastran_type, cell, cell_ref):
167171

168172
keyword = chunks[0]
169173

174+
print(chunks)
175+
170176
# Points
171177
if keyword == "GRID":
172178
# remove empty chunks
@@ -187,8 +193,7 @@ def add_cell(nastran_type, cell, cell_ref):
187193
points.append(
188194
[
189195
_nastran_string_to_float(i + j)
190-
# TODO why 10:12, not 9:11? check this in the manual
191-
for i, j in [chunks[5:7], chunks[7:9], chunks[10:12]]
196+
for i, j in [chunks[5:7], chunks[7:9], chunks[9:11]]
192197
]
193198
)
194199

@@ -307,7 +312,10 @@ def write(filename, mesh, point_format="fixed-large", cell_format="fixed-small")
307312
for point_id, x in enumerate(points):
308313
fx = [float_fmt(k) for k in x]
309314
pref = str(point_refs[point_id]) if point_refs is not None else ""
310-
f.write(grid_fmt.format(point_id + 1, pref, fx[0], fx[1], fx[2]))
315+
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))
318+
f.write(string)
311319

312320
# CellBlock
313321
cell_id = 0
@@ -357,40 +365,62 @@ def _float_rstrip(x, n=8):
357365

358366
def _float_to_nastran_string(value, length=16):
359367
"""
360-
Return a value in NASTRAN scientific notation.
368+
From
369+
<https://docs.plm.automation.siemens.com/data_services/resources/nxnastran/10/help/en_US/tdocExt/pdf/User.pdf>:
370+
371+
Real numbers, including zero, must contain a decimal point. You can enter
372+
real numbers in a variety of formats. For example, the following are all
373+
acceptable versions of the real number, seven:
374+
```
375+
7.0 .7E1 0.7+1
376+
.70+1 7.E+0 70.-1
377+
```
378+
379+
This methods converts a float value into the corresponding string. Choose
380+
the variant with `E` to make the file less ambigious when edited by a
381+
human. (`5.-1` looks like 4.0, not 5.0e-1 = 0.5.)
382+
361383
Examples:
362-
1234.56789 --> "1.23456789+3"
363-
-0.1234 --> "-1.234-1"
364-
3.1415926535897932 --> "3.14159265359+0"
384+
1234.56789 --> "1.23456789E+3"
385+
-0.1234 --> "-1.234E-1"
386+
3.1415926535897932 --> "3.14159265359E+0"
365387
"""
366-
aux = length - 2
367-
# sfmt = "{" + f":{length}s" + "}"
368-
sfmt = "{" + ":s" + "}"
369-
pv_fmt = "{" + f":{length}.{aux}e" + "}"
388+
out = np.format_float_scientific(value, exp_digits=1, precision=11).replace(
389+
"e", "E"
390+
)
391+
assert len(out) <= 16
392+
return out
393+
# The following is the manual float conversion. Keep it around for a while in case
394+
# we still need it.
395+
396+
# aux = length - 2
397+
# # sfmt = "{" + f":{length}s" + "}"
398+
# sfmt = "{" + ":s" + "}"
399+
# pv_fmt = "{" + f":{length}.{aux}e" + "}"
370400

371-
if value == 0.0:
372-
return sfmt.format("0.")
401+
# if value == 0.0:
402+
# return sfmt.format("0.")
373403

374-
python_value = pv_fmt.format(value) # -1.e-2
375-
svalue, sexponent = python_value.strip().split("e")
376-
exponent = int(sexponent) # removes 0s
404+
# python_value = pv_fmt.format(value) # -1.e-2
405+
# svalue, sexponent = python_value.strip().split("e")
406+
# exponent = int(sexponent) # removes 0s
377407

378-
sign = "-" if abs(value) < 1.0 else "+"
408+
# sign = "-" if abs(value) < 1.0 else "+"
379409

380-
# the exponent will be added later...
381-
sexp2 = str(exponent).strip("-+")
382-
value2 = float(svalue)
410+
# # the exponent will be added later...
411+
# sexp2 = str(exponent).strip("-+")
412+
# value2 = float(svalue)
383413

384-
# the plus 1 is for the sign
385-
len_sexp = len(sexp2) + 1
386-
leftover = length - len_sexp
387-
leftover = leftover - 3 if value < 0 else leftover - 2
388-
fmt = "{" + f":1.{leftover:d}f" + "}"
414+
# # the plus 1 is for the sign
415+
# len_sexp = len(sexp2) + 1
416+
# leftover = length - len_sexp
417+
# leftover = leftover - 3 if value < 0 else leftover - 2
418+
# fmt = "{" + f":1.{leftover:d}f" + "}"
389419

390-
svalue3 = fmt.format(value2)
391-
svalue4 = svalue3.strip("0")
392-
field = sfmt.format(svalue4 + sign + sexp2)
393-
return field
420+
# svalue3 = fmt.format(value2)
421+
# svalue4 = svalue3.strip("0")
422+
# field = sfmt.format(svalue4 + sign + sexp2)
423+
# return field
394424

395425

396426
def _nastran_string_to_float(string):
@@ -402,12 +432,15 @@ def _nastran_string_to_float(string):
402432

403433

404434
def _chunk_line(line: str) -> tuple[list[str], bool]:
435+
# remove terminal newline
436+
assert line[-1] == "\n"
437+
line = line[:-1]
405438
if "," in line:
406439
# free format
407440
return line.split(","), True
408441
# fixed format
409442
CHUNK_SIZE = 8
410-
chunks = [line[i : CHUNK_SIZE + i] for i in range(0, 80, CHUNK_SIZE)]
443+
chunks = [line[i : CHUNK_SIZE + i] for i in range(0, len(line), CHUNK_SIZE)]
411444
return chunks, False
412445

413446

0 commit comments

Comments
 (0)