#!/usr/bin/env python3 from __future__ import annotations import math import re from collections import deque from pathlib import Path ROOT = Path("/home/user/Desktop") M = 4.0 N_EQ = 50.0 EXPECTED = (4.104952, 0.225078, 0.571267) 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 reversals(series: list[float]) -> list[float]: if not series: raise ValueError("empty torque series") cleaned = [series[0]] for value in series[1:]: if value != cleaned[-1]: cleaned.append(value) if len(cleaned) == 1: return cleaned points = [cleaned[0]] for index in range(1, len(cleaned) - 1): left = cleaned[index] - cleaned[index - 1] right = cleaned[index + 1] - cleaned[index] if left * right <= 0.0: points.append(cleaned[index]) points.append(cleaned[-1]) return points def rainflow_ranges( series: list[float], ) -> list[tuple[float, float]]: stack: deque[float] = deque() cycles: list[tuple[float, float]] = [] for point in reversals(series): stack.append(point) while len(stack) >= 3: newer = abs(stack[-1] - stack[-2]) older = abs(stack[-2] - stack[-3]) if newer < older: break if len(stack) == 3: cycles.append((older, 0.5)) stack.popleft() else: cycles.append((older, 1.0)) last = stack.pop() stack.pop() stack.pop() stack.append(last) while len(stack) > 1: first = stack.popleft() cycles.append((abs(stack[0] - first), 0.5)) return cycles 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") wind_i = headers.index("Wind1VelX") speed_i = headers.index("RotSpeed") torque_i = headers.index("RotTorq") if ( units[time_i] != "(s)" or units[wind_i] != "(m/s)" or units[speed_i] != "(rpm)" or units[torque_i] != "(kN-m)" ): 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) != 601: 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") wind_values = [row[wind_i] for row in rows] wind_mean = sum(wind_values) / len(wind_values) wind_std = math.sqrt( sum((value - wind_mean) ** 2 for value in wind_values) / len(wind_values) ) if wind_std < 0.5: raise ValueError("native output does not contain turbulent wind") torque_mnm = [row[torque_i] / 1000.0 for row in rows[100:]] if len(torque_mnm) != 501: raise ValueError("incorrect 10-to-60-second sample count") mean = sum(torque_mnm) / len(torque_mnm) std = math.sqrt( sum((value - mean) ** 2 for value in torque_mnm) / len(torque_mnm) ) cycles = rainflow_ranges(torque_mnm) if not cycles or not any(count == 1.0 for _, count in cycles): raise ValueError("rainflow counting produced no closed cycles") if not any(count == 0.5 for _, count in cycles): raise ValueError("rainflow counting produced no residual cycles") damage_sum = sum( count * load_range**M for load_range, count in cycles ) damage_equivalent = (damage_sum / N_EQ) ** (1.0 / M) return mean, std, damage_equivalent 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", "elasto_drivetrain.dat", "aero_drivetrain.dat", "servo_drivetrain.dat", "drivetrain_case.fst", "drivetrain_case.out", "drivetrain_case.log", "summary.txt", "5MW_Baseline/NRELOffshrBsline5MW_InflowWind_12mps.dat", "5MW_Baseline/Wind/90m_12mps_twr.bts", "5MW_Baseline/ServoData/DISCON.dll", ) 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_12mps.dat" ) if ( parameter(inflow, "WindType") != "3" or parameter(inflow, "FileName_BTS") != "Wind/90m_12mps_twr.bts" ): return False elasto = read(ROOT / "elasto_drivetrain.dat") if ( parameter(elasto, "DrTrDOF") != "True" or parameter(elasto, "GenDOF") != "True" or '"RotTorq"' not in elasto or '"RotSpeed"' not in elasto or '"../5MW_Baseline/' in elasto ): return False aero = read(ROOT / "aero_drivetrain.dat") servo = read(ROOT / "servo_drivetrain.dat") if '"../5MW_Baseline/' in aero or '"../5MW_Baseline/' in servo: return False if ( parameter(servo, "DLL_FileName") != "5MW_Baseline/ServoData/DISCON.dll" ): return False main = read(ROOT / "drivetrain_case.fst") expected_main = { "TMax": "60.0", "DT_Out": "0.1", "OutFileFmt": "1", "EDFile": "elasto_drivetrain.dat", "InflowFile": ( "5MW_Baseline/" "NRELOffshrBsline5MW_InflowWind_12mps.dat" ), "AeroFile": "aero_drivetrain.dat", "ServoFile": "servo_drivetrain.dat", } if any( parameter(main, name) != value for name, value in expected_main.items() ): return False log = read(ROOT / "drivetrain_case.log") if ( "OpenFAST-v5.0.0" not in log or "OpenFAST terminated normally." not in log or "Simulated Time: 60 seconds" not in log or "FATAL ERROR" in log ): return False calculated = output_metrics(ROOT / "drivetrain_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())