File size: 2,984 Bytes
fd8b1ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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"]}}