File size: 14,398 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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | #!/usr/bin/env python3
from __future__ import annotations
import argparse
import ast
import concurrent.futures
import json
import os
import re
import subprocess
import sys
import tempfile
from pathlib import Path
FLOAT_RE = re.compile(r"(?<![A-Za-z0-9_])[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?(?![A-Za-z0-9_])")
PRIVATE_RUNNER_B64 = "eNp9V8tu2zoQ3fsrCK2oVhWaTXFhwIugzaK4aW+RBt0IBqFIVMJGJgWSTm0E/vc7M6QelpwaSGCS8zgzc2ZIr9SuM9az0vmM1cpl7LczOmNOVlZ6WLoj/PPHTrpVY82OdaV/atUDi3o/YLlafbu++/fmjm1YIsTNr+tb8ePu66/r+xvx+fr29qcQyWpV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gbnVCPqXrTO1veCL3NC5lGZZrCeLMKoHpjcXJdLZMHCac7oNRHHrTyRoQedKEXserDCdxdXrHNhoVqDB2b9RsjW4tJrBcGI0ZgZWVsHZ8Pl/kCjCSIb7Opr3MxySNwqOukrnnM4mXSxNlM7t8a3AHi6sJJTP9w1NNnctWclT4NtMS4aU52ZRVeFxnbmXrfBuTLp+krPH/xBB6866lkhu9i5AsdTKb1u3c9lFPsBO3pKiOuFx+3A7driUmCJ81xaAV8go0QgjtQpbd6/o2W9/g8HFWh++UunankQtSq8kLk+w67l48hj5oZm1sZzSBxgylXzKQos3S0mnKAD5faRJ7IkM1BBTeQlPUbFihjvfIInbYp7/jKhbxHwPJQyc6zn0eEd3NQ+MsHN8/e2bDOqaFwhEK6+XejoYgfZ8S3pXIyPvvdvvU4jcabL44HGGOTO7jf3Y5vJ7pn4wNP6f6r257doYuJ5uhmi/3UTzNSCfOwswqeHvF32Xv6UZfX+13neMCKs8x68SyPbnNvoaeB8+H3wOp/QMBSug=="
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:
if not is_result_artifact(path):
return True
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 dotted_name(node: ast.AST) -> str:
if isinstance(node, ast.Name):
return node.id
if isinstance(node, ast.Attribute):
prefix = dotted_name(node.value)
return f"{prefix}.{node.attr}" if prefix else node.attr
return ""
def assignment_expressions(tree: ast.AST) -> dict[str, list[ast.AST]]:
assignments: dict[str, list[ast.AST]] = {}
def target_names(target: ast.AST) -> list[str]:
if isinstance(target, ast.Name):
return [target.id]
if isinstance(target, (ast.Tuple, ast.List)):
return [name for item in target.elts for name in target_names(item)]
return []
for node in ast.walk(tree):
if isinstance(node, ast.Assign):
for target in node.targets:
for name in target_names(target):
assignments.setdefault(name, []).append(node.value)
elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name) and node.value is not None:
assignments.setdefault(node.target.id, []).append(node.value)
return assignments
def expression_dependencies(
node: ast.AST,
assignments: dict[str, list[ast.AST]],
function_dependencies: dict[str, set[str]],
seen: set[str] | None = None,
) -> set[str]:
seen = set() if seen is None else seen
dependencies: set[str] = set()
if isinstance(node, ast.Name):
if node.id in seen:
return dependencies
for value in assignments.get(node.id, []):
dependencies.update(expression_dependencies(value, assignments, function_dependencies, seen | {node.id}))
return dependencies
if isinstance(node, ast.Call):
call_name = dotted_name(node.func).lower().split(".")[-1]
dependencies.add(call_name)
dependencies.update(function_dependencies.get(call_name, set()))
dependencies.update(expression_dependencies(node.func, assignments, function_dependencies, seen))
for argument in node.args:
dependencies.update(expression_dependencies(argument, assignments, function_dependencies, seen))
for keyword in node.keywords:
dependencies.update(expression_dependencies(keyword.value, assignments, function_dependencies, seen))
return dependencies
for child in ast.iter_child_nodes(node):
dependencies.update(expression_dependencies(child, assignments, function_dependencies, seen))
return dependencies
def returned_function_dependencies(tree: ast.AST) -> dict[str, set[str]]:
results: dict[str, set[str]] = {}
functions = [node for node in ast.walk(tree) if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))]
for _ in range(max(1, len(functions))):
changed = False
for function in functions:
assignments = assignment_expressions(function)
dependencies: set[str] = set()
for node in ast.walk(function):
if isinstance(node, ast.Return) and node.value is not None:
dependencies.update(expression_dependencies(node.value, assignments, results))
key = function.name.lower()
if dependencies != results.get(key, set()):
results[key] = dependencies
changed = True
if not changed:
break
return results
def result_depends_on_cavity_solution(tree: ast.AST, external_functions: dict[str, set[str]]) -> bool:
functions = dict(external_functions)
functions.update(returned_function_dependencies(tree))
assignments = assignment_expressions(tree)
for node in ast.walk(tree):
if not isinstance(node, ast.Call) or dotted_name(node.func).lower().split(".")[-1] not in {"write", "write_text"}:
continue
if node.args:
dependencies = expression_dependencies(node.args[0], assignments, functions)
if "point" in dependencies and dependencies.intersection({"split", "sub", "function"}):
return True
return False
def execute_workflow(root: Path, entry_name: str, target_names: list[str], timeout: int) -> dict[str, set[str]]:
with tempfile.TemporaryDirectory(prefix="fenics-task13-eval-") as temp_dir:
runner = Path(temp_dir) / "runner.py"
runner.write_bytes(__import__("zlib").decompress(__import__("base64").b64decode(PRIVATE_RUNNER_B64)))
sources = [str((root / entry_name).resolve())]
sources.extend(str((root / name).resolve()) for name in target_names if name != entry_name)
completed = subprocess.run(
[sys.executable, str(runner), *sources], cwd=root,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=timeout,
)
if completed.returncode != 0:
raise ValueError("the cavity workflow did not complete successfully")
marker = "__EVAL_PRIVATE_CALLS__"
if marker not in completed.stdout:
raise ValueError("the cavity workflow produced no private execution evidence")
evidence = json.loads(completed.stdout.rsplit(marker, 1)[1].splitlines()[0])
return {name: set(evidence.get(name, [])) for name in target_names}
def check_common_solver(path: Path) -> bool:
try:
tree = ast.parse(read_text(path), filename=path.name)
except (OSError, SyntaxError, UnicodeError):
return False
imports: set[str] = set()
calls: set[str] = set()
for node in ast.walk(tree):
if isinstance(node, ast.Import):
imports.update(alias.name.split(".")[0].lower() for alias in node.names)
elif isinstance(node, ast.ImportFrom) and node.module:
imports.add(node.module.split(".")[0].lower())
elif isinstance(node, ast.Call):
calls.add(dotted_name(node.func).lower().split(".")[-1])
core = {"unitsquaremesh", "functionspace", "dirichletbc", "point"}
element_api = ({"vectorelement", "finiteelement"}.issubset(calls) or "vectorfunctionspace" in calls)
variational_api = bool(calls.intersection({"trialfunction", "trialfunctions"})) and bool(calls.intersection({"testfunction", "testfunctions"}))
solver_api = bool(calls.intersection({"solve", "linearvariationalsolver", "petsckrylovsolver", "lusolver"}))
return "dolfin" in imports and core.issubset(calls) and element_api and variational_api and solver_api
def check_case_script(path: Path, reynolds: int, external_functions: dict[str, set[str]]) -> bool:
try:
source = read_text(path)
tree = ast.parse(source, filename=path.name)
except (OSError, SyntaxError, UnicodeError):
return False
numeric_literals = {
int(node.value) for node in ast.walk(tree)
if isinstance(node, ast.Constant) and isinstance(node.value, (int, float)) and float(node.value).is_integer()
}
return (
reynolds in numeric_literals
and f"re_{reynolds}.result" in source
and result_depends_on_cavity_solution(tree, external_functions)
)
def check_task(root: Path) -> bool:
source_files = [
"re_100.py",
"re_400.py",
"re_1000.py",
"summarize.py",
]
for rel in source_files:
if not (root / rel).is_file():
return False
shared_solver = (root / "cavity_common.py").is_file()
if shared_solver and not check_common_solver(root / "cavity_common.py"):
return False
shared_functions: dict[str, set[str]] = {}
if shared_solver:
try:
shared_tree = ast.parse(read_text(root / "cavity_common.py"), filename="cavity_common.py")
except (OSError, SyntaxError, UnicodeError):
return False
shared_functions = returned_function_dependencies(shared_tree)
for reynolds in (100, 400, 1000):
if not check_case_script(root / f"re_{reynolds}.py", reynolds, shared_functions):
return False
if not shared_solver and not check_common_solver(root / f"re_{reynolds}.py"):
return False
summarize_source = read_text(root / "summarize.py")
if "summary.txt" not in summarize_source or ".result" not in summarize_source:
return False
for artifact in ("re_100.result", "re_400.result", "re_1000.result", "summary.txt"):
(root / artifact).unlink(missing_ok=True)
case_calls: dict[int, dict[str, set[str]]] = {}
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
pending = {
reynolds: executor.submit(
execute_workflow,
root,
f"re_{reynolds}.py",
([f"re_{reynolds}.py", "cavity_common.py"] if shared_solver else [f"re_{reynolds}.py"]),
105,
)
for reynolds in (100, 400, 1000)
}
for reynolds, future in pending.items():
case_calls[reynolds] = future.result()
solver_calls: list[set[str]] = []
for reynolds, calls_by_file in case_calls.items():
case_name = f"re_{reynolds}.py"
solver_calls.append(calls_by_file["cavity_common.py"] if shared_solver else calls_by_file[case_name])
runtime_core = {"unitsquaremesh", "functionspace", "dirichletbc", "point"}
if any(not runtime_core.issubset(calls) or not calls.intersection({"solve", "linearvariationalsolver", "petsckrylovsolver", "lusolver"}) for calls in solver_calls):
return False
execute_workflow(root, "summarize.py", ["summarize.py"], 15)
result_files: dict[int, float] = {}
for reynolds in (100, 400, 1000):
result_path = root / f"re_{reynolds}.result"
if not is_nonempty_file(result_path):
return False
result_values = parse_floats(read_text(result_path))
if len(result_values) < 2 or int(round(result_values[0])) != reynolds:
return False
result_files[reynolds] = result_values[1]
if not is_nonempty_file(root / "summary.txt"):
return False
results: dict[int, float] = {}
for raw_line in read_text(root / "summary.txt").splitlines():
line = raw_line.strip()
if (not line) or line.startswith("#"):
continue
vals = parse_floats(line)
if len(vals) < 2:
continue
re_float, ux = vals[0], vals[1]
re_int = int(round(re_float))
if abs(re_float - re_int) > 1e-6:
continue
results[re_int] = ux
for re_val in (100, 400, 1000):
if re_val not in results:
return False
if not abs(results[re_val] - result_files[re_val]) <= 1.0e-12:
return False
expected = {
100: -0.2091295638382351,
400: -0.1149858626712449,
1000: -0.06187004148288549,
}
for reynolds, target in expected.items():
if abs(results[reynolds] - target) > 0.025:
return False
if not (results[100] < results[400] < results[1000] < 0.0):
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())
|