HongchengGao's picture
Align task taxonomy and IDs with the updated paper; main evaluation 306
3de3b50 verified
Raw History Blame Contribute Delete
4.3 kB
#!/usr/bin/env python3
from __future__ import annotations
import math
import re
from pathlib import Path
ROOT = Path("/home/user/Desktop")
EXPECTED = (-58.316872, -641.020889)
REL_TOL = 2e-3
ABS_TOL = 2e-4
def read(path: Path) -> str:
return path.read_text(encoding="utf-8", errors="ignore")
def close(actual: float, expected: float) -> bool:
return math.isfinite(actual) and abs(actual - expected) <= max(
ABS_TOL, REL_TOL * max(1.0, abs(expected))
)
def parameter(text: str, name: str) -> str:
match = re.search(rf"^\s*(\S+).*?\s{re.escape(name)}\s+-", text, re.MULTILINE)
if not match:
raise ValueError(f"missing parameter {name}")
return match.group(1).strip('"').rstrip(",")
def calculate(path: Path) -> tuple[float, float]:
lines = read(path).splitlines()
if "at commit v5.0.0" not in "\n".join(lines[:5]):
raise ValueError("output was not generated by OpenFAST 5.0.0")
header_index = next(i for i, line in enumerate(lines) if line.split()[:1] == ["Time"])
headers = lines[header_index].split()
units = lines[header_index + 1].split()
time_i = headers.index("Time")
force_i = headers.index("ReactFXss")
moment_i = headers.index("ReactMYss")
if units[force_i] != "(N)" or units[moment_i] != "(N*m)":
raise ValueError("unexpected SubDyn reaction units")
all_rows: list[list[float]] = []
for line in lines[header_index + 2 :]:
fields = line.split()
if len(fields) != len(headers):
continue
try:
all_rows.append([float(value) for value in fields])
except ValueError:
continue
if not all_rows or not close(all_rows[-1][time_i], 30.0):
raise ValueError("incomplete 30-second output")
rows = [row for row in all_rows if row[time_i] >= 20.0]
if len(rows) < 90:
raise ValueError("insufficient final-window samples")
return (
sum(row[moment_i] for row in rows) / len(rows) / 1.0e6,
sum(row[force_i] for row in rows) / len(rows) / 1000.0,
)
def parse_summary(path: Path) -> tuple[float, float]:
rows = [line.strip() for line in read(path).splitlines() if line.strip()]
if len(rows) != 1:
raise ValueError("summary must contain one row")
values = tuple(float(value.strip()) for value in rows[0].split(","))
if len(values) != 2:
raise ValueError("summary must contain two values")
return values
def check() -> bool:
required = (
"build_case.py",
"postprocess.py",
"subdyn.dat",
"elasto_subdyn.dat",
"aero_subdyn.dat",
"servo_subdyn.dat",
"subdyn_case.fst",
"subdyn_case.out",
"subdyn_case.log",
"summary.txt",
)
if any(not (ROOT / name).is_file() or (ROOT / name).stat().st_size == 0 for name in required):
return False
if "OpenFAST terminated normally." not in read(ROOT / "subdyn_case.log"):
return False
subdyn = read(ROOT / "subdyn.dat")
if '"ReactFXss"' not in subdyn or '"ReactMYss"' not in subdyn:
return False
if not re.search(r"(?m)^\s*1\s+1\s+1\s+1\s+1\s+1\s+1\s+\"\"\s*$", subdyn):
return False
fst = read(ROOT / "subdyn_case.fst")
expected_main = {
"TMax": "30.0",
"CompSeaSt": "0",
"CompHydro": "0",
"CompSub": "1",
"DT_Out": "0.1",
"OutFileFmt": "1",
"EDFile": "elasto_subdyn.dat",
"AeroFile": "aero_subdyn.dat",
"ServoFile": "servo_subdyn.dat",
"SubFile": "subdyn.dat",
}
if any(parameter(fst, name) != value for name, value in expected_main.items()):
return False
for name in ("elasto_subdyn.dat", "aero_subdyn.dat", "servo_subdyn.dat"):
if '"../5MW_Baseline/' in read(ROOT / name):
return False
calculated = calculate(ROOT / "subdyn_case.out")
reported = parse_summary(ROOT / "summary.txt")
return all(close(a, e) for a, e in zip(calculated, EXPECTED)) and all(
close(a, e) for a, e in zip(reported, EXPECTED)
)
def main() -> int:
try:
result = check()
except Exception:
result = False
print("True" if result else "False")
return 0
if __name__ == "__main__":
raise SystemExit(main())