source: src/Parser/FormatParserStorage.cpp@ db67ea

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

Added template getParser/setParser to FormatParserStorage.

  • specialized templated getParser/setParser in FormatParserStorage will soon replace all the add..() and get...() functions.
  • Property mode set to 100644
File size: 11.3 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/** \file FormatParserStorage.cpp
9 *
10 * date: Jun, 22 2010
11 * author: heber
12 *
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "CodePatterns/MemDebug.hpp"
21
22#include <iostream>
23#include <fstream>
24
25#include "Parser/FormatParserStorage.hpp"
26
27#include "CodePatterns/Log.hpp"
28#include "CodePatterns/Verbose.hpp"
29
30#include "CodePatterns/Assert.hpp"
31
32#include "molecule.hpp"
33
34#include "CodePatterns/Singleton_impl.hpp"
35
36/** Increment operator for the enumeration ParserTypes to allow loops.
37 * \param &type value
38 * \return value incremented by one
39 */
40ParserTypes &operator++(ParserTypes &type)
41{
42 return type = ParserTypes(type+1);
43}
44
45/** Constructor of class FormatParserStorage.
46 */
47FormatParserStorage::FormatParserStorage()
48{
49 ParserList.resize(ParserTypes_end, NULL);
50 ParserStream.resize(ParserTypes_end, NULL);
51 ParserPresent.resize(ParserTypes_end, false);
52
53 ParserNames[mpqc] = "mpqc";
54 ParserNames[pcp] = "pcp";
55 ParserNames[pdb] = "pdb";
56 ParserNames[tremolo] = "tremolo";
57 ParserNames[xyz] = "xyz";
58
59 for (std::map<ParserTypes, std::string>::const_iterator it = ParserNames.begin(); it != ParserNames.end(); ++it)
60 ParserLookupNames.insert(pair<std::string, ParserTypes>(it->second,it->first) );
61
62 ParserSuffixes[mpqc] = "in";
63 ParserSuffixes[pcp] = "conf";
64 ParserSuffixes[pdb] = "pdb";
65 ParserSuffixes[tremolo] = "data";
66 ParserSuffixes[xyz] = "xyz";
67
68 for (std::map<ParserTypes, std::string>::const_iterator it = ParserSuffixes.begin(); it != ParserSuffixes.end(); ++it)
69 ParserLookupSuffixes.insert(pair<std::string, ParserTypes>(it->second,it->first) );
70
71 ParserAddFunction[mpqc] = &FormatParserStorage::addMpqc;
72 ParserAddFunction[pcp] = &FormatParserStorage::addPcp;
73 ParserAddFunction[pdb] = &FormatParserStorage::addPdb;
74 ParserAddFunction[tremolo] = &FormatParserStorage::addTremolo;
75 ParserAddFunction[xyz] = &FormatParserStorage::addXyz;
76}
77
78/** Destructor of class FormatParserStorage.
79 * Free all stored FormatParsers.
80 * Save on Exit.
81 */
82FormatParserStorage::~FormatParserStorage()
83{
84 for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
85 if (ParserPresent[iter]) {
86 if (ParserStream[iter]->is_open())
87 ParserStream[iter]->close();
88 delete ParserStream[iter];
89 delete ParserList[iter];
90 }
91}
92
93/** Sets the filename of all current parsers in storage to prefix.suffix.
94 * \param &prefix prefix to use.
95 */
96void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix)
97{
98 prefix=_prefix;
99};
100
101
102void FormatParserStorage::SaveAll()
103{
104 std::string filename;
105 for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
106 if (ParserPresent[iter]) {
107 filename = prefix;
108 filename += ".";
109 filename += ParserSuffixes[iter];
110 ParserStream[iter] = new std::ofstream(filename.c_str());
111 ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
112 }
113}
114
115
116/** Adds an MpqcParser to the storage.
117 */
118void FormatParserStorage::addMpqc()
119{
120 if (!ParserPresent[mpqc]) {
121 ParserList[mpqc] = dynamic_cast<FormatParser *>(new MpqcParser);
122 ParserPresent[mpqc] = true;
123 }
124 else
125 DoeLog(2) && (eLog() << Verbose(2) << "Parser mpqc is already present." << endl
126 << "Note that you don't need to add '-o mpqc' if the input file is of type mpqc." << endl);
127}
128
129
130/** Adds an PcpParser to the storage.
131 */
132void FormatParserStorage::addPcp()
133{
134 if (!ParserPresent[pcp]) {
135 ParserList[pcp] = new PcpParser();
136 ParserPresent[pcp] = true;
137 } else
138 DoeLog(2) && (eLog() << Verbose(2) << "Parser pcp is already present." << endl
139 << "Note that you don't need to add '-o pcp' if the input file is of type pcp." << endl);
140}
141
142
143/** Adds an PdbParser to the storage.
144 */
145void FormatParserStorage::addPdb()
146{
147 if (!ParserPresent[pdb]) {
148 ParserList[pdb] = new PdbParser();
149 ParserPresent[pdb] = true;
150 } else
151 DoeLog(2) && (eLog() << Verbose(2) << "Parser pdb is already present." << endl
152 << "Note that you don't need to add '-o pdb' if the input file is of type pdb." << endl);
153}
154
155
156/** Adds an TremoloParser to the storage.
157 */
158void FormatParserStorage::addTremolo()
159{
160 if (!ParserPresent[tremolo]) {
161 ParserList[tremolo] = new TremoloParser();
162 ParserPresent[tremolo] = true;
163 } else
164 DoeLog(2) && (eLog() << Verbose(2) << "Parser tremolo is already present." << endl
165 << "Note that you don't need to add '-o tremolo' if the input file is of type tremolo." << endl);
166}
167
168
169/** Adds an XyzParser to the storage.
170 */
171void FormatParserStorage::addXyz()
172{
173 if (!ParserPresent[xyz]) {
174 ParserList[xyz] = new XyzParser();
175 ParserPresent[xyz] = true;
176 } else
177 DoeLog(2) && (eLog() << Verbose(2) << "Parser xyz is already present." << endl
178 << "Note that you don't need to add '-o xyz' if the input file is of type xyz." << endl);
179}
180
181ParserTypes FormatParserStorage::getTypeFromName(std::string type)
182{
183 if (ParserLookupNames.find(type) == ParserLookupNames.end()) {
184 DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
185 return ParserTypes_end;
186 } else
187 return ParserLookupNames[type];
188}
189
190ParserTypes FormatParserStorage::getTypeFromSuffix(std::string type)
191{
192 if (ParserLookupSuffixes.find(type) == ParserLookupSuffixes.end()) {
193 DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
194 return ParserTypes_end;
195 } else
196 return ParserLookupSuffixes[type];
197}
198
199bool FormatParserStorage::add(ParserTypes ptype)
200{
201 if (ptype != ParserTypes_end) {
202 if (ParserAddFunction.find(ptype) != ParserAddFunction.end()) {
203 DoLog(0) && (Log() << Verbose(0) << "Adding " << ParserNames[ptype] << " type to output." << endl);
204 (getInstance().*(ParserAddFunction[ptype]))(); // we still need an object to work on ...
205 return true;
206 } else {
207 DoeLog(1) && (eLog() << Verbose(1) << "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?" << endl);
208 return false;
209 }
210 } else {
211 return false;
212 }
213}
214
215bool FormatParserStorage::add(std::string type)
216{
217 return add(getTypeFromName(type));
218}
219
220
221/** Parses an istream depending on its suffix
222 * \param &input input stream
223 * \param suffix
224 * \return true - parsing ok, false - suffix unknown
225 */
226bool FormatParserStorage::load(std::istream &input, std::string suffix)
227{
228 if (suffix == ParserSuffixes[mpqc]) {
229 getMpqc().load(&input);
230 } else if (suffix == ParserSuffixes[pcp]) {
231 getPcp().load(&input);
232 } else if (suffix == ParserSuffixes[pdb]) {
233 getPdb().load(&input);
234 } else if (suffix == ParserSuffixes[tremolo]) {
235 getTremolo().load(&input);
236 } else if (suffix == ParserSuffixes[xyz]) {
237 getXyz().load(&input);
238 } else {
239 DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::get()." << endl);
240 return false;
241 }
242 return true;
243}
244
245/** Stores all selected atoms in an ostream depending on its suffix
246 * \param &output output stream
247 * \param suffix
248 * \return true - storing ok, false - suffix unknown
249 */
250bool FormatParserStorage::saveSelectedAtoms(std::ostream &output, std::string suffix)
251{
252 std::vector<atom *> atoms = World::getInstance().getSelectedAtoms();
253 return save(output, suffix, atoms);
254}
255
256/** Stores all selected atoms in an ostream depending on its suffix
257 * We store in the order of the atomic ids, not in the order they appear in the molecules.
258 * Hence, we first create a vector from all selected molecules' atoms.
259 * \param &output output stream
260 * \param suffix
261 * \return true - storing ok, false - suffix unknown
262 */
263bool FormatParserStorage::saveSelectedMolecules(std::ostream &output, std::string suffix)
264{
265 std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
266 std::map<size_t, atom *> IdAtoms;
267 for (std::vector<molecule *>::const_iterator MolIter = molecules.begin();
268 MolIter != molecules.end();
269 ++MolIter) {
270 for(molecule::atomSet::const_iterator AtomIter = (*MolIter)->begin();
271 AtomIter != (*MolIter)->end();
272 ++AtomIter) {
273 IdAtoms.insert( make_pair((*AtomIter)->getId(), (*AtomIter)) );
274 }
275 }
276 std::vector<atom *> atoms;
277 atoms.reserve(IdAtoms.size());
278 for (std::map<size_t, atom *>::const_iterator iter = IdAtoms.begin();
279 iter != IdAtoms.end();
280 ++iter) {
281 atoms.push_back(iter->second);
282 }
283 return save(output, suffix, atoms);
284}
285
286/** Stores world in an ostream depending on its suffix
287 * \param &output output stream
288 * \param suffix
289 * \return true - storing ok, false - suffix unknown
290 */
291bool FormatParserStorage::saveWorld(std::ostream &output, std::string suffix)
292{
293 std::vector<atom *> atoms = World::getInstance().getAllAtoms();
294 return save(output, suffix, atoms);
295}
296
297/** Stores a given vector of \a atoms in an ostream depending on its suffix
298 * \param &output output stream
299 * \param suffix
300 * \return true - storing ok, false - suffix unknown
301 */
302bool FormatParserStorage::save(std::ostream &output, std::string suffix, const std::vector<atom *> &atoms)
303{
304 if (suffix == ParserSuffixes[mpqc]) {
305 getMpqc().save(&output, atoms);
306 } else if (suffix == ParserSuffixes[pcp]) {
307 getPcp().save(&output, atoms);
308 } else if (suffix == ParserSuffixes[pdb]) {
309 getPdb().save(&output, atoms);
310 } else if (suffix == ParserSuffixes[tremolo]) {
311 getTremolo().save(&output, atoms);
312 } else if (suffix == ParserSuffixes[xyz]) {
313 getXyz().save(&output, atoms);
314 } else {
315 DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::put()." << endl);
316 return false;
317 }
318 return true;
319}
320
321/** Returns reference to the output MpqcParser, adds if not present.
322 * \return reference to the output MpqcParser
323 */
324MpqcParser &FormatParserStorage::getMpqc()
325{
326 if (!ParserPresent[mpqc])
327 addMpqc();
328 return dynamic_cast<MpqcParser &>(*ParserList[mpqc]);
329}
330
331/** Returns reference to the output PcpParser, adds if not present.
332 * \return reference to the output PcpParser
333 */
334PcpParser &FormatParserStorage::getPcp()
335{
336 if (!ParserPresent[pcp])
337 addPcp();
338 return dynamic_cast<PcpParser &>(*ParserList[pcp]);
339}
340
341/** Returns reference to the output PdbParser, adds if not present.
342 * \return reference to the output PdbParser
343 */
344PdbParser &FormatParserStorage::getPdb()
345{
346 if (!ParserPresent[pdb])
347 addPdb();
348 return dynamic_cast<PdbParser &>(*ParserList[pdb]);
349}
350
351/** Returns reference to the output TremoloParser, adds if not present.
352 * \return reference to the output TremoloParser
353 */
354TremoloParser &FormatParserStorage::getTremolo()
355{
356 if (!ParserPresent[tremolo])
357 addTremolo();
358 return dynamic_cast<TremoloParser &>(*ParserList[tremolo]);
359}
360
361/** Returns reference to the output XyzParser, adds if not present.
362 * \return reference to the output XyzParser
363 */
364XyzParser &FormatParserStorage::getXyz()
365{
366 if (!ParserPresent[xyz])
367 addXyz();
368 return dynamic_cast<XyzParser &>(*ParserList[xyz]);
369}
370
371/** Returns reference to the desired output parser as FormatParser, adds if not present.
372 * \param _type type of desired parser
373 * \return reference to the output FormatParser with desired type
374 */
375FormatParser &FormatParserStorage::get(ParserTypes _type)
376{
377 if (!ParserPresent[_type]) {
378 add(_type);
379 }
380 return *ParserList[_type];
381}
382
383CONSTRUCT_SINGLETON(FormatParserStorage)
Note: See TracBrowser for help on using the repository browser.