| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * MpqcParser.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: 12.06.2010
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include <iostream>
 | 
|---|
| 21 | #include <boost/tokenizer.hpp>
 | 
|---|
| 22 | #include <string>
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include "MpqcParser.hpp"
 | 
|---|
| 27 | #include "MpqcParser_Parameters.hpp"
 | 
|---|
| 28 | 
 | 
|---|
| 29 | #include "atom.hpp"
 | 
|---|
| 30 | #include "config.hpp"
 | 
|---|
| 31 | #include "Element/element.hpp"
 | 
|---|
| 32 | #include "molecule.hpp"
 | 
|---|
| 33 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 34 | #include "CodePatterns/toString.hpp"
 | 
|---|
| 35 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 36 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 37 | #include "Element/periodentafel.hpp"
 | 
|---|
| 38 | #include "World.hpp"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | 
 | 
|---|
| 41 | /** Constructor of MpqcParser.
 | 
|---|
| 42 |  *
 | 
|---|
| 43 |  */
 | 
|---|
| 44 | MpqcParser::MpqcParser()
 | 
|---|
| 45 | {
 | 
|---|
| 46 |   parameters = new MpqcParser_Parameters();
 | 
|---|
| 47 | }
 | 
|---|
| 48 | 
 | 
|---|
| 49 | /** Destructor of MpqcParser.
 | 
|---|
| 50 |  *
 | 
|---|
| 51 |  */
 | 
|---|
| 52 | MpqcParser::~MpqcParser()
 | 
|---|
| 53 | {
 | 
|---|
| 54 |   delete parameters;
 | 
|---|
| 55 | }
 | 
|---|
| 56 | 
 | 
|---|
| 57 | /** Load an MPQC config file into the World.
 | 
|---|
| 58 |  * \param *file input stream
 | 
|---|
| 59 |  */
 | 
|---|
| 60 | void MpqcParser::load(istream *file)
 | 
