Index: src/FunctionApproximation/FunctionArgument.hpp
===================================================================
--- src/FunctionApproximation/FunctionArgument.hpp	(revision bc55c96be4a2187ff0a65ac84ba020d9a58c4900)
+++ src/FunctionApproximation/FunctionArgument.hpp	(revision eb1efe719c9062ecc3dbe78c9ad1eefbf84a5023)
@@ -38,5 +38,6 @@
   argument_t() :
     indices( std::make_pair(0,1) ),
-    distance(0.)
+    distance(0.),
+    globalid(-1)
   {}
 
@@ -49,5 +50,6 @@
   argument_t(const double &_distance) :
     indices( std::make_pair(0,1) ),
-    distance(_distance)
+    distance(_distance),
+    globalid(-1)
   {}
 
@@ -59,5 +61,6 @@
   argument_t(const indices_t &_indices, const double &_distance) :
     indices( _indices ),
-    distance(_distance)
+    distance(_distance),
+    globalid(-1)
   {}
 
@@ -66,4 +69,6 @@
   //!> distance
   double distance;
+  //!> global id refers to some global index, e.g. the configuration id in training set
+  size_t globalid;
 };
 
Index: src/LevMartester.cpp
===================================================================
--- src/LevMartester.cpp	(revision bc55c96be4a2187ff0a65ac84ba020d9a58c4900)
+++ src/LevMartester.cpp	(revision eb1efe719c9062ecc3dbe78c9ad1eefbf84a5023)
@@ -44,4 +44,5 @@
 #include <iostream>
 #include <iterator>
+#include <list>
 #include <vector>
 
@@ -58,4 +59,5 @@
 #include "FunctionApproximation/FunctionModel.hpp"
 #include "Potentials/Specifics/PairPotential_Morse.hpp"
+#include "Potentials/Specifics/ManyBodyPotential_Tersoff.hpp"
 
 namespace po = boost::program_options;
@@ -70,4 +72,129 @@
   }
   return HomologyGraph();
