diff --git a/openfast_toolbox/io/fast_input_file.py b/openfast_toolbox/io/fast_input_file.py index c8fd7ec..7408786 100644 --- a/openfast_toolbox/io/fast_input_file.py +++ b/openfast_toolbox/io/fast_input_file.py @@ -20,6 +20,7 @@ class BrokenFormatError(Exception): pass TABTYPE_MIX_WITH_HEADER = 6 TABTYPE_FIL = 3 TABTYPE_OUTLIST = 33 +TABTYPE_OUTLIST_NO_HEADER = 34 TABTYPE_FMT = 9999 # TODO @@ -322,18 +323,13 @@ def __next__(self): # Python 2: def next(self) # Making it behave like a dictionary def __setitem__(self, key, item): - I = self.getIDs(key) - for i in I: - if self.data[i]['tabType'] != TABTYPE_NOT_A_TAB: - # For tables, we automatically update variable that stores the dimension - nRows = len(item) - if 'tabDimVar' in self.data[i].keys(): - dimVar = self.data[i]['tabDimVar'] - iDimVar = self.getID(dimVar) - self.data[iDimVar]['value'] = nRows # Avoiding a recursive call to __setitem__ here - else: - pass - self.data[i]['value'] = item + for i in self.getIDs(key): + entry = self.data[i] + dim_var = entry.get('tabDimVar') + # Automatically sized tables do not store a separate row count. + if entry['tabType'] != TABTYPE_NOT_A_TAB and dim_var not in (None, 'AUTO'): + self.data[self.getID(dim_var)]['value'] = len(item) + entry['value'] = item def __getitem__(self, key): i = self.getID(key) @@ -645,13 +641,13 @@ def _read(self, IComment=None, verbose=False): continue #---The following 3 by 3 matrix is the direction cosine matirx ,GlbDCM(3,3), - elif line.upper().find('OUTPUTS')>0: - if 'Points' in self.keys() and 'dtM' in self.keys(): + elif line.lstrip().startswith('---') and 'OUTPUTS' in line.upper(): + if 'Points' in self and 'LineProp' in self: OutList,i = parseFASTOutList(lines,i+1) d = getDict() d['label'] = 'Outlist' d['descr'] = '' - d['tabType'] = TABTYPE_OUTLIST + d['tabType'] = TABTYPE_OUTLIST_NO_HEADER d['value'] = OutList self.addComment('------------------------ OUTPUTS --------------------------------------------') self.data.append(d) @@ -813,12 +809,17 @@ def _read(self, IComment=None, verbose=False): self.data.append(dd) d['label'] = NUMTAB_FROM_LAB_VARNAME[ii] + tabDimVar = NUMTAB_FROM_LAB_DIM_VAR[ii] + # MoorDyn v2 also uses "ID Type" without a point count. + if (labelRaw == 'type' and i > 0 + and lines[i - 1].strip(' -\t').lower() == 'points'): + d['label'] = 'Points' + tabDimVar = 'AUTO' if d['label'].lower()=='afcoeff' : d['tabType'] = TABTYPE_NUM_WITH_HEADERCOM else: d['tabType'] = TABTYPE2ID [ tab_type ] # Finding table dimension (number of lines) - tabDimVar = NUMTAB_FROM_LAB_DIM_VAR[ii] nTabLines = np.nan if isinstance(tabDimVar, int): # dimension hardcoded d['tabDimVar'] = tabDimVar @@ -1012,6 +1013,10 @@ def mat_tostring(M,fmt='24.16e'): label = d['label'] s+='{:22s} {:11s} - {}\n'.format('', label, d['descr']) s+='\n'.join(fil for fil in d['value'][1:]) + elif d['tabType']==TABTYPE_OUTLIST_NO_HEADER: + if len(d['value']) == 0: + continue + s+='\n'.join(d['value']) elif d['tabType']==TABTYPE_NUM_BEAMDYN: # TODO use dedicated sub-class data = d['value'] diff --git a/openfast_toolbox/io/tests/test_fast_input.py b/openfast_toolbox/io/tests/test_fast_input.py index 7b26a92..93f9927 100644 --- a/openfast_toolbox/io/tests/test_fast_input.py +++ b/openfast_toolbox/io/tests/test_fast_input.py @@ -1,5 +1,7 @@ import unittest import os +from pathlib import Path +from tempfile import TemporaryDirectory import numpy as np from openfast_toolbox.io.tests.helpers_for_test import MyDir, reading_test from openfast_toolbox.io.fast_input_file import FASTInputFile @@ -180,6 +182,68 @@ def test_FASTInMoorDyn(self): self.assertEqual(F['LineProp'][0,1] , 'main') self.assertEqual(F['LineProp'][0,6] , '-') + def test_FASTInMoorDynV1TableReplacementUpdatesCount(self): + mooring = FASTInputFile(os.path.join(MyDir, 'FASTIn_MD-v1.dat')) + lines = mooring['LineProp'][:3] + mooring['LineProp'] = lines + self.assertEqual(mooring['NLines'], len(lines)) + np.testing.assert_array_equal(mooring['LineProp'], lines) + + def test_FASTInMoorDynV2TableReplacement(self): + from openfast_toolbox.case_generation.case_gen import templateReplace + + fixture = Path(MyDir) / 'FASTIn_MD-v2.dat' + mooring = FASTInputFile(str(fixture)) + line_types = mooring['LineTypes'] + new_type = line_types[0].copy() + new_type[0] = 'spare' + points = mooring['Points'].copy() + points[0, 2] = '420.0' + lines = mooring['LineProp'].copy() + lines[0, 1] = 'spare' + tables = { + 'LineTypes': np.vstack((line_types, new_type)), + 'Points': points, + 'LineProp': lines, + } + + for header, has_timestep in [('Attachment', True), ('Type', True), ('Type', False)]: + with self.subTest(header=header, has_timestep=has_timestep), TemporaryDirectory() as folder: + template = Path(folder) / 'template' + template.mkdir() + (template / 'main.fst').write_text( + '! OpenFAST input file\nMoorDyn case generation\n"MoorDyn.dat" MooringFile\n', + encoding='ascii', + ) + mooring_text = fixture.read_text(encoding='ascii').replace('Attachment', header, 1) + if not has_timestep: + mooring_text = ''.join( + line for line in mooring_text.splitlines(keepends=True) if 'dtM' not in line.split() + ) + (template / 'MoorDyn.dat').write_text(mooring_text, encoding='ascii') + params = {'__name__': 'case'} + params.update({'MooringFile|' + name: rows for name, rows in tables.items()}) + [case_path] = templateReplace( + [params], str(template), outputDir=str(Path(folder) / 'generated'), + main_file='main.fst', + ) + relative_name = FASTInputFile(case_path)['MooringFile'].strip('"') + generated = FASTInputFile(str(Path(case_path).parent / relative_name)) + for name, rows in tables.items(): + np.testing.assert_array_equal(generated[name], rows) + self.assertEqual(generated['Outlist'], mooring['Outlist']) + for count in ('NTypes', 'NConnects', 'NPoints', 'NLines'): + self.assertNotIn(count, generated.keys()) + + def test_FASTInMoorDynV2OutlistRoundTrip(self): + mooring = FASTInputFile(os.path.join(MyDir, 'FASTIn_MD-v2.dat')) + for channels in (mooring['Outlist'], []): + with self.subTest(channels=channels), TemporaryDirectory() as folder: + mooring['Outlist'] = channels + filename = str(Path(folder) / 'MoorDyn.dat') + mooring.write(filename) + self.assertEqual(FASTInputFile(filename)['Outlist'], channels) + def test_FASTInAirfoil(self): F=FASTInputFile(os.path.join(MyDir,'FASTIn_AD15_arfl.dat')) F.test_ascii(bCompareWritesOnly=True,bDelete=True)