File size: 6,383 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
from __future__ import annotations

import math
import re
from pathlib import Path


ROOT = Path("/home/user/Desktop")
EXPECTED = (1749.944881, 362.987969)
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")
    thrust_i = headers.index("RtAeroFxh")
    if (
        units[time_i] != "(s)"
        or units[wind_i] != "(m/s)"
        or units[speed_i] != "(rpm)"
        or units[power_i] != "(W)"
        or units[thrust_i] != "(N)"
    ):
        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")

    final_rows = rows[200:]
    if len(final_rows) != 101:
        raise ValueError("incorrect final-window sample count")
    return (
        sum(row[power_i] for row in final_rows)
        / len(final_rows)
        / 1000.0,
        max(abs(row[thrust_i]) for row in final_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 exactly one row")
    values = tuple(float(field.strip()) for field in rows[0].split(","))
    if len(values) != 2 or not all(math.isfinite(value) for value in values):
        raise ValueError("summary must contain two finite values")
    return values


def check() -> bool:
    required = (
        "build_case.py",
        "postprocess.py",
        "minimal_elasto.dat",
        "minimal_aero.dat",
        "minimal.fst",
        "minimal.out",
        "minimal.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 / "minimal_elasto.dat")
    expected_elasto = {
        "DrTrDOF": "False",
        "GenDOF": "False",
        "RotSpeed": "9.0",
    }
    if any(
        parameter(elasto, name) != value
        for name, value in expected_elasto.items()
    ):
        return False
    if '"../5MW_Baseline/' in elasto:
        return False

    aero = read(ROOT / "minimal_aero.dat")
    if parameter(aero, "Wake_Mod") != "1":
        return False
    if '"../5MW_Baseline/' in aero:
        return False
    if any(
        f'"{channel}"' not in aero
        for channel in ("RtAeroPwr", "RtAeroFxh")
    ):
        return False

    main = read(ROOT / "minimal.fst")
    expected_main = {
        "TMax": "30.0",
        "DT": "0.0125",
        "DT_Out": "0.1",
        "OutFileFmt": "1",
        "CompInflow": "1",
        "CompAero": "2",
        "CompServo": "0",
        "EDFile": "minimal_elasto.dat",
        "InflowFile": (
            "5MW_Baseline/"
            "NRELOffshrBsline5MW_InflowWind_Steady8mps.dat"
        ),
        "AeroFile": "minimal_aero.dat",
    }
    if any(
        parameter(main, name) != value
        for name, value in expected_main.items()
    ):
        return False

    log = read(ROOT / "minimal.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
    ):
        return False

    calculated = output_metrics(ROOT / "minimal.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())