+}
+
+HomologyGraph getFirstGraphWithOneCarbon(const HomologyContainer &homologies)
+{
+  FragmentNode SaturatedCarbon(6,3); // carbon has atomic number 6 and has 3 bonds (to other Hs)
+  for (HomologyContainer::container_t::const_iterator iter =
+      homologies.begin(); iter != homologies.end(); ++iter) {
+    if (iter->first.hasNode(SaturatedCarbon,1))
+      return iter->first;
+  }
+  return HomologyGraph();
+}
+
+FunctionModel::arguments_t
+gatherAllDistanceArguments(
+    const Fragment::charges_t &charges,
+    const Fragment::positions_t &positions,
+    const size_t globalid)
+{
+  FunctionModel::arguments_t result;
+
+  // go through current configuration and gather all other distances
+  Fragment::charges_t::const_iterator firstchargeiter = charges.begin();
+  Fragment::positions_t::const_iterator firstpositer = positions.begin();
+  for (;firstchargeiter != charges.end();
+      ++firstchargeiter, ++firstpositer) {
+    Fragment::charges_t::const_iterator secondchargeiter = charges.begin();//firstchargeiter;
+    Fragment::positions_t::const_iterator secondpositer = positions.begin();//firstpositer;
+    for (;
+        secondchargeiter != charges.end();
+        ++secondchargeiter, ++secondpositer) {
+      if (firstchargeiter == secondchargeiter)
+        continue;
+      argument_t arg;
+      const Vector firsttemp((*firstpositer)[0],(*firstpositer)[1],(*firstpositer)[2]);
+      const Vector secondtemp((*secondpositer)[0],(*secondpositer)[1],(*secondpositer)[2]);
+      arg.distance = firsttemp.distance(secondtemp);
+      arg.indices = std::make_pair(
+          std::distance(
+              charges.begin(), firstchargeiter),
+          std::distance(
+              charges.begin(), secondchargeiter)
+          );
+      arg.globalid = globalid;
+      result.push_back(arg);
+    }
+    ASSERT( secondpositer == positions.end(),
+        "gatherAllDistanceArguments() - there are not as many positions as charges.");
+  }
+  ASSERT( firstpositer == positions.end(),
+      "gatherAllDistanceArguments() - there are not as many positions as charges.");
+
+  return result;
+}
+
+/** This function returns the elements of the sum over index "k" for an
+ * argument containing indices "i" and "j"
+ * @param inputs vector of all configuration (containing each a vector of all arguments)
+ * @param arg argument containing indices "i" and "j"
+ * @param cutoff cutoff criterion for sum over k
+ * @return vector of argument pairs (a vector) of ik and jk for at least all k
+ *        within distance of \a cutoff to i
+ */
+std::vector<FunctionModel::arguments_t>
+getTripleFromArgument(const FunctionApproximation::inputs_t &inputs, const argument_t &arg, const double cutoff)
+{
+  typedef std::list<argument_t> arg_list_t;
+  typedef std::map<size_t, arg_list_t > k_args_map_t;
+  k_args_map_t tempresult;
+  ASSERT( inputs.size() > arg.globalid,
+      "getTripleFromArgument() - globalid "+toString(arg.globalid)
+      +" is greater than all inputs "+toString(inputs.size())+".");
+  const FunctionModel::arguments_t &listofargs = inputs[arg.globalid];
+  for (FunctionModel::arguments_t::const_iterator argiter = listofargs.begin();
+      argiter != listofargs.end();
+      ++argiter) {
+    // first index must be either i or j but second index not
+    if (((argiter->indices.first == arg.indices.first)
+        || (argiter->indices.first == arg.indices.second))
+      && ((argiter->indices.second != arg.indices.first)
+          && (argiter->indices.second != arg.indices.second))) {
+      // we need arguments ik and jk
+      std::pair< k_args_map_t::iterator, bool> inserter =
+          tempresult.insert( std::make_pair( argiter->indices.second, arg_list_t(1,*argiter)));
+      if (!inserter.second) {
+        // is present one ik or jk, if ik insert jk at back
+        if (inserter.first->second.begin()->indices.first == arg.indices.first)
+          inserter.first->second.push_back(*argiter);
+        else // if jk, insert ik at front
+          inserter.first->second.push_front(*argiter);
+      }
+    }
+//    // or second index must be either i or j but first index not
+//    else if (((argiter->indices.first != arg.indices.first)
+//              && (argiter->indices.first != arg.indices.second))
+//            && ((argiter->indices.second == arg.indices.first)
+//                || (argiter->indices.second == arg.indices.second))) {
+//      // we need arguments ki and kj
+//      std::pair< k_args_map_t::iterator, bool> inserter =
+//          tempresult.insert( std::make_pair( argiter->indices.first, arg_list_t(1,*argiter)));
+//      if (!inserter.second) {
+//        // is present one ki or kj, if ki insert kj at back
+//        if (inserter.first->second.begin()->indices.second == arg.indices.first)
+//          inserter.first->second.push_back(*argiter);
+//        else // if kj, insert ki at front
+//          inserter.first->second.push_front(*argiter);
+//      }
+//    }
+  }
+  // check that i,j are NOT contained
+  ASSERT( tempresult.count(arg.indices.first) == 0,
+      "getTripleFromArgument() - first index of argument present in k_args_map?");
+  ASSERT( tempresult.count(arg.indices.second) == 0,
+      "getTripleFromArgument() - first index of argument present in k_args_map?");
+
+  // convert
+  std::vector<FunctionModel::arguments_t> result;
+  for (k_args_map_t::const_iterator iter = tempresult.begin();
+      iter != tempresult.end();
+      ++iter) {
+    ASSERT( iter->second.size() == 2,
+        "getTripleFromArgument() - for index "+toString(iter->first)+" we did not find both ik and jk.");
+    result.push_back( FunctionModel::arguments_t(iter->second.begin(), iter->second.end()) );
+  }
+  return result;
 }
 
@@ -122,68 +249,128 @@
   }
 
