From e6a7f8beca554f4523eca1638c4ea8cd669c053c Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Thu, 5 Dec 2019 11:45:18 +0100
Subject: [PATCH] Update the unit tests using the cosmology module for the new
 function signature.

---
 src/outputlist.c       |  2 +-
 tests/testCosmology.c  | 30 ++++++++++++++++++++++--------
 tests/testOutputList.c | 17 ++++++++++++++++-
 3 files changed, 39 insertions(+), 10 deletions(-)

diff --git a/src/outputlist.c b/src/outputlist.c
index da841103d2..d49f0abd98 100644
--- a/src/outputlist.c
+++ b/src/outputlist.c
@@ -106,7 +106,7 @@ void output_list_read_file(struct output_list *outputlist, const char *filename,
     if (type == OUTPUT_LIST_REDSHIFT) *time = 1. / (1. + *time);
 
     if (cosmo && type == OUTPUT_LIST_AGE)
-      *time = cosmology_get_scale_factor(cosmo, *time);
+      *time = cosmology_get_scale_factor_from_time(cosmo, *time);
 
     /* Update size */
     ind += 1;
diff --git a/tests/testCosmology.c b/tests/testCosmology.c
index 05dc69b2a9..a574773cf1 100644
--- a/tests/testCosmology.c
+++ b/tests/testCosmology.c
@@ -20,11 +20,15 @@
 /* Some standard headers. */
 #include "../config.h"
 
+/* Some standard headers */
+#include <fenv.h>
+#include <math.h>
+
 /* Includes. */
 #include "swift.h"
 
-#define N_CHECK 20
-#define TOLERANCE 1e-7
+#define N_CHECK 200
+#define TOLERANCE 1e-6
 
 void test_params_init(struct swift_params *params) {
   parser_init("", params);
@@ -32,12 +36,21 @@ void test_params_init(struct swift_params *params) {
   parser_set_param(params, "Cosmology:Omega_lambda:0.6910");
   parser_set_param(params, "Cosmology:Omega_b:0.0486");
   parser_set_param(params, "Cosmology:h:0.6774");
-  parser_set_param(params, "Cosmology:a_begin:0.1");
+  parser_set_param(params, "Cosmology:a_begin:0.01");
   parser_set_param(params, "Cosmology:a_end:1.0");
 }
 
 int main(int argc, char *argv[]) {
 
+  /* Initialize CPU frequency, this also starts time. */
+  unsigned long long cpufreq = 0;
+  clocks_set_cpufreq(cpufreq);
+
+/* Choke on FP-exceptions */
+#ifdef HAVE_FE_ENABLE_EXCEPT
+  feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
+#endif
+
   message("Initialization...");
 
   /* pseudo initialization of params */
@@ -60,14 +73,15 @@ int main(int argc, char *argv[]) {
 
   for (int i = 0; i < N_CHECK; i++) {
     double a = 0.1 + 0.9 * i / (N_CHECK - 1.);
+
     /* Compute a(t(a)) and check if same results */
-    double tmp = cosmology_get_time_since_big_bang(&cosmo, a);
-    tmp = cosmology_get_scale_factor(&cosmo, tmp);
+    double time = cosmology_get_time_since_big_bang(&cosmo, a);
+    double my_a = cosmology_get_scale_factor_from_time(&cosmo, time);
 
     /* check accuracy */
-    tmp = (tmp - a) / a;
-    message("Accuracy of %g at a=%g", tmp, a);
-    assert(fabs(tmp) < TOLERANCE);
+    double rel_err = (my_a - a) / a;
+    message("Accuracy of %g at a=%g", rel_err, a);
+    assert(fabs(rel_err) < TOLERANCE);
   }
 
   message("Everything seems fine with cosmology.");
diff --git a/tests/testOutputList.c b/tests/testOutputList.c
index fd69ef9138..dfb873b64e 100644
--- a/tests/testOutputList.c
+++ b/tests/testOutputList.c
@@ -19,6 +19,10 @@
  ******************************************************************************/
 #include "../config.h"
 
+/* Some standard headers */
+#include <fenv.h>
+#include <math.h>
+
 /* Includes. */
 #include "swift.h"
 
@@ -86,7 +90,8 @@ void test_cosmo(struct engine *e, char *name, int with_assert) {
   double output_time = 0;
 
   /* Test Time */
-  e->time_base = log(e->time_end / e->cosmology->a_begin) / max_nr_timesteps;
+  e->time_base =
+      log(e->cosmology->a_end / e->cosmology->a_begin) / max_nr_timesteps;
   e->ti_current = 0;
   e->policy = engine_policy_cosmology;
 
@@ -115,6 +120,16 @@ void test_cosmo(struct engine *e, char *name, int with_assert) {
 };
 
 int main(int argc, char *argv[]) {
+
+  /* Initialize CPU frequency, this also starts time. */
+  unsigned long long cpufreq = 0;
+  clocks_set_cpufreq(cpufreq);
+
+/* Choke on FP-exceptions */
+#ifdef HAVE_FE_ENABLE_EXCEPT
+  feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
+#endif
+
   /* Create a structure to read file into. */
   struct swift_params params;
 
-- 
GitLab