File size: 10,156 Bytes
5154c4e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | #!/usr/bin/env python3
from __future__ import annotations
import os
import math
import re
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
FLOAT_RE = re.compile(r"(?<![A-Za-z0-9_])[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?(?![A-Za-z0-9_])")
REL_TOL = 1e-4
ABS_TOL = 1e-3
REQUIRED_FILES = ['fixed_summary.txt', 'diagnosis.txt', 'postprocess.py',
'fixed.fst', 'fixed_elasto.dat', 'fixed_inflow.dat', 'fixed.out',
'diagnostic_1.log', 'diagnostic_2.log']
EXPECTED_ROWS = [[4866.598858, 5139.093750]]
SUMMARY_FILE = 'fixed_summary.txt'
def parse_floats(text: str) -> list[float]:
values: list[float] = []
for token in FLOAT_RE.findall(text):
try:
values.append(float(token))
except ValueError:
continue
return values
def parse_numeric_rows(path: Path) -> list[list[float]]:
rows: list[list[float]] = []
for raw in path.read_text(encoding='utf-8', errors='ignore').splitlines():
line = raw.strip()
if (not line) or line.startswith('#'):
continue
vals = parse_floats(line)
if vals:
rows.append(vals)
return rows
def file_ok(path: Path) -> bool:
return path.exists() and path.is_file() and path.stat().st_size > 0
def close_enough(actual: float, expected: float) -> bool:
return abs(actual - expected) <= max(ABS_TOL, REL_TOL * max(1.0, abs(expected)))
def output_stats(path: Path) -> tuple[float, float]:
lines = path.read_text(encoding='utf-8', errors='ignore').splitlines()
header = next(i for i, line in enumerate(lines) if line.split() and line.split()[0] == 'Time')
names = lines[header].split()
time_i, power_i = names.index('Time'), names.index('GenPwr')
rows = []
for line in lines[header + 2:]:
if not line.split():
continue
values = [float(value) for value in line.split()]
if len(values) != len(names) or not all(math.isfinite(value) for value in values):
raise ValueError('malformed OpenFAST output row')
rows.append(values)
times = [row[time_i] for row in rows]
if (len(rows) < 1000 or times[0] > 0.01 or times[-1] < 59.9 or
any(second <= first for first, second in zip(times, times[1:]))):
raise ValueError('incomplete OpenFAST output')
power = [row[power_i] for row in rows]
return sum(power) / len(power), max(power)
def openfast_fields(path: Path) -> dict[str, str]:
fields: dict[str, str] = {}
pattern = re.compile(r'^\s*(.*?)\s+([A-Za-z][A-Za-z0-9_]*(?:\(\d+\))?)\s+-')
for raw_line in path.read_text(encoding='utf-8', errors='ignore').splitlines():
match = pattern.match(raw_line)
if not match:
continue
value = re.sub(r'\s+', ' ', match.group(1)).strip()
name = match.group(2)
if value and name not in fields:
fields[name] = value
return fields
def unquoted(value: str) -> str:
value = str(value or '').strip()
if len(value) >= 2 and value[0] == value[-1] == '"':
value = value[1:-1]
return value
def only_fields_changed(broken: Path, fixed: Path, changes: dict[str, tuple[str, str]]) -> bool:
before = openfast_fields(broken)
after = openfast_fields(fixed)
if not before or before.keys() != after.keys():
return False
for field, (old, new) in changes.items():
if unquoted(before.get(field, '')) != old or unquoted(after.get(field, '')) != new:
return False
return all(before[field] == after[field] for field in before if field not in changes)
def diagnostics_valid(root: Path) -> bool:
first = (root / 'diagnostic_1.log').read_bytes().decode('utf-8', errors='ignore').replace('\x00', '')
second = (root / 'diagnostic_2.log').read_bytes().decode('utf-8', errors='ignore').replace('\x00', '')
common = ('OpenFAST-v', 'FAST_InitializeAll:', 'FATAL ERROR', 'Aborting OpenFAST')
if not all(token in first and token in second for token in common):
return False
if not ('ReadBladeFile' in first and 'input file' in first and 'not found' in first):
return False
if 'Running InflowWind' in first or 'Invalid WindType' in first:
return False
if not ('Running InflowWind' in second and 'Invalid WindType' in second and 'supported' in second):
return False
return True
def generated_summary(root: Path, expected_stats: tuple[float, float]) -> tuple[float, float] | None:
invocations = [[], ['fixed.out'], ['fixed.out', 'fixed_summary.txt']]
def transformed_output(path: Path) -> str:
lines = path.read_text(encoding='utf-8', errors='ignore').splitlines()
header = next(i for i, line in enumerate(lines) if line.split() and line.split()[0] == 'Time')
power_index = lines[header].split().index('GenPwr')
for index in range(header + 2, len(lines)):
tokens = lines[index].split()
if not tokens:
continue
tokens[power_index] = format(float(tokens[power_index]) * 0.37 + 123.45, '.12g')
lines[index] = '\t'.join(tokens)
return '\n'.join(lines) + '\n'
with tempfile.TemporaryDirectory(prefix='engiworld_openfast_post_') as temp_root:
base = Path(temp_root)
transformed = base / 'transformed.out'
transformed.write_text(transformed_output(root / 'fixed.out'), encoding='utf-8')
probes = [
(root / 'fixed.out', expected_stats),
(transformed, (expected_stats[0] * 0.37 + 123.45,
expected_stats[1] * 0.37 + 123.45)),
]
first_result = None
for probe_index, (source, expected) in enumerate(probes):
probe_passed = False
for invocation_index, arguments in enumerate(invocations):
work = base / ('probe_%d_%d' % (probe_index, invocation_index))
work.mkdir()
shutil.copy2(root / 'postprocess.py', work / 'postprocess.py')
shutil.copy2(source, work / 'fixed.out')
try:
completed = subprocess.run(
[sys.executable, 'postprocess.py', *arguments],
cwd=str(work),
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
timeout=30,
check=False,
)
except (OSError, subprocess.TimeoutExpired):
continue
summary = work / SUMMARY_FILE
if completed.returncode != 0 or not file_ok(summary):
continue
rows = parse_numeric_rows(summary)
if (len(rows) == 1 and len(rows[0]) == 2 and
all(close_enough(actual, wanted) for actual, wanted in zip(rows[0], expected))):
probe_passed = True
if probe_index == 0:
first_result = (rows[0][0], rows[0][1])
break
if not probe_passed:
return None
return first_result
def check_task(root: Path) -> bool:
for rel in REQUIRED_FILES:
if not file_ok(root / rel):
return False
actual = parse_numeric_rows(root / SUMMARY_FILE)
if len(actual) != len(EXPECTED_ROWS):
return False
for row_a, row_e in zip(actual, EXPECTED_ROWS):
if len(row_a) != len(row_e):
return False
for a, e in zip(row_a, row_e):
if not close_enough(a, e):
return False
if not only_fields_changed(root / 'broken_elasto.dat', root / 'fixed_elasto.dat',
{'NumBl': ('4', '3')}):
return False
if not only_fields_changed(root / 'broken_inflow.dat', root / 'fixed_inflow.dat',
{'WindType': ('99', '3')}):
return False
if not only_fields_changed(root / 'broken.fst', root / 'fixed.fst',
{'EDFile': ('broken_elasto.dat', 'fixed_elasto.dat'),
'InflowFile': ('broken_inflow.dat', 'fixed_inflow.dat')}):
return False
diagnosis = (root / 'diagnosis.txt').read_text(encoding='utf-8', errors='ignore')
words = re.findall(r"[A-Za-z0-9_]+", diagnosis)
diagnosis_lower = diagnosis.lower()
if (len(words) < 100 or not all(token in diagnosis for token in ('NumBl', 'WindType')) or
not all(re.search(pattern, diagnosis_lower) for pattern in
(r'\b4\b', r'\b3\b', r'\b99\b', r'not\s+found|nonexistent|unreadable',
r'invalid', r'correct'))):
return False
fixed_fields = openfast_fields(root / 'fixed.fst')
if (unquoted(fixed_fields.get('EDFile', '')) != 'fixed_elasto.dat' or
unquoted(fixed_fields.get('InflowFile', '')) != 'fixed_inflow.dat' or
unquoted(fixed_fields.get('TMax', '')) not in {'60', '60.0'}):
return False
if not diagnostics_valid(root):
return False
logs = [path for path in root.glob('*.log') if path.is_file()]
normal_logs = [path for path in logs if
'OpenFAST terminated normally' in path.read_text(errors='ignore') and
'FATAL ERROR' not in path.read_text(errors='ignore')]
if not normal_logs:
return False
mean, maximum = output_stats(root / 'fixed.out')
if not close_enough(mean, actual[0][0]) or not close_enough(maximum, actual[0][1]):
return False
regenerated = generated_summary(root, (mean, maximum))
if regenerated is None:
return False
return True
def evaluate() -> int:
root = Path(os.environ.get('EVAL_OUTPUT_ROOT', '/home/user/Desktop'))
try:
ok = check_task(root)
except Exception:
ok = False
return 1 if ok else 0
def main() -> int:
result = evaluate()
print('True' if result == 1 else 'False')
return 0
if __name__ == '__main__':
raise SystemExit(main())
|