|---|
| 61 | {
 | 
|---|
| 62 |   bool MpqcSection = false;
 | 
|---|
| 63 |   bool MoleculeSection = false;
 | 
|---|
| 64 |   bool GeometrySection = false;
 | 
|---|
| 65 |   bool BasisSection = false;
 | 
|---|
| 66 |   bool AuxBasisSection = false;
 | 
|---|
| 67 |   char line[MAXSTRINGSIZE];
 | 
|---|
| 68 |   typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
 | 
|---|
| 69 |   boost::char_separator<char> sep("[]");
 | 
|---|
| 70 |   boost::char_separator<char> angularsep("<>");
 | 
|---|
| 71 |   boost::char_separator<char> equalitysep(" =");
 | 
|---|
| 72 |   boost::char_separator<char> whitesep(" \t");
 | 
|---|
| 73 |   ConvertTo<double> toDouble;
 | 
|---|
| 74 | 
 | 
|---|
| 75 |   molecule *newmol = World::getInstance().createMolecule();
 | 
|---|
| 76 |   newmol->ActiveFlag = true;
 | 
|---|
| 77 |   // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include
 | 
|---|
| 78 |   World::getInstance().getMolecules()->insert(newmol);
 | 
|---|
| 79 |   while (file->good()) {
 | 
|---|
| 80 |     file->getline(line, MAXSTRINGSIZE-1);
 | 
|---|
| 81 |     std::string linestring(line);
 | 
|---|
| 82 |     if ((linestring.find("atoms geometry") == string::npos) && (linestring.find("}") != string::npos)) {
 | 
|---|
| 83 |       GeometrySection = false;
 | 
|---|
| 84 |     }
 | 
|---|
| 85 |     if ((linestring.find(")") != string::npos)) { // ends a section which do not overlap
 | 
|---|
| 86 |       MpqcSection = false;
 | 
|---|
| 87 |       MoleculeSection = false;
 | 
|---|
| 88 |       BasisSection = false;
 | 
|---|
| 89 |       AuxBasisSection = false;
 | 
|---|
| 90 |     }
 | 
|---|
| 91 |     if (MoleculeSection) {
 | 
|---|
| 92 |       if (GeometrySection) { // we have an atom
 | 
|---|
| 93 |         tokenizer tokens(linestring, sep);
 | 
|---|
| 94 |   //      if (tokens.size() != 2)
 | 
|---|
| 95 |   //        throw MpqcParseException;
 | 
|---|
| 96 |         tokenizer::iterator tok_iter = tokens.begin();
 | 
|---|
| 97 |         ASSERT(tok_iter != tokens.end(),
 | 
|---|
| 98 |             "MpqcParser::load() - missing token for MoleculeSection in line "+linestring+"!");
 | 
|---|
| 99 |         std::stringstream whitespacefilter(*tok_iter++);
 | 
|---|
| 100 |         std::string element;
 | 
|---|
| 101 |         whitespacefilter >> ws >> element;
 | 
|---|
| 102 |         ASSERT(tok_iter != tokens.end(),
 | 
|---|
| 103 |             "MpqcParser::load() - missing token for MoleculeSection in line "+linestring+"!");
 | 
|---|
| 104 |         std::string vector = *tok_iter;
 | 
|---|
| 105 |         tokenizer vectorcomponents(vector, whitesep);
 | 
|---|
| 106 |         Vector X;
 | 
|---|
| 107 |   //      if (vectorcomponents.size() != NDIM)
 | 
|---|
| 108 |   //        throw MpqcParseException;
 | 
|---|
| 109 |         tok_iter = vectorcomponents.begin();
 | 
|---|
| 110 |         for (int i=0; i<NDIM; ++i) {
 | 
|---|
| 111 |           X[i] = toDouble(*tok_iter++);
 | 
|---|
| 112 |         }
 | 
|---|
| 113 |         // create atom
 | 
|---|
| 114 |         atom *newAtom = World::getInstance().createAtom();
 | 
|---|
| 115 |         newAtom->setType(World::getInstance().getPeriode()->FindElement(element));
 | 
|---|
| 116 |         newAtom->setPosition(X);
 | 
|---|
| 117 |         newmol->AddAtom(newAtom);
 | 
|---|
| 118 |         DoLog(1) && (Log() << Verbose(1) << "Adding atom " << *newAtom << std::endl);
 | 
|---|
| 119 |       }
 | 
|---|
| 120 |     }
 | 
|---|
| 121 |     if (MpqcSection) {
 | 
|---|
| 122 |       if (linestring.find("mole<") != string::npos) { // get theory
 | 
|---|
| 123 |         tokenizer tokens(linestring, angularsep);
 | 
|---|
| 124 |         tokenizer::iterator tok_iter = tokens.begin();
 | 
|---|
| 125 |         ++tok_iter;
 | 
|---|
| 126 |         ASSERT(tok_iter != tokens.end(),
 | 
|---|
| 127 |             "MpqcParser::load() - missing token in brackets<> for mole< in line "+linestring+"!");
 | 
|---|
| 128 |         std::string value(*tok_iter);
 | 
|---|
| 129 |         std::stringstream linestream("theory = "+value);
 | 
|---|
| 130 |         linestream >> getParams();
 | 
|---|
| 131 |       } else if (linestring.find("integrals<") != string::npos) { // get theory
 | 
|---|
| 132 |         tokenizer tokens(linestring, angularsep);
 | 
|---|
| 133 |         tokenizer::iterator tok_iter = tokens.begin();
 | 
|---|
| 134 |         ++tok_iter;
 | 
|---|
| 135 |         ASSERT(tok_iter != tokens.end(),
 | 
|---|
| 136 |             "MpqcParser::load() - missing token in brackets<> for integrals< in line "+linestring+"!");
 | 
|---|
| 137 |         std::string value(*tok_iter);
 | 
|---|
| 138 |         std::stringstream linestream("integration = "+value);
 | 
|---|
| 139 |         linestream >> getParams();
 | 
|---|
| 140 |       } else if ((linestring.find("molecule") == string::npos) && (linestring.find("basis") == string::npos)){
 | 
|---|
| 141 |         // molecule and basis must not be parsed in this section
 | 
|---|
| 142 |         tokenizer tokens(linestring, equalitysep);
 | 
|---|
| 143 |         tokenizer::iterator tok_iter = tokens.begin();
 | 
|---|
| 144 |         ASSERT(tok_iter != tokens.end(),
 | 
|---|
| 145 |             "MpqcParser::load() - missing token before '=' for MpqcSection in line "+linestring+"!");
 | 
|---|
| 146 |         std::stringstream whitespacefilter(*tok_iter);
 | 
|---|
| 147 |         std::string key;
 | 
|---|
| 148 |         whitespacefilter >> ws >> key;
 | 
|---|
| 149 |         if (getParams().haveParam(key)) {
 | 
|---|
| 150 |           std::stringstream linestream(linestring);
 | 
|---|
| 151 |           linestream >> getParams();
 | 
|---|
| 152 |         } else { // unknown keys are simply ignored as long as parser is incomplete
 | 
|---|
| 153 |           DoLog(2) && (Log() << Verbose(2) << "INFO: '"+key+"' is unknown and ignored." << std::endl);
 | 
|---|
| 154 |         }
 | 
|---|
| 155 |       }
 | 
|---|
| 156 |     }
 | 
|---|
| 157 |     if (BasisSection) {
 | 
|---|
| 158 |       tokenizer tokens(linestring, equalitysep);
 | 
|---|
| 159 |       tokenizer::iterator tok_iter = tokens.begin();
 | 
|---|
| 160 |       ASSERT(tok_iter != tokens.end(),
 | 
|---|
| 161 |           "MpqcParser::load() - missing token for BasisSection in line "+linestring+"!");
 | 
|---|
| 162 |       std::string key(*tok_iter++);
 | 
|---|
| 163 |       ASSERT(tok_iter != tokens.end(),
 | 
|---|
| 164 |           "MpqcParser::load() - missing value for BasisSection after key "+key+" in line "+linestring+"!");
 | 
|---|
| 165 |       std::string value(*tok_iter);
 | 
|---|
| 166 |       tok_iter++;
 | 
|---|
| 167 |       // TODO: use exception instead of ASSERT
 | 
|---|
| 168 |       ASSERT(tok_iter == tokens.end(),
 | 
|---|
| 169 |           "MpqcParser::load() - more than (key = value) on line "+linestring+".");
 | 
|---|
| 170 |       if (key == "name") {
 | 
|---|
| 171 |         std::stringstream linestream("basis = "+value);
 | 
|---|
| 172 |         linestream >> getParams();
 | 
|---|
| 173 |       }
 | 
|---|
| 174 |     }
 | 
|---|
| 175 |     if (AuxBasisSection) {
 | 
|---|
| 176 |       tokenizer tokens(linestring, equalitysep);
 | 
|---|
| 177 |       tokenizer::iterator tok_iter = tokens.begin();
 | 
|---|
| 178 |       ASSERT(tok_iter != tokens.end(),
 | 
|---|
| 179 |           "MpqcParser::load() - missing token for AuxBasisSection in line "+linestring+"!");
 | 
|---|
| 180 |       std::string key(*tok_iter++);
 | 
|---|
| 181 |       ASSERT(tok_iter != tokens.end(),
 | 
|---|
| 182 |           "MpqcParser::load() - missing value for BasisSection after key "+key+" in line "+linestring+"!");
 | 
|---|
| 183 |       std::string value(*tok_iter);
 | 
|---|
| 184 |       tok_iter++;
 | 
|---|
| 185 |       // TODO: use exception instead of ASSERT
 | 
|---|
| 186 |       ASSERT(tok_iter == tokens.end(),
 | 
|---|
| 187 |           "MpqcParser::load() - more than (key = value) on line "+linestring+".");
 | 
|---|
| 188 |       if (key == "name") {
 | 
|---|
| 189 |         std::stringstream linestream("aux_basis = "+value);
 | 
|---|
| 190 |         linestream >> getParams();
 | 
|---|
| 191 |       }
 | 
|---|
| 192 |     }
 | 
|---|
| 193 |     // set some scan flags
 | 
|---|
| 194 |     if (linestring.find("mpqc:") != string::npos) {
 | 
|---|
| 195 |       MpqcSection = true;
 | 
|---|
| 196 |     }
 | 
|---|
| 197 |     if (linestring.find("molecule<Molecule>:") != string::npos) {
 | 
|---|
| 198 |       MoleculeSection = true;
 | 
|---|
| 199 |     }
 | 
|---|
| 200 |     if (linestring.find("atoms geometry") != string::npos) {
 | 
|---|
| 201 |       GeometrySection = true;
 | 
|---|
| 202 |     }
 | 
|---|
| 203 |     if ((linestring.find("basis<GaussianBasisSet>:") != string::npos) && ((linestring.find("abasis<") == string::npos))) {
 | 
|---|
| 204 |       BasisSection = true;
 | 
|---|
| 205 |     }
 | 
|---|
| 206 |     if (linestring.find("abasis<") != string::npos) {
 | 
|---|
| 207 |       AuxBasisSection = true;
 | 
|---|
| 208 |     }
 | 
|---|
| 209 |   }
 | 
|---|
| 210 |   // refresh atom::nr and atom::name
 | 
|---|
| 211 |   newmol->getAtomCount();
 | 
|---|
| 212 | }
 | 
|---|
| 213 | 
 | 
|---|
| 214 | /** Saves all atoms and data into a MPQC config file.
 | 
|---|
| 215 |  * \param *file output stream
 | 
|---|
| 216 |  * \param atoms atoms to store
 | 
|---|
| 217 |  */
 | 
|---|
| 218 | void MpqcParser::save(ostream *file, const std::vector<atom *> &atoms)
 | 
|---|
| 219 | {
 | 
|---|
| 220 |   Vector center;
 | 
|---|
| 221 | //  vector<atom *> allatoms = World::getInstance().getAllAtoms();
 | 
|---|
| 222 | 
 | 
|---|
| 223 |   // calculate center
 | 
|---|
| 224 |   for (std::vector<atom *>::const_iterator runner = atoms.begin();runner != atoms.end(); ++runner)
 | 
|---|
| 225 |     center += (*runner)->getPosition();
 | 
|---|
| 226 |   center.Scale(1./(double)atoms.size());
 | 
|---|
| 227 | 
 | 
|---|
| 228 |   // first without hessian
 | 
|---|
| 229 |   if (file->fail()) {
 | 
|---|
| 230 |     DoeLog(1) && (eLog()<< Verbose(1) << "Cannot open mpqc output file." << endl);
 | 
|---|
| 231 |   } else {
 | 
|---|
| 232 |     *file << "% Created by MoleCuilder" << endl;
 | 
|---|
| 233 |     *file << "mpqc: (" << endl;
 | 
|---|
| 234 |     *file << "\tsavestate = " << getParams().getString(MpqcParser_Parameters::savestateParam) << endl;
 | 
|---|
| 235 |     *file << "\tdo_gradient = " << getParams().getString(MpqcParser_Parameters::do_gradientParam) << endl;
 | 
|---|
| 236 |     if (getParams().getBool(MpqcParser_Parameters::hessianParam)) {
 | 
|---|
| 237 |       *file << "\tfreq<MolecularFrequencies>: (" << endl;
 | 
|---|
| 238 |       *file << "\t\tmolecule=$:molecule" << endl;
 | 
|---|
| 239 |       *file << "\t)" << endl;
 | 
|---|
| 240 |     }
 | 
|---|
| 241 |     switch (getParams().getTheory()) {
 | 
|---|
| 242 |       case MpqcParser_Parameters::CLHF:
 | 
|---|
| 243 |         *file << "\tmole<" << getParams().getString(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
 | 
|---|
| 244 |         *file << "\t\tmolecule = $:molecule" << endl;
 | 
|---|
| 245 |         *file << "\t\tbasis = $:basis" << endl;
 | 
|---|
| 246 |         *file << "\t\tmaxiter = " << toString(getParams().getInt(MpqcParser_Parameters::maxiterParam))<< endl;
 | 
|---|
| 247 |         *file << "\t\tmemory = " << toString(getParams().getInt(MpqcParser_Parameters::memoryParam)) << endl;
 | 
|---|
| 248 |         *file << "\t)" << endl;
 | 
|---|
| 249 |         break;
 | 
|---|
| 250 |       case MpqcParser_Parameters::CLKS:
 | 
|---|
| 251 |         *file << "\tmole<" << getParams().getString(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
 | 
|---|
| 252 |         *file << "\t\tfunctional<StdDenFunctional>:(name=B3LYP)" << endl;
 | 
|---|
| 253 |         *file << "\t\tmolecule = $:molecule" << endl;
 | 
|---|
| 254 |         *file << "\t\tbasis = $:basis" << endl;
 | 
|---|
| 255 |         *file << "\t\tmaxiter = " << toString(getParams().getInt(MpqcParser_Parameters::maxiterParam))<< endl;
 | 
|---|
| 256 |         *file << "\t\tmemory = " << toString(getParams().getInt(MpqcParser_Parameters::memoryParam)) << endl;
 | 
|---|
| 257 |         *file << "\t)" << endl;
 | 
|---|
| 258 |         break;
 | 
|---|
| 259 |       case MpqcParser_Parameters::MBPT2:
 | 
|---|
| 260 |         *file << "\tmole<" << getParams().getString(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
 | 
|---|
| 261 |         *file << "\t\tbasis = $:basis" << endl;
 | 
|---|
| 262 |         *file << "\t\tmolecule = $:molecule" << endl;
 | 
|---|
| 263 |         *file << "\t\tmemory = " << toString(getParams().getInt(MpqcParser_Parameters::memoryParam)) << endl;
 | 
|---|
| 264 |         *file << "\t\treference<CLHF>: (" << endl;
 | 
|---|
| 265 |         *file << "\t\t\tmaxiter = " << toString(getParams().getInt(MpqcParser_Parameters::maxiterParam))<< endl;
 | 
|---|
| 266 |         *file << "\t\t\tbasis = $:basis" << endl;
 | 
|---|
| 267 |         *file << "\t\t\tmolecule = $:molecule" << endl;
 | 
|---|
| 268 |         *file << "\t\t\tmemory = " << toString(getParams().getInt(MpqcParser_Parameters::memoryParam)) << endl;
 | 
|---|
| 269 |         *file << "\t\t)" << endl;
 | 
|---|
| 270 |         *file << "\t)" << endl;
 | 
|---|
| 271 |         break;
 | 
|---|
| 272 |       case MpqcParser_Parameters::MBPT2_R12:
 | 
|---|
| 273 |         *file << "\tmole<" << getParams().getString(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
 | 
|---|
| 274 |         *file << "\t\tmolecule = $:molecule" << endl;
 | 
|---|
| 275 |         *file << "\t\tbasis = $:basis" << endl;
 | 
|---|
| 276 |         *file << "\t\taux_basis = $:abasis" << endl;
 | 
|---|
| 277 |         *file << "\t\tstdapprox = \"" << getParams().getString(MpqcParser_Parameters::stdapproxParam) << "\"" << endl;
 | 
|---|
| 278 |         *file << "\t\tnfzc = " << toString(getParams().getInt(MpqcParser_Parameters::nfzcParam)) << endl;
 | 
|---|
| 279 |         *file << "\t\tmemory = " << toString(getParams().getInt(MpqcParser_Parameters::memoryParam)) << endl;
 | 
|---|
| 280 |         *file << "\t\tintegrals<IntegralCints>:()" << endl;
 | 
|---|
| 281 |         *file << "\t\treference<CLHF>: (" << endl;
 | 
|---|
| 282 |         *file << "\t\t\tmolecule = $:molecule" << endl;
 | 
|---|
| 283 |         *file << "\t\t\tbasis = $:basis" << endl;
 | 
|---|
| 284 |         *file << "\t\t\tmaxiter = " << toString(getParams().getInt(MpqcParser_Parameters::maxiterParam)) << endl;
 | 
|---|
| 285 |         *file << "\t\t\tmemory = " << toString(getParams().getInt(MpqcParser_Parameters::memoryParam)) << endl;
 | 
|---|
| 286 |         *file << "\t\t\tintegrals<" << getParams().getString(MpqcParser_Parameters::integrationParam) << ">:()" << endl;
 | 
|---|
| 287 |         *file << "\t\t)" << endl;
 | 
|---|
| 288 |         *file << "\t)" << endl;
 | 
|---|
| 289 |         break;
 | 
|---|
| 290 |       default:
 | 
|---|
| 291 |         DoeLog(0) && (eLog() << Verbose(0)
 | 
|---|
| 292 |             << "Unknown level of theory requested for MPQC output file." << std::endl);
 | 
|---|
| 293 |         break;
 | 
|---|
| 294 |     }
 | 
|---|
| 295 |     *file << ")" << endl;
 | 
|---|
| 296 |     *file << "molecule<Molecule>: (" << endl;
 | 
|---|
| 297 |     *file << "\tunit = " << (World::getInstance().getConfig()->GetIsAngstroem() ? "angstrom" : "bohr" ) << endl;
 | 
|---|
| 298 |     *file << "\t{ atoms geometry } = {" << endl;
 | 
|---|
| 299 |     // output of atoms
 | 
|---|
| 300 |     for (std::vector<atom *>::const_iterator AtomRunner = atoms.begin(); AtomRunner != atoms.end(); ++AtomRunner) {
 | 
|---|
| 301 |       (*AtomRunner)->OutputMPQCLine(file, ¢er);
 | 
|---|
| 302 |     }
 | 
|---|
| 303 |     *file << "\t}" << endl;
 | 
|---|
| 304 |     *file << ")" << endl;
 | 
|---|
| 305 |     *file << "basis<GaussianBasisSet>: (" << endl;
 | 
|---|
| 306 |     *file << "\tname = \"" << getParams().getString(MpqcParser_Parameters::basisParam) << "\"" << endl;
 | 
|---|
| 307 |     *file << "\tmolecule = $:molecule" << endl;
 | 
|---|
| 308 |     *file << ")" << endl;
 | 
|---|
| 309 |     if (getParams().getTheory() == MpqcParser_Parameters::MBPT2_R12) {
 | 
|---|
| 310 |       *file << "% auxiliary basis set specification" << endl;
 | 
|---|
| 311 |       *file << "\tabasis<GaussianBasisSet>: (" << endl;
 | 
|---|
| 312 |       *file << "\tname = \"" << getParams().getString(MpqcParser_Parameters::aux_basisParam) << "\"" << endl;
 | 
|---|
| 313 |       *file << "\tmolecule = $:molecule" << endl;
 | 
|---|
| 314 |       *file << ")" << endl;
 | 
|---|
| 315 |     }
 | 
|---|
| 316 |   }
 | 
|---|
| 317 | }
 | 
|---|
| 318 | 
 | 
|---|
| 319 | 
 | 
|---|