/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2012 University of Bonn. All rights reserved.
 * Copyright (C)  2013 Frederik Heber. All rights reserved.
 * Please see the COPYING file or "Copyright notice" in builder.cpp for details.
 * 
 *
 *   This file is part of MoleCuilder.
 *
 *    MoleCuilder is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    MoleCuilder 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 General Public License
 *    along with MoleCuilder.  If not, see . 
 */
/*
 * WindowGrid_converter.cpp
 *
 *  Created on: Dec 20, 2012
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "grid/grid.hpp"
#include "WindowGrid_converter.hpp"
//#include "CodePatterns/MemDebug.hpp"
#include 
#include 
#include 
#include "CodePatterns/Assert.hpp"
#include "CodePatterns/Log.hpp"
#include "Fragmentation/Summation/SetValues/SamplingGrid.hpp"
#include "Jobs/ChargeSmearer.hpp"
void WindowGrid_converter::addGridOntoWindow(
    VMG::Grid &grid,
    SamplingGrid &window,
    const double prefactor,
    const bool OpenBoundaryConditions)
{
#ifndef NDEBUG
  for(size_t index=0;index= window.begin_window[index],
        "InterfaceVMGJob::addGridOntoWindow() - given grid starts earlier than window in component "
        +toString(index)+".");
    ASSERT( window.end[index] <= window.end_window[index],
        "InterfaceVMGJob::addGridOntoWindow() - given grid ends later than window in component "
        +toString(index)+".");
  }
#endif
  // the only issue are indices
  size_t pre_offset[NDIM];
  size_t post_offset[NDIM];
  size_t length[NDIM];
  size_t total[NDIM];
  window.getDiscreteWindowOffsets(pre_offset, post_offset, length, total);
#ifndef NDEBUG
  const size_t calculated_size = length[0]*length[1]*length[2];
  ASSERT( calculated_size <=  window.sampled_grid.size(),
      "InterfaceVMGJob::addGridOntoWindow() - not enough sampled values available: "
      +toString(calculated_size)+" <= "+toString(window.sampled_grid.size())+".");
  const size_t total_size = total[0]*total[1]*total[2];
  ASSERT( total_size == window.sampled_grid.size(),
      "InterfaceVMGJob::addGridOntoWindow() - total size is not equal to number of present points: "
      +toString(total_size)+" != "+toString(window.sampled_grid.size())+".");
#endif
  size_t N[NDIM];
  SamplingGrid::sampledvalues_t::iterator griditer = window.sampled_grid.begin();
  std::advance(griditer, pre_offset[0]*total[1]*total[2]);
  VMG::Grid::iterator copyiter = grid.Iterators().Local().Begin();
  const VMG::Index size = grid.Local().Size();
  if (OpenBoundaryConditions)
    for(N[0]=(size_t)0; N[0] < (size_t)size[0]/(size_t)4; ++N[0])
      for(N[1]=(size_t)0; N[1] < (size_t)size[1]; ++N[1])
        for(N[2]=(size_t)0; N[2] < (size_t)size[2]; ++N[2]) {
          ASSERT( copyiter != grid.Iterators().Local().End(),
              "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window.");
          ++copyiter;
        }
  for(N[0]=(size_t)0; N[0] < length[0]; ++N[0]) {
    std::advance(griditer, pre_offset[1]*total[2]);
    if (OpenBoundaryConditions)
      for(N[1]=(size_t)0; N[1] < (size_t)size[1]/(size_t)4; ++N[1])
        for(N[2]=(size_t)0; N[2] < (size_t)size[2]; ++N[2]) {
          ASSERT( copyiter != grid.Iterators().Local().End(),
              "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window.");
          ++copyiter;
        }
    for(N[1]=(size_t)0; N[1] < length[1]; ++N[1]) {
      std::advance(griditer, pre_offset[2]);
      if (OpenBoundaryConditions)
        for(N[2]=(size_t)0; N[2] < (size_t)size[2]/(size_t)4; ++N[2]) {
          ASSERT( copyiter != grid.Iterators().Local().End(),
              "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window.");
          ++copyiter;
        }
      for(N[2]=(size_t)0; N[2] < length[2]; ++N[2]) {
        ASSERT( griditer != window.sampled_grid.end(),
            "InterfaceVMGJob::addGridOntoWindow() - griditer is already at end of window.");
        ASSERT( copyiter != grid.Iterators().Local().End(),
            "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window.");
        *griditer++ += prefactor*grid(*copyiter++);
      }
      std::advance(griditer, post_offset[2]);
      if (OpenBoundaryConditions)
        for(N[2]=(size_t)0; N[2] < (size_t)size[2] - (size_t)size[2]/(size_t)4 - length[2]; ++N[2]) {
          ASSERT( copyiter != grid.Iterators().Local().End(),
              "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window.");
          ++copyiter;
        }
    }
    std::advance(griditer, post_offset[1]*total[2]);
    if (OpenBoundaryConditions)
      for(N[1]=(size_t)0; N[1] < (size_t)size[1] - (size_t)size[1]/(size_t)4 - length[1]; ++N[1])
        for(N[2]=(size_t)0; N[2] < (size_t)size[2]; ++N[2]) {
          ASSERT( copyiter != grid.Iterators().Local().End(),
              "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window.");
          ++copyiter;
        }
  }
  if (OpenBoundaryConditions)
    for(N[0]=(size_t)0; N[0] < (size_t)size[0] - (size_t)size[0]/(size_t)4 - length[0]; ++N[0])
      for(N[1]=(size_t)0; N[1] < (size_t)size[1]; ++N[1])
        for(N[2]=(size_t)0; N[2] < (size_t)size[2]; ++N[2]) {
          ASSERT( copyiter != grid.Iterators().Local().End(),
              "InterfaceVMGJob::addGridOntoWindow() - copyiter is already at end of window.");
          ++copyiter;
        }
#ifndef NDEBUG
  std::advance(griditer, post_offset[0]*total[1]*total[2]);
  ASSERT( griditer == window.sampled_grid.end(),
      "InterfaceVMGJob::addGridOntoWindow() - griditer is not at end of window.");
  ASSERT( copyiter == grid.Iterators().Local().End(),
      "InterfaceVMGJob::addGridOntoWindow() - copyiter is not at end of window.");
#endif
}
void WindowGrid_converter::addWindowOntoGrid(
    VMG::Grid& window,
    const SamplingGrid &grid,
    const double prefactor,
    const bool OpenBoundaryConditions,
    const bool DoSmearCharges)
{
#ifndef NDEBUG
  for(size_t index=0;index= grid.begin[index],
        "InterfaceVMGJob::addWindowOntoGrid() - given window starts earlier than grid in component "
        +toString(index)+".");
    ASSERT( grid.end_window[index] <= grid.end[index],
        "InterfaceVMGJob::addWindowOntoGrid() - given window ends later than grid in component "
        +toString(index)+".");
  }
#endif
  // the only issue are indices
  size_t pre_offset[NDIM];
  size_t post_offset[NDIM];
  size_t length[NDIM];
  size_t total[NDIM];
  grid.getDiscreteWindowOffsets(pre_offset, post_offset, length, total);
#ifndef NDEBUG
  const size_t calculated_size = length[0]*length[1]*length[2];
  ASSERT( calculated_size == grid.sampled_grid.size(),
      "InterfaceVMGJob::addWindowOntoGrid() - not enough sampled values given: "
      +toString(calculated_size)+" != "+toString(grid.sampled_grid.size())+".");
#endif
  size_t N[NDIM];
  // in open boundary case grid.Local() contains more than just the inner area. The grid
  // is actually twice as large to allow for the FV discretization to work. Hence, we first
  // have to seek our starting point ... see VMG::Grid::GetSpatialPos()
  if (OpenBoundaryConditions) {
    const VMG::Index size = window.Local().Size();
//    const VMG::Index boundary1size = window.Local().BoundarySize1();
//    const VMG::Index boundary2size = window.Local().BoundarySize2();
//    const VMG::Index halo1size = window.Local().HaloSize1();
//    const VMG::Index halo2size = window.Local().HaloSize2();
    // this mimicks VMG::GridIndexTranslations::EndOffset()
    const size_t off = OpenBoundaryConditions ? 1 : 0;
    for (size_t i=0;i