File size: 10,055 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 | #!/usr/bin/env python3
from __future__ import annotations
import csv
import math
import re
import subprocess
from pathlib import Path
ROOT = Path("/home/user/Desktop")
SWEEP = ROOT / "cavity_sweep"
REYNOLDS = (100, 400, 1000)
def read(path: Path) -> str:
return path.read_text(encoding="utf-8", errors="ignore")
def named_block(text: str, name: str) -> str:
text = re.sub(r"/\*.*?\*/|//[^\n]*", "", text, flags=re.S)
match = re.search(rf"(?m)^\s*{re.escape(name)}\s*\{{", text)
if not match:
raise ValueError(f"missing block {name}")
start = text.find("{", match.start())
depth = 0
for index in range(start, len(text)):
depth += text[index] == "{"
depth -= text[index] == "}"
if depth == 0:
return text[start + 1 : index]
raise ValueError(f"unterminated block {name}")
def has_entry(block: str, key: str, value: str) -> bool:
return re.search(rf"(?<!\w){re.escape(key)}\s+{value}\s*;", block) is not None
def close(actual: float, expected: float, tolerance: float) -> bool:
return math.isfinite(actual) and abs(actual - expected) <= tolerance
def patch(text: str, name: str) -> str:
return named_block(named_block(text, "boundaryField"), name)
def vectors(path: Path) -> list[tuple[float, float, float]]:
text = read(path)
header = named_block(text, "FoamFile")
match = re.search(
r"internalField\s+nonuniform\s+List<vector>\s+(\d+)\s*\((.*?)\)\s*;",
text,
re.S,
)
if not match:
raise ValueError(f"missing vector field: {path}")
values = [
tuple(float(value) for value in item.split())
for item in re.findall(r"\(([^()]+)\)", match.group(2))
]
if (
len(values) != int(match.group(1))
or any(
len(value) != 3 or not all(math.isfinite(component) for component in value)
for value in values
)
or not has_entry(header, "format", r"ascii")
or not has_entry(header, "class", r"volVectorField")
or not has_entry(header, "object", re.escape(path.name))
):
raise ValueError(f"invalid vector field: {path}")
return values
def scalars(path: Path) -> list[float]:
text = read(path)
header = named_block(text, "FoamFile")
match = re.search(
r"internalField\s+nonuniform\s+List<scalar>\s+(\d+)\s*\((.*?)\)\s*;",
text,
re.S,
)
if not match:
raise ValueError(f"missing scalar field: {path}")
values = [float(value) for value in match.group(2).split()]
if (
len(values) != int(match.group(1))
or any(not math.isfinite(value) for value in values)
or not has_entry(header, "format", r"ascii")
or not has_entry(header, "class", r"volScalarField")
or not has_entry(header, "object", re.escape(path.name))
):
raise ValueError(f"invalid scalar field: {path}")
return values
def paren_section(text: str, name: str) -> str:
match = re.search(rf"(?m)^\s*{re.escape(name)}\s*\(", text)
if not match:
raise ValueError(f"missing section {name}")
start = text.find("(", match.start())
depth = 0
for index in range(start, len(text)):
depth += text[index] == "("
depth -= text[index] == ")"
if depth == 0:
return text[start + 1 : index]
raise ValueError(f"unterminated section {name}")
def mesh_geometry_is_valid(mesh: str) -> bool:
scale_match = re.search(r"\bconvertToMeters\s+([^;]+);", mesh)
vertices = [
tuple(float(value) for value in item.split())
for item in re.findall(r"\(([^()]+)\)", paren_section(mesh, "vertices"))
]
if scale_match is None or len(vertices) != 8 or any(len(point) != 3 for point in vertices):
return False
scale = float(scale_match.group(1))
xs, ys, zs = zip(*(tuple(scale * value for value in point) for point in vertices))
return (
abs(min(xs)) <= 1e-12
and abs(max(xs) - 1.0) <= 1e-12
and abs(min(ys)) <= 1e-12
and abs(max(ys) - 1.0) <= 1e-12
and abs(min(zs)) <= 1e-12
and abs(max(zs) - 0.01) <= 1e-12
and re.search(r"hex\s*\([^)]*\)\s*\(\s*50\s+50\s+1\s*\)", mesh) is not None
)
def numeric_entry(text: str, key: str) -> float:
match = re.search(rf"(?m)^\s*{re.escape(key)}\s+([^;]+);", text)
if not match:
raise ValueError(f"missing entry {key}")
return float(match.group(1))
def log_finished(path: Path) -> bool:
text = read(path)
return "FOAM FATAL" not in text and text.rstrip().endswith("End")
def case_inputs(case: Path, reynolds: int) -> bool:
mesh = read(case / "system/blockMeshDict")
velocity = read(case / "0/U")
pressure = read(case / "0/p")
physical = read(case / "constant/physicalProperties")
control = read(case / "system/controlDict")
schemes = read(case / "system/fvSchemes")
solution = read(case / "system/fvSolution")
boundary = read(case / "constant/polyMesh/boundary")
match = re.search(r"\bnu\s+\[[^]]+\]\s+([^;]+);", physical)
return all(
(
mesh_geometry_is_valid(mesh),
has_entry(patch(velocity, "movingWall"), "value", r"uniform\s+\(1(?:\.0+)?\s+0(?:\.0+)?\s+0(?:\.0+)?\)"),
has_entry(patch(velocity, "fixedWalls"), "type", r"noSlip"),
has_entry(patch(velocity, "frontAndBack"), "type", r"empty"),
has_entry(patch(pressure, "movingWall"), "type", r"zeroGradient"),
has_entry(patch(pressure, "fixedWalls"), "type", r"zeroGradient"),
has_entry(patch(pressure, "frontAndBack"), "type", r"empty"),
match is not None and abs(float(match.group(1)) - 1.0 / reynolds) < 1.0e-12,
abs(numeric_entry(control, "endTime") - 30.0) <= 1e-12,
abs(numeric_entry(control, "deltaT") - 0.005) <= 1e-12,
re.search(r"\bwriteFormat\s+ascii\s*;", control) is not None,
has_entry(named_block(schemes, "ddtSchemes"), "default", r"Euler"),
has_entry(named_block(schemes, "divSchemes"), "div(phi,U)", r"Gauss\s+linear"),
has_entry(named_block(solution, "PISO"), "nCorrectors", r"2"),
re.search(r"\bfrontAndBack\b.*?type\s+empty;", boundary, re.S)
is not None,
)
)
def center_result(case: Path) -> tuple[tuple[float, float, float], float]:
final = case / "30"
centres = vectors(final / "C")
velocity = vectors(final / "U")
if len(centres) != 2500 or len(velocity) != len(centres):
raise ValueError("unexpected final field size")
target = (0.5, 0.5, 0.005)
indices = sorted(
range(len(centres)),
key=lambda i: sum((centres[i][j] - target[j]) ** 2 for j in range(3)),
)[:4]
center = tuple(sum(velocity[i][j] for i in indices) / 4.0 for j in range(3))
max_speed = max(math.sqrt(sum(component**2 for component in item)) for item in velocity)
return center, max_speed
def check() -> bool:
required_root = (
ROOT / "build_case.py",
ROOT / "run_sweep.py",
ROOT / "postprocess.py",
ROOT / "summary.txt",
ROOT / "sweep_results.csv",
)
if any(not path.is_file() or path.stat().st_size == 0 for path in required_root):
return False
calculated = {}
for reynolds in REYNOLDS:
case = SWEEP / f"Re{reynolds}"
required = (
case / "log.blockMesh",
case / "log.checkMesh",
case / "log.icoFoam",
case / "log.cellCentres",
case / "30/U",
case / "30/p",
case / "30/C",
)
if any(not path.is_file() or path.stat().st_size == 0 for path in required):
return False
if any(not log_finished(path) for path in required[:4]):
return False
if "Mesh OK." not in read(case / "log.checkMesh") or not case_inputs(case, reynolds):
return False
mesh_check = subprocess.run(
[
"bash",
"--noprofile",
"--norc",
"-c",
f"trap - CHLD; . /opt/openfoam11/etc/bashrc && checkMesh -case {case}",
],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
timeout=120,
)
if mesh_check.returncode != 0 or "Mesh OK." not in mesh_check.stdout:
return False
center, max_speed = center_result(case)
if len(scalars(case / "30/p")) != 2500:
return False
if not (-0.5 < center[0] < 0 and 0.5 < max_speed < 1.2):
return False
calculated[reynolds] = (1.0 / reynolds, *center, max_speed)
if not (
calculated[100][1]
< calculated[400][1]
< calculated[1000][1]
< 0
):
return False
with (ROOT / "sweep_results.csv").open(encoding="utf-8", newline="") as stream:
rows = list(csv.DictReader(stream))
keys = ("nu_m2_s", "center_ux", "center_uy", "center_uz", "max_speed")
if [int(row["reynolds"]) for row in rows] != list(REYNOLDS):
return False
for row in rows:
reynolds = int(row["reynolds"])
actual = tuple(float(row[key]) for key in keys)
if any(not close(a, b, 5.0e-7) for a, b in zip(actual, calculated[reynolds])):
return False
lines = [line.strip() for line in read(ROOT / "summary.txt").splitlines() if line.strip()]
if len(lines) != 3:
return False
for line, reynolds in zip(lines, REYNOLDS):
parts = [item.strip() for item in line.split(",")]
if len(parts) != 2 or int(parts[0]) != reynolds:
return False
if not close(float(parts[1]), calculated[reynolds][1], 5.0e-7):
return False
return True
def main() -> int:
try:
result = check()
except Exception:
result = False
print("True" if result else "False")
return 0
if __name__ == "__main__":
raise SystemExit(main())
|