source: src/Parser/FormatParserStorage.cpp@ 73916f

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

Extended FormatParser::save() to use vector<atom *> to save.

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