diff --git a/.agents/skills/thincurr-model/scripts/inspect_model.py b/.agents/skills/thincurr-model/scripts/inspect_model.py index 19fead825..7d981acbc 100644 --- a/.agents/skills/thincurr-model/scripts/inspect_model.py +++ b/.agents/skills/thincurr-model/scripts/inspect_model.py @@ -114,18 +114,28 @@ def read_file_info(mesh_file): # Legacy (Cubit) nodesets: used as holes when no "thincurr/holes" group exists nodesets = [] if 'mesh/NUM_NODESETS' in fid: - i = 1 - while 'mesh/NODESET{0:04d}'.format(i) in fid: + count = int(numpy.asarray(fid['mesh/NUM_NODESETS'][()]).flat[0]) + for i in range(1, count + 1): nodesets.append(fid['mesh/NODESET{0:04d}'.format(i)][()] - 1) - i += 1 info['nodesets'] = nodesets tc = fid['thincurr'] if 'thincurr' in fid else {} info['holes'] = [] - if 'holes' in tc: + info['modern_holes'] = 'holes' in tc + if info['modern_holes']: nholes = tc['holes/NHOLES'][0] info['holes'] = [tc['holes/HOLE{0:04d}'.format(i+1)][()] - 1 for i in range(nholes)] info['holes_version'] = int(tc['HOLES_VERSION'][0]) if 'HOLES_VERSION' in tc else None - info['closures'] = (tc['CLOSURES'][()] - 1).tolist() if 'CLOSURES' in tc else [] + # The native reader uses the first legacy sideset unless modern closures are present. + info['legacy_closures'] = ( + 'CLOSURES' not in tc and 'mesh/NUM_SIDESETS' in fid + and int(numpy.asarray(fid['mesh/NUM_SIDESETS'][()]).flat[0]) > 0 + ) + if 'CLOSURES' in tc: + info['closures'] = (tc['CLOSURES'][()] - 1).tolist() + elif info['legacy_closures']: + info['closures'] = (fid['mesh/SIDESET0001'][()] - 1).tolist() + else: + info['closures'] = [] info['junctions'] = 'junctions' in tc info['junction_holes'] = int(tc['junctions/NHOLES'][0]) if info['junctions'] else 0 info['njumpers'] = int(tc['jumpers/NJUMPERS'][0]) if 'jumpers' in tc else 0 @@ -314,6 +324,8 @@ def main(): # Hole/closure definition source if info['junctions']: hole_source = 'junction-split mesh ({0} junction holes)'.format(info['junction_holes']) + elif info['legacy_closures'] and info['closures']: + hole_source = 'mesh/SIDESET0001 ({0} legacy closures)'.format(len(info['closures'])) elif info['holes'] or info['closures'] or (info['holes_version'] is not None): hole_source = 'thincurr/holes ({0} holes, {1} closures{2})'.format( len(info['holes']), len(info['closures']), @@ -325,19 +337,34 @@ def main(): report['hole_source'] = hole_source skip_setup = False expected = stats['expected_topology'] - if expected is not None and not info['junctions']: + nsets = len(info['nodesets']) + if abs(args.jumper_start) > nsets: + errors.append('"jumper_start" exceeds number of nodesets in file') + skip_setup = True + periodic = info['nfp'] is not None or info['has_pmap'] + if expected is not None and not info['junctions'] and not skip_setup: if hole_source == 'none': if expected['holes'] > 0 or expected['closures'] > 0: errors.append('Mesh topology needs {holes} hole(s) and {closures} closure(s) but none are defined; ' 'run OFT_ThinCurr_holes (compute_homology) on it'.format(**expected)) skip_setup = True - elif hole_source.startswith('legacy'): - nsets = len(info['nodesets']) - if args.jumper_start == 0 and nsets > expected['holes']: - errors.append('{0} legacy nodesets but the topology needs only {1} hole(s); the extra nodesets are ' - 'probably jumpers (try --jumper-start {2}, and "jumper_start" when running), or ' - 'recreate the holes with OFT_ThinCurr_holes'.format(nsets, expected['holes'], - expected['holes'] - nsets)) + elif not info['modern_holes']: + # The native wrapper treats later nodesets as jumpers, not holes. + nholes = nsets + if args.jumper_start > 0: + nholes = args.jumper_start - 1 + elif args.jumper_start < 0: + nholes = nsets + args.jumper_start + if ((args.jumper_start == 0 and nholes > expected['holes']) + or (not periodic and nholes < expected['holes'])): + errors.append('Legacy nodesets define {0} hole(s), but the topology needs {1}. ' + 'Check --jumper-start, or recreate the holes with OFT_ThinCurr_holes.'.format( + nholes, expected['holes'])) + skip_setup = True + if not periodic and len(info['closures']) != expected['closures']: + errors.append('File defines {0} closure(s), but the topology needs {1}. ' + 'Recreate the closures with OFT_ThinCurr_holes.'.format( + len(info['closures']), expected['closures'])) skip_setup = True elif len(info['holes']) != expected['holes'] or len(info['closures']) != expected['closures']: warnings.append('File defines {0} hole(s) and {1} closure(s); the topology suggests {2} and {3}'.format( @@ -386,7 +413,7 @@ def main(): print(' {0:4d}: {1:8d} cells, area {2:.4E} m^2'.format(reg_id, reg_info['cells'], reg_info['area'])) if skip_setup: - print('\nSkipping ThinCurr setup: it would abort on the problem reported below') + print('\nSkipping ThinCurr setup because the file failed validation') if not (args.no_setup or skip_setup): print('\n--- ThinCurr setup (verifies holes and closures) ---', flush=True) try: diff --git a/.agents/tests/test_inspect_model.py b/.agents/tests/test_inspect_model.py new file mode 100644 index 000000000..5dd60d1e0 --- /dev/null +++ b/.agents/tests/test_inspect_model.py @@ -0,0 +1,59 @@ +# SPDX-License-Identifier: LGPL-3.0-only +"""Inspect small native meshes without a compiled ThinCurr runtime.""" +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +import h5py +import numpy + + +SCRIPT = Path(__file__).resolve().parents[1] / 'skills/thincurr-model/scripts/inspect_model.py' + + +class InspectModelTests(unittest.TestCase): + def inspect(self, points, cells, mesh_sets, *args): + with tempfile.TemporaryDirectory() as directory: + mesh = Path(directory) / 'mesh.h5' + with h5py.File(mesh, 'w') as mesh_file: + mesh_file['mesh/R'] = points + mesh_file['mesh/LC'] = cells + mesh_file['mesh/REG'] = numpy.ones(len(cells), dtype='i4') + for name, values in mesh_sets.items(): + mesh_file['mesh/' + name] = values + return subprocess.run( + [sys.executable, str(SCRIPT), str(mesh), '--no-setup', *args], + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, check=False, + ) + + def test_legacy_sideset_supplies_closure(self): + points = [(0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1)] + cells = [(1, 3, 2), (1, 2, 4), (2, 3, 4), (3, 1, 4)] + sets = {'NUM_SIDESETS': [1], 'SIDESET0001': [1]} + result = self.inspect(points, cells, sets) + self.assertEqual(result.returncode, 0, result.stdout) + self.assertIn('SIDESET0001', result.stdout) + + disk = self.inspect(points[:3], cells[:1], sets) + self.assertEqual(disk.returncode, 1, disk.stdout) + self.assertIn('File defines 1 closure(s), but the topology needs 0', disk.stdout) + + def test_legacy_jumpers_do_not_count_as_holes(self): + # A square annulus has one hole. Its only nodeset is reserved for a jumper. + points = [(-2, -2, 0), (2, -2, 0), (2, 2, 0), (-2, 2, 0), + (-1, -1, 0), (1, -1, 0), (1, 1, 0), (-1, 1, 0)] + cells = [(1, 2, 6), (1, 6, 5), (2, 3, 7), (2, 7, 6), + (3, 4, 8), (3, 8, 7), (4, 1, 5), (4, 5, 8)] + sets = {'NUM_NODESETS': [1], 'NODESET0001': [5, 6, 7, 8]} + valid = self.inspect(points, cells, sets) + self.assertEqual(valid.returncode, 0, valid.stdout) + for start in ('1', '-1'): + result = self.inspect(points, cells, sets, '--jumper-start', start) + self.assertEqual(result.returncode, 1, result.stdout) + self.assertIn('Legacy nodesets define 0 hole(s), but the topology needs 1', result.stdout) + for start in ('2', '-2'): + result = self.inspect(points, cells, sets, '--jumper-start', start) + self.assertEqual(result.returncode, 1, result.stdout) + self.assertIn('exceeds number of nodesets', result.stdout)