/*
 *    vmg - a versatile multigrid solver
 *    Copyright (C) 2012 Institute for Numerical Simulation, University of Bonn
 *
 *  vmg 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  vmg 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 this program.  If not, see .
 */
/**
 * @file   discretization_poisson_fv.cpp
 * @author Julian Iseringhausen 
 * @date   Mon Apr 18 13:03:47 2011
 *
 * @brief  Finite volume discretization for the Poisson
 *         equation. Absolutely equivalent to the finite
 *         difference discretization unless you use
 *         hierarchically coarsened grids.
 *
 */
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "comm/comm.hpp"
#include "discretization/discretization_poisson_fv.hpp"
#include "grid/grid_index_translations.hpp"
using namespace VMG;
void DiscretizationPoissonFV::InitDiscretizationPoissonFV()
{
  switch (order)
  {
  case 2:
    stencil.SetDiag(6.0);
    stencil.push_back(-1,  0,  0, -1.0);
    stencil.push_back( 1,  0,  0, -1.0);
    stencil.push_back( 0, -1,  0, -1.0);
    stencil.push_back( 0,  1,  0, -1.0);
    stencil.push_back( 0,  0, -1, -1.0);
    stencil.push_back( 0,  0,  1, -1.0);
    break;
  case 4:
    stencil.SetDiag(24.0/6.0);
    stencil.push_back(-1,  0,  0, -2.0/6.0);
    stencil.push_back( 1,  0,  0, -2.0/6.0);
    stencil.push_back( 0, -1,  0, -2.0/6.0);
    stencil.push_back( 0,  1,  0, -2.0/6.0);
    stencil.push_back( 0,  0, -1, -2.0/6.0);
    stencil.push_back( 0,  0,  1, -2.0/6.0);
    stencil.push_back(-1, -1,  0, -1.0/6.0);
    stencil.push_back(-1,  1,  0, -1.0/6.0);
    stencil.push_back( 1, -1,  0, -1.0/6.0);
    stencil.push_back( 1,  1,  0, -1.0/6.0);
    stencil.push_back(-1,  0, -1, -1.0/6.0);
    stencil.push_back(-1,  0,  1, -1.0/6.0);
    stencil.push_back( 1,  0, -1, -1.0/6.0);
    stencil.push_back( 1,  0,  1, -1.0/6.0);
    stencil.push_back( 0, -1, -1, -1.0/6.0);
    stencil.push_back( 0, -1,  1, -1.0/6.0);
    stencil.push_back( 0,  1, -1, -1.0/6.0);
    stencil.push_back( 0,  1,  1, -1.0/6.0);
    break;
  default:
    assert(0 != "vmg choose discretization order 2 or 4");
    break;
  }
}
void DiscretizationPoissonFV::ModifyRightHandSide()
{
  if (order == 4) {
    Grid& rhs = MG::GetRhsMaxLevel();
    Stencil stencil(6.0/12.0);
    stencil.push_back(-1,  0,  0, 1.0/12.0);
    stencil.push_back( 1,  0,  0, 1.0/12.0);
    stencil.push_back( 0, -1,  0, 1.0/12.0);
    stencil.push_back( 0,  1,  0, 1.0/12.0);
    stencil.push_back( 0,  0, -1, 1.0/12.0);
    stencil.push_back( 0,  0,  1, 1.0/12.0);
    stencil.Apply(rhs);
  }
}
void DiscretizationPoissonFV::SetInnerBoundaryCompute(Grid& sol_f, Grid& rhs_f, Grid& sol_c) const
{
  Index i_c, i_f;
  Comm& comm = *MG::GetComm();
  const Boundary& bc = comm.BoundaryConditions();
  const Index off((GridIndexTranslations::LocalToGlobal(sol_f, sol_f.Local().Begin())[0] % 2 == 0 ? 0 : 1),
                  (GridIndexTranslations::LocalToGlobal(sol_f, sol_f.Local().Begin())[1] % 2 == 0 ? 0 : 1),
                  (GridIndexTranslations::LocalToGlobal(sol_f, sol_f.Local().Begin())[2] % 2 == 0 ? 0 : 1));
  const Index begin_f = sol_f.Local().Begin() - off;
  const Index end_f = sol_f.Local().End();
  const Index begin_c = GridIndexTranslations::GlobalFinestToLocal(sol_c, GridIndexTranslations::LocalToGlobalFinest(sol_f, begin_f));
  const Index b1_f = sol_f.Local().BoundaryBegin1();
  const Index b2_f = sol_f.Local().BoundaryBegin2();
  const Index b1_finest = GridIndexTranslations::LocalToGlobalFinest(sol_f, sol_f.Local().Begin() + off);
  const Index b2_finest = GridIndexTranslations::LocalToGlobalFinest(sol_f, sol_f.Local().End()-1);
  const Index b1_c = GridIndexTranslations::GlobalFinestToLocal(sol_c, b1_finest) - 1;
  const Index b2_c = GridIndexTranslations::GlobalFinestToLocal(sol_c, b2_finest) + 1;
  const vmg_float c_1_3 = 1.0 / 3.0;
  const vmg_float c_2_3 = 2.0 / 3.0;
  const vmg_float c_4_3 = 4.0 / 3.0;
  comm.CommToGhosts(sol_f);
  comm.CommToGhosts(sol_c);
  //
  // X-direction
  //
  if (bc.X() == Open) {
    if (sol_f.Local().BoundarySize1().X() > 0) {
      for (i_f.Y()=begin_f.Y(), i_c.Y()=begin_c.Y(); i_f.Y() 0) {
      for (i_f.Y()=begin_f.Y(), i_c.Y()=begin_c.Y(); i_f.Y() 0) {
      for (i_f.X()=begin_f.X(), i_c.X()=begin_c.X(); i_f.X() 0) {
      for (i_f.X()=begin_f.X(), i_c.X()=begin_c.X(); i_f.X() 0) {
      for (i_f.X()=begin_f.X(), i_c.X()=begin_c.X(); i_f.X() 0) {
      for (i_f.X()=begin_f.X(), i_c.X()=begin_c.X(); i_f.X()