diff --git a/src/cell.c b/src/cell.c index 6839fc765ee41d14fa32ce8dd54c7518b35b8a35..eb9f0f5b01fd28cdaa43e95fd21b162cea35e713 100644 --- a/src/cell.c +++ b/src/cell.c @@ -3725,12 +3725,12 @@ void cell_recursively_shift_sparts(struct cell *c, c->stars.parts++; } -void cell_add_spart(const struct engine *e, struct cell *const c) { +struct spart *cell_add_spart(const struct engine *e, struct cell *const c) { if (c->nodeID != engine_rank) error("Adding spart on a foreign node"); /* Progeny number at each level */ - int progeny[space_cell_maxdepth] = {0}; + int progeny[space_cell_maxdepth]; #ifdef SWIFT_DEBUG_CHECKS for (int i = 0; i < space_cell_maxdepth; ++i) progeny[i] = -1; #endif @@ -3747,15 +3747,6 @@ void cell_add_spart(const struct engine *e, struct cell *const c) { top = top->parent; } - message("top->cellID=%d", top->cellID); - message("depth=%d progenies=[%d %d %d %d %d %d %d %d]", c->depth, progeny[0], - progeny[1], progeny[2], progeny[3], progeny[4], progeny[5], - progeny[6], progeny[7]); - -#ifdef SWIFT_DEBUG_CHECKS - if (top->depth != 0) error("Cell-linking issue"); -#endif - /* Are there any extra particles left? */ if (top->stars.count == top->stars.count_total) error("We ran out of star particles!"); @@ -3763,11 +3754,11 @@ void cell_add_spart(const struct engine *e, struct cell *const c) { /* 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; - message("top->count=%d c->count=%d n_copy=%d", top->stars.count, - c->stars.count, n_copy); - if (n_copy > 0) { + // MATTHIEU: This can be improved. We don't need to copy everything, just + // need to swap a few particles. + struct spart *temp = NULL; if (posix_memalign((void **)&temp, spart_align, n_copy * sizeof(struct spart)) != 0) @@ -3784,9 +3775,7 @@ void cell_add_spart(const struct engine *e, struct cell *const c) { c->stars.parts[i + 1].gpart->id_or_neg_offset--; } else { #ifdef SWIFT_DEBUG_CHECKS - // MATTHIEU - if (c->stars.parts[i + 1].time_bin != time_bin_not_created) - error("Incorrectly linked spart!"); + error("Incorrectly linked spart!"); #endif } } @@ -3804,15 +3793,15 @@ void cell_add_spart(const struct engine *e, struct cell *const c) { sp->x[0] = c->loc[0] + 0.5 * c->width[0]; sp->x[1] = c->loc[1] + 0.5 * c->width[1]; sp->x[2] = c->loc[2] + 0.5 * c->width[2]; - sp->time_bin = time_bin_not_created; + + /* Set it to the current time-bin */ + sp->time_bin = e->min_active_bin; #ifdef SWIFT_DEBUG_CHECKS sp->ti_drift = e->ti_current; #endif -#ifdef SWIFT_DEBUG_CHECKS - cell_check_spart_pos(top, e->s->sparts); -#endif + return sp; } /** diff --git a/src/cell.h b/src/cell.h index 4177082ee0ac3489596d6f70c15d067ca979bdc9..9fd4158a6ef485d02bc2d5e407f885e3f488a7d5 100644 --- a/src/cell.h +++ b/src/cell.h @@ -664,8 +664,10 @@ void cell_activate_drift_gpart(struct cell *c, struct scheduler *s); void cell_activate_sorts(struct cell *c, int sid, struct scheduler *s); void cell_clear_drift_flags(struct cell *c, void *data); 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); -void cell_add_spart(const struct engine *e, struct cell *c); +struct spart *cell_add_spart(const 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 5f4738bb575d2206dd424e1701edf182e7e51eff..04403c95a489a69baea77201446288f2c7fbf0f7 100644 --- a/src/runner.c +++ b/src/runner.c @@ -525,13 +525,54 @@ void runner_do_star_formation(struct runner *r, struct cell *c, int timer) { if (rho > 1.7e7 && e->step > 2) { message("c->cellID=%d Removing particle id=%lld rho=%e", c->cellID, p->id, rho); - cell_convert_part_to_gpart(e, c, p, xp); - cell_add_spart(e, c); + + /* 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); + + /* Assign the ID back */ + sp->id = gp->id_or_neg_offset; + gp->type = swift_type_stars; + + /* Re-link things */ + sp->gpart = gp; + gp->id_or_neg_offset = -(sp - e->s->sparts); + + /* Synchronize clocks */ + gp->time_bin = sp->time_bin; + + /* Synchronize masses, positions and velocities */ + sp->mass = gp->mass; + sp->x[0] = gp->x[0]; + sp->x[1] = gp->x[1]; + sp->x[2] = gp->x[2]; + sp->v[0] = gp->v_full[0]; + sp->v[1] = gp->v_full[1]; + sp->v[2] = gp->v_full[2]; + +#ifdef SWIFT_DEBUG_CHECKS + sp->ti_kick = gp->ti_kick; +#endif + + /* Set a smoothing length */ + sp->h = max(c->stars.h_max, c->hydro.h_max); + + /* Set everything to a valid state */ + stars_init_spart(sp); } } } } + 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); }