source: src/Parser/FormatParserStorage.cpp@ ebb87c

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

Removed all FormatParserStorage::get() and ::set().

  • we now call directly the template getParser<>(), setParser<>() versions in the few places needed.
  • simplified ::load() and ::save() a lot by using getTypeFromSuffix() instead of the ugly if sequence.
  • Property mode set to 100644
File size: 7.9 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
36template <> enum ParserTypes getPType<MpqcParser>() { return (enum ParserTypes)mpqc; }
37template <> enum ParserTypes getPType<PcpParser>() { return (enum ParserTypes)pcp; }
38template <> enum ParserTypes getPType<PdbParser>() { return (enum ParserTypes)pdb; }
39template <> enum ParserTypes getPType<TremoloParser>() { return (enum ParserTypes)tremolo; }
40template <> enum ParserTypes getPType<XyzParser>() { return (enum ParserTypes)xyz; }
41
42/** Increment operator for the enumeration ParserTypes to allow loops.
43 * \param &type value
44 * \return value incremented by one
45 */
46ParserTypes &operator++(ParserTypes &type)
47{
48 return type = ParserTypes(type+1);
49}
50
51/** Constructor of class FormatParserStorage.
52 */
53FormatParserStorage::FormatParserStorage()
54{
55 ParserList.resize(ParserTypes_end, NULL);
56 ParserStream.resize(ParserTypes_end, NULL);
57 ParserPresent.resize(ParserTypes_end, false);
58
59 ParserNames[mpqc] = "mpqc";
60 ParserNames[pcp] = "pcp";
61 ParserNames[pdb] = "pdb";
62 ParserNames[tremolo] = "tremolo";
63 ParserNames[xyz] = "xyz";
64
65 for (std::map<ParserTypes, std::string>::const_iterator it = ParserNames.begin(); it != ParserNames.end(); ++it)
66 ParserLookupNames.insert(pair<std::string, ParserTypes>(it->second,it->first) );
67
68 ParserSuffixes[mpqc] = "in";
69 ParserSuffixes[pcp] = "conf";
70 ParserSuffixes[pdb] = "pdb";
71 ParserSuffixes[tremolo] = "data";
72 ParserSuffixes[xyz] = "xyz";
73
74 for (std::map<ParserTypes, std::string>::const_iterator it = ParserSuffixes.begin(); it != ParserSuffixes.end(); ++it)
75 ParserLookupSuffixes.insert(pair<std::string, ParserTypes>(it->second,it->first) );
76
77 ParserAddFunction[mpqc] = &FormatParserStorage::addParser<MpqcParser>;
78 ParserAddFunction[pcp] = &FormatParserStorage::addParser<PcpParser>;
79 ParserAddFunction[pdb] = &FormatParserStorage::addParser<PdbParser>;
80 ParserAddFunction[tremolo] = &FormatParserStorage::addParser<TremoloParser>;
81 ParserAddFunction[xyz] = &FormatParserStorage::addParser<XyzParser>;
82}
83
84/** Destructor of class FormatParserStorage.
85 * Free all stored FormatParsers.
86 * Save on Exit.
87 */
88FormatParserStorage::~FormatParserStorage()
89{
90 for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
91 if (ParserPresent[iter]) {
92 if (ParserStream[iter]->is_open())
93 ParserStream[iter]->close();
94 delete ParserStream[iter];
95 delete ParserList[iter];
96 }
97}
98
99/** Sets the filename of all current parsers in storage to prefix.suffix.
100 * \param &prefix prefix to use.
101 */
102void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix)
103{
104 prefix=_prefix;
105};
106
107
108void FormatParserStorage::SaveAll()
109{
110 std::string filename;
111 for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
112 if (ParserPresent[iter]) {
113 filename = prefix;
114 filename += ".";
115 filename += ParserSuffixes[iter];
116 ParserStream[iter] = new std::ofstream(filename.c_str());
117 ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
118 }
119}
120
121
122ParserTypes FormatParserStorage::getTypeFromName(std::string type)
123{
124 if (ParserLookupNames.find(type) == ParserLookupNames.end()) {
125 DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
126 return ParserTypes_end;
127 } else
128 return ParserLookupNames[type];
129}
130
131ParserTypes FormatParserStorage::getTypeFromSuffix(std::string type)
132{
133 if (ParserLookupSuffixes.find(type) == ParserLookupSuffixes.end()) {
134 DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
135 return ParserTypes_end;
136 } else
137 return ParserLookupSuffixes[type];
138}
139
140bool FormatParserStorage::add(ParserTypes ptype)
141{
142 if (ptype != ParserTypes_end) {
143 if (ParserAddFunction.find(ptype) != ParserAddFunction.end()) {
144 DoLog(0) && (Log() << Verbose(0) << "Adding " << ParserNames[ptype] << " type to output." << endl);
145 (getInstance().*(ParserAddFunction[ptype]))(); // we still need an object to work on ...
146 return true;
147 } else {
148 DoeLog(1) && (eLog() << Verbose(1) << "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?" << endl);
149 return false;
150 }
151 } else {
152 return false;
153 }
154}
155
156bool FormatParserStorage::add(std::string type)
157{
158 return add(getTypeFromName(type));
159}
160
161
162/** Parses an istream depending on its suffix
163 * \param &input input stream
164 * \param suffix
165 * \return true - parsing ok, false - suffix unknown
166 */
167bool FormatParserStorage::load(std::istream &input, std::string suffix)
168{
169 enum ParserTypes type = getTypeFromSuffix(suffix);
170 if (type != ParserTypes_end)
171 get(type).load(&input);
172 else
173 return false;
174 return true;
175}
176
177/** Stores all selected atoms in an ostream depending on its suffix
178 * \param &output output stream
179 * \param suffix
180 * \return true - storing ok, false - suffix unknown
181 */
182bool FormatParserStorage::saveSelectedAtoms(std::ostream &output, std::string suffix)
183{
184 std::vector<atom *> atoms = World::getInstance().getSelectedAtoms();
185 return save(output, suffix, atoms);
186}
187
188/** Stores all selected atoms in an ostream depending on its suffix
189 * We store in the order of the atomic ids, not in the order they appear in the molecules.
190 * Hence, we first create a vector from all selected molecules' atoms.
191 * \param &output output stream
192 * \param suffix
193 * \return true - storing ok, false - suffix unknown
194 */
195bool FormatParserStorage::saveSelectedMolecules(std::ostream &output, std::string suffix)
196{
197 std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
198 std::map<size_t, atom *> IdAtoms;
199 for (std::vector<molecule *>::const_iterator MolIter = molecules.begin();
200 MolIter != molecules.end();
201 ++MolIter) {
202 for(molecule::atomSet::const_iterator AtomIter = (*MolIter)->begin();
203 AtomIter != (*MolIter)->end();
204 ++AtomIter) {
205 IdAtoms.insert( make_pair((*AtomIter)->getId(), (*AtomIter)) );
206 }
207 }
208 std::vector<atom *> atoms;
209 atoms.reserve(IdAtoms.size());
210 for (std::map<size_t, atom *>::const_iterator iter = IdAtoms.begin();
211 iter != IdAtoms.end();
212 ++iter) {
213 atoms.push_back(iter->second);
214 }
215 return save(output, suffix, atoms);
216}
217
218/** Stores world in an ostream depending on its suffix
219 * \param &output output stream
220 * \param suffix
221 * \return true - storing ok, false - suffix unknown
222 */
223bool FormatParserStorage::saveWorld(std::ostream &output, std::string suffix)
224{
225 std::vector<atom *> atoms = World::getInstance().getAllAtoms();
226 return save(output, suffix, atoms);
227}
228
229/** Stores a given vector of \a atoms in an ostream depending on its suffix
230 * \param &output output stream
231 * \param suffix
232 * \return true - storing ok, false - suffix unknown
233 */
234bool FormatParserStorage::save(std::ostream &output, std::string suffix, const std::vector<atom *> &atoms)
235{
236 enum ParserTypes type = getTypeFromSuffix(suffix);
237 if (type != ParserTypes_end)
238 get(type).save(&output, atoms);
239 else
240 return false;
241 return true;
242}
243
244/** Returns reference to the desired output parser as FormatParser, adds if not present.
245 * \param _type type of desired parser
246 * \return reference to the output FormatParser with desired type
247 */
248FormatParser &FormatParserStorage::get(ParserTypes _type)
249{
250 if (!ParserPresent[_type]) {
251 add(_type);
252 }
253 return *ParserList[_type];
254}
255
256CONSTRUCT_SINGLETON(FormatParserStorage)
Note: See TracBrowser for help on using the repository browser.