Files
MeltMind/tools/moldflow_time_map.py

114 lines
6.6 KiB
Python

"""Read-only LOG diagnostics under tolerances fixed before numeric rows were read."""
import math
import re
FIELDS = ('time_s', 'fill_percent', 'injection_pressure_Pa', 'clamp_force_N',
'flow_m3_s', 'frozen_percent')
def parse(raw):
# LF is the archive's physical line delimiter; retain raw CR bytes in raw files.
lines = raw.decode('utf-8', errors='strict').split('\n')
rows, headers, units, unsupported, mentions, transitions = [], [], [], [], [], []
active = False
for number, original in enumerate(lines, 1):
text = original.rstrip('\r').strip()
if re.search(r'V/P|velocity/pressure|switch.over', text, re.I):
mentions.append(dict(line=number, text=text))
if '|' in text and 'Time' in text:
active = 'Fill Vol' in text and 'Flow Rate' in text
if active:
headers.append(dict(line=number, text=text))
continue
if text.startswith('End of filling') or text.startswith('Pack Analysis'):
active = False
if not active or not text.startswith('|'):
continue
columns = [v.strip() for v in text.strip('|').split('|')]
if '(s)' in text:
units.append(dict(line=number, text=text))
continue
if set(text) <= set('|-+ '):
continue
try:
if len(columns) != 7:
raise ValueError('expected seven table columns')
values = [float(v) for v in columns[:6]]
if not all(math.isfinite(v) for v in values):
raise ValueError('nonfinite printed value')
except ValueError as exc:
unsupported.append(dict(line=number, text=text, reason=str(exc)))
continue
row = dict(zip(FIELDS, values), line=number, status=columns[6],
printed_columns=columns, raw_line=original.rstrip('\r'))
rows.append(row)
if len(rows) == 1 or rows[-2]['status'] != row['status']:
transitions.append(dict(line=number, time_s=row['time_s'],
fill_percent=row['fill_percent'], status=row['status']))
expected_units = ('(s)', '(%)', '(Pa)', '(N)', '(m^3/s)', 'Vol (%)')
units_supported = bool(units) and all(all(x in u['text'] for x in expected_units) for u in units)
return dict(table_present=bool(headers), headers=headers, units=units,
units_supported=units_supported, rows=rows, unsupported_rows=unsupported,
status_transitions=transitions, switch_mentions=mentions,
physical_line_count=len(lines))
def flow_window(rows, tolerance):
if len(rows) < 2 or rows[-1]['time_s'] <= rows[0]['time_s']:
return dict(defined=False, reason='insufficient_time_span', rows=len(rows))
span = rows[-1]['time_s'] - rows[0]['time_s']
integral = sum((b['time_s']-a['time_s'])*(a['flow_m3_s']+b['flow_m3_s'])/2
for a, b in zip(rows, rows[1:]))
mean = integral/span
common = dict(rows=len(rows), first_time_s=rows[0]['time_s'], last_time_s=rows[-1]['time_s'],
first_fill_percent=rows[0]['fill_percent'], last_fill_percent=rows[-1]['fill_percent'],
printed_flow_time_weighted_mean=mean,
all_printed_flow_zero=all(r['flow_m3_s'] == 0 for r in rows))
if mean == 0:
return dict(common, defined=False, reason='zero_printed_mean_cannot_assess_relative_flatness')
departure = max(abs(r['flow_m3_s']-mean) for r in rows)/abs(mean)
return dict(common, defined=True, max_relative_departure=departure,
within_fixed_tolerance=departure <= tolerance)
def diagnose(parsed, contract):
rows = parsed['rows']
if not parsed['table_present']:
return dict(status='NO_MATCHING_TABLE_HEADER', quantitative_defined=False)
if parsed['unsupported_rows'] or not parsed['units_supported'] or len(rows) < 2:
return dict(status='TABLE_PRESENT_BUT_UNSUPPORTED_OR_INSUFFICIENT', quantitative_defined=False)
epsilon = contract['monotonicity_numeric_tolerance']
end = next((i for i, r in enumerate(rows) if r['fill_percent'] >= 100-epsilon), None)
fill = rows[:end+1] if end is not None else rows
violations = [dict(left_line=a['line'], right_line=b['line']) for a,b in zip(fill,fill[1:])
if b['time_s'] < a['time_s']-epsilon or b['fill_percent'] < a['fill_percent']-epsilon]
if violations or any(r['fill_percent'] < -epsilon or r['fill_percent'] > 100+epsilon or r['time_s'] < 0 for r in fill):
return dict(status='NONMONOTONE_OR_OUT_OF_RANGE', quantitative_defined=False, violations=violations)
first, last = fill[0], fill[-1]
span = last['time_s']-first['time_s']
if span <= 0:
return dict(status='INSUFFICIENT_TIME_SPAN', quantitative_defined=False)
residuals = [r['fill_percent']-(first['fill_percent']+(last['fill_percent']-first['fill_percent'])*
(r['time_s']-first['time_s'])/span) for r in fill]
maximum = max(map(abs,residuals))
switch = [dict(line=r['line'], time_s=r['time_s'], fill_percent=r['fill_percent'], status=r['status'])
for r in fill if re.search(r'V\s*/\s*P|switch.over',r['status'],re.I)]
pre = [r for r in fill if switch and r['line'] < switch[0]['line']]
supplement = None
if end is not None and last['time_s'] > 0:
supp = max(abs(r['fill_percent']-100*r['time_s']/last['time_s']) for r in fill)
supplement = dict(assumed_origin=[0,0], max_deviation_percentage_points=supp,
within_fixed_tolerance=supp <= contract['linearity_tolerance_percentage_points'])
return dict(status='PARSED', quantitative_defined=True, filling_rows=len(fill),
first_row=first, last_row=last, reached_printed_100_percent=end is not None,
equal_printed_time_intervals=sum(a['time_s']==b['time_s'] for a,b in zip(fill,fill[1:])),
observed_switch_rows=switch, no_switch_time_inferred_from_settings=True,
linearity=dict(max_deviation_percentage_points=maximum,
within_fixed_tolerance=maximum <= contract['linearity_tolerance_percentage_points'],
endpoint_residuals_pp=residuals, assumed_origin_supplement=supplement),
flow=dict(whole_fill=flow_window(fill,contract['flow_flatness_relative_tolerance']),
fixed_5_to_95_percent=flow_window([r for r in fill if 5<=r['fill_percent']<=95],contract['flow_flatness_relative_tolerance']),
before_explicit_VP=flow_window(pre,contract['flow_flatness_relative_tolerance']) if switch else
dict(defined=False,reason='no_explicit_switch_row')))