From 21f48d7e7c3a7b6447c053ab672657f76307ec00 Mon Sep 17 00:00:00 2001 From: Matthieu Schaller <matthieu.schaller@durham.ac.uk> Date: Sat, 17 Nov 2018 14:46:08 +0000 Subject: [PATCH] Correctly update the counters when adding a spart to the global array. Handle the case where we run out of free slots. --- src/cell.c | 13 +++++++++---- src/cell.h | 2 +- src/runner.c | 18 +++++++----------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/cell.c b/src/cell.c index 4e55906eb3..33fad78ccc 100644 --- a/src/cell.c +++ b/src/cell.c @@ -3725,7 +3725,7 @@ void cell_recursively_shift_sparts(struct cell *c, c->stars.parts++; } -struct spart *cell_add_spart(const struct engine *e, struct cell *const c) { +struct spart *cell_add_spart(struct engine *e, struct cell *const c) { if (c->nodeID != engine_rank) error("Adding spart on a foreign node"); @@ -3748,8 +3748,11 @@ struct spart *cell_add_spart(const struct engine *e, struct cell *const c) { } /* Are there any extra particles left? */ - if (top->stars.count == top->stars.count_total) - error("We ran out of star particles!"); + if (top->stars.count == top->stars.count_total) { + message("We ran out of star particles!"); + atomic_inc(&e->forcerebuild); + return NULL; + } /* Number of particles to shift in order to get a free space. */ const int n_copy = &top->stars.parts[top->stars.count] - c->stars.parts; @@ -3798,10 +3801,12 @@ struct spart *cell_add_spart(const struct engine *e, struct cell *const c) { sp->time_bin = e->min_active_bin; #ifdef SWIFT_DEBUG_CHECKS + /* Specify it was drifted to this point */ sp->ti_drift = e->ti_current; #endif - e->s->nr_extra_sparts--; + /* Register that we used one of the free slots. */ + atomic_dec(&e->s->nr_extra_sparts); return sp; } diff --git a/src/cell.h b/src/cell.h index 9fd4158a6e..11699a2ce5 100644 --- a/src/cell.h +++ b/src/cell.h @@ -667,7 +667,7 @@ void cell_set_super_mapper(void *map_data, int num_elements, void *extra_data); void cell_check_spart_pos(const struct cell *c, const struct spart *global_sparts); int cell_has_tasks(struct cell *c); -struct spart *cell_add_spart(const struct engine *e, struct cell *c); +struct spart *cell_add_spart(struct engine *e, struct cell *c); void cell_remove_part(const struct engine *e, struct cell *c, struct part *p, struct xpart *xp); void cell_remove_gpart(const struct engine *e, struct cell *c, diff --git a/src/runner.c b/src/runner.c index 04403c95a4..6edb69d06e 100644 --- a/src/runner.c +++ b/src/runner.c @@ -492,7 +492,7 @@ void runner_do_cooling(struct runner *r, struct cell *c, int timer) { */ void runner_do_star_formation(struct runner *r, struct cell *c, int timer) { - const struct engine *e = r->e; + struct engine *e = r->e; const struct cosmology *cosmo = e->cosmology; const int count = c->hydro.count; struct part *restrict parts = c->hydro.parts; @@ -526,12 +526,15 @@ void runner_do_star_formation(struct runner *r, struct cell *c, int timer) { message("c->cellID=%d Removing particle id=%lld rho=%e", c->cellID, p->id, rho); - /* Destroy the gas particle and get it's gpart friend */ - struct gpart *gp = cell_convert_part_to_gpart(e, c, p, xp); - /* Create a fresh (empty) spart */ struct spart *sp = cell_add_spart(e, c); + /* Did we run out of free spart slots? */ + if (sp == NULL) continue; + + /* Destroy the gas particle and get it's gpart friend */ + struct gpart *gp = cell_convert_part_to_gpart(e, c, p, xp); + /* Assign the ID back */ sp->id = gp->id_or_neg_offset; gp->type = swift_type_stars; @@ -566,13 +569,6 @@ void runner_do_star_formation(struct runner *r, struct cell *c, int timer) { } } - if (c->super == c) { -#ifdef SWIFT_DEBUG_CHECKS - /* Check that everything went OK */ - cell_check_spart_pos(c, e->s->sparts); -#endif - } - if (timer) TIMER_TOC(timer_do_star_formation); } -- GitLab