Skip to content

Commit

Permalink
Add tests for native engine
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Aug 30, 2024
1 parent 354f131 commit 3ef4b99
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 27 deletions.
20 changes: 18 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ jobs:
cmake_prefix_path: "/opt/intel/oneapi/tbb/latest"
cmake_flags: ""
cxx_flags: "-march=x86-64-v2"
python_version: "3.12"

- name: macOS-X64
os: macos-13
osx_target: "10.15"
cmake_prefix_path: ""
cmake_flags: '-D OpenMP_ROOT=`brew list libomp | grep libomp.a | sed -E "s/\/lib\/.*//"`'
cxx_flags: "-march=x86-64-v2"
python_version: "3.12"

- name: macOS-ARM64
os: macos-14
osx_target: "11.0"
cmake_prefix_path: ""
cmake_flags: '-D OpenMP_ROOT=`brew list libomp | grep libomp.a | sed -E "s/\/lib\/.*//"`'
cxx_flags: ""
python_version: "3.12"

# - name: Windows
# os: windows-latest
Expand All @@ -56,6 +59,10 @@ jobs:
with:
lfs: true

- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python_version }}

- name: Install dependencies (Linux)
if: runner.os == 'Linux'
run: |
Expand Down Expand Up @@ -105,5 +112,14 @@ jobs:
- name: CMake build
run: cmake --build build

# - name: CTest
# run: ctest --test-dir build -C Release --output-on-failure -j 1
- name: Build and install (Python)
run: pip3 install --verbose .[test]

- name: Test (Python)
run: pytest --cov=pffdtd --cov-branch

- name: Upload coverage report
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
37 changes: 26 additions & 11 deletions src/python/pffdtd/analysis/room_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,7 @@ def frequency_spacing_index(modes):
return psi / (num-1)


