stl2bem
stl2bem computes how a rigid body in free space changes the sound at microphones mounted on it. From the body's CAD model, a closed STL mesh, and the microphone positions, it solves the boundary element method (BEM) for plane waves from every direction of a grid and writes the response of each microphone over directions and frequencies, the table that direction finding and beamforming compare recordings with. It runs on a CPU or an NVIDIA GPU and writes HDF5 or SOFA files.
| star, 4 kHz | toy boat, 3 kHz | mug, 2.5 kHz |
| wine bottle, 144 Hz | bell, 2.5 kHz | elbow pipe, 1.4 kHz |
Plane waves passing six objects modeled in Fusion (3,800 to 12,000 triangles each): real part of the total pressure on a slice through each object, over one period. Surface pressure and cavity resonances of the same objects are in docs/validation.md.
On a rigid sphere the computed field differs from the exact solution by a median 8.6 × 10⁻⁴ of the incident amplitude, and on six CAD objects it agrees with the independent BEM library bempp-cl to 0.01-0.4 % (median).
- Microphone tables from CAD. An STL and microphone coordinates in, the total pressure p[microphone, direction, frequency] out, in the phase convention of an STFT.
- CBIE with CHIEF points against the interior resonances of a closed body; Burton-Miller as an option.
- Exact singular integration and microphones on the surface, second-order accurate also on edges and vertices.
- One factorization per frequency and one solve per microphone (when there are fewer microphones than directions) for all directions, on a GPU (CuPy) or a CPU (NumPy and SciPy).
Install
pip install stl2bem # NumPy, SciPy, h5py (CPU)
pip install "stl2bem[prep,sofa]" # optional: merging CAD parts, SOFA export
pip install cupy-cuda12x # optional GPU backend; pick the CuPy package of your CUDA version
The examples and figures need the repository: git clone https://github.com/Kwon-Junhyeong/stl2bem.
Quick start
stl2bem --stl device.stl --units mm --mics mics.csv --out out/ --dry_run # check mesh, microphones, resources
stl2bem --stl device.stl --units mm --mics mics.csv --out out/ # 0-8 kHz, 1-degree direction grid
stl2bem --stl device.stl --units mm --mics mics.csv --out out/ --source_distance 0.5 # sources 0.5 m away
mics.csv has one x, y, z row per microphone, in the STL units; each microphone is moved to the closest point of
the surface. Without --source_distance the sources are plane waves (far field). The dry run prints the distance
2D²/λ beyond which that holds for your body; closer sources need --source_distance. It also estimates the
memory and the run time. stl2bem --help lists all options.
From Python:
import numpy as np
from stl2bem import build_mesh, surface_receivers, solve_table, spherical_doa_grid
mesh = build_mesh("device.stl", units="mm", recenter="none")
mics, weights, _ = surface_receivers(np.loadtxt("mics.csv", delimiter=",") / 1000, mesh)
doa, az, el = spherical_doa_grid(360, 19) # 1 deg azimuth, 10 deg elevation
freqs = np.linspace(0, 4000, 129)
solve_table(mesh.verts, mesh.tris, mesh.centroids, mesh.normals, mesh.areas, freqs, doa, "table.h5",
receivers=mics, receiver_elements=weights)
The pressure on a plane through the body, as in the gallery above:
from stl2bem.viz import plane_grid, field_on_points
pts, shape = plane_grid(center=(0, 0, 0), u=(1, 0, 0), v=(0, 0, 1), half=(0.2, 0.2), n=(241, 241))
field = field_on_points(mesh, 2000.0, doa=(1, 0, 0), points=pts).reshape(shape) # NaN inside the body
The localization error that the body causes for a free-field SRP-PHAT:
from stl2bem.analysis import load_table, free_field, doa_error, summarize
body = load_table("table.h5", elevations=[0])
print(summarize(doa_error(body, free_field(body), band=(300, 3000), method="srp-phat", search="azimuth")))
Problem and users
A microphone array on a robot or another device finds the direction of a sound by comparing what its microphones record with the response each direction would produce (the steering vectors, or array manifold). Microphones on a body do not hear the free field: the body shadows the sound and diffracts it around its surface, so responses computed from the microphone positions alone do not match the recordings. Measuring the responses takes a recording for every direction and starts again whenever the body or a microphone position changes.
stl2bem is for developers of such arrays who have the CAD model of the body: it computes the responses for any microphone layout before the device is built.
Design requirements
| requirement | how stl2bem meets it |
|---|---|
| Input as CAD exports it: an STL in mm, cm or m, often several overlapping parts | --units is required; stl2bem-prep merges the parts into one closed surface |
| Output that localization code uses without conversion | p[R, D, F] relative to the incident wave at the origin, on the FFT bins of --fs and --nfft, in the phase convention of an STFT |
| Microphones where the CAD puts them, also on an edge or a vertex | projected onto the surface and evaluated with a second-order formula |
| Valid across the band, including the interior resonances of the body | CHIEF points inside the body |
| A full direction grid (65,160 directions at 1 degree) at every frequency | one factorization per frequency and, with fewer microphones than directions, one solve per microphone; each direction is a product |
System architecture
The command stl2bem (run.py) reads and checks the mesh (stl_io.py), projects the microphones onto the surface
(panels.py) and places the CHIEF points (chief.py). bem_core.py assembles and solves every frequency with the
integrals of kernels.py, on CuPy or NumPy through backend.py.
| Existing libraries | NumPy and SciPy (CPU linear algebra), CuPy with cuSOLVER (GPU QR), h5py (HDF5), sofar (SOFA), scikit-image (marching cubes in stl2bem-prep) |
| This package | mesh checks, surface receivers, CHIEF point search, kernel integration, assembly and solve, command line, CAD merge, analysis and plotting tools |
Interior resonances: CBIE with CHIEF points
The surface equation of a rigid body (CBIE) has no unique solution at the frequencies where the interior of the body, with a pressure-release surface, would resonate.
Two remedies were compared. CHIEF adds the interior equation at points inside the body and solves the over-determined
system by least squares. Burton-Miller combines the CBIE with its normal derivative and is unique at every
frequency, but with constant elements it damps sharp cavity resonances. At the Helmholtz resonance of the gallery's
wine bottle its half width is 0.166 Hz and its peak 52.9 dB, against 0.063 Hz and 61.4 dB in bempp-cl; the default
gives 0.061 Hz and 61.6 dB (the bottle has no room for CHIEF points, so this is the CBIE alone, see below). CBIE
with CHIEF is the default, and --formulation burton-miller is an experimental option.
CHIEF points are placed automatically: random candidates inside the body (majority of three rays), at least 0.75 mean edges from the nearest element centroid, spread by farthest-point sampling. Points closer to the surface give inaccurate rows that pull the solution away. Thin-walled bodies such as the bottle and the pipe have no room for such points and need none: their irregular frequencies start near c / 2t for wall thickness t, about 40 kHz for 4 mm, and they are solved with the CBIE alone.
Kernel integration and microphones on the surface
The earlier solver this package grew from took the kernel at each element's centroid and gave each microphone the value of the nearest element centroid. Both are first-order accurate and fail where elements are close compared with their size: at sharp edges, across thin walls, and for a microphone just above the surface. stl2bem integrates the static part of the kernel, that of 1/(4πr), exactly over every triangle as its signed solid angle, and the smooth remainder with a 1-, 3- or 7-point rule. A microphone is projected onto the surface and evaluated with the corrected representation 2(p_inc − Mp)(x) − p_j, which removes the first-order term; on an edge or a vertex the correction uses the solid-angle-weighted mean of the elements meeting there.
On a rigid sphere the surface error falls with the square of the element size, and 320 elements are more
accurate than the earlier solver with 5,120. --integration centroid --receivers snap reproduces the earlier tables.
Equations: docs/method.md.
One factorization per frequency, one solve per microphone
A table needs thousands of incident directions at every frequency, and the BEM matrix depends only on the frequency. stl2bem assembles the dense matrix once per frequency (on the GPU in one CUDA kernel) and factorizes it (QR in place on the GPU; normal equations in double precision with Cholesky on the CPU; LU without CHIEF). The microphone values are linear in the surface solution, so instead of solving every direction stl2bem solves the transposed system once per microphone; each direction then costs one product, and the table is the same up to rounding (method). An iterative solver with a fast multipole method would need less memory on large meshes, and with solves per microphone only a few solves per frequency, but stl2bem does not implement one. The dense matrix sets the mesh size: with CHIEF points about 0.7 GB + 8 bytes × N² of GPU memory or 0.5 GB + 40 bytes × N² of RAM for N elements, plus the block of directions solved at once; Burton-Miller needs 8 bytes × N² more on the GPU. Time grows as N³. On the 8 GB GPU 24,300 elements took 6.0 GB. The dry run prints the estimate for the chosen backend before a run. On an RTX 4060 Ti (8 GB) one frequency with 65,160 directions takes 0.18 s for 4,800 elements and 0.92 s for 10,800, and a 20-thread CPU takes 5.5 s for 4,800; version 0.1.1 took 2.6 s, 15 s and 106 s (examples/benchmark).
Preparing CAD
The solver needs one closed, consistently oriented surface. A single solid exported from CAD usually is one; an
assembly usually is not, because its parts overlap. stl2bem-prep merges the parts:
stl2bem-prep chassis.stl board.stl battery.stl --pitch 8 --units mm --out body.stl
It voxelizes the parts (pitch = element size), fills enclosed cavities and extracts a closed surface. Features
smaller than the pitch disappear and thin plates become one pitch thick; --align_z keeps a flat microphone board
flat. In Fusion, set the maximum edge length of the STL export to about λ/6 at the highest frequency; the mesh
report prints the frequency limit from the longest element edge and warns when some elements are larger than λ/6
at the highest frequency.
Results
Rigid sphere against the exact solution.
A rigid sphere with 5,120 elements at ka = 3.7: the median difference from the exact series solution is 8.6 × 10⁻⁴ of the incident amplitude (examples/validation_sphere).
Six CAD objects. The gallery at the top: plane waves passing six objects modeled in Fusion (3,800 to 12,000 triangles each), the real part of the total pressure on a slice through each object over one period (examples/gallery).
Against bempp-cl.
The six meshes solved again with bempp-cl, which uses a different formulation (Burton-Miller, linear elements): the fields differ by 0.01-0.4 % (median), and the resonance frequencies by at most 0.5 % (wine bottle 144.66 Hz against 145.35 Hz, elbow pipe 1,447.0 Hz against 1,448.0 Hz) (examples/crosscheck_bempp).
Convergence, resonances and timings in detail: docs/validation.md.
Output
One HDF5 file per body:
| dataset | shape | meaning |
|---|---|---|
p |
(R, D, F) complex64 | total pressure at receiver r for source d at frequency f, relative to the incident pressure at the origin |
doa_grid |
(D, 3) | unit vectors towards the sources; az x el grids are stored az-major (i_az * n_el + i_el) |
source_positions |
(D, 3) | point sources only: their positions (m) |
freqs |
(F,) | Hz |
positions |
(R, 3) | where p is evaluated (m, mesh frame); centroids is an older name for the same array |
chief_points |
(N_c, 3) | only when CHIEF is used: the interior points |
Attributes hold the mesh report, the solver options and the convention. --sofa (or
python -m stl2bem.sofa table.h5) also writes the table as a SOFA file (AES69, GeneralTF).
Convention. e^{+jωt}, incident wave exp(+jk d⋅r). For a source signal s(t) from direction d, the FFT/STFT bin
(usual e^{-jωt} kernel) at microphone m is S(ω)⋅p[m, d, ω], so tables and FFT-domain signals combine directly
without conjugation. The phase reference is the center of the bounding box (--recenter bbox); --recenter none
keeps the CAD origin.
Repository structure
| path | contents |
|---|---|
stl2bem/ |
the package: run.py (command line), stl_io.py, panels.py, chief.py, kernels.py, cuda_kernels.py (GPU assembly), bem_core.py, backend.py, prep.py (stl2bem-prep), analysis.py, analytic.py, shapes.py, sofa.py, viz.py |
examples/ |
gallery, rigid-sphere validation, bempp-cl cross-check, benchmark; each writes the numbers and figures it reports |
docs/ |
method.md (equations), validation.md (all results), figures and their data |
tests/ |
CPU tests run by continuous integration; test_gpu.py runs where a GPU is available |
Citation
Kwon, J. stl2bem: sound scattering by rigid objects from STL files. Zenodo. doi:10.5281/zenodo.24376763. The metadata are in CITATION.cff.
License
MIT (see LICENSE).
Metadata
Release files for stl2bem 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| stl2bem-0.2.1.tar.gz | 70.2 kB | With provenance | Details for stl2bem-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| stl2bem-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for stl2bem-0.2.1-py3-none-any.whl |
Total release size: 132.6 kB
Release files / stl2bem-0.2.1.tar.gz
| Download URL | stl2bem-0.2.1.tar.gz |
|---|---|
| Size | 70.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
73ff918a11d3432d8f8231b13933a584e79c9ac62e21e25833cadcd8bb63c886
|
|
BLAKE2b-256 checksum How to use checksums |
66a29b537ebaa5bb17ebf68df41a2222137279cca1b8ea86653385e6e1de8e2a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logRelease files / stl2bem-0.2.1-py3-none-any.whl
| Download URL | stl2bem-0.2.1-py3-none-any.whl |
|---|---|
| Size | 62.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a43ed4fe12ee9916922081be6bf71ae50889292ac1943ae34ad668d78e207ea7
|
|
BLAKE2b-256 checksum How to use checksums |
b35b7e936c4fab4ca11231344a0afd2c9a32285dd18febc0c0fccc25f0d4648c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logSecurity for stl2bem 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for stl2bem-0.2.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.