From 5ca56fa8b308702330d7aba5bd9ff85e70f2d6df Mon Sep 17 00:00:00 2001
From: Pedro Gonnet <gonnet@google.com>
Date: Tue, 3 Jan 2017 14:56:09 +0100
Subject: [PATCH] first stab at the logger, which copies particle data to a
 dump.

---
 src/Makefile.am |  6 ++--
 src/logger.c    | 86 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/logger.h    | 78 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 167 insertions(+), 3 deletions(-)
 create mode 100644 src/logger.c
 create mode 100644 src/logger.h

diff --git a/src/Makefile.am b/src/Makefile.am
index 32262b8acf..3aab888c70 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -44,7 +44,7 @@ include_HEADERS = space.h runner.h queue.h task.h lock.h cell.h part.h const.h \
     common_io.h single_io.h multipole.h map.h tools.h partition.h clocks.h parser.h \
     physical_constants.h physical_constants_cgs.h potential.h version.h \
     hydro_properties.h riemann.h threadpool.h cooling.h cooling_struct.h sourceterms.h \
-    sourceterms_struct.h statistics.h memswap.h profiler.h dump.h
+    sourceterms_struct.h statistics.h memswap.h profiler.h dump.h logger.h
 
 # Common source files
 AM_SOURCES = space.c runner.c queue.c task.c cell.c engine.c \
@@ -53,7 +53,7 @@ AM_SOURCES = space.c runner.c queue.c task.c cell.c engine.c \
     kernel_hydro.c tools.c part.c partition.c clocks.c parser.c \
     physical_constants.c potential.c hydro_properties.c \
     runner_doiact_fft.c threadpool.c cooling.c sourceterms.c \
-    statistics.c profiler.c dump.c
+    statistics.c profiler.c dump.c logger.c
 
 # Include files for distribution, not installation.
 nobase_noinst_HEADERS = align.h approx_math.h atomic.h cycle.h error.h inline.h kernel_hydro.h kernel_gravity.h \
@@ -82,7 +82,7 @@ nobase_noinst_HEADERS = align.h approx_math.h atomic.h cycle.h error.h inline.h
 		 cooling/none/cooling.h cooling/none/cooling_struct.h \
 	         cooling/const_du/cooling.h cooling/const_du/cooling_struct.h \
                  cooling/const_lambda/cooling.h cooling/const_lambda/cooling_struct.h \
-     memswap.h dump.h
+     memswap.h dump.h logger.h
 
 
 # Sources and flags for regular library
diff --git a/src/logger.c b/src/logger.c
new file mode 100644
index 0000000000..cb2be06870
--- /dev/null
+++ b/src/logger.c
@@ -0,0 +1,86 @@
+/*******************************************************************************
+ * This file is part of SWIFT.
+ * Copyright (c) 2017 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ ******************************************************************************/
+
+/* Config parameters. */
+#include "../config.h"
+
+/* Some standard headers. */
+#include <float.h>
+#include <limits.h>
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* This object's header. */
+#include "logger.h"
+
+/* Local headers. */
+#include "atomic.h"
+#include "dump.h"
+#include "error.h"
+#include "part.h"
+
+/**
+ * @brief Compute the size of a message given its mask.
+ */
+
+int logger_size(unsigned int mask) {
+
+  /* Start with 8 bytes for the header. */
+  int size = 8;
+
+  /* Is this a particle or a timestep? */
+  if (mask & logger_mask_timestamp) {
+
+    /* The timestamp should not contain any other bits. */
+    if (mask != logger_mask_timestamp)
+      error("Timestamps should not include any other data.");
+
+    /* A timestamp consists of an unsigned long long int. */
+    size += sizeof(unsigned long long int);
+
+  } else {
+
+    /* Particle position as three doubles. */
+    if (mask & logger_mask_x) size += 3 * sizeof(double);
+
+    /* Particle velocity as three floats. */
+    if (mask & logger_mask_v) size += 3 * sizeof(float);
+
+    /* Particle accelleration as three floats. */
+    if (mask & logger_mask_a) size += 3 * sizeof(float);
+
+    /* Particle internal energy as a single float. */
+    if (mask & logger_mask_u) size += sizeof(float);
+
+    /* Particle smoothing length as a single float. */
+    if (mask & logger_mask_h) size += sizeof(float);
+
+    /* Particle density as a single float. */
+    if (mask & logger_mask_rho) size += sizeof(float);
+
+    /* Particle constants, which is a bit more complicated. */
+    if (mask & logger_mask_rho) {
+      size += sizeof(float) +     // mass
+              sizeof(long long);  // id
+    }
+  }
+
+  return size;
+}
diff --git a/src/logger.h b/src/logger.h
new file mode 100644
index 0000000000..45540824cb
--- /dev/null
+++ b/src/logger.h
@@ -0,0 +1,78 @@
+/*******************************************************************************
+ * This file is part of SWIFT.
+ * Copyright (c) 2017 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ ******************************************************************************/
+#ifndef SWIFT_LOGGER_H
+#define SWIFT_LOGGER_H
+
+/* Includes. */
+#include "dump.h"
+#include "part.h"
+
+/**
+ * Logger entries contain messages representing the particle data at a given
+ * point in time during the simulation. 
+ *
+ * The logger messages always start with an 8-byte header structured as
+ * follows:
+ *
+ *   data: [ mask |                     offset                     ]
+ *   byte: [  01  |  02  |  03  |  04  |  05  |  06  |  07  |  08  ]
+ *
+ * I.e. a first "mask" byte followed by 7 "offset" bytes. The mask contains
+ * information on what kind of data is packed after the header. The mask
+ * bits correspond to the following data:
+ *
+ *   bit | name   | size | comment
+ *   -------------------------------------------------------------------------
+ *   0   | x      | 24   | The particle position, in absolute coordinates,
+ *       |        |      | stored as three doubles.
+ *   1   | v      | 12   | Particle velocity, stored as three floats.
+ *   2   | a      | 12   | Particle acceleration, stored as three floats.
+ *   3   | u      | 4    | Particle internal energy, stored as a single float.
+ *   4   | h      | 4    | Particle smoothing length, stored as a single float.
+ *   5   | rho    | 4    | Particle density, stored as a single float.
+ *   6   | consts | ???  | Particle constants, e.g. mass, ID, etc...
+ *   7   | ts     | 8    | Timestamp, not associated with a particle, just
+ *       |        |      | marks the transitions from one timestep to another.
+ *
+ * There is no distinction between gravity and SPH particles.
+ *
+ * The offset refers to the relative location of the previous message for the
+ * same particle or for the previous timestamp (if mask bit 7 is set). I.e.
+ * the previous log entry will be at the address of the current mask byte minus
+ * the unsigned value stored in the offset. An offset of zero indicates that
+ * this is the first message for the given particle/timestamp.
+ */
+
+/* Some constants. */
+#define logger_mask_x 1
+#define logger_mask_v 2
+#define logger_mask_a 4
+#define logger_mask_u 8
+#define logger_mask_h 16
+#define logger_mask_rho 32
+#define logger_mask_consts 64
+#define logger_mask_timestamp 128
+
+/* Function prototypes. */
+int logger_size(unsigned int mask);
+void logger_log_part(struct part *p, struct xpart *xp, unsigned int mask, struct dump *dump);
+void logger_log_gpart(struct gpart *p, struct xpart *xp, unsigned int mask, struct dump *dump);
+void logger_lot_timestamp(unsigned long long int timestamp, struct dump *dump);
+
+#endif /* SWIFT_LOGGER_H */
-- 
GitLab