source: src/Parser/FormatParserStorage.cpp@ e114e3

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 Candidate_v1.7.0 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 e114e3 was e114e3, checked in by Frederik Heber <heber@…>, 14 years ago

FormatParserStorage::add...() and ::get...() now use internally the templated functions.

  • Property mode set to 100644
File size: 8.6 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::addMpqc;
78 ParserAddFunction[pcp] = &FormatParserStorage::addPcp;
79 ParserAddFunction[pdb] = &FormatParserStorage::addPdb;
80 ParserAddFunction[tremolo] = &FormatParserStorage::addTremolo;
81 ParserAddFunction[xyz] = &FormatParserStorage::addXyz;
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 if (suffix == ParserSuffixes[mpqc]) {
170 getMpqc().load(&input);
171 } else if (suffix == ParserSuffixes[pcp]) {
172 getPcp().load(&input);
173 } else if (suffix == ParserSuffixes[pdb]) {
174 getPdb().load(&input);
175 } else if (suffix == ParserSuffixes[tremolo]) {
176 getTremolo().load(&input);
177 } else if (suffix == ParserSuffixes[xyz]) {
178 getXyz().load(&input);
179 } else {
180 DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::get()." << endl);
181 return false;
182 }
183 return true;
184}
185
186/** Stores all selected atoms in an ostream depending on its suffix
187 * \param &output output stream
188 * \param suffix
189 * \return true - storing ok, false - suffix unknown
190 */
191bool FormatParserStorage::saveSelectedAtoms(std::ostream &output, std::string suffix)
192{
193 std::vector<atom *> atoms = World::getInstance().getSelectedAtoms();
194 return save(output, suffix, atoms);
195}
196
197/** Stores all selected atoms in an ostream depending on its suffix
198 * We store in the order of the atomic ids, not in the order they appear in the molecules.
199 * Hence, we first create a vector from all selected molecules' atoms.
200 * \param &output output stream
201 * \param suffix
202 * \return true - storing ok, false - suffix unknown
203 */
204bool FormatParserStorage::saveSelectedMolecules(std::ostream &output, std::string suffix)
205{
206 std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
207 std::map<size_t, atom *> IdAtoms;
208 for (std::vector<molecule *>::const_iterator MolIter = molecules.begin();
209 MolIter != molecules.end();
210 ++MolIter) {
211 for(molecule::atomSet::const_iterator AtomIter = (*MolIter)->begin();
212 AtomIter != (*MolIter)->end();
213 ++AtomIter) {
214 IdAtoms.insert( make_pair((*AtomIter)->getId(), (*AtomIter)) );
215 }
216 }
217 std::vector<atom *> atoms;
218 atoms.reserve(IdAtoms.size());
219 for (std::map<size_t, atom *>::const_iterator iter = IdAtoms.begin();
220 iter != IdAtoms.end();
221 ++iter) {
222 atoms.push_back(iter->second);
223 }
224 return save(output, suffix, atoms);
225}
226
227/** Stores world in an ostream depending on its suffix
228 * \param &output output stream
229 * \param suffix
230 * \return true - storing ok, false - suffix unknown
231 */
232bool FormatParserStorage::saveWorld(std::ostream &output, std::string suffix)
233{
234 std::vector<atom *> atoms = World::getInstance().getAllAtoms();
235 return save(output, suffix, atoms);
236}
237
238/** Stores a given vector of \a atoms in an ostream depending on its suffix
239 * \param &output output stream
240 * \param suffix
241 * \return true - storing ok, false - suffix unknown
242 */
243bool FormatParserStorage::save(std::ostream &output, std::string suffix, const std::vector<atom *> &atoms)
244{
245 if (suffix == ParserSuffixes[mpqc]) {
246 getMpqc().save(&output, atoms);
247 } else if (suffix == ParserSuffixes[pcp]) {
248 getPcp().save(&output, atoms);
249 } else if (suffix == ParserSuffixes[pdb]) {
250 getPdb().save(&output, atoms);
251 } else if (suffix == ParserSuffixes[tremolo]) {
252 getTremolo().save(&output, atoms);
253 } else if (suffix == ParserSuffixes[xyz]) {
254 getXyz().save(&output, atoms);
255 } else {
256 DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::put()." << endl);
257 return false;
258 }
259 return true;
260}
261
262/** Returns reference to the desired output parser as FormatParser, adds if not present.
263 * \param _type type of desired parser
264 * \return reference to the output FormatParser with desired type
265 */
266FormatParser &FormatParserStorage::get(ParserTypes _type)
267{
268 if (!ParserPresent[_type]) {
269 add(_type);
270 }
271 return *ParserList[_type];
272}
273
274CONSTRUCT_SINGLETON(FormatParserStorage)
Note: See TracBrowser for help on using the repository browser.