cfdbender 0.4.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cfdbender/__init__.py +7 -0
- cfdbender/_pyfs.py +92 -0
- cfdbender/core/__init__.py +13 -0
- cfdbender/core/adapter.py +148 -0
- cfdbender/core/executables.py +157 -0
- cfdbender/core/flight.py +346 -0
- cfdbender/core/matrix.py +354 -0
- cfdbender/core/naming.py +172 -0
- cfdbender/core/plan.py +463 -0
- cfdbender/core/registry.py +735 -0
- cfdbender/core/results.py +117 -0
- cfdbender/core/run.py +150 -0
- cfdbender/core/settings.py +92 -0
- cfdbender/core/workspace.py +93 -0
- cfdbender/geometry/__init__.py +1 -0
- cfdbender/geometry/blade_slicing.py +355 -0
- cfdbender/hpc/__init__.py +21 -0
- cfdbender/hpc/_plan.py +266 -0
- cfdbender/hpc/_profiles.py +305 -0
- cfdbender/hpc/_render.py +271 -0
- cfdbender/hpc/_submit.py +96 -0
- cfdbender/hpc/_walltime.py +301 -0
- cfdbender/matrix/__init__.py +1 -0
- cfdbender/matrix/cli.py +228 -0
- cfdbender/matrix/collection.py +251 -0
- cfdbender/matrix/exports.py +122 -0
- cfdbender/matrix/rerun.py +126 -0
- cfdbender/matrix/selection.py +62 -0
- cfdbender/matrix/submission.py +272 -0
- cfdbender/solvers/__init__.py +47 -0
- cfdbender/solvers/_host/__init__.py +1 -0
- cfdbender/solvers/_host/_config.py +10 -0
- cfdbender/solvers/_host/_execute.py +52 -0
- cfdbender/solvers/_host/_mesh.py +62 -0
- cfdbender/solvers/_host/_ram.py +69 -0
- cfdbender/solvers/_host/_single.py +63 -0
- cfdbender/solvers/flightstream/__init__.py +114 -0
- cfdbender/solvers/flightstream/reader.py +191 -0
- cfdbender/solvers/mock/__init__.py +273 -0
- cfdbender/solvers/openfoam/__init__.py +201 -0
- cfdbender/solvers/openfoam/_config.py +410 -0
- cfdbender/solvers/openfoam/_execute.py +162 -0
- cfdbender/solvers/openfoam/_history.py +158 -0
- cfdbender/solvers/pyfr/__init__.py +118 -0
- cfdbender/solvers/pyfr/_config.py +196 -0
- cfdbender/solvers/pyfr/_execute.py +127 -0
- cfdbender/solvers/pyfr/_history.py +88 -0
- cfdbender/solvers/qprop/__init__.py +303 -0
- cfdbender/solvers/qprop/_config.py +259 -0
- cfdbender/solvers/qprop/_execute.py +269 -0
- cfdbender/solvers/qprop/_output.py +64 -0
- cfdbender/solvers/qprop/_polar.py +98 -0
- cfdbender/solvers/qprop/_prop.py +81 -0
- cfdbender/solvers/qprop/_sections.py +193 -0
- cfdbender/solvers/su2/__init__.py +210 -0
- cfdbender/solvers/su2/_config.py +290 -0
- cfdbender/solvers/su2/_execute.py +196 -0
- cfdbender/solvers/su2/_history.py +117 -0
- cfdbender/solvers/su2/_pysu2_worker.py +25 -0
- cfdbender/solvers/su2/_ram.py +13 -0
- cfdbender/solvers/su2/_reference.py +79 -0
- cfdbender/solvers/su2/_single.py +3 -0
- cfdbender/solvers/su2/routes.py +61 -0
- cfdbender/solvers/xfoil/__init__.py +171 -0
- cfdbender/solvers/xfoil/_config.py +144 -0
- cfdbender/solvers/xfoil/_execute.py +227 -0
- cfdbender/solvers/xfoil/_polar.py +88 -0
- cfdbender/testing/__init__.py +301 -0
- cfdbender/workspace/__init__.py +1 -0
- cfdbender/workspace/cli.py +99 -0
- cfdbender/workspace/planning.py +38 -0
- cfdbender-0.4.2.dist-info/METADATA +57 -0
- cfdbender-0.4.2.dist-info/RECORD +77 -0
- cfdbender-0.4.2.dist-info/WHEEL +5 -0
- cfdbender-0.4.2.dist-info/entry_points.txt +3 -0
- cfdbender-0.4.2.dist-info/licenses/LICENSE +21 -0
- cfdbender-0.4.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
"""Collect saved results once and atomically rebuild matrix summaries.
|
|
2
|
+
|
|
3
|
+
A SUBMITTED point is read and completed from numerical evidence once the files
|
|
4
|
+
its job writes are present: the job_outputs recorded at submission (from the
|
|
5
|
+
adapter's JOB_OUTPUTS), or every declared output when none were recorded. Outputs only
|
|
6
|
+
a local run writes (execution.json) may be absent. PLANNED, RUNNING and
|
|
7
|
+
incomplete submitted points remain outstanding.
|
|
8
|
+
Collection never launches work or rewrites finished evidence. A summary is a
|
|
9
|
+
derived product and can be rebuilt from the registry and adapter outputs.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import hashlib
|
|
15
|
+
import json
|
|
16
|
+
import os
|
|
17
|
+
import re
|
|
18
|
+
import tempfile
|
|
19
|
+
from collections.abc import Sequence
|
|
20
|
+
from dataclasses import dataclass, replace
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
from typing import cast
|
|
23
|
+
|
|
24
|
+
from cfdbender.core.adapter import SolverAdapter, load_point
|
|
25
|
+
from cfdbender.core.plan import workspace_path
|
|
26
|
+
from cfdbender.core.registry import RegistryError, RunRecord, RunRegistry
|
|
27
|
+
from cfdbender.core.settings import read_init
|
|
28
|
+
from cfdbender.matrix.exports import write_tables
|
|
29
|
+
from cfdbender.matrix.selection import chosen
|
|
30
|
+
from cfdbender.solvers import available_solvers, load_solver
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass(frozen=True)
|
|
34
|
+
class CollectReport:
|
|
35
|
+
"""One sweep's counts and derived summary paths; failures take exit precedence.
|
|
36
|
+
|
|
37
|
+
``tables`` are the CSV tables written beside each summary: campaign_sweep.csv
|
|
38
|
+
and polars/P<POL>.csv per matrix.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
collected: int
|
|
42
|
+
failed: int
|
|
43
|
+
outstanding: int
|
|
44
|
+
summaries: tuple[Path, ...]
|
|
45
|
+
tables: tuple[Path, ...] = ()
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def exit_code(self) -> int:
|
|
49
|
+
"""Return 1 for collection failures, 3 for outstanding work, or 0."""
|
|
50
|
+
return 1 if self.failed else 3 if self.outstanding else 0
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _verify_outputs(root: Path, record: RunRecord) -> None:
|
|
54
|
+
for name, expected in record.outputs_sha256.items():
|
|
55
|
+
path = workspace_path(root, name)
|
|
56
|
+
if hashlib.sha256(path.read_bytes()).hexdigest() != expected:
|
|
57
|
+
raise RegistryError(f"{record.point_id}: output changed after execution: {name}")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _verify_submission(root: Path, record: RunRecord) -> None:
|
|
61
|
+
if record.submission is None:
|
|
62
|
+
return
|
|
63
|
+
for key, digest in (("descriptor", "descriptor_sha256"), ("config_path", "config_sha256")):
|
|
64
|
+
path = workspace_path(root, str(record.submission[key]))
|
|
65
|
+
if hashlib.sha256(path.read_bytes()).hexdigest() != record.submission[digest]:
|
|
66
|
+
raise RegistryError(f"{record.point_id}: submission {key} changed")
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _job_outputs(record: RunRecord) -> tuple[str, ...]:
|
|
70
|
+
if record.submission is not None and "job_outputs" in record.submission:
|
|
71
|
+
return cast(tuple[str, ...], record.submission["job_outputs"])
|
|
72
|
+
return record.point.outputs
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _write_summary(root: Path, matrix: str, solver: str, rows: list[dict[str, object]]) -> Path:
|
|
76
|
+
target = workspace_path(root, f"post/{Path(matrix).stem}/campaign_summary.json")
|
|
77
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
78
|
+
data = (
|
|
79
|
+
json.dumps(
|
|
80
|
+
{"schema_version": 1, "matrix": matrix, "solver": solver, "points": rows},
|
|
81
|
+
indent=2,
|
|
82
|
+
sort_keys=True,
|
|
83
|
+
allow_nan=False,
|
|
84
|
+
)
|
|
85
|
+
+ chr(10)
|
|
86
|
+
).encode()
|
|
87
|
+
temporary: Path | None = None
|
|
88
|
+
try:
|
|
89
|
+
with tempfile.NamedTemporaryFile(
|
|
90
|
+
dir=target.parent, prefix=".collect-", delete=False
|
|
91
|
+
) as stream:
|
|
92
|
+
temporary = Path(stream.name)
|
|
93
|
+
stream.write(data)
|
|
94
|
+
stream.flush()
|
|
95
|
+
os.fsync(stream.fileno())
|
|
96
|
+
os.replace(temporary, target)
|
|
97
|
+
temporary = None
|
|
98
|
+
finally:
|
|
99
|
+
if temporary is not None:
|
|
100
|
+
temporary.unlink(missing_ok=True)
|
|
101
|
+
return target
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _previous_rows(root: Path, matrix: str) -> dict[str, dict[str, object]]:
|
|
105
|
+
path = root / "post" / Path(matrix).stem / "campaign_summary.json"
|
|
106
|
+
try:
|
|
107
|
+
points = json.loads(path.read_bytes())["points"]
|
|
108
|
+
return {str(row["point_id"]): dict(row) for row in points}
|
|
109
|
+
except (OSError, ValueError, KeyError, TypeError):
|
|
110
|
+
return {}
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def collect_workspace(
|
|
114
|
+
root: str | Path,
|
|
115
|
+
matrix: str | None = None,
|
|
116
|
+
*,
|
|
117
|
+
sims: Sequence[str] = (),
|
|
118
|
+
points: Sequence[str] = (),
|
|
119
|
+
) -> CollectReport:
|
|
120
|
+
"""Read saved results through the declared adapter and rebuild neutral summaries.
|
|
121
|
+
|
|
122
|
+
Parameters
|
|
123
|
+
----------
|
|
124
|
+
root : str or Path
|
|
125
|
+
Workspace containing init.toml and runs.json.
|
|
126
|
+
matrix : str, optional
|
|
127
|
+
Matrix stem or filename. Omit to collect all recorded matrices.
|
|
128
|
+
sims : sequence of str, optional
|
|
129
|
+
POL ids to collect; empty collects every simulation.
|
|
130
|
+
points : sequence of str, optional
|
|
131
|
+
Point names (DP-name or POL/DP-name) to collect; empty collects every point.
|
|
132
|
+
Points outside the selection are neither read nor counted; the summary
|
|
133
|
+
keeps their rows from the previous summary, or their identity rows.
|
|
134
|
+
|
|
135
|
+
Returns
|
|
136
|
+
-------
|
|
137
|
+
CollectReport
|
|
138
|
+
Successful reads, failed points, outstanding points and summary paths.
|
|
139
|
+
Summaries retain identity rows even when a point has no readable result.
|
|
140
|
+
|
|
141
|
+
Raises
|
|
142
|
+
------
|
|
143
|
+
RegistryError
|
|
144
|
+
No matching records, a solver mismatch or an invalid matrix selection.
|
|
145
|
+
OSError
|
|
146
|
+
A summary cannot be written. Existing summaries remain intact.
|
|
147
|
+
"""
|
|
148
|
+
target = Path(root).resolve()
|
|
149
|
+
config = read_init(target, available_solvers=available_solvers())
|
|
150
|
+
adapter = cast(SolverAdapter, load_solver(config.solver))
|
|
151
|
+
if (
|
|
152
|
+
matrix is not None
|
|
153
|
+
and re.fullmatch("[A-Za-z0-9][A-Za-z0-9_-]*", matrix.removesuffix(".fs")) is None
|
|
154
|
+
):
|
|
155
|
+
raise RegistryError(f"{matrix!r}: name a .fs matrix in inputs/matrices/")
|
|
156
|
+
selected = None if matrix is None else f"inputs/matrices/{matrix.removesuffix('.fs')}.fs"
|
|
157
|
+
registry = RunRegistry(target)
|
|
158
|
+
records = tuple(r for r in registry.read() if selected is None or r.matrix == selected)
|
|
159
|
+
if not records:
|
|
160
|
+
raise RegistryError("no recorded points for this collection")
|
|
161
|
+
if any(r.solver != config.solver for r in records):
|
|
162
|
+
raise RegistryError("init.toml solver differs from the recorded solver")
|
|
163
|
+
keep = chosen((r.point for r in records), sims, points)
|
|
164
|
+
previous = {name: _previous_rows(target, name) for name in {r.matrix for r in records}}
|
|
165
|
+
grouped: dict[str, list[dict[str, object]]] = {}
|
|
166
|
+
collected = failed = outstanding = 0
|
|
167
|
+
for record in records:
|
|
168
|
+
row: dict[str, object] = {
|
|
169
|
+
"point_id": record.point_id,
|
|
170
|
+
"pol": record.point.pol,
|
|
171
|
+
"aircraft": record.point.aircraft,
|
|
172
|
+
"configuration": record.point.configuration,
|
|
173
|
+
"description": record.point.description,
|
|
174
|
+
"condition": dict(record.point.condition),
|
|
175
|
+
"status": record.status,
|
|
176
|
+
"classification": record.classification,
|
|
177
|
+
"created_at": record.created_at,
|
|
178
|
+
"started_at": record.started_at,
|
|
179
|
+
"completed_at": record.completed_at,
|
|
180
|
+
"inputs_sha256": dict(record.point.inputs_sha256),
|
|
181
|
+
"outputs_sha256": dict(record.outputs_sha256),
|
|
182
|
+
"error": record.error,
|
|
183
|
+
"result": None,
|
|
184
|
+
}
|
|
185
|
+
if record.submission is not None:
|
|
186
|
+
row["submission"] = dict(record.submission)
|
|
187
|
+
if record.point_id not in keep:
|
|
188
|
+
row = previous[record.matrix].get(record.point_id, row)
|
|
189
|
+
grouped.setdefault(record.matrix, []).append(row)
|
|
190
|
+
continue
|
|
191
|
+
grouped.setdefault(record.matrix, []).append(row)
|
|
192
|
+
if record.status not in ("COMPLETED", "SUBMITTED"):
|
|
193
|
+
outstanding += 1
|
|
194
|
+
continue
|
|
195
|
+
try:
|
|
196
|
+
if record.status == "SUBMITTED" and any(
|
|
197
|
+
not workspace_path(target, name).is_file() for name in _job_outputs(record)
|
|
198
|
+
):
|
|
199
|
+
outstanding += 1
|
|
200
|
+
continue
|
|
201
|
+
point = load_point(target, record.point, solver=config.solver)
|
|
202
|
+
_verify_submission(target, record)
|
|
203
|
+
measured = record
|
|
204
|
+
if record.status == "SUBMITTED":
|
|
205
|
+
measured = replace(
|
|
206
|
+
record,
|
|
207
|
+
outputs_sha256={
|
|
208
|
+
name: hashlib.sha256(workspace_path(target, name).read_bytes()).hexdigest()
|
|
209
|
+
for name in record.point.outputs
|
|
210
|
+
if workspace_path(target, name).is_file()
|
|
211
|
+
},
|
|
212
|
+
)
|
|
213
|
+
_verify_outputs(target, measured)
|
|
214
|
+
result = adapter.collect(point)
|
|
215
|
+
_verify_outputs(target, measured)
|
|
216
|
+
_verify_submission(target, record)
|
|
217
|
+
if record.status == "SUBMITTED":
|
|
218
|
+
record = registry.transition(
|
|
219
|
+
record.point_id,
|
|
220
|
+
"COMPLETED",
|
|
221
|
+
classification=result.classification,
|
|
222
|
+
error="FAILED_EXECUTION: numerical evidence failed during collect"
|
|
223
|
+
if result.classification == "failed"
|
|
224
|
+
else None,
|
|
225
|
+
)
|
|
226
|
+
row.update(
|
|
227
|
+
status=record.status,
|
|
228
|
+
classification=record.classification,
|
|
229
|
+
completed_at=record.completed_at,
|
|
230
|
+
outputs_sha256=dict(record.outputs_sha256),
|
|
231
|
+
error=record.error,
|
|
232
|
+
)
|
|
233
|
+
if result.classification != record.classification:
|
|
234
|
+
raise RegistryError(
|
|
235
|
+
f"{record.point_id}: collected classification differs from record"
|
|
236
|
+
)
|
|
237
|
+
row["result"] = result.to_dict()
|
|
238
|
+
if result.classification == "failed":
|
|
239
|
+
failed += 1
|
|
240
|
+
else:
|
|
241
|
+
collected += 1
|
|
242
|
+
except (OSError, ValueError) as error:
|
|
243
|
+
row["error"] = str(error)
|
|
244
|
+
failed += 1
|
|
245
|
+
summaries = tuple(
|
|
246
|
+
_write_summary(target, name, config.solver, rows) for name, rows in sorted(grouped.items())
|
|
247
|
+
)
|
|
248
|
+
tables = tuple(
|
|
249
|
+
path for name, rows in sorted(grouped.items()) for path in write_tables(target, name, rows)
|
|
250
|
+
)
|
|
251
|
+
return CollectReport(collected, failed, outstanding, summaries, tables)
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"""Write the CSV tables of a collected matrix beside its campaign summary.
|
|
2
|
+
|
|
3
|
+
``post/<matrix>/campaign_sweep.csv`` holds one row per recorded point of the
|
|
4
|
+
matrix and ``post/<matrix>/polars/P<POL>.csv`` one row per point of each
|
|
5
|
+
simulation, in the pyflightstream spelling of a polar file's ``P<sim>`` stem
|
|
6
|
+
(cfdbender has no boundary groups, so the stem carries no group suffix). The
|
|
7
|
+
columns are POL, point, the FLIGHT_CONDITION keys in the order the matrix
|
|
8
|
+
states them, status, classification, iterations, residual, wall_time_s,
|
|
9
|
+
solver_version and every coefficient the solver reported. A value a point does
|
|
10
|
+
not have is an empty cell; every table of a matrix has the same columns. The
|
|
11
|
+
tables are derived and rebuilt by every collect.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import csv
|
|
17
|
+
import io
|
|
18
|
+
import os
|
|
19
|
+
import tempfile
|
|
20
|
+
from collections.abc import Mapping, Sequence
|
|
21
|
+
from datetime import datetime
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
from typing import cast
|
|
24
|
+
|
|
25
|
+
from cfdbender.core.plan import workspace_path
|
|
26
|
+
|
|
27
|
+
FIXED = ("status", "classification", "iterations", "residual", "wall_time_s", "solver_version")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _wall(row: Mapping[str, object]) -> float | None:
|
|
31
|
+
start, end = row.get("started_at"), row.get("completed_at")
|
|
32
|
+
if not isinstance(start, str) or not isinstance(end, str):
|
|
33
|
+
return None
|
|
34
|
+
return (datetime.fromisoformat(end) - datetime.fromisoformat(start)).total_seconds()
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _cells(row: Mapping[str, object]) -> dict[str, object]:
|
|
38
|
+
result = cast(Mapping[str, object] | None, row.get("result")) or {}
|
|
39
|
+
history = cast(Sequence[Mapping[str, object]], result.get("history") or ())
|
|
40
|
+
last = history[-1] if history else {}
|
|
41
|
+
cells: dict[str, object] = {
|
|
42
|
+
"POL": row["pol"],
|
|
43
|
+
"point": str(row["point_id"]).split("/", 1)[1],
|
|
44
|
+
**cast(Mapping[str, object], row["condition"]),
|
|
45
|
+
"status": row["status"],
|
|
46
|
+
"classification": row.get("classification"),
|
|
47
|
+
"iterations": last.get("iteration"),
|
|
48
|
+
"residual": last.get("residual"),
|
|
49
|
+
"wall_time_s": _wall(row),
|
|
50
|
+
"solver_version": result.get("solver_version"),
|
|
51
|
+
}
|
|
52
|
+
coefficients = cast(Mapping[str, Mapping[str, object]], result.get("coefficients") or {})
|
|
53
|
+
for name, value in coefficients.items():
|
|
54
|
+
cells[name] = value["value"]
|
|
55
|
+
return cells
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _header(rows: Sequence[Mapping[str, object]]) -> list[str]:
|
|
59
|
+
conditions: dict[str, None] = {}
|
|
60
|
+
coefficients: dict[str, None] = {}
|
|
61
|
+
for row in rows:
|
|
62
|
+
conditions.update(dict.fromkeys(cast(Mapping[str, object], row["condition"])))
|
|
63
|
+
result = cast(Mapping[str, object] | None, row.get("result")) or {}
|
|
64
|
+
names = cast(Mapping[str, object], result.get("coefficients") or {})
|
|
65
|
+
coefficients.update(dict.fromkeys(names))
|
|
66
|
+
return ["POL", "point", *conditions, *FIXED, *(c for c in coefficients if c not in conditions)]
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _text(header: Sequence[str], rows: Sequence[Mapping[str, object]]) -> str:
|
|
70
|
+
stream = io.StringIO()
|
|
71
|
+
writer = csv.writer(stream, lineterminator="\n")
|
|
72
|
+
writer.writerow(header)
|
|
73
|
+
for cell in (_cells(row) for row in rows):
|
|
74
|
+
writer.writerow(["" if cell.get(name) is None else cell[name] for name in header])
|
|
75
|
+
return stream.getvalue()
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _write(target: Path, text: str) -> Path:
|
|
79
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
80
|
+
temporary: Path | None = None
|
|
81
|
+
try:
|
|
82
|
+
with tempfile.NamedTemporaryFile(
|
|
83
|
+
dir=target.parent, prefix=".collect-", delete=False
|
|
84
|
+
) as stream:
|
|
85
|
+
temporary = Path(stream.name)
|
|
86
|
+
stream.write(text.encode("utf-8"))
|
|
87
|
+
stream.flush()
|
|
88
|
+
os.fsync(stream.fileno())
|
|
89
|
+
os.replace(temporary, target)
|
|
90
|
+
temporary = None
|
|
91
|
+
finally:
|
|
92
|
+
if temporary is not None:
|
|
93
|
+
temporary.unlink(missing_ok=True)
|
|
94
|
+
return target
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def write_tables(root: Path, matrix: str, rows: Sequence[Mapping[str, object]]) -> tuple[Path, ...]:
|
|
98
|
+
"""Rebuild campaign_sweep.csv and polars/P<POL>.csv for one matrix.
|
|
99
|
+
|
|
100
|
+
Parameters
|
|
101
|
+
----------
|
|
102
|
+
root : Path
|
|
103
|
+
Workspace root.
|
|
104
|
+
matrix : str
|
|
105
|
+
Recorded matrix path, inputs/matrices/<name>.fs.
|
|
106
|
+
rows : sequence of mapping
|
|
107
|
+
The campaign summary's point rows, in record order.
|
|
108
|
+
|
|
109
|
+
Returns
|
|
110
|
+
-------
|
|
111
|
+
tuple of Path
|
|
112
|
+
The written tables, campaign_sweep.csv first.
|
|
113
|
+
"""
|
|
114
|
+
folder = workspace_path(root, f"post/{Path(matrix).stem}")
|
|
115
|
+
header = _header(rows)
|
|
116
|
+
written = [_write(folder / "campaign_sweep.csv", _text(header, rows))]
|
|
117
|
+
by_pol: dict[str, list[Mapping[str, object]]] = {}
|
|
118
|
+
for row in rows:
|
|
119
|
+
by_pol.setdefault(str(row["pol"]), []).append(row)
|
|
120
|
+
for pol, members in by_pol.items():
|
|
121
|
+
written.append(_write(folder / "polars" / f"P{pol}.csv", _text(header, members)))
|
|
122
|
+
return tuple(written)
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""Decide which planned points a run executes again: resume, force-rerun, archive.
|
|
2
|
+
|
|
3
|
+
Without options a recorded point refuses the run, as before. ``resume`` skips
|
|
4
|
+
COMPLETED and SUBMITTED points and runs the rest. A point left RUNNING by a
|
|
5
|
+
local run that crashed is run again only when no solver process holds this
|
|
6
|
+
host's process slot; a RUNNING point of a cluster submission, whose job may
|
|
7
|
+
exist, is run again only when named by ``force``. ``force`` names recorded
|
|
8
|
+
points to redo, leaving the other recorded points untouched, and ``force_all``
|
|
9
|
+
redoes every recorded point of the plan; each
|
|
10
|
+
is archived first (see RunRegistry.archive) and registered again.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from collections.abc import Callable, Sequence
|
|
16
|
+
from dataclasses import dataclass, replace
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
|
|
19
|
+
from cfdbender.core.plan import Point, RunPlan
|
|
20
|
+
from cfdbender.core.registry import RegistryError, RunRecord, RunRegistry
|
|
21
|
+
from cfdbender.solvers._host._single import process_lease
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True)
|
|
25
|
+
class RerunDecision:
|
|
26
|
+
"""The plan to execute, the points skipped with their reasons, and any archive."""
|
|
27
|
+
|
|
28
|
+
plan: RunPlan
|
|
29
|
+
skipped: tuple[tuple[str, str], ...]
|
|
30
|
+
archive: Path | None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def host_slot_free() -> bool:
|
|
34
|
+
"""Return True when no solver process holds this host's process slot."""
|
|
35
|
+
try:
|
|
36
|
+
with process_lease("probe"):
|
|
37
|
+
return True
|
|
38
|
+
except ValueError:
|
|
39
|
+
return False
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _named(plan: RunPlan, names: Sequence[str]) -> set[str]:
|
|
43
|
+
chosen: set[str] = set()
|
|
44
|
+
for name in names:
|
|
45
|
+
matches = [
|
|
46
|
+
p.point_id
|
|
47
|
+
for p in plan.points
|
|
48
|
+
if name in (p.point_id, p.point_id.split("/", 1)[1], Path(p.directory).name)
|
|
49
|
+
]
|
|
50
|
+
if not matches:
|
|
51
|
+
raise RegistryError(f"--force-rerun {name}: no point of this plan carries that name")
|
|
52
|
+
if len(matches) > 1:
|
|
53
|
+
raise RegistryError(
|
|
54
|
+
f"--force-rerun {name}: names {len(matches)} points; give POL/DP-name"
|
|
55
|
+
)
|
|
56
|
+
chosen.add(matches[0])
|
|
57
|
+
return chosen
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def decide(
|
|
61
|
+
plan: RunPlan,
|
|
62
|
+
*,
|
|
63
|
+
resume: bool = False,
|
|
64
|
+
force: Sequence[str] = (),
|
|
65
|
+
force_all: bool = False,
|
|
66
|
+
submitting: bool = False,
|
|
67
|
+
slot_free: Callable[[], bool] | None = None,
|
|
68
|
+
) -> RerunDecision:
|
|
69
|
+
"""Archive the points to redo and return the plan the run executes.
|
|
70
|
+
|
|
71
|
+
Parameters
|
|
72
|
+
----------
|
|
73
|
+
plan : RunPlan
|
|
74
|
+
Validated plan, already narrowed by any selection.
|
|
75
|
+
resume : bool, optional
|
|
76
|
+
Skip recorded COMPLETED and SUBMITTED points instead of refusing.
|
|
77
|
+
force : sequence of str, optional
|
|
78
|
+
Recorded points to redo, by POL/DP-name or DP-name.
|
|
79
|
+
force_all : bool, optional
|
|
80
|
+
Redo every recorded point of the plan.
|
|
81
|
+
submitting : bool, optional
|
|
82
|
+
True for a cluster submission: a RUNNING point is then never presumed gone.
|
|
83
|
+
slot_free : callable, optional
|
|
84
|
+
Reports whether the host's solver process slot is free; host_slot_free by default.
|
|
85
|
+
|
|
86
|
+
Returns
|
|
87
|
+
-------
|
|
88
|
+
RerunDecision
|
|
89
|
+
Points to execute in plan order, skipped points and the archive folder.
|
|
90
|
+
|
|
91
|
+
Raises
|
|
92
|
+
------
|
|
93
|
+
RegistryError
|
|
94
|
+
Conflicting options, a name no recorded point carries, or an archive failure.
|
|
95
|
+
"""
|
|
96
|
+
if force_all and (force or resume):
|
|
97
|
+
raise RegistryError("--force-rerun-all cannot be combined with --resume or --force-rerun")
|
|
98
|
+
named = _named(plan, force)
|
|
99
|
+
slot_free = slot_free or host_slot_free
|
|
100
|
+
registry = RunRegistry(plan.root)
|
|
101
|
+
recorded: dict[str, RunRecord] = {r.point_id: r for r in registry.read()}
|
|
102
|
+
if unrecorded := sorted(n for n in named if n not in recorded):
|
|
103
|
+
raise RegistryError(f"--force-rerun {unrecorded[0]}: the point has no record to redo")
|
|
104
|
+
redo: list[str] = []
|
|
105
|
+
skipped: list[tuple[str, str]] = []
|
|
106
|
+
run: list[Point] = []
|
|
107
|
+
for point in plan.points:
|
|
108
|
+
record = recorded.get(point.point_id)
|
|
109
|
+
if record is None or record.status == "PLANNED":
|
|
110
|
+
run.append(point)
|
|
111
|
+
elif force_all or point.point_id in named:
|
|
112
|
+
redo.append(point.point_id)
|
|
113
|
+
run.append(point)
|
|
114
|
+
elif not resume and not named:
|
|
115
|
+
run.append(point) # the run refuses it, naming its status
|
|
116
|
+
elif not resume:
|
|
117
|
+
skipped.append((point.point_id, f"{record.status}, not named by --force-rerun"))
|
|
118
|
+
elif record.status == "RUNNING" and not submitting and slot_free():
|
|
119
|
+
redo.append(point.point_id)
|
|
120
|
+
run.append(point)
|
|
121
|
+
elif record.status == "RUNNING":
|
|
122
|
+
skipped.append((point.point_id, "RUNNING and its process may be alive"))
|
|
123
|
+
else:
|
|
124
|
+
skipped.append((point.point_id, record.status))
|
|
125
|
+
archive = registry.archive(tuple(redo)) if redo else None
|
|
126
|
+
return RerunDecision(replace(plan, points=tuple(run)), tuple(skipped), archive)
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Select simulations (POL) and points of a plan or of recorded points.
|
|
2
|
+
|
|
3
|
+
``--sims`` takes POL ids, space or comma separated; ``--points`` takes point
|
|
4
|
+
names, ``DP-name`` or ``POL/DP-name``. An id or name the plan or the records do
|
|
5
|
+
not carry is refused before anything runs.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from collections.abc import Iterable, Sequence
|
|
11
|
+
from dataclasses import replace
|
|
12
|
+
|
|
13
|
+
from cfdbender.core.plan import Point, RunPlan
|
|
14
|
+
from cfdbender.core.registry import RegistryError
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def split_ids(values: Sequence[str] | None) -> tuple[str, ...]:
|
|
18
|
+
"""Split space- and comma-separated command values into ids, keeping order."""
|
|
19
|
+
if not values:
|
|
20
|
+
return ()
|
|
21
|
+
found = [part.strip() for value in values for part in value.split(",")]
|
|
22
|
+
if any(not part for part in found):
|
|
23
|
+
raise RegistryError("an empty id in --sims or --points")
|
|
24
|
+
return tuple(dict.fromkeys(found))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _matches(point: Point, name: str) -> bool:
|
|
28
|
+
return name in (point.point_id, point.point_id.split("/", 1)[1])
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def chosen(points: Iterable[Point], sims: Sequence[str], names: Sequence[str]) -> set[str]:
|
|
32
|
+
"""Return the point ids that the selection keeps, refusing unknown ids.
|
|
33
|
+
|
|
34
|
+
Parameters
|
|
35
|
+
----------
|
|
36
|
+
points : iterable of Point
|
|
37
|
+
Planned or recorded points.
|
|
38
|
+
sims : sequence of str
|
|
39
|
+
POL ids; empty keeps every simulation.
|
|
40
|
+
names : sequence of str
|
|
41
|
+
Point names; empty keeps every point of the kept simulations.
|
|
42
|
+
|
|
43
|
+
Returns
|
|
44
|
+
-------
|
|
45
|
+
set of str
|
|
46
|
+
Point ids.
|
|
47
|
+
"""
|
|
48
|
+
pool = list(points)
|
|
49
|
+
if unknown := [sim for sim in sims if all(p.pol != sim for p in pool)]:
|
|
50
|
+
raise RegistryError(f"--sims {unknown[0]}: no simulation with that POL")
|
|
51
|
+
kept = [p for p in pool if not sims or p.pol in sims]
|
|
52
|
+
if unknown := [name for name in names if not any(_matches(p, name) for p in kept)]:
|
|
53
|
+
raise RegistryError(f"--points {unknown[0]}: no selected point carries that name")
|
|
54
|
+
return {p.point_id for p in kept if not names or any(_matches(p, n) for n in names)}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def select_plan(plan: RunPlan, sims: Sequence[str], names: Sequence[str]) -> RunPlan:
|
|
58
|
+
"""Return the plan narrowed to the selection, in matrix order."""
|
|
59
|
+
if not sims and not names:
|
|
60
|
+
return plan
|
|
61
|
+
keep = chosen(plan.points, sims, names)
|
|
62
|
+
return replace(plan, points=tuple(p for p in plan.points if p.point_id in keep))
|