File size: 5,381 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
#!/usr/bin/env python3
from __future__ import annotations

import math
import re
from pathlib import Path


ROOT = Path("/home/user/Desktop")
EXPECTED = (4755.905895, 2868.318485, 0.2)
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 read_output(path: Path) -> tuple[list[str], list[str], list[list[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()
    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 not rows or not close(rows[-1][headers.index("Time")], 60.0):
        raise ValueError("incomplete 60-second output")
    return headers, units, rows


def calculate(path: Path) -> tuple[float, float, float]:
    headers, units, rows = read_output(path)
    names = ("Time", "BldPitch1", "BldPitch2", "BldPitch3", "GenPwr")
    indexes = {name: headers.index(name) for name in names}
    if units[indexes["Time"]] != "(s)" or units[indexes["GenPwr"]] != "(kW)":
        raise ValueError("unexpected channel units")

    pre_rows = [row for row in rows if 20.0 <= row[indexes["Time"]] < 30.0]
    post_rows = [row for row in rows if 40.0 <= row[indexes["Time"]] <= 50.0]
    if len(pre_rows) < 90 or len(post_rows) < 90:
        raise ValueError("insufficient averaging samples")
    pre_power = sum(row[indexes["GenPwr"]] for row in pre_rows) / len(pre_rows)
    post_power = sum(row[indexes["GenPwr"]] for row in post_rows) / len(post_rows)
    pre_pitch = sum(
        sum(row[indexes[name]] for name in names[1:4]) / 3.0 for row in pre_rows
    ) / len(pre_rows)
    low = pre_pitch + 0.1 * (10.0 - pre_pitch)
    high = pre_pitch + 0.9 * (10.0 - pre_pitch)
    maneuver = [
        (
            row[indexes["Time"]],
            sum(row[indexes[name]] for name in names[1:4]) / 3.0,
        )
        for row in rows
        if row[indexes["Time"]] >= 30.0
    ]
    t10 = next(time for time, pitch in maneuver if pitch >= low)
    t90 = next(time for time, pitch in maneuver if pitch >= high)
    if any(not close(maneuver[-1][1], 10.0) for _ in (0,)):
        raise ValueError("final collective pitch is not 10 degrees")
    return pre_power, post_power, t90 - t10


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 one row")
    values = tuple(float(value.strip()) for value in rows[0].split(","))
    if len(values) != 3:
        raise ValueError("summary must contain three values")
    return values


def check() -> bool:
    required = (
        "build_case.py",
        "postprocess.py",
        "servo_step.dat",
        "elasto_step.dat",
        "aero_step.dat",
        "case.fst",
        "case.out",
        "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 / "case.log"):
        return False

    servo = read(ROOT / "servo_step.dat")
    for blade in (1, 2, 3):
        for name, expected in (
            (f"TPitManS({blade})", 30.0),
            (f"PitManRat({blade})", 10.0),
            (f"BlPitchF({blade})", 10.0),
        ):
            if not close(float(parameter(servo, name)), expected):
                return False
    fst = read(ROOT / "case.fst")
    expected_main = {
        "TMax": "60.0",
        "DT_Out": "0.1",
        "OutFileFmt": "1",
        "EDFile": "elasto_step.dat",
        "AeroFile": "aero_step.dat",
        "ServoFile": "servo_step.dat",
    }
    if any(parameter(fst, name) != value for name, value in expected_main.items()):
        return False
    elasto = read(ROOT / "elasto_step.dat")
    if any(f'"BldPitch{blade}"' not in elasto for blade in (1, 2, 3)):
        return False
    if '"../5MW_Baseline/' in elasto or '"../5MW_Baseline/' in read(ROOT / "aero_step.dat"):
        return False

    calculated = calculate(ROOT / "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())