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