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

import argparse
import ast
import json
import math
import re
import subprocess
import sys
from pathlib import Path


FLOAT_RE = re.compile(r"(?<![A-Za-z0-9_])[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?(?![A-Za-z0-9_])")


def is_result_artifact(path: Path) -> bool:
    name = path.name.lower()
    return (
        any(k in name for k in ("summary", "result", "report", "diagnosis"))
        or path.suffix.lower() in {".txt", ".csv", ".xy", ".result"}
    )


def is_nonempty_file(path: Path) -> bool:
    return path.exists() and path.is_file() and path.stat().st_size > 0


def read_text(path: Path) -> str:
    return path.read_text(encoding="utf-8", errors="ignore")


def parse_floats(text: str) -> list[float]:
    out: list[float] = []
    for token in FLOAT_RE.findall(text):
        try:
            out.append(float(token))
        except (TypeError, ValueError):
            continue
    return out


def write_result(path: Path, value: int) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(json.dumps({"result": int(1 if value else 0)}, ensure_ascii=False) + "\n", encoding="utf-8")


def check_task(root: Path) -> bool:
    required = ["poisson.py", "summary.txt"]
    for rel in required:
        if not is_nonempty_file(root / rel):
            return False

    script = root / "poisson.py"
    source = read_text(script)
    try:
        ast.parse(source, filename=str(script))
    except SyntaxError:
        return False
    normalized = re.sub(r"\s+", "", source)
    required_code = (
        "fromdolfinimport",
        "inner(grad(u),grad(v))*dx",
        "Constant(-6.0)",
        "solve(a==L,u,bc)",
        "assemble((u-u_D)**2*dx)",
        "assemble(inner(grad(u-u_D),grad(u-u_D))*dx)",
    )
    if any(fragment not in normalized for fragment in required_code):
        return False

    summary = root / "summary.txt"
    summary.unlink(missing_ok=True)
    try:
        completed = subprocess.run(
            [sys.executable, str(script)],
            cwd=root,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
            timeout=120,
            check=False,
        )
    except (OSError, subprocess.SubprocessError):
        return False
    if completed.returncode != 0 or not is_nonempty_file(summary):
        return False

    vals = parse_floats(read_text(summary))
    if len(vals) != 2 or not all(math.isfinite(value) for value in vals):
        return False
    err_l2, err_h1 = vals[0], vals[1]

    if err_l2 <= 0.0 or err_h1 <= 0.0:
        return False
    if err_l2 > 0.05 or err_h1 > 0.5:
        return False
    return True

def evaluate() -> int:
    root = Path("/home/user/Desktop")
    try:
        ok = check_task(root)
    except Exception:
        ok = False
    return 1 if ok else 0


def main() -> int:
    result = evaluate()
    print("True" if result == 1 else "False")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())