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