From 0a526dd7fc2138b54c04ca13efc3f05ba4f43fd8 Mon Sep 17 00:00:00 2001
From: Pedro Gonnet <gonnet@google.com>
Date: Tue, 5 May 2015 23:09:26 +0200
Subject: [PATCH] check particle position sanity when initializing the space,
 this avoids problems downstream.

Former-commit-id: 5b078c03983e09461e12729551919f2cc8faa404
---
 src/space.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/src/space.c b/src/space.c
index 09667d9496..89acf06034 100644
--- a/src/space.c
+++ b/src/space.c
@@ -1158,6 +1158,21 @@ void space_init ( struct space *s , double dim[3] , struct part *parts , int N ,
     s->nr_queues = 1;
     s->size_parts_foreign = 0;
     
+    /* Check that all the particle positions are reasonable, wrap if periodic. */
+    if ( periodic ) {
+      for ( int k = 0 ; k < N ; k++ )
+        for ( int j = 0 ; j < 3 ; j++ ) {
+          while ( parts[k].x[j] < 0 ) parts[k].x[j] += dim[j];
+          while ( parts[k].x[j] >= dim[j] ) parts[k].x[j] -= dim[j];
+          }
+      }
+    else {
+      for ( int k = 0 ; k < N ; k++ )
+        for ( int j = 0 ; j < 3 ; j++ )
+          if ( parts[k].x[j] < 0 || parts[k].x[j] >= dim[j] )
+            error( "Not all particles are within the specified domain." );
+      }
+    
     /* Allocate the xtra parts array. */
     if ( posix_memalign( (void *)&s->xparts , 32 , N * sizeof(struct xpart) ) != 0 )
         error( "Failed to allocate xparts." );
-- 
GitLab