From 01c115084037b1efbddb63ce2cdd030b44b9f8fa Mon Sep 17 00:00:00 2001
From: Bert Vandenbroucke <bert.vandenbroucke@gmail.com>
Date: Fri, 8 Jan 2021 18:20:32 +0100
Subject: [PATCH] Figured out a way to initialise the Delaunay tessellation in
 the drift task. Requires some magic, including adding libgmp to the linker
 command.

---
 configure.ac                   | 37 ++++++++++++++++++++++++++++++++++
 examples/Makefile.am           |  2 +-
 src/Makefile.am                |  2 +-
 src/cell_drift.c               |  8 ++++++++
 src/cell_hydro.h               |  2 +-
 src/shadowfax/cell_shadowfax.h |  8 ++++----
 src/shadowfax/delaunay.h       |  8 ++++----
 src/shadowfax/voronoi.h        |  4 ++--
 8 files changed, 58 insertions(+), 13 deletions(-)

diff --git a/configure.ac b/configure.ac
index ee6649a7ad..9181685653 100644
--- a/configure.ac
+++ b/configure.ac
@@ -693,6 +693,42 @@ AC_SUBST([GSL_LIBS])
 AC_SUBST([GSL_INCS])
 AM_CONDITIONAL([HAVEGSL],[test -n "$GSL_LIBS"])
 
