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

import math
import re
from pathlib import Path


ROOT = Path("/home/user/Desktop")
EXPECTED = (1023.266727, 3.116419)
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 calculate(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(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")
    tension_i = [headers.index(f"FAIRTEN{i}") for i in (1, 2, 3)]
    if any(units[index] != "(N)" for index in tension_i):
        raise ValueError("unexpected MoorDyn tension units")
    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], 120.0):
        raise ValueError("incomplete 120-second output")
    rows = [row for row in all_rows if row[time_i] >= 60.0]
    if len(rows) < 590:
        raise ValueError("insufficient final-window samples")
    means = [sum(row[index] for row in rows) / len(rows) / 1000.0 for index in tension_i]
    stds = [
        math.sqrt(sum((row[index] / 1000.0 - means[j]) ** 2 for row in rows) / len(rows))
        for j, index in enumerate(tension_i)
    ]
    return means[0], max(stds)


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 has_row(text: str, expected: tuple[str, ...]) -> bool:
    return any(tuple(line.split()) == expected for line in text.splitlines())


def line_type_rows(text: str) -> list[tuple[str, ...]]:
    block = text.split("LINE TYPES", 1)[1].split("POINTS", 1)[0]
    rows: list[tuple[str, ...]] = []
    for line in block.splitlines():
        stripped = line.strip()
        if not stripped or stripped.startswith(("-", "Name", "(-)")):
            continue
        rows.append(tuple(stripped.split()))
    return rows


def check() -> bool:
    required = (
        "build_case.py", "postprocess.py", "moordyn.dat", "seastate_moordyn.dat",
        "elasto_moordyn.dat", "aero_moordyn.dat", "servo_moordyn.dat",
        "hydrodyn_moordyn.dat", "moordyn_case.fst", "moordyn_case.out",
        "moordyn_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 / "moordyn_case.log"):
        return False

    moordyn = read(ROOT / "moordyn.dat")
    required_rows = (
        ("main", "0.0766", "113.35", "7.536E8", "-1.0", "0", "2.0", "0.8", "0.4", "0.25"),
        ("1", "Fixed", "418.8", "725.383", "-200.0", "0", "0", "0", "0"),
        ("2", "Fixed", "-837.6", "0.0", "-200.0", "0", "0", "0", "0"),
        ("3", "Fixed", "418.8", "-725.383", "-200.0", "0", "0", "0", "0"),
        ("4", "Vessel", "20.434", "35.393", "-14.0", "0", "0", "0", "0"),
        ("5", "Vessel", "-40.868", "0.0", "-14.0", "0", "0", "0", "0"),
        ("6", "Vessel", "20.434", "-35.393", "-14.0", "0", "0", "0", "0"),
        ("1", "main", "1", "4", "835.35", "20", "-"),
        ("2", "main", "2", "5", "835.35", "20", "-"),
        ("3", "main", "3", "6", "835.35", "20", "-"),
    )
    if any(not has_row(moordyn, row) for row in required_rows):
        return False
    if line_type_rows(moordyn) != [required_rows[0]]:
        return False
    if any(channel not in moordyn for channel in ("FairTen1", "FairTen2", "FairTen3")):
        return False
    if parameter(read(ROOT / "seastate_moordyn.dat"), "WaveMod") != "0":
        return False
    elasto = read(ROOT / "elasto_moordyn.dat")
    if parameter(elasto, "GenDOF").lower() != "false" or not close(float(parameter(elasto, "RotSpeed")), 9.0):
        return False
    fst = read(ROOT / "moordyn_case.fst")
    expected_main = {
        "TMax": "120.0", "CompInflow": "1", "CompAero": "2",
        "CompSeaSt": "1", "CompHydro": "1", "CompServo": "0",
        "CompMooring": "3", "DT_Out": "0.1", "OutFileFmt": "1",
        "EDFile": "elasto_moordyn.dat", "AeroFile": "aero_moordyn.dat",
        "InflowFile": "5MW_Baseline/NRELOffshrBsline5MW_InflowWind_Steady8mps.dat",
        "ServoFile": "servo_moordyn.dat",
        "SeaStFile": "seastate_moordyn.dat", "HydroFile": "hydrodyn_moordyn.dat",
        "MooringFile": "moordyn.dat",
    }
    if any(parameter(fst, name) != value for name, value in expected_main.items()):
        return False
    for name in ("elasto_moordyn.dat", "aero_moordyn.dat", "hydrodyn_moordyn.dat"):
        if '"../5MW_Baseline/' in read(ROOT / name):
            return False

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