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