Download sample/tasks/inspect/chainlib.py from zero-trust-physics/zero-trust-physics-sample-group-4: direct link, hf CLI and curl.
- Browser
- Download file 2.98 kB
-
https://huggingface.co/datasets/zero-trust-physics/zero-trust-physics-sample-group-4/resolve/main/sample/tasks/inspect/chainlib.py
- Command line
-
hf download hf://datasets/zero-trust-physics/zero-trust-physics-sample-group-4/sample/tasks/inspect/chainlib.py
-
curl -L -o chainlib.py https://huggingface.co/datasets/zero-trust-physics/zero-trust-physics-sample-group-4/resolve/main/sample/tasks/inspect/chainlib.py
2.98 kB
| """Build the solve request of a chained design from a run's answer: the history its coupling hands over. | |
| A coupling hands values from the run's recorded frames into the solve bit for bit: each frame's | |
| 32-bit slot widened exactly to 64 bits. The frame layout is in describe_card (frame_slots). This | |
| module reads the slots from frame_hex with the standard library and lays the history out the way a | |
| task states it. | |
| chained_solve(layout, parameters, run_request, run_answer) -> the solve's arguments, with from | |
| layout is the task's check.design.chain.layout: {"kind": "stardust_slab", "cool_to_s": 600.0} or | |
| {"kind": "triga_element"}. | |
| Licence: Apache-2.0. | |
| """ | |
| import copy | |
| import struct | |
| def _slots(run_answer, f64_at, f32_at): | |
| """Per recorded frame: the f64 at f64_at and each f32 at f32_at, widened.""" | |
| out = [[] for _ in range(1 + len(f32_at))] | |
| for frame in run_answer["frames"]: | |
| raw = bytes.fromhex(frame["frame_hex"]) | |
| out[0].append(struct.unpack("<d", raw[f64_at:f64_at + 8])[0]) | |
| for i, at in enumerate(f32_at): | |
| out[i + 1].append(float(struct.unpack("<f", raw[at:at + 4])[0])) | |
| return out | |
| def stardust_history(run_answer, cool_to_s): | |
| """Rows: 0 s unheated, then each frame (time, stagnation pressure, heat-transfer coefficient, recovery | |
| enthalpy), then the stream off 1 s after the last frame and at cool_to_s, holding the last frame's | |
| pressure and enthalpy.""" | |
| time, pressure, enthalpy, coefficient = _slots(run_answer, 0, (12, 20, 24)) | |
| rows = len(time) + 3 | |
| return { | |
| "history_time_s": [0.0] + time + [time[-1] + 1.0, cool_to_s], | |
| "history_pressure_pa": [pressure[0]] + pressure + [pressure[-1], pressure[-1]], | |
| "history_heat_transfer_coefficient_kg_per_m2_s": [0.0] + coefficient + [0.0, 0.0], | |
| "history_flow_enthalpy_j_per_kg": [enthalpy[0]] + enthalpy + [enthalpy[-1], enthalpy[-1]], | |
| "history_radiative_heating_w_per_m2": [0.0] * rows, | |
| } | |
| def triga_history(run_request, run_answer): | |
| time, energy = _slots(run_answer, 0, (12,)) | |
| return {"start_temperature_k": run_request["initial"]["start_temperature_k"], "time_s": time, | |
| "core_energy_released_mws": energy} | |
| def chained_solve(chain, run_request, run_answer, overrides=None): | |
| """The solve arguments for one design: the task's parameters, the history the run hands, and from.""" | |
| parameters = copy.deepcopy(chain["parameters"]) | |
| parameters.update(overrides or {}) | |
| layout = chain["layout"] | |
| if layout["kind"] == "stardust_slab": | |
| parameters.update(stardust_history(run_answer, layout["cool_to_s"])) | |
| elif layout["kind"] == "triga_element": | |
| parameters.update(triga_history(run_request, run_answer)) | |
| else: | |
| raise ValueError("unknown layout %s" % layout["kind"]) | |
| return {"solver": chain["solver"], "parameters": parameters, | |
| "from": {"request": run_request, "run_seal": run_answer["run_seal"], "coupling": chain["coupling"]}} | |