[cc276e] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2012 University of Bonn. All rights reserved.
|
---|
[94d5ac6] | 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/>.
|
---|
[cc276e] | 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 |
|
---|
[9eb71b3] | 35 | //#include "CodePatterns/MemDebug.hpp"
|
---|
[cc276e] | 36 |
|
---|
[545446] | 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 |
|
---|
[cc276e] | 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 |
|
---|
[004d5c] | 52 | #include "LinearAlgebra/defs.hpp"
|
---|
| 53 |
|
---|
[20bb3b] | 54 | #include "Fragmentation/Summation/SetValues/FragmentForces.hpp"
|
---|
| 55 |
|
---|
[306f60] | 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]:");
|
---|
[cc276e] | 60 |
|
---|
[545446] | 61 | /** Default constructor for class MPQCCommandJob.
|
---|
| 62 | *
|
---|
| 63 | */
|
---|
| 64 | MPQCCommandJob::MPQCCommandJob()
|
---|
| 65 | {}
|
---|
| 66 |
|
---|
[cc276e] | 67 | /** Constructor for class MPQCCommandJob.
|
---|
| 68 | *
|
---|
| 69 | * @param _inputfile string of inputfile contents
|
---|
| 70 | * @param _JobId id of this job
|
---|
[917be8] | 71 | * @param command mpqc command, "mpqc" as default
|
---|
[cc276e] | 72 | */
|
---|
[917be8] | 73 | MPQCCommandJob::MPQCCommandJob(
|
---|
| 74 | const std::string &_inputfile,
|
---|
| 75 | const JobId_t _JobId,
|
---|
| 76 | const std::string &command) :
|
---|
| 77 | SystemCommandJob(command, _inputfile, _JobId)
|
---|
[cc276e] | 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;
|
---|
[306f60] | 96 | boost::char_separator<char> keyvalue_separator("=:");
|
---|
[cc276e] | 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) {
|
---|
[306f60] | 107 | LOG(3, "DEBUG: Current line is '" << *iter << "'.");
|
---|
[cc276e] | 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++;
|
---|
[20bb3b] | 114 | FragmentForces::force_t forcevector(NDIM);
|
---|
[cc276e] | 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") ...
|
---|
[306f60] | 130 | if (((*iter).find(keyword_hartreefock_energy) != std::string::npos)
|
---|
| 131 | || ((*iter).find(keyword_moellerplesset_energy) != std::string::npos))
|
---|
| 132 | {
|
---|
[cc276e] | 133 | LOG(2, "INFO: Recognized energy in '"+*iter+"'.");
|
---|
[306f60] | 134 | tokenizer tokens(*iter, keyvalue_separator);
|
---|
[cc276e] | 135 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
| 136 | tok_iter++;
|
---|
| 137 | std::stringstream energystring(*tok_iter);
|
---|
[a9558f] | 138 | energystring >> data.energies.total;
|
---|
[cc276e] | 139 | }
|
---|
| 140 | // ... and forces ("Total Gradient") from resultstring
|
---|
[306f60] | 141 | if (((*iter).find(keyword_hartreefock_forces) != std::string::npos)
|
---|
| 142 | || ((*iter).find(keyword_moellerplesset_forces) != std::string::npos))
|
---|
| 143 | {
|
---|
[cc276e] | 144 | LOG(2, "INFO: Recognized gradient section init in '"+*iter+"'.");
|
---|
| 145 | gradient_section=true;
|
---|
[306f60] | 146 | data.forces.clear();
|
---|
[cc276e] | 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | // place into returnstream
|
---|
[ae68b5] | 151 | {
|
---|
| 152 | const MPQCData archivedata(data);
|
---|
| 153 | boost::archive::text_oarchive oa(returnstream);
|
---|
| 154 | oa << archivedata;
|
---|
| 155 | }
|
---|
[cc276e] | 156 |
|
---|
| 157 | FragmentResult::ptr s( new FragmentResult(getId(), returnstream.str()) );
|
---|
| 158 | return s;
|
---|
| 159 | }
|
---|
[545446] | 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)
|
---|