/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2013 Frederik Heber. All rights reserved.
 * 
 *
 *   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 .
 */
/*
 * FitParticleChargesAction.cpp
 *
 *  Created on: Jul 03, 2013
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
// needs to come before MemDebug due to placement new
#include 
#include "CodePatterns/MemDebug.hpp"
#include "Atom/atom.hpp"
#include "CodePatterns/Log.hpp"
#include "Fragmentation/Exporters/ExportGraph_ToFiles.hpp"
#include "Fragmentation/Graph.hpp"
#include "World.hpp"
#include 
#include 
#include 
#include 
#include 
#include "Actions/FragmentationAction/FitParticleChargesAction.hpp"
#include "Potentials/PartialNucleiChargeFitter.hpp"
#include "Element/element.hpp"
#include "Fragmentation/Homology/HomologyContainer.hpp"
#include "Fragmentation/Homology/HomologyGraph.hpp"
#include "Fragmentation/Summation/SetValues/SamplingGrid.hpp"
#include "FunctionApproximation/Extractors.hpp"
#include "Potentials/PartialNucleiChargeFitter.hpp"
#include "Potentials/SerializablePotential.hpp"
#include "World.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "FitParticleChargesAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
inline
HomologyGraph getFirstGraphwithSpecifiedElements(
    const HomologyContainer &homologies,
    const SerializablePotential::ParticleTypes_t &types)
{
  ASSERT( !types.empty(),
      "getFirstGraphwithSpecifiedElements() - charges is empty?");
  // create charges
  Fragment::charges_t charges;
  charges.resize(types.size());
  std::transform(types.begin(), types.end(),
      charges.begin(), boost::lambda::_1);
  // convert into count map
  Extractors::elementcounts_t counts_per_charge =
      Extractors::_detail::getElementCounts(charges);
  ASSERT( !counts_per_charge.empty(),
      "getFirstGraphwithSpecifiedElements() - charge counts are empty?");
  LOG(2, "DEBUG: counts_per_charge is " << counts_per_charge << ".");
  // we want to check each (unique) key only once
  HomologyContainer::const_key_iterator olditer = homologies.key_end();
  for (HomologyContainer::const_key_iterator iter =
      homologies.key_begin(); iter != homologies.key_end(); olditer = iter++) {
    // if it's the same as the old one, skip it
    if (*olditer == *iter)
      continue;
    // if it's a new key, check if every element has the right number of counts
    Extractors::elementcounts_t::const_iterator countiter = counts_per_charge.begin();
    for (; countiter != counts_per_charge.end(); ++countiter)
      if (!(*iter).hasTimesAtomicNumber(
          static_cast(countiter->first),
          static_cast(countiter->second))
          )
        break;
    if( countiter == counts_per_charge.end())
      return *iter;
  }
  return HomologyGraph();
}
Action::state_ptr FragmentationFitParticleChargesAction::performCall() {
  // fragment specifies the homology fragment to use
  SerializablePotential::ParticleTypes_t fragmentnumbers;
  {
    const std::vector &fragment = params.fragment.get();
    std::transform(fragment.begin(), fragment.end(), std::back_inserter(fragmentnumbers),
        boost::bind(&element::getAtomicNumber, _1));
  }
  // parse homologies into container
  HomologyContainer &homologies = World::getInstance().getHomologies();
  const HomologyGraph graph = getFirstGraphwithSpecifiedElements(homologies,fragmentnumbers);
  HomologyContainer::range_t range = homologies.getHomologousGraphs(graph);
  // for the moment just use the very first fragment
  if (range.first == range.second) {
    ELOG(1, "HomologyContainer does not contain specified fragment.");
    return Action::failure;
  }
  HomologyContainer::const_iterator iter = range.first;
  if (!iter->second.containsGrids) {
    ELOG(2, "This HomologyGraph does not contain sampled grids.");
    return Action::failure;
  }
  const Fragment &fragment = iter->second.fragment;
//  const double &energy = iter->second.energy;
//  const SamplingGrid &charge = iter->second.charge_distribution;
  const SamplingGrid &potential = iter->second.potential_distribution;
  // then we extract positions from fragment
  PartialNucleiChargeFitter::positions_t positions;
  Fragment::positions_t fragmentpositions = fragment.getPositions();
  positions.reserve(fragmentpositions.size());
  BOOST_FOREACH( Fragment::position_t pos, fragmentpositions) {
    positions.push_back( Vector(pos[0], pos[1], pos[2]) );
  }
  PartialNucleiChargeFitter fitter(potential, positions, params.radius.get());
  fitter();
  PartialNucleiChargeFitter::charges_t return_charges = fitter.getSolutionAsCharges_t();
  // output fitted charges
  LOG(0, "STATUS: We have fitted the following charges " << return_charges << ".");
  return Action::success;
}
Action::state_ptr FragmentationFitParticleChargesAction::performUndo(Action::state_ptr _state) {
  return Action::success;
}
Action::state_ptr FragmentationFitParticleChargesAction::performRedo(Action::state_ptr _state){
  return Action::success;
}
bool FragmentationFitParticleChargesAction::canUndo() {
  return false;
}
bool FragmentationFitParticleChargesAction::shouldUndo() {
  return false;
}
/** =========== end of function ====================== */