From 9341034a446645c3123ee3ed80eaa1e56f73106a Mon Sep 17 00:00:00 2001 From: Bert Vandenbroucke <bert.vandenbroucke@ugent.be> Date: Thu, 8 Sep 2016 12:24:52 +0100 Subject: [PATCH] Added GLOBAL variable storing the box used as an initial guess for the 3D Voronoi algorithm. --- examples/main.c | 25 ++++++++++++++++++ src/hydro/Shadowswift/voronoi3d_algorithm.h | 29 ++++++++++++++++----- tests/test125cells.c | 10 +++++++ tests/test27cells.c | 10 +++++++ tests/testMatrixInversion.c | 5 +++- tests/testPair.c | 10 +++++++ tests/testRiemannExact.c | 5 +++- tests/testRiemannHLLC.c | 5 +++- tests/testRiemannTRRS.c | 5 +++- tests/testSymmetry.c | 11 ++++++++ tests/testTimeIntegration.c | 4 +++ tests/testVoronoi3D.c | 7 +++++ 12 files changed, 116 insertions(+), 10 deletions(-) diff --git a/examples/main.c b/examples/main.c index 3ffc8e88eb..e3fedabb30 100644 --- a/examples/main.c +++ b/examples/main.c @@ -95,6 +95,10 @@ void print_help_message() { "parameter file.\n"); } +#if defined(SHADOWSWIFT) && defined(HYDRO_DIMENSION_3D) +VORONOI3D_DECLARE_GLOBAL_VARIABLES() +#endif + /** * @brief Main routine that loads a few particles and generates some output. * @@ -371,6 +375,27 @@ int main(int argc, char *argv[]) { fflush(stdout); } +#if defined(SHADOWSWIFT) && defined(HYDRO_DIMENSION_3D) + /* set the *global* box dimensions */ + float box_anchor[3], box_side[3]; + if (periodic) { + box_anchor[0] = -0.5f * dim[0]; + box_anchor[1] = -0.5f * dim[1]; + box_anchor[2] = -0.5f * dim[2]; + box_side[0] = 2.0f * dim[0]; + box_side[1] = 2.0f * dim[1]; + box_side[2] = 2.0f * dim[2]; + } else { + box_anchor[0] = 0.0f; + box_anchor[1] = 0.0f; + box_anchor[2] = 0.0f; + box_side[0] = dim[0]; + box_side[1] = dim[1]; + box_side[2] = dim[2]; + } + voronoi_set_box(box_anchor, box_side); +#endif + /* Discard gparts if we don't have gravity * (Better implementation of i/o will come)*/ if (!with_external_gravity && !with_self_gravity) { diff --git a/src/hydro/Shadowswift/voronoi3d_algorithm.h b/src/hydro/Shadowswift/voronoi3d_algorithm.h index 2f1208245a..febce5df4f 100644 --- a/src/hydro/Shadowswift/voronoi3d_algorithm.h +++ b/src/hydro/Shadowswift/voronoi3d_algorithm.h @@ -43,17 +43,34 @@ #define VORONOI3D_BOX_LEFT 18446744073709551604llu #define VORONOI3D_BOX_RIGHT 18446744073709551605llu +extern float global_voronoi_box_anchor[3]; +extern float global_voronoi_box_side[3]; + +#define VORONOI3D_DECLARE_GLOBAL_VARIABLES() \ + float global_voronoi_box_anchor[3]; \ + float global_voronoi_box_side[3]; + /* Bottom front left corner and side lengths of the large box that contains all particles and is used as initial cell at the start of the construction */ /* We should make sure that this box is either so large a particle can never fall outside (by using FLT_MAX if that works), or is initialized to be larger than the (periodic) simulation box */ -#define VORONOI3D_BOX_ANCHOR_X -2.0f -#define VORONOI3D_BOX_ANCHOR_Y -2.0f -#define VORONOI3D_BOX_ANCHOR_Z -2.0f -#define VORONOI3D_BOX_SIDE_X 6.0f -#define VORONOI3D_BOX_SIDE_Y 6.0f -#define VORONOI3D_BOX_SIDE_Z 6.0f +#define VORONOI3D_BOX_ANCHOR_X global_voronoi_box_anchor[0] +#define VORONOI3D_BOX_ANCHOR_Y global_voronoi_box_anchor[1] +#define VORONOI3D_BOX_ANCHOR_Z global_voronoi_box_anchor[2] +#define VORONOI3D_BOX_SIDE_X global_voronoi_box_side[0] +#define VORONOI3D_BOX_SIDE_Y global_voronoi_box_side[1] +#define VORONOI3D_BOX_SIDE_Z global_voronoi_box_side[2] + +__attribute__((always_inline)) INLINE static void voronoi_set_box(float *anchor, + float *side) { + global_voronoi_box_anchor[0] = anchor[0]; + global_voronoi_box_anchor[1] = anchor[1]; + global_voronoi_box_anchor[2] = anchor[2]; + global_voronoi_box_side[0] = side[0]; + global_voronoi_box_side[1] = side[1]; + global_voronoi_box_side[2] = side[2]; +} __attribute__((always_inline)) INLINE static float voronoi_get_box_volume() { return VORONOI3D_BOX_SIDE_X * VORONOI3D_BOX_SIDE_Y * VORONOI3D_BOX_SIDE_Z; diff --git a/tests/test125cells.c b/tests/test125cells.c index d9c04fdede..e63c5ff4e0 100644 --- a/tests/test125cells.c +++ b/tests/test125cells.c @@ -420,6 +420,10 @@ void runner_doself1_density(struct runner *r, struct cell *ci); void runner_dopair2_force(struct runner *r, struct cell *ci, struct cell *cj); void runner_doself2_force(struct runner *r, struct cell *ci); +#if defined(SHADOWSWIFT) && defined(HYDRO_DIMENSION_3D) +VORONOI3D_DECLARE_GLOBAL_VARIABLES() +#endif + /* And go... */ int main(int argc, char *argv[]) { @@ -440,6 +444,12 @@ int main(int argc, char *argv[]) { /* Get some randomness going */ srand(0); +#if defined(SHADOWSWIFT) && defined(HYDRO_DIMENSION_3D) + float box_anchor[3] = {-2.0f, -2.0f, -2.0f}; + float box_side[3] = {8.0f, 8.0f, 8.0f}; + voronoi_set_box(box_anchor, box_side); +#endif + char c; while ((c = getopt(argc, argv, "m:s:h:n:r:t:d:f:v:p:")) != -1) { switch (c) { diff --git a/tests/test27cells.c b/tests/test27cells.c index e327cc4694..54a84ebb3d 100644 --- a/tests/test27cells.c +++ b/tests/test27cells.c @@ -262,6 +262,10 @@ void dump_particle_fields(char *fileName, struct cell *main_cell, void runner_dopair1_density(struct runner *r, struct cell *ci, struct cell *cj); void runner_doself1_density(struct runner *r, struct cell *ci); +#if defined(SHADOWSWIFT) && defined(HYDRO_DIMENSION_3D) +VORONOI3D_DECLARE_GLOBAL_VARIABLES() +#endif + /* And go... */ int main(int argc, char *argv[]) { size_t runs = 0, particles = 0; @@ -281,6 +285,12 @@ int main(int argc, char *argv[]) { /* Get some randomness going */ srand(0); +#if defined(SHADOWSWIFT) && defined(HYDRO_DIMENSION_3D) + float box_anchor[3] = {-2.0f, -2.0f, -2.0f}; + float box_side[3] = {6.0f, 6.0f, 6.0f}; + voronoi_set_box(box_anchor, box_side); +#endif + char c; while ((c = getopt(argc, argv, "m:s:h:n:r:t:d:f:v:")) != -1) { switch (c) { diff --git a/tests/testMatrixInversion.c b/tests/testMatrixInversion.c index 9a45cd52d6..d28bb49b03 100644 --- a/tests/testMatrixInversion.c +++ b/tests/testMatrixInversion.c @@ -22,7 +22,10 @@ #include "const.h" #include "dimension.h" #include "error.h" -#include "tools.h" + +double random_uniform(double a, double b) { + return (rand() / (double)RAND_MAX) * (b - a) + a; +} void setup_matrix(float A[3][3]) { A[0][0] = random_uniform(-1.0, 1.0); diff --git a/tests/testPair.c b/tests/testPair.c index 08171597b7..86f3692c94 100644 --- a/tests/testPair.c +++ b/tests/testPair.c @@ -181,6 +181,10 @@ void dump_particle_fields(char *fileName, struct cell *ci, struct cell *cj) { /* Just a forward declaration... */ void runner_dopair1_density(struct runner *r, struct cell *ci, struct cell *cj); +#if defined(SHADOWSWIFT) && defined(HYDRO_DIMENSION_3D) +VORONOI3D_DECLARE_GLOBAL_VARIABLES() +#endif + int main(int argc, char *argv[]) { size_t particles = 0, runs = 0, volume, type = 0; double offset[3] = {0, 0, 0}, h = 1.1255, size = 1., rho = 1.; @@ -201,6 +205,12 @@ int main(int argc, char *argv[]) { srand(0); +#if defined(SHADOWSWIFT) && defined(HYDRO_DIMENSION_3D) + float box_anchor[3] = {-2.0f, -2.0f, -2.0f}; + float box_side[3] = {6.0f, 6.0f, 6.0f}; + voronoi_set_box(box_anchor, box_side); +#endif + while ((c = getopt(argc, argv, "h:p:r:t:d:f:")) != -1) { switch (c) { case 'h': diff --git a/tests/testRiemannExact.c b/tests/testRiemannExact.c index 1943820339..a81a8712ca 100644 --- a/tests/testRiemannExact.c +++ b/tests/testRiemannExact.c @@ -20,7 +20,10 @@ #include <string.h> #include "error.h" #include "riemann/riemann_exact.h" -#include "tools.h" + +double random_uniform(double a, double b) { + return (rand() / (double)RAND_MAX) * (b - a) + a; +} int opposite(float a, float b) { if ((a - b)) { diff --git a/tests/testRiemannHLLC.c b/tests/testRiemannHLLC.c index 4cf883b68e..f9e6242666 100644 --- a/tests/testRiemannHLLC.c +++ b/tests/testRiemannHLLC.c @@ -20,7 +20,10 @@ #include <string.h> #include "error.h" #include "riemann/riemann_hllc.h" -#include "tools.h" + +double random_uniform(double a, double b) { + return (rand() / (double)RAND_MAX) * (b - a) + a; +} int consistent_with_zero(float val) { return fabs(val) < 1.e-4; } diff --git a/tests/testRiemannTRRS.c b/tests/testRiemannTRRS.c index 18ecbdce91..6d89af11d4 100644 --- a/tests/testRiemannTRRS.c +++ b/tests/testRiemannTRRS.c @@ -20,7 +20,10 @@ #include <string.h> #include "error.h" #include "riemann/riemann_trrs.h" -#include "tools.h" + +double random_uniform(double a, double b) { + return (rand() / (double)RAND_MAX) * (b - a) + a; +} int opposite(float a, float b) { if ((a - b)) { diff --git a/tests/testSymmetry.c b/tests/testSymmetry.c index 4995aaa761..9a94b28a1f 100644 --- a/tests/testSymmetry.c +++ b/tests/testSymmetry.c @@ -26,11 +26,22 @@ #include "swift.h" +#if defined(SHADOWSWIFT) && defined(HYDRO_DIMENSION_3D) +VORONOI3D_DECLARE_GLOBAL_VARIABLES() +#endif + int main(int argc, char *argv[]) { /* Choke if need be */ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); +#if defined(SHADOWSWIFT) && defined(HYDRO_DIMENSION_3D) + /* Initialize the Voronoi simulation box */ + float box_anchor[3] = {-2.0f, -2.0f, -2.0f}; + float box_side[3] = {6.0f, 6.0f, 6.0f}; + voronoi_set_box(box_anchor, box_side); +#endif + /* Create two random particles (don't do this at home !) */ struct part pi, pj; for (size_t i = 0; i < sizeof(struct part) / sizeof(float); ++i) { diff --git a/tests/testTimeIntegration.c b/tests/testTimeIntegration.c index f39adaee90..d59906189b 100644 --- a/tests/testTimeIntegration.c +++ b/tests/testTimeIntegration.c @@ -22,6 +22,10 @@ #include <stdlib.h> #include <string.h> +#if defined(SHADOWSWIFT) && defined(HYDRO_DIMENSION_3D) +VORONOI3D_DECLARE_GLOBAL_VARIABLES() +#endif + /** * @brief Test the kick-drift-kick leapfrog integration * via a Sun-Earth simulation diff --git a/tests/testVoronoi3D.c b/tests/testVoronoi3D.c index 744b81b088..036f1e0bbe 100644 --- a/tests/testVoronoi3D.c +++ b/tests/testVoronoi3D.c @@ -1192,8 +1192,15 @@ void test_degeneracies() { #endif } +VORONOI3D_DECLARE_GLOBAL_VARIABLES() + int main() { + /* Set the all enclosing simulation box dimensions */ + float box_anchor[3] = {0.0f, 0.0f, 0.0f}; + float box_side[3] = {1.0f, 1.0f, 1.0f}; + voronoi_set_box(box_anchor, box_side); + /* Check basic Voronoi cell functions */ test_voronoi_volume_tetrahedron(); test_voronoi_centroid_tetrahedron(); -- GitLab