| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * 
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *   This file is part of MoleCuilder.
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 12 |  *    (at your option) any later version.
 | 
|---|
| 13 |  *
 | 
|---|
| 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |  *    GNU General Public License for more details.
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * MPQCCommandJob.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Feb 05, 2012
 | 
|---|
| 27 |  *      Author: heber
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | // include headers that implement a archive in simple text format
 | 
|---|
| 38 | // otherwise BOOST_CLASS_EXPORT_IMPLEMENT has no effect
 | 
|---|
| 39 | #include <boost/archive/text_oarchive.hpp>
 | 
|---|
| 40 | #include <boost/archive/text_iarchive.hpp>
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #include "MPQCCommandJob.hpp"
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #include <boost/algorithm/string.hpp>
 | 
|---|
| 45 | #include <boost/lexical_cast.hpp>
 | 
|---|
| 46 | #include <boost/tokenizer.hpp>
 | 
|---|
| 47 | #include <sstream>
 | 
|---|
| 48 | 
 | 
|---|
| 49 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 50 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 51 | 
 | 
|---|
| 52 | #include "LinearAlgebra/defs.hpp"
 | 
|---|
| 53 | 
 | 
|---|
| 54 | #include "Fragmentation/Summation/SetValues/FragmentForces.hpp"
 | 
|---|
| 55 | 
 | 
|---|
| 56 | const std::string MPQCCommandJob::keyword_hartreefock_energy("total scf energy");
 | 
|---|
| 57 | const std::string MPQCCommandJob::keyword_moellerplesset_energy("MP2 energy [au]:");
 | 
|---|
| 58 | const std::string MPQCCommandJob::keyword_hartreefock_forces("Total Gradient");
 | 
|---|
| 59 | const std::string MPQCCommandJob::keyword_moellerplesset_forces("Total MP2 gradient [au]:");
 | 
|---|
| 60 | 
 | 
|---|
| 61 | /** Default constructor for class MPQCCommandJob.
 | 
|---|
| 62 |  *
 | 
|---|
| 63 |  */
 | 
|---|
| 64 | MPQCCommandJob::MPQCCommandJob()
 | 
|---|
| 65 | {}
 | 
|---|
| 66 | 
 | 
|---|
| 67 | /** Constructor for class MPQCCommandJob.
 | 
|---|
| 68 |  *
 | 
|---|
| 69 |  * @param _inputfile string of inputfile contents
 | 
|---|
| 70 |  * @param _JobId id of this job
 | 
|---|
| 71 |  * @param command mpqc command, "mpqc" as default
 | 
|---|
| 72 |  */
 | 
|---|
| 73 | MPQCCommandJob::MPQCCommandJob(
 | 
|---|
| 74 |     const std::string &_inputfile,
 | 
|---|
| 75 |     const JobId_t _JobId,
 | 
|---|
| 76 |     const std::string &command) :
 | 
|---|
| 77 |     SystemCommandJob(command, _inputfile, _JobId)
 | 
|---|
| 78 | {}
 | 
|---|
| 79 | 
 | 
|---|
| 80 | /** Destructor for class MPQCCommandJob.
 | 
|---|
| 81 |  *
 | 
|---|
| 82 |  */
 | 
|---|
| 83 | MPQCCommandJob::~MPQCCommandJob()
 | 
|---|
| 84 | {}
 | 
|---|
| 85 | 
 | 
|---|
| 86 | /** Extracts energy and forces from the output of the command.
 | 
|---|
| 87 |  *
 | 
|---|
| 88 |  * @param resultstring output of the command to parse
 | 
|---|
| 89 |  * @return FragmentResult with energy and forces
 | 
|---|
| 90 |  */
 | 
|---|
| 91 | FragmentResult::ptr MPQCCommandJob::extractResult(const std::string &resultstring)
 | 
