Index: src/Fragmentation/Automation/controller.cpp
===================================================================
--- src/Fragmentation/Automation/controller.cpp	(revision 917be8b2ef9da0d7774581a46ba0ecc45a38e985)
+++ src/Fragmentation/Automation/controller.cpp	(revision d76161ebad259dbe9c91bea5ed52654bda2d48d6)
@@ -43,4 +43,5 @@
 #include "Fragmentation/KeySetsContainer.hpp"
 #include "FragmentController.hpp"
+#include "Helpers/defs.hpp"
 #include "Jobs/MPQCCommandJob.hpp"
 #include "Jobs/MPQCCommandJob_MPQCData.hpp"
@@ -115,10 +116,12 @@
 /** Print MPQCData from received results.
  *
- * @param results vector of all received FragmentResult's
- * @param KeySetFilename filename containing keysets
+ * @param results received results to extract MPQCData from
+ * @param KeySetFilename filename with keysets to associate forces correctly
+ * @param NoAtoms total number of atoms
  */
 bool printReceivedMPQCResults(
-    std::vector<FragmentResult::ptr> &results,
-    const std::string &KeySetFilename)
+    const std::vector<FragmentResult::ptr> &results,
+    const std::string &KeySetFilename,
+    size_t NoAtoms)
 {
   EnergyMatrix Energy;
@@ -128,8 +131,29 @@
   KeySetsContainer KeySet;
 
-  // place results into EnergyMatrix and ForceMatrix
-  //if (!Energy.ParseFragmentMatrix(argv[1], dir, EnergySuffix, 0,0)) return false;
-  //if (!Force.ParseFragmentMatrix(argv[1], dir, ForcesSuffix, 0,0)) return false;
-  // combine all found data
+  // align fragments
+  std::map< JobId_t, size_t > MatrixNrLookup;
+  size_t FragmentCounter = 0;
+  {
+    // bring ids in order ...
+    typedef std::map< JobId_t, FragmentResult::ptr> IdResultMap_t;
+    IdResultMap_t IdResultMap;
+    for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
+        iter != results.end(); ++iter) {
+  #ifndef NDEBUG
+      std::pair< IdResultMap_t::iterator, bool> inserter =
+  #endif
+      IdResultMap.insert( make_pair((*iter)->getId(), *iter) );
+      ASSERT( inserter.second,
+          "printReceivedMPQCResults() - two results have same id "
+          +toString((*iter)->getId())+".");
+    }
+    // ... and fill lookup
+    for(IdResultMap_t::const_iterator iter = IdResultMap.begin();
+        iter != IdResultMap.end(); ++iter)
+      MatrixNrLookup.insert( make_pair(iter->first, FragmentCounter++) );
+  }
+  LOG(1, "INFO: There are " << FragmentCounter << " fragments.");
+
+  // extract results
   std::vector<MPQCData> fragmentData(results.size());
   MPQCData combinedData;
@@ -141,9 +165,59 @@
     MPQCData extractedData;
     std::stringstream inputstream((*iter)->result);
+    LOG(2, "DEBUG: First 50 characters FragmentResult's string: "+(*iter)->result.substr(0, 50));
     boost::archive::text_iarchive ia(inputstream);
     ia >> extractedData;
     LOG(1, "INFO: extracted data is " << extractedData << ".");
