Skip to content

Commit 3a08fee

Browse files
authoredMar 25, 2025··
ParticleUtils.H: pass geometry as a parameter to findParticlesInEachCell instead on relying on WarpX::GetInstance() (#5771)
This PR removes the call to `WarpX::GetInstance().Geom(lev);` inside `findParticlesInEachCell`. Instead, the geometry is passed as a parameter to the function.
1 parent 2a4eeae commit 3a08fee

17 files changed

+63
-39
lines changed
 

‎Source/Diagnostics/ReducedDiags/DifferentialLuminosity.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ void DifferentialLuminosity::ComputeDiags (int step)
174174
ParticleTileType& ptile_1 = species_1.ParticlesAt(lev, mfi);
175175
ParticleTileType& ptile_2 = species_2.ParticlesAt(lev, mfi);
176176

177-
ParticleBins bins_1 = ParticleUtils::findParticlesInEachCell( lev, mfi, ptile_1 );
178-
ParticleBins bins_2 = ParticleUtils::findParticlesInEachCell( lev, mfi, ptile_2 );
177+
ParticleBins bins_1 = ParticleUtils::findParticlesInEachCell( warpx.Geom(lev), mfi, ptile_1 );
178+
ParticleBins bins_2 = ParticleUtils::findParticlesInEachCell( warpx.Geom(lev), mfi, ptile_2 );
179179

180180
// Species
181181
const auto soa_1 = ptile_1.getParticleTileData();

‎Source/Diagnostics/ReducedDiags/DifferentialLuminosity2D.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ void DifferentialLuminosity2D::ComputeDiags (int step)
194194
ParticleTileType& ptile_1 = species_1.ParticlesAt(lev, mfi);
195195
ParticleTileType& ptile_2 = species_2.ParticlesAt(lev, mfi);
196196

197-
ParticleBins bins_1 = ParticleUtils::findParticlesInEachCell( lev, mfi, ptile_1 );
198-
ParticleBins bins_2 = ParticleUtils::findParticlesInEachCell( lev, mfi, ptile_2 );
197+
ParticleBins bins_1 = ParticleUtils::findParticlesInEachCell( warpx.Geom(lev), mfi, ptile_1 );
198+
ParticleBins bins_2 = ParticleUtils::findParticlesInEachCell( warpx.Geom(lev), mfi, ptile_2 );
199199

200200
// species 1
201201
const auto soa_1 = ptile_1.getParticleTileData();

‎Source/Evolve/WarpXEvolve.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ WarpX::Evolve (int numsteps)
233233
// Resample particles
234234
// +1 is necessary here because value of step seen by user (first step is 1) is different than
235235
// value of step in code (first step is 0)
236-
mypc->doResampling(istep[0]+1, verbose);
236+
mypc->doResampling(Geom(), istep[0]+1, verbose);
237237

238238
if (evolve_scheme == EvolveScheme::Explicit) {
239239
applyMirrors(cur_time);

‎Source/Particles/Collision/BinaryCollision/BinaryCollision.H

+6-6
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,14 @@ public:
305305
global_debye_length_data = global_debye_length_fab.dataPtr();
306306
}
307307

308-
amrex::Geometry const& geom = WarpX::GetInstance().Geom(lev);
309-
auto const dV = AMREX_D_TERM(geom.CellSize(0), *geom.CellSize(1), *geom.CellSize(2));
308+
amrex::Geometry const& geom_lev = WarpX::GetInstance().Geom(lev);
309+
auto const dV = AMREX_D_TERM(geom_lev.CellSize(0), *geom_lev.CellSize(1), *geom_lev.CellSize(2));
310310
#if defined WARPX_DIM_RZ
311311
amrex::Box const& cbx = mfi.tilebox(amrex::IntVect::TheZeroVector()); //Cell-centered box
312312
auto const lo = lbound(cbx);
313313
auto const hi = ubound(cbx);
314314
int const nz = hi.y - lo.y + 1;
315-
auto const dr = geom.CellSize(0);
315+
auto const dr = geom_lev.CellSize(0);
316316
#endif
317317

318318
auto volume_factor = [=] AMREX_GPU_DEVICE(int i_cell) noexcept {
@@ -334,7 +334,7 @@ public:
334334

335335
// Find the particles that are in each cell of this tile
336336
WARPX_PROFILE_VAR("BinaryCollision::doCollisionsWithinTile::findParticlesInEachCell", prof_findParticlesInEachCell);
337-
ParticleBins bins_1 = findParticlesInEachCell( lev, mfi, ptile_1 );
337+
ParticleBins bins_1 = findParticlesInEachCell( geom_lev, mfi, ptile_1 );
338338
WARPX_PROFILE_VAR_STOP(prof_findParticlesInEachCell);
339339

340340
// Loop over cells, and collide the particles in each cell
@@ -551,8 +551,8 @@ public:
551551

552552
// Find the particles that are in each cell of this tile
553553
WARPX_PROFILE_VAR("BinaryCollision::doCollisionsWithinTile::findParticlesInEachCell", prof_findParticlesInEachCell);
554-
ParticleBins bins_1 = findParticlesInEachCell( lev, mfi, ptile_1 );
555-
ParticleBins bins_2 = findParticlesInEachCell( lev, mfi, ptile_2 );
554+
ParticleBins bins_1 = findParticlesInEachCell( geom_lev, mfi, ptile_1 );
555+
ParticleBins bins_2 = findParticlesInEachCell( geom_lev, mfi, ptile_2 );
556556
WARPX_PROFILE_VAR_STOP(prof_findParticlesInEachCell);
557557

558558
// Loop over cells, and collide the particles in each cell

‎Source/Particles/MultiParticleContainer.H

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <AMReX_BLassert.H>
3232
#include <AMReX_Box.H>
3333
#include <AMReX_Config.H>
34+
#include <AMReX_Geometry.H>
3435
#include <AMReX_GpuControl.H>
3536
#include <AMReX_INT.H>
3637
#include <AMReX_MFIter.H>
@@ -184,9 +185,11 @@ public:
184185
/**
185186
* \brief This function loops over all species and performs resampling if appropriate.
186187
*
188+
* @param[in] geom the geometry of all refinement levels
187189
* @param[in] timestep the current timestep.
190+
* @param[in] verbose flag to control if information on the resampling process is printed out.
188191
*/
189-
void doResampling (int timestep, bool verbose);
192+
void doResampling (const amrex::Vector<amrex::Geometry>& geom, int timestep, bool verbose);
190193

191194
#ifdef WARPX_QED
192195
/** If Schwinger process is activated, this function is called at every

‎Source/Particles/MultiParticleContainer.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1035,14 +1035,15 @@ MultiParticleContainer::doCollisions ( Real cur_time, amrex::Real dt )
10351035
collisionhandler->doCollisions(cur_time, dt, this);
10361036
}
10371037

1038-
void MultiParticleContainer::doResampling (const int timestep, const bool verbose)
1038+
void MultiParticleContainer::doResampling (
1039+
const amrex::Vector<amrex::Geometry>& geom, const int timestep, const bool verbose)
10391040
{
10401041
for (auto& pc : allcontainers)
10411042
{
10421043
// do_resampling can only be true for PhysicalParticleContainers
10431044
if (!pc->do_resampling){ continue; }
10441045

1045-
pc->resample(timestep, verbose);
1046+
pc->resample(geom, timestep, verbose);
10461047
}
10471048
}
10481049

‎Source/Particles/PhysicalParticleContainer.H

+4-1
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,12 @@ public:
288288
* \brief This function determines if resampling should be done for the current species, and
289289
* if so, performs the resampling.
290290
*
291+
* @param[in] geom the geometry of all refinement levels
291292
* @param[in] timestep the current timestep.
293+
* @param[in] verbose flag to control if information on the resampling process is printed out.
292294
*/
293-
void resample (int timestep, bool verbose=true) final;
295+
void resample (
296+
const amrex::Vector<amrex::Geometry>& geom, int timestep, bool verbose=true) final;
294297

295298
#ifdef WARPX_QED
296299
//Functions decleared in WarpXParticleContainer.H

‎Source/Particles/PhysicalParticleContainer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3193,7 +3193,7 @@ PlasmaInjector* PhysicalParticleContainer::GetPlasmaInjector (int i)
31933193
}
31943194
}
31953195

3196-
void PhysicalParticleContainer::resample (const int timestep, const bool verbose)
3196+
void PhysicalParticleContainer::resample (const amrex::Vector<amrex::Geometry>& geom, const int timestep, const bool verbose)
31973197
{
31983198
// In heavily load imbalanced simulations, MPI processes with few particles will spend most of
31993199
// the time at the MPI synchronization in TotalNumberOfParticles(). Having two profiler entries
@@ -3216,7 +3216,7 @@ void PhysicalParticleContainer::resample (const int timestep, const bool verbose
32163216
{
32173217
for (WarpXParIter pti(*this, lev); pti.isValid(); ++pti)
32183218
{
3219-
m_resampler(pti, lev, this);
3219+
m_resampler(geom[lev], pti, lev, this);
32203220
}
32213221
}
32223222
deleteInvalidParticles();

‎Source/Particles/Resampling/LevelingThinning.H

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "Particles/WarpXParticleContainer_fwd.H"
1313

14+
#include <AMReX_Geometry.H>
1415
#include <AMReX_REAL.H>
1516

1617
#include <string>
@@ -47,11 +48,14 @@ public:
4748
/**
4849
* \brief A method that performs leveling thinning for the considered species.
4950
*
51+
* @param[in] geom_lev the geometry of the current refinement level
5052
* @param[in] pti WarpX particle iterator of the particles to resample.
5153
* @param[in] lev the index of the refinement level.
5254
* @param[in] pc a pointer to the particle container.
5355
*/
54-
void operator() (WarpXParIter& pti, int lev, WarpXParticleContainer* pc) const final;
56+
void operator() (
57+
const amrex::Geometry& geom_lev, WarpXParIter& pti,
58+
int lev, WarpXParticleContainer* pc) const final;
5559

5660
private:
5761
amrex::Real m_target_ratio = amrex::Real(1.5);

‎Source/Particles/Resampling/LevelingThinning.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ void LevelingThinning::BackwardCompatibility (const std::string& species_name )
6464
);
6565
}
6666

67-
void LevelingThinning::operator() (WarpXParIter& pti, const int lev,
68-
WarpXParticleContainer * const pc) const
67+
void LevelingThinning::operator() (
68+
const amrex::Geometry& geom_lev, WarpXParIter& pti,
69+
const int lev, WarpXParticleContainer * const pc) const
6970
{
7071
using namespace amrex::literals;
7172

@@ -79,7 +80,7 @@ void LevelingThinning::operator() (WarpXParIter& pti, const int lev,
7980
// efficient to directly loop over the particles. Nevertheless, this structure with a loop over
8081
// the cells is more general and can be readily used to implement almost any other resampling
8182
// algorithm.
82-
auto bins = ParticleUtils::findParticlesInEachCell(lev, pti, ptile);
83+
auto bins = ParticleUtils::findParticlesInEachCell(geom_lev, pti, ptile);
8384

8485
const auto n_cells = static_cast<int>(bins.numBins());
8586
auto *const indices = bins.permutationPtr();

‎Source/Particles/Resampling/Resampling.H

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "Particles/WarpXParticleContainer_fwd.H"
1313

1414
#include <AMReX_REAL.H>
15+
#include <AMReX_Geometry.H>
1516

1617
#include <memory>
1718
#include <string>
@@ -24,7 +25,9 @@ struct ResamplingAlgorithm
2425
/**
2526
* \brief Virtual operator() of the abstract ResamplingAlgorithm class
2627
*/
27-
virtual void operator() (WarpXParIter& /*pti*/, int /*lev*/, WarpXParticleContainer* /*pc*/) const = 0;
28+
virtual void operator() (
29+
const amrex::Geometry& /*geom_lev*/, WarpXParIter& /*pti*/,
30+
int /*lev*/, WarpXParticleContainer* /*pc*/) const = 0;
2831

2932
/**
3033
* \brief Virtual destructor of the abstract ResamplingAlgorithm class
@@ -76,11 +79,14 @@ public:
7679
/**
7780
* \brief A method that uses the ResamplingAlgorithm object to perform resampling.
7881
*
82+
* @param[in] geom_lev the geometry of the current refinement level.
7983
* @param[in] pti WarpX particle iterator of the particles to resample.
8084
* @param[in] lev the index of the refinement level.
8185
* @param[in] pc a pointer to the particle container.
8286
*/
83-
void operator() (WarpXParIter& pti, int lev, WarpXParticleContainer* pc) const;
87+
void operator() (
88+
const amrex::Geometry& geom_lev, WarpXParIter& pti,
89+
int lev, WarpXParticleContainer* pc) const;
8490

8591
private:
8692
ResamplingTrigger m_resampling_trigger;

‎Source/Particles/Resampling/Resampling.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ bool Resampling::triggered (const int timestep, const amrex::Real global_numpart
3838
return m_resampling_trigger.triggered(timestep, global_numparts);
3939
}
4040

41-
void Resampling::operator() (WarpXParIter& pti, const int lev, WarpXParticleContainer * const pc) const
41+
void Resampling::operator() (
42+
const amrex::Geometry& geom_lev, WarpXParIter& pti,
43+
const int lev, WarpXParticleContainer * const pc) const
4244
{
43-
(*m_resampling_algorithm)(pti, lev, pc);
45+
(*m_resampling_algorithm)(geom_lev, pti, lev, pc);
4446
}

‎Source/Particles/Resampling/VelocityCoincidenceThinning.H

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "Utils/ParticleUtils.H"
1616

1717
#include <AMReX_Algorithm.H>
18+
#include <AMReX_Geometry.H>
19+
1820

1921
/**
2022
* \brief This class implements a particle merging scheme wherein particles
@@ -45,11 +47,14 @@ public:
4547
/**
4648
* \brief A method that performs merging for the considered species.
4749
*
50+
* @param[in] geom_lev the geometry of the current refinement level
4851
* @param[in] pti WarpX particle iterator of the particles to resample.
4952
* @param[in] lev the index of the refinement level.
5053
* @param[in] pc a pointer to the particle container.
5154
*/
52-
void operator() (WarpXParIter& pti, int lev, WarpXParticleContainer* pc) const final;
55+
void operator() (
56+
const amrex::Geometry& geom_lev, WarpXParIter& pti,
57+
int lev, WarpXParticleContainer* pc) const final;
5358

5459
/**
5560
* \brief This merging routine requires functionality to sort a GPU vector

‎Source/Particles/Resampling/VelocityCoincidenceThinning.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ VelocityCoincidenceThinning::VelocityCoincidenceThinning (const std::string& spe
6161
}
6262
}
6363

64-
void VelocityCoincidenceThinning::operator() (WarpXParIter& pti, const int lev,
65-
WarpXParticleContainer * const pc) const
64+
void VelocityCoincidenceThinning::operator() (
65+
const amrex::Geometry& geom_lev, WarpXParIter& pti,
66+
const int lev, WarpXParticleContainer * const pc) const
6667
{
6768
using namespace amrex::literals;
6869

@@ -85,7 +86,7 @@ void VelocityCoincidenceThinning::operator() (WarpXParIter& pti, const int lev,
8586
auto * const AMREX_RESTRICT idcpu = soa.GetIdCPUData().data();
8687

8788
// Using this function means that we must loop over the cells in the ParallelFor.
88-
auto bins = ParticleUtils::findParticlesInEachCell(lev, pti, ptile);
89+
auto bins = ParticleUtils::findParticlesInEachCell(geom_lev, pti, ptile);
8990

9091
const auto n_cells = static_cast<int>(bins.numBins());
9192
auto *const indices = bins.permutationPtr();

‎Source/Particles/WarpXParticleContainer.H

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <AMReX_Array.H>
3030
#include <AMReX_FArrayBox.H>
31+
#include <AMReX_Geometry.H>
3132
#include <AMReX_GpuAllocators.H>
3233
#include <AMReX_GpuContainers.H>
3334
#include <AMReX_INT.H>
@@ -485,7 +486,7 @@ public:
485486
* override the method for every derived class. Note that in practice this function is never
486487
* called because resample() is only called for PhysicalParticleContainers.
487488
*/
488-
virtual void resample (const int /*timestep*/, bool /*verbose*/) {}
489+
virtual void resample (const amrex::Vector<amrex::Geometry>& /*geom*/, const int /*timestep*/, bool /*verbose*/) {}
489490

490491
/**
491492
* When using runtime components, AMReX requires to touch all tiles

‎Source/Utils/ParticleUtils.H

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "Utils/WarpXConst.H"
1212

1313
#include <AMReX_DenseBins.H>
14+
#include <AMReX_Geometry.H>
1415
#include <AMReX_Particles.H>
1516

1617
#include <AMReX_BaseFwd.H>
@@ -24,12 +25,12 @@ namespace ParticleUtils {
2425
* particles of a given species present in each cell.
2526
* Note that this does *not* rearrange particle arrays.
2627
*
27-
* @param[in] lev the index of the refinement level.
28+
* @param[in] geom_lev the geometry of the current refinement level.
2829
* @param[in] mfi the MultiFAB iterator.
2930
* @param[in] ptile the particle tile.
3031
*/
3132
amrex::DenseBins<typename WarpXParticleContainer::ParticleTileType::ParticleTileDataType>
32-
findParticlesInEachCell (int lev,
33+
findParticlesInEachCell (amrex::Geometry const& geom_lev,
3334
amrex::MFIter const & mfi,
3435
WarpXParticleContainer::ParticleTileType & ptile);
3536

‎Source/Utils/ParticleUtils.cpp

+3-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
*/
77
#include "ParticleUtils.H"
88

9-
#include "WarpX.H"
10-
119
#include <AMReX_Algorithm.H>
1210
#include <AMReX_Array.H>
1311
#include <AMReX_Box.H>
@@ -32,12 +30,11 @@ namespace ParticleUtils
3230
using ParticleTileType = typename WarpXParticleContainer::ParticleTileType;
3331
using ParticleTileDataType = typename ParticleTileType::ParticleTileDataType;
3432
using ParticleBins = DenseBins<ParticleTileDataType>;
35-
using index_type = typename ParticleBins::index_type;
3633

3734
/* Find the particles and count the particles that are in each cell.
3835
Note that this does *not* rearrange particle arrays */
3936
ParticleBins
40-
findParticlesInEachCell (int lev,
37+
findParticlesInEachCell (Geometry const& geom_lev,
4138
MFIter const & mfi,
4239
ParticleTileType & ptile) {
4340

@@ -46,11 +43,10 @@ namespace ParticleUtils
4643
auto ptd = ptile.getParticleTileData();
4744

4845
// Extract box properties
49-
Geometry const& geom = WarpX::GetInstance().Geom(lev);
5046
Box const& cbx = mfi.tilebox(IntVect::TheZeroVector()); //Cell-centered box
5147
const auto lo = lbound(cbx);
52-
const auto dxi = geom.InvCellSizeArray();
53-
const auto plo = geom.ProbLoArray();
48+
const auto dxi = geom_lev.InvCellSizeArray();
49+
const auto plo = geom_lev.ProbLoArray();
5450

5551
// Find particles that are in each cell;
5652
// results are stored in the object `bins`.

0 commit comments

Comments
 (0)
Please sign in to comment.