-  // then we ought to pick the right HomologyGraph ...
-  const HomologyGraph graph = getFirstGraphWithTwoCarbons(homologies);
-  LOG(1, "First representative graph containing two saturated carbons is " << graph << ".");
-
-  // Afterwards we go through all of this type and gather the distance and the energy value
-  typedef std::pair<
-      FunctionApproximation::inputs_t,
-      FunctionApproximation::outputs_t> InputOutputVector_t;
-  InputOutputVector_t DistanceEnergyVector;
-  std::pair<HomologyContainer::const_iterator, HomologyContainer::const_iterator> range =
-      homologies.getHomologousGraphs(graph);
-  for (HomologyContainer::const_iterator iter = range.first; iter != range.second; ++iter) {
-    // get distance out of Fragment
-    const Fragment &fragment = iter->second.first;
-    const Fragment::charges_t charges = fragment.getCharges();
-    const Fragment::positions_t positions = fragment.getPositions();
-    std::vector< std::pair<Vector, size_t> > DistanceVectors;
-    for (Fragment::charges_t::const_iterator chargeiter = charges.begin();
-        chargeiter != charges.end(); ++chargeiter) {
-      if (*chargeiter == 6) {
-        Fragment::positions_t::const_iterator positer = positions.begin();
-        const size_t steps = std::distance(charges.begin(), chargeiter);
-        std::advance(positer, steps);
-        DistanceVectors.push_back(
-            std::make_pair(Vector((*positer)[0], (*positer)[1], (*positer)[2]),
-                steps));
-      }
-    }
-    if (DistanceVectors.size() == (size_t)2) {
-      argument_t arg;
-      arg.indices.first = DistanceVectors[0].second;
-      arg.indices.second = DistanceVectors[1].second;
-      arg.distance = DistanceVectors[0].first.distance(DistanceVectors[1].first);
-      const double energy = iter->second.second;
-      DistanceEnergyVector.first.push_back( FunctionModel::arguments_t(1,arg) );
+  /******************** MORSE TRAINING ********************/
+  {
+    // then we ought to pick the right HomologyGraph ...
+    const HomologyGraph graph = getFirstGraphWithTwoCarbons(homologies);
+    LOG(1, "First representative graph containing two saturated carbons is " << graph << ".");
+
+    // Afterwards we go through all of this type and gather the distance and the energy value
+    typedef std::pair<
+        FunctionApproximation::inputs_t,
+        FunctionApproximation::outputs_t> InputOutputVector_t;
+    InputOutputVector_t DistanceEnergyVector;
+    std::pair<HomologyContainer::const_iterator, HomologyContainer::const_iterator> range =
+        homologies.getHomologousGraphs(graph);
+    for (HomologyContainer::const_iterator iter = range.first; iter != range.second; ++iter) {
+      // get distance out of Fragment
+      const double &energy = iter->second.second;
+      const Fragment &fragment = iter->second.first;
+      const Fragment::charges_t charges = fragment.getCharges();
+      const Fragment::positions_t positions = fragment.getPositions();
+      std::vector< std::pair<Vector, size_t> > DistanceVectors;
+      for (Fragment::charges_t::const_iterator chargeiter = charges.begin();
+          chargeiter != charges.end(); ++chargeiter) {
+        if (*chargeiter == 6) {
+          Fragment::positions_t::const_iterator positer = positions.begin();
+          const size_t steps = std::distance(charges.begin(), chargeiter);
+          std::advance(positer, steps);
+          DistanceVectors.push_back(
+              std::make_pair(Vector((*positer)[0], (*positer)[1], (*positer)[2]),
+                  steps));
+        }
+      }
+      if (DistanceVectors.size() == (size_t)2) {
+        argument_t arg;
+        arg.indices.first = DistanceVectors[0].second;
+        arg.indices.second = DistanceVectors[1].second;
+        arg.distance = DistanceVectors[0].first.distance(DistanceVectors[1].first);
+        arg.globalid = DistanceEnergyVector.first.size();
+        DistanceEnergyVector.first.push_back( FunctionModel::arguments_t(1,arg) );
+        DistanceEnergyVector.second.push_back( FunctionModel::results_t(1,energy) );
+      } else {
+        ELOG(2, "main() - found not exactly two carbon atoms in fragment "
+            << fragment << ".");
+      }
+    }
+    // print training data for debugging
+    {
+      LOG(1, "INFO: I gathered the following (" << DistanceEnergyVector.first.size()
+          << "," << DistanceEnergyVector.second.size() << ") data pairs: ");
+      FunctionApproximation::inputs_t::const_iterator initer = DistanceEnergyVector.first.begin();
+      FunctionApproximation::outputs_t::const_iterator outiter = DistanceEnergyVector.second.begin();
+      for (; initer != DistanceEnergyVector.first.end(); ++initer, ++outiter) {
+        LOG(1, "INFO: (" << (*initer)[0].indices.first << "," << (*initer)[0].indices.second
+            << ") " << (*initer)[0].distance << " with energy " << *outiter);
+      }
+    }
+    // NOTICE that distance are in bohrradi as they come from MPQC!
+
+    // now perform the function approximation by optimizing the model function
+    PairPotential_Morse morse(1., 2.9, 0.5, -80.);
+    FunctionModel &model = morse;
+    FunctionApproximation approximator(1, 1, model);
+    approximator.setTrainingData(DistanceEnergyVector.first,DistanceEnergyVector.second);
+    approximator();
+    const FunctionModel::parameters_t params = model.getParameters();
+
+    LOG(0, "RESULT: Best parameters are " << params << ".");
+  }
+
+  /******************* TERSOFF TRAINING *******************/
+  {
+    // then we ought to pick the right HomologyGraph ...
+    const HomologyGraph graph = getFirstGraphWithOneCarbon(homologies);
+    LOG(1, "First representative graph containing one saturated carbon is " << graph << ".");
+
+    // Afterwards we go through all of this type and gather the distance and the energy value
+    typedef std::pair<
+        FunctionApproximation::inputs_t,
+        FunctionApproximation::outputs_t> InputOutputVector_t;
+    InputOutputVector_t DistanceEnergyVector;
+    std::pair<HomologyContainer::const_iterator, HomologyContainer::const_iterator> range =
+        homologies.getHomologousGraphs(graph);
+    for (HomologyContainer::const_iterator iter = range.first; iter != range.second; ++iter) {
+      // get distance out of Fragment
+      const double &energy = iter->second.second;
+      const Fragment &fragment = iter->second.first;
+      const Fragment::charges_t charges = fragment.getCharges();
+      const Fragment::positions_t positions = fragment.getPositions();
+      FunctionModel::arguments_t args =
+          gatherAllDistanceArguments(charges, positions, DistanceEnergyVector.first.size());
+      DistanceEnergyVector.first.push_back( args );
       DistanceEnergyVector.second.push_back( FunctionModel::results_t(1,energy) );
-    } else {
-      ELOG(2, "main() - found not exactly two carbon atoms in fragment "
-          << fragment << ".");
-    }
-  }
-  // print training data for debugging
-  {
-    LOG(1, "INFO: I gathered the following (" << DistanceEnergyVector.first.size()
-        << "," << DistanceEnergyVector.second.size() << ") data pairs: ");
-    FunctionApproximation::inputs_t::const_iterator initer = DistanceEnergyVector.first.begin();
-    FunctionApproximation::outputs_t::const_iterator outiter = DistanceEnergyVector.second.begin();
-    for (; initer != DistanceEnergyVector.first.end(); ++initer, ++outiter) {
-      LOG(1, "INFO: (" << (*initer)[0].indices.first << "," << (*initer)[0].indices.second
-          << ") " << (*initer)[0].distance << " with energy " << *outiter);
-    }
-  }
-  // NOTICE that distance are in bohrradi as they come from MPQC!
-
-  // now perform the function approximation by optimizing the model function
-  PairPotential_Morse morse(1., 2.9, 0.5, -80.);
-  FunctionModel &model = morse;
-  FunctionApproximation approximator(1, 1, model);
-  approximator.setTrainingData(DistanceEnergyVector.first,DistanceEnergyVector.second);
-  approximator();
-  const FunctionModel::parameters_t params = model.getParameters();
-
-  LOG(0, "RESULT: Best parameters are " << params[0] << ","
-      << params[1] << "," << params[2] << " and " << params[3] << ".");
+    }
+    // print training data for debugging
+    {
+      LOG(1, "INFO: I gathered the following (" << DistanceEnergyVector.first.size()
+          << "," << DistanceEnergyVector.second.size() << ") data pairs: ");
+      FunctionApproximation::inputs_t::const_iterator initer = DistanceEnergyVector.first.begin();
+      FunctionApproximation::outputs_t::const_iterator outiter = DistanceEnergyVector.second.begin();
+      for (; initer != DistanceEnergyVector.first.end(); ++initer, ++outiter) {
+        std::stringstream stream;
+        for (size_t index = 0; index < (*initer).size(); ++index)
+           stream << "(" << (*initer)[index].indices.first << "," << (*initer)[index].indices.second
+              << ") " << (*initer)[index].distance;
+        stream << " with energy " << *outiter;
+        LOG(1, "INFO: " << stream.str());
+      }
+    }
+    // NOTICE that distance are in bohrradi as they come from MPQC!
+
+    // now perform the function approximation by optimizing the model function
+    boost::function< std::vector<FunctionModel::arguments_t>(const argument_t &, const double)> triplefunction =
+        boost::bind(&getTripleFromArgument, DistanceEnergyVector.first, _1, _2);
+    ManyBodyPotential_Tersoff tersoff(triplefunction);
+    FunctionModel &model = tersoff;
+    FunctionApproximation approximator(
+        DistanceEnergyVector.first.begin()->size(), 
+        DistanceEnergyVector.second.begin()->size(), 
+        model); // CH4 has 5 atoms, hence 5*4/2 distances
+    approximator.setTrainingData(DistanceEnergyVector.first,DistanceEnergyVector.second);
+    approximator();
+    const FunctionModel::parameters_t params = model.getParameters();
+
+    LOG(0, "RESULT: Best parameters are " << params << ".");
+  }
 
   return 0;
