[1bef07] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[1bef07] | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | /*
|
---|
| 9 | * Psi3Parser.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Oct 04, 2011
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
| 20 | #include <iostream>
|
---|
[03a589] | 21 | #include <boost/foreach.hpp>
|
---|
[1bef07] | 22 | #include <boost/tokenizer.hpp>
|
---|
| 23 | #include <string>
|
---|
| 24 |
|
---|
| 25 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 26 |
|
---|
| 27 | #include "Psi3Parser.hpp"
|
---|
| 28 | #include "Psi3Parser_Parameters.hpp"
|
---|
| 29 |
|
---|
[6f0841] | 30 | #include "Atom/atom.hpp"
|
---|
[41a467] | 31 | #include "CodePatterns/Log.hpp"
|
---|
| 32 | #include "CodePatterns/toString.hpp"
|
---|
[360c8b] | 33 | #include "Element/element.hpp"
|
---|
| 34 | #include "Element/periodentafel.hpp"
|
---|
[41a467] | 35 | #include "LinearAlgebra/Vector.hpp"
|
---|
[1bef07] | 36 | #include "molecule.hpp"
|
---|
[42127c] | 37 | #include "MoleculeListClass.hpp"
|
---|
[1bef07] | 38 | #include "World.hpp"
|
---|
| 39 |
|
---|
| 40 | // declare specialized static variables
|
---|
| 41 | const std::string FormatParserTrait<psi3>::name = "psi3";
|
---|
[752bb4] | 42 | const std::string FormatParserTrait<psi3>::suffix = "psi";
|
---|
[1bef07] | 43 | const ParserTypes FormatParserTrait<psi3>::type = psi3;
|
---|
| 44 |
|
---|
| 45 | // a converter we often need
|
---|
| 46 | ConvertTo<bool> FormatParser<psi3>::Converter;
|
---|
| 47 |
|
---|
| 48 | /** Constructor of Psi3Parser.
|
---|
| 49 | *
|
---|
| 50 | */
|
---|
| 51 | FormatParser< psi3 >::FormatParser() :
|
---|
| 52 | FormatParser_common(new Psi3Parser_Parameters())
|
---|
| 53 | {}
|
---|
| 54 |
|
---|
| 55 | /** Destructor of Psi3Parser.
|
---|
| 56 | *
|
---|
| 57 | */
|
---|
| 58 | FormatParser< psi3 >::~FormatParser()
|
---|
| 59 | {}
|
---|
| 60 |
|
---|
[41a467] | 61 | /** Load an PSI3 config file into the World.
|
---|
[1bef07] | 62 | * \param *file input stream
|
---|
| 63 | */
|
---|
| 64 | void FormatParser< psi3 >::load(istream *file)
|
---|
| 65 | {
|
---|
[032f31] | 66 | bool Psi3Section = false;
|
---|
| 67 | bool GeometrySection = false;
|
---|
| 68 | char line[MAXSTRINGSIZE];
|
---|
| 69 | typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
|
---|
| 70 | boost::char_separator<char> sep("()");
|
---|
| 71 | boost::char_separator<char> angularsep("<>");
|
---|
| 72 | boost::char_separator<char> equalitysep(" =");
|
---|
| 73 | boost::char_separator<char> whitesep(" \t");
|
---|
| 74 | ConvertTo<double> toDouble;
|
---|
| 75 |
|
---|
| 76 | molecule *newmol = World::getInstance().createMolecule();
|
---|
| 77 | newmol->ActiveFlag = true;
|
---|
| 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);
|
---|
| 80 | while (file->good()) {
|
---|
| 81 | file->getline(line, MAXSTRINGSIZE-1);
|
---|
| 82 | std::string linestring(line);
|
---|
| 83 | LOG(3, "INFO: Current line is: " << line);
|
---|
| 84 | if ((linestring.find(")") != string::npos) && (linestring.find("(") == string::npos)) {
|
---|
| 85 | LOG(3, "INFO: Line contains ')' and no '(' (end of section): " << line);
|
---|
| 86 | // ends a section which do not overlap
|
---|
| 87 | if (GeometrySection)
|
---|
| 88 | GeometrySection = false;
|
---|
| 89 | else
|
---|
| 90 | Psi3Section = false;
|
---|
| 91 | }
|
---|
| 92 | if (GeometrySection) { // we have an atom
|
---|
| 93 | tokenizer tokens(linestring, sep);
|
---|
| 94 | // if (tokens.size() != 2)
|
---|
| 95 | // throw Psi3ParseException;
|
---|
| 96 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
| 97 | ASSERT(tok_iter != tokens.end(),
|
---|
| 98 | "FormatParser< psi3 >::load() - missing token for MoleculeSection in line "+linestring+"!");
|
---|
| 99 | std::stringstream whitespacefilter(*++tok_iter);
|
---|
| 100 | std::string element;
|
---|
| 101 | whitespacefilter >> ws >> element;
|
---|
| 102 | LOG(2, "INFO: element of atom is " << element);
|
---|
| 103 | ASSERT(tok_iter != tokens.end(),
|
---|
| 104 | "FormatParser< psi3 >::load() - missing token for MoleculeSection in line "+linestring+"!");
|
---|
| 105 | std::string vector = *tok_iter;
|
---|
| 106 | tokenizer vectorcomponents(vector, whitesep);
|
---|
| 107 | Vector X;
|
---|
| 108 | // if (vectorcomponents.size() != NDIM)
|
---|
| 109 | // throw Psi3ParseException;
|
---|
| 110 | tok_iter = vectorcomponents.begin();
|
---|
| 111 | ++tok_iter;
|
---|
| 112 | for (int i=0; i<NDIM; ++i) {
|
---|
| 113 | LOG(4, "INFO: Current value is " << *tok_iter << ".");
|
---|
| 114 | X[i] = toDouble(*tok_iter++);
|
---|
| 115 | }
|
---|
| 116 | LOG(2, "INFO: position of atom is " << X);
|
---|
| 117 | // create atom
|
---|
| 118 | atom *newAtom = World::getInstance().createAtom();
|
---|
| 119 | newAtom->setType(World::getInstance().getPeriode()->FindElement(element));
|
---|
| 120 | newAtom->setPosition(X);
|
---|
| 121 | newmol->AddAtom(newAtom);
|
---|
[47d041] | 122 | LOG(1, "Adding atom " << *newAtom);
|
---|
[032f31] | 123 | }
|
---|
| 124 | if ((Psi3Section) && (!GeometrySection)) {
|
---|
| 125 | if (linestring.find("=") != string::npos) { // get param value
|
---|
| 126 | tokenizer tokens(linestring, equalitysep);
|
---|
| 127 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
| 128 | ASSERT(tok_iter != tokens.end(),
|
---|
| 129 | "FormatParser< psi3 >::load() - missing token before '=' for Psi3Section in line "+linestring+"!");
|
---|
| 130 | std::stringstream whitespacefilter(*tok_iter);
|
---|
| 131 | std::string key;
|
---|
| 132 | whitespacefilter >> ws >> key;
|
---|
| 133 | //LOG(2, "INFO: key to check is: " << key);
|
---|
| 134 | if (getParams().haveParameter(key)) {
|
---|
| 135 | //LOG(2, "INFO: Line submitted to parameter is: " << linestring);
|
---|
| 136 | std::stringstream linestream(linestring);
|
---|
| 137 | linestream >> getParams();
|
---|
| 138 | } else { // unknown keys are simply ignored as long as parser is incomplete
|
---|
| 139 | LOG(3, "INFO: '"+key+"' is unknown and ignored.");
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | if ((linestring.find("geometry") != string::npos) && (linestring.find("(") != string::npos)) {
|
---|
| 144 | LOG(3, "INFO: Line contains geometry and '(': " << line);
|
---|
| 145 | GeometrySection = true;
|
---|
| 146 | }
|
---|
| 147 | if ((linestring.find("psi:") != string::npos) && (linestring.find("(") != string::npos)) {
|
---|
| 148 | LOG(3, "INFO: Line contains psi: and '(': " << line);
|
---|
| 149 | Psi3Section = true;
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | // refresh atom::nr and atom::name
|
---|
| 153 | newmol->getAtomCount();
|
---|
[1bef07] | 154 | }
|
---|
| 155 |
|
---|
[03a589] | 156 | void FormatParser< psi3 >::OutputPsi3Line(ostream * const out, const atom &_atom, const Vector *center) const
|
---|
| 157 | {
|
---|
| 158 | Vector recentered(_atom.getPosition());
|
---|
| 159 | recentered -= *center;
|
---|
| 160 | *out << "\t( " << _atom.getType()->getSymbol() << "\t" << recentered[0] << "\t" << recentered[1] << "\t" << recentered[2] << " )" << endl;
|
---|
| 161 | };
|
---|
| 162 |
|
---|
| 163 |
|
---|
[41a467] | 164 | /** Saves all atoms and data into a PSI3 config file.
|
---|
[1bef07] | 165 | * \param *file output stream
|
---|
| 166 | * \param atoms atoms to store
|
---|
| 167 | */
|
---|
| 168 | void FormatParser< psi3 >::save(ostream *file, const std::vector<atom *> &atoms)
|
---|
| 169 | {
|
---|
[d640ea] | 170 | Vector center;
|
---|
| 171 | // vector<atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
| 172 |
|
---|
| 173 | // calculate center
|
---|
| 174 | for (std::vector<atom *>::const_iterator runner = atoms.begin();runner != atoms.end(); ++runner)
|
---|
| 175 | center += (*runner)->getPosition();
|
---|
| 176 | center.Scale(1./(double)atoms.size());
|
---|
| 177 |
|
---|
| 178 | // first without hessian
|
---|
| 179 | if (file->fail()) {
|
---|
[47d041] | 180 | ELOG(1, "Cannot open psi3 output file.");
|
---|
[d640ea] | 181 | } else {
|
---|
| 182 | *file << "% Created by MoleCuilder" << std::endl;
|
---|
| 183 | *file << "psi: (" << std::endl;
|
---|
| 184 | *file << "\t" << getParams().getParameterName(Psi3Parser_Parameters::labelParam)
|
---|
| 185 | << " = \"" << getParams().getParameter(Psi3Parser_Parameters::labelParam) << "\"" << std::endl;
|
---|
| 186 | *file << "\t" << getParams().getParameterName(Psi3Parser_Parameters::jobtypeParam)
|
---|
| 187 | << " = " << getParams().getParameter(Psi3Parser_Parameters::jobtypeParam) << std::endl;
|
---|
| 188 | *file << "\t" << getParams().getParameterName(Psi3Parser_Parameters::wavefunctionParam)
|
---|
| 189 | << " = " << getParams().getParameter(Psi3Parser_Parameters::wavefunctionParam) << std::endl;
|
---|
| 190 | *file << "\t" << getParams().getParameterName(Psi3Parser_Parameters::maxiterParam)
|
---|
| 191 | << " = " << getParams().getParameter(Psi3Parser_Parameters::maxiterParam) << std::endl;
|
---|
| 192 | *file << "\t" << getParams().getParameterName(Psi3Parser_Parameters::referenceParam)
|
---|
| 193 | << " = " << getParams().getParameter(Psi3Parser_Parameters::referenceParam) << std::endl;
|
---|
| 194 | *file << "\t" << getParams().getParameterName(Psi3Parser_Parameters::basisParam)
|
---|
| 195 | << " = \"" << getParams().getParameter(Psi3Parser_Parameters::basisParam) << "\"" << std::endl;
|
---|
| 196 | const std::string reference = getParams().getParameter(Psi3Parser_Parameters::referenceParam);
|
---|
| 197 | // if (reference == getParams().getReferenceName(Psi3Parser_Parameters::RHF)) {
|
---|
[1bef07] | 198 | // }
|
---|
[d640ea] | 199 | // if (reference == getParams().getReferenceName(Psi3Parser_Parameters::ROHF)) {
|
---|
[1bef07] | 200 | // }
|
---|
[d640ea] | 201 | if ((reference == getParams().getReferenceName(Psi3Parser_Parameters::UHF))
|
---|
| 202 | || (reference == getParams().getReferenceName(Psi3Parser_Parameters::TWOCON))) {
|
---|
| 203 | *file << "\t" << getParams().getParameterName(Psi3Parser_Parameters::multiplicityParam)
|
---|
| 204 | << " = " << getParams().getParameter(Psi3Parser_Parameters::multiplicityParam) << std::endl;
|
---|
| 205 | *file << "\t" << getParams().getParameterName(Psi3Parser_Parameters::chargeParam)
|
---|
| 206 | << " = " << getParams().getParameter(Psi3Parser_Parameters::chargeParam) << std::endl;
|
---|
| 207 | }
|
---|
| 208 | if (reference == getParams().getReferenceName(Psi3Parser_Parameters::TWOCON)) {
|
---|
| 209 | *file << "\t" << getParams().getParameterName(Psi3Parser_Parameters::soccParam)
|
---|
| 210 | << " = " << getParams().getParameter(Psi3Parser_Parameters::soccParam) << std::endl;
|
---|
| 211 | *file << "\t" << getParams().getParameterName(Psi3Parser_Parameters::doccParam)
|
---|
| 212 | << " = " << getParams().getParameter(Psi3Parser_Parameters::doccParam) << std::endl;
|
---|
| 213 | *file << "\t" << getParams().getParameterName(Psi3Parser_Parameters::subgroupParam)
|
---|
| 214 | << " = " << getParams().getParameter(Psi3Parser_Parameters::subgroupParam) << std::endl;
|
---|
| 215 | *file << "\t" << getParams().getParameterName(Psi3Parser_Parameters::unique_axisParam)
|
---|
| 216 | << " = " << getParams().getParameter(Psi3Parser_Parameters::unique_axisParam) << std::endl;
|
---|
| 217 | }
|
---|
| 218 | if ((reference != getParams().getReferenceName(Psi3Parser_Parameters::RHF))
|
---|
| 219 | && (reference != getParams().getReferenceName(Psi3Parser_Parameters::ROHF))
|
---|
| 220 | && (reference != getParams().getReferenceName(Psi3Parser_Parameters::UHF))
|
---|
| 221 | && (reference != getParams().getReferenceName(Psi3Parser_Parameters::TWOCON)))
|
---|
| 222 | {
|
---|
[47d041] | 223 | ELOG(0, "Unknown level of reference requested for Psi3 output file.");
|
---|
[d640ea] | 224 | }
|
---|
| 225 | *file << "\t" << getParams().getParameterName(Psi3Parser_Parameters::freeze_coreParam)
|
---|
| 226 | << " = " << getParams().getParameter(Psi3Parser_Parameters::freeze_coreParam) << std::endl;
|
---|
| 227 | *file << "\t" << getParams().getParameterName(Psi3Parser_Parameters::unitsParam)
|
---|
| 228 | << " = " << getParams().getParameter(Psi3Parser_Parameters::unitsParam) << std::endl;
|
---|
| 229 | *file << "\tgeometry = (" << std::endl;
|
---|
| 230 | // output of atoms
|
---|
[03a589] | 231 | BOOST_FOREACH(const atom *_atom, atoms) {
|
---|
| 232 | OutputPsi3Line(file, *_atom, ¢er);
|
---|
[d640ea] | 233 | }
|
---|
| 234 | *file << "\t)" << std::endl;
|
---|
| 235 | *file << "\t" << getParams().getParameterName(Psi3Parser_Parameters::originParam)
|
---|
| 236 | << " = " << getParams().getParameter(Psi3Parser_Parameters::originParam) << std::endl;
|
---|
| 237 | *file << ")" << std::endl;
|
---|
| 238 | }
|
---|
[1bef07] | 239 | }
|
---|
| 240 |
|
---|
| 241 |
|
---|