[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"
|
---|
| 27 |
|
---|
| 28 | #include "atom.hpp"
|
---|
| 29 | #include "config.hpp"
|
---|
| 30 | #include "element.hpp"
|
---|
[aa8ef2] | 31 | #include "molecule.hpp"
|
---|
[ad011c] | 32 | #include "CodePatterns/Log.hpp"
|
---|
[aa8ef2] | 33 | #include "CodePatterns/toString.hpp"
|
---|
[ad011c] | 34 | #include "CodePatterns/Verbose.hpp"
|
---|
[e97a44] | 35 | #include "LinearAlgebra/Vector.hpp"
|
---|
| 36 | #include "periodentafel.hpp"
|
---|
[1b2d30] | 37 | #include "World.hpp"
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | /** Constructor of MpqcParser.
|
---|
| 41 | *
|
---|
| 42 | */
|
---|
[61d69a4] | 43 | MpqcParser::MpqcParser()
|
---|
[97b825] | 44 | {}
|
---|
[1b2d30] | 45 |
|
---|
| 46 | /** Destructor of MpqcParser.
|
---|
| 47 | *
|
---|
| 48 | */
|
---|
[97b825] | 49 | MpqcParser::~MpqcParser()
|
---|
| 50 | {}
|
---|
[1b2d30] | 51 |
|
---|
| 52 | /** Load an MPQC config file into the World.
|
---|
| 53 | * \param *file input stream
|
---|
| 54 | */
|
---|
| 55 | void MpqcParser::load(istream *file)
|
---|
| 56 | {
|
---|
[4cbca0] | 57 | bool MpqcSection = false;
|
---|
| 58 | bool MoleculeSection = false;
|
---|
| 59 | bool GeometrySection = false;
|
---|
| 60 | bool BasisSection = false;
|
---|
| 61 | bool AuxBasisSection = false;
|
---|
[aa8ef2] | 62 | char line[MAXSTRINGSIZE];
|
---|
| 63 | typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
|
---|
| 64 | boost::char_separator<char> sep("[]");
|
---|
[4cbca0] | 65 | boost::char_separator<char> angularsep("<>");
|
---|
[311da7b] | 66 | boost::char_separator<char> equalitysep(" =");
|
---|
[aa8ef2] | 67 | boost::char_separator<char> whitesep(" \t");
|
---|
| 68 | ConvertTo<double> toDouble;
|
---|
| 69 |
|
---|
| 70 | molecule *newmol = World::getInstance().createMolecule();
|
---|
| 71 | newmol->ActiveFlag = true;
|
---|
[4cbca0] | 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);
|
---|
[aa8ef2] | 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)) {
|
---|
[4cbca0] | 78 | GeometrySection = false;
|
---|
[aa8ef2] | 79 | }
|
---|
[4cbca0] | 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();
|
---|
[311da7b] | 92 | ASSERT(tok_iter != tokens.end(),
|
---|
| 93 | "MpqcParser::load() - missing token for MoleculeSection in line "+linestring+"!");
|
---|
[4cbca0] | 94 | std::stringstream whitespacefilter(*tok_iter++);
|
---|
| 95 | std::string element;
|
---|
[311da7b] | 96 | whitespacefilter >> ws >> element;
|
---|
| 97 | ASSERT(tok_iter != tokens.end(),
|
---|
| 98 | "MpqcParser::load() - missing token for MoleculeSection in line "+linestring+"!");
|
---|
[4cbca0] | 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) {
|
---|
[311da7b] | 117 | if (linestring.find("mole<") != string::npos) { // get theory
|
---|
[4cbca0] | 118 | tokenizer tokens(linestring, angularsep);
|
---|
| 119 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
[311da7b] | 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);
|
---|
[4cbca0] | 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();
|
---|
[311da7b] | 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);
|
---|
[4cbca0] | 133 | std::stringstream linestream("integration = "+value);
|
---|
| 134 | linestream >> params;
|
---|
[311da7b] | 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 | }
|
---|
[4cbca0] | 150 | }
|
---|
| 151 | }
|
---|
| 152 | if (BasisSection) {
|
---|
| 153 | tokenizer tokens(linestring, equalitysep);
|
---|
| 154 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
[311da7b] | 155 | ASSERT(tok_iter != tokens.end(),
|
---|
| 156 | "MpqcParser::load() - missing token for BasisSection in line "+linestring+"!");
|
---|
[4cbca0] | 157 | std::string key(*tok_iter++);
|
---|
[311da7b] | 158 | ASSERT(tok_iter != tokens.end(),
|
---|
| 159 | "MpqcParser::load() - missing value for BasisSection after key "+key+" in line "+linestring+"!");
|
---|
[4cbca0] | 160 | std::string value(*tok_iter);
|
---|
[311da7b] | 161 | tok_iter++;
|
---|
[4cbca0] | 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();
|
---|
[311da7b] | 173 | ASSERT(tok_iter != tokens.end(),
|
---|
| 174 | "MpqcParser::load() - missing token for AuxBasisSection in line "+linestring+"!");
|
---|
[4cbca0] | 175 | std::string key(*tok_iter++);
|
---|
[311da7b] | 176 | ASSERT(tok_iter != tokens.end(),
|
---|
| 177 | "MpqcParser::load() - missing value for BasisSection after key "+key+" in line "+linestring+"!");
|
---|
[4cbca0] | 178 | std::string value(*tok_iter);
|
---|
[311da7b] | 179 | tok_iter++;
|
---|
[4cbca0] | 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;
|
---|
[aa8ef2] | 186 | }
|
---|
| 187 | }
|
---|
| 188 | // set some scan flags
|
---|
[4cbca0] | 189 | if (linestring.find("mpqc:") != string::npos) {
|
---|
| 190 | MpqcSection = true;
|
---|
| 191 | }
|
---|
| 192 | if (linestring.find("molecule<Molecule>:") != string::npos) {
|
---|
| 193 | MoleculeSection = true;
|
---|
| 194 | }
|
---|
[aa8ef2] | 195 | if (linestring.find("atoms geometry") != string::npos) {
|
---|
[4cbca0] | 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;
|
---|
[aa8ef2] | 203 | }
|
---|
| 204 | }
|
---|
[4afa46] | 205 | // refresh atom::nr and atom::name
|
---|
| 206 | newmol->getAtomCount();
|
---|
[1b2d30] | 207 | }
|
---|
| 208 |
|
---|
[f31edc] | 209 | /** Saves all atoms and data into a MPQC config file.
|
---|
[1b2d30] | 210 | * \param *file output stream
|
---|
[73916f] | 211 | * \param atoms atoms to store
|
---|
[1b2d30] | 212 | */
|
---|
[f31edc] | 213 | void MpqcParser::save(ostream *file, const std::vector<atom *> &atoms)
|
---|
[1b2d30] | 214 | {
|
---|
| 215 | Vector center;
|
---|
| 216 | vector<atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
| 217 |
|
---|
[d6b8e1] | 218 | // calculate center
|
---|
| 219 | for (vector<atom *>::iterator runner = allatoms.begin();runner != allatoms.end(); ++runner)
|
---|
[d74077] | 220 | center += (*runner)->getPosition();
|
---|
[61d69a4] | 221 | center.Scale(1./(double)allatoms.size());
|
---|
[d6b8e1] | 222 |
|
---|
[1b2d30] | 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;
|
---|
[61d69a4] | 229 | *file << "\tsavestate = " << params.getString(MpqcParser_Parameters::savestateParam) << endl;
|
---|
| 230 | *file << "\tdo_gradient = " << params.getString(MpqcParser_Parameters::do_gradientParam) << endl;
|
---|
[44fce5] | 231 | if (params.getBool(MpqcParser_Parameters::hessianParam)) {
|
---|
[f31edc] | 232 | *file << "\tfreq<MolecularFrequencies>: (" << endl;
|
---|
| 233 | *file << "\t\tmolecule=$:molecule" << endl;
|
---|
| 234 | *file << "\t)" << endl;
|
---|
| 235 | }
|
---|
[61d69a4] | 236 | switch (params.getTheory()) {
|
---|
| 237 | case MpqcParser_Parameters::CLHF:
|
---|
| 238 | *file << "\tmole<" << params.getString(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
|
---|
[f31edc] | 239 | *file << "\t\tmolecule = $:molecule" << endl;
|
---|
| 240 | *file << "\t\tbasis = $:basis" << endl;
|
---|
[61d69a4] | 241 | *file << "\t\tmaxiter = " << toString(params.getInt(MpqcParser_Parameters::maxiterParam))<< endl;
|
---|
| 242 | *file << "\t\tmemory = " << toString(params.getInt(MpqcParser_Parameters::memoryParam)) << endl;
|
---|
[f31edc] | 243 | *file << "\t)" << endl;
|
---|
| 244 | break;
|
---|
[61d69a4] | 245 | case MpqcParser_Parameters::CLKS:
|
---|
| 246 | *file << "\tmole<" << params.getString(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
|
---|
[f31edc] | 247 | *file << "\t\tfunctional<StdDenFunctional>:(name=B3LYP)" << endl;
|
---|
| 248 | *file << "\t\tmolecule = $:molecule" << endl;
|
---|
| 249 | *file << "\t\tbasis = $:basis" << endl;
|
---|
[61d69a4] | 250 | *file << "\t\tmaxiter = " << toString(params.getInt(MpqcParser_Parameters::maxiterParam))<< endl;
|
---|
| 251 | *file << "\t\tmemory = " << toString(params.getInt(MpqcParser_Parameters::memoryParam)) << endl;
|
---|
[f31edc] | 252 | *file << "\t)" << endl;
|
---|
| 253 | break;
|
---|
[61d69a4] | 254 | case MpqcParser_Parameters::MBPT2:
|
---|
| 255 | *file << "\tmole<" << params.getString(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
|
---|
[f31edc] | 256 | *file << "\t\tbasis = $:basis" << endl;
|
---|
| 257 | *file << "\t\tmolecule = $:molecule" << endl;
|
---|
[61d69a4] | 258 | *file << "\t\tmemory = " << toString(params.getInt(MpqcParser_Parameters::memoryParam)) << endl;
|
---|
[f31edc] | 259 | *file << "\t\treference<CLHF>: (" << endl;
|
---|
[61d69a4] | 260 | *file << "\t\t\tmaxiter = " << toString(params.getInt(MpqcParser_Parameters::maxiterParam))<< endl;
|
---|
[f31edc] | 261 | *file << "\t\t\tbasis = $:basis" << endl;
|
---|
| 262 | *file << "\t\t\tmolecule = $:molecule" << endl;
|
---|
[61d69a4] | 263 | *file << "\t\t\tmemory = " << toString(params.getInt(MpqcParser_Parameters::memoryParam)) << endl;
|
---|
[f31edc] | 264 | *file << "\t\t)" << endl;
|
---|
| 265 | *file << "\t)" << endl;
|
---|
| 266 | break;
|
---|
[61d69a4] | 267 | case MpqcParser_Parameters::MBPT2_R12:
|
---|
| 268 | *file << "\tmole<" << params.getString(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
|
---|
[f31edc] | 269 | *file << "\t\tmolecule = $:molecule" << endl;
|
---|
| 270 | *file << "\t\tbasis = $:basis" << endl;
|
---|
| 271 | *file << "\t\taux_basis = $:abasis" << endl;
|
---|
[61d69a4] | 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;
|
---|
[f31edc] | 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;
|
---|
[61d69a4] | 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;
|
---|
[f31edc] | 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;
|
---|
[1b2d30] | 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) {
|
---|
[0dc86e2] | 296 | (*AtomRunner)->OutputMPQCLine(file, ¢er);
|
---|
[1b2d30] | 297 | }
|
---|
| 298 | *file << "\t}" << endl;
|
---|
| 299 | *file << ")" << endl;
|
---|
| 300 | *file << "basis<GaussianBasisSet>: (" << endl;
|
---|
[61d69a4] | 301 | *file << "\tname = \"" << params.getString(MpqcParser_Parameters::basisParam) << "\"" << endl;
|
---|
[1b2d30] | 302 | *file << "\tmolecule = $:molecule" << endl;
|
---|
| 303 | *file << ")" << endl;
|
---|
[61d69a4] | 304 | if (params.getTheory() == MpqcParser_Parameters::MBPT2_R12) {
|
---|
[f31edc] | 305 | *file << "% auxiliary basis set specification" << endl;
|
---|
| 306 | *file << "\tabasis<GaussianBasisSet>: (" << endl;
|
---|
[61d69a4] | 307 | *file << "\tname = \"" << params.getString(MpqcParser_Parameters::aux_basisParam) << "\"" << endl;
|
---|
[f31edc] | 308 | *file << "\tmolecule = $:molecule" << endl;
|
---|
| 309 | *file << ")" << endl;
|
---|
| 310 | }
|
---|
[1b2d30] | 311 | }
|
---|
| 312 | }
|
---|
| 313 |
|
---|
[963321a] | 314 | MpqcParser_Parameters & MpqcParser::getParams()
|
---|
| 315 | {
|
---|
| 316 | return params;
|
---|
| 317 | }
|
---|
| 318 |
|
---|