Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/jmrplens/phonometry. Standards-conformant acoustics library for Python: measurement and metrology, docs.jmrp.io/phonometry
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139# Copyright (c) 2026. Jose Manuel Requena Plens"""A remote run on a real node, over a real ssh, equals the run here.
The CI job "Remote simulation on a localhost node" builds the cpu stage ofthe node image with the pins it installs itself, serves it through sshd onlocalhost, names it in a hosts file and sets PHONOMETRY_TEST_NODE to thatname. Without it this module skips: it needs Docker and an sshd that onlythat job sets up. The run here and the run there share the pins, the Clibrary and the CPU, so they agree bit for bit."""
from __future__ import annotations
import dataclassesimport jsonimport loggingimport osfrom typing import TYPE_CHECKING
import numpy as npimport pytestfrom test_remote_simulation import _assert_same_result
from phonometry.simulation import ( Compute, ComputeRemoteError, ElasticRecording, ElasticRun, ExplosionSource, FDTDBoundaries, FDTDRecording, FDTDRun, GaussianPulse, elastic_fdtd_simulation, fdtd_simulation,)from phonometry.simulation._remote import hosts, package
if TYPE_CHECKING: from pathlib import Path
NODE = os.environ.get("PHONOMETRY_TEST_NODE", "")pytestmark = [ pytest.mark.skipif( not NODE, reason="PHONOMETRY_TEST_NODE names no node; the remote-node CI job sets one up", ), pytest.mark.xdist_group("localhost-node"),]
def _acoustic(duration: float) -> FDTDRun: mask = np.zeros((120, 160), dtype=bool) mask[50:70, 90:95] = True return FDTDRun( c=343.0, dx=0.01, duration_s=duration, shape=(120, 160), sources=[GaussianPulse(ix=30, iy=60, half_width_s=4e-5)], recording=FDTDRecording( probes=[(120, 60), (80, 20)], snapshot_every=25, snapshot_decimation=2 ), boundaries=FDTDBoundaries(sides="absorbing", absorbing_layer_cells=12), obstacle_mask=mask, )
def test_a_remote_acoustic_run_equals_the_run_here() -> None: # One run for both, as the tests over fake_ssh.py in # test_remote_simulation.py do: a record's sources are compared, and a # bound waveform built twice is two. run = _acoustic(4e-3) here = fdtd_simulation(run) there = fdtd_simulation(run, compute=Compute(host=NODE)) _assert_same_result(there, here) assert there.provenance.host == NODE assert there.provenance.package_sha256 == package.installed_package().digest assert ( dataclasses.replace(there.provenance, host=None, package_sha256=None) == here.provenance )
def test_a_remote_elastic_run_equals_the_run_here() -> None: run = ElasticRun( c_p=4000.0, c_s=2300.0, dx=0.01, duration_s=4e-4, rho=2500.0, shape=(60, 61), sources=[ ExplosionSource( ix=30, iy=29, waveform=GaussianPulse(ix=0, iy=0, half_width_s=1e-5).value, ) ], recording=ElasticRecording( probes=[(10, 10), (40, 20)], probe_fields=("p", "vy"), snapshot_every=10 ), ) here = elastic_fdtd_simulation(run) there = elastic_fdtd_simulation(run, compute=Compute(host=NODE)) _assert_same_result(there, here) assert there.provenance.host == NODE
def test_the_second_run_finds_the_package_on_the_node( caplog: pytest.LogCaptureFixture,) -> None: caplog.set_level(logging.INFO, logger="phonometry.simulation") fdtd_simulation(_acoustic(1e-3), compute=Compute(host=NODE)) caplog.clear() fdtd_simulation(_acoustic(1e-3), compute=Compute(host=NODE)) messages = [record.getMessage() for record in caplog.records] assert not any("sending the package" in m for m in messages) assert any(m.endswith("fdtd_simulation done") for m in messages)
def test_a_node_without_the_image_asks_for_make_node_image( tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: entry = hosts.load_host(NODE) file = tmp_path / "hosts.toml" file.write_text( "[hosts.absent]\n" f"ssh = {json.dumps(entry.ssh)}\n" 'runner = "docker"\n' 'image = "phonometry-node:absent"\n', encoding="utf-8", ) monkeypatch.setenv("PHONOMETRY_HOSTS_FILE", str(file)) run = _acoustic(1e-3) request = Compute(host="absent") with pytest.raises(ComputeRemoteError, match="make node-image HOST=absent"): fdtd_simulation(run, compute=request)