diff --git a/src/space.c b/src/space.c index 93af4222b7f56ae65e1d11d8c6df6026e180ff03..571cf09e3c80d3ae029d01f7c1cac49e145d97f3 100644 --- a/src/space.c +++ b/src/space.c @@ -433,7 +433,7 @@ void space_rebuild(struct space *s, double cell_max, int verbose) { #ifdef WITH_MPI /* Move non-local parts to the end of the list. */ const int local_nodeID = s->e->nodeID; - for (size_t k = 0; k < nr_parts; k++) + for (size_t k = 0; k < nr_parts;) { if (cells[ind[k]].nodeID != local_nodeID) { cells[ind[k]].count -= 1; nr_parts -= 1; @@ -453,9 +453,14 @@ void space_rebuild(struct space *s, double cell_max, int verbose) { ind[k] = ind[nr_parts]; ind[nr_parts] = t; } + else { + /* Increment when not exchanging otherwise we need to retest "k".*/ + k++; + } + } /* Move non-local gparts to the end of the list. */ - for (int k = 0; k < nr_gparts; k++) + for (int k = 0; k < nr_gparts;) { if (cells[gind[k]].nodeID != local_nodeID) { cells[gind[k]].gcount -= 1; nr_gparts -= 1; @@ -472,6 +477,11 @@ void space_rebuild(struct space *s, double cell_max, int verbose) { gind[k] = gind[nr_gparts]; gind[nr_gparts] = t; } + else { + /* Increment when not exchanging otherwise we need to retest "k".*/ + k++; + } + } /* Exchange the strays, note that this potentially re-allocates the parts arrays. */