+# Check for GMP. We test for this in the standard directories by default,
+# and only disable if using --with-gmp=no or --without-gmp. When a value
+# is given GMP must be found.
+have_gmp="no"
+AC_ARG_WITH([gmp],
+    [AS_HELP_STRING([--with-gmp=PATH],
+       [root directory where GMP is installed @<:@yes/no@:>@]
+    )],
+    [with_gmp="$withval"],
+    [with_gmp="test"]
+)
+if test "x$with_gmp" != "xno"; then
+   if test "x$with_gmp" != "xyes" -a "x$with_gmp" != "xtest" -a "x$with_gmp" != "x"; then
+      GMP_LIBS="-L$with_gmp/lib -lgsl -lgslcblas"
+   else
+      GMP_LIBS="-lgmp"
+   fi
+   #  GMP is not specified, so just check if we have it.
+   if test "x$with_gmp" = "xtest"; then
+      AC_CHECK_LIB([gmp],[__gmpz_inits],[have_gmp="yes"],[have_gmp="no"],$GMP_LIBS)
+      if test "x$have_gmp" != "xno"; then
+         AC_DEFINE([HAVE_LIBGMP],1,[The GMP library appears to be present.])
+      fi
+   else
+      AC_CHECK_LIB([gmp],[__gmpz_inits],
+         AC_DEFINE([HAVE_LIBGMP],1,[The GMP library appears to be present.]),
+         AC_MSG_ERROR(something is wrong with the GMP library!), $GMP_LIBS)
+      have_gmp="yes"
+   fi
+   if test "$have_gmp" = "no"; then
+      GMP_LIBS=""
+   fi
+fi
+AC_SUBST([GMP_LIBS])
+AM_CONDITIONAL([HAVEGMP],[test -n "$GMP_LIBS"])
+
 # Check for pthreads.
 AX_PTHREAD([LIBS="$PTHREAD_LIBS $LIBS" CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
     CC="$PTHREAD_CC" LDFLAGS="$LDFLAGS $PTHREAD_LIBS $LIBS"],
@@ -2456,6 +2492,7 @@ AC_MSG_RESULT([
    METIS/ParMETIS       : $have_metis / $have_parmetis
    FFTW3 enabled        : $have_fftw
    GSL enabled          : $have_gsl
+   GMP enabled          : $have_gmp
    libNUMA enabled      : $have_numa
    GRACKLE enabled      : $have_grackle
    Special allocators   : $have_special_allocator
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 64735be2e1..e21f702b20 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -27,7 +27,7 @@ AM_LDFLAGS = $(HDF5_LDFLAGS)
 # Extra libraries.
 EXTRA_LIBS = $(HDF5_LIBS) $(FFTW_LIBS) $(NUMA_LIBS) $(PROFILER_LIBS) \
 	$(TCMALLOC_LIBS) $(JEMALLOC_LIBS) $(TBBMALLOC_LIBS) $(GRACKLE_LIBS) \
-	$(VELOCIRAPTOR_LIBS) $(GSL_LIBS)
+	$(VELOCIRAPTOR_LIBS) $(GSL_LIBS) $(GMP_LIBS)
 
 # MPI libraries.
 MPI_LIBS = $(PARMETIS_LIBS) $(METIS_LIBS) $(MPI_THREAD_LIBS)
diff --git a/src/Makefile.am b/src/Makefile.am
index ad1a3b8c33..8d02acf0df 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -25,7 +25,7 @@ AM_LDFLAGS = $(HDF5_LDFLAGS) $(FFTW_LIBS)
 GIT_CMD = @GIT_CMD@
 
 # Additional dependencies for shared libraries.
-EXTRA_LIBS = $(HDF5_LIBS) $(FFTW_LIBS) $(NUMA_LIBS) $(PROFILER_LIBS) $(TCMALLOC_LIBS) $(JEMALLOC_LIBS) $(TBBMALLOC_LIBS) $(GRACKLE_LIBS) $(GSL_LIBS)
+EXTRA_LIBS = $(HDF5_LIBS) $(FFTW_LIBS) $(NUMA_LIBS) $(PROFILER_LIBS) $(TCMALLOC_LIBS) $(JEMALLOC_LIBS) $(TBBMALLOC_LIBS) $(GRACKLE_LIBS) $(GSL_LIBS) $(GMP_LIBS)
 
 # MPI libraries.
 MPI_LIBS = $(PARMETIS_LIBS) $(METIS_LIBS) $(MPI_THREAD_LIBS)
diff --git a/src/cell_drift.c b/src/cell_drift.c
index 291316b17b..008516fdb6 100644
--- a/src/cell_drift.c
+++ b/src/cell_drift.c
@@ -36,6 +36,10 @@
 #include "star_formation.h"
 #include "tracers.h"
 
+#ifdef SHADOWFAX_SPH
+#include "shadowfax/cell_shadowfax.h"
+#endif
+
 /**
  * @brief Recursively drifts the #part in a cell hierarchy.
  *
@@ -236,6 +240,10 @@ void cell_drift_part(struct cell *c, const struct engine *e, int force) {
       }
     }
 
+#ifdef SHADOWFAX_SPH
+    cell_malloc_delaunay_tesselation(c, &e->s->hs);
+#endif
+
     /* Now, get the maximal particle motion from its square */
     dx_max = sqrtf(dx2_max);
     dx_max_sort = sqrtf(dx2_max_sort);
diff --git a/src/cell_hydro.h b/src/cell_hydro.h
index 19928930d9..566e231cb2 100644
--- a/src/cell_hydro.h
+++ b/src/cell_hydro.h
@@ -43,7 +43,7 @@ struct cell_hydro {
 
   /*! Pointer to the #xpart data. */
   struct xpart *xparts;
-  
+
 #ifdef SHADOWFAX_SPH
   /*! Delaunay tessellation. */
   struct delaunay deltess;
diff --git a/src/shadowfax/cell_shadowfax.h b/src/shadowfax/cell_shadowfax.h
index 387225ca02..88a55d3cf4 100644
--- a/src/shadowfax/cell_shadowfax.h
+++ b/src/shadowfax/cell_shadowfax.h
@@ -4,10 +4,10 @@
 #include "cell.h"
 #include "delaunay.h"
 
-__attribute__((always_inline)) INLINE static void cell_malloc_delaunay_tesselation(
-    struct cell *c, const struct hydro_space *hs) {
-    
-    delaunay_init(&c->hydro.deltess, hs, 100, 100);
+__attribute__((always_inline)) INLINE static void
+cell_malloc_delaunay_tesselation(struct cell *c, const struct hydro_space *hs) {
+
+  delaunay_init(&c->hydro.deltess, hs, 100, 100);
 }
 
 #endif /* SWIFT_CELL_SHADOWFAX_H */
diff --git a/src/shadowfax/delaunay.h b/src/shadowfax/delaunay.h
index 9e15c3bbc4..b9b5ab841f 100644
--- a/src/shadowfax/delaunay.h
+++ b/src/shadowfax/delaunay.h
@@ -28,15 +28,15 @@
 #ifndef SWIFT_DELAUNAY_H
 #define SWIFT_DELAUNAY_H
 
+#include "geometry.h"
+#include "hydro_space.h"
+#include "triangle.h"
+
 #include <float.h>
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 
-#include "geometry.h"
-#include "hydro_space.h"
-#include "triangle.h"
-
 /*! @brief Activate extensive log output. */
 /*#define DELAUNAY_LOG_OUTPUT*/
 /*! @brief Activate runtime assertions. */
diff --git a/src/shadowfax/voronoi.h b/src/shadowfax/voronoi.h
index 4e11aca563..ef8f0fecbb 100644
--- a/src/shadowfax/voronoi.h
+++ b/src/shadowfax/voronoi.h
@@ -28,10 +28,10 @@
 #ifndef SWIFT_VORONOI_H
 #define SWIFT_VORONOI_H
 
-#include <string.h>
-
 #include "voronoi.h"
 
+#include <string.h>
+
 /**
  * @brief Voronoi grid.
  *
-- 
GitLab