source: src/Parser/MpqcParser.cpp@ 39fa96

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 39fa96 was 765f16, checked in by Frederik Heber <heber@…>, 13 years ago

Huge Refactoring of FormatParserStorage and all FormatParser's.

FormatParser:

FormatParserStorage:

  • We construct all lookup tables and alikes via boost preprocessor magic from a list of all available parser (enums).
  • we use the templated getParser() and addParser() functions to serve parsers to the outside. Instances are as usual created only once.

Tests:

  • tests only had to be changed because of the "renaming" of the Parsers.
  • Property mode set to 100644
File size: 13.4 KB
Line 
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
41const std::string FormatParserTrait<mpqc>::name = "mpqc";
42const std::string FormatParserTrait<mpqc>::suffix = "in";
43const ParserTypes FormatParserTrait<mpqc>::type = mpqc;
44
45/** Constructor of MpqcParser.
46 *
47 */
48FormatParser< mpqc >::FormatParser() :
49 FormatParser_common(new MpqcParser_Parameters())
50{}
51
52/** Destructor of MpqcParser.
53 *
54 */
55FormatParser< mpqc >::~FormatParser()
56{}
57
58/** Load an MPQC config file into the World.
59 * \param *file input stream
60 */
61void 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 */
219void 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, &center);
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
Note: See TracBrowser for help on using the repository browser.