Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to get particle cell index. #5118

Merged
merged 30 commits into from
Aug 21, 2024

Conversation

archermarx
Copy link
Contributor

@archermarx archermarx commented Aug 7, 2024

Close #4977

This PR adds a function to get a particle's cell index to ParticleUtils.H, and uses it in both ParticleUtils.cpp and TemperatureFunctor.cpp.

@ax3l
Copy link
Member

ax3l commented Aug 7, 2024

Thanks for the PR, @archermarx!
We just mitigated a CI issue, can you rebase against the latest development and force push (or merge development)? Thanks!

@archermarx
Copy link
Contributor Author

yes, done!

@ax3l ax3l added the component: core Core WarpX functionality label Aug 7, 2024
Copy link
Member

@ax3l ax3l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks splendid, thank you so much for this!

@archermarx
Copy link
Contributor Author

Double checking, I had to cast a few parameters to void in order to avoid unused variable warnings due to y and/or z positions not being needed in 1D and 2D simulations. Is this ok, or is there a cleaner way to do this?

Copy link
Member

@roelof-groenewald roelof-groenewald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this cleanup! Just a couple small comments.

Source/Utils/ParticleUtils.H Outdated Show resolved Hide resolved
@archermarx
Copy link
Contributor Author

Ok, I added an overload for the function when called on a SuperParticleType and incorporated it into TemperatureFunctor.cpp

@ax3l
Copy link
Member

ax3l commented Aug 8, 2024

CI is fixed for good now. Once adding the update from @roelof-groenewald , please rebase once more @archermarx .

@archermarx
Copy link
Contributor Author

Ok, made the requested changes and rebased

static_cast<int>((p.pos(0)-plo[0])*dxi[0] - lo.x),
static_cast<int>((p.pos(1)-plo[1])*dxi[1] - lo.y),
static_cast<int>((p.pos(2)-plo[2])*dxi[2] - lo.z))};
return getParticleCell(p, plo, dxi, cbx);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see most (maybe all) of the examples that involve sorting the particles per cell are still failing. Looking closer, I see that the current development function here calculates the cell indices with

ii = (p.pos(0) - plo[0]) * dxi[0] - lo.x

whereas the AMReX function uses

ii = (p.pos(0) - plo[0]) * dxi[0] + cbx.smallend[0]

So the difference is that the development version calculates a cell index starting from 0 for each tile whereas the AMReX function calculates a global cell index for the entire domain. It's not clear to me why this breaks the code though since the binning should only care about which particles have the same cell index, not about what the value of the indices are.

@roelof-groenewald
Copy link
Member

I'll look more into the current reason for the test failures but I also wanted to point out that the new utility function can be used in https://github.com/ECP-WarpX/WarpX/blob/development/Source/Diagnostics/ComputeDiagFunctors/ParticleReductionFunctor.cpp in a similar way as in the temperature diagnostic.

@archermarx
Copy link
Contributor Author

OK, I can add it in there too

@roelof-groenewald
Copy link
Member

Looks like collisionXZ and collisionXYZ tests are still failing, but all other CI tests pass.

Great! Those tests have been problematic for a while now (see #5132). After that PR is merged you can rebase and they should pass.

@archermarx
Copy link
Contributor Author

Oh great, that's good to know!

@roelof-groenewald
Copy link
Member

roelof-groenewald commented Aug 14, 2024

I would be okay to merge this since it already cleans up the code quite a bit, but we should be able to replace

    AMREX_GPU_HOST_DEVICE AMREX_INLINE
    amrex::IntVect getParticleCellIndex (const WarpXParticleContainer::SuperParticleType& p,
                                         amrex::GpuArray<amrex::Real,AMREX_SPACEDIM> const& plo,
                                         amrex::GpuArray<amrex::Real,AMREX_SPACEDIM> const& dxi) {
        return getParticleCellIndex(p.pos(0), p.pos(1), p.pos(2), plo, dxi);
    }

with something like

    AMREX_GPU_HOST_DEVICE AMREX_INLINE
    amrex::IntVect getParticleCellIndex (const WarpXParticleContainer::SuperParticleType& p,
                                         amrex::GpuArray<amrex::Real,AMREX_SPACEDIM> const& plo,
                                         amrex::GpuArray<amrex::Real,AMREX_SPACEDIM> const& dxi) {
        // create zero_low_bound_box
        return amrex::getParticleCell(p, plo, dxi, zero_low_bound_box);
    }

where zero_low_bound_box is an amrex::Box that has the property zero_low_bound_box.smallEnd() == {0, 0, 0}. This would allow us to directly use the AMReX function and not need all the extra logic in WarpX repeated. @ax3l could you help us out with how to create zero_low_bound_box?
Or alternatively, we can maybe add an overload to AMReX for amrex::getParticleCell that does not take the 4th argument and in that case does not shift the cell index based on the global box index.

EDIT: On second thought, I like that second option. I'll go ahead and submit a PR to AMReX.

@archermarx
Copy link
Contributor Author

That sounds good, and would let us eliminate a lot of code. For the version that takes positions instead of a particle type, is there a way we can delegate to getParticleCell as well? That version is only called in the WarpXParIter loop in TemperatureFunctor.cpp. Can we create a WarpxParticleType from the elements of that iterator?

@archermarx
Copy link
Contributor Author

I just pushed a version that uses your overload of getParticleCell. Let me know what you think and if you think we can further simplify

atmyers pushed a commit to AMReX-Codes/amrex that referenced this pull request Aug 16, 2024
## 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
@archermarx
Copy link
Contributor Author

Ok, looks like that got merged into AMReX. Once we update AMReX for the week, this should work, I think

@roelof-groenewald
Copy link
Member

@archermarx would you mind rebasing on development now that the AMReX version has been updated?

@archermarx
Copy link
Contributor Author

archermarx commented Aug 20, 2024 via email

@archermarx
Copy link
Contributor Author

To fix some further redundancy, I have eliminated the new function entirely and just call the new overload of amrex::GetParticleCell. To do that, I had to make a small modification to TemperatureFunctor.cpp to grab a ParticleType instead of using the GetPosition API. Let me know if there's a cleaner way to do this than the way I'm doing it.

@archermarx
Copy link
Contributor Author

CI should pass now. I tried replacing the particle cell finding in ParticleUtils.cpp with a use of amrex::getParticleCell but CI failed for some reason. Not sure why.

Co-authored-by: Roelof Groenewald <[email protected]>
Copy link
Member

@roelof-groenewald roelof-groenewald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR! I think PRs that remove this many lines of code without altering functionality (or obscuring it) are really great!

@roelof-groenewald roelof-groenewald merged commit 5f9a37f into ECP-WarpX:development Aug 21, 2024
47 checks passed
@archermarx archermarx deleted the particle_cell_id branch August 21, 2024 23:38
const amrex::Real lz = (z - plo[2]) * dxi[2];
kk = static_cast<int>(amrex::Math::floor(lz));
#endif
const auto [ii, jj, kk] = amrex::getParticleCell(p, plo, dxi).dim3();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, there is another branch, m_do_average, below that can use the same functionality.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up in #5557

roelof-groenewald pushed a commit that referenced this pull request Jan 14, 2025
One leftover occurance in `ParticleReductionFunctor`.

Follow-up to #5118
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: core Core WarpX functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Function to get particle cell index.
3 participants