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

import math
import re
from pathlib import Path


ROOT = Path("/home/user/Desktop")
EXPECTED = (33.69477, 0.024675711)
ABS_TOL = 2e-6
REL_TOL = 2e-4
MODE = re.compile(
    r"^\s*1\s+([+\-0-9.Ee]+)\s+([+\-0-9.Ee]+)\s+([+\-0-9.Ee]+)\s+([+\-0-9.Ee]+)",
    re.M,
)


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 parse_result(path: Path) -> tuple[float, float]:
    match = MODE.search(read(path))
    if not match:
        raise ValueError("first eigenmode row missing")
    frequency = float(match.group(3))
    omega = 2.0 * math.pi * frequency
    return frequency, 10.0 / (2.0 * omega) + 1.0e-5 * omega / 2.0


def parse_summary(path: Path) -> tuple[float, float]:
    values = [float(value.strip()) for value in read(path).strip().split(",")]
    if len(values) != 2:
        raise ValueError("summary must contain two values")
    return values[0], values[1]


def valid_input(path: Path) -> bool:
    text = re.sub(r"\s+", "", read(path).lower())
    return all(
        token in text
        for token in (
            "*damping,alpha=10.0,beta=1.0e-5",
            "*frequency,storage=yes",
            "\n5\n".replace("\n", ""),
            "fixed,1,3,0.0",
            "*nodefile,nset=tip",
        )
    )


def check() -> bool:
    required = ("complex.inp", "complex.dat", "complex.frd", "complex.eig", "complex.sta", "postprocess.py", "summary.txt")
    if any(not (ROOT / name).is_file() or (ROOT / name).stat().st_size == 0 for name in required):
        return False
    if not valid_input(ROOT / "complex.inp"):
        return False
    calculated = parse_result(ROOT / "complex.dat")
    reported = parse_summary(ROOT / "summary.txt")
    return all(
        close(actual, expected)
        for actual, expected in zip((*calculated, *reported), (*EXPECTED, *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())