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
|
---|
57 | FormatParser< xml >::additionalAtomInfo FormatParser< xml >::defaultAtomInfo;
|
---|
58 |
|
---|
59 | // declare specialized static variables
|
---|
60 | const std::string FormatParserTrait<xml>::name = "xml";
|
---|
61 | const std::string FormatParserTrait<xml>::suffix = "xml";
|
---|
62 | const ParserTypes FormatParserTrait<xml>::type = xml;
|
---|
63 |
|
---|
64 | const char *box_axis[NDIM] = { "box_a", "box_b", "box_c" };
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Constructor.
|
---|
68 | */
|
---|
69 | FormatParser< xml >::FormatParser() :
|
---|
70 | FormatParser_common(NULL)
|
---|
71 | {}
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Destructor.
|
---|
75 | */
|
---|
76 | FormatParser< xml >::~FormatParser()
|
---|
77 | {}
|
---|
78 |
|
---|
79 | Vector 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 |
|
---|
88 | double 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 |
|
---|
96 | std::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 |
|
---|
107 | std::string fromDouble(const double &a)
|
---|
108 | {
|
---|
109 | std::stringstream returnstring;
|
---|
110 | returnstring << std::setprecision(16) << a;
|
---|
111 | return returnstring.str();
|
---|
112 | }
|
---|
113 |
|
---|
114 | std::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 | */
|
---|
130 | void 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(atom *_atom, World::getInstance().getAllAtoms())
|
---|
205 | LOG(3, "INFO: Atom " << _atom->getName() << " " << *dynamic_cast<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 | */
|
---|
217 | void FormatParser< xml >::save(std::ostream* file, const std::vector<atom *> &atoms) {
|
---|
218 | LOG(2, "DEBUG: Saving changes to xml.");
|
---|
219 |
|
---|
220 | // fill the structure with updated information
|
---|
221 | const Box &domain = World::getInstance().getDomain();
|
---|
222 | data.config.box = domain.getM();
|
---|
223 | for (size_t i=0;i<NDIM;++i)
|
---|
224 | data.config.periodicity[i] = domain.getCondition(i) == BoundaryConditions::Wrap;
|
---|
225 | data.config.p.clear();
|
---|
226 | for(std::vector<atom*>::const_iterator it = atoms.begin(); it != atoms.end(); it++) {
|
---|
227 | struct scafacos::configuration::particle p;
|
---|
228 | const additionalAtomInfo &atomInfo = getAtomData(*(*it));
|
---|
229 | p.position = (*it)->getPosition();
|
---|
230 | p.q = (*it)->getCharge();
|
---|
231 | p.potential = atomInfo.potential;
|
---|
232 | p.field = atomInfo.field;
|
---|
233 | data.config.p.push_back(p);
|
---|
234 | }
|
---|
235 |
|
---|
236 | // create the xml tree
|
---|
237 | pugi::xml_document doc;
|
---|
238 | pugi::xml_attribute attr;
|
---|
239 |
|
---|
240 | // header
|
---|
241 | pugi::xml_node xml_scafacos_test = doc.root().append_child();
|
---|
242 | xml_scafacos_test.set_name("scafacos_test");
|
---|
243 | xml_scafacos_test.append_attribute("name").set_value(data.name.c_str());
|
---|
244 | xml_scafacos_test.append_attribute("description").set_value(data.description.c_str());
|
---|
245 | xml_scafacos_test.append_attribute("reference_method").set_value(data.reference_method.c_str());
|
---|
246 | xml_scafacos_test.append_attribute("error_potential").set_value(data.error_potential);
|
---|
247 | xml_scafacos_test.append_attribute("error_field").set_value(data.error_field);
|
---|
248 |
|
---|
249 | // configuration
|
---|
250 | pugi::xml_node xml_configuration = xml_scafacos_test.append_child();
|
---|
251 | xml_configuration.set_name("configuration");
|
---|
252 | xml_configuration.append_attribute("offset").set_value(fromVector(data.config.offset).c_str());
|
---|
253 | for (size_t i=0; i<NDIM; ++i)
|
---|
254 | xml_configuration.append_attribute(box_axis[i]).set_value(fromVector(data.config.box.column(i)).c_str());
|
---|
255 | xml_configuration.append_attribute("periodicity").set_value(fromBoolean(data.config.periodicity).c_str());
|
---|
256 | xml_configuration.append_attribute("epsilon").set_value(toString(data.config.epsilon).c_str());
|
---|
257 |
|
---|
258 | // particles
|
---|
259 | for (std::vector<scafacos::configuration::particle>::const_iterator iter = data.config.p.begin();
|
---|
260 | iter != data.config.p.end();++iter) {
|
---|
261 | pugi::xml_node particle = xml_configuration.append_child();
|
---|
262 | particle.set_name("particle");
|
---|
263 | particle.append_attribute("position").set_value(fromVector((*iter).position).c_str());
|
---|
264 | particle.append_attribute("q").set_value(fromDouble((*iter).q).c_str());
|
---|
265 | particle.append_attribute("potential").set_value(fromDouble((*iter).potential).c_str());
|
---|
266 | particle.append_attribute("field").set_value(fromVector((*iter).field).c_str());
|
---|
267 | }
|
---|
268 |
|
---|
269 | // print standard header and save without declaration
|
---|
270 | *file << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
---|
271 | *file << "<!DOCTYPE scafacos_test SYSTEM 'scafacos_test.dtd'>\n";
|
---|
272 | doc.save(*file, "\t", pugi::format_no_declaration, pugi::encoding_utf8);
|
---|
273 | }
|
---|
274 |
|
---|
275 | /** Observer callback when new atom is added to World.
|
---|
276 | *
|
---|
277 | * @param id of atom
|
---|
278 | */
|
---|
279 | void FormatParser< xml >::AtomInserted(atomId_t id)
|
---|
280 | {
|
---|
281 | std::map<const atomId_t, additionalAtomInfo>::iterator iter = additionalAtomData.find(id);
|
---|
282 | ASSERT(iter == additionalAtomData.end(),
|
---|
283 | "FormatParser< xml >::AtomInserted() - additionalAtomData already present for newly added atom "
|
---|
284 | +toString(id)+".");
|
---|
285 | // additionalAtomData.insert( std::make_pair(id, additionalAtomInfo()) );
|
---|
286 | }
|
---|
287 |
|
---|
288 | /** Remove additional AtomData info, when atom has been removed from World.
|
---|
289 | *
|
---|
290 | * @param id of atom
|
---|
291 | */
|
---|
292 | void FormatParser< xml >::AtomRemoved(atomId_t id)
|
---|
293 | {
|
---|
294 | std::map<const atomId_t, additionalAtomInfo>::iterator iter = additionalAtomData.find(id);
|
---|
295 | // as we do not insert AtomData on AtomInserted, we cannot be assured of its presence
|
---|
296 | // ASSERT(iter != additionalAtomData.end(),
|
---|
297 | // "FormatParser< tremolo >::AtomRemoved() - additionalAtomData is not present for atom "
|
---|
298 | // +toString(id)+" to remove.");
|
---|
299 | if (iter != additionalAtomData.end())
|
---|
300 | additionalAtomData.erase(iter);
|
---|
301 | }
|
---|
302 |
|
---|
303 | const FormatParser< xml >::additionalAtomInfo& FormatParser< xml >::getAtomData(const atom &_atom) const
|
---|
304 | {
|
---|
305 | {
|
---|
306 | // has its own entry?
|
---|
307 | AtomInfoMap_t::const_iterator iter = additionalAtomData.find(_atom.getId());
|
---|
308 | if (iter != additionalAtomData.end()) {
|
---|
309 | return iter->second;
|
---|
310 | }
|
---|
311 | }
|
---|
312 | {
|
---|
313 | // father has an entry?
|
---|
314 | AtomInfoMap_t::const_iterator iter = additionalAtomData.find(_atom.GetTrueFather()->getId());
|
---|
315 | if (iter != additionalAtomData.end()) {
|
---|
316 | return iter->second;
|
---|
317 | }
|
---|
318 | }
|
---|
319 | return defaultAtomInfo;
|
---|
320 | }
|
---|
321 |
|
---|
322 |
|
---|
323 | #define comparator(x,y) if (x != y) { LOG(2, "DEBUG: Mismatch in " << #x << ": " << x << " != " << y); return false; }
|
---|
324 | #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; }
|
---|
325 |
|
---|
326 | bool FormatParser< xml >::scafacos::configuration::particle::operator==(const particle &p) const {
|
---|
327 | comparator(position, p.position)
|
---|
328 | num_comparator(q, p.q)
|
---|
329 | num_comparator(potential, p.potential)
|
---|
330 | comparator(field, p.field)
|
---|
331 | return true;
|
---|
332 | }
|
---|
333 |
|
---|
334 | bool FormatParser< xml >::scafacos::configuration::operator==(const configuration &c) const {
|
---|
335 | comparator(offset, c.offset)
|
---|
336 | comparator(box, c.box)
|
---|
337 | for (size_t i=0;i<NDIM;++i)
|
---|
338 | comparator(periodicity[i], c.periodicity[i])
|
---|
339 | comparator(epsilon, c.epsilon)
|
---|
340 |
|
---|
341 | if (p.size() != c.p.size()) {
|
---|
342 | LOG(2, "DEBUG: Mismatch in p's size: " << p.size() << " != " << c.p.size() << ".");
|
---|
343 | return false;
|
---|
344 | }
|
---|
345 | std::vector<scafacos::configuration::particle>::const_iterator iter = p.begin();
|
---|
346 | std::vector<scafacos::configuration::particle>::const_iterator citer = c.p.begin();
|
---|
347 | for (;iter != p.end(); ++iter, ++citer) {
|
---|
348 | if ((*iter) != (*citer))
|
---|
349 | return false;
|
---|
350 | }
|
---|
351 | return true;
|
---|
352 | }
|
---|
353 |
|
---|
354 |
|
---|
355 | bool FormatParser< xml >::scafacos::operator==(const scafacos &s) const {
|
---|
356 | comparator(name, s.name)
|
---|
357 | comparator(description, s.description)
|
---|
358 | comparator(reference_method, s.reference_method)
|
---|
359 | num_comparator(error_potential, s.error_potential)
|
---|
360 | num_comparator(error_field, s.error_field)
|
---|
361 |
|
---|
362 | if (config != s.config) {
|
---|
363 | return false;
|
---|
364 | }
|
---|
365 | return true;
|
---|
366 | }
|
---|