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 |
|
---|
28 | #include "atom.hpp"
|
---|
29 | #include "config.hpp"
|
---|
30 | #include "element.hpp"
|
---|
31 | #include "molecule.hpp"
|
---|
32 | #include "CodePatterns/Log.hpp"
|
---|
33 | #include "CodePatterns/toString.hpp"
|
---|
34 | #include "CodePatterns/Verbose.hpp"
|
---|
35 | #include "LinearAlgebra/Vector.hpp"
|
---|
36 | #include "periodentafel.hpp"
|
---|
37 | #include "World.hpp"
|
---|
38 |
|
---|
39 |
|
---|
40 | /** Constructor of MpqcParser.
|
---|
41 | *
|
---|
42 | */
|
---|
43 | MpqcParser::MpqcParser()
|
---|
44 | {}
|
---|
45 |
|
---|
46 | /** Destructor of MpqcParser.
|
---|
47 | *
|
---|
48 | */
|
---|
49 | MpqcParser::~MpqcParser()
|
---|
50 | {}
|
---|
51 |
|
---|
52 | /** Load an MPQC config file into the World.
|
---|
53 | * \param *file input stream
|
---|
54 | */
|
---|
55 | void MpqcParser::load(istream *file)
|
---|
56 | {
|
---|
57 | bool MpqcSection = false;
|
---|
58 | bool MoleculeSection = false;
|
---|
59 | bool GeometrySection = false;
|
---|
60 | bool BasisSection = false;
|
---|
61 | bool AuxBasisSection = false;
|
---|
62 | char line[MAXSTRINGSIZE];
|
---|
63 | typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
|
---|
64 | boost::char_separator<char> sep("[]");
|
---|
65 | boost::char_separator<char> angularsep("<>");
|
---|
66 | boost::char_separator<char> equalitysep("=");
|
---|
67 | boost::char_separator<char> whitesep(" \t");
|
---|
68 | ConvertTo<double> toDouble;
|
---|
69 |
|
---|
70 | molecule *newmol = World::getInstance().createMolecule();
|
---|
71 | newmol->ActiveFlag = true;
|
---|
72 | // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include
|
---|
73 | World::getInstance().getMolecules()->insert(newmol);
|
---|
74 | while (file->good()) {
|
---|
75 | file->getline(line, MAXSTRINGSIZE-1);
|
---|
76 | std::string linestring(line);
|
---|
77 | if ((linestring.find("atoms geometry") == string::npos) && (linestring.find("}") != string::npos)) {
|
---|
78 | GeometrySection = false;
|
---|
79 | }
|
---|
80 | if ((linestring.find(")") != string::npos)) { // ends a section which do not overlap
|
---|
81 | MpqcSection = false;
|
---|
82 | MoleculeSection = false;
|
---|
83 | BasisSection = false;
|
---|
84 | AuxBasisSection = false;
|
---|
85 | }
|
---|
86 | if (MoleculeSection) {
|
---|
87 | if (GeometrySection) { // we have an atom
|
---|
88 | tokenizer tokens(linestring, sep);
|
---|
89 | // if (tokens.size() != 2)
|
---|
90 | // throw MpqcParseException;
|
---|
91 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
92 | std::stringstream whitespacefilter(*tok_iter++);
|
---|
93 | std::string element;
|
---|
94 | whitespacefilter >> element;
|
---|
95 | std::string vector = *tok_iter;
|
---|
96 | tokenizer vectorcomponents(vector, whitesep);
|
---|
97 | Vector X;
|
---|
98 | // if (vectorcomponents.size() != NDIM)
|
---|
99 | // throw MpqcParseException;
|
---|
100 | tok_iter = vectorcomponents.begin();
|
---|
101 | for (int i=0; i<NDIM; ++i) {
|
---|
102 | X[i] = toDouble(*tok_iter++);
|
---|
103 | }
|
---|
104 | // create atom
|
---|
105 | atom *newAtom = World::getInstance().createAtom();
|
---|
106 | newAtom->setType(World::getInstance().getPeriode()->FindElement(element));
|
---|
107 | newAtom->setPosition(X);
|
---|
108 | newmol->AddAtom(newAtom);
|
---|
109 | DoLog(1) && (Log() << Verbose(1) << "Adding atom " << *newAtom << std::endl);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | if (MpqcSection) {
|
---|
113 | tokenizer tokens(linestring, equalitysep);
|
---|
114 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
115 | std::stringstream whitespacefilter(*tok_iter++);
|
---|
116 | std::string key(*tok_iter++);
|
---|
117 | if (params.haveParam(key)) {
|
---|
118 | std::stringstream linestream(linestring);
|
---|
119 | linestream >> params;
|
---|
120 | } else if (linestring.find("mole<") != string::npos) { // get theory
|
---|
121 | tokenizer tokens(linestring, angularsep);
|
---|
122 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
123 | std::string value(*(++tok_iter));
|
---|
124 | std::stringstream linestream("theory = "+value);
|
---|
125 | linestream >> params;
|
---|
126 | } else if (linestring.find("integrals<") != string::npos) { // get theory
|
---|
127 | tokenizer tokens(linestring, angularsep);
|
---|
128 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
129 | std::string value(*(++tok_iter));
|
---|
130 | std::stringstream linestream("integration = "+value);
|
---|
131 | linestream >> params;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | if (BasisSection) {
|
---|
135 | tokenizer tokens(linestring, equalitysep);
|
---|
136 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
137 | std::string key(*tok_iter++);
|
---|
138 | std::string value(*tok_iter);
|
---|
139 | // TODO: use exception instead of ASSERT
|
---|
140 | ASSERT(tok_iter == tokens.end(),
|
---|
141 | "MpqcParser::load() - more than (key = value) on line "+linestring+".");
|
---|
142 | if (key == "name") {
|
---|
143 | std::stringstream linestream("basis = "+value);
|
---|
144 | linestream >> params;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | if (AuxBasisSection) {
|
---|
148 | tokenizer tokens(linestring, equalitysep);
|
---|
149 | tokenizer::iterator tok_iter = tokens.begin();
|
---|
150 | std::string key(*tok_iter++);
|
---|
151 | std::string value(*tok_iter);
|
---|
152 | // TODO: use exception instead of ASSERT
|
---|
153 | ASSERT(tok_iter == tokens.end(),
|
---|
154 | "MpqcParser::load() - more than (key = value) on line "+linestring+".");
|
---|
155 | if (key == "name") {
|
---|
156 | std::stringstream linestream("aux_basis = "+value);
|
---|
157 | linestream >> params;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | // set some scan flags
|
---|
161 | if (linestring.find("mpqc:") != string::npos) {
|
---|
162 | MpqcSection = true;
|
---|
163 | }
|
---|
164 | if (linestring.find("molecule<Molecule>:") != string::npos) {
|
---|
165 | MoleculeSection = true;
|
---|
166 | }
|
---|
167 | if (linestring.find("atoms geometry") != string::npos) {
|
---|
168 | GeometrySection = true;
|
---|
169 | }
|
---|
170 | if ((linestring.find("basis<GaussianBasisSet>:") != string::npos) && ((linestring.find("abasis<") == string::npos))) {
|
---|
171 | BasisSection = true;
|
---|
172 | }
|
---|
173 | if (linestring.find("abasis<") != string::npos) {
|
---|
174 | AuxBasisSection = true;
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | /** Saves all atoms and data into a MPQC config file.
|
---|
180 | * \param *file output stream
|
---|
181 | * \param atoms atoms to store
|
---|
182 | */
|
---|
183 | void MpqcParser::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 (vector<atom *>::iterator runner = allatoms.begin();runner != allatoms.end(); ++runner)
|
---|
190 | center += (*runner)->getPosition();
|
---|
191 | center.Scale(1./(double)allatoms.size());
|
---|
192 |
|
---|
193 | // first without hessian
|
---|
194 | if (file->fail()) {
|
---|
195 | DoeLog(1) && (eLog()<< Verbose(1) << "Cannot open mpqc output file." << endl);
|
---|
196 | } else {
|
---|
197 | *file << "% Created by MoleCuilder" << endl;
|
---|
198 | *file << "mpqc: (" << endl;
|
---|
199 | *file << "\tsavestate = " << params.getString(MpqcParser_Parameters::savestateParam) << endl;
|
---|
200 | *file << "\tdo_gradient = " << params.getString(MpqcParser_Parameters::do_gradientParam) << endl;
|
---|
201 | if (params.getBool(MpqcParser_Parameters::hessianParam)) {
|
---|
202 | *file << "\tfreq<MolecularFrequencies>: (" << endl;
|
---|
203 | *file << "\t\tmolecule=$:molecule" << endl;
|
---|
204 | *file << "\t)" << endl;
|
---|
205 | }
|
---|
206 | switch (params.getTheory()) {
|
---|
207 | case MpqcParser_Parameters::CLHF:
|
---|
208 | *file << "\tmole<" << params.getString(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
|
---|
209 | *file << "\t\tmolecule = $:molecule" << endl;
|
---|
210 | *file << "\t\tbasis = $:basis" << endl;
|
---|
211 | *file << "\t\tmaxiter = " << toString(params.getInt(MpqcParser_Parameters::maxiterParam))<< endl;
|
---|
212 | *file << "\t\tmemory = " << toString(params.getInt(MpqcParser_Parameters::memoryParam)) << endl;
|
---|
213 | *file << "\t)" << endl;
|
---|
214 | break;
|
---|
215 | case MpqcParser_Parameters::CLKS:
|
---|
216 | *file << "\tmole<" << params.getString(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
|
---|
217 | *file << "\t\tfunctional<StdDenFunctional>:(name=B3LYP)" << endl;
|
---|
218 | *file << "\t\tmolecule = $:molecule" << endl;
|
---|
219 | *file << "\t\tbasis = $:basis" << endl;
|
---|
220 | *file << "\t\tmaxiter = " << toString(params.getInt(MpqcParser_Parameters::maxiterParam))<< endl;
|
---|
221 | *file << "\t\tmemory = " << toString(params.getInt(MpqcParser_Parameters::memoryParam)) << endl;
|
---|
222 | *file << "\t)" << endl;
|
---|
223 | break;
|
---|
224 | case MpqcParser_Parameters::MBPT2:
|
---|
225 | *file << "\tmole<" << params.getString(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
|
---|
226 | *file << "\t\tbasis = $:basis" << endl;
|
---|
227 | *file << "\t\tmolecule = $:molecule" << endl;
|
---|
228 | *file << "\t\tmemory = " << toString(params.getInt(MpqcParser_Parameters::memoryParam)) << endl;
|
---|
229 | *file << "\t\treference<CLHF>: (" << endl;
|
---|
230 | *file << "\t\t\tmaxiter = " << toString(params.getInt(MpqcParser_Parameters::maxiterParam))<< endl;
|
---|
231 | *file << "\t\t\tbasis = $:basis" << endl;
|
---|
232 | *file << "\t\t\tmolecule = $:molecule" << endl;
|
---|
233 | *file << "\t\t\tmemory = " << toString(params.getInt(MpqcParser_Parameters::memoryParam)) << endl;
|
---|
234 | *file << "\t\t)" << endl;
|
---|
235 | *file << "\t)" << endl;
|
---|
236 | break;
|
---|
237 | case MpqcParser_Parameters::MBPT2_R12:
|
---|
238 | *file << "\tmole<" << params.getString(MpqcParser_Parameters::theoryParam) << ">: (" << endl;
|
---|
239 | *file << "\t\tmolecule = $:molecule" << endl;
|
---|
240 | *file << "\t\tbasis = $:basis" << endl;
|
---|
241 | *file << "\t\taux_basis = $:abasis" << endl;
|
---|
242 | *file << "\t\tstdapprox = \"" << params.getString(MpqcParser_Parameters::stdapproxParam) << "\"" << endl;
|
---|
243 | *file << "\t\tnfzc = " << toString(params.getInt(MpqcParser_Parameters::nfzcParam)) << endl;
|
---|
244 | *file << "\t\tmemory = " << toString(params.getInt(MpqcParser_Parameters::memoryParam)) << endl;
|
---|
245 | *file << "\t\tintegrals<IntegralCints>:()" << endl;
|
---|
246 | *file << "\t\treference<CLHF>: (" << endl;
|
---|
247 | *file << "\t\t\tmolecule = $:molecule" << endl;
|
---|
248 | *file << "\t\t\tbasis = $:basis" << endl;
|
---|
249 | *file << "\t\t\tmaxiter = " << toString(params.getInt(MpqcParser_Parameters::maxiterParam)) << endl;
|
---|
250 | *file << "\t\t\tmemory = " << toString(params.getInt(MpqcParser_Parameters::memoryParam)) << endl;
|
---|
251 | *file << "\t\t\tintegrals<" << params.getString(MpqcParser_Parameters::integrationParam) << ">:()" << endl;
|
---|
252 | *file << "\t\t)" << endl;
|
---|
253 | *file << "\t)" << endl;
|
---|
254 | break;
|
---|
255 | default:
|
---|
256 | DoeLog(0) && (eLog() << Verbose(0)
|
---|
257 | << "Unknown level of theory requested for MPQC output file." << std::endl);
|
---|
258 | break;
|
---|
259 | }
|
---|
260 | *file << ")" << endl;
|
---|
261 | *file << "molecule<Molecule>: (" << endl;
|
---|
262 | *file << "\tunit = " << (World::getInstance().getConfig()->GetIsAngstroem() ? "angstrom" : "bohr" ) << endl;
|
---|
263 | *file << "\t{ atoms geometry } = {" << endl;
|
---|
264 | // output of atoms
|
---|
265 | for (vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
|
---|
266 | (*AtomRunner)->OutputMPQCLine(file, ¢er);
|
---|
267 | }
|
---|
268 | *file << "\t}" << endl;
|
---|
269 | *file << ")" << endl;
|
---|
270 | *file << "basis<GaussianBasisSet>: (" << endl;
|
---|
271 | *file << "\tname = \"" << params.getString(MpqcParser_Parameters::basisParam) << "\"" << endl;
|
---|
272 | *file << "\tmolecule = $:molecule" << endl;
|
---|
273 | *file << ")" << endl;
|
---|
274 | if (params.getTheory() == MpqcParser_Parameters::MBPT2_R12) {
|
---|
275 | *file << "% auxiliary basis set specification" << endl;
|
---|
276 | *file << "\tabasis<GaussianBasisSet>: (" << endl;
|
---|
277 | *file << "\tname = \"" << params.getString(MpqcParser_Parameters::aux_basisParam) << "\"" << endl;
|
---|
278 | *file << "\tmolecule = $:molecule" << endl;
|
---|
279 | *file << ")" << endl;
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | MpqcParser_Parameters & MpqcParser::getParams()
|
---|
285 | {
|
---|
286 | return params;
|
---|
287 | }
|
---|
288 |
|
---|