source: src/Parser/XmlParser.cpp@ 94d5ac6

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 94d5ac6 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: 12.2 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 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/*
24 * XmlParser.cpp
25 *
26 * Created on: Mar 23, 2012
27 * Author: heber
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 <limits>
38#include <vector>
39
40#include "CodePatterns/Log.hpp"
41#include "CodePatterns/Verbose.hpp"
42
43#include "XmlParser.hpp"
44
45#include "Atom/atom.hpp"
46#include "Box.hpp"
47#include "Element/element.hpp"
48#include "Element/periodentafel.hpp"
49#include "molecule.hpp"
50#include "MoleculeListClass.hpp"
51#include "World.hpp"
52
53#include "Parser/pugixml/pugixml.hpp"
54
55// static instances
56FormatParser< xml >::additionalAtomInfo FormatParser< xml >::defaultAtomInfo;
57
58// declare specialized static variables
59const std::string FormatParserTrait<xml>::name = "xml";
60const std::string FormatParserTrait<xml>::suffix = "xml";
61const ParserTypes FormatParserTrait<xml>::type = xml;
62
63const char *box_axis[NDIM] = { "box_a", "box_b", "box_c" };
64
65/**
66 * Constructor.
67 */
68FormatParser< xml >::FormatParser() :
69 FormatParser_common(NULL)
70{}
71
72/**
73 * Destructor.
74 */
75FormatParser< xml >::~FormatParser()
76{}
77
78Vector toVector(const std::string &value)
79{
80 std::stringstream inputstream(value);
81 Vector returnVector;
82 for (size_t i=0;i<NDIM;++i)
83 inputstream >> std::setprecision(16) >> returnVector[i];
84 return returnVector;
85}
86
87double toDouble(const std::string &value)
88{
89 std::stringstream inputstream(value);
90 double returnDouble;
91 inputstream >> std::setprecision(16) >> returnDouble;
92 return returnDouble;
93}
94
95std::string fromVector(const Vector&a)
96{
97 std::stringstream returnstring;
98 for (size_t i=0;i<NDIM;++i) {
99 returnstring << std::setprecision(16) << a[i];
100 if (i != NDIM-1)
101 returnstring << std::string(" ");
102 }
103 return returnstring.str();
104}
105
106std::string fromDouble(const double &a)
107{
108 std::stringstream returnstring;
109 returnstring << std::setprecision(16) << a;
110 return returnstring.str();
111}
112
113std::string fromBoolean(const bool c[])
114{
115 std::stringstream returnstring;
116 for (size_t i=0;i<NDIM;++i) {
117 returnstring << (c[i] ? std::string("1") : std::string("0"));
118 if (i != NDIM-1)
119 returnstring << std::string(" ");
120 }
121 return returnstring.str();
122}
123
124/**
125 * Loads an XYZ file into the World.
126 *
127 * \param XYZ file
128 */
129void FormatParser< xml >::load(std::istream* file)
130{
131 // create the molecule
132 molecule * const newmol = World::getInstance().createMolecule();
133 newmol->ActiveFlag = true;
134 // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include
135 World::getInstance().getMolecules()->insert(newmol);
136
137 // load file into xml tree
138 pugi::xml_document doc;
139 doc.load(*file);
140
141 // header
142 pugi::xml_node scafacos_test = doc.root().child("scafacos_test");
143 data.name = toString(scafacos_test.attribute("name").value());
144 data.description = toString(scafacos_test.attribute("description").value());
145 data.reference_method = toString(scafacos_test.attribute("reference_method").value());
146 data.error_potential = scafacos_test.attribute("error_potential").as_double();
147 data.error_field = scafacos_test.attribute("error_field").as_double();
148 LOG(1, "INFO: scafacos_test.name is '" << data.name << "'.");
149 newmol->setName(data.name);
150
151 // configuration information
152 pugi::xml_node configuration = scafacos_test.child("configuration");
153 data.config.offset = toVector(configuration.attribute("offset").value());
154 for (size_t i=0; i<NDIM; ++i)
155 data.config.box.column(i) = toVector(configuration.attribute(box_axis[i]).value());
156 World::getInstance().getDomain().setM(data.config.box);
157 {
158 std::stringstream inputstream(configuration.attribute("periodicity").value());
159 for (size_t i=0; i<NDIM; ++i) {
160 ASSERT( inputstream.good(),
161 "FormatParser< xml >::load() - periodicity attribute has less than 3 entries.");
162 inputstream >> data.config.periodicity[i];
163 }
164 }
165 data.config.epsilon = toString(configuration.attribute("epsilon").value());
166
167 // particles
168 for(pugi::xml_node::iterator iter = configuration.begin();
169 iter != configuration.end(); ++iter) {
170 struct scafacos::configuration::particle p;
171 p.position = toVector((*iter).attribute("position").value());
172 p.q = toDouble((*iter).attribute("q").value());
173 p.potential = toDouble((*iter).attribute("potential").value());
174 p.field = toVector((*iter).attribute("field").value());
175 data.config.p.push_back(p);
176 LOG(2, "DEBUG: Parsing particle at " << p.position << ".");
177 atom * const newAtom = World::getInstance().createAtom();
178 // for the moment each becomes a hydrogen
179 newAtom->setType(World::getInstance().getPeriode()->FindElement((atomicNumber_t)1));
180 newAtom->setPosition(p.position);
181 newAtom->setCharge(p.q);
182 newmol->AddAtom(newAtom);
183 additionalAtomInfo atomInfo(p.q, p.potential, p.field);
184#ifndef NDEBUG
185 std::pair<AtomInfoMap_t::iterator, bool> inserter =
186#endif
187 additionalAtomData.insert( std::make_pair(newAtom->getId(), atomInfo) );
188 ASSERT( inserter.second,
189 "FormatParser< xml >::load() - atomInfo entry for atom "+toString(newAtom->getId())
190 +" already present.");
191 }
192
193 BOOST_FOREACH(atom *_atom, World::getInstance().getAllAtoms())
194 LOG(3, "INFO: Atom " << _atom->getName() << " " << *dynamic_cast<AtomInfo *>(_atom) << ".");
195
196 // refresh atom::nr and atom::name
197 newmol->getAtomCount();
198}
199
200/**
201 * Saves the \a atoms into as a XYZ file.
202 *
203 * \param file where to save the state
204 * \param atoms atoms to store
205 */
206void FormatParser< xml >::save(std::ostream* file, const std::vector<atom *> &atoms) {
207 LOG(0, "Saving changes to xml.");
208
209 // fill the structure with updated information
210 const Box &domain = World::getInstance().getDomain();
211 data.config.box = domain.getM();
212 for (size_t i=0;i<NDIM;++i)
213 data.config.periodicity[i] = domain.getCondition(i) == BoundaryConditions::Wrap;
214 data.config.p.clear();
215 for(std::vector<atom*>::const_iterator it = atoms.begin(); it != atoms.end(); it++) {
216 struct scafacos::configuration::particle p;
217 const additionalAtomInfo &atomInfo = getAtomData(*(*it));
218 p.position = (*it)->getPosition();
219 p.q = (*it)->getCharge();
220 p.potential = atomInfo.potential;
221 p.field = atomInfo.field;
222 data.config.p.push_back(p);
223 }
224
225 // create the xml tree
226 pugi::xml_document doc;
227 pugi::xml_attribute attr;
228
229 // header
230 pugi::xml_node xml_scafacos_test = doc.root().append_child();
231 xml_scafacos_test.set_name("scafacos_test");
232 xml_scafacos_test.append_attribute("name").set_value(data.name.c_str());
233 xml_scafacos_test.append_attribute("description").set_value(data.description.c_str());
234 xml_scafacos_test.append_attribute("reference_method").set_value(data.reference_method.c_str());
235 xml_scafacos_test.append_attribute("error_potential").set_value(data.error_potential);
236 xml_scafacos_test.append_attribute("error_field").set_value(data.error_field);
237
238 // configuration
239 pugi::xml_node xml_configuration = xml_scafacos_test.append_child();
240 xml_configuration.set_name("configuration");
241 xml_configuration.append_attribute("offset").set_value(fromVector(data.config.offset).c_str());
242 for (size_t i=0; i<NDIM; ++i)
243 xml_configuration.append_attribute(box_axis[i]).set_value(fromVector(data.config.box.column(i)).c_str());
244 xml_configuration.append_attribute("periodicity").set_value(fromBoolean(data.config.periodicity).c_str());
245 xml_configuration.append_attribute("epsilon").set_value(toString(data.config.epsilon).c_str());
246
247 // particles
248 for (std::vector<scafacos::configuration::particle>::const_iterator iter = data.config.p.begin();
249 iter != data.config.p.end();++iter) {
250 pugi::xml_node particle = xml_configuration.append_child();
251 particle.set_name("particle");
252 particle.append_attribute("position").set_value(fromVector((*iter).position).c_str());
253 particle.append_attribute("q").set_value(fromDouble((*iter).q).c_str());
254 particle.append_attribute("potential").set_value(fromDouble((*iter).potential).c_str());
255 particle.append_attribute("field").set_value(fromVector((*iter).field).c_str());
256 }
257
258 // print standard header and save without declaration
259 *file << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
260 *file << "<!DOCTYPE scafacos_test SYSTEM 'scafacos_test.dtd'>\n";
261 doc.save(*file, "\t", pugi::format_no_declaration, pugi::encoding_utf8);
262}
263
264/** Observer callback when new atom is added to World.
265 *
266 * @param id of atom
267 */
268void FormatParser< xml >::AtomInserted(atomId_t id)
269{
270 std::map<const atomId_t, additionalAtomInfo>::iterator iter = additionalAtomData.find(id);
271 ASSERT(iter == additionalAtomData.end(),
272 "FormatParser< xml >::AtomInserted() - additionalAtomData already present for newly added atom "
273 +toString(id)+".");
274 // additionalAtomData.insert( std::make_pair(id, additionalAtomInfo()) );
275}
276
277/** Remove additional AtomData info, when atom has been removed from World.
278 *
279 * @param id of atom
280 */
281void FormatParser< xml >::AtomRemoved(atomId_t id)
282{
283 std::map<const atomId_t, additionalAtomInfo>::iterator iter = additionalAtomData.find(id);
284 // as we do not insert AtomData on AtomInserted, we cannot be assured of its presence
285// ASSERT(iter != additionalAtomData.end(),
286// "FormatParser< tremolo >::AtomRemoved() - additionalAtomData is not present for atom "
287// +toString(id)+" to remove.");
288 if (iter != additionalAtomData.end())
289 additionalAtomData.erase(iter);
290}
291
292const FormatParser< xml >::additionalAtomInfo& FormatParser< xml >::getAtomData(const atom &_atom) const
293{
294 {
295 // has its own entry?
296 AtomInfoMap_t::const_iterator iter = additionalAtomData.find(_atom.getId());
297 if (iter != additionalAtomData.end()) {
298 return iter->second;
299 }
300 }
301 {
302 // father has an entry?
303 AtomInfoMap_t::const_iterator iter = additionalAtomData.find(_atom.GetTrueFather()->getId());
304 if (iter != additionalAtomData.end()) {
305 return iter->second;
306 }
307 }
308 return defaultAtomInfo;
309}
310
311
312#define comparator(x,y) if (x != y) { LOG(2, "DEBUG: Mismatch in " << #x << ": " << x << " != " << y); return false; }
313#define num_comparator(x,y) if (fabs(x - y) > MYEPSILON) { LOG(2, "DEBUG: Numeric mismatch in " << #x << ": " << x << " != " << y << " by " << fabs(x - y) << "."); return false; }
314
315bool FormatParser< xml >::scafacos::configuration::particle::operator==(const particle &p) const {
316 comparator(position, p.position)
317 num_comparator(q, p.q)
318 num_comparator(potential, p.potential)
319 comparator(field, p.field)
320 return true;
321}
322
323bool FormatParser< xml >::scafacos::configuration::operator==(const configuration &c) const {
324 comparator(offset, c.offset)
325 comparator(box, c.box)
326 for (size_t i=0;i<NDIM;++i)
327 comparator(periodicity[i], c.periodicity[i])
328 comparator(epsilon, c.epsilon)
329
330 if (p.size() != c.p.size()) {
331 LOG(2, "DEBUG: Mismatch in p's size: " << p.size() << " != " << c.p.size() << ".");
332 return false;
333 }
334 std::vector<scafacos::configuration::particle>::const_iterator iter = p.begin();
335 std::vector<scafacos::configuration::particle>::const_iterator citer = c.p.begin();
336 for (;iter != p.end(); ++iter, ++citer) {
337 if ((*iter) != (*citer))
338 return false;
339 }
340 return true;
341}
342
343
344bool FormatParser< xml >::scafacos::operator==(const scafacos &s) const {
345 comparator(name, s.name)
346 comparator(description, s.description)
347 comparator(reference_method, s.reference_method)
348 num_comparator(error_potential, s.error_potential)
349 num_comparator(error_field, s.error_field)
350
351 if (config != s.config) {
352 return false;
353 }
354 return true;
355}
Note: See TracBrowser for help on using the repository browser.