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