From 06c31aa452d8509fff781e9f4883cc904cb7778d Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Thu, 4 Jul 2019 10:52:00 +0200
Subject: [PATCH] Box-wrap the stars and black holes in the snapshots.

---
 src/black_holes/Default/black_holes_io.h | 19 +++++++++++++++++--
 src/black_holes/EAGLE/black_holes_io.h   | 18 ++++++++++++++++--
 src/stars/Default/stars_io.h             | 18 ++++++++++++++++--
 src/stars/EAGLE/stars_io.h               | 18 ++++++++++++++++--
 4 files changed, 65 insertions(+), 8 deletions(-)

diff --git a/src/black_holes/Default/black_holes_io.h b/src/black_holes/Default/black_holes_io.h
index e193f550bf..41ca7c1cd0 100644
--- a/src/black_holes/Default/black_holes_io.h
+++ b/src/black_holes/Default/black_holes_io.h
@@ -22,6 +22,20 @@
 #include "black_holes_part.h"
 #include "io_properties.h"
 
+INLINE static void convert_bpart_pos(const struct engine *e,
+                                     const struct bpart *bp, double *ret) {
+
+  if (e->s->periodic) {
+    ret[0] = box_wrap(bp->x[0], 0.0, e->s->dim[0]);
+    ret[1] = box_wrap(bp->x[1], 0.0, e->s->dim[1]);
+    ret[2] = box_wrap(bp->x[2], 0.0, e->s->dim[2]);
+  } else {
+    ret[0] = bp->x[0];
+    ret[1] = bp->x[1];
+    ret[2] = bp->x[2];
+  }
+}
+
 /**
  * @brief Specifies which b-particle fields to read from a dataset
  *
@@ -37,8 +51,9 @@ INLINE static void black_holes_read_particles(struct bpart *bparts,
   *num_fields = 5;
 
   /* List what we want to read */
-  list[0] = io_make_input_field("Coordinates", DOUBLE, 3, COMPULSORY,
-                                UNIT_CONV_LENGTH, bparts, x);
+  list[0] = io_make_output_field_convert_bpart(
+      "Coordinates", DOUBLE, 3, UNIT_CONV_LENGTH, bparts, convert_bpart_pos);
+
   list[1] = io_make_input_field("Velocities", FLOAT, 3, COMPULSORY,
                                 UNIT_CONV_SPEED, bparts, v);
   list[2] = io_make_input_field("Masses", FLOAT, 1, COMPULSORY, UNIT_CONV_MASS,
diff --git a/src/black_holes/EAGLE/black_holes_io.h b/src/black_holes/EAGLE/black_holes_io.h
index cc6cb962db..f5a8da344f 100644
--- a/src/black_holes/EAGLE/black_holes_io.h
+++ b/src/black_holes/EAGLE/black_holes_io.h
@@ -22,6 +22,20 @@
 #include "black_holes_part.h"
 #include "io_properties.h"
 
+INLINE static void convert_bpart_pos(const struct engine *e,
+                                     const struct bpart *bp, double *ret) {
+
+  if (e->s->periodic) {
+    ret[0] = box_wrap(bp->x[0], 0.0, e->s->dim[0]);
+    ret[1] = box_wrap(bp->x[1], 0.0, e->s->dim[1]);
+    ret[2] = box_wrap(bp->x[2], 0.0, e->s->dim[2]);
+  } else {
+    ret[0] = bp->x[0];
+    ret[1] = bp->x[1];
+    ret[2] = bp->x[2];
+  }
+}
+
 /**
  * @brief Specifies which b-particle fields to read from a dataset
  *
@@ -66,8 +80,8 @@ INLINE static void black_holes_write_particles(const struct bpart *bparts,
   *num_fields = 12;
 
   /* List what we want to write */
-  list[0] = io_make_output_field("Coordinates", DOUBLE, 3, UNIT_CONV_LENGTH,
-                                 bparts, x);
+  list[0] = io_make_output_field_convert_bpart(
+      "Coordinates", DOUBLE, 3, UNIT_CONV_LENGTH, bparts, convert_bpart_pos);
   list[1] =
       io_make_output_field("Velocities", FLOAT, 3, UNIT_CONV_SPEED, bparts, v);
   list[2] =
diff --git a/src/stars/Default/stars_io.h b/src/stars/Default/stars_io.h
index 5ff57549b9..a8ec1cfa55 100644
--- a/src/stars/Default/stars_io.h
+++ b/src/stars/Default/stars_io.h
@@ -22,6 +22,20 @@
 #include "io_properties.h"
 #include "stars_part.h"
 
+INLINE static void convert_spart_pos(const struct engine *e,
+                                     const struct spart *sp, double *ret) {
+
+  if (e->s->periodic) {
+    ret[0] = box_wrap(sp->x[0], 0.0, e->s->dim[0]);
+    ret[1] = box_wrap(sp->x[1], 0.0, e->s->dim[1]);
+    ret[2] = box_wrap(sp->x[2], 0.0, e->s->dim[2]);
+  } else {
+    ret[0] = sp->x[0];
+    ret[1] = sp->x[1];
+    ret[2] = sp->x[2];
+  }
+}
+
 /**
  * @brief Specifies which s-particle fields to read from a dataset
  *
@@ -64,8 +78,8 @@ INLINE static void stars_write_particles(const struct spart *sparts,
   *num_fields = 5;
 
   /* List what we want to write */
-  list[0] = io_make_output_field("Coordinates", DOUBLE, 3, UNIT_CONV_LENGTH,
-                                 sparts, x);
+  list[0] = io_make_output_field_convert_spart(
+      "Coordinates", DOUBLE, 3, UNIT_CONV_LENGTH, sparts, convert_spart_pos);
   list[1] =
       io_make_output_field("Velocities", FLOAT, 3, UNIT_CONV_SPEED, sparts, v);
   list[2] =
diff --git a/src/stars/EAGLE/stars_io.h b/src/stars/EAGLE/stars_io.h
index 0c03bee300..cfacd52106 100644
--- a/src/stars/EAGLE/stars_io.h
+++ b/src/stars/EAGLE/stars_io.h
@@ -23,6 +23,20 @@
 #include "io_properties.h"
 #include "stars_part.h"
 
+INLINE static void convert_spart_pos(const struct engine *e,
+                                     const struct spart *sp, double *ret) {
+
+  if (e->s->periodic) {
+    ret[0] = box_wrap(sp->x[0], 0.0, e->s->dim[0]);
+    ret[1] = box_wrap(sp->x[1], 0.0, e->s->dim[1]);
+    ret[2] = box_wrap(sp->x[2], 0.0, e->s->dim[2]);
+  } else {
+    ret[0] = sp->x[0];
+    ret[1] = sp->x[1];
+    ret[2] = sp->x[2];
+  }
+}
+
 /**
  * @brief Specifies which s-particle fields to read from a dataset
  *
@@ -67,8 +81,8 @@ INLINE static void stars_write_particles(const struct spart *sparts,
   *num_fields = 10;
 
   /* List what we want to write */
-  list[0] = io_make_output_field("Coordinates", DOUBLE, 3, UNIT_CONV_LENGTH,
-                                 sparts, x);
+  list[0] = io_make_output_field_convert_spart(
+      "Coordinates", DOUBLE, 3, UNIT_CONV_LENGTH, sparts, convert_spart_pos);
   list[1] =
       io_make_output_field("Velocities", FLOAT, 3, UNIT_CONV_SPEED, sparts, v);
   list[2] =
-- 
GitLab