Skip to content

Commit 7e0d20f

Browse files
committed
Fix reading of continuation lines in the Nastran modal solver mesh reader
The sorted bulk data echo of an f06 file is paginated: page titles, carriage control lines and column rulers can interrupt an entry at any point. The SET1 and CORD2R readers consumed the next physical line as the continuation, so any page break inside an entry made the reader fail (issue su2code#2313). Continuation markers other than a bare + were also mishandled: Nastran generated markers such as +000001 were silently appended to the marker set as grid point 1. Continuation lines are now located by their marker field, skipping the page furniture, and THRU ranges of the SET1 entry are supported.
1 parent 27cab06 commit 7e0d20f

2 files changed

Lines changed: 245 additions & 26 deletions

File tree

‎SU2_PY/SU2_Nastran/pysu2_nastran.py‎

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
# Imports
3030
# ----------------------------------------------------------------------
3131

32+
import re
33+
3234
import numpy as np
3335
import scipy.linalg as linalg
3436
from math import *
@@ -505,9 +507,9 @@ def nastran_float(s):
505507
BZ = nastran_float(line[64:72])
506508
z_direction = np.array([BX - AX, BY - AY, BZ - AZ])
507509
z_direction = z_direction / linalg.norm(z_direction)
508-
line = meshfile.readline()
509-
line = line.strip("\r\n")
510-
line = line[30:]
510+
line = self.__readContinuationLine(
511+
meshfile, "CORD2R {}".format(CID)
512+
)
511513
CX = nastran_float(line[8:16])
512514
CY = nastran_float(line[16:24])
513515
CZ = nastran_float(line[24:32])
@@ -524,32 +526,41 @@ def nastran_float(s):
524526

525527
pos = line.find("SET1")
526528
if pos == 30:
527-
line = line.strip("\r\n")
528-
line = line[37:]
529-
line = line.split()
530-
existValue = True
531-
markerTag = line.pop(0)
529+
fields = line.strip("\r\n")[30:].split()
530+
fields.pop(0)
531+
markerTag = fields.pop(0)
532532
self.markers[markerTag] = []
533-
while existValue:
534-
if line[0] == "+":
535-
line = meshfile.readline()
536-
line = line.strip("\r\n")
537-
line = line[37:]
538-
line = line.split()
539-
ID = int(line.pop(0))
533+
534+
def addPoint(ID):
540535
for iPoint in range(self.nPoint):
541536
if self.node[iPoint].GetID() == ID:
542-
break
543-
if (iPoint == (self.nPoint - 1)) and (
544-
self.node[iPoint].GetID() != ID
545-
):
546-
raise Exception(
547-
"Point {} in the set {} was not found in the mesh".format(
548-
ID, markerTag
549-
)
537+
self.markers[markerTag].append(iPoint)
538+
return
539+
raise Exception(
540+
"Point {} in the set {} was not found in the mesh".format(
541+
ID, markerTag
550542
)
551-
self.markers[markerTag].append(iPoint)
552-
existValue = len(line) >= 1
543+
)
544+
545+
ID = None
546+
while fields:
547+
entry = fields.pop(0)
548+
if entry[0] in "+*" and not fields:
549+
# continuation marker in field 10, the set goes on
550+
fields = self.__readContinuationLine(
551+
meshfile, "SET1 {}".format(markerTag)
552+
).split()
553+
fields.pop(0)
554+
elif entry == "THRU":
555+
# ranges may not open or close a line (QRG SET1 remark 4)
556+
last = ID
557+
ID = int(fields.pop(0))
558+
for thruID in range(last + 1, ID):
559+
addPoint(thruID)
560+
addPoint(ID)
561+
else:
562+
ID = int(entry)
563+
addPoint(ID)
553564
self.nMarker += 1
554565
continue
555566

@@ -563,11 +574,37 @@ def nastran_float(s):
563574
print("Number of reference systems: {}".format(self.nRefSys))
564575
print("Moving marker: {}".format(self.FSI_marker))
565576
print(
566-
"Number of points in the moving marker".format(
577+
"Number of points in the moving marker: {}".format(
567578
len(self.markers[self.FSI_marker])
568579
)
569580
)
570581

582+
def __readContinuationLine(self, meshfile, entry):
583+
"""
584+
This method returns the card image of the next continuation line, skipping
585+
the page titles, carriage control characters and column rulers with which
586+
the f06 file paginates the sorted bulk data echo.
587+
"""
588+
589+
while True:
590+
line = meshfile.readline()
591+
if not line:
592+
raise Exception(
593+
"Unexpected end of {} while reading a continuation of {}".format(
594+
self.Mesh_file, entry
595+
)
596+
)
597+
image = line.strip("\r\n")[30:]
598+
fields = image.split()
599+
if fields and fields[0][0] in "+*":
600+
return image
601+
if fields and re.match(r" *[0-9]+-", line):
602+
raise Exception(
603+
"Expected a continuation of {} but found {}".format(
604+
entry, fields[0]
605+
)
606+
)
607+
571608
def __checkBlankField(self, string):
572609
"""
573610
This method considers that Nastran apply 0 when the reference system is not specified
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
#!/usr/bin/env python
2+
3+
## \file test_pysu2_nastran_continuations.py
4+
# \brief Tests for continuation and page break handling in the Nastran mesh reader.
5+
# \version 8.5.0 "Harrier"
6+
#
7+
# SU2 Project Website: https://su2code.github.io
8+
#
9+
# The SU2 Project is maintained by the SU2 Foundation
10+
# (http://su2foundation.org)
11+
#
12+
# Copyright 2012-2026, SU2 Contributors (cf. AUTHORS.md)
13+
#
14+
# SU2 is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU Lesser General Public
16+
# License as published by the Free Software Foundation; either
17+
# version 2.1 of the License, or (at your option) any later version.
18+
#
19+
# SU2 is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22+
# Lesser General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU Lesser General Public
25+
# License along with SU2. If not, see <http://www.gnu.org/licenses/>.
26+
27+
import os
28+
import sys
29+
import tempfile
30+
import unittest
31+
32+
import numpy as np
33+
34+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
35+
36+
from pysu2_nastran import Solver
37+
38+
# Page header block interrupting the sorted bulk data echo, as printed by
39+
# Nastran at every page break (structure taken from the f06 in issue #2313).
40+
PAGE_HEADER = [
41+
"1 NX NASTRAN MODES ANALYSIS SET "
42+
"NOVEMBER 15, 2022 MSC Nastran 11/19/16 PAGE 4257",
43+
" " * 132,
44+
"0" + " " * 131,
45+
" " * 50 + "S O R T E D B U L K D A T A E C H O",
46+
" " * 17 + "ENTRY",
47+
" " * 17 + "COUNT . 1 .. 2 .. 3 .. 4 .. 5 .. 6 "
48+
".. 7 .. 8 .. 9 .. 10 .",
49+
]
50+
51+
52+
def echo(count, *fields):
53+
"""Builds one numbered line of the sorted bulk data echo."""
54+
55+
line = ("%6d-" % count).rjust(22) + " " * 8
56+
for field in fields:
57+
line += str(field).ljust(8)
58+
return line.rstrip()
59+
60+
61+
def grid_lines(ids, start=1):
62+
return [
63+
echo(start + i, "GRID", gid, 0, "1.0", "2.0", "3.0", 0)
64+
for i, gid in enumerate(ids)
65+
]
66+
67+
68+
def set1_lines(ids, start, markers):
69+
"""Echoes SET1 1 with the given continuation markers between chunks."""
70+
71+
chunks = [ids[:7]] + [ids[i : i + 8] for i in range(7, len(ids), 8)]
72+
lines = []
73+
for i, chunk in enumerate(chunks):
74+
head = ["SET1", 1] if i == 0 else [markers[i - 1]]
75+
tail = [markers[i]] if i < len(chunks) - 1 else []
76+
lines.append(echo(start + i, *(head + list(chunk) + tail)))
77+
return lines
78+
79+
80+
class TestContinuations(unittest.TestCase):
81+
def read(self, lines, marker="1"):
82+
handle, path = tempfile.mkstemp(suffix=".f06")
83+
with os.fdopen(handle, "w") as mesh_file:
84+
mesh_file.write("\n".join(lines) + "\n")
85+
self.addCleanup(os.remove, path)
86+
solver = Solver.__new__(Solver)
87+
solver.Mesh_file = path
88+
solver.FSI_marker = marker
89+
solver.node = []
90+
solver.markers = {}
91+
solver.refsystems = []
92+
solver._Solver__readNastranMesh()
93+
return solver
94+
95+
def marker_ids(self, solver, marker="1"):
96+
return sorted(solver.node[i].GetID() for i in solver.markers[marker])
97+
98+
def test_page_break_inside_set1_continuations(self):
99+
# The scenario of issue #2313: headers interrupt the continuations.
100+
set1 = set1_lines(list(range(1, 24)), 25, ["+", "+", "+"])
101+
lines = grid_lines(range(1, 24)) + set1[:2] + PAGE_HEADER + set1[2:]
102+
solver = self.read(lines)
103+
self.assertEqual(self.marker_ids(solver), list(range(1, 24)))
104+
105+
def test_page_break_between_set1_parent_and_continuation(self):
106+
set1 = set1_lines(list(range(1, 16)), 25, ["+"])
107+
lines = grid_lines(range(1, 16)) + set1[:1] + PAGE_HEADER + set1[1:]
108+
solver = self.read(lines)
109+
self.assertEqual(self.marker_ids(solver), list(range(1, 16)))
110+
111+
def test_page_break_between_cord2r_lines(self):
112+
lines = (
113+
[echo(1, "CORD2R", 1, 0, "0.", "0.", "0.", "0.", "0.", "1.", "+")]
114+
+ PAGE_HEADER
115+
+ [echo(2, "+", "1.", "0.", "0.")]
116+
+ grid_lines([1], start=3)
117+
+ [echo(4, "SET1", 1, 1)]
118+
)
119+
solver = self.read(lines)
120+
self.assertEqual(solver.nRefSys, 1)
121+
np.testing.assert_allclose(
122+
solver.refsystems[0].GetRotMatrix(), np.eye(3), atol=1e-12
123+
)
124+
self.assertEqual(self.marker_ids(solver), [1])
125+
126+
def test_generated_continuation_markers(self):
127+
# Nastran generates +000001 style markers for blank continuations,
128+
# they must not be read as the grid point 1.
129+
markers = ["+000001", "+000002"]
130+
lines = grid_lines(range(1, 17)) + set1_lines(list(range(1, 17)), 20, markers)
131+
solver = self.read(lines)
132+
self.assertEqual(self.marker_ids(solver), list(range(1, 17)))
133+
134+
def test_user_named_continuation_markers(self):
135+
lines = grid_lines(range(1, 17)) + set1_lines(
136+
list(range(1, 17)), 20, ["+PB1", "+PB2"]
137+
)
138+
solver = self.read(lines)
139+
self.assertEqual(self.marker_ids(solver), list(range(1, 17)))
140+
141+
def test_thru(self):
142+
# SET1 6 29 32 THRU 50 61 THRU 70, the second example of the QRG entry.
143+
ids = [29] + list(range(32, 51)) + list(range(61, 71))
144+
lines = grid_lines(ids) + [
145+
echo(40, "SET1", 6, 29, 32, "THRU", 50, 61, "THRU", 70)
146+
]
147+
solver = self.read(lines, marker="6")
148+
self.assertEqual(self.marker_ids(solver, "6"), sorted(ids))
149+
150+
def test_missing_continuation_is_reported(self):
151+
lines = grid_lines(range(1, 9)) + [
152+
echo(10, "SET1", 1, 1, 2, 3, 4, 5, 6, 7, "+"),
153+
echo(11, "SPC1", 2, 123456, 8),
154+
]
155+
with self.assertRaisesRegex(Exception, "continuation of SET1 1.*SPC1"):
156+
self.read(lines)
157+
158+
def test_truncated_file_is_reported(self):
159+
lines = grid_lines(range(1, 9)) + [
160+
echo(10, "SET1", 1, 1, 2, 3, 4, 5, 6, 7, "+")
161+
]
162+
with self.assertRaisesRegex(Exception, "end of .*continuation of SET1 1"):
163+
self.read(lines)
164+
165+
def test_point_missing_from_mesh_is_reported(self):
166+
lines = grid_lines(range(1, 4)) + [echo(5, "SET1", 1, 1, 2, 99)]
167+
with self.assertRaisesRegex(Exception, "Point 99 in the set 1 was not found"):
168+
self.read(lines)
169+
170+
def test_unpaginated_echo_still_reads(self):
171+
# The tutorial shaped file must parse exactly as before.
172+
lines = grid_lines(range(1, 24)) + set1_lines(
173+
list(range(1, 24)), 25, ["+", "+", "+"]
174+
)
175+
solver = self.read(lines)
176+
self.assertEqual(solver.nPoint, 23)
177+
self.assertEqual(solver.nMarker, 1)
178+
self.assertEqual(self.marker_ids(solver), list(range(1, 24)))
179+
180+
181+
if __name__ == "__main__":
182+
unittest.main()

0 commit comments

Comments
 (0)