| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [0aa122] | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [94d5ac6] | 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/>. | 
|---|
| [bcf653] | 21 | */ | 
|---|
|  | 22 |  | 
|---|
| [6bc51d] | 23 | /* | 
|---|
|  | 24 | * TremoloParser.cpp | 
|---|
|  | 25 | * | 
|---|
|  | 26 | *  Created on: Mar 2, 2010 | 
|---|
|  | 27 | *      Author: metzler | 
|---|
|  | 28 | */ | 
|---|
|  | 29 |  | 
|---|
| [bf3817] | 30 | // include config.h | 
|---|
|  | 31 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 32 | #include <config.h> | 
|---|
|  | 33 | #endif | 
|---|
|  | 34 |  | 
|---|
| [ad011c] | 35 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| [112b09] | 36 |  | 
|---|
| [ad011c] | 37 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 38 | #include "CodePatterns/Log.hpp" | 
|---|
| [4d4d33] | 39 | #include "CodePatterns/toString.hpp" | 
|---|
| [ad011c] | 40 | #include "CodePatterns/Verbose.hpp" | 
|---|
| [42127c] | 41 |  | 
|---|
| [9131f3] | 42 | #include "TremoloParser.hpp" | 
|---|
| [42127c] | 43 |  | 
|---|
| [6f0841] | 44 | #include "Atom/atom.hpp" | 
|---|
| [129204] | 45 | #include "Bond/bond.hpp" | 
|---|
| [ccb487] | 46 | #include "Box.hpp" | 
|---|
| [42127c] | 47 | #include "Descriptors/AtomIdDescriptor.hpp" | 
|---|
| [3bdb6d] | 48 | #include "Element/element.hpp" | 
|---|
|  | 49 | #include "Element/periodentafel.hpp" | 
|---|
| [ccb487] | 50 | #include "LinearAlgebra/RealSpaceMatrix.hpp" | 
|---|
| [42127c] | 51 | #include "molecule.hpp" | 
|---|
|  | 52 | #include "MoleculeListClass.hpp" | 
|---|
|  | 53 | #include "World.hpp" | 
|---|
|  | 54 | #include "WorldTime.hpp" | 
|---|
|  | 55 |  | 
|---|
| [9131f3] | 56 |  | 
|---|
| [9f8b01] | 57 | #include <algorithm> | 
|---|
| [ca331c] | 58 | #include <boost/lambda/lambda.hpp> | 
|---|
| [2034f3] | 59 | #include <boost/lexical_cast.hpp> | 
|---|
| [72d108] | 60 | #include <boost/tokenizer.hpp> | 
|---|
| [74a444] | 61 | #include <iostream> | 
|---|
|  | 62 | #include <iomanip> | 
|---|
| [8bf9c6] | 63 | #include <map> | 
|---|
|  | 64 | #include <sstream> | 
|---|
| [ca331c] | 65 | #include <string> | 
|---|
| [8bf9c6] | 66 | #include <vector> | 
|---|
| [9131f3] | 67 |  | 
|---|
| [5a667d] | 68 | #include <boost/assign/list_of.hpp> // for 'map_list_of()' | 
|---|
|  | 69 | #include <boost/assert.hpp> | 
|---|
|  | 70 |  | 
|---|
| [765f16] | 71 | // declare specialized static variables | 
|---|
|  | 72 | const std::string FormatParserTrait<tremolo>::name = "tremolo"; | 
|---|
|  | 73 | const std::string FormatParserTrait<tremolo>::suffix = "data"; | 
|---|
|  | 74 | const ParserTypes FormatParserTrait<tremolo>::type = tremolo; | 
|---|
|  | 75 |  | 
|---|
| [5a667d] | 76 | // static instances | 
|---|
|  | 77 | std::map<std::string, TremoloKey::atomDataKey> FormatParser<tremolo>::knownKeys = | 
|---|
|  | 78 | boost::assign::map_list_of("x",TremoloKey::x) | 
|---|
|  | 79 | ("u",TremoloKey::u) | 
|---|
|  | 80 | ("F",TremoloKey::F) | 
|---|
|  | 81 | ("stress",TremoloKey::stress) | 
|---|
|  | 82 | ("Id",TremoloKey::Id) | 
|---|
|  | 83 | ("neighbors",TremoloKey::neighbors) | 
|---|
|  | 84 | ("imprData",TremoloKey::imprData) | 
|---|
|  | 85 | ("GroupMeasureTypeNo",TremoloKey::GroupMeasureTypeNo) | 
|---|
|  | 86 | ("type",TremoloKey::type) | 
|---|
|  | 87 | ("extType",TremoloKey::extType) | 
|---|
|  | 88 | ("name",TremoloKey::name) | 
|---|
|  | 89 | ("resName",TremoloKey::resName) | 
|---|
|  | 90 | ("chainID",TremoloKey::chainID) | 
|---|
|  | 91 | ("resSeq",TremoloKey::resSeq) | 
|---|
|  | 92 | ("occupancy",TremoloKey::occupancy) | 
|---|
|  | 93 | ("tempFactor",TremoloKey::tempFactor) | 
|---|
|  | 94 | ("segID",TremoloKey::segID) | 
|---|
|  | 95 | ("Charge",TremoloKey::Charge) | 
|---|
|  | 96 | ("charge",TremoloKey::charge) | 
|---|
|  | 97 | ("GrpTypeNo",TremoloKey::GrpTypeNo) | 
|---|
|  | 98 | ("torsion",TremoloKey::torsion) | 
|---|
|  | 99 | (" ",TremoloKey::noKey); // with this we can detect invalid keys | 
|---|
|  | 100 |  | 
|---|
| [9131f3] | 101 | /** | 
|---|
|  | 102 | * Constructor. | 
|---|
|  | 103 | */ | 
|---|
| [765f16] | 104 | FormatParser< tremolo >::FormatParser()  : | 
|---|
|  | 105 | FormatParser_common(NULL) | 
|---|
|  | 106 | { | 
|---|
| [4d4d33] | 107 | createKnownTypesByIdentity(); | 
|---|
|  | 108 |  | 
|---|
|  | 109 | // invert knownKeys for debug output | 
|---|
|  | 110 | for (std::map<std::string, TremoloKey::atomDataKey>::iterator iter = knownKeys.begin(); iter != knownKeys.end(); ++iter) | 
|---|
|  | 111 | knownKeyNames.insert( make_pair( iter->second, iter->first) ); | 
|---|
|  | 112 |  | 
|---|
|  | 113 | additionalAtomData.clear(); | 
|---|
| [9131f3] | 114 | } | 
|---|
|  | 115 |  | 
|---|
| [5a667d] | 116 |  | 
|---|
| [9131f3] | 117 | /** | 
|---|
|  | 118 | * Destructor. | 
|---|
|  | 119 | */ | 
|---|
| [765f16] | 120 | FormatParser< tremolo >::~FormatParser() | 
|---|
|  | 121 | { | 
|---|
| [23fd43] | 122 | usedFields_save.clear(); | 
|---|
| [b8d4a3] | 123 | additionalAtomData.clear(); | 
|---|
|  | 124 | } | 
|---|
|  | 125 |  | 
|---|
|  | 126 | /** | 
|---|
|  | 127 | * Loads atoms from a tremolo-formatted file. | 
|---|
|  | 128 | * | 
|---|
|  | 129 | * \param tremolo file | 
|---|
|  | 130 | */ | 
|---|
| [765f16] | 131 | void FormatParser< tremolo >::load(istream* file) { | 
|---|
| [8bf9c6] | 132 | std::string line; | 
|---|
|  | 133 | std::string::size_type location; | 
|---|
| [b8d4a3] | 134 |  | 
|---|
| [c0e28c] | 135 | // reset the id maps | 
|---|
|  | 136 | resetIdAssociations(); | 
|---|
|  | 137 |  | 
|---|
| [dc1d9e] | 138 | molecule *newmol = World::getInstance().createMolecule(); | 
|---|
| [bd2390] | 139 | newmol->ActiveFlag = true; | 
|---|
|  | 140 | // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include | 
|---|
|  | 141 | World::getInstance().getMolecules()->insert(newmol); | 
|---|
| [b8d4a3] | 142 | while (file->good()) { | 
|---|
|  | 143 | std::getline(*file, line, '\n'); | 
|---|
| [23fd43] | 144 | // we only parse in the first ATOMDATA line | 
|---|
|  | 145 | if (usedFields_load.empty()) { | 
|---|
| [b8d4a3] | 146 | location = line.find("ATOMDATA", 0); | 
|---|
|  | 147 | if (location != string::npos) { | 
|---|
| [23fd43] | 148 | parseAtomDataKeysLine(line, location + 8, usedFields_load); | 
|---|
| [b8d4a3] | 149 | } | 
|---|
|  | 150 | } | 
|---|
|  | 151 | if (line.length() > 0 && line.at(0) != '#') { | 
|---|
| [dc1d9e] | 152 | readAtomDataLine(line, newmol); | 
|---|
| [b8d4a3] | 153 | } | 
|---|
|  | 154 | } | 
|---|
| [23fd43] | 155 | LOG(3, "DEBUG: Local usedFields is: " << usedFields_load); | 
|---|
| [2e352f] | 156 |  | 
|---|
| [9f8b01] | 157 | // refresh atom::nr and atom::name | 
|---|
|  | 158 | std::vector<atomId_t> atoms(newmol->getAtomCount()); | 
|---|
|  | 159 | std::transform(newmol->begin(), newmol->end(), atoms.begin(), | 
|---|
|  | 160 | boost::bind(&atom::getId, _1)); | 
|---|
|  | 161 | processNeighborInformation(atoms); | 
|---|
| [b8d4a3] | 162 | adaptImprData(); | 
|---|
|  | 163 | adaptTorsion(); | 
|---|
| [23fd43] | 164 |  | 
|---|
|  | 165 | // append usedFields to global usedFields, is made unique on save, clear after use | 
|---|
|  | 166 | usedFields_save.insert(usedFields_save.end(), usedFields_load.begin(), usedFields_load.end()); | 
|---|
|  | 167 | usedFields_load.clear(); | 
|---|
| [b8d4a3] | 168 | } | 
|---|
|  | 169 |  | 
|---|
|  | 170 | /** | 
|---|
| [73916f] | 171 | * Saves the \a atoms into as a tremolo file. | 
|---|
| [b8d4a3] | 172 | * | 
|---|
|  | 173 | * \param file where to save the state | 
|---|
| [73916f] | 174 | * \param atoms atoms to store | 
|---|
| [b8d4a3] | 175 | */ | 
|---|
| [23fd43] | 176 | void FormatParser< tremolo >::save(std::ostream* file, const std::vector<atom *> &AtomList) { | 
|---|
| [47d041] | 177 | LOG(0, "Saving changes to tremolo."); | 
|---|
| [e97a44] | 178 |  | 
|---|
| [23fd43] | 179 | // install default usedFields if empty so far | 
|---|
|  | 180 | if (usedFields_save.empty()) { | 
|---|
|  | 181 | // default behavior: use all possible keys on output | 
|---|
|  | 182 | for (std::map<std::string, TremoloKey::atomDataKey>::iterator iter = knownKeys.begin(); | 
|---|
|  | 183 | iter != knownKeys.end(); ++iter) | 
|---|
|  | 184 | if (iter->second != TremoloKey::noKey) // don't add noKey | 
|---|
|  | 185 | usedFields_save.push_back(iter->first); | 
|---|
|  | 186 | } | 
|---|
|  | 187 | // make present usedFields_save unique | 
|---|
|  | 188 | makeUsedFieldsUnique(usedFields_save); | 
|---|
|  | 189 | LOG(1, "DEBUG: Global (with unique entries) usedFields_save is: " << usedFields_save); | 
|---|
|  | 190 |  | 
|---|
|  | 191 | // distribute ids continuously | 
|---|
|  | 192 | distributeContinuousIds(AtomList); | 
|---|
|  | 193 |  | 
|---|
|  | 194 | // store atomdata | 
|---|
|  | 195 | save_AtomDataLine(file); | 
|---|
|  | 196 |  | 
|---|
|  | 197 | // store box | 
|---|
|  | 198 | save_BoxLine(file); | 
|---|
| [b8d4a3] | 199 |  | 
|---|
| [23fd43] | 200 | // store particles | 
|---|
|  | 201 | for (std::vector<atom*>::const_iterator  atomIt = AtomList.begin(); | 
|---|
|  | 202 | atomIt != AtomList.end(); ++atomIt) | 
|---|
|  | 203 | saveLine(file, *atomIt); | 
|---|
|  | 204 | } | 
|---|
| [acd638] | 205 |  | 
|---|
| [5b0581] | 206 | struct usedFieldsWeakComparator | 
|---|
|  | 207 | { | 
|---|
|  | 208 | /** Special comparator regards "neighbors=4" and "neighbors=2" as equal | 
|---|
|  | 209 | * | 
|---|
|  | 210 | * \note This one is used for making usedFields unique, i.e. throwing out the "smaller" | 
|---|
|  | 211 | * neighbors. | 
|---|
|  | 212 | */ | 
|---|
|  | 213 | bool operator()(const std::string &a, const std::string &b) const | 
|---|
|  | 214 | { | 
|---|
|  | 215 | // only compare up to first equality sign | 
|---|
|  | 216 | return (a.substr(0, a.find_first_of('=')) == b.substr(0, b.find_first_of('='))); | 
|---|
|  | 217 | } | 
|---|
|  | 218 | }; | 
|---|
|  | 219 |  | 
|---|
|  | 220 | struct usedFieldsSpecialOrderer | 
|---|
|  | 221 | { | 
|---|
|  | 222 | /** Special string comparator that regards "neighbors=4" < "neighbors=2" as true and | 
|---|
|  | 223 | * the other way round as false. | 
|---|
|  | 224 | * | 
|---|
|  | 225 | * Here, we implement the operator "\a < \b" in a special way to allow the | 
|---|
|  | 226 | * above. | 
|---|
|  | 227 | * | 
|---|
|  | 228 | * \note This one is used for sorting usedFields in preparation for making it unique. | 
|---|
|  | 229 | */ | 
|---|
|  | 230 | bool operator()(const std::string &a, const std::string &b) const | 
|---|
|  | 231 | { | 
|---|
|  | 232 | // only compare up to first equality sign | 
|---|
|  | 233 | size_t a_equality = a.find_first_of('='); | 
|---|
|  | 234 | size_t b_equality = b.find_first_of('='); | 
|---|
|  | 235 | // if key before equality is not equal, return whether it is smaller or not | 
|---|
|  | 236 | if (a.substr(0, a_equality) != b.substr(0, b_equality)) { | 
|---|
|  | 237 | return a.substr(0, a_equality) < b.substr(0, b_equality); | 
|---|
|  | 238 | } else { // now we know that the key before equality is the same in either string | 
|---|
|  | 239 | // if one of them has no equality, the one with equality must go before | 
|---|
|  | 240 | if ((a_equality != std::string::npos) && (b_equality == std::string::npos)) | 
|---|
|  | 241 | return true; | 
|---|
|  | 242 | if ((a_equality == std::string::npos) && (b_equality != std::string::npos)) | 
|---|
|  | 243 | return false; | 
|---|
|  | 244 | // if both don't have equality (and the token before is equal), it is not "<" but "==" | 
|---|
|  | 245 | if ((a_equality == std::string::npos) && (b_equality == std::string::npos)) | 
|---|
|  | 246 | return false; | 
|---|
|  | 247 | // if now both have equality sign, the larger value after it, must come first | 
|---|
|  | 248 | return a.substr(a_equality, std::string::npos) > b.substr(b_equality, std::string::npos); | 
|---|
|  | 249 | } | 
|---|
|  | 250 | } | 
|---|
|  | 251 | }; | 
|---|
|  | 252 |  | 
|---|
| [23fd43] | 253 | /** Helper function to make \given fields unique while preserving the order of first appearance. | 
|---|
|  | 254 | * | 
|---|
|  | 255 | * As std::unique only removes element if equal to predecessor, a vector is only | 
|---|
|  | 256 | * made unique if sorted beforehand. But sorting would destroy order of first | 
|---|
|  | 257 | * appearance, hence we do the sorting on a temporary field and add the unique | 
|---|
|  | 258 | * elements in the order as in \a fields. | 
|---|
|  | 259 | * | 
|---|
|  | 260 | * @param fields usedFields to make unique while preserving order of appearance | 
|---|
|  | 261 | */ | 
|---|
| [27cfde] | 262 | void FormatParser< tremolo >::makeUsedFieldsUnique(usedFields_t &fields) const | 
|---|
| [23fd43] | 263 | { | 
|---|
|  | 264 | // std::unique only removes if predecessor is equal, not over whole range, hence do it manually | 
|---|
| [27cfde] | 265 | usedFields_t temp_fields(fields); | 
|---|
| [5b0581] | 266 | usedFieldsSpecialOrderer SpecialOrderer; | 
|---|
|  | 267 | usedFieldsWeakComparator WeakComparator; | 
|---|
|  | 268 | std::sort(temp_fields.begin(), temp_fields.end(), SpecialOrderer); | 
|---|
| [23fd43] | 269 | usedFields_t::iterator it = | 
|---|
| [5b0581] | 270 | std::unique(temp_fields.begin(), temp_fields.end(), WeakComparator); | 
|---|
| [23fd43] | 271 | temp_fields.erase(it, temp_fields.end()); | 
|---|
| [27cfde] | 272 | usedFields_t usedfields(fields); | 
|---|
|  | 273 | fields.clear(); | 
|---|
|  | 274 | fields.reserve(temp_fields.size()); | 
|---|
| [23fd43] | 275 | // now go through each usedFields entry, check if in temp_fields and remove there on first occurence | 
|---|
|  | 276 | for (usedFields_t::const_iterator iter = usedfields.begin(); | 
|---|
|  | 277 | iter != usedfields.end(); ++iter) { | 
|---|
|  | 278 | usedFields_t::iterator uniqueiter = | 
|---|
|  | 279 | std::find(temp_fields.begin(), temp_fields.end(), *iter); | 
|---|
|  | 280 | if (uniqueiter != temp_fields.end()) { | 
|---|
| [27cfde] | 281 | fields.push_back(*iter); | 
|---|
| [23fd43] | 282 | // add only once to ATOMDATA | 
|---|
|  | 283 | temp_fields.erase(uniqueiter); | 
|---|
|  | 284 | } | 
|---|
|  | 285 | } | 
|---|
|  | 286 | ASSERT( temp_fields.empty(), | 
|---|
|  | 287 | "FormatParser< tremolo >::save() - still unique entries left in temp_fields after unique?"); | 
|---|
|  | 288 | } | 
|---|
|  | 289 |  | 
|---|
|  | 290 |  | 
|---|
|  | 291 | /** Resets and distributes the indices continuously. | 
|---|
|  | 292 | * | 
|---|
|  | 293 | * \param atoms atoms to store | 
|---|
|  | 294 | */ | 
|---|
|  | 295 | void FormatParser< tremolo >::distributeContinuousIds(const std::vector<atom *> &AtomList) | 
|---|
|  | 296 | { | 
|---|
| [812155] | 297 | resetIdAssociations(); | 
|---|
|  | 298 | atomId_t lastid = 0; | 
|---|
| [23fd43] | 299 | for (std::vector<atom*>::const_iterator atomIt = AtomList.begin(); | 
|---|
|  | 300 | atomIt != AtomList.end(); ++atomIt) | 
|---|
| [812155] | 301 | associateLocaltoGlobalId(++lastid, (*atomIt)->getId()); | 
|---|
| [23fd43] | 302 | } | 
|---|
| [812155] | 303 |  | 
|---|
| [23fd43] | 304 | /** Store Atomdata line to \a file. | 
|---|
|  | 305 | * | 
|---|
|  | 306 | * @param file output stream | 
|---|
|  | 307 | */ | 
|---|
|  | 308 | void FormatParser< tremolo >::save_AtomDataLine(std::ostream* file) const | 
|---|
|  | 309 | { | 
|---|
| [b8d4a3] | 310 | *file << "# ATOMDATA"; | 
|---|
| [23fd43] | 311 | for (usedFields_t::const_iterator it=usedFields_save.begin(); | 
|---|
|  | 312 | it != usedFields_save.end(); ++it) | 
|---|
| [b8d4a3] | 313 | *file << "\t" << *it; | 
|---|
| [23fd43] | 314 | *file << std::endl; | 
|---|
|  | 315 | } | 
|---|
| [ccb487] | 316 |  | 
|---|
| [23fd43] | 317 | /** Store Box info to \a file | 
|---|
|  | 318 | * | 
|---|
|  | 319 | * @param file output stream | 
|---|
|  | 320 | */ | 
|---|
|  | 321 | void FormatParser< tremolo >::save_BoxLine(std::ostream* file) const | 
|---|
|  | 322 | { | 
|---|
| [ccb487] | 323 | *file << "# Box"; | 
|---|
|  | 324 | const RealSpaceMatrix &M = World::getInstance().getDomain().getM(); | 
|---|
|  | 325 | for (size_t i=0; i<NDIM;++i) | 
|---|
|  | 326 | for (size_t j=0; j<NDIM;++j) | 
|---|
|  | 327 | *file << "\t" << M.at(i,j); | 
|---|
|  | 328 | *file << std::endl; | 
|---|
| [b8d4a3] | 329 | } | 
|---|
|  | 330 |  | 
|---|
| [6bc86c] | 331 | /** Add default info, when new atom is added to World. | 
|---|
|  | 332 | * | 
|---|
|  | 333 | * @param id of atom | 
|---|
|  | 334 | */ | 
|---|
| [765f16] | 335 | void FormatParser< tremolo >::AtomInserted(atomId_t id) | 
|---|
| [6bc86c] | 336 | { | 
|---|
| [8bf9c6] | 337 | std::map<const atomId_t, TremoloAtomInfoContainer>::iterator iter = additionalAtomData.find(id); | 
|---|
| [6bc86c] | 338 | ASSERT(iter == additionalAtomData.end(), | 
|---|
| [765f16] | 339 | "FormatParser< tremolo >::AtomInserted() - additionalAtomData already present for newly added atom " | 
|---|
| [6bc86c] | 340 | +toString(id)+"."); | 
|---|
|  | 341 | // don't add entry, as this gives a default resSeq of 0 not the molecule id | 
|---|
|  | 342 | // additionalAtomData.insert( std::make_pair(id, TremoloAtomInfoContainer()) ); | 
|---|
|  | 343 | } | 
|---|
|  | 344 |  | 
|---|
|  | 345 | /** Remove additional AtomData info, when atom has been removed from World. | 
|---|
|  | 346 | * | 
|---|
|  | 347 | * @param id of atom | 
|---|
|  | 348 | */ | 
|---|
| [765f16] | 349 | void FormatParser< tremolo >::AtomRemoved(atomId_t id) | 
|---|
| [6bc86c] | 350 | { | 
|---|
| [8bf9c6] | 351 | std::map<const atomId_t, TremoloAtomInfoContainer>::iterator iter = additionalAtomData.find(id); | 
|---|
| [6bc86c] | 352 | // as we do not insert AtomData on AtomInserted, we cannot be assured of its presence | 
|---|
|  | 353 | //  ASSERT(iter != additionalAtomData.end(), | 
|---|
| [765f16] | 354 | //      "FormatParser< tremolo >::AtomRemoved() - additionalAtomData is not present for atom " | 
|---|
| [6bc86c] | 355 | //      +toString(id)+" to remove."); | 
|---|
|  | 356 | if (iter != additionalAtomData.end()) | 
|---|
|  | 357 | additionalAtomData.erase(iter); | 
|---|
|  | 358 | } | 
|---|
|  | 359 |  | 
|---|
| [b8d4a3] | 360 | /** | 
|---|
|  | 361 | * Writes one line of tremolo-formatted data to the provided stream. | 
|---|
|  | 362 | * | 
|---|
|  | 363 | * \param stream where to write the line to | 
|---|
|  | 364 | * \param reference to the atom of which information should be written | 
|---|
|  | 365 | */ | 
|---|
| [23fd43] | 366 | void FormatParser< tremolo >::saveLine(std::ostream* file, const atom* currentAtom) | 
|---|
|  | 367 | { | 
|---|
| [b8d4a3] | 368 | TremoloKey::atomDataKey currentField; | 
|---|
|  | 369 |  | 
|---|
| [47d041] | 370 | LOG(4, "INFO: Saving atom " << *currentAtom << ", its father id is " << currentAtom->GetTrueFather()->getId()); | 
|---|
| [4d4d33] | 371 |  | 
|---|
| [23fd43] | 372 | for (usedFields_t::iterator it = usedFields_save.begin(); it != usedFields_save.end(); it++) { | 
|---|
| [b8d4a3] | 373 | currentField = knownKeys[it->substr(0, it->find("="))]; | 
|---|
|  | 374 | switch (currentField) { | 
|---|
|  | 375 | case TremoloKey::x : | 
|---|
|  | 376 | // for the moment, assume there are always three dimensions | 
|---|
| [47d041] | 377 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getPosition()); | 
|---|
| [d74077] | 378 | *file << currentAtom->at(0) << "\t"; | 
|---|
|  | 379 | *file << currentAtom->at(1) << "\t"; | 
|---|
|  | 380 | *file << currentAtom->at(2) << "\t"; | 
|---|
| [b8d4a3] | 381 | break; | 
|---|
|  | 382 | case TremoloKey::u : | 
|---|
|  | 383 | // for the moment, assume there are always three dimensions | 
|---|
| [47d041] | 384 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getAtomicVelocity()); | 
|---|
| [bce72c] | 385 | *file << currentAtom->getAtomicVelocity()[0] << "\t"; | 
|---|
|  | 386 | *file << currentAtom->getAtomicVelocity()[1] << "\t"; | 
|---|
|  | 387 | *file << currentAtom->getAtomicVelocity()[2] << "\t"; | 
|---|
| [b8d4a3] | 388 | break; | 
|---|
| [e6e4a0] | 389 | case TremoloKey::F : | 
|---|
|  | 390 | // for the moment, assume there are always three dimensions | 
|---|
|  | 391 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getAtomicForce()); | 
|---|
|  | 392 | *file << currentAtom->getAtomicForce()[0] << "\t"; | 
|---|
|  | 393 | *file << currentAtom->getAtomicForce()[1] << "\t"; | 
|---|
|  | 394 | *file << currentAtom->getAtomicForce()[2] << "\t"; | 
|---|
|  | 395 | break; | 
|---|
| [305e7e] | 396 | case TremoloKey::type : | 
|---|
| [acd638] | 397 | if (additionalAtomData.count(currentAtom->getId())) { | 
|---|
|  | 398 | if (additionalAtomData[currentAtom->getId()].get(currentField) != "-") { | 
|---|
| [47d041] | 399 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField)); | 
|---|
| [acd638] | 400 | *file << additionalAtomData[currentAtom->getId()].get(currentField) << "\t"; | 
|---|
|  | 401 | } else { | 
|---|
| [47d041] | 402 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " default value: " << currentAtom->getType()->getSymbol()); | 
|---|
| [acd638] | 403 | *file << currentAtom->getType()->getSymbol() << "\t"; | 
|---|
|  | 404 | } | 
|---|
|  | 405 | } else if (additionalAtomData.count(currentAtom->GetTrueFather()->getId())) { | 
|---|
|  | 406 | if (additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField) != "-") { | 
|---|
| [47d041] | 407 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " stuff from father: " << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField)); | 
|---|
| [acd638] | 408 | *file << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField) << "\t"; | 
|---|
|  | 409 | } else { | 
|---|
| [47d041] | 410 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " default value from father: " << currentAtom->GetTrueFather()->getType()->getSymbol()); | 
|---|
| [acd638] | 411 | *file << currentAtom->GetTrueFather()->getType()->getSymbol() << "\t"; | 
|---|
|  | 412 | } | 
|---|
| [4d4d33] | 413 | } else { | 
|---|
| [47d041] | 414 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " its default value: " << currentAtom->getType()->getSymbol()); | 
|---|
| [acd638] | 415 | *file << currentAtom->getType()->getSymbol() << "\t"; | 
|---|
| [4d4d33] | 416 | } | 
|---|
| [b8d4a3] | 417 | break; | 
|---|
|  | 418 | case TremoloKey::Id : | 
|---|
| [47d041] | 419 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getId()+1); | 
|---|
| [812155] | 420 | *file << getLocalId(currentAtom->getId()) << "\t"; | 
|---|
| [b8d4a3] | 421 | break; | 
|---|
|  | 422 | case TremoloKey::neighbors : | 
|---|
| [47d041] | 423 | LOG(3, "Writing type " << knownKeyNames[currentField]); | 
|---|
| [b8d4a3] | 424 | writeNeighbors(file, atoi(it->substr(it->find("=") + 1, 1).c_str()), currentAtom); | 
|---|
|  | 425 | break; | 
|---|
| [74a444] | 426 | case TremoloKey::resSeq : | 
|---|
| [4d4d33] | 427 | if (additionalAtomData.count(currentAtom->getId())) { | 
|---|
| [47d041] | 428 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField)); | 
|---|
| [74a444] | 429 | *file << additionalAtomData[currentAtom->getId()].get(currentField); | 
|---|
|  | 430 | } else if (currentAtom->getMolecule() != NULL) { | 
|---|
| [47d041] | 431 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " its own id: " << currentAtom->getMolecule()->getId()+1); | 
|---|
| [74a444] | 432 | *file << setw(4) << currentAtom->getMolecule()->getId()+1; | 
|---|
|  | 433 | } else { | 
|---|
| [47d041] | 434 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " default value: " << defaultAdditionalData.get(currentField)); | 
|---|
| [74a444] | 435 | *file << defaultAdditionalData.get(currentField); | 
|---|
|  | 436 | } | 
|---|
|  | 437 | *file << "\t"; | 
|---|
| [4d4d33] | 438 | break; | 
|---|
| [2034f3] | 439 | case TremoloKey::charge : | 
|---|
|  | 440 | if (currentAtom->getCharge() == 0.) { | 
|---|
|  | 441 | if (additionalAtomData.count(currentAtom->getId())) { | 
|---|
|  | 442 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField)); | 
|---|
|  | 443 | *file << additionalAtomData[currentAtom->getId()].get(currentField); | 
|---|
|  | 444 | } else if (additionalAtomData.count(currentAtom->GetTrueFather()->getId())) { | 
|---|
|  | 445 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " stuff from father: " << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField)); | 
|---|
|  | 446 | *file << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField); | 
|---|
|  | 447 | } else { | 
|---|
|  | 448 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " AtomInfo::charge : " << currentAtom->getCharge()); | 
|---|
|  | 449 | *file << currentAtom->getCharge(); | 
|---|
|  | 450 | } | 
|---|
|  | 451 | } else { | 
|---|
|  | 452 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " AtomInfo::charge : " << currentAtom->getCharge()); | 
|---|
|  | 453 | *file << currentAtom->getCharge(); | 
|---|
|  | 454 | } | 
|---|
|  | 455 | *file << "\t"; | 
|---|
|  | 456 | break; | 
|---|
| [b8d4a3] | 457 | default : | 
|---|
| [4d4d33] | 458 | if (additionalAtomData.count(currentAtom->getId())) { | 
|---|
| [47d041] | 459 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField)); | 
|---|
| [74a444] | 460 | *file << additionalAtomData[currentAtom->getId()].get(currentField); | 
|---|
| [4d4d33] | 461 | } else if (additionalAtomData.count(currentAtom->GetTrueFather()->getId())) { | 
|---|
| [47d041] | 462 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " stuff from father: " << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField)); | 
|---|
| [74a444] | 463 | *file << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField); | 
|---|
|  | 464 | } else { | 
|---|
| [47d041] | 465 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " the default: " << defaultAdditionalData.get(currentField)); | 
|---|
| [74a444] | 466 | *file << defaultAdditionalData.get(currentField); | 
|---|
|  | 467 | } | 
|---|
| [b8d4a3] | 468 | *file << "\t"; | 
|---|
|  | 469 | break; | 
|---|
|  | 470 | } | 
|---|
|  | 471 | } | 
|---|
|  | 472 |  | 
|---|
| [23fd43] | 473 | *file << std::endl; | 
|---|
| [b8d4a3] | 474 | } | 
|---|
|  | 475 |  | 
|---|
|  | 476 | /** | 
|---|
|  | 477 | * Writes the neighbor information of one atom to the provided stream. | 
|---|
|  | 478 | * | 
|---|
| [9d83b6] | 479 | * Note that ListOfBonds of WorldTime::CurrentTime is used. | 
|---|
|  | 480 | * | 
|---|
| [b8d4a3] | 481 | * \param stream where to write neighbor information to | 
|---|
|  | 482 | * \param number of neighbors | 
|---|
|  | 483 | * \param reference to the atom of which to take the neighbor information | 
|---|
|  | 484 | */ | 
|---|
| [23fd43] | 485 | void FormatParser< tremolo >::writeNeighbors(std::ostream* file, const int numberOfNeighbors, const atom* currentAtom) { | 
|---|
| [9d83b6] | 486 | const BondList& ListOfBonds = currentAtom->getListOfBonds(); | 
|---|
| [ca2cfa] | 487 | // sort bonded indices | 
|---|
|  | 488 | typedef std::set<atomId_t> sortedIndices; | 
|---|
|  | 489 | sortedIndices sortedBonds; | 
|---|
|  | 490 | for (BondList::const_iterator iter = ListOfBonds.begin(); | 
|---|
|  | 491 | iter != ListOfBonds.end(); ++iter) | 
|---|
| [812155] | 492 | sortedBonds.insert(getLocalId((*iter)->GetOtherAtom(currentAtom)->getId())); | 
|---|
| [ca2cfa] | 493 | // print indices | 
|---|
|  | 494 | sortedIndices::const_iterator currentBond = sortedBonds.begin(); | 
|---|
| [b8d4a3] | 495 | for (int i = 0; i < numberOfNeighbors; i++) { | 
|---|
| [812155] | 496 | *file << (currentBond != sortedBonds.end() ? (*currentBond) : 0) << "\t"; | 
|---|
| [ca2cfa] | 497 | if (currentBond != sortedBonds.end()) | 
|---|
| [0bbfa1] | 498 | ++currentBond; | 
|---|
| [b8d4a3] | 499 | } | 
|---|
| [9131f3] | 500 | } | 
|---|
|  | 501 |  | 
|---|
|  | 502 | /** | 
|---|
| [23fd43] | 503 | * Stores keys from the ATOMDATA line in \a fields. | 
|---|
| [9131f3] | 504 | * | 
|---|
|  | 505 | * \param line to parse the keys from | 
|---|
| [23fd43] | 506 | * \param offset with which offset the keys begin within the line | 
|---|
|  | 507 | * \param fields which usedFields to use | 
|---|
| [9131f3] | 508 | */ | 
|---|
| [23fd43] | 509 | void FormatParser< tremolo >::parseAtomDataKeysLine( | 
|---|
|  | 510 | const std::string &line, | 
|---|
|  | 511 | const int offset, | 
|---|
|  | 512 | usedFields_t &fields) { | 
|---|
| [8bf9c6] | 513 | std::string keyword; | 
|---|
|  | 514 | std::stringstream lineStream; | 
|---|
| [9131f3] | 515 |  | 
|---|
|  | 516 | lineStream << line.substr(offset); | 
|---|
|  | 517 | while (lineStream.good()) { | 
|---|
|  | 518 | lineStream >> keyword; | 
|---|
| [b8d4a3] | 519 | if (knownKeys[keyword.substr(0, keyword.find("="))] == TremoloKey::noKey) { | 
|---|
| [ecb799] | 520 | // TODO: throw exception about unknown key | 
|---|
| [5a667d] | 521 | cout << "Unknown key: " << keyword.substr(0, keyword.find("=")) << " is not part of the tremolo format specification." << endl; | 
|---|
|  | 522 | throw IllegalParserKeyException(); | 
|---|
| [4415da] | 523 | break; | 
|---|
|  | 524 | } | 
|---|
| [23fd43] | 525 | fields.push_back(keyword); | 
|---|
| [9131f3] | 526 | } | 
|---|
| [23fd43] | 527 | //LOG(1, "INFO: " << fields); | 
|---|
| [9131f3] | 528 | } | 
|---|
|  | 529 |  | 
|---|
| [5a667d] | 530 | /** | 
|---|
|  | 531 | * Tests whether the keys from the ATOMDATA line can be read correctly. | 
|---|
|  | 532 | * | 
|---|
|  | 533 | * \param line to parse the keys from | 
|---|
|  | 534 | */ | 
|---|
|  | 535 | bool FormatParser< tremolo >::testParseAtomDataKeysLine( | 
|---|
|  | 536 | const std::string &line) { | 
|---|
|  | 537 | std::string keyword; | 
|---|
|  | 538 | std::stringstream lineStream; | 
|---|
|  | 539 |  | 
|---|
|  | 540 | // check string after ATOMDATA | 
|---|
|  | 541 | const std::string AtomData("ATOMDATA"); | 
|---|
|  | 542 | const size_t AtomDataOffset = line.find(AtomData, 0); | 
|---|
|  | 543 | if (AtomDataOffset == std::string::npos) | 
|---|
|  | 544 | lineStream << line; | 
|---|
|  | 545 | else | 
|---|
|  | 546 | lineStream << line.substr(AtomDataOffset + AtomData.length()); | 
|---|
|  | 547 | while (lineStream.good()) { | 
|---|
|  | 548 | lineStream >> keyword; | 
|---|
|  | 549 | //LOG(2, "DEBUG: Checking key " << keyword.substr(0, keyword.find("=")) << "."); | 
|---|
|  | 550 | if (knownKeys[keyword.substr(0, keyword.find("="))] == TremoloKey::noKey) | 
|---|
|  | 551 | return false; | 
|---|
|  | 552 | } | 
|---|
|  | 553 | //LOG(1, "INFO: " << fields); | 
|---|
|  | 554 | return true; | 
|---|
|  | 555 | } | 
|---|
|  | 556 |  | 
|---|
| [ca331c] | 557 | std::string FormatParser< tremolo >::getAtomData() const | 
|---|
|  | 558 | { | 
|---|
|  | 559 | std::stringstream output; | 
|---|
|  | 560 | std::for_each(usedFields_save.begin(), usedFields_save.end(), | 
|---|
|  | 561 | output << boost::lambda::_1 << " "); | 
|---|
|  | 562 | const std::string returnstring(output.str()); | 
|---|
|  | 563 | return returnstring.substr(0, returnstring.find_last_of(" ")); | 
|---|
|  | 564 | } | 
|---|
|  | 565 |  | 
|---|
|  | 566 | /** Appends the properties per atom to print to .data file by parsing line from | 
|---|
|  | 567 | *  \a atomdata_string. | 
|---|
|  | 568 | * | 
|---|
|  | 569 | *  We just call \sa  FormatParser< tremolo >::parseAtomDataKeysLine(). | 
|---|
|  | 570 | * | 
|---|
|  | 571 | * @param atomdata_string line to parse with space-separated values | 
|---|
|  | 572 | */ | 
|---|
|  | 573 | void FormatParser< tremolo >::setAtomData(const std::string &atomdata_string) | 
|---|
|  | 574 | { | 
|---|
|  | 575 | parseAtomDataKeysLine(atomdata_string, 0, usedFields_save); | 
|---|
|  | 576 | } | 
|---|
|  | 577 |  | 
|---|
| [81c980b] | 578 | /** Sets the properties per atom to print to .data file by parsing line from | 
|---|
|  | 579 | *  \a atomdata_string. | 
|---|
|  | 580 | * | 
|---|
| [23fd43] | 581 | *  We just call \sa  FormatParser< tremolo >::parseAtomDataKeysLine(), however | 
|---|
|  | 582 | *  we clear FormatParser< tremolo >::usedFields_save. | 
|---|
| [81c980b] | 583 | * | 
|---|
|  | 584 | * @param atomdata_string line to parse with space-separated values | 
|---|
|  | 585 | */ | 
|---|
| [ca331c] | 586 | void FormatParser< tremolo >::resetAtomData(const std::string &atomdata_string) | 
|---|
| [81c980b] | 587 | { | 
|---|
| [23fd43] | 588 | usedFields_save.clear(); | 
|---|
|  | 589 | parseAtomDataKeysLine(atomdata_string, 0, usedFields_save); | 
|---|
| [81c980b] | 590 | } | 
|---|
|  | 591 |  | 
|---|
|  | 592 |  | 
|---|
| [9131f3] | 593 | /** | 
|---|
|  | 594 | * Reads one data line of a tremolo file and interprets it according to the keys | 
|---|
|  | 595 | * obtained from the ATOMDATA line. | 
|---|
|  | 596 | * | 
|---|
|  | 597 | * \param line to parse as an atom | 
|---|
| [dc1d9e] | 598 | * \param *newmol molecule to add atom to | 
|---|
| [9131f3] | 599 | */ | 
|---|
| [23fd43] | 600 | void FormatParser< tremolo >::readAtomDataLine(const std::string &line, molecule *newmol = NULL) { | 
|---|
| [8bf9c6] | 601 | std::stringstream lineStream; | 
|---|
| [4415da] | 602 | atom* newAtom = World::getInstance().createAtom(); | 
|---|
| [89a31d] | 603 | const atomId_t atomid = newAtom->getId(); | 
|---|
|  | 604 | additionalAtomData[atomid] = TremoloAtomInfoContainer(); // fill with default values | 
|---|
|  | 605 | TremoloAtomInfoContainer *atomInfo = &additionalAtomData[atomid]; | 
|---|
| [b8d4a3] | 606 | TremoloKey::atomDataKey currentField; | 
|---|
| [72d108] | 607 | ConvertTo<double> toDouble; | 
|---|
|  | 608 | ConvertTo<int> toInt; | 
|---|
| [056e70] | 609 | Vector tempVector; | 
|---|
| [72d108] | 610 |  | 
|---|
|  | 611 | // setup tokenizer, splitting up white-spaced entries | 
|---|
|  | 612 | typedef boost::tokenizer<boost::char_separator<char> > | 
|---|
|  | 613 | tokenizer; | 
|---|
|  | 614 | boost::char_separator<char> whitespacesep(" \t"); | 
|---|
|  | 615 | tokenizer tokens(line, whitespacesep); | 
|---|
|  | 616 | ASSERT(tokens.begin() != tokens.end(), | 
|---|
| [765f16] | 617 | "FormatParser< tremolo >::readAtomDataLine - empty string, need at least ' '!"); | 
|---|
| [72d108] | 618 | tokenizer::iterator tok_iter = tokens.begin(); | 
|---|
|  | 619 | // then associate each token to each file | 
|---|
| [23fd43] | 620 | for (usedFields_t::const_iterator it = usedFields_load.begin(); it < usedFields_load.end(); it++) { | 
|---|
| [72d108] | 621 | const std::string keyName = it->substr(0, it->find("=")); | 
|---|
|  | 622 | currentField = knownKeys[keyName]; | 
|---|
| [8bf9c6] | 623 | const std::string word = *tok_iter; | 
|---|
| [47d041] | 624 | LOG(4, "INFO: Parsing key " << keyName << " with remaining data " << word); | 
|---|
| [4415da] | 625 | switch (currentField) { | 
|---|
| [b8d4a3] | 626 | case TremoloKey::x : | 
|---|
| [4415da] | 627 | // for the moment, assume there are always three dimensions | 
|---|
| [d74077] | 628 | for (int i=0;i<NDIM;i++) { | 
|---|
| [765f16] | 629 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for x["+toString(i)+"]!"); | 
|---|
| [47d041] | 630 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter); | 
|---|
| [72d108] | 631 | newAtom->set(i, toDouble(*tok_iter)); | 
|---|
|  | 632 | tok_iter++; | 
|---|
| [d74077] | 633 | } | 
|---|
| [4415da] | 634 | break; | 
|---|
| [b8d4a3] | 635 | case TremoloKey::u : | 
|---|
| [4415da] | 636 | // for the moment, assume there are always three dimensions | 
|---|
| [72d108] | 637 | for (int i=0;i<NDIM;i++) { | 
|---|
| [765f16] | 638 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for u["+toString(i)+"]!"); | 
|---|
| [47d041] | 639 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter); | 
|---|
| [056e70] | 640 | tempVector[i] = toDouble(*tok_iter); | 
|---|
| [72d108] | 641 | tok_iter++; | 
|---|
|  | 642 | } | 
|---|
| [056e70] | 643 | newAtom->setAtomicVelocity(tempVector); | 
|---|
| [4415da] | 644 | break; | 
|---|
| [e6e4a0] | 645 | case TremoloKey::F : | 
|---|
|  | 646 | // for the moment, assume there are always three dimensions | 
|---|
|  | 647 | for (int i=0;i<NDIM;i++) { | 
|---|
|  | 648 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for F["+toString(i)+"]!"); | 
|---|
|  | 649 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter); | 
|---|
|  | 650 | tempVector[i] = toDouble(*tok_iter); | 
|---|
|  | 651 | tok_iter++; | 
|---|
|  | 652 | } | 
|---|
|  | 653 | newAtom->setAtomicForce(tempVector); | 
|---|
|  | 654 | break; | 
|---|
| [305e7e] | 655 | case TremoloKey::type : | 
|---|
| [4d4d33] | 656 | { | 
|---|
| [765f16] | 657 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
| [47d041] | 658 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter); | 
|---|
| [a275b3] | 659 | std::string element; | 
|---|
|  | 660 | try { | 
|---|
|  | 661 | element = knownTypes.getType(*tok_iter); | 
|---|
|  | 662 | } catch(IllegalParserKeyException) { | 
|---|
|  | 663 | // clean up | 
|---|
|  | 664 | World::getInstance().destroyAtom(newAtom); | 
|---|
|  | 665 | // give an error | 
|---|
|  | 666 | ELOG(0, "TremoloParser: I do not understand the element token " << *tok_iter << "."); | 
|---|
|  | 667 | } | 
|---|
| [4d4d33] | 668 | // put type name into container for later use | 
|---|
|  | 669 | atomInfo->set(currentField, *tok_iter); | 
|---|
| [47d041] | 670 | LOG(4, "INFO: Parsing element " << (*tok_iter) << " as " << element << " according to KnownTypes."); | 
|---|
| [72d108] | 671 | tok_iter++; | 
|---|
| [4d4d33] | 672 | newAtom->setType(World::getInstance().getPeriode()->FindElement(element)); | 
|---|
| [b8d4a3] | 673 | ASSERT(newAtom->getType(), "Type was not set for this atom"); | 
|---|
| [4415da] | 674 | break; | 
|---|
| [4d4d33] | 675 | } | 
|---|
| [b8d4a3] | 676 | case TremoloKey::Id : | 
|---|
| [765f16] | 677 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
| [47d041] | 678 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter); | 
|---|
| [c0e28c] | 679 | associateLocaltoGlobalId(toInt(*tok_iter), atomid); | 
|---|
| [72d108] | 680 | tok_iter++; | 
|---|
| [4415da] | 681 | break; | 
|---|
| [b8d4a3] | 682 | case TremoloKey::neighbors : | 
|---|
| [72d108] | 683 | for (int i=0;i<atoi(it->substr(it->find("=") + 1, 1).c_str());i++) { | 
|---|
| [765f16] | 684 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
| [47d041] | 685 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter); | 
|---|
| [72d108] | 686 | lineStream << *tok_iter << "\t"; | 
|---|
|  | 687 | tok_iter++; | 
|---|
|  | 688 | } | 
|---|
| [b8d4a3] | 689 | readNeighbors(&lineStream, | 
|---|
| [89a31d] | 690 | atoi(it->substr(it->find("=") + 1, 1).c_str()), atomid); | 
|---|
| [9131f3] | 691 | break; | 
|---|
| [2034f3] | 692 | case TremoloKey::charge : | 
|---|
|  | 693 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
|  | 694 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter); | 
|---|
|  | 695 | atomInfo->set(currentField, *tok_iter); | 
|---|
|  | 696 | newAtom->setCharge(boost::lexical_cast<double>(*tok_iter)); | 
|---|
|  | 697 | tok_iter++; | 
|---|
|  | 698 | break; | 
|---|
| [9131f3] | 699 | default : | 
|---|
| [765f16] | 700 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
| [47d041] | 701 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter); | 
|---|
| [72d108] | 702 | atomInfo->set(currentField, *tok_iter); | 
|---|
|  | 703 | tok_iter++; | 
|---|
| [9131f3] | 704 | break; | 
|---|
|  | 705 | } | 
|---|
|  | 706 | } | 
|---|
| [89a31d] | 707 | LOG(3, "INFO: Parsed atom " << atomid << "."); | 
|---|
|  | 708 | if (newmol != NULL) | 
|---|
| [dc1d9e] | 709 | newmol->AddAtom(newAtom); | 
|---|
| [6bc51d] | 710 | } | 
|---|
| [9131f3] | 711 |  | 
|---|
| [531f27] | 712 | bool FormatParser< tremolo >::saveAtomsInExttypes(std::ostream &output, const std::vector<atom*> &atoms, const int id) const | 
|---|
|  | 713 | { | 
|---|
|  | 714 | bool status = true; | 
|---|
|  | 715 | // parse the file | 
|---|
|  | 716 | for (std::vector<atom *>::const_iterator iter = atoms.begin(); | 
|---|
|  | 717 | iter != atoms.end(); ++iter) { | 
|---|
|  | 718 | const int atomicid = getLocalId((*iter)->getId()); | 
|---|
|  | 719 | if (atomicid == -1) | 
|---|
|  | 720 | status = false; | 
|---|
|  | 721 | output << atomicid << "\t" << id << std::endl; | 
|---|
|  | 722 | } | 
|---|
|  | 723 |  | 
|---|
|  | 724 | return status; | 
|---|
|  | 725 | } | 
|---|
|  | 726 |  | 
|---|
| [b8d4a3] | 727 | /** | 
|---|
|  | 728 | * Reads neighbor information for one atom from the input. | 
|---|
|  | 729 | * | 
|---|
| [0bbfa1] | 730 | * \param line stream where to read the information from | 
|---|
|  | 731 | * \param numberOfNeighbors number of neighbors to read | 
|---|
|  | 732 | * \param atomid world id of the atom the information belongs to | 
|---|
| [b8d4a3] | 733 | */ | 
|---|
| [23fd43] | 734 | void FormatParser< tremolo >::readNeighbors(std::stringstream* line, const int numberOfNeighbors, const int atomId) { | 
|---|
| [b8d4a3] | 735 | int neighborId = 0; | 
|---|
|  | 736 | for (int i = 0; i < numberOfNeighbors; i++) { | 
|---|
|  | 737 | *line >> neighborId; | 
|---|
|  | 738 | // 0 is used to fill empty neighbor positions in the tremolo file. | 
|---|
|  | 739 | if (neighborId > 0) { | 
|---|
| [47d041] | 740 | LOG(4, "INFO: Atom with global id " << atomId | 
|---|
|  | 741 | << " has neighbour with serial " << neighborId); | 
|---|
| [b8d4a3] | 742 | additionalAtomData[atomId].neighbors.push_back(neighborId); | 
|---|
|  | 743 | } | 
|---|
|  | 744 | } | 
|---|
|  | 745 | } | 
|---|
| [9131f3] | 746 |  | 
|---|
|  | 747 | /** | 
|---|
| [23fd43] | 748 | * Checks whether the provided name is within \a fields. | 
|---|
| [b8d4a3] | 749 | * | 
|---|
| [23fd43] | 750 | * \param fields which usedFields to use | 
|---|
|  | 751 | * \param fieldName name to check | 
|---|
| [b8d4a3] | 752 | * \return true if the field name is used | 
|---|
| [9131f3] | 753 | */ | 
|---|
| [23fd43] | 754 | bool FormatParser< tremolo >::isUsedField(const usedFields_t &fields, const std::string &fieldName) const | 
|---|
|  | 755 | { | 
|---|
| [b8d4a3] | 756 | bool fieldNameExists = false; | 
|---|
| [23fd43] | 757 | for (usedFields_t::const_iterator usedField = fields.begin(); | 
|---|
|  | 758 | usedField != fields.end(); usedField++) { | 
|---|
| [b8d4a3] | 759 | if (usedField->substr(0, usedField->find("=")) == fieldName) | 
|---|
|  | 760 | fieldNameExists = true; | 
|---|
|  | 761 | } | 
|---|
| [9131f3] | 762 |  | 
|---|
| [b8d4a3] | 763 | return fieldNameExists; | 
|---|
|  | 764 | } | 
|---|
|  | 765 |  | 
|---|
|  | 766 |  | 
|---|
|  | 767 | /** | 
|---|
|  | 768 | * Adds the collected neighbor information to the atoms in the world. The atoms | 
|---|
|  | 769 | * are found by their current ID and mapped to the corresponding atoms with the | 
|---|
|  | 770 | * Id found in the parsed file. | 
|---|
| [9f8b01] | 771 | * | 
|---|
|  | 772 | * @param atoms vector with all newly added (global) atomic ids | 
|---|
| [b8d4a3] | 773 | */ | 
|---|
| [9f8b01] | 774 | void FormatParser< tremolo >::processNeighborInformation(const std::vector<atomId_t> &atoms) { | 
|---|
| [23fd43] | 775 | if (!isUsedField(usedFields_load, "neighbors")) { | 
|---|
| [b8d4a3] | 776 | return; | 
|---|
|  | 777 | } | 
|---|
|  | 778 |  | 
|---|
| [9f8b01] | 779 | for (std::vector<atomId_t>::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) { | 
|---|
|  | 780 | ASSERT(additionalAtomData.count(*iter) != 0, | 
|---|
|  | 781 | "FormatParser< tremolo >::processNeighborInformation() - global id " | 
|---|
|  | 782 | +toString(*iter)+" unknown in additionalAtomData."); | 
|---|
|  | 783 | TremoloAtomInfoContainer ¤tInfo = additionalAtomData[*iter]; | 
|---|
|  | 784 | ASSERT (!currentInfo.neighbors_processed, | 
|---|
|  | 785 | "FormatParser< tremolo >::processNeighborInformation() - neighbors of new atom " | 
|---|
|  | 786 | +toString(*iter)+" are already processed."); | 
|---|
|  | 787 | for(std::vector<int>::const_iterator neighbor = currentInfo.neighbors.begin(); | 
|---|
|  | 788 | neighbor != currentInfo.neighbors.end(); neighbor++ | 
|---|
|  | 789 | ) { | 
|---|
|  | 790 | LOG(3, "INFO: Creating bond between (" | 
|---|
|  | 791 | << *iter | 
|---|
|  | 792 | << ") and (" | 
|---|
|  | 793 | << getGlobalId(*neighbor) << "|" << *neighbor << ")"); | 
|---|
|  | 794 | ASSERT(getGlobalId(*neighbor) != -1, | 
|---|
|  | 795 | "FormatParser< tremolo >::processNeighborInformation() - global id to local id " | 
|---|
|  | 796 | +toString(*neighbor)+" is unknown."); | 
|---|
|  | 797 | World::getInstance().getAtom(AtomById(*iter)) | 
|---|
|  | 798 | ->addBond(WorldTime::getTime(), World::getInstance().getAtom(AtomById(getGlobalId(*neighbor)))); | 
|---|
| [9131f3] | 799 | } | 
|---|
| [9f8b01] | 800 | currentInfo.neighbors_processed = true; | 
|---|
| [9131f3] | 801 | } | 
|---|
| [6bc51d] | 802 | } | 
|---|
|  | 803 |  | 
|---|
| [9131f3] | 804 | /** | 
|---|
| [b8d4a3] | 805 | * Replaces atom IDs read from the file by the corresponding world IDs. All IDs | 
|---|
|  | 806 | * IDs of the input string will be replaced; expected separating characters are | 
|---|
|  | 807 | * "-" and ",". | 
|---|
| [9131f3] | 808 | * | 
|---|
| [b8d4a3] | 809 | * \param string in which atom IDs should be adapted | 
|---|
|  | 810 | * | 
|---|
|  | 811 | * \return input string with modified atom IDs | 
|---|
| [9131f3] | 812 | */ | 
|---|
| [955b91] | 813 | std::string FormatParser< tremolo >::adaptIdDependentDataString(std::string data) { | 
|---|
| [b8d4a3] | 814 | // there might be no IDs | 
|---|
|  | 815 | if (data == "-") { | 
|---|
|  | 816 | return "-"; | 
|---|
|  | 817 | } | 
|---|
|  | 818 |  | 
|---|
|  | 819 | char separator; | 
|---|
|  | 820 | int id; | 
|---|
| [8bf9c6] | 821 | std::stringstream line, result; | 
|---|
| [b8d4a3] | 822 | line << data; | 
|---|
|  | 823 |  | 
|---|
|  | 824 | line >> id; | 
|---|
| [c0e28c] | 825 | result << getGlobalId(id); | 
|---|
| [b8d4a3] | 826 | while (line.good()) { | 
|---|
|  | 827 | line >> separator >> id; | 
|---|
| [c0e28c] | 828 | result << separator << getGlobalId(id); | 
|---|
| [b8d4a3] | 829 | } | 
|---|
|  | 830 |  | 
|---|
|  | 831 | return result.str(); | 
|---|
| [6bc51d] | 832 | } | 
|---|
| [b8d4a3] | 833 |  | 
|---|
|  | 834 | /** | 
|---|
|  | 835 | * Corrects the atom IDs in each imprData entry to the corresponding world IDs | 
|---|
|  | 836 | * as they might differ from the originally read IDs. | 
|---|
|  | 837 | */ | 
|---|
| [765f16] | 838 | void FormatParser< tremolo >::adaptImprData() { | 
|---|
| [23fd43] | 839 | if (!isUsedField(usedFields_load, "imprData")) { | 
|---|
| [b8d4a3] | 840 | return; | 
|---|
|  | 841 | } | 
|---|
|  | 842 |  | 
|---|
| [8bf9c6] | 843 | for(std::map<const atomId_t, TremoloAtomInfoContainer>::iterator currentInfo = additionalAtomData.begin(); | 
|---|
| [b8d4a3] | 844 | currentInfo != additionalAtomData.end(); currentInfo++ | 
|---|
|  | 845 | ) { | 
|---|
|  | 846 | currentInfo->second.imprData = adaptIdDependentDataString(currentInfo->second.imprData); | 
|---|
|  | 847 | } | 
|---|
| [6bc51d] | 848 | } | 
|---|
| [4415da] | 849 |  | 
|---|
| [b8d4a3] | 850 | /** | 
|---|
|  | 851 | * Corrects the atom IDs in each torsion entry to the corresponding world IDs | 
|---|
|  | 852 | * as they might differ from the originally read IDs. | 
|---|
|  | 853 | */ | 
|---|
| [765f16] | 854 | void FormatParser< tremolo >::adaptTorsion() { | 
|---|
| [23fd43] | 855 | if (!isUsedField(usedFields_load, "torsion")) { | 
|---|
| [b8d4a3] | 856 | return; | 
|---|
|  | 857 | } | 
|---|
|  | 858 |  | 
|---|
| [8bf9c6] | 859 | for(std::map<const atomId_t, TremoloAtomInfoContainer>::iterator currentInfo = additionalAtomData.begin(); | 
|---|
| [b8d4a3] | 860 | currentInfo != additionalAtomData.end(); currentInfo++ | 
|---|
|  | 861 | ) { | 
|---|
|  | 862 | currentInfo->second.torsion = adaptIdDependentDataString(currentInfo->second.torsion); | 
|---|
|  | 863 | } | 
|---|
|  | 864 | } | 
|---|
|  | 865 |  | 
|---|