Skip to content

Commit

Permalink
new tests for lib robustness, verifying fst_vt
Browse files Browse the repository at this point in the history
  • Loading branch information
mayankchetan committed Sep 9, 2024
1 parent 862b4b5 commit 7ba57ed
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 40 deletions.
94 changes: 79 additions & 15 deletions openfast_io/openfast_io/FAST_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,38 @@


def readline_filterComments(f):
read = True
while read:
line = f.readline().strip()
if len(line)>0:
if line[0] != '!':
read = False
return line
"""
Filter out comments and empty lines from a file
Args:
f: file handle
Returns:
line: next line in the file that is not a comment or empty
"""
read = True
while read:
line = f.readline().strip()
if len(line)>0:
if line[0] != '!':
read = False
return line

def read_array(f,len,split_val=None,array_type=str):
"""
Read an array of values from a line in a file
Args:
f: file handle
len: number of values to read
split_val: value to stop reading at
array_type: type of values to return
Returns:
arr: list of values read from the file line with the specified type
"""


strings = re.split(',| ',f.readline().strip())
while '' in strings: # remove empties
strings.remove('')
Expand Down Expand Up @@ -54,15 +77,31 @@ def read_array(f,len,split_val=None,array_type=str):
return arr

def fix_path(name):
""" split a path, then reconstruct it using os.path.join """
"""
split a path, then reconstruct it using os.path.join
Args:
name: path to fix
Returns:
new: reconstructed path
"""
name = re.split("\\|/", name)
new = name[0]
for i in range(1,len(name)):
new = os.path.join(new, name[i])
return new

def bool_read(text):
# convert true/false strings to boolean
"""
Read a boolean value from a string
Args:
text: string to read
Returns:
True if the string is 'true', False otherwise
"""
if 'default' in text.lower():
return str(text)
else:
Expand All @@ -72,7 +111,15 @@ def bool_read(text):
return False

def float_read(text):
# return float with error handing for "default" values
"""
Read a float value from a string, with error handling for 'default' values
Args:
text: string to read
Returns:
float value if the string can be converted, string otherwise
"""
if 'default' in text.lower():
return str(text)
else:
Expand All @@ -82,7 +129,15 @@ def float_read(text):
return str(text)

def int_read(text):
# return int with error handing for "default" values
"""
Read an integer value from a string, with error handling for 'default' values
Args:
text: string to read
Returns:
int value if the string can be converted, string otherwise
"""
if 'default' in text.lower():
return str(text)
else:
Expand All @@ -92,7 +147,16 @@ def int_read(text):
return str(text)

def quoted_read(text):
# read a line, if the first part is quoted, return the quoted part, otherwise return the unquoted part
"""
Read a quoted value from a string (i.e. a value between quotes)
Args:
text: string to read
Returns:
quoted value if the string is quoted, unquoted value otherwise
"""
if '"' in text:
return text.split('"')[1]
elif "'" in text:
Expand Down Expand Up @@ -2555,11 +2619,11 @@ def read_ExtPtfm(self, ep_file):
self.fst_vt['ExtPtfm']['Red_FileName'] = os.path.join(os.path.dirname(ep_file), quoted_read(f.readline().split()[0]))
self.fst_vt['ExtPtfm']['RedCst_FileName'] = os.path.join(os.path.dirname(ep_file), quoted_read(f.readline().split()[0]))
self.fst_vt['ExtPtfm']['NActiveDOFList'] = int_read(f.readline().split()[0])
self.fst_vt['ExtPtfm']['ActiveDOFList'] = [idx.strip() for idx in f.readline().split('ActiveDOFList')[0].split(',')]
self.fst_vt['ExtPtfm']['ActiveDOFList'] = read_array(f,None,split_val='ActiveDOFList',array_type=int)
self.fst_vt['ExtPtfm']['NInitPosList'] = int_read(f.readline().split()[0])
self.fst_vt['ExtPtfm']['InitPosList'] = [idx.strip() for idx in f.readline().split('InitPosList')[0].split(',')]
self.fst_vt['ExtPtfm']['InitPosList'] = read_array(f,None,split_val='InitPosList',array_type=float)
self.fst_vt['ExtPtfm']['NInitVelList'] = int_read(f.readline().split()[0])
self.fst_vt['ExtPtfm']['InitVelList'] = [idx.strip() for idx in f.readline().split('InitVelList')[0].split(',')]
self.fst_vt['ExtPtfm']['InitVelList'] = read_array(f,None,split_val='InitVelList',array_type=float)
f.readline()

# Output
Expand Down
56 changes: 39 additions & 17 deletions openfast_io/openfast_io/FAST_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@


def auto_format(f, var):
# Error handling for variables with 'Default' options
"""
Error handling for variables with 'Default' options
args:
f: file object
var: variable to write to file
"""
if isinstance(var, str):
f.write('{:}\n'.format(var))
elif isinstance(var, int):
Expand All @@ -24,21 +31,46 @@ def auto_format(f, var):
f.write('{: 2.15e}\n'.format(var))

def float_default_out(val):
# formatted float output when 'default' is an option
"""
Formatted float output when 'default' is an option
args:
val: value to be formatted
returns:
formatted value
"""
if type(val) is float:
return '{: 22f}'.format(val)
else:
return '{:<22}'.format(val)

def int_default_out(val):
# formatted int output when 'default' is an option
"""
Formatted int output when 'default' is an option
args:
val: value to be formatted
returns:
formatted value
"""
if type(val) is float:
return '{:<22d}'.format(val)
else:
return '{:<22}'.format(val)

# given a list of nested dictionary keys, return the dict at that point
def get_dict(vartree, branch):
"""
Given a list of nested dictionary keys, return the dictionary at that point
args:
vartree: dictionary to search
branch: list of keys to search
returns:
dictionary at the specified branch
"""
return reduce(operator.getitem, branch, vartree)

class InputWriter_OpenFAST(object):
Expand Down Expand Up @@ -2102,21 +2134,11 @@ def write_ExtPtfm(self):
f.write('{!s:<22} {:<11} {:}'.format(self.fst_vt['ExtPtfm']['Red_FileName'], 'Red_FileName', '- Path of the file containing Guyan/Craig-Bampton inputs (-)\n'))
f.write('{!s:<22} {:<11} {:}'.format(self.fst_vt['ExtPtfm']['RedCst_FileName'], 'RedCst_FileName', '- Path of the file containing Guyan/Craig-Bampton constant inputs (-) (currently unused)\n'))
f.write('{:<22d} {:<11} {:}'.format(self.fst_vt['ExtPtfm']['NActiveDOFList'], 'NActiveDOFList', '- Number of active CB mode listed in ActiveDOFList, use -1 for all modes (integer)\n'))
# f.write('{:<22} {:<11} {:}'.format(", ".join([str(i) for i in self.fst_vt['ExtPtfm']['ActiveDOFList']]), 'ActiveDOFList', '- List of CB modes index that are active, [unused if NActiveDOFList<=0]\n'))
if self.fst_vt['ExtPtfm']['NActiveDOFList'] > -1:
f.write('{:<22} {:<11} {:}'.format(', '.join(self.fst_vt['ExtPtfm']['ActiveDOFList'][:self.fst_vt['ExtPtfm']['NActiveDOFList']]), 'ActiveDOFList', '- List of CB modes index that are active, [unused if NActiveDOFList<=0]\n'))
else:
f.write('{:<22} {:<11} {:}'.format(', '.join(self.fst_vt['ExtPtfm']['ActiveDOFList']), 'ActiveDOFList', '- List of CB modes index that are active, [unused if NActiveDOFList<=0]\n'))
f.write('{:<22} {:<11} {:}'.format(', '.join([f'{val}' for val in self.fst_vt['ExtPtfm']['ActiveDOFList']]), 'ActiveDOFList', '- List of CB modes index that are active, [unused if NActiveDOFList<=0]\n'))
f.write('{:<22d} {:<11} {:}'.format(self.fst_vt['ExtPtfm']['NInitPosList'], 'NInitPosList', '- Number of initial positions listed in InitPosList, using 0 implies all DOF initialized to 0 (integer)\n'))
if self.fst_vt['ExtPtfm']['NInitPosList'] > 0:
f.write('{:<22} {:<11} {:}'.format(', '.join(self.fst_vt['ExtPtfm']['InitPosList'][:self.fst_vt['ExtPtfm']['NInitPosList']]), 'InitPosList', '- List of initial positions for the CB modes [unused if NInitPosList<=0 or EquilStart=True]\n'))
else:
f.write('{:<22d} {:<11} {:}'.format(0, 'InitPosList', '- List of initial positions for the CB modes [unused if NInitPosList<=0 or EquilStart=True]\n'))
f.write('{:<22} {:<11} {:}'.format(', '.join([f'{val}' for val in self.fst_vt['ExtPtfm']['InitPosList']]), 'InitPosList', '- List of initial positions for the CB modes [unused if NInitPosList<=0 or EquilStart=True]\n'))
f.write('{:<22d} {:<11} {:}'.format(self.fst_vt['ExtPtfm']['NInitVelList'], 'NInitVelList', '- Number of initial positions listed in InitVelList, using 0 implies all DOF initialized to 0 (integer)\n'))
if self.fst_vt['ExtPtfm']['NInitVelList'] > 0:
f.write('{:<22} {:<11} {:}'.format(', '.join(self.fst_vt['ExtPtfm']['InitVelList'][:self.fst_vt['ExtPtfm']['NInitVelList']]), 'InitVelList', '- List of initial velocities for the CB modes [unused if NInitVelPosList<=0 or EquilStart=True]\n'))
else:
f.write('{:<22d} {:<11} {:}'.format(0, 'InitVelList', '- List of initial velocities for the CB modes [unused if NInitVelPosList<=0 or EquilStart=True]\n'))
f.write('{:<22} {:<11} {:}'.format(', '.join([f'{val}' for val in self.fst_vt['ExtPtfm']['InitVelList']]), 'InitVelList', '- List of initial velocities for the CB modes [unused if NInitVelPosList<=0 or EquilStart=True]\n'))

f.write('---------------------- OUTPUT --------------------------------------------------\n')
f.write('{!s:<22} {:<11} {:}'.format(self.fst_vt['ExtPtfm']['SumPrint'], 'SumPrint', '- Print summary data to <RootName>.sum (flag)\n'))
Expand Down
Loading

0 comments on commit 7ba57ed

Please sign in to comment.