Skip to content

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

@JStewart28

Description

@JStewart28

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] );
                        }
            } );

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions