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