Skip to content

Commit

Permalink
Add overload for getParticleCell that returns local cell index (#4081)
Browse files Browse the repository at this point in the history
## Summary
In WarpX we have a few routines that requires the local cell index of
particles (i.e. the cell number in the current box). The AMReX function
`getParticleCell` currently only has logic to return the global cell
index (relative to the domain low end). This PR adds a new overload for
that function which does not shift the cell index based on the global
box index.

## Additional background
See ECP-WarpX/WarpX#5118 for WarpX context.

## Checklist

The proposed changes:
- [ ] fix a bug or incorrect behavior in AMReX
- [x] add new capabilities to AMReX
- [ ] changes answers in the test suite to more than roundoff level
- [ ] are likely to significantly affect the results of downstream AMReX
users
- [ ] include documentation in the code and/or rst files, if appropriate
  • Loading branch information
roelof-groenewald authored Aug 16, 2024
1 parent d9919c9 commit c061a14
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions Src/Particle/AMReX_ParticleUtil.H
Original file line number Diff line number Diff line change
Expand Up @@ -356,17 +356,54 @@ struct GetParticleBin
}
};

/**
* \brief Returns the cell index for a given particle using the
* provided lower bounds and cell sizes.
*
* This version indexes cells starting from 0 at the lower left corner of
* the provided lower bounds, i.e., it returns a local index.
*
* \tparam P a type of AMReX particle.
*
* \param p the particle for which the cell index is calculated
* \param plo the low end of the domain
* \param dxi cell sizes in each dimension
*/
template <typename P>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
IntVect getParticleCell (P const& p,
amrex::GpuArray<amrex::Real,AMREX_SPACEDIM> const& plo,
amrex::GpuArray<amrex::Real,AMREX_SPACEDIM> const& dxi,
const Box& domain) noexcept
amrex::GpuArray<amrex::Real,AMREX_SPACEDIM> const& dxi) noexcept
{
IntVect iv(
AMREX_D_DECL(int(amrex::Math::floor((p.pos(0)-plo[0])*dxi[0])),
int(amrex::Math::floor((p.pos(1)-plo[1])*dxi[1])),
int(amrex::Math::floor((p.pos(2)-plo[2])*dxi[2]))));
return iv;
}

/**
* \brief Returns the cell index for a given particle using the
* provided lower bounds, cell sizes and global domain offset.
*
* This version indexes cells starting from 0 at the lower left corner of
* the simulation geometry, i.e., it returns a global index.
*
* \tparam P a type of AMReX particle.
*
* \param p the particle for which the cell index is calculated
* \param plo the low end of the domain
* \param dxi cell sizes in each dimension
* \param domain AMReX box in which the given particle resides
*/
template <typename P>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
IntVect getParticleCell (P const& p,
amrex::GpuArray<amrex::Real,AMREX_SPACEDIM> const& plo,
amrex::GpuArray<amrex::Real,AMREX_SPACEDIM> const& dxi,
const Box& domain) noexcept
{
IntVect iv = getParticleCell(p, plo, dxi);
iv += domain.smallEnd();
return iv;
}
Expand Down

0 comments on commit c061a14

Please sign in to comment.