Skip to content

Commit 3d29fd7

Browse files
authored
Preserve neighbor particles when sorting particles. (AMReX-Codes#2923)
1 parent 8294c3a commit 3d29fd7

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

Src/Particle/AMReX_ParticleContainerI.H

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,10 +1117,11 @@ ParticleContainer<NStructReal, NStructInt, NArrayReal, NArrayInt, Allocator>::So
11171117

11181118
for(MFIter mfi = MakeMFIter(lev); mfi.isValid(); ++mfi)
11191119
{
1120-
auto& ptile = ParticlesAt(lev, mfi);
1121-
auto& aos = ptile.GetArrayOfStructs();
1122-
const size_t np = aos.numParticles();
1123-
auto pstruct_ptr = aos().dataPtr();
1120+
auto& ptile = ParticlesAt(lev, mfi);
1121+
auto& aos = ptile.GetArrayOfStructs();
1122+
auto pstruct_ptr = aos().dataPtr();
1123+
const size_t np = aos.numParticles();
1124+
const size_t np_total = np + aos.numNeighborParticles();
11241125

11251126
const Box& box = mfi.validbox();
11261127

@@ -1131,40 +1132,40 @@ ParticleContainer<NStructReal, NStructInt, NArrayReal, NArrayInt, Allocator>::So
11311132

11321133
if (memEfficientSort) {
11331134
{
1134-
ParticleVector tmp_particles(np);
1135+
ParticleVector tmp_particles(np_total);
11351136
auto src = ptile.getParticleTileData();
11361137
ParticleType* dst = tmp_particles.data();
11371138

1138-
AMREX_HOST_DEVICE_FOR_1D( np, i,
1139+
AMREX_HOST_DEVICE_FOR_1D( np_total, i,
11391140
{
1140-
dst[i] = src.m_aos[inds[i]];
1141+
dst[i] = i < np ? src.m_aos[inds[i]] : src.m_aos[i];
11411142
});
11421143

11431144
Gpu::streamSynchronize();
11441145
ptile.GetArrayOfStructs()().swap(tmp_particles);
11451146
}
11461147

1147-
RealVector tmp_real(np);
1148+
RealVector tmp_real(np_total);
11481149
for (int comp = 0; comp < NArrayReal + m_num_runtime_real; ++comp) {
11491150
auto src = ptile.GetStructOfArrays().GetRealData(comp).data();
11501151
ParticleReal* dst = tmp_real.data();
1151-
AMREX_HOST_DEVICE_FOR_1D( np, i,
1152+
AMREX_HOST_DEVICE_FOR_1D( np_total, i,
11521153
{
1153-
dst[i] = src[inds[i]];
1154+
dst[i] = i < np ? src[inds[i]] : src[i];
11541155
});
11551156

11561157
Gpu::streamSynchronize();
11571158

11581159
ptile.GetStructOfArrays().GetRealData(comp).swap(tmp_real);
11591160
}
11601161

1161-
IntVector tmp_int(np);
1162+
IntVector tmp_int(np_total);
11621163
for (int comp = 0; comp < NArrayInt + m_num_runtime_int; ++comp) {
11631164
auto src = ptile.GetStructOfArrays().GetIntData(comp).data();
11641165
int* dst = tmp_int.data();
1165-
AMREX_HOST_DEVICE_FOR_1D( np, i,
1166+
AMREX_HOST_DEVICE_FOR_1D( np_total , i,
11661167
{
1167-
dst[i] = src[inds[i]];
1168+
dst[i] = i < np ? src[inds[i]] : src[i];
11681169
});
11691170

11701171
Gpu::streamSynchronize();
@@ -1174,8 +1175,11 @@ ParticleContainer<NStructReal, NStructInt, NArrayReal, NArrayInt, Allocator>::So
11741175
} else {
11751176
ParticleTileType ptile_tmp;
11761177
ptile_tmp.define(m_num_runtime_real, m_num_runtime_int);
1177-
ptile_tmp.resize(np);
1178+
ptile_tmp.resize(np_total);
1179+
// copy re-ordered particles
11781180
gatherParticles(ptile_tmp, ptile, np, m_bins.permutationPtr());
1181+
// copy neighbor particles
1182+
amrex::copyParticles(ptile_tmp, ptile, np, np, np_total-np);
11791183
ptile.swap(ptile_tmp);
11801184
}
11811185
}

0 commit comments

Comments
 (0)