| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-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 | * TremoloParser.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Mar 2, 2010 | 
|---|
| 12 | *      Author: metzler | 
|---|
| 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 "CodePatterns/Assert.hpp" | 
|---|
| 23 | #include "CodePatterns/Log.hpp" | 
|---|
| 24 | #include "CodePatterns/toString.hpp" | 
|---|
| 25 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 26 |  | 
|---|
| 27 | #include "TremoloParser.hpp" | 
|---|
| 28 |  | 
|---|
| 29 | #include "Atom/atom.hpp" | 
|---|
| 30 | #include "Bond/bond.hpp" | 
|---|
| 31 | #include "Descriptors/AtomIdDescriptor.hpp" | 
|---|
| 32 | #include "Element/element.hpp" | 
|---|
| 33 | #include "Element/periodentafel.hpp" | 
|---|
| 34 | #include "molecule.hpp" | 
|---|
| 35 | #include "MoleculeListClass.hpp" | 
|---|
| 36 | #include "World.hpp" | 
|---|
| 37 | #include "WorldTime.hpp" | 
|---|
| 38 |  | 
|---|
| 39 |  | 
|---|
| 40 | #include <algorithm> | 
|---|
| 41 | #include <boost/tokenizer.hpp> | 
|---|
| 42 | #include <iostream> | 
|---|
| 43 | #include <iomanip> | 
|---|
| 44 | #include <map> | 
|---|
| 45 | #include <sstream> | 
|---|
| 46 | #include <vector> | 
|---|
| 47 |  | 
|---|
| 48 | // declare specialized static variables | 
|---|
| 49 | const std::string FormatParserTrait<tremolo>::name = "tremolo"; | 
|---|
| 50 | const std::string FormatParserTrait<tremolo>::suffix = "data"; | 
|---|
| 51 | const ParserTypes FormatParserTrait<tremolo>::type = tremolo; | 
|---|
| 52 |  | 
|---|
| 53 | /** | 
|---|
| 54 | * Constructor. | 
|---|
| 55 | */ | 
|---|
| 56 | FormatParser< tremolo >::FormatParser()  : | 
|---|
| 57 | FormatParser_common(NULL) | 
|---|
| 58 | { | 
|---|
| 59 | knownKeys["x"] = TremoloKey::x; | 
|---|
| 60 | knownKeys["u"] = TremoloKey::u; | 
|---|
| 61 | knownKeys["F"] = TremoloKey::F; | 
|---|
| 62 | knownKeys["stress"] = TremoloKey::stress; | 
|---|
| 63 | knownKeys["Id"] = TremoloKey::Id; | 
|---|
| 64 | knownKeys["neighbors"] = TremoloKey::neighbors; | 
|---|
| 65 | knownKeys["imprData"] = TremoloKey::imprData; | 
|---|
| 66 | knownKeys["GroupMeasureTypeNo"] = TremoloKey::GroupMeasureTypeNo; | 
|---|
| 67 | knownKeys["type"] = TremoloKey::type; | 
|---|
| 68 | knownKeys["extType"] = TremoloKey::extType; | 
|---|
| 69 | knownKeys["name"] = TremoloKey::name; | 
|---|
| 70 | knownKeys["resName"] = TremoloKey::resName; | 
|---|
| 71 | knownKeys["chainID"] = TremoloKey::chainID; | 
|---|
| 72 | knownKeys["resSeq"] = TremoloKey::resSeq; | 
|---|
| 73 | knownKeys["occupancy"] = TremoloKey::occupancy; | 
|---|
| 74 | knownKeys["tempFactor"] = TremoloKey::tempFactor; | 
|---|
| 75 | knownKeys["segID"] = TremoloKey::segID; | 
|---|
| 76 | knownKeys["Charge"] = TremoloKey::Charge; | 
|---|
| 77 | knownKeys["charge"] = TremoloKey::charge; | 
|---|
| 78 | knownKeys["GrpTypeNo"] = TremoloKey::GrpTypeNo; | 
|---|
| 79 | knownKeys["torsion"] = TremoloKey::torsion; | 
|---|
| 80 |  | 
|---|
| 81 | createKnownTypesByIdentity(); | 
|---|
| 82 |  | 
|---|
| 83 | // default behavior: use all possible keys on output | 
|---|
| 84 | for (std::map<std::string, TremoloKey::atomDataKey>::iterator iter = knownKeys.begin(); iter != knownKeys.end(); ++iter) | 
|---|
| 85 | usedFields.push_back(iter->first); | 
|---|
| 86 |  | 
|---|
| 87 | // and noKey afterwards(!) such that it is not used in usedFields | 
|---|
| 88 | knownKeys[" "] = TremoloKey::noKey; // with this we can detect invalid keys | 
|---|
| 89 |  | 
|---|
| 90 | // invert knownKeys for debug output | 
|---|
| 91 | for (std::map<std::string, TremoloKey::atomDataKey>::iterator iter = knownKeys.begin(); iter != knownKeys.end(); ++iter) | 
|---|
| 92 | knownKeyNames.insert( make_pair( iter->second, iter->first) ); | 
|---|
| 93 |  | 
|---|
| 94 | additionalAtomData.clear(); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | /** | 
|---|
| 98 | * Destructor. | 
|---|
| 99 | */ | 
|---|
| 100 | FormatParser< tremolo >::~FormatParser() | 
|---|
| 101 | { | 
|---|
| 102 | LOG(1, "INFO: Clearing usedFields."); | 
|---|
| 103 | usedFields.clear(); | 
|---|
| 104 | additionalAtomData.clear(); | 
|---|
| 105 | knownKeys.clear(); | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | /** | 
|---|
| 109 | * Loads atoms from a tremolo-formatted file. | 
|---|
| 110 | * | 
|---|
| 111 | * \param tremolo file | 
|---|
| 112 | */ | 
|---|
| 113 | void FormatParser< tremolo >::load(istream* file) { | 
|---|
| 114 | std::string line; | 
|---|
| 115 | std::string::size_type location; | 
|---|
| 116 |  | 
|---|
| 117 | // reset the id maps | 
|---|
| 118 | resetIdAssociations(); | 
|---|
| 119 |  | 
|---|
| 120 | LOG(1, "INFO: Clearing usedFields."); | 
|---|
| 121 | usedFields.clear(); | 
|---|
| 122 |  | 
|---|
| 123 | molecule *newmol = World::getInstance().createMolecule(); | 
|---|
| 124 | newmol->ActiveFlag = true; | 
|---|
| 125 | // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include | 
|---|
| 126 | World::getInstance().getMolecules()->insert(newmol); | 
|---|
| 127 | while (file->good()) { | 
|---|
| 128 | std::getline(*file, line, '\n'); | 
|---|
| 129 | if (usedFields.empty()) { | 
|---|
| 130 | location = line.find("ATOMDATA", 0); | 
|---|
| 131 | if (location != string::npos) { | 
|---|
| 132 | parseAtomDataKeysLine(line, location + 8); | 
|---|
| 133 | } | 
|---|
| 134 | } | 
|---|
| 135 | if (line.length() > 0 && line.at(0) != '#') { | 
|---|
| 136 | readAtomDataLine(line, newmol); | 
|---|
| 137 | } | 
|---|
| 138 | } | 
|---|
| 139 | LOG(3, "usedFields after load contains: " << usedFields); | 
|---|
| 140 |  | 
|---|
| 141 | // refresh atom::nr and atom::name | 
|---|
| 142 | std::vector<atomId_t> atoms(newmol->getAtomCount()); | 
|---|
| 143 | std::transform(newmol->begin(), newmol->end(), atoms.begin(), | 
|---|
| 144 | boost::bind(&atom::getId, _1)); | 
|---|
| 145 | processNeighborInformation(atoms); | 
|---|
| 146 | adaptImprData(); | 
|---|
| 147 | adaptTorsion(); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | /** | 
|---|
| 151 | * Saves the \a atoms into as a tremolo file. | 
|---|
| 152 | * | 
|---|
| 153 | * \param file where to save the state | 
|---|
| 154 | * \param atoms atoms to store | 
|---|
| 155 | */ | 
|---|
| 156 | void FormatParser< tremolo >::save(ostream* file, const std::vector<atom *> &AtomList) { | 
|---|
| 157 | LOG(0, "Saving changes to tremolo."); | 
|---|
| 158 |  | 
|---|
| 159 | std::vector<atom*>::const_iterator atomIt; | 
|---|
| 160 | /*vector<string>::iterator it;*/ | 
|---|
| 161 | std::vector<std::string>::iterator it = unique(usedFields.begin(), usedFields.end()); // skips all duplicates in the vector | 
|---|
| 162 |  | 
|---|
| 163 | LOG(3, "INFO: usedFields before save contains: " << usedFields); | 
|---|
| 164 | //LOG(3, "INFO: additionalAtomData contains: " << additionalAtomData); | 
|---|
| 165 |  | 
|---|
| 166 | // distribute continuous indices | 
|---|
| 167 | resetIdAssociations(); | 
|---|
| 168 | atomId_t lastid = 0; | 
|---|
| 169 | for (atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) { | 
|---|
| 170 | associateLocaltoGlobalId(++lastid, (*atomIt)->getId()); | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | // store | 
|---|
| 174 | *file << "# ATOMDATA"; | 
|---|
| 175 | for (it=usedFields.begin(); it < usedFields.end(); it++) { | 
|---|
| 176 | *file << "\t" << *it; | 
|---|
| 177 | } | 
|---|
| 178 | *file << endl; | 
|---|
| 179 | for (atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) { | 
|---|
| 180 | saveLine(file, *atomIt); | 
|---|
| 181 | } | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | /** Add default info, when new atom is added to World. | 
|---|
| 185 | * | 
|---|
| 186 | * @param id of atom | 
|---|
| 187 | */ | 
|---|
| 188 | void FormatParser< tremolo >::AtomInserted(atomId_t id) | 
|---|
| 189 | { | 
|---|
| 190 | std::map<const atomId_t, TremoloAtomInfoContainer>::iterator iter = additionalAtomData.find(id); | 
|---|
| 191 | ASSERT(iter == additionalAtomData.end(), | 
|---|
| 192 | "FormatParser< tremolo >::AtomInserted() - additionalAtomData already present for newly added atom " | 
|---|
| 193 | +toString(id)+"."); | 
|---|
| 194 | // don't add entry, as this gives a default resSeq of 0 not the molecule id | 
|---|
| 195 | // additionalAtomData.insert( std::make_pair(id, TremoloAtomInfoContainer()) ); | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | /** Remove additional AtomData info, when atom has been removed from World. | 
|---|
| 199 | * | 
|---|
| 200 | * @param id of atom | 
|---|
| 201 | */ | 
|---|
| 202 | void FormatParser< tremolo >::AtomRemoved(atomId_t id) | 
|---|
| 203 | { | 
|---|
| 204 | std::map<const atomId_t, TremoloAtomInfoContainer>::iterator iter = additionalAtomData.find(id); | 
|---|
| 205 | // as we do not insert AtomData on AtomInserted, we cannot be assured of its presence | 
|---|
| 206 | //  ASSERT(iter != additionalAtomData.end(), | 
|---|
| 207 | //      "FormatParser< tremolo >::AtomRemoved() - additionalAtomData is not present for atom " | 
|---|
| 208 | //      +toString(id)+" to remove."); | 
|---|
| 209 | if (iter != additionalAtomData.end()) | 
|---|
| 210 | additionalAtomData.erase(iter); | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | /** | 
|---|
| 214 | * Sets the keys for which data should be written to the stream when save is | 
|---|
| 215 | * called. | 
|---|
| 216 | * | 
|---|
| 217 | * \param string of field names with the same syntax as for an ATOMDATA line | 
|---|
| 218 | *        but without the prexix "ATOMDATA" | 
|---|
| 219 | */ | 
|---|
| 220 | void FormatParser< tremolo >::setFieldsForSave(std::string atomDataLine) { | 
|---|
| 221 | parseAtomDataKeysLine(atomDataLine, 0); | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|
| 224 |  | 
|---|
| 225 | /** | 
|---|
| 226 | * Writes one line of tremolo-formatted data to the provided stream. | 
|---|
| 227 | * | 
|---|
| 228 | * \param stream where to write the line to | 
|---|
| 229 | * \param reference to the atom of which information should be written | 
|---|
| 230 | */ | 
|---|
| 231 | void FormatParser< tremolo >::saveLine(ostream* file, atom* currentAtom) { | 
|---|
| 232 | std::vector<string>::iterator it; | 
|---|
| 233 |  | 
|---|
| 234 | TremoloKey::atomDataKey currentField; | 
|---|
| 235 |  | 
|---|
| 236 | LOG(4, "INFO: Saving atom " << *currentAtom << ", its father id is " << currentAtom->GetTrueFather()->getId()); | 
|---|
| 237 |  | 
|---|
| 238 | for (it = usedFields.begin(); it != usedFields.end(); it++) { | 
|---|
| 239 | currentField = knownKeys[it->substr(0, it->find("="))]; | 
|---|
| 240 | switch (currentField) { | 
|---|
| 241 | case TremoloKey::x : | 
|---|
| 242 | // for the moment, assume there are always three dimensions | 
|---|
| 243 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getPosition()); | 
|---|
| 244 | *file << currentAtom->at(0) << "\t"; | 
|---|
| 245 | *file << currentAtom->at(1) << "\t"; | 
|---|
| 246 | *file << currentAtom->at(2) << "\t"; | 
|---|
| 247 | break; | 
|---|
| 248 | case TremoloKey::u : | 
|---|
| 249 | // for the moment, assume there are always three dimensions | 
|---|
| 250 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getAtomicVelocity()); | 
|---|
| 251 | *file << currentAtom->getAtomicVelocity()[0] << "\t"; | 
|---|
| 252 | *file << currentAtom->getAtomicVelocity()[1] << "\t"; | 
|---|
| 253 | *file << currentAtom->getAtomicVelocity()[2] << "\t"; | 
|---|
| 254 | break; | 
|---|
| 255 | case TremoloKey::type : | 
|---|
| 256 | if (additionalAtomData.count(currentAtom->getId())) { | 
|---|
| 257 | if (additionalAtomData[currentAtom->getId()].get(currentField) != "-") { | 
|---|
| 258 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField)); | 
|---|
| 259 | *file << additionalAtomData[currentAtom->getId()].get(currentField) << "\t"; | 
|---|
| 260 | } else { | 
|---|
| 261 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " default value: " << currentAtom->getType()->getSymbol()); | 
|---|
| 262 | *file << currentAtom->getType()->getSymbol() << "\t"; | 
|---|
| 263 | } | 
|---|
| 264 | } else if (additionalAtomData.count(currentAtom->GetTrueFather()->getId())) { | 
|---|
| 265 | if (additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField) != "-") { | 
|---|
| 266 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " stuff from father: " << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField)); | 
|---|
| 267 | *file << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField) << "\t"; | 
|---|
| 268 | } else { | 
|---|
| 269 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " default value from father: " << currentAtom->GetTrueFather()->getType()->getSymbol()); | 
|---|
| 270 | *file << currentAtom->GetTrueFather()->getType()->getSymbol() << "\t"; | 
|---|
| 271 | } | 
|---|
| 272 | } else { | 
|---|
| 273 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " its default value: " << currentAtom->getType()->getSymbol()); | 
|---|
| 274 | *file << currentAtom->getType()->getSymbol() << "\t"; | 
|---|
| 275 | } | 
|---|
| 276 | break; | 
|---|
| 277 | case TremoloKey::Id : | 
|---|
| 278 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getId()+1); | 
|---|
| 279 | *file << getLocalId(currentAtom->getId()) << "\t"; | 
|---|
| 280 | break; | 
|---|
| 281 | case TremoloKey::neighbors : | 
|---|
| 282 | LOG(3, "Writing type " << knownKeyNames[currentField]); | 
|---|
| 283 | writeNeighbors(file, atoi(it->substr(it->find("=") + 1, 1).c_str()), currentAtom); | 
|---|
| 284 | break; | 
|---|
| 285 | case TremoloKey::resSeq : | 
|---|
| 286 | if (additionalAtomData.count(currentAtom->getId())) { | 
|---|
| 287 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField)); | 
|---|
| 288 | *file << additionalAtomData[currentAtom->getId()].get(currentField); | 
|---|
| 289 | } else if (currentAtom->getMolecule() != NULL) { | 
|---|
| 290 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " its own id: " << currentAtom->getMolecule()->getId()+1); | 
|---|
| 291 | *file << setw(4) << currentAtom->getMolecule()->getId()+1; | 
|---|
| 292 | } else { | 
|---|
| 293 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " default value: " << defaultAdditionalData.get(currentField)); | 
|---|
| 294 | *file << defaultAdditionalData.get(currentField); | 
|---|
| 295 | } | 
|---|
| 296 | *file << "\t"; | 
|---|
| 297 | break; | 
|---|
| 298 | default : | 
|---|
| 299 | if (additionalAtomData.count(currentAtom->getId())) { | 
|---|
| 300 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField)); | 
|---|
| 301 | *file << additionalAtomData[currentAtom->getId()].get(currentField); | 
|---|
| 302 | } else if (additionalAtomData.count(currentAtom->GetTrueFather()->getId())) { | 
|---|
| 303 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " stuff from father: " << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField)); | 
|---|
| 304 | *file << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField); | 
|---|
| 305 | } else { | 
|---|
| 306 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " the default: " << defaultAdditionalData.get(currentField)); | 
|---|
| 307 | *file << defaultAdditionalData.get(currentField); | 
|---|
| 308 | } | 
|---|
| 309 | *file << "\t"; | 
|---|
| 310 | break; | 
|---|
| 311 | } | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | *file << endl; | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 | /** | 
|---|
| 318 | * Writes the neighbor information of one atom to the provided stream. | 
|---|
| 319 | * | 
|---|
| 320 | * Note that ListOfBonds of WorldTime::CurrentTime is used. | 
|---|
| 321 | * | 
|---|
| 322 | * \param stream where to write neighbor information to | 
|---|
| 323 | * \param number of neighbors | 
|---|
| 324 | * \param reference to the atom of which to take the neighbor information | 
|---|
| 325 | */ | 
|---|
| 326 | void FormatParser< tremolo >::writeNeighbors(ostream* file, int numberOfNeighbors, atom* currentAtom) { | 
|---|
| 327 | const BondList& ListOfBonds = currentAtom->getListOfBonds(); | 
|---|
| 328 | // sort bonded indices | 
|---|
| 329 | typedef std::set<atomId_t> sortedIndices; | 
|---|
| 330 | sortedIndices sortedBonds; | 
|---|
| 331 | for (BondList::const_iterator iter = ListOfBonds.begin(); | 
|---|
| 332 | iter != ListOfBonds.end(); ++iter) | 
|---|
| 333 | sortedBonds.insert(getLocalId((*iter)->GetOtherAtom(currentAtom)->getId())); | 
|---|
| 334 | // print indices | 
|---|
| 335 | sortedIndices::const_iterator currentBond = sortedBonds.begin(); | 
|---|
| 336 | for (int i = 0; i < numberOfNeighbors; i++) { | 
|---|
| 337 | *file << (currentBond != sortedBonds.end() ? (*currentBond) : 0) << "\t"; | 
|---|
| 338 | if (currentBond != sortedBonds.end()) | 
|---|
| 339 | ++currentBond; | 
|---|
| 340 | } | 
|---|
| 341 | } | 
|---|
| 342 |  | 
|---|
| 343 | /** | 
|---|
| 344 | * Stores keys from the ATOMDATA line. | 
|---|
| 345 | * | 
|---|
| 346 | * \param line to parse the keys from | 
|---|
| 347 | * \param with which offset the keys begin within the line | 
|---|
| 348 | */ | 
|---|
| 349 | void FormatParser< tremolo >::parseAtomDataKeysLine(std::string line, int offset) { | 
|---|
| 350 | std::string keyword; | 
|---|
| 351 | std::stringstream lineStream; | 
|---|
| 352 |  | 
|---|
| 353 | lineStream << line.substr(offset); | 
|---|
| 354 | LOG(1, "INFO: Clearing usedFields in parseAtomDataKeysLine."); | 
|---|
| 355 | usedFields.clear(); | 
|---|
| 356 | while (lineStream.good()) { | 
|---|
| 357 | lineStream >> keyword; | 
|---|
| 358 | if (knownKeys[keyword.substr(0, keyword.find("="))] == TremoloKey::noKey) { | 
|---|
| 359 | // TODO: throw exception about unknown key | 
|---|
| 360 | cout << "Unknown key: " << keyword << " is not part of the tremolo format specification." << endl; | 
|---|
| 361 | break; | 
|---|
| 362 | } | 
|---|
| 363 | usedFields.push_back(keyword); | 
|---|
| 364 | } | 
|---|
| 365 | //LOG(1, "INFO: " << usedFields); | 
|---|
| 366 | } | 
|---|
| 367 |  | 
|---|
| 368 | /** Sets the properties per atom to print to .data file by parsing line from | 
|---|
| 369 | *  \a atomdata_string. | 
|---|
| 370 | * | 
|---|
| 371 | *  We just call \sa  FormatParser< tremolo >::parseAtomDataKeysLine() which is left | 
|---|
| 372 | *  private., | 
|---|
| 373 | * | 
|---|
| 374 | * @param atomdata_string line to parse with space-separated values | 
|---|
| 375 | */ | 
|---|
| 376 | void FormatParser< tremolo >::setAtomData(const std::string &atomdata_string) | 
|---|
| 377 | { | 
|---|
| 378 | parseAtomDataKeysLine(atomdata_string, 0); | 
|---|
| 379 | } | 
|---|
| 380 |  | 
|---|
| 381 |  | 
|---|
| 382 | /** | 
|---|
| 383 | * Reads one data line of a tremolo file and interprets it according to the keys | 
|---|
| 384 | * obtained from the ATOMDATA line. | 
|---|
| 385 | * | 
|---|
| 386 | * \param line to parse as an atom | 
|---|
| 387 | * \param *newmol molecule to add atom to | 
|---|
| 388 | */ | 
|---|
| 389 | void FormatParser< tremolo >::readAtomDataLine(std::string line, molecule *newmol = NULL) { | 
|---|
| 390 | std::vector<string>::iterator it; | 
|---|
| 391 | std::stringstream lineStream; | 
|---|
| 392 | atom* newAtom = World::getInstance().createAtom(); | 
|---|
| 393 | const atomId_t atomid = newAtom->getId(); | 
|---|
| 394 | additionalAtomData[atomid] = TremoloAtomInfoContainer(); // fill with default values | 
|---|
| 395 | TremoloAtomInfoContainer *atomInfo = &additionalAtomData[atomid]; | 
|---|
| 396 | TremoloKey::atomDataKey currentField; | 
|---|
| 397 | ConvertTo<double> toDouble; | 
|---|
| 398 | ConvertTo<int> toInt; | 
|---|
| 399 | Vector tempVector; | 
|---|
| 400 |  | 
|---|
| 401 | // setup tokenizer, splitting up white-spaced entries | 
|---|
| 402 | typedef boost::tokenizer<boost::char_separator<char> > | 
|---|
| 403 | tokenizer; | 
|---|
| 404 | boost::char_separator<char> whitespacesep(" \t"); | 
|---|
| 405 | tokenizer tokens(line, whitespacesep); | 
|---|
| 406 | ASSERT(tokens.begin() != tokens.end(), | 
|---|
| 407 | "FormatParser< tremolo >::readAtomDataLine - empty string, need at least ' '!"); | 
|---|
| 408 | tokenizer::iterator tok_iter = tokens.begin(); | 
|---|
| 409 | // then associate each token to each file | 
|---|
| 410 | for (it = usedFields.begin(); it < usedFields.end(); it++) { | 
|---|
| 411 | const std::string keyName = it->substr(0, it->find("=")); | 
|---|
| 412 | currentField = knownKeys[keyName]; | 
|---|
| 413 | const std::string word = *tok_iter; | 
|---|
| 414 | LOG(4, "INFO: Parsing key " << keyName << " with remaining data " << word); | 
|---|
| 415 | switch (currentField) { | 
|---|
| 416 | case TremoloKey::x : | 
|---|
| 417 | // for the moment, assume there are always three dimensions | 
|---|
| 418 | for (int i=0;i<NDIM;i++) { | 
|---|
| 419 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for x["+toString(i)+"]!"); | 
|---|
| 420 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter); | 
|---|
| 421 | newAtom->set(i, toDouble(*tok_iter)); | 
|---|
| 422 | tok_iter++; | 
|---|
| 423 | } | 
|---|
| 424 | break; | 
|---|
| 425 | case TremoloKey::u : | 
|---|
| 426 | // for the moment, assume there are always three dimensions | 
|---|
| 427 | for (int i=0;i<NDIM;i++) { | 
|---|
| 428 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for u["+toString(i)+"]!"); | 
|---|
| 429 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter); | 
|---|
| 430 | tempVector[i] = toDouble(*tok_iter); | 
|---|
| 431 | tok_iter++; | 
|---|
| 432 | } | 
|---|
| 433 | newAtom->setAtomicVelocity(tempVector); | 
|---|
| 434 | break; | 
|---|
| 435 | case TremoloKey::type : | 
|---|
| 436 | { | 
|---|
| 437 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
| 438 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter); | 
|---|
| 439 | std::string element(knownTypes[(*tok_iter)]); | 
|---|
| 440 | // put type name into container for later use | 
|---|
| 441 | atomInfo->set(currentField, *tok_iter); | 
|---|
| 442 | LOG(4, "INFO: Parsing element " << (*tok_iter) << " as " << element << " according to KnownTypes."); | 
|---|
| 443 | tok_iter++; | 
|---|
| 444 | newAtom->setType(World::getInstance().getPeriode()->FindElement(element)); | 
|---|
| 445 | ASSERT(newAtom->getType(), "Type was not set for this atom"); | 
|---|
| 446 | break; | 
|---|
| 447 | } | 
|---|
| 448 | case TremoloKey::Id : | 
|---|
| 449 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
| 450 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter); | 
|---|
| 451 | associateLocaltoGlobalId(toInt(*tok_iter), atomid); | 
|---|
| 452 | tok_iter++; | 
|---|
| 453 | break; | 
|---|
| 454 | case TremoloKey::neighbors : | 
|---|
| 455 | for (int i=0;i<atoi(it->substr(it->find("=") + 1, 1).c_str());i++) { | 
|---|
| 456 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
| 457 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter); | 
|---|
| 458 | lineStream << *tok_iter << "\t"; | 
|---|
| 459 | tok_iter++; | 
|---|
| 460 | } | 
|---|
| 461 | readNeighbors(&lineStream, | 
|---|
| 462 | atoi(it->substr(it->find("=") + 1, 1).c_str()), atomid); | 
|---|
| 463 | break; | 
|---|
| 464 | default : | 
|---|
| 465 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
| 466 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter); | 
|---|
| 467 | atomInfo->set(currentField, *tok_iter); | 
|---|
| 468 | tok_iter++; | 
|---|
| 469 | break; | 
|---|
| 470 | } | 
|---|
| 471 | } | 
|---|
| 472 | LOG(3, "INFO: Parsed atom " << atomid << "."); | 
|---|
| 473 | if (newmol != NULL) | 
|---|
| 474 | newmol->AddAtom(newAtom); | 
|---|
| 475 | } | 
|---|
| 476 |  | 
|---|
| 477 | /** | 
|---|
| 478 | * Reads neighbor information for one atom from the input. | 
|---|
| 479 | * | 
|---|
| 480 | * \param line stream where to read the information from | 
|---|
| 481 | * \param numberOfNeighbors number of neighbors to read | 
|---|
| 482 | * \param atomid world id of the atom the information belongs to | 
|---|
| 483 | */ | 
|---|
| 484 | void FormatParser< tremolo >::readNeighbors(std::stringstream* line, int numberOfNeighbors, int atomId) { | 
|---|
| 485 | int neighborId = 0; | 
|---|
| 486 | for (int i = 0; i < numberOfNeighbors; i++) { | 
|---|
| 487 | *line >> neighborId; | 
|---|
| 488 | // 0 is used to fill empty neighbor positions in the tremolo file. | 
|---|
| 489 | if (neighborId > 0) { | 
|---|
| 490 | LOG(4, "INFO: Atom with global id " << atomId | 
|---|
| 491 | << " has neighbour with serial " << neighborId); | 
|---|
| 492 | additionalAtomData[atomId].neighbors.push_back(neighborId); | 
|---|
| 493 | } | 
|---|
| 494 | } | 
|---|
| 495 | } | 
|---|
| 496 |  | 
|---|
| 497 | /** | 
|---|
| 498 | * Checks whether the provided name is within the list of used fields. | 
|---|
| 499 | * | 
|---|
| 500 | * \param field name to check | 
|---|
| 501 | * | 
|---|
| 502 | * \return true if the field name is used | 
|---|
| 503 | */ | 
|---|
| 504 | bool FormatParser< tremolo >::isUsedField(std::string fieldName) { | 
|---|
| 505 | bool fieldNameExists = false; | 
|---|
| 506 | for (std::vector<std::string>::iterator usedField = usedFields.begin(); usedField != usedFields.end(); usedField++) { | 
|---|
| 507 | if (usedField->substr(0, usedField->find("=")) == fieldName) | 
|---|
| 508 | fieldNameExists = true; | 
|---|
| 509 | } | 
|---|
| 510 |  | 
|---|
| 511 | return fieldNameExists; | 
|---|
| 512 | } | 
|---|
| 513 |  | 
|---|
| 514 |  | 
|---|
| 515 | /** | 
|---|
| 516 | * Adds the collected neighbor information to the atoms in the world. The atoms | 
|---|
| 517 | * are found by their current ID and mapped to the corresponding atoms with the | 
|---|
| 518 | * Id found in the parsed file. | 
|---|
| 519 | * | 
|---|
| 520 | * @param atoms vector with all newly added (global) atomic ids | 
|---|
| 521 | */ | 
|---|
| 522 | void FormatParser< tremolo >::processNeighborInformation(const std::vector<atomId_t> &atoms) { | 
|---|
| 523 | if (!isUsedField("neighbors")) { | 
|---|
| 524 | return; | 
|---|
| 525 | } | 
|---|
| 526 |  | 
|---|
| 527 | for (std::vector<atomId_t>::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) { | 
|---|
| 528 | ASSERT(additionalAtomData.count(*iter) != 0, | 
|---|
| 529 | "FormatParser< tremolo >::processNeighborInformation() - global id " | 
|---|
| 530 | +toString(*iter)+" unknown in additionalAtomData."); | 
|---|
| 531 | TremoloAtomInfoContainer ¤tInfo = additionalAtomData[*iter]; | 
|---|
| 532 | ASSERT (!currentInfo.neighbors_processed, | 
|---|
| 533 | "FormatParser< tremolo >::processNeighborInformation() - neighbors of new atom " | 
|---|
| 534 | +toString(*iter)+" are already processed."); | 
|---|
| 535 | for(std::vector<int>::const_iterator neighbor = currentInfo.neighbors.begin(); | 
|---|
| 536 | neighbor != currentInfo.neighbors.end(); neighbor++ | 
|---|
| 537 | ) { | 
|---|
| 538 | LOG(3, "INFO: Creating bond between (" | 
|---|
| 539 | << *iter | 
|---|
| 540 | << ") and (" | 
|---|
| 541 | << getGlobalId(*neighbor) << "|" << *neighbor << ")"); | 
|---|
| 542 | ASSERT(getGlobalId(*neighbor) != -1, | 
|---|
| 543 | "FormatParser< tremolo >::processNeighborInformation() - global id to local id " | 
|---|
| 544 | +toString(*neighbor)+" is unknown."); | 
|---|
| 545 | World::getInstance().getAtom(AtomById(*iter)) | 
|---|
| 546 | ->addBond(WorldTime::getTime(), World::getInstance().getAtom(AtomById(getGlobalId(*neighbor)))); | 
|---|
| 547 | } | 
|---|
| 548 | currentInfo.neighbors_processed = true; | 
|---|
| 549 | } | 
|---|
| 550 | } | 
|---|
| 551 |  | 
|---|
| 552 | /** | 
|---|
| 553 | * Replaces atom IDs read from the file by the corresponding world IDs. All IDs | 
|---|
| 554 | * IDs of the input string will be replaced; expected separating characters are | 
|---|
| 555 | * "-" and ",". | 
|---|
| 556 | * | 
|---|
| 557 | * \param string in which atom IDs should be adapted | 
|---|
| 558 | * | 
|---|
| 559 | * \return input string with modified atom IDs | 
|---|
| 560 | */ | 
|---|
| 561 | std::string FormatParser< tremolo >::adaptIdDependentDataString(std::string data) { | 
|---|
| 562 | // there might be no IDs | 
|---|
| 563 | if (data == "-") { | 
|---|
| 564 | return "-"; | 
|---|
| 565 | } | 
|---|
| 566 |  | 
|---|
| 567 | char separator; | 
|---|
| 568 | int id; | 
|---|
| 569 | std::stringstream line, result; | 
|---|
| 570 | line << data; | 
|---|
| 571 |  | 
|---|
| 572 | line >> id; | 
|---|
| 573 | result << getGlobalId(id); | 
|---|
| 574 | while (line.good()) { | 
|---|
| 575 | line >> separator >> id; | 
|---|
| 576 | result << separator << getGlobalId(id); | 
|---|
| 577 | } | 
|---|
| 578 |  | 
|---|
| 579 | return result.str(); | 
|---|
| 580 | } | 
|---|
| 581 |  | 
|---|
| 582 | /** | 
|---|
| 583 | * Corrects the atom IDs in each imprData entry to the corresponding world IDs | 
|---|
| 584 | * as they might differ from the originally read IDs. | 
|---|
| 585 | */ | 
|---|
| 586 | void FormatParser< tremolo >::adaptImprData() { | 
|---|
| 587 | if (!isUsedField("imprData")) { | 
|---|
| 588 | return; | 
|---|
| 589 | } | 
|---|
| 590 |  | 
|---|
| 591 | for(std::map<const atomId_t, TremoloAtomInfoContainer>::iterator currentInfo = additionalAtomData.begin(); | 
|---|
| 592 | currentInfo != additionalAtomData.end(); currentInfo++ | 
|---|
| 593 | ) { | 
|---|
| 594 | currentInfo->second.imprData = adaptIdDependentDataString(currentInfo->second.imprData); | 
|---|
| 595 | } | 
|---|
| 596 | } | 
|---|
| 597 |  | 
|---|
| 598 | /** | 
|---|
| 599 | * Corrects the atom IDs in each torsion entry to the corresponding world IDs | 
|---|
| 600 | * as they might differ from the originally read IDs. | 
|---|
| 601 | */ | 
|---|
| 602 | void FormatParser< tremolo >::adaptTorsion() { | 
|---|
| 603 | if (!isUsedField("torsion")) { | 
|---|
| 604 | return; | 
|---|
| 605 | } | 
|---|
| 606 |  | 
|---|
| 607 | for(std::map<const atomId_t, TremoloAtomInfoContainer>::iterator currentInfo = additionalAtomData.begin(); | 
|---|
| 608 | currentInfo != additionalAtomData.end(); currentInfo++ | 
|---|
| 609 | ) { | 
|---|
| 610 | currentInfo->second.torsion = adaptIdDependentDataString(currentInfo->second.torsion); | 
|---|
| 611 | } | 
|---|
| 612 | } | 
|---|
| 613 |  | 
|---|
| 614 | /** Creates knownTypes as the identity, e.g. H -> H, He -> He, ... . | 
|---|
| 615 | * | 
|---|
| 616 | */ | 
|---|
| 617 | void FormatParser< tremolo >::createKnownTypesByIdentity() | 
|---|
| 618 | { | 
|---|
| 619 | // remove old mapping | 
|---|
| 620 | knownTypes.clear(); | 
|---|
| 621 | // make knownTypes the identity mapping | 
|---|
| 622 | const periodentafel *periode = World::getInstance().getPeriode(); | 
|---|
| 623 | for (periodentafel::const_iterator iter = periode->begin(); | 
|---|
| 624 | iter != periode->end(); | 
|---|
| 625 | ++iter) { | 
|---|
| 626 | knownTypes.insert( make_pair(iter->second->getSymbol(), iter->second->getSymbol()) ); | 
|---|
| 627 | } | 
|---|
| 628 | } | 
|---|
| 629 |  | 
|---|
| 630 | /** Parses a .potentials file and creates from it the knownTypes file. | 
|---|
| 631 | * | 
|---|
| 632 | * @param file input stream of .potentials file | 
|---|
| 633 | */ | 
|---|
| 634 | void FormatParser< tremolo >::parseKnownTypes(std::istream &file) | 
|---|
| 635 | { | 
|---|
| 636 | const periodentafel *periode = World::getInstance().getPeriode(); | 
|---|
| 637 | // remove old mapping | 
|---|
| 638 | knownTypes.clear(); | 
|---|
| 639 |  | 
|---|
| 640 | //  LOG(3, "INFO: additionalAtomData contains: " << additionalAtomData); | 
|---|
| 641 |  | 
|---|
| 642 | // parse in file | 
|---|
| 643 | typedef boost::tokenizer<boost::char_separator<char> > | 
|---|
| 644 | tokenizer; | 
|---|
| 645 | boost::char_separator<char> tokensep(":\t ,;"); | 
|---|
| 646 | boost::char_separator<char> equalitysep("\t ="); | 
|---|
| 647 | std::string line; | 
|---|
| 648 | while (file.good()) { | 
|---|
| 649 | std::getline( file, line ); | 
|---|
| 650 | LOG(4, "INFO: full line of parameters is '" << line << "'"); | 
|---|
| 651 | if (line.find("particle:") != string::npos) { | 
|---|
| 652 | LOG(3, "INFO: found line '" << line << "' containing keyword 'particle:'."); | 
|---|
| 653 | tokenizer tokens(line, tokensep); | 
|---|
| 654 | ASSERT(tokens.begin() != tokens.end(), | 
|---|
| 655 | "FormatParser< tremolo >::parseKnownTypes() - line with 'particle:' but no particles separated by comma."); | 
|---|
| 656 | // look for particle_type | 
|---|
| 657 | std::string particle_type("NULL"); | 
|---|
| 658 | std::string element_type("NULL"); | 
|---|
| 659 | for (tokenizer::iterator tok_iter = tokens.begin(); | 
|---|
| 660 | tok_iter != tokens.end(); | 
|---|
| 661 | ++tok_iter) { | 
|---|
| 662 | if ((*tok_iter).find("particle_type") != string::npos) { | 
|---|
| 663 | LOG(3, "INFO: found token '" << *tok_iter << "' containing keyword 'particle_type'."); | 
|---|
| 664 | tokenizer token((*tok_iter), equalitysep); | 
|---|
| 665 | ASSERT(token.begin() != token.end(), | 
|---|
| 666 | "FormatParser< tremolo >::parseKnownTypes() - could not split particle_type by equality sign"); | 
|---|
| 667 | tokenizer::iterator particle_iter = token.begin(); | 
|---|
| 668 | particle_iter++; | 
|---|
| 669 | particle_type = *particle_iter; | 
|---|
| 670 | } | 
|---|
| 671 | if ((*tok_iter).find("element_name") != string::npos) { | 
|---|
| 672 | LOG(3, "INFO: found token '" << *tok_iter << "' containing keyword 'element_name'."); | 
|---|
| 673 | tokenizer token((*tok_iter), equalitysep); | 
|---|
| 674 | ASSERT(token.begin() != token.end(), | 
|---|
| 675 | "FormatParser< tremolo >::parseKnownTypes() - could not split particle_type by equality sign"); | 
|---|
| 676 | tokenizer::iterator element_iter = token.begin(); | 
|---|
| 677 | element_iter++; | 
|---|
| 678 | element_type = *element_iter; | 
|---|
| 679 | } | 
|---|
| 680 | } | 
|---|
| 681 | if ((particle_type != "NULL") && (element_type != "NULL")) { | 
|---|
| 682 | if (periode->FindElement(element_type) != NULL) { | 
|---|
| 683 | LOG(1, "INFO: Added Type " << particle_type << " as reference to element " << element_type << "."); | 
|---|
| 684 | knownTypes.insert( make_pair (particle_type, element_type) ); | 
|---|
| 685 | } else { | 
|---|
| 686 | ELOG(1, "INFO: Either Type " << particle_type << " or " << element_type << " could not be recognized." ); | 
|---|
| 687 | } | 
|---|
| 688 | } else { | 
|---|
| 689 | ELOG(3, "Line does not contain both 'particle_type' and 'element_name' as keys." ); | 
|---|
| 690 | } | 
|---|
| 691 | } | 
|---|
| 692 | } | 
|---|
| 693 |  | 
|---|
| 694 | } | 
|---|