|
| 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