From d6ab77d496cbdab5d8e605f141d5bb33d4c4ddac Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Sat, 26 Mar 2016 17:47:50 +0000
Subject: [PATCH] Adapted the diffing script for various tolerance levels in
 each column

---
 tests/Makefile.am             |  2 +-
 tests/difffloat.py            | 44 ++++++++++++++++++++++++----------
 tests/test27cells.c           | 12 +++++-----
 tests/test27cells.sh          |  2 +-
 tests/test27cellsPerturbed.sh |  2 +-
 tests/testPair.c              | 45 ++++++++++++++++-------------------
 tests/testPair.sh             |  2 +-
 tests/testPairPerturbed.sh    |  2 +-
 tests/tolerance.dat           |  3 +++
 9 files changed, 66 insertions(+), 48 deletions(-)
 create mode 100644 tests/tolerance.dat

diff --git a/tests/Makefile.am b/tests/Makefile.am
index a353447909..7edf6e0659 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -45,4 +45,4 @@ test27cells_SOURCES = test27cells.c
 
 # Files necessary for distribution
 EXTRA_DIST = testReading.sh makeInput.py testPair.sh testPairPerturbed.sh \
-	     test27cells.sh test27cellsPerturbed.sh
+	     test27cells.sh test27cellsPerturbed.sh tolerance.dat
diff --git a/tests/difffloat.py b/tests/difffloat.py
index fe1118dfa0..bbb7c95a1e 100644
--- a/tests/difffloat.py
+++ b/tests/difffloat.py
@@ -25,20 +25,24 @@ rel_tol = 1e-7
 
 # Compares the content of two ASCII tables of floats line by line and
 # reports all differences beyond the given tolerances
-# Comparisons are done both in absolute and relative values
+# Comparisons are done both in absolute and relative terms
+
+# Individual tolerances for each column can be provided in a file
 
 file1 = sys.argv[1]
 file2 = sys.argv[2]
+fileTol = ""
 
-if len(sys.argv) >= 5:
-    abs_tol = float(sys.argv[3])
-    rel_tol = float(sys.argv[4])
+if len(sys.argv) == 4:
+    fileTol = sys.argv[3]
 
-print "Absolute difference tolerance:", abs_tol
-print "Relative difference tolerance:", rel_tol
-    
 data1 = loadtxt(file1)
 data2 = loadtxt(file2)
+if fileTol != "":
+    dataTol = loadtxt(fileTol)
+    n_linesTol = shape(dataTol)[0]
+    n_columnsTol = shape(dataTol)[1]
+
 
 if shape(data1) != shape(data2):
     print "Non-matching array sizes in the files", file1, "and", file2, "."
@@ -47,6 +51,22 @@ if shape(data1) != shape(data2):
 n_lines = shape(data1)[0]
 n_columns = shape(data1)[1]
 
+if fileTol != "":
+    if n_linesTol != 2:
+        print "Incorrect number of lines in tolerance file '%s'."%fileTol
+    if n_columnsTol != n_columns:
+        print "Incorrect number of columns in tolerance file '%s'."%fileTol
+
+if fileTol == "":
+    print "Absolute difference tolerance:", abs_tol
+    print "Relative difference tolerance:", rel_tol
+    absTol = ones(n_columns) * abs_tol
+    relTol = ones(n_columns) * rel_tol
+else:
+    print "Tolerances read from file"
+    absTol = dataTol[0,:]
+    relTol = dataTol[1,:]
+
 error = False
 for i in range(n_lines):
     for j in range(n_columns):
@@ -58,17 +78,17 @@ for i in range(n_lines):
             rel_diff = abs(data1[i,j] - data2[i,j]) / sum
         else:
             rel_diff = 0.
-            
-        if( abs_diff > abs_tol):
-            print "Absolute difference larger than tolerance (%e) on line %d, column %d:"%(abs_tol, i,j)
+
+        if( abs_diff > absTol[j]):
+            print "Absolute difference larger than tolerance (%e) on line %d, column %d:"%(absTol[j], i,j)
             print "%10s:           a = %e"%("File 1", data1[i,j])
             print "%10s:           b = %e"%("File 2", data2[i,j])
             print "%10s:       |a-b| = %e"%("Difference", abs_diff)
             print ""
             error = True
 
