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