Index: src/Potentials/Specifics/unittests/ManyBodyPotential_TersoffUnitTest.cpp
===================================================================
--- src/Potentials/Specifics/unittests/ManyBodyPotential_TersoffUnitTest.cpp	(revision bc55c96be4a2187ff0a65ac84ba020d9a58c4900)
+++ src/Potentials/Specifics/unittests/ManyBodyPotential_TersoffUnitTest.cpp	(revision eb1efe719c9062ecc3dbe78c9ad1eefbf84a5023)
@@ -65,5 +65,4 @@
 
 ManyBodyPotential_TersoffTest::configurations_t ManyBodyPotential_TersoffTest::configurations;
-ManyBodyPotential_TersoffTest::configuration_t *ManyBodyPotential_TersoffTest::CurrentConfiguration = NULL;
 
 /** This function looks up all distances ik and jk to a given ij and
@@ -73,15 +72,17 @@
 triplefunction(const argument_t &arguments, const double cutoff)
 {
+  const ManyBodyPotential_TersoffTest::configuration_t &CurrentConfiguration =
+      ManyBodyPotential_TersoffTest::configurations[arguments.globalid];
   std::vector<FunctionModel::arguments_t> result;
   // go through current configuration and gather all other distances
   ManyBodyPotential_TersoffTest::configuration_t::const_iterator firstiter =
-      ManyBodyPotential_TersoffTest::CurrentConfiguration->begin();
+      CurrentConfiguration.begin();
   std::advance(firstiter, arguments.indices.first);
   ManyBodyPotential_TersoffTest::configuration_t::const_iterator seconditer =
-      ManyBodyPotential_TersoffTest::CurrentConfiguration->begin();
+      CurrentConfiguration.begin();
   std::advance(seconditer, arguments.indices.second);
   for (ManyBodyPotential_TersoffTest::configuration_t::const_iterator iter =
-      ManyBodyPotential_TersoffTest::CurrentConfiguration->begin();
-      iter != ManyBodyPotential_TersoffTest::CurrentConfiguration->end();
+      CurrentConfiguration.begin();
+      iter != CurrentConfiguration.end();
       ++iter) {
     // skip k==i and k==j
@@ -92,25 +93,15 @@
     args[0].distance = firstiter->distance(*iter);
     args[0].indices = std::make_pair(
-        std::distance( // enforce const_iterator return from begin()
-            const_cast<const ManyBodyPotential_TersoffTest::configuration_t *>(
-                ManyBodyPotential_TersoffTest::CurrentConfiguration
-                )->begin(), firstiter),
-        std::distance( // enforce const_iterator return from begin()
-            const_cast<const ManyBodyPotential_TersoffTest::configuration_t *>(
-                ManyBodyPotential_TersoffTest::CurrentConfiguration
-                )->begin(), iter)
+        std::distance(CurrentConfiguration.begin(), firstiter),
+        std::distance(CurrentConfiguration.begin(), iter)
     );
+    args[0].globalid = arguments.globalid;
     // jk
     args[1].distance = seconditer->distance(*iter);
     args[1].indices = std::make_pair(
-        std::distance( // enforce const_iterator return from begin()
-            const_cast<const ManyBodyPotential_TersoffTest::configuration_t *>(
-                ManyBodyPotential_TersoffTest::CurrentConfiguration
-                )->begin(), seconditer),
-        std::distance( // enforce const_iterator return from begin()
-            const_cast<const ManyBodyPotential_TersoffTest::configuration_t *>(
-                ManyBodyPotential_TersoffTest::CurrentConfiguration
-                )->begin(), iter)
+        std::distance(CurrentConfiguration.begin(), seconditer),
+        std::distance(CurrentConfiguration.begin(), iter)
     );
+    args[1].globalid = arguments.globalid;
     result.push_back(args);
   }
@@ -276,5 +267,4 @@
 {
   configurations.clear();
-  CurrentConfiguration = NULL;
 }
 
@@ -292,13 +282,14 @@
   const_cast<double &>(tersoff.S) = 2.1;
   for (size_t index = 0; index < configurations.size(); ++index) {
-    CurrentConfiguration = &(configurations[index]);
+    const configuration_t &CurrentConfiguration = configurations[index];
     double temp = 0.;
-    for (size_t i=0; i < CurrentConfiguration->size(); ++i)
-      for (size_t j=0; j < CurrentConfiguration->size(); ++j) {
+    for (size_t i=0; i < CurrentConfiguration.size(); ++i)
+      for (size_t j=0; j < CurrentConfiguration.size(); ++j) {
         if (i == j)
           continue;
         argument_t arg;
         arg.indices = std::make_pair(i,j);
-        arg.distance = (*CurrentConfiguration)[i].distance((*CurrentConfiguration)[j]);
+        arg.distance = CurrentConfiguration[i].distance(CurrentConfiguration[j]);
+        arg.globalid = index; // this is needed for the triplefunction to the configuration
         FunctionModel::arguments_t args(1,arg);
         const ManyBodyPotential_Tersoff::results_t res = tersoff(args);
Index: src/Potentials/Specifics/unittests/ManyBodyPotential_TersoffUnitTest.hpp
===================================================================
--- src/Potentials/Specifics/unittests/ManyBodyPotential_TersoffUnitTest.hpp	(revision bc55c96be4a2187ff0a65ac84ba020d9a58c4900)
+++ src/Potentials/Specifics/unittests/ManyBodyPotential_TersoffUnitTest.hpp	(revision eb1efe719c9062ecc3dbe78c9ad1eefbf84a5023)
@@ -45,5 +45,4 @@
       typedef std::vector<configuration_t> configurations_t;
       static configurations_t configurations;
-      static configuration_t *CurrentConfiguration;
 private:
       FunctionModel::parameters_t params;