-  }
-
+
+    // place results into EnergyMatrix ...
+    {
+      MatrixContainer::MatrixArray matrix;
+      matrix.resize(1);
+      matrix[0].resize(1, extractedData.energy);
+      if (!Energy.AddMatrix(
+          std::string("MPQCJob ")+toString((*iter)->getId()),
+          matrix,
+          MatrixNrLookup[(*iter)->getId()])) {
+        ELOG(1, "Adding energy matrix failed.");
+        return false;
+      }
+    }
+    // ... and ForceMatrix (with two empty columns in front)
+    {
+      MatrixContainer::MatrixArray matrix;
+      const size_t rows = extractedData.forces.size();
+      matrix.resize(rows);
+      for (size_t i=0;i<rows;++i) {
+        const size_t columns = 2+extractedData.forces[i].size();
+        matrix[i].resize(columns, 0.);
+  //      for (size_t j=0;j<2;++j)
+  //        matrix[i][j] = 0.;
+        for (size_t j=2;j<columns;++j)
+          matrix[i][j] = extractedData.forces[i][j-2];
+      }
+      if (!Force.AddMatrix(
+          std::string("MPQCJob ")+toString((*iter)->getId()),
+          matrix,
+          MatrixNrLookup[(*iter)->getId()])) {
+        ELOG(1, "Adding force matrix failed.");
+        return false;
+      }
+    }
+  }
+  // add one more matrix (not required for energy)
+  MatrixContainer::MatrixArray matrix;
+  matrix.resize(1);
+  matrix[0].resize(1, 0.);
+  if (!Energy.AddMatrix(std::string("MPQCJob total"), matrix, FragmentCounter))
+    return false;
+  // but for energy because we need to know total number of atoms
+  matrix.resize(NoAtoms);
+  for (size_t i = 0; i< NoAtoms; ++i)
+    matrix[i].resize(2+NDIM, 0.);
+  if (!Force.AddMatrix(std::string("MPQCJob total"), matrix, FragmentCounter))
+    return false;
+
+
+  // combine all found data
   if (!Energy.InitialiseIndices()) return false;
 
@@ -172,5 +246,44 @@
   }
 
+  // for debugging print resulting energy and forces
+  LOG(1, "INFO: Resulting energy is " << Energy.Matrix[ FragmentCounter ][0][0]);
+  std::stringstream output;
+  for (int i=0; i< Force.RowCounter[FragmentCounter]; ++i) {
+    for (int j=0; j< Force.ColumnCounter[FragmentCounter]; ++j)
+      output << Force.Matrix[ FragmentCounter ][i][j] << " ";
+    output << "\n";
+  }
+  LOG(1, "INFO: Resulting forces are " << std::endl << output.str());
+
   return true;
+}
+
+/** Helper function to get number of atoms somehow.
+ *
+ * Here, we just parse the number of lines in the adjacency file as
+ * it should correspond to the number of atoms, except when some atoms
+ * are not bonded, but then fragmentation makes no sense.
+ *
+ * @param path path to the adjacency file
+ */
+size_t getNoAtomsFromAdjacencyFile(const std::string &path)
+{
+  size_t NoAtoms = 0;
+
+  // parse in special file to get atom count (from line count)
+  std::string filename(path);
+  filename += FRAGMENTPREFIX;
+  filename += ADJACENCYFILE;
+  std::ifstream adjacency(filename.c_str());
+  if (adjacency.fail()) {
+    LOG(0, endl << "getNoAtomsFromAdjacencyFile() - Unable to open " << filename << ", is the directory correct?");
+    return false;
+  }
+  std::string buffer;
+  while (getline(adjacency, buffer))
+    NoAtoms++;
+  LOG(1, "INFO: There are " << NoAtoms << " atoms.");
+
+  return NoAtoms;
 }
 
@@ -355,11 +468,14 @@
       case ReceiveMPQCIndex:
       {
-        if (argc == 4) {
-          ELOG(1, "'receivempqc' requires one option: [KeySetFilename].");
+        if (argc < 5) {
+          ELOG(1, "'receivempqc' require one option: [path to fragment files].");
         } else {
-          const std::string KeySetFilename = argv[4];
-          LOG(1, "INFO: Parsing id associations from " << KeySetFilename << ".");
+          const std::string path = argv[4];
+          LOG(1, "INFO: Parsing fragment files from " << path << ".");
           std::vector<FragmentResult::ptr> results = controller.getReceivedResults();
-          printReceivedMPQCResults(results, KeySetFilename);
+          printReceivedMPQCResults(
+              results,
+              path,
+              getNoAtomsFromAdjacencyFile(path));
         }
         break;