-        if( rel_diff > rel_tol):
-            print "Relative difference larger than tolerance (%e) on line %d, column %d:"%(rel_tol, i,j)
+        if( rel_diff > relTol[j]):
+            print "Relative difference larger than tolerance (%e) on line %d, column %d:"%(relTol[j], i,j)
             print "%10s:           a = %e"%("File 1", data1[i,j])
             print "%10s:           b = %e"%("File 2", data2[i,j])
             print "%10s: |a-b|/|a+b| = %e"%("Difference", rel_diff)
diff --git a/tests/test27cells.c b/tests/test27cells.c
index e9e7039179..7003ce8e6f 100644
--- a/tests/test27cells.c
+++ b/tests/test27cells.c
@@ -138,8 +138,8 @@ void dump_particle_fields(char *fileName, struct cell *main_cell,
 
   /* Write header */
   fprintf(file,
-          "# %4s %10s %10s %10s %10s %10s %10s %10s %10s  %10s   %10s %10s "
-          "%10s %10s %10s\n",
+          "# %4s %10s %10s %10s %10s %10s %10s %13s %13s %13s %13s %13s "
+          "%13s %13s %13s\n",
           "ID", "pos_x", "pos_y", "pos_z", "v_x", "v_y", "v_z", "rho", "rho_dh",
           "wcount", "wcount_dh", "div_v", "curl_vx", "curl_vy", "curl_vz");
 
@@ -148,8 +148,8 @@ void dump_particle_fields(char *fileName, struct cell *main_cell,
   /* Write main cell */
   for (size_t pid = 0; pid < main_cell->count; pid++) {
     fprintf(file,
-            "%6llu %10f %10f %10f %10f %10f %10f %10f %10f  %10f   %10f %10f "
-            "%10f %10f %10f\n",
+            "%6llu %10f %10f %10f %10f %10f %10f %13e %13e %13e %13e %13e "
+            "%13e %13e %13e\n",
             main_cell->parts[pid].id, main_cell->parts[pid].x[0],
             main_cell->parts[pid].x[1], main_cell->parts[pid].x[2],
             main_cell->parts[pid].v[0], main_cell->parts[pid].v[1],
@@ -176,8 +176,8 @@ void dump_particle_fields(char *fileName, struct cell *main_cell,
         for (size_t pjd = 0; pjd < cj->count; pjd++) {
           fprintf(
               file,
-              "%6llu %10f %10f %10f %10f %10f %10f %10f %10f  %10f   %10f %10f "
-              "%10f %10f %10f\n",
+              "%6llu %10f %10f %10f %10f %10f %10f %13e %13e %13e %13e %13e "
+              "%13e %13e %13e\n",
               cj->parts[pjd].id, cj->parts[pjd].x[0], cj->parts[pjd].x[1],
               cj->parts[pjd].x[2], cj->parts[pjd].v[0], cj->parts[pjd].v[1],
               cj->parts[pjd].v[2], cj->parts[pjd].rho, cj->parts[pjd].rho_dh,
diff --git a/tests/test27cells.sh b/tests/test27cells.sh
index 03fb5e0935..09d2513bd3 100755
--- a/tests/test27cells.sh
+++ b/tests/test27cells.sh
@@ -3,6 +3,6 @@ rm brute_force_27_standard.dat swift_dopair_27_standard.dat
 
 ./test27cells -p 6 -r 1 -d 0 -f standard
 
-python difffloat.py brute_force_27_standard.dat swift_dopair_27_standard.dat 1e-5 5e-6
+python difffloat.py brute_force_27_standard.dat swift_dopair_27_standard.dat tolerance.dat
 
 exit $?
diff --git a/tests/test27cellsPerturbed.sh b/tests/test27cellsPerturbed.sh
index 0d2f6d4762..73d2933984 100755
--- a/tests/test27cellsPerturbed.sh
+++ b/tests/test27cellsPerturbed.sh
@@ -3,6 +3,6 @@ rm brute_force_27_perturbed.dat swift_dopair_27_perturbed.dat
 
 ./test27cells -p 6 -r 1 -d 0.1 -f perturbed
 
-python difffloat.py brute_force_27_perturbed.dat swift_dopair_27_perturbed.dat 1e-5 5e-6
+python difffloat.py brute_force_27_perturbed.dat swift_dopair_27_perturbed.dat tolerance.dat
 
 exit $?
diff --git a/tests/testPair.c b/tests/testPair.c
index 8d2b4a6651..0bd513f059 100644
--- a/tests/testPair.c
+++ b/tests/testPair.c
@@ -35,7 +35,8 @@ double random_uniform(double a, double b) {
  * particles are generated on a mesh with unit spacing
  */
 struct cell *make_cell(size_t n, double *offset, double size, double h,
-                       double density, unsigned long long *partId, double pert) {
+                       double density, unsigned long long *partId,
+                       double pert) {
   const size_t count = n * n * n;
   const double volume = size * size * size;
   struct cell *cell = malloc(sizeof(struct cell));
@@ -64,7 +65,7 @@ struct cell *make_cell(size_t n, double *offset, double size, double h,
         // part->v[0] = part->x[0] - 1.5;
         // part->v[1] = part->x[1] - 1.5;
         // part->v[2] = part->x[2] - 1.5;
-	part->v[0] = random_uniform(-0.05, 0.05);
+        part->v[0] = random_uniform(-0.05, 0.05);
         part->v[1] = random_uniform(-0.05, 0.05);
         part->v[2] = random_uniform(-0.05, 0.05);
         part->h = size * h / (float)n;
@@ -127,8 +128,8 @@ void dump_particle_fields(char *fileName, struct cell *ci, struct cell *cj) {
 
   /* Write header */
   fprintf(file,
-          "# %4s %10s %10s %10s %10s %10s %10s %10s %10s  %10s   %10s %10s "
-          "%10s %10s %10s\n",
+          "# %4s %10s %10s %10s %10s %10s %10s %13s %13s %13s %13s %13s "
+          "%13s %13s %13s\n",
           "ID", "pos_x", "pos_y", "pos_z", "v_x", "v_y", "v_z", "rho", "rho_dh",
           "wcount", "wcount_dh", "div_v", "curl_vx", "curl_vy", "curl_vz");
 
@@ -136,34 +137,28 @@ void dump_particle_fields(char *fileName, struct cell *ci, struct cell *cj) {
 
   for (size_t pid = 0; pid < ci->count; pid++) {
     fprintf(file,
-            "%6llu %10f %10f %10f %10f %10f %10f %10f %10f  %10f   %10f %10f "
-            "%10f %10f %10f\n",
-            ci->parts[pid].id, ci->parts[pid].x[0],
-            ci->parts[pid].x[1], ci->parts[pid].x[2],
-            ci->parts[pid].v[0], ci->parts[pid].v[1],
-            ci->parts[pid].v[2], ci->parts[pid].rho,
-            ci->parts[pid].rho_dh, ci->parts[pid].density.wcount,
-            ci->parts[pid].density.wcount_dh,
+            "%6llu %10f %10f %10f %10f %10f %10f %13e %13e %13e %13e %13e "
+            "%13e %13e %13e\n",
+            ci->parts[pid].id, ci->parts[pid].x[0], ci->parts[pid].x[1],
+            ci->parts[pid].x[2], ci->parts[pid].v[0], ci->parts[pid].v[1],
+            ci->parts[pid].v[2], ci->parts[pid].rho, ci->parts[pid].rho_dh,
+            ci->parts[pid].density.wcount, ci->parts[pid].density.wcount_dh,
             ci->parts[pid].div_v, ci->parts[pid].density.rot_v[0],
-            ci->parts[pid].density.rot_v[1],
-            ci->parts[pid].density.rot_v[2]);
+            ci->parts[pid].density.rot_v[1], ci->parts[pid].density.rot_v[2]);
   }
 
   fprintf(file, "# cj --------------------------------------------\n");
-  
+
   for (size_t pjd = 0; pjd < cj->count; pjd++) {
     fprintf(file,
-            "%6llu %10f %10f %10f %10f %10f %10f %10f %10f  %10f   %10f %10f "
-            "%10f %10f %10f\n",
-            cj->parts[pjd].id, cj->parts[pjd].x[0],
-            cj->parts[pjd].x[1], cj->parts[pjd].x[2],
-            cj->parts[pjd].v[0], cj->parts[pjd].v[1],
-            cj->parts[pjd].v[2], cj->parts[pjd].rho,
-            cj->parts[pjd].rho_dh, cj->parts[pjd].density.wcount,
-            cj->parts[pjd].density.wcount_dh,
+            "%6llu %10f %10f %10f %10f %10f %10f %13e %13e %13e %13e %13e "
+            "%13e %13e %13e\n",
+            cj->parts[pjd].id, cj->parts[pjd].x[0], cj->parts[pjd].x[1],
+            cj->parts[pjd].x[2], cj->parts[pjd].v[0], cj->parts[pjd].v[1],
+            cj->parts[pjd].v[2], cj->parts[pjd].rho, cj->parts[pjd].rho_dh,
+            cj->parts[pjd].density.wcount, cj->parts[pjd].density.wcount_dh,
             cj->parts[pjd].div_v, cj->parts[pjd].density.rot_v[0],
-            cj->parts[pjd].density.rot_v[1],
-            cj->parts[pjd].density.rot_v[2]);
+            cj->parts[pjd].density.rot_v[1], cj->parts[pjd].density.rot_v[2]);
   }
 
   fclose(file);
diff --git a/tests/testPair.sh b/tests/testPair.sh
index d39ad74e3f..f6f505e56a 100755
--- a/tests/testPair.sh
+++ b/tests/testPair.sh
@@ -3,6 +3,6 @@ rm brute_force_standard.dat swift_dopair_standard.dat
 
 ./testPair -p 6 -r 1 -d 0 -f standard
 
-python difffloat.py brute_force_standard.dat swift_dopair_standard.dat 1e-5 5e-6
+python difffloat.py brute_force_standard.dat swift_dopair_standard.dat tolerance.dat
 
 exit $?
diff --git a/tests/testPairPerturbed.sh b/tests/testPairPerturbed.sh
index c3c6fc82eb..544ba1b032 100755
--- a/tests/testPairPerturbed.sh
+++ b/tests/testPairPerturbed.sh
@@ -3,6 +3,6 @@ rm brute_force_perturbed.dat swift_dopair_perturbed.dat
 
 ./testPair -p 6 -r 1 -d 0.1 -f perturbed
 
-python difffloat.py brute_force_perturbed.dat swift_dopair_perturbed.dat 1e-5 5e-6
+python difffloat.py brute_force_perturbed.dat swift_dopair_perturbed.dat tolerance.dat
 
 exit $?
diff --git a/tests/tolerance.dat b/tests/tolerance.dat
new file mode 100644
index 0000000000..2122579f80
--- /dev/null
+++ b/tests/tolerance.dat
@@ -0,0 +1,3 @@
+#   ID      pos_x      pos_y      pos_z        v_x        v_y        v_z           rho        rho_dh        wcount     wcount_dh         div_v       curl_vx       curl_vy       curl_vz
+    0	    1e-6       1e-6	  1e-6 	       1e-6 	  1e-6	     1e-6	   1e-5	      1e-5	    2e-5       3e-4		 1e-5	     1e-5	   1e-5		 1e-5
+    0	    1e-6       1e-6	  1e-6 	       1e-6 	  1e-6	     1e-6	   1e-5	      1e-5	    1e-5       1e-5		 1e-4	     1e-4	   1e-4		 1e-4
-- 
GitLab