Index: src/Jobs/Grid/SamplingGrid.cpp
===================================================================
--- src/Jobs/Grid/SamplingGrid.cpp	(revision de6dfb38ac7746ff442a0ddb707c6faa2fdcb721)
+++ src/Jobs/Grid/SamplingGrid.cpp	(revision c6355f2a2d0b8d1942a82247a03b8c3193315bb1)
@@ -60,4 +60,14 @@
 SamplingGrid::SamplingGrid(const double _begin[3],
     const double _end[3],
+    const int _level) :
+  SamplingGridProperties(_begin, _end, _level)
+{
+  setWindowSize(zeroOffset, zeroOffset);
+  ASSERT( getWindowGridPoints() == (size_t)0,
+      "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
+}
+
+SamplingGrid::SamplingGrid(const double _begin[3],
+    const double _end[3],
     const int _level,
     const sampledvalues_t &_sampled_grid) :
@@ -74,5 +84,7 @@
   sampled_grid(_grid.sampled_grid)
 {
-  setWindowSize(_grid.begin, _grid.end);
+  setWindowSize(_grid.begin_window, _grid.end_window);
+  ASSERT( getWindowGridPoints() == _grid.getWindowGridPoints(),
+      "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
 }
 
@@ -80,5 +92,7 @@
   SamplingGridProperties(_props)
 {
-  setWindowSize(_props.begin, _props.end);
+  setWindowSize(zeroOffset, zeroOffset);
+  ASSERT( getWindowGridPoints() == (size_t)0,
+      "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
 }
 
@@ -241,4 +255,5 @@
   setWindowSize(_begin_window, _end_window);
   const size_t gridpoints_window = getWindowGridPoints();
+  sampled_grid.clear();
   sampled_grid.resize(gridpoints_window, 0.);
 }
@@ -268,7 +283,7 @@
     const double _end_window[3])
 {
-  // check that we truly have to extend the window
 #ifndef NDEBUG
   for(size_t index=0;index < 3; ++index) {
+    // check that we truly have to extend the window
     ASSERT ( begin_window[index] >= _begin_window[index],
         "SamplingGrid::extendWindow() - component "+toString(index)+
@@ -277,4 +292,12 @@
         "SamplingGrid::extendWindow() - component "+toString(index)+
         " of window end is less than old value.");
+
+    // check that we are still less than domain
+    ASSERT ( _begin_window[index] >= begin[index],
+        "SamplingGrid::extendWindow() - component "+toString(index)+
+        " of window start is less than domain start.");
+    ASSERT ( _end_window[index] <= end[index],
+        "SamplingGrid::extendWindow() - component "+toString(index)+
+        " of window end is greater than domain end.");
   }
 #endif
@@ -311,5 +334,5 @@
 #endif
   // the only issue are indices
-  const size_t gridpoints_axis = pow(2, level);
+  const size_t gridpoints_axis = getGridPointsPerAxis();
   size_t pre_offset[3];
   size_t post_offset[3];
@@ -318,16 +341,26 @@
   for(size_t index=0;index<3;++index) {
     pre_offset[index] = (_begin_window[index] - begin_window[index])*gridpoints_axis;
+    length[index] = (_end_window[index] - _begin_window[index])*gridpoints_axis;
     post_offset[index] = (end_window[index] - _end_window[index])*gridpoints_axis;
-    length[index] = (_end_window[index] - _begin_window[index])*gridpoints_axis;
     total[index] = (end_window[index] - begin_window[index])*gridpoints_axis;
     ASSERT( pre_offset[index]+post_offset[index]+length[index] == total[index],
-        "SamplingGrid::addOntoWindow() - pre, past, and length don't sum up to total for "
+        "SamplingGrid::addOntoWindow() - pre, post, and length don't sum up to total for "
         +toString(index)+"th component.");
   }
-  ASSERT( length[0]*length[1]*length[2] ==  _sampled_grid.size(),
-      "SamplingGrid::addOntoWindow() - not enough sampled values given.");
-  ASSERT( length[0]*length[1]*length[2] <=  sampled_grid.size(),
-      "SamplingGrid::addOntoWindow() - not enough sampled values available.");
+#ifndef NDEBUG
+  const size_t calculated_size = length[0]*length[1]*length[2];
+  ASSERT( calculated_size == _sampled_grid.size(),
+      "SamplingGrid::addOntoWindow() - not enough sampled values given: "
+      +toString(calculated_size)+" != "+toString(_sampled_grid.size())+".");
+  ASSERT( calculated_size <=  sampled_grid.size(),
+      "SamplingGrid::addOntoWindow() - not enough sampled values available: "
+      +toString(calculated_size)+" <= "+toString(sampled_grid.size())+".");
+  const size_t total_size = total[0]*total[1]*total[2];
+  ASSERT( total_size == sampled_grid.size(),
+      "SamplingGrid::addOntoWindow() - total size is not equal to number of present points: "
+      +toString(total_size)+" != "+toString(sampled_grid.size())+".");
+#endif
   size_t N[3];
+  size_t counter = 0;
   sampledvalues_t::iterator griditer = sampled_grid.begin();
   std::advance(griditer, pre_offset[0]*total[1]*total[2]);
@@ -338,4 +371,8 @@
       std::advance(griditer, pre_offset[2]);
       for(N[2]=0; N[2] < length[2]; ++N[2]) {
+        ASSERT( griditer != sampled_grid.end(),
+            "SamplingGrid::addOntoWindow() - griditer is already at end of window.");
+        ASSERT( copyiter != _sampled_grid.end(),
+            "SamplingGrid::addOntoWindow() - griditer is already at end of window.");
         *griditer++ += prefactor*(*copyiter++);
       }
@@ -344,5 +381,11 @@
     std::advance(griditer, post_offset[1]*total[2]);
   }
-  //std::advance(griditer, post_offset[0]*total[1]*total[2]);
+#ifndef NDEBUG
+  std::advance(griditer, post_offset[0]*total[1]*total[2]);
+  ASSERT( griditer == sampled_grid.end(),
+      "SamplingGrid::addOntoWindow() - griditer is not at end of window.");
+  ASSERT( copyiter == _sampled_grid.end(),
+      "SamplingGrid::addOntoWindow() - copyiter is not at end of window.");
+#endif
   LOG(2, "DEBUG: Grid after adding other is " << sampled_grid << ".");
 }
