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