From b27fa98f7245a2f678ac5ef5f7565db280131b4e Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Wed, 4 Dec 2019 10:05:13 +0100
Subject: [PATCH] Un-dither the position of the cells in the cell meta-data to
 make them match the particle positions.

---
 src/common_io.c   | 14 +++++++++++++-
 src/common_io.h   |  3 ++-
 src/parallel_io.c |  6 +++---
 src/serial_io.c   |  6 +++---
 src/single_io.c   |  7 ++++---
 5 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/src/common_io.c b/src/common_io.c
index 2ad47f12be..fc524bacdb 100644
--- a/src/common_io.c
+++ b/src/common_io.c
@@ -545,7 +545,8 @@ static long long cell_count_non_inhibited_black_holes(const struct cell* c) {
   return count;
 }
 
-void io_write_cell_offsets(hid_t h_grp, const int cdim[3],
+void io_write_cell_offsets(hid_t h_grp, const int cdim[3], const double dim[3],
+                           const double pos_dithering[3],
                            const struct cell* cells_top, const int nr_cells,
                            const double width[3], const int nodeID,
                            const long long global_counts[swift_type_count],
@@ -601,6 +602,17 @@ void io_write_cell_offsets(hid_t h_grp, const int cdim[3],
       centres[i * 3 + 1] = cells_top[i].loc[1] + cell_width[1] * 0.5;
       centres[i * 3 + 2] = cells_top[i].loc[2] + cell_width[2] * 0.5;
 
+      /* Undo the dithering since the particles will have this vector applied to
+       * them */
+      centres[i * 3 + 0] = centres[i * 3 + 0] - pos_dithering[0];
+      centres[i * 3 + 1] = centres[i * 3 + 1] - pos_dithering[1];
+      centres[i * 3 + 2] = centres[i * 3 + 2] - pos_dithering[2];
+
+      /* Finish by box wrapping to match what is done to the particles */
+      centres[i * 3 + 0] = box_wrap(centres[i * 3 + 0], 0.0, dim[0]);
+      centres[i * 3 + 1] = box_wrap(centres[i * 3 + 1], 0.0, dim[1]);
+      centres[i * 3 + 2] = box_wrap(centres[i * 3 + 2], 0.0, dim[2]);
+
       /* Count real particles that will be written */
       count_part[i] = cell_count_non_inhibited_gas(&cells_top[i]);
       count_gpart[i] = cell_count_non_inhibited_dark_matter(&cells_top[i]);
diff --git a/src/common_io.h b/src/common_io.h
index de73db3476..024d6027c5 100644
--- a/src/common_io.h
+++ b/src/common_io.h
@@ -86,7 +86,8 @@ void io_write_attribute_s(hid_t grp, const char* name, const char* str);
 void io_write_code_description(hid_t h_file);
 void io_write_engine_policy(hid_t h_file, const struct engine* e);
 
-void io_write_cell_offsets(hid_t h_grp, const int cdim[3],
+void io_write_cell_offsets(hid_t h_grp, const int cdim[3], const double dim[3],
+                           const double pos_dithering[3],
                            const struct cell* cells_top, const int nr_cells,
                            const double width[3], const int nodeID,
                            const long long global_counts[swift_type_count],
diff --git a/src/parallel_io.c b/src/parallel_io.c
index 804013f1dd..154ec442c6 100644
--- a/src/parallel_io.c
+++ b/src/parallel_io.c
@@ -1477,9 +1477,9 @@ void write_output_parallel(struct engine* e, const char* baseName,
   }
 
   /* Write the location of the particles in the arrays */
-  io_write_cell_offsets(h_grp_cells, e->s->cdim, e->s->cells_top,
-                        e->s->nr_cells, e->s->width, mpi_rank, N_total, offset,
-                        internal_units, snapshot_units);
+  io_write_cell_offsets(h_grp_cells, e->s->cdim, e->s->dim, e->s->pos_dithering,
+                        e->s->cells_top, e->s->nr_cells, e->s->width, mpi_rank,
+                        N_total, offset, internal_units, snapshot_units);
 
   /* Close everything */
   if (mpi_rank == 0) {
diff --git a/src/serial_io.c b/src/serial_io.c
index e291bf49ec..9efa2e81aa 100644
--- a/src/serial_io.c
+++ b/src/serial_io.c
@@ -1157,9 +1157,9 @@ void write_output_serial(struct engine* e, const char* baseName,
   }
 
   /* Write the location of the particles in the arrays */
-  io_write_cell_offsets(h_grp_cells, e->s->cdim, e->s->cells_top,
-                        e->s->nr_cells, e->s->width, mpi_rank, N_total, offset,
-                        internal_units, snapshot_units);
+  io_write_cell_offsets(h_grp_cells, e->s->cdim, e->s->dim, e->s->pos_dithering,
+                        e->s->cells_top, e->s->nr_cells, e->s->width, mpi_rank,
+                        N_total, offset, internal_units, snapshot_units);
 
   /* Close everything */
   if (mpi_rank == 0) {
diff --git a/src/single_io.c b/src/single_io.c
index 6385b85988..05ccfa8fde 100644
--- a/src/single_io.c
+++ b/src/single_io.c
@@ -937,9 +937,10 @@ void write_output_single(struct engine* e, const char* baseName,
   if (h_grp < 0) error("Error while creating cells group");
 
   /* Write the location of the particles in the arrays */
-  io_write_cell_offsets(h_grp, e->s->cdim, e->s->cells_top, e->s->nr_cells,
-                        e->s->width, e->nodeID, N_total, global_offsets,
-                        internal_units, snapshot_units);
+  io_write_cell_offsets(h_grp, e->s->cdim, e->s->dim, e->s->pos_dithering,
+                        e->s->cells_top, e->s->nr_cells, e->s->width, e->nodeID,
+                        N_total, global_offsets, internal_units,
+                        snapshot_units);
   H5Gclose(h_grp);
 
   /* Tell the user if a conversion will be needed */
-- 
GitLab