Download task/single-software-execution/cli/openfast/task-16/eval.py from HongchengGao/EngiWorld-Tasks: direct link, hf CLI and curl.
- Browser
- Download file 8.13 kB
-
https://huggingface.co/datasets/HongchengGao/EngiWorld-Tasks/resolve/main/task/single-software-execution/cli/openfast/task-16/eval.py
- Command line
-
hf download hf://datasets/HongchengGao/EngiWorld-Tasks/task/single-software-execution/cli/openfast/task-16/eval.py
-
curl -L -o eval.py https://huggingface.co/datasets/HongchengGao/EngiWorld-Tasks/resolve/main/task/single-software-execution/cli/openfast/task-16/eval.py
8.13 kB
| #!/usr/bin/env python3 | |
| from __future__ import annotations | |
| import cmath | |
| import math | |
| import re | |
| from pathlib import Path | |
| ROOT = Path("/home/user/Desktop") | |
| ONE_P_HZ = 9.0 / 60.0 | |
| EXPECTED = ( | |
| 12.214448, | |
| 88.000774, | |
| 7.204646, | |
| 0.230884, | |
| 1.874851, | |
| ) | |
| REL_TOL = 2.0e-3 | |
| ABS_TOL = 2.0e-5 | |
| def read(path: Path) -> str: | |
| return path.read_text(encoding="utf-8", errors="ignore") | |
| def close( | |
| actual: float, | |
| expected: float, | |
| rel_tol: float = REL_TOL, | |
| abs_tol: float = ABS_TOL, | |
| ) -> 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 output_metrics(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( | |
| index | |
| for index, 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") | |
| wind_i = headers.index("Wind1VelX") | |
| speed_i = headers.index("RotSpeed") | |
| power_i = headers.index("RtAeroPwr") | |
| if ( | |
| units[time_i] != "(s)" | |
| or units[wind_i] != "(m/s)" | |
| or units[speed_i] != "(rpm)" | |
| or units[power_i] != "(W)" | |
| ): | |
| raise ValueError("unexpected native OpenFAST units") | |
| rows: list[list[float]] = [] | |
| for line in lines[header_index + 2 :]: | |
| fields = line.split() | |
| if len(fields) != len(headers): | |
| continue | |
| try: | |
| rows.append([float(value) for value in fields]) | |
| except ValueError: | |
| continue | |
| if len(rows) != 301: | |
| raise ValueError("incorrect output sample count") | |
| for index, row in enumerate(rows): | |
| if not close( | |
| row[time_i], | |
| index / 10.0, | |
| rel_tol=0.0, | |
| abs_tol=2.0e-8, | |
| ): | |
| raise ValueError("output timestamps are not 0.1-second samples") | |
| if not close(row[wind_i], 8.0, rel_tol=0.0, abs_tol=2.0e-6): | |
| raise ValueError("output wind speed is not fixed at 8 m/s") | |
| if not close(row[speed_i], 9.0, rel_tol=0.0, abs_tol=2.0e-6): | |
| raise ValueError("rotor speed is not fixed at 9 rpm") | |
| samples = [ | |
| (row[time_i], row[power_i] / 1000.0) | |
| for row in rows[100:] | |
| ] | |
| if len(samples) != 201: | |
| raise ValueError("incorrect 10-to-30-second sample count") | |
| mean = sum(value for _, value in samples) / len(samples) | |
| std = math.sqrt( | |
| sum((value - mean) ** 2 for _, value in samples) | |
| / len(samples) | |
| ) | |
| coefficient = sum( | |
| (value - mean) | |
| * cmath.exp(-2j * math.pi * ONE_P_HZ * time) | |
| for time, value in samples | |
| ) | |
| amplitude = 2.0 * abs(coefficient) / len(samples) | |
| return std, amplitude | |
| def parse_summary( | |
| path: Path, | |
| ) -> tuple[float, float, float, float, float]: | |
| rows = [line.strip() for line in read(path).splitlines() if line.strip()] | |
| if len(rows) != 1: | |
| raise ValueError("summary must contain exactly one row") | |
| values = tuple(float(field.strip()) for field in rows[0].split(",")) | |
| if len(values) != 5 or not all(math.isfinite(value) for value in values): | |
| raise ValueError("summary must contain five finite values") | |
| return values | |
| def normalize_shadow_parameter(text: str) -> str: | |
| updated, count = re.subn( | |
| r"(?m)^\s*\S+(\s+TwrShadow\s+-.*)$", | |
| r"<TwrShadow>\1", | |
| text, | |
| ) | |
| if count != 1: | |
| raise ValueError("expected exactly one TwrShadow parameter") | |
| return updated | |
| def check() -> bool: | |
| required = ( | |
| "build_case.py", | |
| "postprocess.py", | |
| "elasto_shadow.dat", | |
| "aero_notower.dat", | |
| "aero_tower.dat", | |
| "case_notower.fst", | |
| "case_tower.fst", | |
| "case_notower.out", | |
| "case_tower.out", | |
| "case_notower.log", | |
| "case_tower.log", | |
| "summary.txt", | |
| "5MW_Baseline/NRELOffshrBsline5MW_InflowWind_Steady8mps.dat", | |
| ) | |
| if any( | |
| not (ROOT / name).is_file() or (ROOT / name).stat().st_size == 0 | |
| for name in required | |
| ): | |
| return False | |
| inflow = read( | |
| ROOT | |
| / "5MW_Baseline/NRELOffshrBsline5MW_InflowWind_Steady8mps.dat" | |
| ) | |
| if ( | |
| parameter(inflow, "WindType") != "1" | |
| or parameter(inflow, "HWindSpeed") != "8" | |
| ): | |
| return False | |
| elasto = read(ROOT / "elasto_shadow.dat") | |
| rigid_dofs = ( | |
| "FlapDOF1", | |
| "FlapDOF2", | |
| "EdgeDOF", | |
| "PitchDOF", | |
| "TeetDOF", | |
| "DrTrDOF", | |
| "GenDOF", | |
| "YawDOF", | |
| "TwFADOF1", | |
| "TwFADOF2", | |
| "TwSSDOF1", | |
| "TwSSDOF2", | |
| ) | |
| if any(parameter(elasto, name) != "False" for name in rigid_dofs): | |
| return False | |
| if ( | |
| parameter(elasto, "RotSpeed") != "9.0" | |
| or parameter(elasto, "OverHang") != "20.0" | |
| or '"../5MW_Baseline/' in elasto | |
| ): | |
| return False | |
| no_tower_aero = read(ROOT / "aero_notower.dat") | |
| tower_aero = read(ROOT / "aero_tower.dat") | |
| if ( | |
| parameter(no_tower_aero, "TwrShadow") != "0" | |
| or parameter(tower_aero, "TwrShadow") != "1" | |
| or parameter(no_tower_aero, "Wake_Mod") != "1" | |
| or parameter(tower_aero, "Wake_Mod") != "1" | |
| or '"../5MW_Baseline/' in no_tower_aero | |
| or '"../5MW_Baseline/' in tower_aero | |
| or '"RtAeroPwr"' not in no_tower_aero | |
| or '"RtAeroPwr"' not in tower_aero | |
| or normalize_shadow_parameter(no_tower_aero) | |
| != normalize_shadow_parameter(tower_aero) | |
| ): | |
| return False | |
| for label in ("notower", "tower"): | |
| main = read(ROOT / f"case_{label}.fst") | |
| expected_main = { | |
| "TMax": "30.0", | |
| "DT_Out": "0.1", | |
| "OutFileFmt": "1", | |
| "CompInflow": "1", | |
| "CompAero": "2", | |
| "CompServo": "0", | |
| "EDFile": "elasto_shadow.dat", | |
| "InflowFile": ( | |
| "5MW_Baseline/" | |
| "NRELOffshrBsline5MW_InflowWind_Steady8mps.dat" | |
| ), | |
| "AeroFile": f"aero_{label}.dat", | |
| } | |
| if any( | |
| parameter(main, name) != value | |
| for name, value in expected_main.items() | |
| ): | |
| return False | |
| log = read(ROOT / f"case_{label}.log") | |
| if ( | |
| "OpenFAST-v5.0.0" not in log | |
| or "OpenFAST terminated normally." not in log | |
| or "Simulated Time: 30 seconds" not in log | |
| or "FATAL ERROR" in log | |
| or "Tower strike" in log | |
| ): | |
| return False | |
| no_tower_std, no_tower_amp = output_metrics( | |
| ROOT / "case_notower.out" | |
| ) | |
| tower_std, tower_amp = output_metrics(ROOT / "case_tower.out") | |
| calculated = ( | |
| no_tower_std, | |
| tower_std, | |
| tower_std / no_tower_std, | |
| no_tower_amp, | |
| tower_amp, | |
| ) | |
| reported = parse_summary(ROOT / "summary.txt") | |
| return ( | |
| tower_std > 5.0 * no_tower_std | |
| and all( | |
| close(actual, expected) | |
| for actual, expected in zip(calculated, EXPECTED) | |
| ) | |
| and all( | |
| close(actual, expected) | |
| for actual, expected in zip(reported, EXPECTED) | |
| ) | |
| and all( | |
| close( | |
| actual, | |
| calculated_value, | |
| rel_tol=0.0, | |
| abs_tol=2.0e-6, | |
| ) | |
| for actual, calculated_value in zip(reported, calculated) | |
| ) | |
| ) | |
| def main() -> int: | |
| try: | |
| result = check() | |
| except Exception: | |
| result = False | |
| print("True" if result else "False") | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |