source: src/Parser/MpqcParser.cpp@ b1b413

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since b1b413 was 52cac0, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: MpqcParser now writes atom coordinates with precision of 10.

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