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 | * MpqcParser.cpp
|
---|
10 | *
|
---|
11 | * Created on: 12.06.2010
|
---|
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 "MpqcParser.hpp"
|
---|
27 | #include "MpqcParser_Parameters.hpp"
|
---|
28 |
|
---|
29 | #include "atom.hpp"
|
---|
30 | #include "config.hpp"
|
---|
31 | #include "Element/element.hpp"
|
---|
32 | #include "molecule.hpp"
|
---|
33 | #include "MoleculeListClass.hpp"
|
---|
34 | #include "CodePatterns/Log.hpp"
|
---|
35 | #include "CodePatterns/toString.hpp"
|
---|
36 | #include "CodePatterns/Verbose.hpp"
|
---|
37 | #include "LinearAlgebra/Vector.hpp"
|
---|
38 | #include "Element/periodentafel.hpp"
|
---|
39 | #include "World.hpp"
|
---|
40 |
|
---|
41 | // declare specialized static variables
|
---|
42 | const std::string FormatParserTrait<mpqc>::name = "mpqc";
|
---|
43 | const std::string FormatParserTrait<mpqc>::suffix = "in";
|
---|
44 | const ParserTypes FormatParserTrait<mpqc>::type = mpqc;
|
---|
45 |
|
---|
46 | // a converter we often need
|
---|
47 | ConvertTo<bool> FormatParser<mpqc>::Converter;
|
---|
48 |
|
---|
49 | /** Constructor of MpqcParser.
|
---|
50 | *
|
---|
51 | */
|
---|
52 | FormatParser< mpqc >::FormatParser() :
|
---|
53 | FormatParser_common(new MpqcParser_Parameters())
|
---|
54 | {}
|
---|
55 |
|
---|
56 | /** Destructor of MpqcParser.
|
---|
57 | *
|
---|
58 | */
|
---|
59 | FormatParser< mpqc >::~FormatParser()
|
---|
60 | {}
|
---|
61 |
|
---|
62 | /** Load an MPQC config file into the World.
|
---|
63 | * \param *file input stream
|
---|
64 | */
|
---|
65 | void FormatParser< mpqc >::load(istream *file)
|
---|
66 | {
|
---|
67 | bool MpqcSection = false;
|
---|
68 | bool MoleculeSection = false;
|
---|
69 | bool GeometrySection = false;
|
---|
70 | bool BasisSection = false;
|
---|
71 | bool AuxBasisSection = false;
|
---|
72 | char line[MAXSTRINGSIZE];
|
---|
73 | typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
|
---|
74 | boost::char_separator<char> sep("[]");
|
---|
75 | boost::char_separator<char> angularsep("<>");
|
---|
76 | boost::char_separator<char> equalitysep(" =");
|
---|
77 | boost::char_separator<char> whitesep(" \t");
|
---|
78 | ConvertTo<double> toDouble;
|
---|
79 |
|
---|
80 | molecule *newmol = World::getInstance().createMolecule();
|
---|
81 | newmol->ActiveFlag = true;
|
---|
82 | // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include
|
---|
83 | World::getInstance().getMolecules()->insert(newmol);
|
---|
84 | while (file->good()) {
|
---|
85 | file->getline(line, MAXSTRINGSIZE-1);
|
---|
86 | std::string linestring(line);
|
---|
87 | if ((linestring.find("atoms geometry") == string::npos) && (linestring.find("}") != string::npos)) {
|
---|
88 | GeometrySection = false;
|
---|
89 | }
|
---|
90 | if ((linestring.find(")") != string::npos)) { // ends a section which do not overlap
|
---|
91 | MpqcSection = false;
|
---|
92 | MoleculeSection = false;
|
---|
93 | BasisSection = false;
|
---|
94 | AuxBasisSection = false;
|
---|
95 | }
|
---|
96 | if (MoleculeSection) {
|
---|
97 | if (GeometrySection) { // we have an atom
|
---|
98 | tokenizer tokens(linestring, sep);
|
---|
99 | // if (tokens.size() != 2)
|
---|
100 | // throw MpqcParseException;
|
---|
101 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
102 | ASSERT(tok_iter != tokens.end(),
|
---|
103 | "FormatParser< mpqc >::load() - missing token for MoleculeSection in line "+linestring+"!");
|
---|
104 | std::stringstream whitespacefilter(*tok_iter++);
|
---|
105 | std::string element;
|
---|
106 | whitespacefilter >> ws >> element;
|
---|
107 | ASSERT(tok_iter != tokens.end(),
|
---|
108 | "FormatParser< mpqc >::load() - missing token for MoleculeSection in line "+linestring+"!");
|
---|
109 | std::string vector = *tok_iter;
|
---|
110 | tokenizer vectorcomponents(vector, whitesep);
|
---|
111 | Vector X;
|
---|
112 | // if (vectorcomponents.size() != NDIM)
|
---|
113 | // throw MpqcParseException;
|
---|
114 | tok_iter = vectorcomponents.begin();
|
---|
115 | for (int i=0; i<NDIM; ++i) {
|
---|
116 | X[i] = toDouble(*tok_iter++);
|
---|
117 | }
|
---|
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 | DoLog(1) && (Log() << Verbose(1) << "Adding atom " << *newAtom << std::endl);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | if (MpqcSection) {
|
---|
127 | if (linestring.find("mole<") != string::npos) { // get theory
|
---|
128 | tokenizer tokens(linestring, angularsep);
|
---|
129 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
130 | ++tok_iter;
|
---|
131 | ASSERT(tok_iter != tokens.end(),
|
---|
132 | "FormatParser< mpqc >::load() - missing token in brackets<> for mole< in line "+linestring+"!");
|
---|
133 | std::string value(*tok_iter);
|
---|
134 | std::stringstream linestream("theory = "+value);
|
---|
135 | linestream >> getParams();
|
---|
136 | } else if (linestring.find("integrals<") != string::npos) { // get theory
|
---|
137 | tokenizer tokens(linestring, angularsep);
|
---|
138 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
139 | ++tok_iter;
|
---|
140 | ASSERT(tok_iter != tokens.end(),
|
---|
141 | "FormatParser< mpqc >::load() - missing token in brackets<> for integrals< in line "+linestring+"!");
|
---|
142 | std::string value(*tok_iter);
|
---|
143 | std::stringstream linestream("integration = "+value);
|
---|
144 | linestream >> getParams();
|
---|
145 | } else if ((linestring.find("molecule") == string::npos) && (linestring.find("basis") == string::npos)){
|
---|
146 | // molecule and basis must not be parsed in this section
|
---|
147 | tokenizer tokens(linestring, equalitysep);
|
---|
148 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
149 | ASSERT(tok_iter != tokens.end(),
|
---|
150 | "FormatParser< mpqc >::load() - missing token before '=' for MpqcSection in line "+linestring+"!");
|
---|
151 | std::stringstream whitespacefilter(*tok_iter);
|
---|
152 | std::string key;
|
---|
153 | whitespacefilter >> ws >> key;
|
---|
154 | if (getParams().haveParameter(key)) {
|
---|
155 | std::stringstream linestream(linestring);
|
---|
156 | linestream >> getParams();
|
---|
157 | } else { // unknown keys are simply ignored as long as parser is incomplete
|
---|
158 | DoLog(2) && (Log() << Verbose(2) << "INFO: '"+key+"' is unknown and ignored." << std::endl);
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 | if (BasisSection) {
|
---|
163 | tokenizer tokens(linestring, equalitysep);
|
---|
164 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
165 | ASSERT(tok_iter != tokens.end(),
|
---|
166 | "FormatParser< mpqc >::load() - missing token for BasisSection in line "+linestring+"!");
|
---|
167 | std::string key(*tok_iter++);
|
---|
168 | ASSERT(tok_iter != tokens.end(),
|
---|
169 | "FormatParser< mpqc >::load() - missing value for BasisSection after key "+key+" in line "+linestring+"!");
|
---|
170 | std::string value(*tok_iter);
|
---|
171 | tok_iter++;
|
---|
172 | // TODO: use exception instead of ASSERT
|
---|
173 | ASSERT(tok_iter == tokens.end(),
|
---|
174 | "FormatParser< mpqc >::load() - more than (key = value) on line "+linestring+".");
|
---|
175 | if (key == "name") {
|
---|
176 | std::stringstream linestream("basis = "+value);
|
---|
177 | linestream >> getParams();
|
---|
178 | }
|
---|
179 | }
|
---|
180 | if (AuxBasisSection) {
|
---|
181 | tokenizer tokens(linestring, equalitysep);
|
---|
182 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
183 | ASSERT(tok_iter != tokens.end(),
|
---|
184 | "FormatParser< mpqc >::load() - missing token for AuxBasisSection in line "+linestring+"!");
|
---|
185 | std::string key(*tok_iter++);
|
---|
186 | ASSERT(tok_iter != tokens.end(),
|
---|
187 | "FormatParser< mpqc >::load() - missing value for BasisSection after key "+key+" in line "+linestring+"!");
|
---|
188 | std::string value(*tok_iter);
|
---|
189 | tok_iter++;
|
---|
190 | // TODO: use exception instead of ASSERT
|
---|
191 | ASSERT(tok_iter == tokens.end(),
|
---|
192 | "FormatParser< mpqc >::load() - more than (key = value) on line "+linestring+".");
|
---|
193 | if (key == "name") {
|
---|
194 | std::stringstream linestream("aux_basis = "+value);
|
---|
195 | linestream >> getParams();
|
---|
196 | }
|
---|
197 | }
|
---|
198 | // set some scan flags
|
---|
199 | if (linestring.find("mpqc:") != string::npos) {
|
---|
200 | MpqcSection = true;
|
---|
201 | }
|
---|
202 | if (linestring.find("molecule<Molecule>:") != string::npos) {
|
---|
203 | MoleculeSection = true;
|
---|
204 | }
|
---|
205 | if (linestring.find("atoms geometry") != string::npos) {
|
---|
206 | GeometrySection = true;
|
---|
207 | }
|
---|
208 | if ((linestring.find("basis<GaussianBasisSet>:") != string::npos) && ((linestring.find("abasis<") == string::npos))) {
|
---|
209 | BasisSection = true;
|
---|
210 | }
|
---|
211 | if (linestring.find("abasis<") != string::npos) {
|
---|
212 | AuxBasisSection = true;
|
---|
213 | }
|
---|
214 | }
|
---|
215 | // refresh atom::nr and atom::name
|
---|
216 | newmol->getAtomCount();
|
---|
217 | }
|
---|
218 |
|
---|
219 | /** Saves all atoms and data into a MPQC config file.
|
---|
220 | * \param *file output stream
|
---|
221 | * \param atoms atoms to store
|
---|
222 | */
|
---|
223 | void FormatParser< mpqc >::save(ostream *file, const std::vector<atom *> &atoms)
|
---|
224 | {
|
---|
225 | Vector center;
|
---|
226 | // vector<atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
227 |
|
---|
228 | // calculate center
|
---|
229 | for (std::vector<atom *>::const_iterator runner = atoms.begin();runner != atoms.end(); ++runner)
|
---|
230 | center += (*runner)->getPosition();
|
---|
231 | center.Scale(1./(double)atoms.size());
|
---|
232 |
|
---|
233 | // first without hessian
|
---|
234 | if (file->fail()) {
|
---|
235 | DoeLog(1) && (eLog()<< Verbose(1) << "Cannot open mpqc output file." << endl);
|
---|
236 | } else {
|
---|
237 | *file << "% Created by MoleCuilder" << endl;
|
---|
238 | *file << "mpqc: (" << endl;
|
---|
239 | *file << "\tsavestate = " << getParams().getParameter(MpqcParser_Parameters::savestateParam) << endl;
|
---|
240 | *file << "\tdo_gradient = " << getParams().getParameter(MpqcParser_Parameters::do_gradientParam) << endl;
|
---|
241 | if (Converter(getParams().getParameter(MpqcParser_Parameters::hessianParam))) {
|
---|
242 | *file << "\tfreq<MolecularFrequencies>: (" << endl;
|
---|
243 | *file << "\t\tmolecule=$:molecule" << endl;
|
---|
244 | *file << "\t)" << endl;
|
---|
245 | }
|
---|
246 | const std::string theory = getParams().getParameter(MpqcParser_Parameters::theoryParam);
|
---|
247 | if (theory == getParams().getTheoryName(MpqcParser_Parameters::CLHF)) {
|
---|
248 | *file << "\tmole<" << getParams().getParameter(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
|
---|
249 | *file << "\t\tmolecule = $:molecule" << endl;
|
---|
250 | *file << "\t\t" << getParams().getParameterName(MpqcParser_Parameters::basisParam) << " = $:basis" << endl;
|
---|
251 | *file << "\t\t" << getParams().getParameterName(MpqcParser_Parameters::maxiterParam)
|
---|
252 | << " = " << getParams().getParameter(MpqcParser_Parameters::maxiterParam)<< endl;
|
---|
253 | *file << "\t\t" << getParams().getParameterName(MpqcParser_Parameters::memoryParam)
|
---|
254 | << " = " << getParams().getParameter(MpqcParser_Parameters::memoryParam) << endl;
|
---|
255 | *file << "\t)" << endl;
|
---|
256 | } else if (theory == getParams().getTheoryName(MpqcParser_Parameters::CLKS)) {
|
---|
257 | *file << "\tmole<" << getParams().getParameter(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
|
---|
258 | *file << "\t\tfunctional<StdDenFunctional>:(name=B3LYP)" << endl;
|
---|
259 | *file << "\t\tmolecule = $:molecule" << endl;
|
---|
260 | *file << "\t\t" << getParams().getParameterName(MpqcParser_Parameters::basisParam) << " = $:basis" << endl;
|
---|
261 | *file << "\t\t" << getParams().getParameterName(MpqcParser_Parameters::maxiterParam)
|
---|
262 | << " = " << getParams().getParameter(MpqcParser_Parameters::maxiterParam)<< endl;
|
---|
263 | *file << "\t\t" << getParams().getParameterName(MpqcParser_Parameters::memoryParam)
|
---|
264 | << " = " << getParams().getParameter(MpqcParser_Parameters::memoryParam) << endl;
|
---|
265 | *file << "\t)" << endl;
|
---|
266 | } else if (theory == getParams().getTheoryName(MpqcParser_Parameters::MBPT2)) {
|
---|
267 | *file << "\tmole<" << getParams().getParameter(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
|
---|
268 | *file << "\t\t" << getParams().getParameterName(MpqcParser_Parameters::basisParam) << " = $:basis" << endl;
|
---|
269 | *file << "\t\tmolecule = $:molecule" << endl;
|
---|
270 | *file << "\t\t" << getParams().getParameterName(MpqcParser_Parameters::memoryParam)
|
---|
271 | << " = " << getParams().getParameter(MpqcParser_Parameters::memoryParam) << endl;
|
---|
272 | *file << "\t\treference<CLHF>: (" << endl;
|
---|
273 | *file << "\t\t\t" << getParams().getParameterName(MpqcParser_Parameters::maxiterParam)
|
---|
274 | << " = " << getParams().getParameter(MpqcParser_Parameters::maxiterParam)<< endl;
|
---|
275 | *file << "\t\t\t" << getParams().getParameterName(MpqcParser_Parameters::basisParam) << " = $:basis" << endl;
|
---|
276 | *file << "\t\t\tmolecule = $:molecule" << endl;
|
---|
277 | *file << "\t\t\t" << getParams().getParameterName(MpqcParser_Parameters::memoryParam)
|
---|
278 | << " = " << getParams().getParameter(MpqcParser_Parameters::memoryParam) << endl;
|
---|
279 | *file << "\t\t)" << endl;
|
---|
280 | *file << "\t)" << endl;
|
---|
281 | } else if (theory == getParams().getTheoryName(MpqcParser_Parameters::MBPT2_R12)) {
|
---|
282 | *file << "\tmole<" << getParams().getParameter(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
|
---|
283 | *file << "\t\tmolecule = $:molecule" << endl;
|
---|
284 | *file << "\t\t" << getParams().getParameterName(MpqcParser_Parameters::basisParam) << " = $:basis" << endl;
|
---|
285 | *file << "\t\t" << getParams().getParameterName(MpqcParser_Parameters::aux_basisParam) << " = $:abasis" << endl;
|
---|
286 | *file << "\t\t" << getParams().getParameterName(MpqcParser_Parameters::stdapproxParam)
|
---|
287 | << " = \"" << getParams().getParameter(MpqcParser_Parameters::stdapproxParam) << "\"" << endl;
|
---|
288 | *file << "\t\t" << getParams().getParameterName(MpqcParser_Parameters::nfzcParam)
|
---|
289 | << " = " << getParams().getParameter(MpqcParser_Parameters::nfzcParam) << endl;
|
---|
290 | *file << "\t\t" << getParams().getParameterName(MpqcParser_Parameters::memoryParam)
|
---|
291 | << " = " << getParams().getParameter(MpqcParser_Parameters::memoryParam) << endl;
|
---|
292 | *file << "\t\tintegrals<IntegralCints>:()" << endl;
|
---|
293 | *file << "\t\treference<CLHF>: (" << endl;
|
---|
294 | *file << "\t\t\tmolecule = $:molecule" << endl;
|
---|
295 | *file << "\t\t\t" << getParams().getParameterName(MpqcParser_Parameters::basisParam) << " = $:basis" << endl;
|
---|
296 | *file << "\t\t\tmaxiter = " << getParams().getParameter(MpqcParser_Parameters::maxiterParam) << endl;
|
---|
297 | *file << "\t\t\tmemory = " << getParams().getParameter(MpqcParser_Parameters::memoryParam) << endl;
|
---|
298 | *file << "\t\t\tintegrals<" << getParams().getParameter(MpqcParser_Parameters::integrationParam) << ">:()" << endl;
|
---|
299 | *file << "\t\t)" << endl;
|
---|
300 | *file << "\t)" << endl;
|
---|
301 | } else {
|
---|
302 | DoeLog(0) && (eLog() << Verbose(0)
|
---|
303 | << "Unknown level of theory requested for MPQC output file." << std::endl);
|
---|
304 | }
|
---|
305 | *file << ")" << endl;
|
---|
306 | *file << "molecule<Molecule>: (" << endl;
|
---|
307 | *file << "\tunit = " << (World::getInstance().getConfig()->GetIsAngstroem() ? "angstrom" : "bohr" ) << endl;
|
---|
308 | *file << "\t{ atoms geometry } = {" << endl;
|
---|
309 | // output of atoms
|
---|
310 | for (std::vector<atom *>::const_iterator AtomRunner = atoms.begin(); AtomRunner != atoms.end(); ++AtomRunner) {
|
---|
311 | (*AtomRunner)->OutputMPQCLine(file, ¢er);
|
---|
312 | }
|
---|
313 | *file << "\t}" << endl;
|
---|
314 | *file << ")" << endl;
|
---|
315 | *file << "basis<GaussianBasisSet>: (" << endl;
|
---|
316 | *file << "\tname = \"" << getParams().getParameter(MpqcParser_Parameters::basisParam) << "\"" << endl;
|
---|
317 | *file << "\tmolecule = $:molecule" << endl;
|
---|
318 | *file << ")" << endl;
|
---|
319 | if (theory == getParams().getTheoryName(MpqcParser_Parameters::MBPT2_R12)) {
|
---|
320 | *file << "% auxiliary basis set specification" << endl;
|
---|
321 | *file << "\tabasis<GaussianBasisSet>: (" << endl;
|
---|
322 | *file << "\tname = \"" << getParams().getParameter(MpqcParser_Parameters::aux_basisParam) << "\"" << endl;
|
---|
323 | *file << "\tmolecule = $:molecule" << endl;
|
---|
324 | *file << ")" << endl;
|
---|
325 | }
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 |
|
---|