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