Index: src/Actions/FragmentationAction/FragmentationAutomationAction.cpp
===================================================================
--- src/Actions/FragmentationAction/FragmentationAutomationAction.cpp	(revision 36bb2d7a076ed791ede05f0dc93c94e2ad78995d)
+++ src/Actions/FragmentationAction/FragmentationAutomationAction.cpp	(revision c4ee0808e13827946db48dc3d32793d3fd932f0e)
@@ -186,26 +186,18 @@
 }
 
-
-/** Print MPQCData from received results.
- *
- * @param results results with ids to associate with fragment number
- * @param fragmentData MPQCData resulting from the jobs
- * @param KeySetFilename filename with keysets to associate forces correctly
- * @param NoAtoms total number of atoms
+/** Creates a lookup from FragmentJob::id to the true fragment  number.
+ *
+ * @param results result with job ids
+ * @param MatrixNrLookup Lookup up-map, filled on return
+ * @param FragmentCounter total number of fragments on return
  */
-bool printReceivedMPQCResults(
+void createMatrixNrLookup(
     const std::vector<FragmentResult::ptr> &results,
-    const std::vector<MPQCData> &fragmentData,
-    const std::string &KeySetFilename,
-    size_t NoAtoms)
-{
-  EnergyMatrix Energy;
-  EnergyMatrix EnergyFragments;
-  ForceMatrix Force;
-  ForceMatrix ForceFragments;
-
+    std::map< JobId_t, size_t > &MatrixNrLookup,
+    size_t &FragmentCounter)
+{
   // align fragments
-  std::map< JobId_t, size_t > MatrixNrLookup;
-  size_t FragmentCounter = 0;
+  MatrixNrLookup.clear();
+  FragmentCounter = 0;
   {
     // bring ids in order ...
@@ -228,5 +220,28 @@
   }
   LOG(1, "INFO: There are " << FragmentCounter << " fragments.");
-
+}
+
+/** Place results from FragmentResult into EnergyMatrix and ForceMatrix.
+ *
+ * @param results results with ids to associate with fragment number
+ * @param fragmentData MPQCData resulting from the jobs
+ * @param MatrixNrLookup Lookup up-map from job id to fragment number
+ * @param FragmentCounter total number of fragments
+ * @param NoAtoms total number of atoms
+ * @param Energy energy matrix to be filled on return
+ * @param Force force matrix to be filled on return
+ * @return true - everything ok, false - else
+ */
+bool putResultsintoMatrices(
+    const std::vector<FragmentResult::ptr> &results,
+    const std::vector<MPQCData> &fragmentData,
+    std::map< JobId_t, size_t > &MatrixNrLookup,
+    const size_t FragmentCounter,
+    const size_t NoAtoms,
+    EnergyMatrix &Energy,
+    ForceMatrix &Force)
+{
+  ASSERT( results.size() == fragmentData.size(),
+      "printReceivedMPQCResults() - results and fragmentData differ in size.");
   std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
   std::vector<FragmentResult::ptr>::const_iterator resultiter = results.begin();
@@ -281,5 +296,117 @@
     return false;
 
-  // initialise indices
+  return true;
+}
+
+void convertMPQCDatatoEnergyMap(
+    const std::vector<MPQCData> &fragmentData,
+    std::vector<MPQCDataEnergyMap_t> &MPQCData_Energy_fused)
+{
+  // energy_t
+  MPQCData_Energy_fused.clear();
+  MPQCData_Energy_fused.reserve(fragmentData.size());
+  for(std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
+      dataiter != fragmentData.end(); ++dataiter) {
+    const MPQCData &extractedData = *dataiter;
+    LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
+    MPQCDataEnergyMap_t instance;
+    boost::fusion::at_key<MPQCDataFused::energy_total>(instance) = extractedData.energies.total;
+    boost::fusion::at_key<MPQCDataFused::energy_nuclear_repulsion>(instance) = extractedData.energies.nuclear_repulsion;
+    boost::fusion::at_key<MPQCDataFused::energy_electron_coulomb>(instance) = extractedData.energies.electron_coulomb;
+    boost::fusion::at_key<MPQCDataFused::energy_electron_exchange>(instance) = extractedData.energies.electron_exchange;
+    boost::fusion::at_key<MPQCDataFused::energy_correlation>(instance) = extractedData.energies.correlation;
+    boost::fusion::at_key<MPQCDataFused::energy_overlap>(instance) = extractedData.energies.overlap;
+    boost::fusion::at_key<MPQCDataFused::energy_kinetic>(instance) = extractedData.energies.kinetic;
+    boost::fusion::at_key<MPQCDataFused::energy_hcore>(instance) = extractedData.energies.hcore;
+    boost::fusion::at_key<MPQCDataFused::energy_eigenvalues>(instance) = extractedData.energies.eigenvalues;
+    MPQCData_Energy_fused.push_back(instance);
+  }
+}
+
+void convertMPQCDatatoForceMap(
+    const std::vector<MPQCData> &fragmentData,
+    const KeySetsContainer &ForceKeySet,
+    std::vector<MPQCDataForceMap_t> &MPQCData_Force_fused)
+{
+  // forces
+  ASSERT( ForceKeySet.KeySets.size() == fragmentData.size(),
+      "FragmentationAutomationAction::performCall() - indices and fragmentData differ in size.");
+  MPQCData_Force_fused.clear();
+  MPQCData_Force_fused.reserve(fragmentData.size());
+  std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
+  KeySetsContainer::ArrayOfIntVectors::const_iterator arrayiter = ForceKeySet.KeySets.begin();
+  for(;dataiter != fragmentData.end(); ++dataiter, ++arrayiter) {
+    const MPQCData &extractedData = *dataiter;
+    LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
+    MPQCDataForceMap_t instance;
+    // must convert int to index_t
+    IndexedVectors::indices_t indices(arrayiter->begin(), arrayiter->end());
+    boost::fusion::at_key<MPQCDataFused::forces>(instance) =
+        IndexedVectors(indices, extractedData.forces);
+    MPQCData_Force_fused.push_back(instance);
+  }
+}
+
+void convertMPQCDatatoGridMap(
+    const std::vector<MPQCData> &fragmentData,
+    std::vector<MPQCDataGridMap_t> &MPQCData_Grid_fused)
+{
+  // sampled_grid
+  MPQCData_Grid_fused.clear();
+  MPQCData_Grid_fused.reserve(fragmentData.size());
+  for(std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
+      dataiter != fragmentData.end(); ++dataiter) {
+    const MPQCData &extractedData = *dataiter;
+    LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
+    MPQCDataGridMap_t instance;
+    boost::fusion::at_key<MPQCDataFused::sampled_grid>(instance) = extractedData.sampled_grid;
+    MPQCData_Grid_fused.push_back(instance);
+  }
+}
+
+void convertMPQCDatatoTimeMap(
+    const std::vector<MPQCData> &fragmentData,
+    std::vector<MPQCDataTimeMap_t> &MPQCData_Time_fused)
+{
+  // times
+  MPQCData_Time_fused.clear();
+  MPQCData_Time_fused.reserve(fragmentData.size());
+  for(std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
+      dataiter != fragmentData.end(); ++dataiter) {
+    const MPQCData &extractedData = *dataiter;
+    LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
+    MPQCDataTimeMap_t instance;
+    boost::fusion::at_key<MPQCDataFused::times_walltime>(instance) = extractedData.times.walltime;
+    boost::fusion::at_key<MPQCDataFused::times_cputime>(instance) = extractedData.times.cputime;
+    boost::fusion::at_key<MPQCDataFused::times_flops>(instance) = extractedData.times.flops;
+    MPQCData_Time_fused.push_back(instance);
+  }
+}
+
+/** Print MPQCData from received results.
+ *
+ * @param results results with ids to associate with fragment number
+ * @param fragmentData MPQCData resulting from the jobs
+ * @param KeySetFilename filename with keysets to associate forces correctly
+ * @param NoAtoms total number of atoms
+ */
+bool printReceivedMPQCResults(
+    const std::vector<FragmentResult::ptr> &results,
+    const std::vector<MPQCData> &fragmentData,
+    const std::string &KeySetFilename,
+    size_t NoAtoms)
+{
+  // create lookup from job nr to fragment number
+  std::map< JobId_t, size_t > MatrixNrLookup;
+  size_t FragmentCounter = 0;
+  createMatrixNrLookup(results, MatrixNrLookup, FragmentCounter);
+
+  // place results into maps
+  EnergyMatrix Energy;
+  ForceMatrix Force;
+  if (!putResultsintoMatrices(results, fragmentData, MatrixNrLookup, FragmentCounter, NoAtoms, Energy, Force))
+    return false;
+
+  // initialise keysets
   KeySetsContainer KeySet;
   KeySetsContainer ForceKeySet;
@@ -289,4 +416,5 @@
 
   {
+    // else needs keysets without hydrogens
     std::stringstream filename;
     filename << FRAGMENTPREFIX << KEYSETFILE;
@@ -295,4 +423,5 @@
 
   {
+    // forces need keysets including hydrogens
     std::stringstream filename;
     filename << FRAGMENTPREFIX << FORCESFILE;
@@ -307,76 +436,17 @@
   SubsetMap::ptr subsetmap(new SubsetMap(*container));
 
-// we probably don't need this, it suffices if IndexedVectors has some extra indices
-//  // forces need different keysets: they must include hydrogen
-//  // (though without ones added by saturation)
-//  IndexSetContainer::ptr forceindices(new IndexSetContainer(ForceKeySet));
-//  // create the map of all keysets
-//  SubsetMap::ptr subsetmapforces(new SubsetMap(*forceindices));
-
   /// convert all MPQCData to MPQCDataMap_t
   {
-    // energy_t
-    std::vector<MPQCDataEnergyMap_t> MPQCData_Energy_fused;
-    MPQCData_Energy_fused.reserve(fragmentData.size());
-    for(std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
-        dataiter != fragmentData.end(); ++dataiter) {
-      const MPQCData &extractedData = *dataiter;
-      LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
-      MPQCDataEnergyMap_t instance;
-      boost::fusion::at_key<MPQCDataFused::energy_total>(instance) = extractedData.energies.total;
-      boost::fusion::at_key<MPQCDataFused::energy_nuclear_repulsion>(instance) = extractedData.energies.nuclear_repulsion;
-      boost::fusion::at_key<MPQCDataFused::energy_electron_coulomb>(instance) = extractedData.energies.electron_coulomb;
-      boost::fusion::at_key<MPQCDataFused::energy_electron_exchange>(instance) = extractedData.energies.electron_exchange;
-      boost::fusion::at_key<MPQCDataFused::energy_correlation>(instance) = extractedData.energies.correlation;
-      boost::fusion::at_key<MPQCDataFused::energy_overlap>(instance) = extractedData.energies.overlap;
-      boost::fusion::at_key<MPQCDataFused::energy_kinetic>(instance) = extractedData.energies.kinetic;
-      boost::fusion::at_key<MPQCDataFused::energy_hcore>(instance) = extractedData.energies.hcore;
-      boost::fusion::at_key<MPQCDataFused::energy_eigenvalues>(instance) = extractedData.energies.eigenvalues;
-      MPQCData_Energy_fused.push_back(instance);
-    }
-
-    // forces
     ASSERT( ForceKeySet.KeySets.size() == fragmentData.size(),
         "FragmentationAutomationAction::performCall() - ForceKeySet's KeySets and fragmentData differ in size.");
+
+    std::vector<MPQCDataEnergyMap_t> MPQCData_Energy_fused;
+    convertMPQCDatatoEnergyMap(fragmentData, MPQCData_Energy_fused);
     std::vector<MPQCDataForceMap_t> MPQCData_Force_fused;
-    MPQCData_Force_fused.reserve(fragmentData.size());
-    std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
-    KeySetsContainer::ArrayOfIntVectors::const_iterator arrayiter = ForceKeySet.KeySets.begin();
-    for(;dataiter != fragmentData.end(); ++dataiter, ++arrayiter) {
-      const MPQCData &extractedData = *dataiter;
-      LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
-      MPQCDataForceMap_t instance;
-      // must convert int to index_t
-      IndexedVectors::indices_t indices(arrayiter->begin(), arrayiter->end());
-      boost::fusion::at_key<MPQCDataFused::forces>(instance) =
-          IndexedVectors(indices, extractedData.forces);
-      MPQCData_Force_fused.push_back(instance);
-    }
-
-    // sampled_grid
+    convertMPQCDatatoForceMap(fragmentData, ForceKeySet, MPQCData_Force_fused);
     std::vector<MPQCDataGridMap_t> MPQCData_Grid_fused;
-    MPQCData_Grid_fused.reserve(fragmentData.size());
-    for(std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
-        dataiter != fragmentData.end(); ++dataiter) {
-      const MPQCData &extractedData = *dataiter;
-      LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
-      MPQCDataGridMap_t instance;
-      boost::fusion::at_key<MPQCDataFused::sampled_grid>(instance) = extractedData.sampled_grid;
-      MPQCData_Grid_fused.push_back(instance);
-    }
-
-    // times
+    convertMPQCDatatoGridMap(fragmentData, MPQCData_Grid_fused);
     std::vector<MPQCDataTimeMap_t> MPQCData_Time_fused;
-    MPQCData_Time_fused.reserve(fragmentData.size());
-    for(std::vector<MPQCData>::const_iterator dataiter = fragmentData.begin();
-        dataiter != fragmentData.end(); ++dataiter) {
-      const MPQCData &extractedData = *dataiter;
-      LOG(2, "DEBUG: Current extracted Data is " << extractedData << ".");
-      MPQCDataTimeMap_t instance;
-      boost::fusion::at_key<MPQCDataFused::times_walltime>(instance) = extractedData.times.walltime;
-      boost::fusion::at_key<MPQCDataFused::times_cputime>(instance) = extractedData.times.cputime;
-      boost::fusion::at_key<MPQCDataFused::times_flops>(instance) = extractedData.times.flops;
-      MPQCData_Time_fused.push_back(instance);
-    }
+    convertMPQCDatatoTimeMap(fragmentData, MPQCData_Time_fused);
 
     // create a vector of all job ids
@@ -452,4 +522,6 @@
   if (!KeySet.ParseManyBodyTerms()) return false;
 
+  EnergyMatrix EnergyFragments;
+  ForceMatrix ForceFragments;
   if (!EnergyFragments.AllocateMatrix(Energy.Header, Energy.MatrixCounter, Energy.RowCounter, Energy.ColumnCounter)) return false;
   if (!ForceFragments.AllocateMatrix(Force.Header, Force.MatrixCounter, Force.RowCounter, Force.ColumnCounter)) return false;
