|
| 1 | +import imas |
| 2 | +import numpy as np |
| 3 | +import pytest |
| 4 | + |
| 5 | +# libmuscle and ymmsl are optional dependencies, so may not be installed |
| 6 | +libmuscle = pytest.importorskip("libmuscle") |
| 7 | +ymmsl = pytest.importorskip("ymmsl") |
| 8 | + |
| 9 | +# This cannot be imported if libmuscle is not available |
| 10 | +from waveform_editor.muscle3 import waveform_actor # noqa: E402 |
| 11 | + |
| 12 | +# imas_core is required for IDS serialize, unfortunately this means we cannot run these |
| 13 | +# tests in github Actions yet.. |
| 14 | +pytest.importorskip("imas_core") |
| 15 | + |
| 16 | + |
| 17 | +WAVEFORM_YAML = """ |
| 18 | +ec_launchers: |
| 19 | + beams: |
| 20 | + ec_launchers/beam(1)/phase/angle: 1 |
| 21 | + ec_launchers/beam(2)/phase/angle: 2 |
| 22 | + ec_launchers/beam(3)/phase/angle: 3 |
| 23 | + ec_launchers/beam(4)/power_launched/data: |
| 24 | + - {to: 8.33e5, duration: 20} |
| 25 | + - {type: constant, duration: 20} |
| 26 | + - {duration: 25, to: 0} |
| 27 | +globals: |
| 28 | + dd_version: 4.0.0 |
| 29 | +""" |
| 30 | +TIMES = [1, 21, 50] |
| 31 | +VALUES_PER_TIME = [8.33e5 / 20, 8.33e5, 8.33e5 * 15 / 25] |
| 32 | + |
| 33 | +YMMSL = """ |
| 34 | +ymmsl_version: v0.1 |
| 35 | +
|
| 36 | +model: |
| 37 | + name: test_waveform_actor |
| 38 | +
|
| 39 | + components: |
| 40 | + time_generator: |
| 41 | + implementation: time_generator |
| 42 | + waveform_actor: |
| 43 | + implementation: waveform_actor |
| 44 | + waveform_validator: |
| 45 | + implementation: waveform_validator |
| 46 | +
|
| 47 | + conduits: |
| 48 | + time_generator.output: waveform_actor.input |
| 49 | + waveform_actor.ec_launchers_out: waveform_validator.ec_launchers_in |
| 50 | +
|
| 51 | +settings: |
| 52 | + waveform_actor.waveforms: {waveform_yaml} |
| 53 | +""" |
| 54 | + |
| 55 | + |
| 56 | +def time_generator(): |
| 57 | + instance = libmuscle.Instance({ymmsl.Operator.O_I: ["output"]}) |
| 58 | + |
| 59 | + while instance.reuse_instance(): |
| 60 | + for t in TIMES: |
| 61 | + instance.send("output", libmuscle.Message(t)) |
| 62 | + |
| 63 | + |
| 64 | +def waveform_validator(): |
| 65 | + instance = libmuscle.Instance({ymmsl.Operator.F_INIT: ["ec_launchers_in"]}) |
| 66 | + |
| 67 | + i = 0 |
| 68 | + while instance.reuse_instance(): |
| 69 | + msg = instance.receive("ec_launchers_in") |
| 70 | + assert msg.timestamp == TIMES[i] |
| 71 | + |
| 72 | + ids = imas.IDSFactory("4.0.0").ec_launchers() |
| 73 | + ids.deserialize(msg.data) |
| 74 | + |
| 75 | + assert np.array_equal(ids.time, [TIMES[i]]) |
| 76 | + assert len(ids.beam) == 4 |
| 77 | + assert np.array_equal(ids.beam[0].phase.angle, [1]) |
| 78 | + assert np.array_equal(ids.beam[1].phase.angle, [2]) |
| 79 | + assert np.array_equal(ids.beam[2].phase.angle, [3]) |
| 80 | + assert np.allclose(ids.beam[3].power_launched.data, [VALUES_PER_TIME[i]]) |
| 81 | + |
| 82 | + i += 1 |
| 83 | + assert i == len(TIMES) |
| 84 | + |
| 85 | + |
| 86 | +# Running `os.fork()` after `import pandas` triggers this warning... |
| 87 | +# It doesn't seem to be an issue (and not relevant in production where muscle_manager |
| 88 | +# will start the actor in a standalone process), so we'll ignore this warning: |
| 89 | +@pytest.mark.filterwarnings("ignore:.*use of fork():DeprecationWarning") |
| 90 | +def test_muscle3(tmp_path, monkeypatch): |
| 91 | + monkeypatch.chdir(tmp_path) |
| 92 | + waveform_yaml = (tmp_path / "waveform.yml").resolve() |
| 93 | + waveform_yaml.write_text(WAVEFORM_YAML) |
| 94 | + configuration = ymmsl.load(YMMSL.format(waveform_yaml=waveform_yaml)) |
| 95 | + implementations = { |
| 96 | + "time_generator": time_generator, |
| 97 | + "waveform_actor": waveform_actor, |
| 98 | + "waveform_validator": waveform_validator, |
| 99 | + } |
| 100 | + libmuscle.runner.run_simulation(configuration, implementations) |
0 commit comments