ymf 0.3.0__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.
- ymf/__init__.py +149 -0
- ymf/archive.py +2104 -0
- ymf/boundaries.py +119 -0
- ymf/cli.py +307 -0
- ymf/closure.py +340 -0
- ymf/compose.py +613 -0
- ymf/conditions.py +193 -0
- ymf/data_sources.py +311 -0
- ymf/discretizations.py +303 -0
- ymf/dispersion.py +160 -0
- ymf/domain.py +235 -0
- ymf/explicit.py +213 -0
- ymf/generated.py +197 -0
- ymf/mesh.py +271 -0
- ymf/names.py +174 -0
- ymf/normalize.py +168 -0
- ymf/references.py +509 -0
- ymf/schema.py +764 -0
- ymf/spaces.py +145 -0
- ymf/stabilization.py +214 -0
- ymf/state.py +122 -0
- ymf/symbolic/__init__.py +17 -0
- ymf/symbolic/adr.py +357 -0
- ymf/symbolic/language.py +244 -0
- ymf/symbolic/notation.py +447 -0
- ymf/symbolic/printer.py +207 -0
- ymf/symbolic/problem.py +914 -0
- ymf/symbolic/split.py +568 -0
- ymf/units.py +1016 -0
- ymf/vocabulary.py +660 -0
- ymf/xdmf.py +728 -0
- ymf-0.3.0.dist-info/METADATA +252 -0
- ymf-0.3.0.dist-info/RECORD +37 -0
- ymf-0.3.0.dist-info/WHEEL +5 -0
- ymf-0.3.0.dist-info/entry_points.txt +2 -0
- ymf-0.3.0.dist-info/licenses/LICENSE +21 -0
- ymf-0.3.0.dist-info/top_level.txt +1 -0
ymf/closure.py
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
"""The archive as the closure of a spec.
|
|
2
|
+
|
|
3
|
+
A spec is the input; an archive is that spec, as realized, followed by its
|
|
4
|
+
``outputs``. Each output is one discretization run on one mesh (one
|
|
5
|
+
*realization*), and is keyed by what produced it::
|
|
6
|
+
|
|
7
|
+
ymf: 0.3.0
|
|
8
|
+
model: ...
|
|
9
|
+
solution_paths: ...
|
|
10
|
+
outputs:
|
|
11
|
+
P1/cells=8/3f2a9c01d4e7:
|
|
12
|
+
input: {sha256: 3f2a9c01d4e7...}
|
|
13
|
+
realization: {cells: 8, levels: 1}
|
|
14
|
+
transformations: ...
|
|
15
|
+
approximation: {...} # the XDMF Domain
|
|
16
|
+
|
|
17
|
+
The key hashes the output's slice of the input, whole: the model without
|
|
18
|
+
its weak forms, the one weak form the discretization uses, the analytical
|
|
19
|
+
solutions it is checked against, the one discretization entry (with its
|
|
20
|
+
studied parameters replaced by the realization's values) and the
|
|
21
|
+
realization. Prose is included, so a key is a fingerprint of the input text
|
|
22
|
+
as well as of its numbers. Anything that changes the slice changes the key;
|
|
23
|
+
editing another discretization, or another weak form, does not.
|
|
24
|
+
|
|
25
|
+
Until 2026-10-18 (between releases: no release wrote archives) the slice
|
|
26
|
+
held the whole model block, every weak form included, so adding a weak form
|
|
27
|
+
re-keyed every output. An output keyed that way is recognised by
|
|
28
|
+
:func:`partition`, which re-keys it rather than drop it when its input is
|
|
29
|
+
otherwise unchanged (see :func:`legacy_input_slice`).
|
|
30
|
+
|
|
31
|
+
A discretization's ``mesh.cells`` or ``mesh.h``, its ``mesh.levels`` (and its
|
|
32
|
+
``discretization.dt``) may be lists: a study. Each combination is one
|
|
33
|
+
realization, and one output.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
from __future__ import annotations
|
|
37
|
+
|
|
38
|
+
import copy
|
|
39
|
+
import hashlib
|
|
40
|
+
import itertools
|
|
41
|
+
import json
|
|
42
|
+
from pathlib import Path
|
|
43
|
+
from typing import Any, Dict, Iterator, List, Optional, Tuple
|
|
44
|
+
|
|
45
|
+
from ymf.archive import write_document
|
|
46
|
+
|
|
47
|
+
__all__ = [
|
|
48
|
+
"KEY_DIGITS",
|
|
49
|
+
"realizations",
|
|
50
|
+
"realize",
|
|
51
|
+
"plain",
|
|
52
|
+
"used_weak_form",
|
|
53
|
+
"input_slice",
|
|
54
|
+
"legacy_input_slice",
|
|
55
|
+
"input_digest",
|
|
56
|
+
"output_key",
|
|
57
|
+
"planned_outputs",
|
|
58
|
+
"partition",
|
|
59
|
+
"file_stem",
|
|
60
|
+
"archive_stem",
|
|
61
|
+
"load",
|
|
62
|
+
"write",
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
#: hex digits of the digest shown in a key (the full digest is in the output)
|
|
66
|
+
KEY_DIGITS = 12
|
|
67
|
+
|
|
68
|
+
# The studied parameters -- which fields a study may list -- belong to each
|
|
69
|
+
# discretization method: see ymf.discretizations.
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _get(entry: Dict[str, Any], path: Tuple[str, ...]) -> Any:
|
|
73
|
+
for part in path:
|
|
74
|
+
if not isinstance(entry, dict) or part not in entry:
|
|
75
|
+
return None
|
|
76
|
+
entry = entry[part]
|
|
77
|
+
return entry
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _set(entry: Dict[str, Any], path: Tuple[str, ...], value: Any) -> None:
|
|
81
|
+
for part in path[:-1]:
|
|
82
|
+
entry = entry[part]
|
|
83
|
+
entry[path[-1]] = value
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def realizations(discretization: Dict[str, Any], domain: Any = None) -> List[Dict[str, Any]]:
|
|
87
|
+
"""The realizations a discretization entry asks for, in study order
|
|
88
|
+
(``domain``: the strong form's, which decides what a mesh needs).
|
|
89
|
+
|
|
90
|
+
Each is a mapping such as ``{"cells": 8, "levels": 1}`` or
|
|
91
|
+
``{"h": 0.1, "levels": 1}`` (and ``"dt"`` when the entry steps in time).
|
|
92
|
+
An entry without ``mesh`` asks for none: the mesh must then come from
|
|
93
|
+
the command line, as an override. ``cells`` and ``h`` are alternatives;
|
|
94
|
+
an entry giving both is refused.
|
|
95
|
+
"""
|
|
96
|
+
from ymf.discretizations import method_of
|
|
97
|
+
mesh = _get(discretization, ("mesh",))
|
|
98
|
+
if isinstance(mesh, dict) and "cells" in mesh and "h" in mesh:
|
|
99
|
+
raise ValueError("discretization %r: mesh gives both cells and h; "
|
|
100
|
+
"cells is for a box, h for any other region"
|
|
101
|
+
% (discretization.get("name"),))
|
|
102
|
+
method = method_of(discretization)
|
|
103
|
+
if method.missing(discretization, domain):
|
|
104
|
+
return [] # see ymf.discretizations.missing
|
|
105
|
+
block = _get(discretization, (method.block,))
|
|
106
|
+
axes = []
|
|
107
|
+
for name, path in method.axes():
|
|
108
|
+
value = _get(discretization, path)
|
|
109
|
+
if value is None:
|
|
110
|
+
if name in method.defaults and block is not None:
|
|
111
|
+
axes.append((name, [method.defaults[name]]))
|
|
112
|
+
continue
|
|
113
|
+
axes.append((name, list(value) if isinstance(value, (list, tuple)) else [value]))
|
|
114
|
+
names = [name for name, _ in axes]
|
|
115
|
+
return [dict(zip(names, combo)) for combo in itertools.product(*(v for _, v in axes))]
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def realize(discretization: Dict[str, Any], realization: Dict[str, Any]) -> Dict[str, Any]:
|
|
119
|
+
"""The discretization entry with each studied list replaced by one value."""
|
|
120
|
+
from ymf.discretizations import method_of
|
|
121
|
+
entry = copy.deepcopy(plain(discretization))
|
|
122
|
+
for name, path in method_of(entry).axes():
|
|
123
|
+
if name in realization and _get(entry, path[:-1]) is not None:
|
|
124
|
+
_set(entry, path, realization[name])
|
|
125
|
+
return entry
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def plain(value: Any) -> Any:
|
|
129
|
+
"""strictyaml's mappings and sequences, as plain dicts and lists."""
|
|
130
|
+
if isinstance(value, dict):
|
|
131
|
+
return {str(k): plain(v) for k, v in value.items()}
|
|
132
|
+
if isinstance(value, (list, tuple)):
|
|
133
|
+
return [plain(v) for v in value]
|
|
134
|
+
return value
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _discretization(doc: Dict[str, Any], name: str) -> Dict[str, Any]:
|
|
138
|
+
for entry in doc["solution_paths"]["discretizations"]:
|
|
139
|
+
if entry["name"] == name:
|
|
140
|
+
return entry
|
|
141
|
+
raise KeyError("no discretization named %r" % (name,))
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def used_weak_form(doc: Dict[str, Any], discretization: Dict[str, Any]
|
|
145
|
+
) -> Optional[Dict[str, Any]]:
|
|
146
|
+
"""The weak form a discretization entry solves, or None if it names none.
|
|
147
|
+
|
|
148
|
+
Its ``from_weak_form``; the composer fills that in where the document has
|
|
149
|
+
one weak form, and this does the same for a document built without it.
|
|
150
|
+
"""
|
|
151
|
+
weak_forms = list((doc.get("model") or {}).get("weak_forms") or [])
|
|
152
|
+
label = discretization.get("from_weak_form")
|
|
153
|
+
if label is None:
|
|
154
|
+
return weak_forms[0] if len(weak_forms) == 1 else None
|
|
155
|
+
for weak_form in weak_forms:
|
|
156
|
+
if str(weak_form.get("label")) == str(label):
|
|
157
|
+
return weak_form
|
|
158
|
+
raise KeyError("discretization %r names weak form %r, which the model does not have"
|
|
159
|
+
% (discretization.get("name"), label))
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def input_slice(doc: Dict[str, Any], discretization: str,
|
|
163
|
+
realization: Dict[str, Any]) -> Dict[str, Any]:
|
|
164
|
+
"""The part of the input one output depends on (see the module docstring).
|
|
165
|
+
|
|
166
|
+
Taken from the document as a closed spec records it (ymf.explicit: the
|
|
167
|
+
boundary-condition aliases and condition sets expanded, the sub-problems
|
|
168
|
+
written out), so a spec and the archive it closes into key their outputs
|
|
169
|
+
alike; a spec with no shorthand is unchanged by it. What closure derives
|
|
170
|
+
from the input alone (a coefficient's ``depends_on``) is left out: the
|
|
171
|
+
formula it is read from is in the slice already.
|
|
172
|
+
"""
|
|
173
|
+
from ymf.explicit import explicit, without_derived
|
|
174
|
+
doc = without_derived(explicit(doc))
|
|
175
|
+
paths = doc.get("solution_paths") or {}
|
|
176
|
+
entry = _discretization(doc, discretization)
|
|
177
|
+
return plain({
|
|
178
|
+
"model": {k: v for k, v in doc["model"].items() if k != "weak_forms"},
|
|
179
|
+
"weak_form": used_weak_form(doc, entry),
|
|
180
|
+
"analytical": paths.get("analytical") or [],
|
|
181
|
+
"discretization": realize(entry, realization),
|
|
182
|
+
"realization": realization,
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def legacy_input_slice(doc: Dict[str, Any], discretization: str,
|
|
187
|
+
realization: Dict[str, Any]) -> Dict[str, Any]:
|
|
188
|
+
"""The slice until 2026-10-18: the whole model block, every weak form in it.
|
|
189
|
+
|
|
190
|
+
Kept only to recognise outputs keyed that way, so they can be re-keyed
|
|
191
|
+
instead of recomputed.
|
|
192
|
+
"""
|
|
193
|
+
paths = doc.get("solution_paths") or {}
|
|
194
|
+
return plain({
|
|
195
|
+
"model": doc["model"],
|
|
196
|
+
"analytical": paths.get("analytical") or [],
|
|
197
|
+
"discretization": realize(_discretization(doc, discretization), realization),
|
|
198
|
+
"realization": realization,
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _digest(slice_: Dict[str, Any]) -> str:
|
|
203
|
+
text = json.dumps(slice_, sort_keys=True, ensure_ascii=False, separators=(",", ":"))
|
|
204
|
+
return hashlib.sha256(text.encode("utf-8")).hexdigest()
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def input_digest(doc: Dict[str, Any], discretization: str,
|
|
208
|
+
realization: Dict[str, Any]) -> str:
|
|
209
|
+
"""sha256 of the input slice in a canonical JSON form."""
|
|
210
|
+
return _digest(input_slice(doc, discretization, realization))
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def _key(doc: Dict[str, Any], discretization: str, realization: Dict[str, Any],
|
|
214
|
+
digest: str) -> str:
|
|
215
|
+
from ymf.discretizations import method_of
|
|
216
|
+
method = method_of(_discretization(doc, discretization))
|
|
217
|
+
return "/".join([discretization] + method.key_parts(realization)
|
|
218
|
+
+ [digest[:KEY_DIGITS]])
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def output_key(doc: Dict[str, Any], discretization: str,
|
|
222
|
+
realization: Dict[str, Any]) -> str:
|
|
223
|
+
"""``<discretization>/cells=N[/levels=L][/dt=T]/<digest prefix>``,
|
|
224
|
+
with ``h=H`` in place of ``cells=N`` for an unstructured mesh, and each
|
|
225
|
+
other method's own axes (ymf.discretizations) in place of the mesh's."""
|
|
226
|
+
return _key(doc, discretization, realization,
|
|
227
|
+
input_digest(doc, discretization, realization))
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def planned_outputs(doc: Dict[str, Any]) -> Iterator[Tuple[str, str, Dict[str, Any]]]:
|
|
231
|
+
"""Every (key, discretization name, realization) the spec asks for."""
|
|
232
|
+
for entry in doc["solution_paths"]["discretizations"]:
|
|
233
|
+
for realization in realizations(entry, _domain_of(doc)):
|
|
234
|
+
yield output_key(doc, entry["name"], realization), entry["name"], realization
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def _domain_of(doc: Dict[str, Any]) -> Any:
|
|
238
|
+
"""The strong form's domain, which decides what a mesh needs."""
|
|
239
|
+
return ((doc.get("model") or {}).get("strong_form") or {}).get("domain")
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def _base_key(key: str) -> str:
|
|
243
|
+
"""A key without its ``~N`` suffix (a kept, differing rerun)."""
|
|
244
|
+
return key.split("~", 1)[0]
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def load(path: str | Path, check_state: bool = True
|
|
248
|
+
) -> Tuple[Dict[str, Any], Dict[str, Any], Dict[str, str]]:
|
|
249
|
+
"""Load a spec or archive: ``(spec, outputs, dropped)``.
|
|
250
|
+
|
|
251
|
+
``spec`` is the composed, validated input. ``outputs`` are those -- the
|
|
252
|
+
file's own and any inherited through ``extends`` -- whose key, recomputed
|
|
253
|
+
against the composed input, is still theirs. ``dropped`` maps every other
|
|
254
|
+
key to why: an inherited output whose input the overrides changed belongs
|
|
255
|
+
to the old archive, not to this one. ``check_state`` is
|
|
256
|
+
:func:`ymf.compose.load_composed`'s: False for a runner that brings an
|
|
257
|
+
over-claiming archive up to date.
|
|
258
|
+
"""
|
|
259
|
+
from ymf.compose import load_composed
|
|
260
|
+
spec = load_composed(path, check_state=check_state)
|
|
261
|
+
outputs, dropped = partition(spec, spec.pop("outputs", None) or {})
|
|
262
|
+
return spec, outputs, dropped
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def partition(spec: Dict[str, Any], outputs: Dict[str, Any]
|
|
266
|
+
) -> Tuple[Dict[str, Any], Dict[str, str]]:
|
|
267
|
+
"""Split ``outputs`` into those ``spec`` still produces, and the rest.
|
|
268
|
+
|
|
269
|
+
Returns ``(kept, dropped)``; ``dropped`` maps each other key to why.
|
|
270
|
+
|
|
271
|
+
An output keyed by the slice used until 2026-10-18 (:func:`legacy_input_slice`)
|
|
272
|
+
whose input is otherwise unchanged is kept under its current key, with
|
|
273
|
+
``input.sha256`` brought up to date and ``input.rekeyed_from`` naming
|
|
274
|
+
the key it had. Its files are not renamed: an output records its own
|
|
275
|
+
(``storage``), and only new outputs are named by their key.
|
|
276
|
+
"""
|
|
277
|
+
kept, dropped = {}, {}
|
|
278
|
+
for key, output in outputs.items():
|
|
279
|
+
base = _base_key(key)
|
|
280
|
+
name = base.split("/", 1)[0]
|
|
281
|
+
realization = (output or {}).get("realization")
|
|
282
|
+
if not realization:
|
|
283
|
+
dropped[key] = "it records no realization"
|
|
284
|
+
continue
|
|
285
|
+
try:
|
|
286
|
+
current = output_key(spec, name, realization)
|
|
287
|
+
except KeyError:
|
|
288
|
+
dropped[key] = "the discretization %r is not in this input" % (name,)
|
|
289
|
+
continue
|
|
290
|
+
if current == base:
|
|
291
|
+
kept[key] = output
|
|
292
|
+
elif _key(spec, name, realization,
|
|
293
|
+
_digest(legacy_input_slice(spec, name, realization))) == base:
|
|
294
|
+
output = copy.deepcopy(output)
|
|
295
|
+
output["input"] = dict(output.get("input") or {},
|
|
296
|
+
sha256=input_digest(spec, name, realization),
|
|
297
|
+
rekeyed_from=key)
|
|
298
|
+
kept[current + key[len(base):]] = output
|
|
299
|
+
else:
|
|
300
|
+
dropped[key] = "its input changed (it would now be %s)" % (current,)
|
|
301
|
+
return kept, dropped
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def file_stem(stem: str, key: str) -> str:
|
|
305
|
+
"""The name of an output's files: ``poisson`` + key -> ``poisson_3f2a9c01d4e7``.
|
|
306
|
+
|
|
307
|
+
A kept, differing rerun (``key~2``) gets ``poisson_3f2a9c01d4e7_2``.
|
|
308
|
+
"""
|
|
309
|
+
base, _, n = key.partition("~")
|
|
310
|
+
return "%s_%s%s" % (stem, base.rsplit("/", 1)[-1], "_" + n if n else "")
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def archive_stem(path: str | Path) -> str:
|
|
314
|
+
"""``poisson.ymf`` and ``poisson.archive.ymf`` -> ``poisson``."""
|
|
315
|
+
name = Path(path).name
|
|
316
|
+
for suffix in (".ymf", ".archive"):
|
|
317
|
+
if name.endswith(suffix):
|
|
318
|
+
name = name[: -len(suffix)]
|
|
319
|
+
return name
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def write(path: str | Path, spec: Dict[str, Any], outputs: Dict[str, Any]) -> None:
|
|
323
|
+
"""Write an archive: ``ymf:``, its ``ymf_state``, the realized spec, then
|
|
324
|
+
``outputs``. The state is the one the archive reaches (see ymf.state),
|
|
325
|
+
whatever its input declared. The realized spec is written out
|
|
326
|
+
(ymf.explicit): boundary-condition aliases and condition sets expanded,
|
|
327
|
+
the coefficients of the state named with what they follow, every
|
|
328
|
+
sub-problem with its solver, regularization and lags in full."""
|
|
329
|
+
from ymf.explicit import explicit
|
|
330
|
+
from ymf.state import reached
|
|
331
|
+
# the closure records what was solved, with nothing left to a reader's
|
|
332
|
+
# defaults (ymf.explicit)
|
|
333
|
+
document = dict(plain(explicit(spec)))
|
|
334
|
+
document.pop("outputs", None)
|
|
335
|
+
document.pop("ymf_state", None)
|
|
336
|
+
document.pop("generated", None) # it describes the input file, not this one
|
|
337
|
+
if outputs:
|
|
338
|
+
document["outputs"] = outputs
|
|
339
|
+
state, _ = reached(document)
|
|
340
|
+
write_document(path, dict({"ymf_state": state}, **document))
|