|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## What is k-wave-python? |
| 6 | + |
| 7 | +Python implementation of the [k-Wave](http://www.k-wave.org/) acoustics toolbox for time-domain acoustic and ultrasound simulations. Two backends: a pure Python/NumPy/CuPy solver and C++ binaries (OMP/CUDA). |
| 8 | + |
| 9 | +## Commands |
| 10 | + |
| 11 | +```bash |
| 12 | +# Setup |
| 13 | +uv sync --extra dev --extra test |
| 14 | +uv run pre-commit install |
| 15 | + |
| 16 | +# Run tests |
| 17 | +uv run pytest # all tests |
| 18 | +uv run pytest tests/test_kgrid.py # single file |
| 19 | +uv run pytest tests/test_kgrid.py::test_name # single test |
| 20 | +uv run pytest -m integration # MATLAB-reference tests only |
| 21 | + |
| 22 | +# Lint/format |
| 23 | +uv run ruff check . --fix |
| 24 | +uv run ruff format . |
| 25 | +uv run pre-commit run --all-files |
| 26 | + |
| 27 | +# Run an example |
| 28 | +uv run examples/ivp_homogeneous_medium.py |
| 29 | + |
| 30 | +# Build |
| 31 | +uv build |
| 32 | +``` |
| 33 | + |
| 34 | +Always use `uv run` (not `uv run python`) for pytest, examples, and scripts. |
| 35 | + |
| 36 | +## Code Style |
| 37 | + |
| 38 | +- Line length: 140 (configured in `pyproject.toml` under `[tool.ruff]`) |
| 39 | +- Pre-commit hooks: ruff (lint + format), codespell, nb-clean |
| 40 | +- Ruff ignores F821 (nested function refs) and F722 (jaxtyping annotations) |
| 41 | +- Type annotations use `beartype` + `jaxtyping` |
| 42 | + |
| 43 | +## Architecture |
| 44 | + |
| 45 | +### Entry point |
| 46 | + |
| 47 | +`kwave/kspaceFirstOrder.py` — `kspaceFirstOrder()` is the unified API. It accepts `kgrid`, `medium`, `source`, `sensor` and dispatches to the appropriate backend. |
| 48 | + |
| 49 | +``` |
| 50 | +kspaceFirstOrder(kgrid, medium, source, sensor, backend="python"|"cpp", device="cpu"|"gpu") |
| 51 | +``` |
| 52 | + |
| 53 | +### Two backends |
| 54 | + |
| 55 | +- **Python backend** (`kwave/solvers/kspace_solver.py`): `Simulation` class implementing k-space pseudospectral method in NumPy/CuPy. Supports 1D/2D/3D. Uses CuPy for GPU when `device="gpu"`. |
| 56 | +- **C++ backend** (`kwave/solvers/cpp_simulation.py` + `kwave/executor.py`): Serializes to HDF5, invokes compiled C++ binary, reads results back. `save_only=True` writes HDF5 without running (for cluster jobs). |
| 57 | + |
| 58 | +### Core data classes |
| 59 | + |
| 60 | +- `kWaveGrid` (`kwave/kgrid.py`) — domain discretization, spacing, time array |
| 61 | +- `kWaveMedium` (`kwave/kmedium.py`) — sound speed, density, absorption, nonlinearity |
| 62 | +- `kSource` (`kwave/ksource.py`) — pressure/velocity sources with masks and signals |
| 63 | +- `kSensor` (`kwave/ksensor.py`) — sensor mask and recording configuration |
| 64 | + |
| 65 | +### Legacy path |
| 66 | + |
| 67 | +`kspaceFirstOrder2D()` / `kspaceFirstOrder3D()` in their respective files route through `kWaveSimulation` (`kwave/kWaveSimulation.py`) — a large legacy dataclass used by the C++ backend path. New code should use `kspaceFirstOrder()` directly. |
| 68 | + |
| 69 | +### PML handling |
| 70 | + |
| 71 | +When `pml_inside=False` (default), `kspaceFirstOrder()` expands the grid by `2*pml_size` before simulation and strips PML from full-grid output fields afterward. `pml_size="auto"` selects optimal sizes via `get_optimal_pml_size()`. |
| 72 | + |
| 73 | +### Key utilities |
| 74 | + |
| 75 | +- `kwave/utils/pml.py` — PML sizing (`get_pml()`, `get_optimal_pml_size()`) |
| 76 | +- `kwave/utils/mapgen.py` — geometry generators (`make_disc`, `make_ball`, `make_cart_circle`, etc.) |
| 77 | +- `kwave/utils/signals.py` — signal generation (tone bursts, filtering) |
| 78 | +- `kwave/utils/filters.py` — spatial smoothing, Gaussian filters |
| 79 | +- `kwave/utils/io.py` — HDF5 read/write |
| 80 | +- `kwave/utils/conversion.py` — unit conversion, `cart2grid` |
| 81 | + |
| 82 | +## Testing |
| 83 | + |
| 84 | +- Tests in `tests/`, configured via `[tool.pytest.ini_options]` in `pyproject.toml` |
| 85 | +- Integration tests (`@pytest.mark.integration`) compare against MATLAB reference data |
| 86 | +- Test fixtures in `tests/integration/conftest.py`: `load_matlab_ref` (fixture), `assert_fields_close` (helper function for field comparison) |
| 87 | +- MATLAB reference data for `@pytest.mark.integration` tests lives in `tests/matlab_test_data_collectors/python_testers/collectedValues/` (hardcoded in `conftest.py`) |
| 88 | +- `test_example_parity.py` (MATLAB-parity tests) reads `KWAVE_MATLAB_REF_DIR` (defaults to `~/git/k-wave-cupy/tests` if unset) |
| 89 | + |
| 90 | +## Naming Conventions |
| 91 | + |
| 92 | +- `backend="python"` or `"cpp"` (not "native") |
| 93 | +- `device="cpu"` or `"gpu"` (not `use_gpu` bool) |
| 94 | +- `quiet=True` to suppress output; `debug=True` for detailed output |
| 95 | +- `pml_size="auto"` for automatic PML sizing (not a separate boolean) |
0 commit comments