source: src/Parser/FormatParserStorage.cpp@ aec098

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

Added an additionalAtomInfo container to XmlParser.

  • this stores the parser-specific information such as charge, ...
  • Property mode set to 100644
File size: 10.9 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 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 <boost/preprocessor/iteration/local.hpp>
26
27#include "CodePatterns/Assert.hpp"
28#include "CodePatterns/Log.hpp"
29
30#include "molecule.hpp"
31#include "FormatParserStorage.hpp"
32#include "ParserTypes.hpp"
33
34#include "MpqcParser.hpp"
35#include "PcpParser.hpp"
36#include "PdbParser.hpp"
37#include "Psi3Parser.hpp"
38#include "TremoloParser.hpp"
39#include "XmlParser.hpp"
40#include "XyzParser.hpp"
41
42#include "CodePatterns/Singleton_impl.hpp"
43
44const std::string FormatParserStorage::unknownTypeString("unknown");
45
46/** Constructor of class FormatParserStorage.
47 */
48FormatParserStorage::FormatParserStorage()
49{
50 ParserList.resize(ParserTypes_end, NULL);
51 ParserStream.resize(ParserTypes_end, NULL);
52 ParserPresent.resize(ParserTypes_end, false);
53 ParserDesiredOutputFormat.resize(ParserTypes_end, false);
54
55#include "ParserTypes.def"
56
57#define insert_print(z,n,seq,map, before, after) \
58 map .insert( std::make_pair( \
59 BOOST_PP_SEQ_ELEM(n, seq) \
60 , before < \
61 BOOST_PP_SEQ_ELEM(n, seq) \
62 > after \
63 ) );
64
65#define insert_invert_print(z,n,seq,map, before, after) \
66 map .insert( std::make_pair( before < \
67 BOOST_PP_SEQ_ELEM(n, seq) \
68 > after, \
69 BOOST_PP_SEQ_ELEM(n, seq) \
70 ) );
71
72 // fill ParserNames
73#if defined ParserTypes_END // do we have parameters at all?
74#define BOOST_PP_LOCAL_MACRO(n) insert_print(~, n, PARSERSEQUENCE, ParserNames, FormatParserTrait, ::name)
75#define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
76#include BOOST_PP_LOCAL_ITERATE()
77#endif
78
79 // fill ParserLookupNames
80#if defined ParserTypes_END // do we have parameters at all?
81#define BOOST_PP_LOCAL_MACRO(n) insert_invert_print(~, n, PARSERSEQUENCE, ParserLookupNames, FormatParserTrait, ::name)
82#define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
83#include BOOST_PP_LOCAL_ITERATE()
84#endif
85
86 // fill ParserSuffixes
87#if defined ParserTypes_END // do we have parameters at all?
88#define BOOST_PP_LOCAL_MACRO(n) insert_print(~, n, PARSERSEQUENCE, ParserSuffixes, FormatParserTrait, ::suffix)
89#define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
90#include BOOST_PP_LOCAL_ITERATE()
91#endif
92
93 // fill ParserLookupSuffixes
94#if defined ParserTypes_END // do we have parameters at all?
95#define BOOST_PP_LOCAL_MACRO(n) insert_invert_print(~, n, PARSERSEQUENCE, ParserLookupSuffixes, FormatParserTrait, ::suffix)
96#define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
97#include BOOST_PP_LOCAL_ITERATE()
98#endif
99
100 // fill ParserAddFunction
101#if defined ParserTypes_END // do we have parameters at all?
102#define BOOST_PP_LOCAL_MACRO(n) insert_print(~, n, PARSERSEQUENCE, ParserAddFunction, &FormatParserStorage::addParser, )
103#define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
104#include BOOST_PP_LOCAL_ITERATE()
105#endif
106
107#undef insert_print
108#undef insert_invert_print
109#include "ParserTypes.undef"
110
111 //std::cout << "ParserNames:" << std::endl << ParserNames << std::endl;
112 //std::cout << "ParserSuffixes:" << std::endl << ParserSuffixes << std::endl;
113 //std::cout << "ParserLookupNames:" << std::endl << ParserLookupNames << std::endl;
114 //std::cout << "ParserLookupSuffixes:" << std::endl << ParserLookupSuffixes << std::endl;
115 //std::cout << "ParserAddFunction:" << std::endl << ParserAddFunction << std::endl;
116
117}
118
119/** Destructor of class FormatParserStorage.
120 * Free all stored FormatParsers.
121 * Save on Exit.
122 */
123FormatParserStorage::~FormatParserStorage()
124{
125 for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
126 if (ParserPresent[iter]) {
127 if (ParserStream[iter] != NULL) {
128 if (ParserStream[iter]->is_open())
129 ParserStream[iter]->close();
130 delete ParserStream[iter];
131 }
132 delete ParserList[iter];
133 }
134}
135
136/** Sets the filename of all current parsers in storage to prefix.suffix.
137 * \param &prefix prefix to use.
138 */
139void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix)
140{
141 prefix=_prefix;
142};
143
144/** Sets \a type as a format to be stored on call of SaveAll.
145 *
146 * @param type type to add to desired output formats
147 */
148void FormatParserStorage::setOutputFormat(ParserTypes type)
149{
150 LOG(0, "STATUS: Adding " << ParserNames[type] << " type to output.");
151 ParserDesiredOutputFormat[type] = true;
152}
153
154/** Sets \a type as a format to be stored on call of SaveAll.
155 *
156 * @param type type to add to desired output formats
157 */
158void FormatParserStorage::setOutputFormat(std::string type)
159{
160 std::map<std::string, ParserTypes>::const_iterator iter = ParserLookupNames.find(type);
161 ASSERT(iter != ParserLookupNames.end(),
162 "FormatParserStorage::setOutputFormat() - output format "+type+" is unknown.");
163 setOutputFormat(iter->second);
164}
165
166/** Saves the world in the desired output formats.
167 *
168 */
169void FormatParserStorage::SaveAll()
170{
171 std::string filename;
172 for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
173 if (ParserPresent[iter] && ParserDesiredOutputFormat[iter]) {
174 filename = prefix;
175 filename += ".";
176 filename += ParserSuffixes[iter];
177 ParserStream[iter] = new std::ofstream(filename.c_str());
178 ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
179 }
180}
181
182
183ParserTypes FormatParserStorage::getTypeFromName(std::string type)
184{
185 if (ParserLookupNames.find(type) == ParserLookupNames.end()) {
186 ELOG(1, "Unknown type " << type << ".");
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 ELOG(1, "Unknown type " << type << ".");
196 return ParserTypes_end;
197 } else
198 return ParserLookupSuffixes[type];
199}
200
201const std::string &FormatParserStorage::getNameFromType(ParserTypes type)
202{
203 if (ParserNames.find(type) == ParserNames.end()) {
204 ELOG(1, "Unknown type " << type << ".");
205 return unknownTypeString;
206 } else
207 return ParserNames[type];
208}
209
210const std::string &FormatParserStorage::getSuffixFromType(ParserTypes type)
211{
212 if (ParserSuffixes.find(type) == ParserSuffixes.end()) {
213 ELOG(1, "Unknown type " << type << ".");
214 return unknownTypeString;
215 } else
216 return ParserSuffixes[type];
217}
218
219bool FormatParserStorage::add(ParserTypes ptype)
220{
221 if (ptype != ParserTypes_end) {
222 if (ParserAddFunction.find(ptype) != ParserAddFunction.end()) {
223 (getInstance().*(ParserAddFunction[ptype]))(); // we still need an object to work on ...
224 return true;
225 } else {
226 ELOG(1, "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?");
227 return false;
228 }
229 } else {
230 return false;
231 }
232}
233
234bool FormatParserStorage::add(std::string type)
235{
236 enum ParserTypes Ptype = getTypeFromName(type);
237 return add(Ptype);
238}
239
240/** Recognizes type of file and parse via FormatParserStorage::load().
241 * \param filename path and filename
242 * \return true - parsing ok, false - suffix unknown
243 */
244bool FormatParserStorage::load(boost::filesystem::path filename)
245{
246 return load(filename.string());
247}
248
249/** Recognizes type of file and parse via FormatParserStorage::load().
250 * \param filename path and filename
251 * \return true - parsing ok, false - suffix unknown
252 */
253bool FormatParserStorage::load(std::string &filename)
254{
255 std::string FilenameSuffix = filename.substr(filename.find_last_of('.')+1, filename.length());
256 ifstream input;
257 LOG(0, "STATUS: Loading filler molecule " << filename
258 << " of suffix " << FilenameSuffix << ".");
259 input.open(filename.c_str());
260 const bool status = load(input, FilenameSuffix);
261 input.close();
262
263 return status;
264}
265
266/** Parses an istream depending on its suffix
267 * \param &input input stream
268 * \param suffix
269 * \return true - parsing ok, false - suffix unknown
270 */
271bool FormatParserStorage::load(std::istream &input, std::string &suffix)
272{
273 enum ParserTypes type = getTypeFromSuffix(suffix);
274 if (type != ParserTypes_end)
275 get(type).load(&input);
276 else
277 return false;
278 return true;
279}
280
281/** Stores all selected atoms in an ostream depending on its suffix
282 * \param &output output stream
283 * \param suffix
284 * \return true - storing ok, false - suffix unknown
285 */
286bool FormatParserStorage::saveSelectedAtoms(std::ostream &output, std::string suffix)
287{
288 std::vector<atom *> atoms = World::getInstance().getSelectedAtoms();
289 return save(output, suffix, atoms);
290}
291
292/** Stores all selected atoms in an ostream depending on its suffix
293 * We store in the order of the atomic ids, not in the order they appear in the molecules.
294 * Hence, we first create a vector from all selected molecules' atoms.
295 *
296 * TODO: Change here atom * to const atom * when FormatParserStorage::save() uses vector<const atom *>
297 *
298 * \param &output output stream
299 * \param suffix
300 * \return true - storing ok, false - suffix unknown
301 */
302bool FormatParserStorage::saveSelectedMolecules(std::ostream &output, std::string suffix)
303{
304 std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
305 std::map<size_t, atom *> IdAtoms;
306 for (std::vector<molecule *>::const_iterator MolIter = molecules.begin();
307 MolIter != molecules.end();
308 ++MolIter) {
309 for(molecule::iterator AtomIter = (*MolIter)->begin();
310 AtomIter != (*MolIter)->end();
311 ++AtomIter) {
312 IdAtoms.insert( make_pair((*AtomIter)->getId(), (*AtomIter)) );
313 }
314 }
315 std::vector<atom *> atoms;
316 atoms.reserve(IdAtoms.size());
317 for (std::map<size_t, atom *>::const_iterator iter = IdAtoms.begin();
318 iter != IdAtoms.end();
319 ++iter) {
320 atoms.push_back(iter->second);
321 }
322 return save(output, suffix, atoms);
323}
324
325/** Stores world in an ostream depending on its suffix
326 * \param &output output stream
327 * \param suffix
328 * \return true - storing ok, false - suffix unknown
329 */
330bool FormatParserStorage::saveWorld(std::ostream &output, std::string suffix)
331{
332 std::vector<atom *> atoms = World::getInstance().getAllAtoms();
333 return save(output, suffix, atoms);
334}
335
336/** Stores a given vector of \a atoms in an ostream depending on its suffix
337 * \param &output output stream
338 * \param suffix
339 * \return true - storing ok, false - suffix unknown
340 */
341bool FormatParserStorage::save(std::ostream &output, std::string suffix, const std::vector<atom *> &atoms)
342{
343 enum ParserTypes type = getTypeFromSuffix(suffix);
344 if (type != ParserTypes_end)
345 get(type).save(&output, atoms);
346 else
347 return false;
348 return true;
349}
350
351/** Returns reference to the desired output parser as FormatParser, adds if not present.
352 * \param _type type of desired parser
353 * \return reference to the output FormatParser with desired type
354 */
355FormatParserInterface &FormatParserStorage::get(ParserTypes _type)
356{
357 if (!ParserPresent[_type]) {
358 add(_type);
359 }
360 return *ParserList[_type];
361}
362
363CONSTRUCT_SINGLETON(FormatParserStorage)
Note: See TracBrowser for help on using the repository browser.