@@ -116,6 +116,9 @@ def add_cell(nastran_type, cell, cell_ref):
116
116
else :
117
117
break
118
118
119
+ print ()
120
+ print ()
121
+ print (repr (next_line ))
119
122
while True :
120
123
# End loop when ENDDATA detected
121
124
if next_line .startswith ("ENDDATA" ):
@@ -128,6 +131,7 @@ def add_cell(nastran_type, cell, cell_ref):
128
131
chunks .append (c )
129
132
while True :
130
133
next_line = f .readline ()
134
+ print (repr (next_line ))
131
135
132
136
if not next_line :
133
137
raise ReadError ("Premature EOF" )
@@ -167,6 +171,8 @@ def add_cell(nastran_type, cell, cell_ref):
167
171
168
172
keyword = chunks [0 ]
169
173
174
+ print (chunks )
175
+
170
176
# Points
171
177
if keyword == "GRID" :
172
178
# remove empty chunks
@@ -187,8 +193,7 @@ def add_cell(nastran_type, cell, cell_ref):
187
193
points .append (
188
194
[
189
195
_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 ]]
192
197
]
193
198
)
194
199
@@ -307,7 +312,10 @@ def write(filename, mesh, point_format="fixed-large", cell_format="fixed-small")
307
312
for point_id , x in enumerate (points ):
308
313
fx = [float_fmt (k ) for k in x ]
309
314
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 )
311
319
312
320
# CellBlock
313
321
cell_id = 0
@@ -357,40 +365,62 @@ def _float_rstrip(x, n=8):
357
365
358
366
def _float_to_nastran_string (value , length = 16 ):
359
367
"""
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
+
361
383
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"
365
387
"""
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" + "}"
370
400
371
- if value == 0.0 :
372
- return sfmt .format ("0." )
401
+ # if value == 0.0:
402
+ # return sfmt.format("0.")
373
403
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
377
407
378
- sign = "-" if abs (value ) < 1.0 else "+"
408
+ # sign = "-" if abs(value) < 1.0 else "+"
379
409
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)
383
413
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" + "}"
389
419
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
394
424
395
425
396
426
def _nastran_string_to_float (string ):
@@ -402,12 +432,15 @@ def _nastran_string_to_float(string):
402
432
403
433
404
434
def _chunk_line (line : str ) -> tuple [list [str ], bool ]:
435
+ # remove terminal newline
436
+ assert line [- 1 ] == "\n "
437
+ line = line [:- 1 ]
405
438
if "," in line :
406
439
# free format
407
440
return line .split ("," ), True
408
441
# fixed format
409
442
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 )]
411
444
return chunks , False
412
445
413
446
0 commit comments