| 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
|
|---|
| 56 | FormatParser< xml >::additionalAtomInfo FormatParser< xml >::defaultAtomInfo;
|
|---|
| 57 |
|
|---|
| 58 | // declare specialized static variables
|
|---|
| 59 | const std::string FormatParserTrait<xml>::name = "xml";
|
|---|
| 60 | const std::string FormatParserTrait<xml>::suffix = "xml";
|
|---|
| 61 | const ParserTypes FormatParserTrait<xml>::type = xml;
|
|---|
| 62 |
|
|---|
| 63 | const char *box_axis[NDIM] = { "box_a", "box_b", "box_c" };
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Constructor.
|
|---|
| 67 | */
|
|---|
| 68 | FormatParser< xml >::FormatParser() :
|
|---|
| 69 | FormatParser_common(NULL)
|
|---|
| 70 | {}
|
|---|
| 71 |
|
|---|
| 72 | /**
|
|---|
| 73 | * Destructor.
|
|---|
| 74 | */
|
|---|
| 75 | FormatParser< xml >::~FormatParser()
|
|---|
| 76 | {}
|
|---|
| 77 |
|
|---|
| 78 | Vector 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 |
|
|---|
| 87 | double 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 |
|
|---|
| 95 | std::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 |
|
|---|
| 106 | std::string fromDouble(const double &a)
|
|---|
| 107 | {
|
|---|
| 108 | std::stringstream returnstring;
|
|---|
| 109 | returnstring << std::setprecision(16) << a;
|
|---|
| 110 | return returnstring.str();
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | std::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 | */
|
|---|
| 129 | void 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 | */
|
|---|
| 206 | void FormatParser< xml >::save(std::ostream* file, const std::vector<atom *> &atoms) {
|
|---|
| 207 | LOG(2, "DEBUG: 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 | */
|
|---|
| 268 | void 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 | */
|
|---|
| 281 | void 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 |
|
|---|
| 292 | const 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 |
|
|---|
| 315 | bool 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 |
|
|---|
| 323 | bool 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 |
|
|---|
| 344 | bool 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 | }
|
|---|