source: src/Parser/XmlParser.cpp@ fac58f

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

Converted FormatParser::save() to using vector of const atom ptrs.

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