/ challenges-old / string-solutions
Sign in
challenges-old / string-solutions
string-solutions / solvers / fields.py
29 lines · 1.1 KBCodeBlameHistory
8f29519Export displacement fields for the chain tops, add this repository's own site with a field viewer, and leave the comparison to the challenge repositoryJeremy Magland 1"""Writing the optional field export (HDF5) described in the challenge README."""
2from __future__ import annotations
4from pathlib import Path
6import h5py
7import numpy as np
10def field_grid(challenge: dict):
11 """The recommended export grid for a challenge, from challenge.json."""
12 fg = challenge["fields"]
13 p = challenge["parameters"]
15 x = np.linspace(0.0, p["L"], int(nx))
16 t = np.arange(int(nt)) / float(fg["frame_rate_hz"])
20def write_fields(path: Path, x: np.ndarray, t: np.ndarray, u: np.ndarray, challenge: dict, solution_id: str) -> None:
21 path.parent.mkdir(parents=True, exist_ok=True)
22 with h5py.File(path, "w") as f:
23 f.attrs["challenge_id"] = challenge["challenge_id"]
24 f.attrs["solution_id"] = solution_id
25 f.attrs["quantity"] = "transverse displacement"
26 f.attrs["units"] = "m"
27 f.create_dataset("x", data=np.asarray(x, dtype=np.float64))
28 f.create_dataset("t", data=np.asarray(t, dtype=np.float64))
29 f.create_dataset("u", data=np.asarray(u, dtype=np.float32), compression="gzip", compression_opts=4, chunks=(min(64, len(t)), len(x)))
↑↓ move↵openescclose