Fix multi-environment nanogrid cell lookup
Bug Description
Fix multi-environment nanogrid lookup for particles near voxel boundaries. This was encountered while running implicit MPM simulations with Newton in IsaacLab: a particle could receive a cell index of -1 even though its cell had been allocated, causing it to be omitted from grid integration.
Lookup currently adds environment offsets to floating-point grid coordinates before flooring. In float32, that addition can round a coordinate across a cell boundary. Fix suggestion is apply integer environment offsets after flooring local coordinates so lookup agrees with voxel construction.
The nearby-cell search also truncates its bounds toward zero, which can skip valid cells at negative coordinates and return -1 even when a cell is within the search distance. Similarly, fix is floor the local search bounds before converting to integers and adding environment offsets.
Run this reproducer on a CUDA device. Without the fix, the second particle receives cell index -1; with the fix, both particles map to cells in their own environments.
import numpy as np
import warp as wp
import warp.fem as fem
wp.init()
positions = np.zeros((2, 3), dtype=np.float32)
positions[1, 0] = np.nextafter(np.float32(-2.5), np.float32(-np.inf))
points = wp.array(positions, dtype=wp.vec3, device="cuda:0")
environments = wp.array([0, 1], dtype=wp.int32, device="cuda:0")
grid = fem.Nanogrid.from_environment_voxels(
points,
environments,
2,
env_offsets=np.array([[0, 0, 0], [-1024, 0, 0]], dtype=np.int32),
voxel_size=1.0,
device="cuda:0",
)
quadrature = fem.PicQuadrature(fem.Cells(grid), positions=points, env_indices=environments)
cells = quadrature.cell_indices.numpy()
assert np.all(cells >= 0), f"Particles missing from grid: {cells}"
np.testing.assert_array_equal(grid.cell_env.numpy()[cells], [0, 1])System Information
Software Warp (package): 1.19.0.dev0 Warp (core): 1.19.0.dev0 warp-clang: 1.19.0.dev0 (LLVM 22.1.8) NumPy: 2.5.1 PyTorch: 2.12.0+cu130 Python: 3.12.14 (main, Sep 1 2026, 14:16:52) [Clang 22.1.3 ] Platform: Linux-7.0.0-31-generic-x86_64-with-glibc2.39
CUDA Enabled: True Toolkit: 12.9 Driver: 13.2 NVRTC: 12.9 Forward compat: False
Libraries MathDx: 0.3.1 cuBQL: enabled NanoVDB: 32.8.0
Build Compiler: GCC 13.3.0 Debug: False Verify FP: False Fast math: False Sanitizer: none
CPU Model: arrowlake-s ISA features: 62 64bit, adx, aes, avx, avx2, avxifma, avxneconvert, avxvnni, ...
Devices cpu Name: x86_64 cuda:0 Name: NVIDIA GeForce RTX 5090 Memory: 31.3 GiB Arch: sm_120 SMs: 170 PCI: 00000000:02:00 Mempool: enabled GPU->CPU mem: True CPU->GPU mem: False CPU/GPU atomics: False
Source: NVIDIA/warp