From 84f6c5bc8f8e6a99f6fce7ad4bf6054e6f17423c Mon Sep 17 00:00:00 2001
From: Bert Vandenbroucke <bert.vandenbroucke@gmail.com>
Date: Wed, 15 Mar 2017 10:17:16 +0000
Subject: [PATCH] Fixed small mistake in 2D Voronoi neighbour calculation.
 Added check for neighbour relations to unit test.

---
 src/hydro/Shadowswift/voronoi2d_algorithm.h |  2 +-
 tests/testVoronoi2D.c                       | 39 +++++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/src/hydro/Shadowswift/voronoi2d_algorithm.h b/src/hydro/Shadowswift/voronoi2d_algorithm.h
index 6bf976772b..ff3a2dddc3 100644
--- a/src/hydro/Shadowswift/voronoi2d_algorithm.h
+++ b/src/hydro/Shadowswift/voronoi2d_algorithm.h
@@ -428,7 +428,7 @@ __attribute__((always_inline)) INLINE void voronoi_cell_interact(
                        b2 * cell->vertices[index_above2][0];
   vertices[nvert][1] = a2 * cell->vertices[index_below2][1] +
                        b2 * cell->vertices[index_above2][1];
-  ngbs[nvert] = cell->ngbs[index_above1];
+  ngbs[nvert] = cell->ngbs[index_above2];
   ++nvert;
   VORONOI_CHECK_SIZE();
 
diff --git a/tests/testVoronoi2D.c b/tests/testVoronoi2D.c
index 308f83d221..a13521d7e9 100644
--- a/tests/testVoronoi2D.c
+++ b/tests/testVoronoi2D.c
@@ -34,6 +34,8 @@ int main() {
 
   /* test initialization and finalization algorithms */
   {
+    message("Testing initialization and finalization algorithm...");
+
     struct voronoi_cell cell;
     double x[3] = {0.5, 0.5, 0.5};
 
@@ -47,10 +49,15 @@ int main() {
 
     assert(cell.centroid[0] == 0.5f);
     assert(cell.centroid[1] == 0.5f);
+
+    message("Done.");
   }
 
   /* test interaction algorithm: normal case */
   {
+    message("Testing %i cell grid with random positions...",
+            TESTVORONOI2D_NUMCELL);
+
     /* create 100 cells with random positions in [0,1]x[0,1] */
     struct voronoi_cell cells[TESTVORONOI2D_NUMCELL];
     double x[2];
@@ -102,16 +109,34 @@ int main() {
 
     /* print the cells to the stdout */
     for (i = 0; i < TESTVORONOI2D_NUMCELL; ++i) {
+      printf("Cell %i:\n", i);
       voronoi_print_cell(&cells[i]);
       voronoi_cell_finalize(&cells[i]);
       Atot += cells[i].volume;
     }
 
+    /* Check the total surface area */
     assert(fabs(Atot - 1.0f) < 1.e-6);
+
+    /* Check the neighbour relations for an arbitrary cell: cell 44
+       We plotted the grid and manually found the correct neighbours and their
+       order. */
+    assert(cells[44].nvert == 7);
+    assert(cells[44].ngbs[0] == 26);
+    assert(cells[44].ngbs[1] == 38);
+    assert(cells[44].ngbs[2] == 3);
+    assert(cells[44].ngbs[3] == 33);
+    assert(cells[44].ngbs[4] == 5);
+    assert(cells[44].ngbs[5] == 90);
+    assert(cells[44].ngbs[6] == 4);
+
+    message("Done.");
   }
 
   /* test interaction algorithm */
   {
+    message("Testing 100 cell grid with Cartesian mesh positions...");
+
     struct voronoi_cell cells[100];
     double x[2];
     float dx[2];
@@ -161,12 +186,26 @@ int main() {
 
     /* print the cells to the stdout */
     for (i = 0; i < 100; ++i) {
+      printf("Cell %i:\n", i);
       voronoi_print_cell(&cells[i]);
       voronoi_cell_finalize(&cells[i]);
       Atot += cells[i].volume;
     }
 
+    /* Check the total surface area */
     assert(fabs(Atot - 1.0f) < 1.e-6);
+
+    /* Check the neighbour relations for an arbitrary cell: cell 44
+       We plotted the grid and manually found the correct neighbours and their
+       order. */
+    assert(cells[44].nvert == 5);
+    assert(cells[44].ngbs[0] == 34);
+    assert(cells[44].ngbs[1] == 35);
+    assert(cells[44].ngbs[2] == 45);
+    assert(cells[44].ngbs[3] == 54);
+    assert(cells[44].ngbs[4] == 43);
+
+    message("Done.");
   }
 
   return 0;
-- 
GitLab