source: src/Parser/FormatParserStorage.cpp@ 8b58ac

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 8b58ac was 94d5ac6, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: As we use GSL internally, we are as of now required to use GPL v2 license.

  • GNU Scientific Library is used at every place in the code, especially the sub-package LinearAlgebra is based on it which in turn is used really everywhere in the remainder of MoleCuilder. Hence, we have to use the GPL license for the whole of MoleCuilder. In effect, GPL's COPYING was present all along and stated the terms of the GPL v2 license.
  • Hence, I added the default GPL v2 disclaimer to every source file and removed the note about a (actually missing) LICENSE file.
  • also, I added a help-redistribute action which again gives the disclaimer of the GPL v2.
  • also, I changed in the disclaimer that is printed at every program start in builder_init.cpp.
  • TEST: Added check on GPL statement present in every module to test CodeChecks project-disclaimer.
  • Property mode set to 100644
File size: 11.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 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/** \file FormatParserStorage.cpp
24 *
25 * date: Jun, 22 2010
26 * author: heber
27 *
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include "CodePatterns/MemDebug.hpp"
36
37#include <iostream>
38#include <fstream>
39
40#include <boost/preprocessor/iteration/local.hpp>
41
42#include "CodePatterns/Assert.hpp"
43#include "CodePatterns/Log.hpp"
44
45#include "molecule.hpp"
46#include "FormatParserStorage.hpp"
47#include "ParserTypes.hpp"
48
49#include "MpqcParser.hpp"
50#include "PcpParser.hpp"
51#include "PdbParser.hpp"
52#include "Psi3Parser.hpp"
53#include "TremoloParser.hpp"
54#include "XmlParser.hpp"
55#include "XyzParser.hpp"
56
57#include "CodePatterns/Singleton_impl.hpp"
58
59const std::string FormatParserStorage::unknownTypeString("unknown");
60
61/** Constructor of class FormatParserStorage.
62 */
63FormatParserStorage::FormatParserStorage()
64{
65 ParserList.resize(ParserTypes_end, NULL);
66 ParserStream.resize(ParserTypes_end, NULL);
67 ParserPresent.resize(ParserTypes_end, false);
68 ParserDesiredOutputFormat.resize(ParserTypes_end, false);
69
70#include "ParserTypes.def"
71
72#define insert_print(z,n,seq,map, before, after) \
73 map .insert( std::make_pair( \
74 BOOST_PP_SEQ_ELEM(n, seq) \
75 , before < \
76 BOOST_PP_SEQ_ELEM(n, seq) \
77 > after \
78 ) );
79
80#define insert_invert_print(z,n,seq,map, before, after) \
81 map .insert( std::make_pair( before < \
82 BOOST_PP_SEQ_ELEM(n, seq) \
83 > after, \
84 BOOST_PP_SEQ_ELEM(n, seq) \
85 ) );
86
87 // fill ParserNames
88#if defined ParserTypes_END // do we have parameters at all?
89#define BOOST_PP_LOCAL_MACRO(n) insert_print(~, n, PARSERSEQUENCE, ParserNames, FormatParserTrait, ::name)
90#define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
91#include BOOST_PP_LOCAL_ITERATE()
92#endif
93
94 // fill ParserLookupNames
95#if defined ParserTypes_END // do we have parameters at all?
96#define BOOST_PP_LOCAL_MACRO(n) insert_invert_print(~, n, PARSERSEQUENCE, ParserLookupNames, FormatParserTrait, ::name)
97#define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
98#include BOOST_PP_LOCAL_ITERATE()
99#endif
100
101 // fill ParserSuffixes
102#if defined ParserTypes_END // do we have parameters at all?
103#define BOOST_PP_LOCAL_MACRO(n) insert_print(~, n, PARSERSEQUENCE, ParserSuffixes, FormatParserTrait, ::suffix)
104#define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
105#include BOOST_PP_LOCAL_ITERATE()
106#endif
107
108 // fill ParserLookupSuffixes
109#if defined ParserTypes_END // do we have parameters at all?
110#define BOOST_PP_LOCAL_MACRO(n) insert_invert_print(~, n, PARSERSEQUENCE, ParserLookupSuffixes, FormatParserTrait, ::suffix)
111#define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
112#include BOOST_PP_LOCAL_ITERATE()
113#endif
114
115 // fill ParserAddFunction
116#if defined ParserTypes_END // do we have parameters at all?
117#define BOOST_PP_LOCAL_MACRO(n) insert_print(~, n, PARSERSEQUENCE, ParserAddFunction, &FormatParserStorage::addParser, )
118#define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
119#include BOOST_PP_LOCAL_ITERATE()
120#endif
121
122#undef insert_print
123#undef insert_invert_print
124#include "ParserTypes.undef"
125
126 //std::cout << "ParserNames:" << std::endl << ParserNames << std::endl;
127 //std::cout << "ParserSuffixes:" << std::endl << ParserSuffixes << std::endl;
128 //std::cout << "ParserLookupNames:" << std::endl << ParserLookupNames << std::endl;
129 //std::cout << "ParserLookupSuffixes:" << std::endl << ParserLookupSuffixes << std::endl;
130 //std::cout << "ParserAddFunction:" << std::endl << ParserAddFunction << std::endl;
131
132}
133
134/** Destructor of class FormatParserStorage.
135 * Free all stored FormatParsers.
136 * Save on Exit.
137 */
138FormatParserStorage::~FormatParserStorage()
139{
140 for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
141 if (ParserPresent[iter]) {
142 if (ParserStream[iter] != NULL) {
143 if (ParserStream[iter]->is_open())
144 ParserStream[iter]->close();
145 delete ParserStream[iter];
146 }
147 delete ParserList[iter];
148 }
149}
150
151
152/** Tests whether a file and parsers are selected for saving.
153 */
154bool FormatParserStorage::isAbleToSave()
155{
156 if (prefix.empty())
157 return false;
158
159 for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
160 if (ParserPresent[iter] && ParserDesiredOutputFormat[iter])
161 return true;
162 return false;
163}
164
165/** Sets the filename of all current parsers in storage to prefix.suffix.
166 * \param &prefix prefix to use.
167 */
168void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix)
169{
170 prefix=_prefix;
171};
172
173/** Sets \a type as a format to be stored on call of SaveAll.
174 *
175 * @param type type to add to desired output formats
176 */
177void FormatParserStorage::setOutputFormat(ParserTypes type)
178{
179 LOG(0, "STATUS: Adding " << ParserNames[type] << " type to output.");
180 ParserDesiredOutputFormat[type] = true;
181}
182
183/** Sets \a type as a format to be stored on call of SaveAll.
184 *
185 * @param type type to add to desired output formats
186 */
187void FormatParserStorage::setOutputFormat(std::string type)
188{
189 std::map<std::string, ParserTypes>::const_iterator iter = ParserLookupNames.find(type);
190 ASSERT(iter != ParserLookupNames.end(),
191 "FormatParserStorage::setOutputFormat() - output format "+type+" is unknown.");
192 setOutputFormat(iter->second);
193}
194
195/** Saves the world in the desired output formats.
196 *
197 */
198void FormatParserStorage::SaveAll()
199{
200 std::string filename;
201 for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
202 if (ParserPresent[iter] && ParserDesiredOutputFormat[iter]) {
203 filename = prefix;
204 filename += ".";
205 filename += ParserSuffixes[iter];
206 ParserStream[iter] = new std::ofstream(filename.c_str());
207 ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
208 }
209}
210
211
212ParserTypes FormatParserStorage::getTypeFromName(std::string type)
213{
214 if (ParserLookupNames.find(type) == ParserLookupNames.end()) {
215 ELOG(1, "Unknown type " << type << ".");
216 return ParserTypes_end;
217 } else
218 return ParserLookupNames[type];
219}
220
221ParserTypes FormatParserStorage::getTypeFromSuffix(std::string type)
222{
223 if (ParserLookupSuffixes.find(type) == ParserLookupSuffixes.end()) {
224 ELOG(1, "Unknown type " << type << ".");
225 return ParserTypes_end;
226 } else
227 return ParserLookupSuffixes[type];
228}
229
230const std::string &FormatParserStorage::getNameFromType(ParserTypes type)
231{
232 if (ParserNames.find(type) == ParserNames.end()) {
233 ELOG(1, "Unknown type " << type << ".");
234 return unknownTypeString;
235 } else
236 return ParserNames[type];
237}
238
239const std::string &FormatParserStorage::getSuffixFromType(ParserTypes type)
240{
241 if (ParserSuffixes.find(type) == ParserSuffixes.end()) {
242 ELOG(1, "Unknown type " << type << ".");
243 return unknownTypeString;
244 } else
245 return ParserSuffixes[type];
246}
247
248bool FormatParserStorage::add(ParserTypes ptype)
249{
250 if (ptype != ParserTypes_end) {
251 if (ParserAddFunction.find(ptype) != ParserAddFunction.end()) {
252 (getInstance().*(ParserAddFunction[ptype]))(); // we still need an object to work on ...
253 return true;
254 } else {
255 ELOG(1, "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?");
256 return false;
257 }
258 } else {
259 return false;
260 }
261}
262
263bool FormatParserStorage::add(std::string type)
264{
265 enum ParserTypes Ptype = getTypeFromName(type);
266 return add(Ptype);
267}
268
269/** Recognizes type of file and parse via FormatParserStorage::load().
270 * \param filename path and filename
271 * \return true - parsing ok, false - suffix unknown
272 */
273bool FormatParserStorage::load(boost::filesystem::path filename)
274{
275 return load(filename.string());
276}
277
278/** Recognizes type of file and parse via FormatParserStorage::load().
279 * \param filename path and filename
280 * \return true - parsing ok, false - suffix unknown
281 */
282bool FormatParserStorage::load(std::string &filename)
283{
284 std::string FilenameSuffix = filename.substr(filename.find_last_of('.')+1, filename.length());
285 ifstream input;
286 LOG(0, "STATUS: Loading filler molecule " << filename
287 << " of suffix " << FilenameSuffix << ".");
288 input.open(filename.c_str());
289 const bool status = load(input, FilenameSuffix);
290 input.close();
291
292 return status;
293}
294
295/** Parses an istream depending on its suffix
296 * \param &input input stream
297 * \param suffix
298 * \return true - parsing ok, false - suffix unknown
299 */
300bool FormatParserStorage::load(std::istream &input, std::string &suffix)
301{
302 enum ParserTypes type = getTypeFromSuffix(suffix);
303 if (type != ParserTypes_end)
304 get(type).load(&input);
305 else
306 return false;
307 return true;
308}
309
310/** Stores all selected atoms in an ostream depending on its suffix
311 * \param &output output stream
312 * \param suffix
313 * \return true - storing ok, false - suffix unknown
314 */
315bool FormatParserStorage::saveSelectedAtoms(std::ostream &output, std::string suffix)
316{
317 std::vector<atom *> atoms = World::getInstance().getSelectedAtoms();
318 return save(output, suffix, atoms);
319}
320
321/** Stores all selected atoms in an ostream depending on its suffix
322 * We store in the order of the atomic ids, not in the order they appear in the molecules.
323 * Hence, we first create a vector from all selected molecules' atoms.
324 *
325 * TODO: Change here atom * to const atom * when FormatParserStorage::save() uses vector<const atom *>
326 *
327 * \param &output output stream
328 * \param suffix
329 * \return true - storing ok, false - suffix unknown
330 */
331bool FormatParserStorage::saveSelectedMolecules(std::ostream &output, std::string suffix)
332{
333 std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
334 std::map<size_t, atom *> IdAtoms;
335 for (std::vector<molecule *>::const_iterator MolIter = molecules.begin();
336 MolIter != molecules.end();
337 ++MolIter) {
338 for(molecule::iterator AtomIter = (*MolIter)->begin();
339 AtomIter != (*MolIter)->end();
340 ++AtomIter) {
341 IdAtoms.insert( make_pair((*AtomIter)->getId(), (*AtomIter)) );
342 }
343 }
344 std::vector<atom *> atoms;
345 atoms.reserve(IdAtoms.size());
346 for (std::map<size_t, atom *>::const_iterator iter = IdAtoms.begin();
347 iter != IdAtoms.end();
348 ++iter) {
349 atoms.push_back(iter->second);
350 }
351 return save(output, suffix, atoms);
352}
353
354/** Stores world in an ostream depending on its suffix
355 * \param &output output stream
356 * \param suffix
357 * \return true - storing ok, false - suffix unknown
358 */
359bool FormatParserStorage::saveWorld(std::ostream &output, std::string suffix)
360{
361 std::vector<atom *> atoms = World::getInstance().getAllAtoms();
362 return save(output, suffix, atoms);
363}
364
365/** Stores a given vector of \a atoms in an ostream depending on its suffix
366 * \param &output output stream
367 * \param suffix
368 * \return true - storing ok, false - suffix unknown
369 */
370bool FormatParserStorage::save(std::ostream &output, std::string suffix, const std::vector<atom *> &atoms)
371{
372 enum ParserTypes type = getTypeFromSuffix(suffix);
373 if (type != ParserTypes_end)
374 get(type).save(&output, atoms);
375 else
376 return false;
377 return true;
378}
379
380/** Returns reference to the desired output parser as FormatParser, adds if not present.
381 * \param _type type of desired parser
382 * \return reference to the output FormatParser with desired type
383 */
384FormatParserInterface &FormatParserStorage::get(ParserTypes _type)
385{
386 if (!ParserPresent[_type]) {
387 add(_type);
388 }
389 return *ParserList[_type];
390}
391
392CONSTRUCT_SINGLETON(FormatParserStorage)
Note: See TracBrowser for help on using the repository browser.