|---|
| 92 | {
 | 
|---|
| 93 |   std::stringstream returnstream;
 | 
|---|
| 94 |   // tokenizers
 | 
|---|
| 95 |   typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
 | 
|---|
| 96 |   boost::char_separator<char> keyvalue_separator("=:");
 | 
|---|
| 97 |   boost::char_separator<char> whitespace(" \t");
 | 
|---|
| 98 | 
 | 
|---|
| 99 |   // split into lines
 | 
|---|
| 100 |   std::vector<std::string> lines;
 | 
|---|
| 101 |   boost::split(lines, resultstring, boost::is_any_of("\n"));
 | 
|---|
| 102 | 
 | 
|---|
| 103 |   // go through each line
 | 
|---|
| 104 |   bool gradient_section = false;
 | 
|---|
| 105 |   for (std::vector<std::string>::const_iterator iter = lines.begin();
 | 
|---|
| 106 |       iter != lines.end();++iter) {
 | 
|---|
| 107 |     LOG(3, "DEBUG: Current line is '" << *iter << "'.");
 | 
|---|
| 108 |     if (gradient_section) {
 | 
|---|
| 109 |       tokenizer tokens(*iter, whitespace);
 | 
|---|
| 110 |       if (*iter != std::string("")) {
 | 
|---|
| 111 |         tokenizer::iterator tok_iter = tokens.begin();
 | 
|---|
| 112 |         tok_iter++;
 | 
|---|
| 113 |         tok_iter++;
 | 
|---|
| 114 |         FragmentForces::force_t forcevector(NDIM);
 | 
|---|
| 115 |         try { // first col is index, second is element
 | 
|---|
| 116 |           for (size_t index = 0;index < NDIM; ++index)
 | 
|---|
| 117 |             forcevector[index] = boost::lexical_cast<double>(*(tok_iter++));
 | 
|---|
| 118 |           LOG(2, "INFO: Recognized force vector in '"+*iter+"'.");
 | 
|---|
| 119 |           data.forces.push_back( forcevector );
 | 
|---|
| 120 |         } catch(boost::bad_lexical_cast){
 | 
|---|
| 121 |           LOG(2, "INFO: Did not recognize a force vector, hence ending section at '"+*iter+"'.");
 | 
|---|
| 122 |           gradient_section = false;
 | 
|---|
| 123 |         }
 | 
|---|
| 124 |       } else {
 | 
|---|
| 125 |         LOG(2, "INFO: Recognized gradient section end in '"+*iter+"'.");
 | 
|---|
| 126 |         gradient_section = false;
 | 
|---|
| 127 |       }
 | 
|---|
| 128 |     }
 | 
|---|
| 129 |     // extract energy ("total scf energy") ...
 | 
|---|
| 130 |     if (((*iter).find(keyword_hartreefock_energy) != std::string::npos)
 | 
|---|
| 131 |         || ((*iter).find(keyword_moellerplesset_energy) != std::string::npos))
 | 
|---|
| 132 |     {
 | 
|---|
| 133 |       LOG(2, "INFO: Recognized energy in '"+*iter+"'.");
 | 
|---|
| 134 |       tokenizer tokens(*iter, keyvalue_separator);
 | 
|---|
| 135 |       tokenizer::iterator tok_iter = tokens.begin();
 | 
|---|
| 136 |       tok_iter++;
 | 
|---|
| 137 |       std::stringstream energystring(*tok_iter);
 | 
|---|
| 138 |       energystring >> data.energies.total;
 | 
|---|
| 139 |     }
 | 
|---|
| 140 |     // ... and forces ("Total Gradient") from resultstring
 | 
|---|
| 141 |     if (((*iter).find(keyword_hartreefock_forces) != std::string::npos)
 | 
|---|
| 142 |         || ((*iter).find(keyword_moellerplesset_forces) != std::string::npos))
 | 
|---|
| 143 |     {
 | 
|---|
| 144 |       LOG(2, "INFO: Recognized gradient section init in '"+*iter+"'.");
 | 
|---|
| 145 |       gradient_section=true;
 | 
|---|
| 146 |       data.forces.clear();
 | 
|---|
| 147 |     }
 | 
|---|
| 148 |   }
 | 
|---|
| 149 | 
 | 
|---|
| 150 |   // place into returnstream
 | 
|---|
| 151 |   {
 | 
|---|
| 152 |     const MPQCData archivedata(data);
 | 
|---|
| 153 |     boost::archive::text_oarchive oa(returnstream);
 | 
|---|
| 154 |     oa << archivedata;
 | 
|---|
| 155 |   }
 | 
|---|
| 156 | 
 | 
|---|
| 157 |   FragmentResult::ptr s( new FragmentResult(getId(), returnstream.str()) );
 | 
|---|
| 158 |   return s;
 | 
|---|
| 159 | }
 | 
|---|
| 160 | 
 | 
|---|
| 161 | bool MPQCCommandJob::operator==(const MPQCCommandJob &other) const
 | 
|---|
| 162 | {
 | 
|---|
| 163 |   if (data != other.data) {
 | 
|---|
| 164 |     LOG(2, "INFO: data's of both MPQCCommandJob don't match.");
 | 
|---|
| 165 |     return false;
 | 
|---|
| 166 |   }
 | 
|---|
| 167 |   return (static_cast<const SystemCommandJob &>(*this)
 | 
|---|
| 168 |       == static_cast<const SystemCommandJob &>(other));
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|
| 171 | 
 | 
|---|
| 172 | // we need to explicitly instantiate the serialization functions as
 | 
|---|
| 173 | // its is only serialized through its base class FragmentJob
 | 
|---|
| 174 | BOOST_CLASS_EXPORT_IMPLEMENT(MPQCCommandJob)
 | 
|---|