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

import math
import re
from pathlib import Path


ROOT = Path("/home/user/Desktop")
EXPECTED = (1842.854029, 563.535813)
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 mean_power(path: Path) -> 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()
    time_i = headers.index("Time")
    power_i = headers.index("RtAeroPwr")
    if units[power_i] != "(W)":
        raise ValueError("RtAeroPwr must be reported in watts")
    all_rows: list[list[float]] = []
    for line in lines[header_index + 2 :]:
        fields = line.split()
        if len(fields) != len(headers):
            continue
        try:
            all_rows.append([float(value) for value in fields])
        except ValueError:
            continue
    if not all_rows or not close(all_rows[-1][time_i], 630.0):
        raise ValueError("incomplete 630-second turbine output")
    rows = [row for row in all_rows if row[time_i] >= 570.0]
    if len(rows) < 590:
        raise ValueError("insufficient final-window samples")
    return sum(row[power_i] for row in rows) / len(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 one row")
    values = tuple(float(value.strip()) for value in rows[0].split(","))
    if len(values) != 2:
        raise ValueError("summary must contain two values")
    return values


def check_turbine_table(text: str) -> bool:
    rows = [line.split() for line in text.splitlines()]
    expected = (
        ("0.0", "0.0", "0.0", '"turbine1.fst"', "-69.49", "-80.0", "5.0", "10.17", "10.0", "10.0"),
        ("630.0", "0.0", "0.0", '"turbine2.fst"', "560.51", "-80.0", "5.0", "10.17", "10.0", "10.0"),
    )
    return all(list(row) in rows for row in expected)


def check() -> bool:
    required = (
        "build_case.py", "postprocess.py", "farm_inflow.dat", "elasto_farm.dat",
        "aero_farm.dat", "turbine1.fst", "turbine2.fst", "farm.fstf",
        "farm.out", "farm.T1.out", "farm.T2.out", "farm.log", "summary.txt",
    )
    if any(not (ROOT / name).is_file() or (ROOT / name).stat().st_size == 0 for name in required):
        return False
    if "FAST.Farm terminated normally." not in read(ROOT / "farm.log"):
        return False

    inflow = read(ROOT / "farm_inflow.dat")
    expected_inflow = {"WindType": "1", "PropagationDir": "0", "HWindSpeed": "8.0", "PLExp": "0.0"}
    if any(parameter(inflow, name) != value for name, value in expected_inflow.items()):
        return False

    elasto = read(ROOT / "elasto_farm.dat")
    if parameter(elasto, "GenDOF").lower() != "false" or not close(float(parameter(elasto, "RotSpeed")), 9.0):
        return False
    if '"../5MW_Baseline/' in elasto:
        return False
    aero = read(ROOT / "aero_farm.dat")
    if '"RtAeroPwr"' not in aero or '"../5MW_Baseline/' in aero:
        return False

    expected_turbine = {
        "TMax": "630.0", "CompInflow": "1", "CompServo": "0", "DT_Out": "0.1",
        "OutFileFmt": "1", "EDFile": "elasto_farm.dat", "InflowFile": "farm_inflow.dat",
        "AeroFile": "aero_farm.dat",
    }
    for filename in ("turbine1.fst", "turbine2.fst"):
        text = read(ROOT / filename)
        if any(parameter(text, name) != value for name, value in expected_turbine.items()):
            return False

    farm = read(ROOT / "farm.fstf")
    expected_farm = {
        "TMax": "630.0", "Mod_AmbWind": "2", "InflowFile": "farm_inflow.dat",
        "NumTurbines": "2", "Mod_Wake": "1", "WAT": "0", "OutFileFmt": "1",
    }
    if any(parameter(farm, name) != value for name, value in expected_farm.items()):
        return False
    if not check_turbine_table(farm):
        return False
    farm_output = read(ROOT / "farm.out")
    if "at commit v5.0.0" not in "\n".join(farm_output.splitlines()[:5]):
        return False

    calculated = tuple(mean_power(ROOT / f"farm.T{i}.out") for i in (1, 2))
    reported = parse_summary(ROOT / "summary.txt")
    if calculated[1] >= 0.5 * calculated[0]:
        return False
    return all(close(actual, expected) for actual, expected in zip(calculated, EXPECTED)) and all(
        close(actual, expected) for actual, expected 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())