@click.command(name="room-modes", help="Plot room modes.")
@click.argument('filename', nargs=-1, type=click.Path(exists=True))
@click.option('--data_dir', type=click.Path(exists=True))
@click.option('--fmin', default=1.0, type=float)
@click.option('--fmax', default=200.0, type=float)
@click.option('--width', default=2.0, type=float)
@click.option('--length', default=3.0, type=float)
@click.option('--height', default=4.0, type=float)
@click.option('--num_modes', default=10, type=int)
@click.option('--plot/--no-plot', default=True)
def main(
def detect_room_modes(
filename,
data_dir,
fmin,
Expand Down Expand Up @@ -195,3 +185,28 @@ def main(
plt.show()

return calculated_mode_freqs, measured_mode_freqs


@click.command(name="room-modes", help="Plot room modes.")
@click.argument('filename', nargs=-1, type=click.Path(exists=True))
@click.option('--data_dir', type=click.Path(exists=True))
@click.option('--fmin', default=1.0, type=float)
@click.option('--fmax', default=200.0, type=float)
@click.option('--width', default=2.0, type=float)
@click.option('--length', default=3.0, type=float)
@click.option('--height', default=4.0, type=float)
@click.option('--num_modes', default=10, type=int)
@click.option('--plot/--no-plot', default=True)
def main(
filename,
data_dir,
fmin,
fmax,
width,
length,
height,
num_modes,
plot,
):
detect_room_modes(filename, data_dir, fmin, fmax, width,
length, height, num_modes, plot)
62 changes: 48 additions & 14 deletions src/python/test/test_sim3d.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from contextlib import chdir
import json
import pathlib
import subprocess

import pytest
import numpy as np
import scipy.io.wavfile as wavfile

from pffdtd.analysis.room_modes import find_nearest
from pffdtd.analysis.room_modes import main as room_modes
from pffdtd.analysis.room_modes import detect_room_modes
from pffdtd.materials.adm_funcs import write_freq_ind_mat_from_Yn, convert_Sabs_to_Yn
from pffdtd.sim2d.fdtd import point_on_circle
from pffdtd.sim3d.room_builder import RoomBuilder
Expand All @@ -15,7 +18,37 @@
from pffdtd.localization.tdoa import locate_sound_source


def test_sim3d_locate_sound_source(tmp_path):
ENGINE_EXE = pathlib.Path("./build/src/cpp/main_3d/pffdtd_3d").absolute()


def _run_engine(sim_dir, engine):
if engine == "python":
eng = EnginePython3D(sim_dir)
eng.run_all(1)
eng.save_outputs()
else:
assert engine == "native"
assert ENGINE_EXE.exists()

with chdir(sim_dir):
result = subprocess.run(
args=[str(ENGINE_EXE)],
capture_output=True,
text=True,
check=True,
)
assert result.returncode == 0


def _skip_if_native_engine_unavailable(engine):
if engine == "native" and not ENGINE_EXE.exists():
pytest.skip("Native engine not available")


@pytest.mark.parametrize("engine", ["python", "native"])
def test_sim3d_locate_sound_source(tmp_path, engine):
_skip_if_native_engine_unavailable(engine)

fmin = 20
fmax = 1000
ppw = 10.5
Expand Down Expand Up @@ -69,9 +102,7 @@ def test_sim3d_locate_sound_source(tmp_path):
save_folder=sim_dir,
)

eng = EnginePython3D(sim_dir)
eng.run_all(1)
eng.save_outputs()
_run_engine(sim_dir=sim_dir, engine=engine)

process_outputs(
data_dir=sim_dir,
Expand Down Expand Up @@ -112,7 +143,10 @@ def test_sim3d_locate_sound_source(tmp_path):
assert np.linalg.norm(actual-estimated) <= 0.1


def test_sim3d_infinite_baffle(tmp_path):
@pytest.mark.parametrize("engine", ["python", "native"])
def test_sim3d_infinite_baffle(tmp_path, engine):
_skip_if_native_engine_unavailable(engine)

fmin = 20
fmax = 1000
ppw = 10.5
Expand Down Expand Up @@ -184,9 +218,7 @@ def test_sim3d_infinite_baffle(tmp_path):
bmin=[0, 0, 0],
)

eng = EnginePython3D(sim_dir)
eng.run_all(1)
eng.save_outputs()
_run_engine(sim_dir=sim_dir, engine=engine)

process_outputs(
data_dir=sim_dir,
Expand Down Expand Up @@ -214,14 +246,17 @@ def test_sim3d_infinite_baffle(tmp_path):
assert error_3 < -12.0


@pytest.mark.parametrize("engine", ["python", "native"])
@pytest.mark.parametrize(
"size,fmax,ppw,fcc,dx_scale,tolerance",
[
((2.8, 2.076, 1.48), 400, 10.5, False, 2, 3.0),
((3.0, 1.0, 2.0), 800, 7.7, True, 3, 6),
]
)
def test_sim3d_detect_room_modes(tmp_path, size, fmax, ppw, fcc, dx_scale, tolerance):
def test_sim3d_detect_room_modes(tmp_path, engine, size, fmax, ppw, fcc, dx_scale, tolerance):
_skip_if_native_engine_unavailable(engine)

L = size[0]
W = size[1]
H = size[2]
Expand Down Expand Up @@ -258,9 +293,7 @@ def test_sim3d_detect_room_modes(tmp_path, size, fmax, ppw, fcc, dx_scale, toler
save_folder=sim_dir,
)

eng = EnginePython3D(sim_dir)
eng.run_all(1)
eng.save_outputs()
_run_engine(sim_dir=sim_dir, engine=engine)

process_outputs(
data_dir=sim_dir,
Expand All @@ -276,7 +309,8 @@ def test_sim3d_detect_room_modes(tmp_path, size, fmax, ppw, fcc, dx_scale, toler
plot=False,
)

actual, measured = room_modes(
actual, measured = detect_room_modes(
filename=None,
data_dir=sim_dir,
fmin=fmin,
fmax=fmax,
Expand Down

0 comments on commit 3ef4b99

Please sign in to comment.