Skip to content
Snippets Groups Projects
Commit 1802f8f5 authored by Pedro Gonnet's avatar Pedro Gonnet
Browse files

first batch of functions in space.c.

parent 1b316fc5
No related branches found
No related tags found
2 merge requests!136Master,!123Local variable cleanup
...@@ -97,12 +97,10 @@ const int sortlistID[27] = { ...@@ -97,12 +97,10 @@ const int sortlistID[27] = {
int space_getsid(struct space *s, struct cell **ci, struct cell **cj, int space_getsid(struct space *s, struct cell **ci, struct cell **cj,
double *shift) { double *shift) {
int k, sid = 0, periodic = s->periodic;
struct cell *temp;
double dx[3];
/* Get the relative distance between the pairs, wrapping. */ /* Get the relative distance between the pairs, wrapping. */
for (k = 0; k < 3; k++) { const int periodic = s->periodic;
double dx[3];
for (int k = 0; k < 3; k++) {
dx[k] = (*cj)->loc[k] - (*ci)->loc[k]; dx[k] = (*cj)->loc[k] - (*ci)->loc[k];
if (periodic && dx[k] < -s->dim[k] / 2) if (periodic && dx[k] < -s->dim[k] / 2)
shift[k] = s->dim[k]; shift[k] = s->dim[k];
...@@ -114,12 +112,13 @@ int space_getsid(struct space *s, struct cell **ci, struct cell **cj, ...@@ -114,12 +112,13 @@ int space_getsid(struct space *s, struct cell **ci, struct cell **cj,
} }
/* Get the sorting index. */ /* Get the sorting index. */
int sid = 0;
for (k = 0; k < 3; k++) for (k = 0; k < 3; k++)
sid = 3 * sid + ((dx[k] < 0.0) ? 0 : ((dx[k] > 0.0) ? 2 : 1)); sid = 3 * sid + ((dx[k] < 0.0) ? 0 : ((dx[k] > 0.0) ? 2 : 1));
/* Switch the cells around? */ /* Switch the cells around? */
if (runner_flip[sid]) { if (runner_flip[sid]) {
temp = *ci; const cell *temp = *ci;
*ci = *cj; *ci = *cj;
*cj = temp; *cj = temp;
for (k = 0; k < 3; k++) shift[k] = -shift[k]; for (k = 0; k < 3; k++) shift[k] = -shift[k];
...@@ -137,10 +136,8 @@ int space_getsid(struct space *s, struct cell **ci, struct cell **cj, ...@@ -137,10 +136,8 @@ int space_getsid(struct space *s, struct cell **ci, struct cell **cj,
void space_rebuild_recycle(struct space *s, struct cell *c) { void space_rebuild_recycle(struct space *s, struct cell *c) {
int k;
if (c->split) if (c->split)
for (k = 0; k < 8; k++) for (int k = 0; k < 8; k++)
if (c->progeny[k] != NULL) { if (c->progeny[k] != NULL) {
space_rebuild_recycle(s, c->progeny[k]); space_rebuild_recycle(s, c->progeny[k]);
space_recycle(s, c->progeny[k]); space_recycle(s, c->progeny[k]);
...@@ -158,19 +155,19 @@ void space_rebuild_recycle(struct space *s, struct cell *c) { ...@@ -158,19 +155,19 @@ void space_rebuild_recycle(struct space *s, struct cell *c) {
void space_regrid(struct space *s, double cell_max, int verbose) { void space_regrid(struct space *s, double cell_max, int verbose) {
float h_max = s->cell_min / kernel_gamma / space_stretch, dmin; float h_max = s->cell_min / kernel_gamma / space_stretch;
int i, j, k, cdim[3], nr_parts = s->nr_parts; const size_t nr_parts = s->nr_parts;
struct cell *restrict c; struct cell *restrict c;
ticks tic = getticks(); ticks tic = getticks();
/* Run through the parts and get the current h_max. */ /* Run through the parts and get the current h_max. */
// tic = getticks(); // tic = getticks();
if (s->cells != NULL) { if (s->cells != NULL) {
for (k = 0; k < s->nr_cells; k++) { for (int k = 0; k < s->nr_cells; k++) {
if (s->cells[k].h_max > h_max) h_max = s->cells[k].h_max; if (s->cells[k].h_max > h_max) h_max = s->cells[k].h_max;
} }
} else { } else {
for (k = 0; k < nr_parts; k++) { for (int k = 0; k < nr_parts; k++) {
if (s->parts[k].h > h_max) h_max = s->parts[k].h; if (s->parts[k].h > h_max) h_max = s->parts[k].h;
} }
s->h_max = h_max; s->h_max = h_max;
...@@ -190,7 +187,8 @@ void space_regrid(struct space *s, double cell_max, int verbose) { ...@@ -190,7 +187,8 @@ void space_regrid(struct space *s, double cell_max, int verbose) {
if (verbose) message("h_max is %.3e (cell_max=%.3e).", h_max, cell_max); if (verbose) message("h_max is %.3e (cell_max=%.3e).", h_max, cell_max);
/* Get the new putative cell dimensions. */ /* Get the new putative cell dimensions. */
for (k = 0; k < 3; k++) int cdim[3]
for (int k = 0; k < 3; k++)
cdim[k] = cdim[k] =
floor(s->dim[k] / fmax(h_max * kernel_gamma * space_stretch, cell_max)); floor(s->dim[k] / fmax(h_max * kernel_gamma * space_stretch, cell_max));
...@@ -213,7 +211,7 @@ void space_regrid(struct space *s, double cell_max, int verbose) { ...@@ -213,7 +211,7 @@ void space_regrid(struct space *s, double cell_max, int verbose) {
/* Free the old cells, if they were allocated. */ /* Free the old cells, if they were allocated. */
if (s->cells != NULL) { if (s->cells != NULL) {
for (k = 0; k < s->nr_cells; k++) { for (int k = 0; k < s->nr_cells; k++) {
space_rebuild_recycle(s, &s->cells[k]); space_rebuild_recycle(s, &s->cells[k]);
if (s->cells[k].sort != NULL) free(s->cells[k].sort); if (s->cells[k].sort != NULL) free(s->cells[k].sort);
} }
...@@ -222,12 +220,12 @@ void space_regrid(struct space *s, double cell_max, int verbose) { ...@@ -222,12 +220,12 @@ void space_regrid(struct space *s, double cell_max, int verbose) {
} }
/* Set the new cell dimensions only if smaller. */ /* Set the new cell dimensions only if smaller. */
for (k = 0; k < 3; k++) { for (int k = 0; k < 3; k++) {
s->cdim[k] = cdim[k]; s->cdim[k] = cdim[k];
s->h[k] = s->dim[k] / cdim[k]; s->h[k] = s->dim[k] / cdim[k];
s->ih[k] = 1.0 / s->h[k]; s->ih[k] = 1.0 / s->h[k];
} }
dmin = fminf(s->h[0], fminf(s->h[1], s->h[2])); const float dmin = fminf(s->h[0], fminf(s->h[1], s->h[2]));
/* Allocate the highest level of cells. */ /* Allocate the highest level of cells. */
s->tot_cells = s->nr_cells = cdim[0] * cdim[1] * cdim[2]; s->tot_cells = s->nr_cells = cdim[0] * cdim[1] * cdim[2];
...@@ -239,9 +237,9 @@ void space_regrid(struct space *s, double cell_max, int verbose) { ...@@ -239,9 +237,9 @@ void space_regrid(struct space *s, double cell_max, int verbose) {
if (lock_init(&s->cells[k].lock) != 0) error("Failed to init spinlock."); if (lock_init(&s->cells[k].lock) != 0) error("Failed to init spinlock.");
/* Set the cell location and sizes. */ /* Set the cell location and sizes. */
for (i = 0; i < cdim[0]; i++) for (int i = 0; i < cdim[0]; i++)
for (j = 0; j < cdim[1]; j++) for (int j = 0; j < cdim[1]; j++)
for (k = 0; k < cdim[2]; k++) { for (int k = 0; k < cdim[2]; k++) {
c = &s->cells[cell_getid(cdim, i, j, k)]; c = &s->cells[cell_getid(cdim, i, j, k)];
c->loc[0] = i * s->h[0]; c->loc[0] = i * s->h[0];
c->loc[1] = j * s->h[1]; c->loc[1] = j * s->h[1];
...@@ -271,7 +269,7 @@ void space_regrid(struct space *s, double cell_max, int verbose) { ...@@ -271,7 +269,7 @@ void space_regrid(struct space *s, double cell_max, int verbose) {
else { else {
/* Free the old cells, if they were allocated. */ /* Free the old cells, if they were allocated. */
for (k = 0; k < s->nr_cells; k++) { for (int k = 0; k < s->nr_cells; k++) {
space_rebuild_recycle(s, &s->cells[k]); space_rebuild_recycle(s, &s->cells[k]);
s->cells[k].sorts = NULL; s->cells[k].sorts = NULL;
s->cells[k].nr_tasks = 0; s->cells[k].nr_tasks = 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment