#!/usr/bin/env python3 from __future__ import annotations import math import re from pathlib import Path ROOT = Path("/home/user/Desktop") EXPECTED = (0.0, 39.9, 0.071472) 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, 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") power_i = headers.index("GenPwr") speed_i = headers.index("RotSpeed") pitch_indices = [ headers.index(f"BldPitch{blade}") for blade in (1, 2, 3) ] if ( units[time_i] != "(s)" or units[power_i] != "(kW)" or units[speed_i] != "(rpm)" or any(units[index] != "(deg)" for index in pitch_indices) ): 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) != 901: 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") shutdown_rows = rows[200:] if abs(rows[199][power_i]) < 100.0: raise ValueError("generator was already disconnected before 20 seconds") power_time = next( row[time_i] - 20.0 for row in shutdown_rows if abs(row[power_i]) < 100.0 ) feather_time = next( row[time_i] - 20.0 for row in shutdown_rows if all(row[index] >= 89.9 for index in pitch_indices) ) for row in shutdown_rows: pitches = [row[index] for index in pitch_indices] if max(pitches) - min(pitches) > 2.0e-6: raise ValueError("blade pitch trajectories are not collective") if any( not close(rows[-1][index], 90.0, rel_tol=0.0, abs_tol=2.0e-6) for index in pitch_indices ): raise ValueError("final blade pitch is not 90 degrees") return power_time, feather_time, abs(rows[-1][speed_i]) def parse_summary(path: Path) -> tuple[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) != 3 or not all(math.isfinite(value) for value in values): raise ValueError("summary must contain three finite values") return values def check() -> bool: required = ( "build_case.py", "postprocess.py", "inflow_shutdown.dat", "elasto_shutdown.dat", "aero_shutdown.dat", "servo_shutdown.dat", "shutdown_case.fst", "shutdown_case.out", "shutdown_case.log", "summary.txt", ) if any( not (ROOT / name).is_file() or (ROOT / name).stat().st_size == 0 for name in required ): return False inflow = read(ROOT / "inflow_shutdown.dat") expected_inflow = { "WindType": "1", "HWindSpeed": "15.0", } if any( parameter(inflow, name) != value for name, value in expected_inflow.items() ): return False elasto = read(ROOT / "elasto_shutdown.dat") if parameter(elasto, "RotSpeed") != "12.1": return False if '"../5MW_Baseline/' in elasto: return False if any( f'"{channel}"' not in elasto for channel in ("BldPitch1", "BldPitch2", "BldPitch3", "RotSpeed") ): return False if '"../5MW_Baseline/' in read(ROOT / "aero_shutdown.dat"): return False servo = read(ROOT / "servo_shutdown.dat") expected_servo = { "GenTiStp": "True", "TimGenOf": "20.0", "HSSBrMode": "0", "TPitManS(1)": "20.0", "TPitManS(2)": "20.0", "TPitManS(3)": "20.0", "PitManRat(1)": "2.0", "PitManRat(2)": "2.0", "PitManRat(3)": "2.0", "BlPitchF(1)": "90.0", "BlPitchF(2)": "90.0", "BlPitchF(3)": "90.0", } if any( parameter(servo, name) != value for name, value in expected_servo.items() ): return False if '"GenPwr"' not in servo or '"../5MW_Baseline/' in servo: return False main = read(ROOT / "shutdown_case.fst") expected_main = { "TMax": "90.0", "DT_Out": "0.1", "OutFileFmt": "1", "EDFile": "elasto_shutdown.dat", "InflowFile": "inflow_shutdown.dat", "AeroFile": "aero_shutdown.dat", "ServoFile": "servo_shutdown.dat", } if any( parameter(main, name) != value for name, value in expected_main.items() ): return False log = read(ROOT / "shutdown_case.log") if ( "OpenFAST-v5.0.0" not in log or "OpenFAST terminated normally." not in log or "Simulated Time: 90 seconds" not in log or "FATAL ERROR" in log ): return False calculated = output_metrics(ROOT / "shutdown_case.out") reported = parse_summary(ROOT / "summary.txt") return ( 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())