Cabana icon indicating copy to clipboard operation
Cabana copied to clipboard

Usage of std::lround instead of std::floor in Cabana_Grid_SparseArray.hpp causing out-of-bounds cells to be activated

Open JStewart28 opened this issue 4 months ago • 2 comments

In Cabana_Grid_SparseArray.hpp, the following code to register cells in the sparse map uses std::lround to determine which cell ijk is activated. I believe this should be std::floor instead. When testing with 1 tile per dimension, 2 cells per tile, and a domain from (-3, -3, -3) to (3, 3, 3), cells with an x-position above 2 were being assigned a cell with i-index 2. With two cells per dimension, the only valid i-indexes are 0 and 1.

Using std::floor here and in the Sparse Array test fixes this issue. The tests pass when using std::floor.

Kokkos::parallel_for(
            "Cabana::Grid::Experimental::SparseArrayLayout::registerSparseMap",
            Kokkos::RangePolicy<ExecSpace>( 0, particle_num ),
            KOKKOS_LAMBDA( const int pid ) {
                scalar_type pos[3] = { positions( pid, 0 ) - low_corner[0],
                                       positions( pid, 1 ) - low_corner[1],
                                       positions( pid, 2 ) - low_corner[2] };
                int grid_base[3] = {
                    static_cast<int>( std::lround( pos[0] * dx_inv[0] ) -
                                      p2g_radius ),
                    static_cast<int>( std::lround( pos[1] * dx_inv[1] ) -
                                      p2g_radius ),
                    static_cast<int>( std::lround( pos[2] * dx_inv[2] ) -
                                      p2g_radius ) };
                // register grids that will have data transfer with the particle
                const int p2g_size = p2g_radius * 2;
                for ( int i = 0; i <= p2g_size; ++i )
                    for ( int j = 0; j <= p2g_size; ++j )
                        for ( int k = 0; k <= p2g_size; ++k )
                        {
                            int cell_id[3] = { grid_base[0] + i,
                                               grid_base[1] + j,
                                               grid_base[2] + k };
                            map.insertCell( cell_id[0], cell_id[1],
                                            cell_id[2] );
                        }
            } );

JStewart28 avatar Nov 13 '25 21:11 JStewart28