[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 |
|
---|
| 40 |
|
---|
| 41 | /** Constructor of MpqcParser.
|
---|
| 42 | *
|
---|
| 43 | */
|
---|
[61d69a4] | 44 | MpqcParser::MpqcParser()
|
---|
[c1db05] | 45 | {
|
---|
| 46 | parameters = new MpqcParser_Parameters();
|
---|
| 47 | }
|
---|
[1b2d30] | 48 |
|
---|
| 49 | /** Destructor of MpqcParser.
|
---|
| 50 | *
|
---|
| 51 | */
|
---|
[97b825] | 52 | MpqcParser::~MpqcParser()
|
---|
[c1db05] | 53 | {
|
---|
| 54 | delete parameters;
|
---|
| 55 | }
|
---|
[1b2d30] | 56 |
|
---|
| 57 | /** Load an MPQC config file into the World.
|
---|
| 58 | * \param *file input stream
|
---|
| 59 | */
|
---|
| 60 | void MpqcParser::load(istream *file)
|
---|
| 61 | {
|
---|
[4cbca0] | 62 | bool MpqcSection = false;
|
---|
| 63 | bool MoleculeSection = false;
|
---|
| 64 | bool GeometrySection = false;
|
---|
| 65 | bool BasisSection = false;
|
---|
| 66 | bool AuxBasisSection = false;
|
---|
[aa8ef2] | 67 | char line[MAXSTRINGSIZE];
|
---|
| 68 | typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
|
---|
| 69 | boost::char_separator<char> sep("[]");
|
---|
[4cbca0] | 70 | boost::char_separator<char> angularsep("<>");
|
---|
[311da7b] | 71 | boost::char_separator<char> equalitysep(" =");
|
---|
[aa8ef2] | 72 | boost::char_separator<char> whitesep(" \t");
|
---|
| 73 | ConvertTo<double> toDouble;
|
---|
| 74 |
|
---|
| 75 | molecule *newmol = World::getInstance().createMolecule();
|
---|
| 76 | newmol->ActiveFlag = true;
|
---|
[4cbca0] | 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);
|
---|
[aa8ef2] | 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)) {
|
---|
[4cbca0] | 83 | GeometrySection = false;
|
---|
[aa8ef2] | 84 | }
|
---|
[4cbca0] | 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();
|
---|
[311da7b] | 97 | ASSERT(tok_iter != tokens.end(),
|
---|
| 98 | "MpqcParser::load() - missing token for MoleculeSection in line "+linestring+"!");
|
---|
[4cbca0] | 99 | std::stringstream whitespacefilter(*tok_iter++);
|
---|
| 100 | std::string element;
|
---|
[311da7b] | 101 | whitespacefilter >> ws >> element;
|
---|
| 102 | ASSERT(tok_iter != tokens.end(),
|
---|
| 103 | "MpqcParser::load() - missing token for MoleculeSection in line "+linestring+"!");
|
---|
[4cbca0] | 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) {
|
---|
[311da7b] | 122 | if (linestring.find("mole<") != string::npos) { // get theory
|
---|
[4cbca0] | 123 | tokenizer tokens(linestring, angularsep);
|
---|
| 124 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
[311da7b] | 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);
|
---|
[4cbca0] | 129 | std::stringstream linestream("theory = "+value);
|
---|
[c1db05] | 130 | linestream >> getParams();
|
---|
[4cbca0] | 131 | } else if (linestring.find("integrals<") != string::npos) { // get theory
|
---|
| 132 | tokenizer tokens(linestring, angularsep);
|
---|
| 133 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
[311da7b] | 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);
|
---|
[4cbca0] | 138 | std::stringstream linestream("integration = "+value);
|
---|
[c1db05] | 139 | linestream >> getParams();
|
---|
[311da7b] | 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;
|
---|
[c1db05] | 149 | if (getParams().haveParam(key)) {
|
---|
[311da7b] | 150 | std::stringstream linestream(linestring);
|
---|
[c1db05] | 151 | linestream >> getParams();
|
---|
[311da7b] | 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 | }
|
---|
[4cbca0] | 155 | }
|
---|
| 156 | }
|
---|
| 157 | if (BasisSection) {
|
---|
| 158 | tokenizer tokens(linestring, equalitysep);
|
---|
| 159 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
[311da7b] | 160 | ASSERT(tok_iter != tokens.end(),
|
---|
| 161 | "MpqcParser::load() - missing token for BasisSection in line "+linestring+"!");
|
---|
[4cbca0] | 162 | std::string key(*tok_iter++);
|
---|
[311da7b] | 163 | ASSERT(tok_iter != tokens.end(),
|
---|
| 164 | "MpqcParser::load() - missing value for BasisSection after key "+key+" in line "+linestring+"!");
|
---|
[4cbca0] | 165 | std::string value(*tok_iter);
|
---|
[311da7b] | 166 | tok_iter++;
|
---|
[4cbca0] | 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);
|
---|
[c1db05] | 172 | linestream >> getParams();
|
---|
[4cbca0] | 173 | }
|
---|
| 174 | }
|
---|
| 175 | if (AuxBasisSection) {
|
---|
| 176 | tokenizer tokens(linestring, equalitysep);
|
---|
| 177 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
[311da7b] | 178 | ASSERT(tok_iter != tokens.end(),
|
---|
| 179 | "MpqcParser::load() - missing token for AuxBasisSection in line "+linestring+"!");
|
---|
[4cbca0] | 180 | std::string key(*tok_iter++);
|
---|
[311da7b] | 181 | ASSERT(tok_iter != tokens.end(),
|
---|
| 182 | "MpqcParser::load() - missing value for BasisSection after key "+key+" in line "+linestring+"!");
|
---|
[4cbca0] | 183 | std::string value(*tok_iter);
|
---|
[311da7b] | 184 | tok_iter++;
|
---|
[4cbca0] | 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);
|
---|
[c1db05] | 190 | linestream >> getParams();
|
---|
[aa8ef2] | 191 | }
|
---|
| 192 | }
|
---|
| 193 | // set some scan flags
|
---|
[4cbca0] | 194 | if (linestring.find("mpqc:") != string::npos) {
|
---|
| 195 | MpqcSection = true;
|
---|
| 196 | }
|
---|
| 197 | if (linestring.find("molecule<Molecule>:") != string::npos) {
|
---|
| 198 | MoleculeSection = true;
|
---|
| 199 | }
|
---|
[aa8ef2] | 200 | if (linestring.find("atoms geometry") != string::npos) {
|
---|
[4cbca0] | 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;
|
---|
[aa8ef2] | 208 | }
|
---|
| 209 | }
|
---|
[4afa46] | 210 | // refresh atom::nr and atom::name
|
---|
| 211 | newmol->getAtomCount();
|
---|
[1b2d30] | 212 | }
|
---|
| 213 |
|
---|
[f31edc] | 214 | /** Saves all atoms and data into a MPQC config file.
|
---|
[1b2d30] | 215 | * \param *file output stream
|
---|
[73916f] | 216 | * \param atoms atoms to store
|
---|
[1b2d30] | 217 | */
|
---|
[f31edc] | 218 | void MpqcParser::save(ostream *file, const std::vector<atom *> &atoms)
|
---|
[1b2d30] | 219 | {
|
---|
| 220 | Vector center;
|
---|
[f7c19e] | 221 | // vector<atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
[1b2d30] | 222 |
|
---|
[d6b8e1] | 223 | // calculate center
|
---|
[f7c19e] | 224 | for (std::vector<atom *>::const_iterator runner = atoms.begin();runner != atoms.end(); ++runner)
|
---|
[d74077] | 225 | center += (*runner)->getPosition();
|
---|
[f7c19e] | 226 | center.Scale(1./(double)atoms.size());
|
---|
[d6b8e1] | 227 |
|
---|
[1b2d30] | 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;
|
---|
[c1db05] | 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)) {
|
---|
[f31edc] | 237 | *file << "\tfreq<MolecularFrequencies>: (" << endl;
|
---|
| 238 | *file << "\t\tmolecule=$:molecule" << endl;
|
---|
| 239 | *file << "\t)" << endl;
|
---|
| 240 | }
|
---|
[c1db05] | 241 | switch (getParams().getTheory()) {
|
---|
[61d69a4] | 242 | case MpqcParser_Parameters::CLHF:
|
---|
[c1db05] | 243 | *file << "\tmole<" << getParams().getString(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
|
---|
[f31edc] | 244 | *file << "\t\tmolecule = $:molecule" << endl;
|
---|
| 245 | *file << "\t\tbasis = $:basis" << endl;
|
---|
[c1db05] | 246 | *file << "\t\tmaxiter = " << toString(getParams().getInt(MpqcParser_Parameters::maxiterParam))<< endl;
|
---|
| 247 | *file << "\t\tmemory = " << toString(getParams().getInt(MpqcParser_Parameters::memoryParam)) << endl;
|
---|
[f31edc] | 248 | *file << "\t)" << endl;
|
---|
| 249 | break;
|
---|
[61d69a4] | 250 | case MpqcParser_Parameters::CLKS:
|
---|
[c1db05] | 251 | *file << "\tmole<" << getParams().getString(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
|
---|
[f31edc] | 252 | *file << "\t\tfunctional<StdDenFunctional>:(name=B3LYP)" << endl;
|
---|
| 253 | *file << "\t\tmolecule = $:molecule" << endl;
|
---|
| 254 | *file << "\t\tbasis = $:basis" << endl;
|
---|
[c1db05] | 255 | *file << "\t\tmaxiter = " << toString(getParams().getInt(MpqcParser_Parameters::maxiterParam))<< endl;
|
---|
| 256 | *file << "\t\tmemory = " << toString(getParams().getInt(MpqcParser_Parameters::memoryParam)) << endl;
|
---|
[f31edc] | 257 | *file << "\t)" << endl;
|
---|
| 258 | break;
|
---|
[61d69a4] | 259 | case MpqcParser_Parameters::MBPT2:
|
---|
[c1db05] | 260 | *file << "\tmole<" << getParams().getString(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
|
---|
[f31edc] | 261 | *file << "\t\tbasis = $:basis" << endl;
|
---|
| 262 | *file << "\t\tmolecule = $:molecule" << endl;
|
---|
[c1db05] | 263 | *file << "\t\tmemory = " << toString(getParams().getInt(MpqcParser_Parameters::memoryParam)) << endl;
|
---|
[f31edc] | 264 | *file << "\t\treference<CLHF>: (" << endl;
|
---|
[c1db05] | 265 | *file << "\t\t\tmaxiter = " << toString(getParams().getInt(MpqcParser_Parameters::maxiterParam))<< endl;
|
---|
[f31edc] | 266 | *file << "\t\t\tbasis = $:basis" << endl;
|
---|
| 267 | *file << "\t\t\tmolecule = $:molecule" << endl;
|
---|
[c1db05] | 268 | *file << "\t\t\tmemory = " << toString(getParams().getInt(MpqcParser_Parameters::memoryParam)) << endl;
|
---|
[f31edc] | 269 | *file << "\t\t)" << endl;
|
---|
| 270 | *file << "\t)" << endl;
|
---|
| 271 | break;
|
---|
[61d69a4] | 272 | case MpqcParser_Parameters::MBPT2_R12:
|
---|
[c1db05] | 273 | *file << "\tmole<" << getParams().getString(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
|
---|
[f31edc] | 274 | *file << "\t\tmolecule = $:molecule" << endl;
|
---|
| 275 | *file << "\t\tbasis = $:basis" << endl;
|
---|
| 276 | *file << "\t\taux_basis = $:abasis" << endl;
|
---|
[c1db05] | 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;
|
---|
[f31edc] | 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;
|
---|
[c1db05] | 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;
|
---|
[f31edc] | 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;
|
---|
[1b2d30] | 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
|
---|
[f7c19e] | 300 | for (std::vector<atom *>::const_iterator AtomRunner = atoms.begin(); AtomRunner != atoms.end(); ++AtomRunner) {
|
---|
[0dc86e2] | 301 | (*AtomRunner)->OutputMPQCLine(file, ¢er);
|
---|
[1b2d30] | 302 | }
|
---|
| 303 | *file << "\t}" << endl;
|
---|
| 304 | *file << ")" << endl;
|
---|
| 305 | *file << "basis<GaussianBasisSet>: (" << endl;
|
---|
[c1db05] | 306 | *file << "\tname = \"" << getParams().getString(MpqcParser_Parameters::basisParam) << "\"" << endl;
|
---|
[1b2d30] | 307 | *file << "\tmolecule = $:molecule" << endl;
|
---|
| 308 | *file << ")" << endl;
|
---|
[c1db05] | 309 | if (getParams().getTheory() == MpqcParser_Parameters::MBPT2_R12) {
|
---|
[f31edc] | 310 | *file << "% auxiliary basis set specification" << endl;
|
---|
| 311 | *file << "\tabasis<GaussianBasisSet>: (" << endl;
|
---|
[c1db05] | 312 | *file << "\tname = \"" << getParams().getString(MpqcParser_Parameters::aux_basisParam) << "\"" << endl;
|
---|
[f31edc] | 313 | *file << "\tmolecule = $:molecule" << endl;
|
---|
| 314 | *file << ")" << endl;
|
---|
| 315 | }
|
---|
[1b2d30] | 316 | }
|
---|
| 317 | }
|
---|
| 318 |
|
---|
[963321a] | 319 |
|
---|