source: src/Parser/FormatParserStorage.cpp@ cabb46

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

Extended LoadXyz and SaveXyz to any format.

Testchanges:

  • copied all from Simple_configuration/2/pre to ./post.
  • changes to test.conf:

MaxPsiStep (if 0 given, then 3 is set).
MaxMinGapStopStep (if 0 given, then 1 is set).
InitMaxMinGapStopStep (if 0 given, then 1 is set).

  • changes to test.pdb:

segName is tes (from _tes_t.pdb) not non.
Obtaining molecule names from pdb file's segName is not implemented yet.

  • ParserCommonUnitTest::rewriteAnXyzTest(), we don't check for string equality but parse the output stream again and check whether first and second half of all atoms are equal with respect to position and element.
  • all regression test that diff an xyz file now use -I '.*Created by....*'
  • changed regression tests with respect to changed short forms.
  • Property mode set to 100644
File size: 8.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 "Patterns/Singleton_impl.hpp"
40
41/** Increment operator for the enumeration ParserTypes to allow loops.
42 * \param &type value
43 * \return value incremented by one
44 */
45ParserTypes &operator++(ParserTypes &type)
46{
47 return type = ParserTypes(type+1);
48}
49
50/** Constructor of class FormatParserStorage.
51 */
52FormatParserStorage::FormatParserStorage()
53{
54 ParserList.resize(ParserTypes_end, NULL);
55 ParserStream.resize(ParserTypes_end, NULL);
56 ParserPresent.resize(ParserTypes_end, false);
57
58 ParserNames[mpqc] = "mpqc";
59 ParserNames[pcp] = "pcp";
60 ParserNames[pdb] = "pdb";
61 ParserNames[tremolo] = "tremolo";
62 ParserNames[xyz] = "xyz";
63
64 for (std::map<ParserTypes, std::string>::const_iterator it = ParserNames.begin(); it != ParserNames.end(); ++it)
65 ParserLookupNames.insert(pair<std::string, ParserTypes>(it->second,it->first) );
66
67 ParserSuffixes[mpqc] = "in";
68 ParserSuffixes[pcp] = "conf";
69 ParserSuffixes[pdb] = "pdb";
70 ParserSuffixes[tremolo] = "data";
71 ParserSuffixes[xyz] = "xyz";
72
73 for (std::map<ParserTypes, std::string>::const_iterator it = ParserSuffixes.begin(); it != ParserSuffixes.end(); ++it)
74 ParserLookupSuffixes.insert(pair<std::string, ParserTypes>(it->second,it->first) );
75
76 ParserAddFunction[mpqc] = &FormatParserStorage::addMpqc;
77 ParserAddFunction[pcp] = &FormatParserStorage::addPcp;
78 ParserAddFunction[pdb] = &FormatParserStorage::addPdb;
79 ParserAddFunction[tremolo] = &FormatParserStorage::addTremolo;
80 ParserAddFunction[xyz] = &FormatParserStorage::addXyz;
81}
82
83/** Destructor of class FormatParserStorage.
84 * Free all stored FormatParsers.
85 * Save on Exit.
86 */
87FormatParserStorage::~FormatParserStorage()
88{
89 for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
90 if (ParserPresent[iter]) {
91 if (ParserStream[iter]->is_open())
92 ParserStream[iter]->close();
93 delete ParserStream[iter];
94 delete ParserList[iter];
95 }
96}
97
98/** Sets the filename of all current parsers in storage to prefix.suffix.
99 * \param &prefix prefix to use.
100 */
101void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix)
102{
103 prefix=_prefix;
104};
105
106
107void FormatParserStorage::SaveAll()
108{
109 std::string filename;
110 for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
111 if (ParserPresent[iter]) {
112 filename = prefix;
113 filename += ".";
114 filename += ParserSuffixes[iter];
115 ParserStream[iter] = new std::ofstream(filename.c_str());
116 ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
117 }
118}
119
120
121/** Adds an MpqcParser to the storage.
122 */
123void FormatParserStorage::addMpqc()
124{
125 if (!ParserPresent[mpqc]) {
126 ParserList[mpqc] = dynamic_cast<FormatParser *>(new MpqcParser);
127 ParserPresent[mpqc] = true;
128 }
129 else
130 DoeLog(1) && (eLog() << Verbose(1) << "Parser mpqc is already present." << endl);
131}
132
133
134/** Adds an PcpParser to the storage.
135 */
136void FormatParserStorage::addPcp()
137{
138 if (!ParserPresent[pcp]) {
139 ParserList[pcp] = new PcpParser();
140 ParserPresent[pcp] = true;
141 } else
142 DoeLog(1) && (eLog() << Verbose(1) << "Parser pcp is already present." << endl);
143}
144
145
146/** Adds an PdbParser to the storage.
147 */
148void FormatParserStorage::addPdb()
149{
150 if (!ParserPresent[pdb]) {
151 ParserList[pdb] = new PdbParser();
152 ParserPresent[pdb] = true;
153 } else
154 DoeLog(1) && (eLog() << Verbose(1) << "Parser pdb is already present." << endl);
155}
156
157
158/** Adds an TremoloParser to the storage.
159 */
160void FormatParserStorage::addTremolo()
161{
162 if (!ParserPresent[tremolo]) {
163 ParserList[tremolo] = new TremoloParser();
164 ParserPresent[tremolo] = true;
165 } else
166 DoeLog(1) && (eLog() << Verbose(1) << "Parser tremolo is already present." << endl);
167}
168
169
170/** Adds an XyzParser to the storage.
171 */
172void FormatParserStorage::addXyz()
173{
174 if (!ParserPresent[xyz]) {
175 ParserList[xyz] = new XyzParser();
176 ParserPresent[xyz] = true;
177 } else
178 DoeLog(1) && (eLog() << Verbose(1) << "Parser xyz is already present." << 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::get(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
246/** Stores world in an ostream depending on its suffix
247 * \param &output output stream
248 * \param suffix
249 * \return true - storing ok, false - suffix unknown
250 */
251bool FormatParserStorage::put(std::ostream &output, std::string suffix)
252{
253 if (suffix == ParserSuffixes[mpqc]) {
254 getMpqc().save(&output);
255 } else if (suffix == ParserSuffixes[pcp]) {
256 getPcp().save(&output);
257 } else if (suffix == ParserSuffixes[pdb]) {
258 getPdb().save(&output);
259 } else if (suffix == ParserSuffixes[tremolo]) {
260 getTremolo().save(&output);
261 } else if (suffix == ParserSuffixes[xyz]) {
262 getXyz().save(&output);
263 } else {
264 DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::put()." << endl);
265 return false;
266 }
267 return true;
268}
269
270/** Returns reference to the output MpqcParser, adds if not present.
271 * \return reference to the output MpqcParser
272 */
273MpqcParser &FormatParserStorage::getMpqc()
274{
275 if (!ParserPresent[mpqc])
276 addMpqc();
277 return dynamic_cast<MpqcParser &>(*ParserList[mpqc]);
278}
279
280/** Returns reference to the output PcpParser, adds if not present.
281 * \return reference to the output PcpParser
282 */
283PcpParser &FormatParserStorage::getPcp()
284{
285 if (!ParserPresent[pcp])
286 addPcp();
287 return dynamic_cast<PcpParser &>(*ParserList[pcp]);
288}
289
290/** Returns reference to the output PdbParser, adds if not present.
291 * \return reference to the output PdbParser
292 */
293PdbParser &FormatParserStorage::getPdb()
294{
295 if (!ParserPresent[pdb])
296 addPdb();
297 return dynamic_cast<PdbParser &>(*ParserList[pdb]);
298}
299
300/** Returns reference to the output TremoloParser, adds if not present.
301 * \return reference to the output TremoloParser
302 */
303TremoloParser &FormatParserStorage::getTremolo()
304{
305 if (!ParserPresent[tremolo])
306 addTremolo();
307 return dynamic_cast<TremoloParser &>(*ParserList[tremolo]);
308}
309
310/** Returns reference to the output XyzParser, adds if not present.
311 * \return reference to the output XyzParser
312 */
313XyzParser &FormatParserStorage::getXyz()
314{
315 if (!ParserPresent[xyz])
316 addXyz();
317 return dynamic_cast<XyzParser &>(*ParserList[xyz]);
318}
319
320
321
322CONSTRUCT_SINGLETON(FormatParserStorage)
Note: See TracBrowser for help on using the repository browser.