Index: src/Actions/FragmentationAction/FitParticleChargesAction.cpp
===================================================================
--- src/Actions/FragmentationAction/FitParticleChargesAction.cpp	(revision c4a32355115c344281807a20f6f7992f85eba727)
+++ src/Actions/FragmentationAction/FitParticleChargesAction.cpp	(revision c4a32355115c344281807a20f6f7992f85eba727)
@@ -0,0 +1,172 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * FitParticleChargesAction.cpp
+ *
+ *  Created on: Jul 03, 2013
+ *      Author: heber
+ */
+
+// include config.h
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+// needs to come before MemDebug due to placement new
+#include <boost/archive/text_iarchive.hpp>
+
+#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 <boost/filesystem.hpp>
+#include <boost/foreach.hpp>
+#include <algorithm>
+#include <iostream>
+#include <string>
+
+#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<size_t>(countiter->first),
+          static_cast<size_t>(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<const element *> &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 ====================== */
Index: src/Actions/FragmentationAction/FitParticleChargesAction.def
===================================================================
--- src/Actions/FragmentationAction/FitParticleChargesAction.def	(revision c4a32355115c344281807a20f6f7992f85eba727)
+++ src/Actions/FragmentationAction/FitParticleChargesAction.def	(revision c4a32355115c344281807a20f6f7992f85eba727)
@@ -0,0 +1,44 @@
+/*
+ * FitParticleChargesAction.def
+ *
+ *  Created on: Jul 03, 2013
+ *      Author: heber
+ */
+
+// all includes and forward declarations necessary for non-integral types below
+#include <boost/filesystem/path.hpp>
+#include "Element/element.hpp"
+#include <vector>
+
+#include "Parameters/Validators/DummyValidator.hpp"
+#include "Parameters/Validators/GenericValidators.hpp"
+#include "Parameters/Validators/STLVectorValidator.hpp"
+#include "Parameters/Validators/Specific/ElementValidator.hpp"
+
+// i.e. there is an integer with variable name Z that can be found in
+// ValueStorage by the token "Z" -> first column: int, Z, "Z"
+// "undefine" if no parameters are required, use (NOPARAM_DEFAULT) for each (undefined) default value
+#define paramtypes (boost::filesystem::path)(std::vector<const element *>)(double)
+#define paramtokens ("potential-file")("fragment-charges")("radius")
+#define paramdescriptions ("potential file specifying multiple potentials to fit")("charges specifying the fragment")("radius of sphere around nuclei where potential does not need to match")
+#define paramdefaults (NOPARAM_DEFAULT)(NOPARAM_DEFAULT)(PARAM_DEFAULT(0))
+#define paramreferences (potential_file)(fragment)(radius)
+#define paramvalids \
+(DummyValidator<boost::filesystem::path>()) \
+(STLVectorValidator< std::vector<const element *> >(1,99, ElementValidator())) \
+(NonNegativeValidator<double>())
+
+#undef statetypes
+#undef statereferences
+
+// some defines for all the names, you may use ACTION, STATE and PARAMS
+#define CATEGORY Fragmentation
+#define MENUNAME "fragmentation"
+#define MENUPOSITION 9
+#define ACTIONNAME FitParticleCharges
+#define TOKEN "fit-particle-charges"
+
+
+// finally the information stored in the ActionTrait specialization
+#define DESCRIPTION "fits partial nuclear charges to present particle types from long-range fragmentation results"
+#undef SHORTFORM
Index: src/Actions/FragmentationAction/FitParticleChargesAction.hpp
===================================================================
--- src/Actions/FragmentationAction/FitParticleChargesAction.hpp	(revision c4a32355115c344281807a20f6f7992f85eba727)
+++ src/Actions/FragmentationAction/FitParticleChargesAction.hpp	(revision c4a32355115c344281807a20f6f7992f85eba727)
@@ -0,0 +1,22 @@
+/*
+ * FitParticleChargesAction.hpp
+ *
+ *  Created on: Jul 03, 2013
+ *      Author: heber
+ */
+
+#ifndef FITPARTICLECHARGESACTION_HPP_
+#define FITPARTICLECHARGESACTION_HPP_
+
+// include config.h
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+
+#include "Actions/Action.hpp"
+
+#include "FitParticleChargesAction.def"
+#include "Action_impl_header.hpp"
+
+#endif /* FITPARTICLECHARGESACTION_HPP_ */
Index: src/Actions/GlobalListOfActions.hpp
===================================================================
--- src/Actions/GlobalListOfActions.hpp	(revision e355b3198d825bb9c375781f753efe001a9585b8)
+++ src/Actions/GlobalListOfActions.hpp	(revision c4a32355115c344281807a20f6f7992f85eba727)
@@ -123,4 +123,5 @@
   (SelectionNotShapeByName) \
   (FragmentationAnalyseFragmentationResults) \
+  (FragmentationFitParticleCharges) \
   (FragmentationFragmentation) \
   (FragmentationParseHomologies) \
Index: src/Actions/Makefile.am
===================================================================
--- src/Actions/Makefile.am	(revision e355b3198d825bb9c375781f753efe001a9585b8)
+++ src/Actions/Makefile.am	(revision c4a32355115c344281807a20f6f7992f85eba727)
@@ -214,4 +214,5 @@
 FRAGMENTATIONACTIONSOURCE = \
   Actions/FragmentationAction/AnalyseFragmentationResultsAction.cpp \
+  Actions/FragmentationAction/FitParticleChargesAction.cpp \
   Actions/FragmentationAction/FragmentationAction.cpp \
   Actions/FragmentationAction/ParseHomologiesAction.cpp \
@@ -220,4 +221,5 @@
 FRAGMENTATIONACTIONHEADER = \
   Actions/FragmentationAction/AnalyseFragmentationResultsAction.hpp \
+  Actions/FragmentationAction/FitParticleChargesAction.hpp \
   Actions/FragmentationAction/FragmentationAction.hpp \
   Actions/FragmentationAction/ParseHomologiesAction.hpp \
@@ -226,4 +228,5 @@
 FRAGMENTATIONACTIONDEFS = \
   Actions/FragmentationAction/AnalyseFragmentationResultsAction.def \
+  Actions/FragmentationAction/FitParticleChargesAction.def  \
   Actions/FragmentationAction/FragmentationAction.def \
   Actions/FragmentationAction/ParseHomologiesAction.def \
