Index: src/ControllerOptions_MPQCCommandJob.cpp
===================================================================
--- src/ControllerOptions_MPQCCommandJob.cpp	(revision ee61ce4beaab0d3c2f7cf501fc17d4a09765d3b7)
+++ src/ControllerOptions_MPQCCommandJob.cpp	(revision cbcbbd63e97a2e60704da03bab709f5377d591aa)
@@ -25,4 +25,26 @@
 #include "ControllerOptions_MPQCCommandJob.hpp"
 
+int ControllerOptions_MPQCCommandJob::parseExecutable(boost::program_options::variables_map &vm) {
+  if ((command == "createjobs") || (command == "addjobs")) {
+    if (!vm.count("executable")) {
+      ELOG(1, "'"+command+"' requires two options: [executable] [jobcommand].");
+      return 255;
+    }
+    executable = vm["executable"].as< std::string >();
+  }
+  return 0;
+}
+
+int ControllerOptions_MPQCCommandJob::parseJobCommand(boost::program_options::variables_map &vm) {
+  if (command == "createjobs") {
+    if (!vm.count("jobcommand")) {
+      ELOG(1, "'"+command+"' requires two options: [executable] [jobcommand].");
+      return 255;
+    }
+    jobcommand = vm["jobcommand"].as< std::string >();
+  }
+  return 0;
+}
+
 int ControllerOptions_MPQCCommandJob::parseFragmentpath(boost::program_options::variables_map &vm) {
   if (command == "receivempqc") {
Index: src/ControllerOptions_MPQCCommandJob.hpp
===================================================================
--- src/ControllerOptions_MPQCCommandJob.hpp	(revision ee61ce4beaab0d3c2f7cf501fc17d4a09765d3b7)
+++ src/ControllerOptions_MPQCCommandJob.hpp	(revision cbcbbd63e97a2e60704da03bab709f5377d591aa)
@@ -18,11 +18,16 @@
 #include <string>
 
-#include "ControllerOptions_SystemCommandJob.hpp"
+#include "ControllerOptions.hpp"
 
-struct ControllerOptions_MPQCCommandJob : public ControllerOptions_SystemCommandJob
+struct ControllerOptions_MPQCCommandJob : public ControllerOptions
 {
+  int parseExecutable(boost::program_options::variables_map &vm);
+  int parseJobCommand(boost::program_options::variables_map &vm);
+
   int parseFragmentpath(boost::program_options::variables_map &vm);
   int parseJobfiles(boost::program_options::variables_map &vm);
 
+  std::string executable;
+  std::string jobcommand;
   std::string fragmentpath;
   std::vector<std::string> jobfiles;
Index: src/Makefile.am
===================================================================
--- src/Makefile.am	(revision ee61ce4beaab0d3c2f7cf501fc17d4a09765d3b7)
+++ src/Makefile.am	(revision cbcbbd63e97a2e60704da03bab709f5377d591aa)
@@ -381,5 +381,6 @@
   Fragmentation/Automation/Pool/WorkerPool.hpp
 
-Controller_SOURCES = $(CONTROLLERSOURCE) $(CONTROLLERHEADER) controller.cpp ControllerCommand.hpp ControllerCommandRegistry.cpp ControllerCommandRegistry.hpp DefaultOptions.cpp DefaultOptions.hpp ControllerOptions.cpp ControllerOptions.hpp ControllerOptions_MPQCCommandJob.cpp ControllerOptions_MPQCCommandJob.hpp ControllerOptions_SystemCommandJob.cpp ControllerOptions_SystemCommandJob.hpp
+Controller_SOURCES = $(CONTROLLERSOURCE) $(CONTROLLERHEADER) controller.cpp controller_MPQCCommandJob.cpp controller_MPQCCommandJob.hpp ControllerCommand.hpp ControllerCommandRegistry.cpp ControllerCommandRegistry.hpp DefaultOptions.cpp DefaultOptions.hpp ControllerOptions.cpp ControllerOptions.hpp ControllerOptions_MPQCCommandJob.cpp ControllerOptions_MPQCCommandJob.hpp
+#controller_SystemCommandJob.cpp controller_SystemCommandJob.hpp ControllerOptions_SystemCommandJob.cpp ControllerOptions_SystemCommandJob.hpp
 Controller_LDFLAGS = $(AM_LDFLAGS) $(BOOST_PROGRAM__OPTIONS_LDFLAGS) $(BOOST_SYSTEM_LDFLAGS) $(BOOST_THREAD_LDFLAGS) $(BOOST_SERIALIZATION_LDFLAGS)
 Controller_CXXFLAGS = $(AM_CPPFLAGS)
Index: src/controller.cpp
===================================================================
--- src/controller.cpp	(revision ee61ce4beaab0d3c2f7cf501fc17d4a09765d3b7)
+++ src/controller.cpp	(revision cbcbbd63e97a2e60704da03bab709f5377d591aa)
@@ -40,16 +40,10 @@
 #include "CodePatterns/Info.hpp"
 #include "CodePatterns/Log.hpp"
-#include "Fragmentation/EnergyMatrix.hpp"
-#include "Fragmentation/ForceMatrix.hpp"
-#include "Fragmentation/KeySetsContainer.hpp"
+
 #include "FragmentController.hpp"
-#include "Helpers/defs.hpp"
-#include "Jobs/MPQCCommandJob.hpp"
-#include "Jobs/MPQCCommandJob_MPQCData.hpp"
-#include "Fragmentation/Automation/Jobs/SystemCommandJob.hpp"
-#include "Fragmentation/Automation/Results/FragmentResult.hpp"
 #include "ControllerOptions_MPQCCommandJob.hpp"
-#include "ControllerOptions_SystemCommandJob.hpp"
 #include "ControllerCommandRegistry.hpp"
+
+#include "controller_MPQCCommandJob.hpp"
 
 /** Print the status of scheduled and done jobs.
@@ -63,248 +57,12 @@
 }
 
-/** Creates a MPQCCommandJob with argument \a filename.
- *
- * @param jobs created job is added to this vector
- * @param command mpqc command to execute
- * @param filename filename being argument to job
- * @param nextid id for this job
- */
-void parsejob(
-    std::vector<FragmentJob::ptr> &jobs,
-    const std::string &command,
-    const std::string &filename,
-    const JobId_t nextid)
+inline std::vector<std::string> getListOfCommands(const ControllerCommandRegistry &ControllerCommands)
 {
-  std::ifstream file;
-  file.open(filename.c_str());
-  ASSERT( file.good(), "parsejob() - file "+filename+" does not exist.");
-  std::string output((std::istreambuf_iterator<char>(file)),
-      std::istreambuf_iterator<char>());
-  FragmentJob::ptr testJob( new MPQCCommandJob(output, nextid, command) );
-  jobs.push_back(testJob);
-  file.close();
-  LOG(1, "INFO: Added MPQCCommandJob from file "+filename+".");
-}
+  std::vector<std::string> Commands;
+  for (ControllerCommandRegistry::const_iterator iter = ControllerCommands.getBeginIter();
+      iter != ControllerCommands.getEndIter(); ++iter)
+    Commands.push_back(iter->first);
 
-/** Print received results.
- *
- * @param results received results to print
- */
-void printReceivedResults(const std::vector<FragmentResult::ptr> &results)
-{
-  for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
-      iter != results.end(); ++iter)
-    LOG(1, "RESULT: job #"+toString((*iter)->getId())+": "+toString((*iter)->result));
-}
-
-/** Print MPQCData from received results.
- *
- * @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(
-    const std::vector<FragmentResult::ptr> &results,
-    const std::string &KeySetFilename,
-    size_t NoAtoms)
-{
-  EnergyMatrix Energy;
-  EnergyMatrix EnergyFragments;
-  ForceMatrix Force;
-  ForceMatrix ForceFragments;
-  KeySetsContainer KeySet;
-
-  // 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;
-
-  LOG(2, "DEBUG: Parsing now through " << results.size() << " results.");
-  for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
-      iter != results.end(); ++iter) {
-    LOG(1, "RESULT: job #"+toString((*iter)->getId())+": "+toString((*iter)->result));
-    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;
-
-  if (!Force.ParseIndices(KeySetFilename.c_str())) return false;
-
-  if (!KeySet.ParseKeySets(KeySetFilename.c_str(), Force.RowCounter, Force.MatrixCounter)) return false;
-
-  if (!KeySet.ParseManyBodyTerms()) return false;
-
-  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;
-
-  if(!Energy.SetLastMatrix(0., 0)) return false;
-  if(!Force.SetLastMatrix(0., 2)) return false;
-
-  for (int BondOrder=0;BondOrder<KeySet.Order;BondOrder++) {
-    // --------- sum up energy --------------------
-    LOG(1, "INFO: Summing energy of order " << BondOrder+1 << " ...");
-    if (!EnergyFragments.SumSubManyBodyTerms(Energy, KeySet, BondOrder)) return false;
-    if (!Energy.SumSubEnergy(EnergyFragments, NULL, KeySet, BondOrder, 1.)) return false;
-
-    // --------- sum up Forces --------------------
-    LOG(1, "INFO: Summing forces of order " << BondOrder+1 << " ...");
-    if (!ForceFragments.SumSubManyBodyTerms(Force, KeySet, BondOrder)) return false;
-    if (!Force.SumSubForces(ForceFragments, KeySet, BondOrder, 1.)) return false;
-  }
-
-  // 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;
-}
-
-/** Creates a SystemCommandJob out of give \a command with \a argument.
- *
- * @param controller reference to controller to add jobs
- * @param ControllerInfo information on the job
- */
-void createJobs(FragmentController &controller, const ControllerOptions_SystemCommandJob &ControllerInfo)
-{
-  const JobId_t next_id = controller.getAvailableId();
-  FragmentJob::ptr testJob(
-      new SystemCommandJob(ControllerInfo.executable, ControllerInfo.jobcommand, next_id) );
-  std::vector<FragmentJob::ptr> jobs;
-  jobs.push_back(testJob);
-  controller.addJobs(jobs);
-  controller.sendJobs(ControllerInfo.server, ControllerInfo.serverport);
-  LOG(1, "INFO: Added one SystemCommandJob.");
-}
-
-/** Creates a MPQCCommandJob out of give \a command with \a argument.
- *
- * @param controller reference to controller to add jobs
- * @param ControllerInfo information on the job
- */
-void AddJobs(FragmentController &controller, const ControllerOptions_MPQCCommandJob &ControllerInfo)
-{
-  std::vector<FragmentJob::ptr> jobs;
-  for (std::vector< std::string >::const_iterator iter = ControllerInfo.jobfiles.begin();
-      iter != ControllerInfo.jobfiles.end(); ++iter) {
-    const JobId_t next_id = controller.getAvailableId();
-    const std::string &filename = *iter;
-    LOG(1, "INFO: Creating MPQCCommandJob with filename '"
-        +filename+"', and id "+toString(next_id)+".");
-    parsejob(jobs, ControllerInfo.executable, filename, next_id);
-  }
-  controller.addJobs(jobs);
-  controller.sendJobs(ControllerInfo.server, ControllerInfo.serverport);
+  return Commands;
 }
 
@@ -317,5 +75,5 @@
   size_t Exitflag = 0;
 
-  ControllerOptions_MPQCCommandJob ControllerInfo;
+  ControllerOptions *ControllerInfo = allocateControllerInfo();
   boost::asio::io_service io_service;
   FragmentController controller(io_service);
@@ -326,54 +84,24 @@
   // in contrast to other functions that return void
   ControllerCommandRegistry ControllerCommands;
-  ControllerCommands.registerInstance(new ControllerCommand("addjobs",
-      boost::assign::list_of< ControllerCommand::commands_t >
-      (boost::bind(&FragmentController::requestIds,
-          boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport),
-          boost::bind(&std::vector<std::string>::size, boost::cref(ControllerInfo.jobfiles))))
-      (boost::bind(&AddJobs, boost::ref(controller), boost::cref(ControllerInfo)))
-      ));
-  ControllerCommands.registerInstance(new ControllerCommand("createjobs",
-      boost::assign::list_of< ControllerCommand::commands_t >
-        (boost::bind(&FragmentController::requestIds,
-            boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport), 1))
-        (boost::bind(&createJobs, boost::ref(controller), boost::cref(ControllerInfo)))
-      ));
-  ControllerCommands.registerInstance(new ControllerCommand("checkresults",
+  boost::function<void (ControllerCommand *)> registrator =
+      (boost::bind(&Registry<ControllerCommand>::registerInstance, boost::ref(ControllerCommands), _1));
+  registrator(new ControllerCommand("checkresults",
       boost::assign::list_of< ControllerCommand::commands_t >
         (boost::bind(&FragmentController::checkResults,
-            boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport)))
+            boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
         (boost::bind(&printJobStatus,
             boost::bind(&FragmentController::getJobStatus, boost::ref(controller))))
       ));
-  ControllerCommands.registerInstance(new ControllerCommand("receiveresults",
-      boost::assign::list_of< ControllerCommand::commands_t >
-        (boost::bind(&FragmentController::receiveResults,
-            boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport)))
-        (boost::bind(&printReceivedResults,
-            boost::bind(&FragmentController::getReceivedResults, boost::ref(controller))))
-      ));
-  ControllerCommands.registerInstance(new ControllerCommand("receivempqc",
-      boost::assign::list_of< ControllerCommand::commands_t >
-        (boost::bind(&FragmentController::receiveResults,
-            boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport)))
-        (boost::bind(&printReceivedMPQCResults,
-            boost::bind(&FragmentController::getReceivedResults, boost::ref(controller)),
-            boost::cref(ControllerInfo.fragmentpath),
-            boost::bind(&getNoAtomsFromAdjacencyFile, boost::cref(ControllerInfo.fragmentpath))))
-      ));
-  ControllerCommands.registerInstance(new ControllerCommand("removeall",
+  registrator(new ControllerCommand("removeall",
       boost::assign::list_of< ControllerCommand::commands_t >
         (boost::bind(&FragmentController::removeall,
-            boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport)))
+            boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
       ));
-  ControllerCommands.registerInstance(new ControllerCommand("shutdown",
+  registrator(new ControllerCommand("shutdown",
       boost::assign::list_of< ControllerCommand::commands_t >
         (boost::bind(&FragmentController::shutdown,
-            boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport)))
+            boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
       ));
-  std::vector<std::string> Commands;
-  for (ControllerCommandRegistry::const_iterator iter = ControllerCommands.getBeginIter();
-      iter != ControllerCommands.getEndIter(); ++iter)
-    Commands.push_back(iter->first);
+  addSpecificCommands(registrator, controller, *ControllerInfo, vm);
 
   // Declare the supported options.
@@ -383,10 +111,7 @@
       ("verbosity,v", boost::program_options::value<size_t>(), "set verbosity level")
       ("server", boost::program_options::value< std::string >(), "connect to server at this address (host:port)")
-      ("command", boost::program_options::value< std::string >(), (std::string("command to send to server: ")+toString(Commands)).c_str())
-      ("executable", boost::program_options::value< std::string >(), "executable for commands 'addjobs' and 'createjobs'")
-      ("fragment-path", boost::program_options::value< std::string >(), "path to fragment files for 'receivempqc'")
-      ("jobcommand", boost::program_options::value< std::string >(), "command argument for executable for 'createjobs'")
-      ("jobfiles", boost::program_options::value< std::vector< std::string > >()->multitoken(), "list of files as single argument to executable for 'addjobs'")
+      ("command", boost::program_options::value< std::string >(), (std::string("command to send to server: ")+toString(getListOfCommands(ControllerCommands))).c_str())
   ;
+  addSpecificOptions(desc.add_options());
 
   // parse command line
@@ -396,29 +121,23 @@
   // set controller information
   int status = 0;
-  status = ControllerInfo.parseHelp(vm, desc);
+  status = ControllerInfo->parseHelp(vm, desc);
   if (status) return status;
-  status = ControllerInfo.parseVerbosity(vm);
+  status = ControllerInfo->parseVerbosity(vm);
   if (status) return status;
-  status = ControllerInfo.parseServerPort(vm);
+  status = ControllerInfo->parseServerPort(vm);
   if (status) return status;
-  status = ControllerInfo.parseCommand(vm, Commands);
+  status = ControllerInfo->parseCommand(vm, getListOfCommands(ControllerCommands));
   if (status) return status;
 
   // all later parse functions depend on parsed command
-  status = ControllerInfo.parseExecutable(vm);
-  if (status) return status;
-  status = ControllerInfo.parseJobCommand(vm);
-  if (status) return status;
-  status = ControllerInfo.parseFragmentpath(vm);
-  if (status) return status;
-  status = ControllerInfo.parseJobfiles(vm);
+  status = addOtherParsings(*ControllerInfo, vm, ControllerCommands);
   if (status) return status;
 
   // parse given ControllerCommand
-  if(!ControllerCommands.isPresentByName(ControllerInfo.command)) {
-    ELOG(1, "Unrecognized command '"+toString(ControllerInfo.command)+"'.");
+  if(!ControllerCommands.isPresentByName(ControllerInfo->command)) {
+    ELOG(1, "Unrecognized command '"+toString(ControllerInfo->command)+"'.");
     return 255;
   }
-  const ControllerCommand *commands = ControllerCommands.getByName(ControllerInfo.command);
+  const ControllerCommand *commands = ControllerCommands.getByName(ControllerInfo->command);
   try
   {
@@ -440,4 +159,5 @@
     std::cerr << e.what() << std::endl;
   }
+  delete ControllerInfo;
 
   return Exitflag;
Index: src/controller_MPQCCommandJob.cpp
===================================================================
--- src/controller_MPQCCommandJob.cpp	(revision cbcbbd63e97a2e60704da03bab709f5377d591aa)
+++ src/controller_MPQCCommandJob.cpp	(revision cbcbbd63e97a2e60704da03bab709f5377d591aa)
@@ -0,0 +1,369 @@
+/*
+ * Project: MoleCuilder
+ * Description: creates and alters molecular systems
+ * Copyright (C)  2012 University of Bonn. All rights reserved.
+ * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
+ */
+
+/*
+ * controller_MPQCCommandJob.cpp
+ *
+ *  Created on: Jun 6, 2012
+ *      Author: heber
+ */
+
+
+// include config.h
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+// boost asio needs specific operator new
+#include <boost/asio.hpp>
+
+#include "CodePatterns/MemDebug.hpp"
+
+#include "controller_MPQCCommandJob.hpp"
+
+#include <boost/assign.hpp>
+#include <boost/bind.hpp>
+#include <fstream>
+
+#include "FragmentController.hpp"
+#include "Jobs/SystemCommandJob.hpp"
+#include "Results/FragmentResult.hpp"
+
+#include "ControllerCommand.hpp"
+#include "ControllerCommandRegistry.hpp"
+#include "ControllerOptions_MPQCCommandJob.hpp"
+
+#include "Fragmentation/EnergyMatrix.hpp"
+#include "Fragmentation/ForceMatrix.hpp"
+#include "Fragmentation/KeySetsContainer.hpp"
+#include "Fragmentation/defs.hpp"
+
+#include "Helpers/defs.hpp"
+
+#include "Jobs/MPQCCommandJob.hpp"
+#include "Jobs/MPQCCommandJob_MPQCData.hpp"
+
+/** Creates a MPQCCommandJob with argument \a filename.
+ *
+ * @param jobs created job is added to this vector
+ * @param command mpqc command to execute
+ * @param filename filename being argument to job
+ * @param nextid id for this job
+ */
+void parsejob(
+    std::vector<FragmentJob::ptr> &jobs,
+    const std::string &command,
+    const std::string &filename,
+    const JobId_t nextid)
+{
+  std::ifstream file;
+  file.open(filename.c_str());
+  ASSERT( file.good(), "parsejob() - file "+filename+" does not exist.");
+  std::string output((std::istreambuf_iterator<char>(file)),
+      std::istreambuf_iterator<char>());
+  FragmentJob::ptr testJob( new MPQCCommandJob(output, nextid, command) );
+  jobs.push_back(testJob);
+  file.close();
+  LOG(1, "INFO: Added MPQCCommandJob from file "+filename+".");
+}
+
+/** Print received results.
+ *
+ * @param results received results to print
+ */
+void printReceivedResults(const std::vector<FragmentResult::ptr> &results)
+{
+  for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
+      iter != results.end(); ++iter)
+    LOG(1, "RESULT: job #"+toString((*iter)->getId())+": "+toString((*iter)->result));
+}
+
+/** Print MPQCData from received results.
+ *
+ * @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(
+    const std::vector<FragmentResult::ptr> &results,
+    const std::string &KeySetFilename,
+    size_t NoAtoms)
+{
+  EnergyMatrix Energy;
+  EnergyMatrix EnergyFragments;
+  ForceMatrix Force;
+  ForceMatrix ForceFragments;
+  KeySetsContainer KeySet;
+
+  // 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;
+
+  LOG(2, "DEBUG: Parsing now through " << results.size() << " results.");
+  for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
+      iter != results.end(); ++iter) {
+    LOG(1, "RESULT: job #"+toString((*iter)->getId())+": "+toString((*iter)->result));
+    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;
+
+  if (!Force.ParseIndices(KeySetFilename.c_str())) return false;
+
+  if (!KeySet.ParseKeySets(KeySetFilename.c_str(), Force.RowCounter, Force.MatrixCounter)) return false;
+
+  if (!KeySet.ParseManyBodyTerms()) return false;
+
+  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;
+
+  if(!Energy.SetLastMatrix(0., 0)) return false;
+  if(!Force.SetLastMatrix(0., 2)) return false;
+
+  for (int BondOrder=0;BondOrder<KeySet.Order;BondOrder++) {
+    // --------- sum up energy --------------------
+    LOG(1, "INFO: Summing energy of order " << BondOrder+1 << " ...");
+    if (!EnergyFragments.SumSubManyBodyTerms(Energy, KeySet, BondOrder)) return false;
+    if (!Energy.SumSubEnergy(EnergyFragments, NULL, KeySet, BondOrder, 1.)) return false;
+
+    // --------- sum up Forces --------------------
+    LOG(1, "INFO: Summing forces of order " << BondOrder+1 << " ...");
+    if (!ForceFragments.SumSubManyBodyTerms(Force, KeySet, BondOrder)) return false;
+    if (!Force.SumSubForces(ForceFragments, KeySet, BondOrder, 1.)) return false;
+  }
+
+  // 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;
+}
+
+/** Creates a SystemCommandJob out of give \a command with \a argument.
+ *
+ * @param controller reference to controller to add jobs
+ * @param ControllerInfo information on the job
+ */
+void createJobs(FragmentController &controller, const ControllerOptions_MPQCCommandJob &ControllerInfo)
+{
+  const JobId_t next_id = controller.getAvailableId();
+  FragmentJob::ptr testJob(
+      new SystemCommandJob(ControllerInfo.executable, ControllerInfo.jobcommand, next_id) );
+  std::vector<FragmentJob::ptr> jobs;
+  jobs.push_back(testJob);
+  controller.addJobs(jobs);
+  controller.sendJobs(ControllerInfo.server, ControllerInfo.serverport);
+  LOG(1, "INFO: Added one SystemCommandJob.");
+}
+
+/** Creates a MPQCCommandJob out of give \a command with \a argument.
+ *
+ * @param controller reference to controller to add jobs
+ * @param ControllerInfo information on the job
+ */
+void AddJobs(FragmentController &controller, const ControllerOptions_MPQCCommandJob &ControllerInfo)
+{
+  std::vector<FragmentJob::ptr> jobs;
+  for (std::vector< std::string >::const_iterator iter = ControllerInfo.jobfiles.begin();
+      iter != ControllerInfo.jobfiles.end(); ++iter) {
+    const JobId_t next_id = controller.getAvailableId();
+    const std::string &filename = *iter;
+    LOG(1, "INFO: Creating MPQCCommandJob with filename '"
+        +filename+"', and id "+toString(next_id)+".");
+    parsejob(jobs, ControllerInfo.executable, filename, next_id);
+  }
+  controller.addJobs(jobs);
+  controller.sendJobs(ControllerInfo.server, ControllerInfo.serverport);
+}
+
+ControllerOptions *allocateControllerInfo()
+{
+  return new ControllerOptions_MPQCCommandJob();
+}
+
+void addSpecificCommands(
+    boost::function<void (ControllerCommand *)> &registrator,
+    FragmentController &controller,
+    ControllerOptions &ControllerInfo,
+    boost::program_options::variables_map &vm)
+{
+  ControllerOptions_MPQCCommandJob &CI =
+      reinterpret_cast<ControllerOptions_MPQCCommandJob &>(ControllerInfo);
+  registrator(new ControllerCommand("addjobs",
+      boost::assign::list_of< ControllerCommand::commands_t >
+      (boost::bind(&FragmentController::requestIds,
+          boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport),
+          boost::bind(&std::vector<std::string>::size, boost::cref(CI.jobfiles))))
+      (boost::bind(&AddJobs, boost::ref(controller), boost::cref(CI)))
+      ));
+  registrator(new ControllerCommand("createjobs",
+      boost::assign::list_of< ControllerCommand::commands_t >
+        (boost::bind(&FragmentController::requestIds,
+            boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport), 1))
+        (boost::bind(&createJobs, boost::ref(controller), boost::cref(CI)))
+      ));
+  registrator(new ControllerCommand("receivempqc",
+      boost::assign::list_of< ControllerCommand::commands_t >
+        (boost::bind(&FragmentController::receiveResults,
+            boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport)))
+        (boost::bind(&printReceivedMPQCResults,
+            boost::bind(&FragmentController::getReceivedResults, boost::ref(controller)),
+            boost::cref(CI.fragmentpath),
+            boost::bind(&getNoAtomsFromAdjacencyFile, boost::cref(CI.fragmentpath))))
+      ));
+  registrator(new ControllerCommand("receiveresults",
+      boost::assign::list_of< ControllerCommand::commands_t >
+        (boost::bind(&FragmentController::receiveResults,
+            boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport)))
+        (boost::bind(&printReceivedResults,
+            boost::bind(&FragmentController::getReceivedResults, boost::ref(controller))))
+      ));
+}
+
+void addSpecificOptions(
+    boost::program_options::options_description_easy_init option)
+{
+  option
+    ("executable", boost::program_options::value< std::string >(), "executable for commands 'addjobs' and 'createjobs'")
+    ("jobcommand", boost::program_options::value< std::string >(), "command argument for executable for 'createjobs'")
+    ("fragment-path", boost::program_options::value< std::string >(), "path to fragment files for 'receivempqc'")
+    ("jobfiles", boost::program_options::value< std::vector< std::string > >()->multitoken(), "list of files as single argument to executable for 'addjobs'")
+    ;
+}
+
+int addOtherParsings(
+    ControllerOptions &ControllerInfo,
+    boost::program_options::variables_map &vm,
+    const ControllerCommandRegistry &ControllerCommands)
+{
+  ControllerOptions_MPQCCommandJob &CI =
+      reinterpret_cast<ControllerOptions_MPQCCommandJob &>(ControllerInfo);
+
+  int status = 0;
+  status = CI.parseExecutable(vm);
+  if (status) return status;
+  status = CI.parseJobCommand(vm);
+  if (status) return status;
+  status = CI.parseFragmentpath(vm);
+  if (status) return status;
+  status = CI.parseJobfiles(vm);
+  return status;
+}
Index: src/controller_MPQCCommandJob.hpp
===================================================================
--- src/controller_MPQCCommandJob.hpp	(revision cbcbbd63e97a2e60704da03bab709f5377d591aa)
+++ src/controller_MPQCCommandJob.hpp	(revision cbcbbd63e97a2e60704da03bab709f5377d591aa)
@@ -0,0 +1,44 @@
+/*
+ * controller_MPQCCommandJob.hpp
+ *
+ *  Created on: Jun 6, 2012
+ *      Author: heber
+ */
+
+#ifndef CONTROLLER_MPQCCOMMANDJOB_HPP_
+#define CONTROLLER_MPQCCOMMANDJOB_HPP_
+
+
+// include config.h
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+
+#include <boost/function.hpp>
+#include <boost/program_options.hpp>
+
+class ControllerCommand;
+class ControllerCommandRegistry;
+class ControllerInfo;
+class ControllerOptions;
+class FragmentController;
+
+ControllerOptions *allocateControllerInfo();
+
+void addSpecificCommands(
+    boost::function<void (ControllerCommand *)> &registrator,
+    FragmentController &controller,
+    ControllerOptions &ControllerInfo,
+    boost::program_options::variables_map &vm);
+
+void addSpecificOptions(
+    boost::program_options::options_description_easy_init option);
+
+int addOtherParsings(
+    ControllerOptions &ControllerInfo,
+    boost::program_options::variables_map &vm,
+    const ControllerCommandRegistry &ControllerCommands);
+
+
+#endif /* CONTROLLER_MPQCCOMMANDJOB_HPP_ */
Index: src/controller_SystemCommandJob.cpp
===================================================================
--- src/controller_SystemCommandJob.cpp	(revision cbcbbd63e97a2e60704da03bab709f5377d591aa)
+++ src/controller_SystemCommandJob.cpp	(revision cbcbbd63e97a2e60704da03bab709f5377d591aa)
@@ -0,0 +1,115 @@
+/*
+ * Project: MoleCuilder
+ * Description: creates and alters molecular systems
+ * Copyright (C)  2012 University of Bonn. All rights reserved.
+ * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
+ */
+
+/*
+ * controller_SystemCommandJob.cpp
+ *
+ *  Created on: 01.06.2012
+ *      Author: heber
+ */
+
+// include config.h
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "CodePatterns/MemDebug.hpp"
+
+#include "controller_SystemCommandJob.hpp"
+
+#include <boost/assign.hpp>
+#include <boost/bind.hpp>
+
+#include "FragmentController.hpp"
+#include "Jobs/SystemCommandJob.hpp"
+#include "Results/FragmentResult.hpp"
+
+#include "ControllerCommand.hpp"
+#include "ControllerCommandRegistry.hpp"
+#include "ControllerOptions_SystemCommandJob.hpp"
+
+/** Print received results.
+ *
+ * @param results received results to print
+ */
+void printReceivedResults(const std::vector<FragmentResult::ptr> &results)
+{
+  for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
+      iter != results.end(); ++iter)
+    LOG(1, "RESULT: job #"+toString((*iter)->getId())+": "+toString((*iter)->result));
+}
+
+/** Creates a SystemCommandJob out of give \a command with \a argument.
+ *
+ * @param controller reference to controller to add jobs
+ * @param ControllerInfo information on the job
+ */
+void createJobs(FragmentController &controller, const ControllerOptions_SystemCommandJob &ControllerInfo)
+{
+  const JobId_t next_id = controller.getAvailableId();
+  LOG(2, "DEBUG: Creating new SystemCommandJob with '"
+      +toString(ControllerInfo.executable)+"' and argument '"
+      +toString(ControllerInfo.jobcommand)+"'.");
+  FragmentJob::ptr testJob(
+      new SystemCommandJob(ControllerInfo.executable, ControllerInfo.jobcommand, next_id) );
+  std::vector<FragmentJob::ptr> jobs;
+  jobs.push_back(testJob);
+  controller.addJobs(jobs);
+  controller.sendJobs(ControllerInfo.server, ControllerInfo.serverport);
+  LOG(1, "INFO: Added one SystemCommandJob.");
+}
+
+ControllerOptions *allocateControllerInfo()
+{
+  return new ControllerOptions_SystemCommandJob();
+}
+
+void addSpecificCommands(
+    boost::function<void (ControllerCommand *)> &registrator,
+    FragmentController &controller,
+    ControllerOptions &ControllerInfo,
+    boost::program_options::variables_map &vm)
+{
+  ControllerOptions_SystemCommandJob &CI =
+      reinterpret_cast<ControllerOptions_SystemCommandJob &>(ControllerInfo);
+  registrator(new ControllerCommand("createjobs",
+      boost::assign::list_of< ControllerCommand::commands_t >
+        (boost::bind(&FragmentController::requestIds,
+            boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport), 1))
+        (boost::bind(&createJobs, boost::ref(controller), boost::cref(CI)))
+      ));
+  registrator(new ControllerCommand("receiveresults",
+      boost::assign::list_of< ControllerCommand::commands_t >
+        (boost::bind(&FragmentController::receiveResults,
+            boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport)))
+        (boost::bind(&printReceivedResults,
+            boost::bind(&FragmentController::getReceivedResults, boost::ref(controller))))
+      ));
+}
+
+void addSpecificOptions(
+    boost::program_options::options_description_easy_init option)
+{
+  option
+    ("jobcommand", boost::program_options::value< std::string >(), "command argument for executable for 'createjobs'")
+    ("executable", boost::program_options::value< std::string >(), "executable for commands 'createjobs'")
+    ;
+}
+
+int addOtherParsings(
+    ControllerOptions &ControllerInfo,
+    boost::program_options::variables_map &vm,
+    const ControllerCommandRegistry &ControllerCommands)
+{
+  ControllerOptions_SystemCommandJob &CI =
+      reinterpret_cast<ControllerOptions_SystemCommandJob &>(ControllerInfo);
+  int status = 0;
+  status = CI.parseExecutable(vm);
+  if (status) return status;
+  status = CI.parseJobCommand(vm);
+  return status;
+}
Index: src/controller_SystemCommandJob.hpp
===================================================================
--- src/controller_SystemCommandJob.hpp	(revision cbcbbd63e97a2e60704da03bab709f5377d591aa)
+++ src/controller_SystemCommandJob.hpp	(revision cbcbbd63e97a2e60704da03bab709f5377d591aa)
@@ -0,0 +1,41 @@
+/*
+ * controller_SystemCommandJob.hpp
+ *
+ *  Created on: 01.06.2012
+ *      Author: heber
+ */
+
+#ifndef CONTROLLER_SYSTEMCOMMANDJOB_HPP_
+#define CONTROLLER_SYSTEMCOMMANDJOB_HPP_
+
+// include config.h
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <boost/function.hpp>
+#include <boost/program_options.hpp>
+
+class ControllerCommand;
+class ControllerCommandRegistry;
+class ControllerInfo;
+class ControllerOptions;
+class FragmentController;
+
+ControllerOptions *allocateControllerInfo();
+
+void addSpecificCommands(
+    boost::function<void (ControllerCommand *)> &registrator,
+    FragmentController &controller,
+    ControllerOptions &ControllerInfo,
+    boost::program_options::variables_map &vm);
+
+void addSpecificOptions(
+    boost::program_options::options_description_easy_init option);
+
+int addOtherParsings(
+    ControllerOptions &ControllerInfo,
+    boost::program_options::variables_map &vm,
+    const ControllerCommandRegistry &ControllerCommands);
+
+#endif /* CONTROLLER_SYSTEMCOMMANDJOB_HPP_ */
